Rotate gdb/ChangeLog (start of New Year procedure)
[binutils-gdb.git] / gdb / inferior.c
blob87df193ac392eef2fc2882c1681611057ddc6b07
1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "exec.h"
22 #include "inferior.h"
23 #include "target.h"
24 #include "command.h"
25 #include "completer.h"
26 #include "gdbcmd.h"
27 #include "gdbthread.h"
28 #include "ui-out.h"
29 #include "observable.h"
30 #include "gdbcore.h"
31 #include "symfile.h"
32 #include "gdbsupport/environ.h"
33 #include "cli/cli-utils.h"
34 #include "continuations.h"
35 #include "arch-utils.h"
36 #include "target-descriptions.h"
37 #include "readline/tilde.h"
38 #include "progspace-and-thread.h"
40 /* Keep a registry of per-inferior data-pointers required by other GDB
41 modules. */
43 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
45 struct inferior *inferior_list = NULL;
46 static int highest_inferior_num;
48 /* See inferior.h. */
49 bool print_inferior_events = true;
51 /* The Current Inferior. This is a strong reference. I.e., whenever
52 an inferior is the current inferior, its refcount is
53 incremented. */
54 static struct inferior *current_inferior_ = NULL;
56 struct inferior*
57 current_inferior (void)
59 return current_inferior_;
62 void
63 set_current_inferior (struct inferior *inf)
65 /* There's always an inferior. */
66 gdb_assert (inf != NULL);
68 inf->incref ();
69 current_inferior_->decref ();
70 current_inferior_ = inf;
73 private_inferior::~private_inferior () = default;
75 inferior::~inferior ()
77 inferior *inf = this;
79 discard_all_inferior_continuations (inf);
80 inferior_free_data (inf);
81 xfree (inf->args);
82 xfree (inf->terminal);
83 target_desc_info_free (inf->tdesc_info);
86 inferior::inferior (int pid_)
87 : num (++highest_inferior_num),
88 pid (pid_),
89 environment (gdb_environ::from_host_environ ()),
90 registry_data ()
92 inferior_alloc_data (this);
95 struct inferior *
96 add_inferior_silent (int pid)
98 inferior *inf = new inferior (pid);
100 if (inferior_list == NULL)
101 inferior_list = inf;
102 else
104 inferior *last;
106 for (last = inferior_list; last->next != NULL; last = last->next)
108 last->next = inf;
111 gdb::observers::inferior_added.notify (inf);
113 if (pid != 0)
114 inferior_appeared (inf, pid);
116 return inf;
119 struct inferior *
120 add_inferior (int pid)
122 struct inferior *inf = add_inferior_silent (pid);
124 if (print_inferior_events)
126 if (pid != 0)
127 printf_unfiltered (_("[New inferior %d (%s)]\n"),
128 inf->num,
129 target_pid_to_str (ptid_t (pid)).c_str ());
130 else
131 printf_unfiltered (_("[New inferior %d]\n"), inf->num);
134 return inf;
137 void
138 delete_inferior (struct inferior *todel)
140 struct inferior *inf, *infprev;
142 infprev = NULL;
144 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
145 if (inf == todel)
146 break;
148 if (!inf)
149 return;
151 for (thread_info *tp : inf->threads_safe ())
152 delete_thread_silent (tp);
154 if (infprev)
155 infprev->next = inf->next;
156 else
157 inferior_list = inf->next;
159 gdb::observers::inferior_removed.notify (inf);
161 /* If this program space is rendered useless, remove it. */
162 if (program_space_empty_p (inf->pspace))
163 delete_program_space (inf->pspace);
165 delete inf;
168 /* If SILENT then be quiet -- don't announce a inferior exit, or the
169 exit of its threads. */
171 static void
172 exit_inferior_1 (struct inferior *inftoex, int silent)
174 struct inferior *inf;
176 for (inf = inferior_list; inf; inf = inf->next)
177 if (inf == inftoex)
178 break;
180 if (!inf)
181 return;
183 for (thread_info *tp : inf->threads_safe ())
185 if (silent)
186 delete_thread_silent (tp);
187 else
188 delete_thread (tp);
191 gdb::observers::inferior_exit.notify (inf);
193 inf->pid = 0;
194 inf->fake_pid_p = false;
195 inf->priv = NULL;
197 if (inf->vfork_parent != NULL)
199 inf->vfork_parent->vfork_child = NULL;
200 inf->vfork_parent = NULL;
202 if (inf->vfork_child != NULL)
204 inf->vfork_child->vfork_parent = NULL;
205 inf->vfork_child = NULL;
208 inf->pending_detach = 0;
209 /* Reset it. */
210 inf->control = inferior_control_state (NO_STOP_QUIETLY);
212 /* Clear the register cache and the frame cache. */
213 registers_changed ();
214 reinit_frame_cache ();
217 void
218 exit_inferior (inferior *inf)
220 exit_inferior_1 (inf, 0);
223 void
224 exit_inferior_silent (inferior *inf)
226 exit_inferior_1 (inf, 1);
229 /* See inferior.h. */
231 void
232 detach_inferior (inferior *inf)
234 /* Save the pid, since exit_inferior_1 will reset it. */
235 int pid = inf->pid;
237 exit_inferior_1 (inf, 0);
239 if (print_inferior_events)
240 printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
241 inf->num,
242 target_pid_to_str (ptid_t (pid)).c_str ());
245 void
246 inferior_appeared (struct inferior *inf, int pid)
248 /* If this is the first inferior with threads, reset the global
249 thread id. */
250 if (!any_thread_p ())
251 init_thread_list ();
253 inf->pid = pid;
254 inf->has_exit_code = 0;
255 inf->exit_code = 0;
257 gdb::observers::inferior_appeared.notify (inf);
260 void
261 discard_all_inferiors (void)
263 for (inferior *inf : all_non_exited_inferiors ())
264 exit_inferior_silent (inf);
267 struct inferior *
268 find_inferior_id (int num)
270 for (inferior *inf : all_inferiors ())
271 if (inf->num == num)
272 return inf;
274 return NULL;
277 struct inferior *
278 find_inferior_pid (int pid)
280 /* Looking for inferior pid == 0 is always wrong, and indicative of
281 a bug somewhere else. There may be more than one with pid == 0,
282 for instance. */
283 gdb_assert (pid != 0);
285 for (inferior *inf : all_inferiors ())
286 if (inf->pid == pid)
287 return inf;
289 return NULL;
292 /* See inferior.h */
294 struct inferior *
295 find_inferior_ptid (ptid_t ptid)
297 return find_inferior_pid (ptid.pid ());
300 /* See inferior.h. */
302 struct inferior *
303 find_inferior_for_program_space (struct program_space *pspace)
305 struct inferior *cur_inf = current_inferior ();
307 if (cur_inf->pspace == pspace)
308 return cur_inf;
310 for (inferior *inf : all_inferiors ())
311 if (inf->pspace == pspace)
312 return inf;
314 return NULL;
317 struct inferior *
318 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
319 void *data)
321 for (inferior *inf : all_inferiors_safe ())
322 if ((*callback) (inf, data))
323 return inf;
325 return NULL;
329 have_inferiors (void)
331 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
332 return 1;
334 return 0;
337 /* Return the number of live inferiors. We account for the case
338 where an inferior might have a non-zero pid but no threads, as
339 in the middle of a 'mourn' operation. */
342 number_of_live_inferiors (void)
344 int num_inf = 0;
346 for (inferior *inf : all_non_exited_inferiors ())
347 if (target_has_execution_1 (ptid_t (inf->pid)))
348 for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
350 /* Found a live thread in this inferior, go to the next
351 inferior. */
352 ++num_inf;
353 break;
356 return num_inf;
359 /* Return true if there is at least one live inferior. */
362 have_live_inferiors (void)
364 return number_of_live_inferiors () > 0;
367 /* Prune away any unused inferiors, and then prune away no longer used
368 program spaces. */
370 void
371 prune_inferiors (void)
373 inferior *ss;
375 ss = inferior_list;
376 while (ss)
378 if (!ss->deletable ()
379 || !ss->removable
380 || ss->pid != 0)
382 ss = ss->next;
383 continue;
386 inferior *ss_next = ss->next;
387 delete_inferior (ss);
388 ss = ss_next;
392 /* Simply returns the count of inferiors. */
395 number_of_inferiors (void)
397 auto rng = all_inferiors ();
398 return std::distance (rng.begin (), rng.end ());
401 /* Converts an inferior process id to a string. Like
402 target_pid_to_str, but special cases the null process. */
404 static std::string
405 inferior_pid_to_str (int pid)
407 if (pid != 0)
408 return target_pid_to_str (ptid_t (pid));
409 else
410 return _("<null>");
413 /* See inferior.h. */
415 void
416 print_selected_inferior (struct ui_out *uiout)
418 struct inferior *inf = current_inferior ();
419 const char *filename = inf->pspace->pspace_exec_filename;
421 if (filename == NULL)
422 filename = _("<noexec>");
424 uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"),
425 inf->num, inferior_pid_to_str (inf->pid).c_str (), filename);
428 /* Prints the list of inferiors and their details on UIOUT. This is a
429 version of 'info_inferior_command' suitable for use from MI.
431 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
432 inferiors that should be printed. Otherwise, all inferiors are
433 printed. */
435 static void
436 print_inferior (struct ui_out *uiout, const char *requested_inferiors)
438 int inf_count = 0;
440 /* Compute number of inferiors we will print. */
441 for (inferior *inf : all_inferiors ())
443 if (!number_is_in_list (requested_inferiors, inf->num))
444 continue;
446 ++inf_count;
449 if (inf_count == 0)
451 uiout->message ("No inferiors.\n");
452 return;
455 ui_out_emit_table table_emitter (uiout, 4, inf_count, "inferiors");
456 uiout->table_header (1, ui_left, "current", "");
457 uiout->table_header (4, ui_left, "number", "Num");
458 uiout->table_header (17, ui_left, "target-id", "Description");
459 uiout->table_header (17, ui_left, "exec", "Executable");
461 uiout->table_body ();
462 for (inferior *inf : all_inferiors ())
464 if (!number_is_in_list (requested_inferiors, inf->num))
465 continue;
467 ui_out_emit_tuple tuple_emitter (uiout, NULL);
469 if (inf == current_inferior ())
470 uiout->field_string ("current", "*");
471 else
472 uiout->field_skip ("current");
474 uiout->field_signed ("number", inf->num);
476 uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
478 if (inf->pspace->pspace_exec_filename != NULL)
479 uiout->field_string ("exec", inf->pspace->pspace_exec_filename);
480 else
481 uiout->field_skip ("exec");
483 /* Print extra info that isn't really fit to always present in
484 tabular form. Currently we print the vfork parent/child
485 relationships, if any. */
486 if (inf->vfork_parent)
488 uiout->text (_("\n\tis vfork child of inferior "));
489 uiout->field_signed ("vfork-parent", inf->vfork_parent->num);
491 if (inf->vfork_child)
493 uiout->text (_("\n\tis vfork parent of inferior "));
494 uiout->field_signed ("vfork-child", inf->vfork_child->num);
497 uiout->text ("\n");
501 static void
502 detach_inferior_command (const char *args, int from_tty)
504 if (!args || !*args)
505 error (_("Requires argument (inferior id(s) to detach)"));
507 number_or_range_parser parser (args);
508 while (!parser.finished ())
510 int num = parser.get_number ();
512 inferior *inf = find_inferior_id (num);
513 if (inf == NULL)
515 warning (_("Inferior ID %d not known."), num);
516 continue;
519 if (inf->pid == 0)
521 warning (_("Inferior ID %d is not running."), num);
522 continue;
525 thread_info *tp = any_thread_of_inferior (inf);
526 if (tp == NULL)
528 warning (_("Inferior ID %d has no threads."), num);
529 continue;
532 switch_to_thread (tp);
534 detach_command (NULL, from_tty);
538 static void
539 kill_inferior_command (const char *args, int from_tty)
541 if (!args || !*args)
542 error (_("Requires argument (inferior id(s) to kill)"));
544 number_or_range_parser parser (args);
545 while (!parser.finished ())
547 int num = parser.get_number ();
549 inferior *inf = find_inferior_id (num);
550 if (inf == NULL)
552 warning (_("Inferior ID %d not known."), num);
553 continue;
556 if (inf->pid == 0)
558 warning (_("Inferior ID %d is not running."), num);
559 continue;
562 thread_info *tp = any_thread_of_inferior (inf);
563 if (tp == NULL)
565 warning (_("Inferior ID %d has no threads."), num);
566 continue;
569 switch_to_thread (tp);
571 target_kill ();
574 bfd_cache_close_all ();
577 static void
578 inferior_command (const char *args, int from_tty)
580 struct inferior *inf;
581 int num;
583 num = parse_and_eval_long (args);
585 inf = find_inferior_id (num);
586 if (inf == NULL)
587 error (_("Inferior ID %d not known."), num);
589 if (inf->pid != 0)
591 if (inf != current_inferior ())
593 thread_info *tp = any_thread_of_inferior (inf);
594 if (tp == NULL)
595 error (_("Inferior has no threads."));
597 switch_to_thread (tp);
600 gdb::observers::user_selected_context_changed.notify
601 (USER_SELECTED_INFERIOR
602 | USER_SELECTED_THREAD
603 | USER_SELECTED_FRAME);
605 else
607 set_current_inferior (inf);
608 switch_to_no_thread ();
609 set_current_program_space (inf->pspace);
611 gdb::observers::user_selected_context_changed.notify
612 (USER_SELECTED_INFERIOR);
616 /* Print information about currently known inferiors. */
618 static void
619 info_inferiors_command (const char *args, int from_tty)
621 print_inferior (current_uiout, args);
624 /* remove-inferior ID */
626 static void
627 remove_inferior_command (const char *args, int from_tty)
629 if (args == NULL || *args == '\0')
630 error (_("Requires an argument (inferior id(s) to remove)"));
632 number_or_range_parser parser (args);
633 while (!parser.finished ())
635 int num = parser.get_number ();
636 struct inferior *inf = find_inferior_id (num);
638 if (inf == NULL)
640 warning (_("Inferior ID %d not known."), num);
641 continue;
644 if (!inf->deletable ())
646 warning (_("Can not remove current inferior %d."), num);
647 continue;
650 if (inf->pid != 0)
652 warning (_("Can not remove active inferior %d."), num);
653 continue;
656 delete_inferior (inf);
660 struct inferior *
661 add_inferior_with_spaces (void)
663 struct address_space *aspace;
664 struct program_space *pspace;
665 struct inferior *inf;
666 struct gdbarch_info info;
668 /* If all inferiors share an address space on this system, this
669 doesn't really return a new address space; otherwise, it
670 really does. */
671 aspace = maybe_new_address_space ();
672 pspace = new program_space (aspace);
673 inf = add_inferior (0);
674 inf->pspace = pspace;
675 inf->aspace = pspace->aspace;
677 /* Setup the inferior's initial arch, based on information obtained
678 from the global "set ..." options. */
679 gdbarch_info_init (&info);
680 inf->gdbarch = gdbarch_find_by_info (info);
681 /* The "set ..." options reject invalid settings, so we should
682 always have a valid arch by now. */
683 gdb_assert (inf->gdbarch != NULL);
685 return inf;
688 /* add-inferior [-copies N] [-exec FILENAME] */
690 static void
691 add_inferior_command (const char *args, int from_tty)
693 int i, copies = 1;
694 gdb::unique_xmalloc_ptr<char> exec;
695 symfile_add_flags add_flags = 0;
697 if (from_tty)
698 add_flags |= SYMFILE_VERBOSE;
700 if (args)
702 gdb_argv built_argv (args);
704 for (char **argv = built_argv.get (); *argv != NULL; argv++)
706 if (**argv == '-')
708 if (strcmp (*argv, "-copies") == 0)
710 ++argv;
711 if (!*argv)
712 error (_("No argument to -copies"));
713 copies = parse_and_eval_long (*argv);
715 else if (strcmp (*argv, "-exec") == 0)
717 ++argv;
718 if (!*argv)
719 error (_("No argument to -exec"));
720 exec.reset (tilde_expand (*argv));
723 else
724 error (_("Invalid argument"));
728 scoped_restore_current_pspace_and_thread restore_pspace_thread;
730 for (i = 0; i < copies; ++i)
732 struct inferior *inf = add_inferior_with_spaces ();
734 printf_filtered (_("Added inferior %d\n"), inf->num);
736 if (exec != NULL)
738 /* Switch over temporarily, while reading executable and
739 symbols.q. */
740 set_current_program_space (inf->pspace);
741 set_current_inferior (inf);
742 switch_to_no_thread ();
744 exec_file_attach (exec.get (), from_tty);
745 symbol_file_add_main (exec.get (), add_flags);
750 /* clone-inferior [-copies N] [ID] */
752 static void
753 clone_inferior_command (const char *args, int from_tty)
755 int i, copies = 1;
756 struct inferior *orginf = NULL;
758 if (args)
760 gdb_argv built_argv (args);
762 char **argv = built_argv.get ();
763 for (; *argv != NULL; argv++)
765 if (**argv == '-')
767 if (strcmp (*argv, "-copies") == 0)
769 ++argv;
770 if (!*argv)
771 error (_("No argument to -copies"));
772 copies = parse_and_eval_long (*argv);
774 if (copies < 0)
775 error (_("Invalid copies number"));
778 else
780 if (orginf == NULL)
782 int num;
784 /* The first non-option (-) argument specified the
785 program space ID. */
786 num = parse_and_eval_long (*argv);
787 orginf = find_inferior_id (num);
789 if (orginf == NULL)
790 error (_("Inferior ID %d not known."), num);
791 continue;
793 else
794 error (_("Invalid argument"));
799 /* If no inferior id was specified, then the user wants to clone the
800 current inferior. */
801 if (orginf == NULL)
802 orginf = current_inferior ();
804 scoped_restore_current_pspace_and_thread restore_pspace_thread;
806 for (i = 0; i < copies; ++i)
808 struct address_space *aspace;
809 struct program_space *pspace;
810 struct inferior *inf;
812 /* If all inferiors share an address space on this system, this
813 doesn't really return a new address space; otherwise, it
814 really does. */
815 aspace = maybe_new_address_space ();
816 pspace = new program_space (aspace);
817 inf = add_inferior (0);
818 inf->pspace = pspace;
819 inf->aspace = pspace->aspace;
820 inf->gdbarch = orginf->gdbarch;
822 /* If the original inferior had a user specified target
823 description, make the clone use it too. */
824 if (target_desc_info_from_user_p (inf->tdesc_info))
825 copy_inferior_target_desc_info (inf, orginf);
827 printf_filtered (_("Added inferior %d.\n"), inf->num);
829 set_current_inferior (inf);
830 switch_to_no_thread ();
831 clone_program_space (pspace, orginf->pspace);
835 /* Print notices when new inferiors are created and die. */
836 static void
837 show_print_inferior_events (struct ui_file *file, int from_tty,
838 struct cmd_list_element *c, const char *value)
840 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
843 /* Return a new value for the selected inferior's id. */
845 static struct value *
846 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
847 void *ignore)
849 struct inferior *inf = current_inferior ();
851 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
854 /* Implementation of `$_inferior' variable. */
856 static const struct internalvar_funcs inferior_funcs =
858 inferior_id_make_value,
859 NULL,
860 NULL
865 void
866 initialize_inferiors (void)
868 struct cmd_list_element *c = NULL;
870 /* There's always one inferior. Note that this function isn't an
871 automatic _initialize_foo function, since other _initialize_foo
872 routines may need to install their per-inferior data keys. We
873 can only allocate an inferior when all those modules have done
874 that. Do this after initialize_progspace, due to the
875 current_program_space reference. */
876 current_inferior_ = add_inferior_silent (0);
877 current_inferior_->incref ();
878 current_inferior_->pspace = current_program_space;
879 current_inferior_->aspace = current_program_space->aspace;
880 /* The architecture will be initialized shortly, by
881 initialize_current_architecture. */
883 add_info ("inferiors", info_inferiors_command,
884 _("Print a list of inferiors being managed.\n\
885 Usage: info inferiors [ID]...\n\
886 If IDs are specified, the list is limited to just those inferiors.\n\
887 By default all inferiors are displayed."));
889 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
890 Add a new inferior.\n\
891 Usage: add-inferior [-copies N] [-exec FILENAME]\n\
892 N is the optional number of inferiors to add, default is 1.\n\
893 FILENAME is the file name of the executable to use\n\
894 as main program."));
895 set_cmd_completer (c, filename_completer);
897 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
898 Remove inferior ID (or list of IDs).\n\
899 Usage: remove-inferiors ID..."));
901 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
902 Clone inferior ID.\n\
903 Usage: clone-inferior [-copies N] [ID]\n\
904 Add N copies of inferior ID. The new inferior has the same\n\
905 executable loaded as the copied inferior. If -copies is not specified,\n\
906 adds 1 copy. If ID is not specified, it is the current inferior\n\
907 that is cloned."));
909 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
910 Detach from inferior ID (or list of IDS).\n\
911 Usage; detach inferiors ID..."),
912 &detachlist);
914 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
915 Kill inferior ID (or list of IDs).\n\
916 Usage: kill inferiors ID..."),
917 &killlist);
919 add_cmd ("inferior", class_run, inferior_command, _("\
920 Use this command to switch between inferiors.\n\
921 Usage: inferior ID\n\
922 The new inferior ID must be currently known."),
923 &cmdlist);
925 add_setshow_boolean_cmd ("inferior-events", no_class,
926 &print_inferior_events, _("\
927 Set printing of inferior events (such as inferior start and exit)."), _("\
928 Show printing of inferior events (such as inferior start and exit)."), NULL,
929 NULL,
930 show_print_inferior_events,
931 &setprintlist, &showprintlist);
933 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);