1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2016 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/>. */
25 #include "completer.h"
27 #include "gdbthread.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"
39 void _initialize_inferiors (void);
41 /* Keep a registry of per-inferior data-pointers required by other GDB
44 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
46 struct inferior *inferior_list = NULL;
47 static int highest_inferior_num;
49 /* Print notices on inferior events (attach, detach, etc.), set with
50 `set print inferior-events'. */
51 static int print_inferior_events = 0;
53 /* The Current Inferior. */
54 static struct inferior *current_inferior_ = NULL;
57 current_inferior (void)
59 return current_inferior_;
63 set_current_inferior (struct inferior *inf)
65 /* There's always an inferior. */
66 gdb_assert (inf != NULL);
68 current_inferior_ = inf;
71 /* A cleanups callback, helper for save_current_program_space
75 restore_inferior (void *arg)
77 struct inferior *saved_inferior = (struct inferior *) arg;
79 set_current_inferior (saved_inferior);
82 /* Save the current program space so that it may be restored by a later
83 call to do_cleanups. Returns the struct cleanup pointer needed for
84 later doing the cleanup. */
87 save_current_inferior (void)
89 struct cleanup *old_chain = make_cleanup (restore_inferior,
96 free_inferior (struct inferior *inf)
98 discard_all_inferior_continuations (inf);
99 inferior_free_data (inf);
101 xfree (inf->terminal);
102 free_environ (inf->environment);
103 target_desc_info_free (inf->tdesc_info);
109 init_inferior_list (void)
111 struct inferior *inf, *infnext;
113 highest_inferior_num = 0;
117 for (inf = inferior_list; inf; inf = infnext)
123 inferior_list = NULL;
127 add_inferior_silent (int pid)
129 struct inferior *inf;
131 inf = XNEW (struct inferior);
132 memset (inf, 0, sizeof (*inf));
135 inf->control.stop_soon = NO_STOP_QUIETLY;
137 inf->num = ++highest_inferior_num;
139 if (inferior_list == NULL)
143 struct inferior *last;
145 for (last = inferior_list; last->next != NULL; last = last->next)
150 inf->environment = make_environ ();
151 init_environ (inf->environment);
153 inferior_alloc_data (inf);
155 observer_notify_inferior_added (inf);
158 inferior_appeared (inf, pid);
164 add_inferior (int pid)
166 struct inferior *inf = add_inferior_silent (pid);
168 if (print_inferior_events)
169 printf_unfiltered (_("[New inferior %d]\n"), pid);
174 struct delete_thread_of_inferior_arg
181 delete_thread_of_inferior (struct thread_info *tp, void *data)
183 struct delete_thread_of_inferior_arg *arg
184 = (struct delete_thread_of_inferior_arg *) data;
186 if (ptid_get_pid (tp->ptid) == arg->pid)
189 delete_thread_silent (tp->ptid);
191 delete_thread (tp->ptid);
198 delete_inferior (struct inferior *todel)
200 struct inferior *inf, *infprev;
201 struct delete_thread_of_inferior_arg arg;
205 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
215 iterate_over_threads (delete_thread_of_inferior, &arg);
218 infprev->next = inf->next;
220 inferior_list = inf->next;
222 observer_notify_inferior_removed (inf);
224 /* If this program space is rendered useless, remove it. */
225 if (program_space_empty_p (inf->pspace))
226 delete_program_space (inf->pspace);
231 /* If SILENT then be quiet -- don't announce a inferior exit, or the
232 exit of its threads. */
235 exit_inferior_1 (struct inferior *inftoex, int silent)
237 struct inferior *inf;
238 struct delete_thread_of_inferior_arg arg;
240 for (inf = inferior_list; inf; inf = inf->next)
250 iterate_over_threads (delete_thread_of_inferior, &arg);
252 observer_notify_inferior_exit (inf);
256 if (inf->vfork_parent != NULL)
258 inf->vfork_parent->vfork_child = NULL;
259 inf->vfork_parent = NULL;
261 if (inf->vfork_child != NULL)
263 inf->vfork_child->vfork_parent = NULL;
264 inf->vfork_child = NULL;
267 inf->pending_detach = 0;
271 exit_inferior (int pid)
273 struct inferior *inf = find_inferior_pid (pid);
275 exit_inferior_1 (inf, 0);
277 if (print_inferior_events)
278 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
282 exit_inferior_silent (int pid)
284 struct inferior *inf = find_inferior_pid (pid);
286 exit_inferior_1 (inf, 1);
290 exit_inferior_num_silent (int num)
292 struct inferior *inf = find_inferior_id (num);
294 exit_inferior_1 (inf, 1);
298 detach_inferior (int pid)
300 struct inferior *inf = find_inferior_pid (pid);
302 exit_inferior_1 (inf, 0);
304 if (print_inferior_events)
305 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
309 inferior_appeared (struct inferior *inf, int pid)
312 inf->has_exit_code = 0;
315 observer_notify_inferior_appeared (inf);
319 discard_all_inferiors (void)
321 struct inferior *inf;
323 for (inf = inferior_list; inf; inf = inf->next)
326 exit_inferior_silent (inf->pid);
331 find_inferior_id (int num)
333 struct inferior *inf;
335 for (inf = inferior_list; inf; inf = inf->next)
343 find_inferior_pid (int pid)
345 struct inferior *inf;
347 /* Looking for inferior pid == 0 is always wrong, and indicative of
348 a bug somewhere else. There may be more than one with pid == 0,
350 gdb_assert (pid != 0);
352 for (inf = inferior_list; inf; inf = inf->next)
362 find_inferior_ptid (ptid_t ptid)
364 return find_inferior_pid (ptid_get_pid (ptid));
367 /* See inferior.h. */
370 find_inferior_for_program_space (struct program_space *pspace)
372 struct inferior *inf = current_inferior ();
374 if (inf->pspace == pspace)
377 for (inf = inferior_list; inf != NULL; inf = inf->next)
379 if (inf->pspace == pspace)
387 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
390 struct inferior *inf, *infnext;
392 for (inf = inferior_list; inf; inf = infnext)
395 if ((*callback) (inf, data))
403 valid_gdb_inferior_id (int num)
405 struct inferior *inf;
407 for (inf = inferior_list; inf; inf = inf->next)
415 pid_to_gdb_inferior_id (int pid)
417 struct inferior *inf;
419 for (inf = inferior_list; inf; inf = inf->next)
427 gdb_inferior_id_to_pid (int num)
429 struct inferior *inferior = find_inferior_id (num);
431 return inferior->pid;
437 in_inferior_list (int pid)
439 struct inferior *inf;
441 for (inf = inferior_list; inf; inf = inf->next)
449 have_inferiors (void)
451 struct inferior *inf;
453 for (inf = inferior_list; inf; inf = inf->next)
460 /* Return the number of live inferiors. We account for the case
461 where an inferior might have a non-zero pid but no threads, as
462 in the middle of a 'mourn' operation. */
465 number_of_live_inferiors (void)
467 struct inferior *inf;
470 for (inf = inferior_list; inf; inf = inf->next)
473 struct thread_info *tp;
475 ALL_NON_EXITED_THREADS (tp)
476 if (tp && ptid_get_pid (tp->ptid) == inf->pid)
477 if (target_has_execution_1 (tp->ptid))
479 /* Found a live thread in this inferior, go to the next
489 /* Return true if there is at least one live inferior. */
492 have_live_inferiors (void)
494 return number_of_live_inferiors () > 0;
497 /* Prune away any unused inferiors, and then prune away no longer used
501 prune_inferiors (void)
503 struct inferior *ss, **ss_link;
504 struct inferior *current = current_inferior ();
507 ss_link = &inferior_list;
520 delete_inferior (ss);
525 /* Simply returns the count of inferiors. */
528 number_of_inferiors (void)
530 struct inferior *inf;
533 for (inf = inferior_list; inf != NULL; inf = inf->next)
539 /* Converts an inferior process id to a string. Like
540 target_pid_to_str, but special cases the null process. */
543 inferior_pid_to_str (int pid)
546 return target_pid_to_str (pid_to_ptid (pid));
551 /* Prints the list of inferiors and their details on UIOUT. This is a
552 version of 'info_inferior_command' suitable for use from MI.
554 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
555 inferiors that should be printed. Otherwise, all inferiors are
559 print_inferior (struct ui_out *uiout, char *requested_inferiors)
561 struct inferior *inf;
562 struct cleanup *old_chain;
565 /* Compute number of inferiors we will print. */
566 for (inf = inferior_list; inf; inf = inf->next)
568 if (!number_is_in_list (requested_inferiors, inf->num))
576 ui_out_message (uiout, 0, "No inferiors.\n");
580 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
582 ui_out_table_header (uiout, 1, ui_left, "current", "");
583 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
584 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
585 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
587 ui_out_table_body (uiout);
588 for (inf = inferior_list; inf; inf = inf->next)
590 struct cleanup *chain2;
592 if (!number_is_in_list (requested_inferiors, inf->num))
595 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
597 if (inf == current_inferior ())
598 ui_out_field_string (uiout, "current", "*");
600 ui_out_field_skip (uiout, "current");
602 ui_out_field_int (uiout, "number", inf->num);
604 ui_out_field_string (uiout, "target-id",
605 inferior_pid_to_str (inf->pid));
607 if (inf->pspace->pspace_exec_filename != NULL)
608 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
610 ui_out_field_skip (uiout, "exec");
612 /* Print extra info that isn't really fit to always present in
613 tabular form. Currently we print the vfork parent/child
614 relationships, if any. */
615 if (inf->vfork_parent)
617 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
618 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
620 if (inf->vfork_child)
622 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
623 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
626 ui_out_text (uiout, "\n");
627 do_cleanups (chain2);
630 do_cleanups (old_chain);
634 detach_inferior_command (char *args, int from_tty)
637 struct thread_info *tp;
638 struct get_number_or_range_state state;
641 error (_("Requires argument (inferior id(s) to detach)"));
643 init_number_or_range (&state, args);
644 while (!state.finished)
646 num = get_number_or_range (&state);
648 if (!valid_gdb_inferior_id (num))
650 warning (_("Inferior ID %d not known."), num);
654 pid = gdb_inferior_id_to_pid (num);
657 warning (_("Inferior ID %d is not running."), num);
661 tp = any_thread_of_process (pid);
664 warning (_("Inferior ID %d has no threads."), num);
668 switch_to_thread (tp->ptid);
670 detach_command (NULL, from_tty);
675 kill_inferior_command (char *args, int from_tty)
678 struct thread_info *tp;
679 struct get_number_or_range_state state;
682 error (_("Requires argument (inferior id(s) to kill)"));
684 init_number_or_range (&state, args);
685 while (!state.finished)
687 num = get_number_or_range (&state);
689 if (!valid_gdb_inferior_id (num))
691 warning (_("Inferior ID %d not known."), num);
695 pid = gdb_inferior_id_to_pid (num);
698 warning (_("Inferior ID %d is not running."), num);
702 tp = any_thread_of_process (pid);
705 warning (_("Inferior ID %d has no threads."), num);
709 switch_to_thread (tp->ptid);
714 bfd_cache_close_all ();
718 inferior_command (char *args, int from_tty)
720 struct inferior *inf;
723 num = parse_and_eval_long (args);
725 inf = find_inferior_id (num);
727 error (_("Inferior ID %d not known."), num);
729 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
731 inferior_pid_to_str (inf->pid),
732 (inf->pspace->pspace_exec_filename != NULL
733 ? inf->pspace->pspace_exec_filename
738 if (inf->pid != ptid_get_pid (inferior_ptid))
740 struct thread_info *tp;
742 tp = any_thread_of_process (inf->pid);
744 error (_("Inferior has no threads."));
746 switch_to_thread (tp->ptid);
749 printf_filtered (_("[Switching to thread %s (%s)] "),
750 print_thread_id (inferior_thread ()),
751 target_pid_to_str (inferior_ptid));
755 struct inferior *inf;
757 inf = find_inferior_id (num);
758 set_current_inferior (inf);
759 switch_to_thread (null_ptid);
760 set_current_program_space (inf->pspace);
763 if (inf->pid != 0 && is_running (inferior_ptid))
764 ui_out_text (current_uiout, "(running)\n");
765 else if (inf->pid != 0)
767 ui_out_text (current_uiout, "\n");
768 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
772 /* Print information about currently known inferiors. */
775 info_inferiors_command (char *args, int from_tty)
777 print_inferior (current_uiout, args);
780 /* remove-inferior ID */
783 remove_inferior_command (char *args, int from_tty)
786 struct inferior *inf;
787 struct get_number_or_range_state state;
789 if (args == NULL || *args == '\0')
790 error (_("Requires an argument (inferior id(s) to remove)"));
792 init_number_or_range (&state, args);
793 while (!state.finished)
795 num = get_number_or_range (&state);
796 inf = find_inferior_id (num);
800 warning (_("Inferior ID %d not known."), num);
804 if (inf == current_inferior ())
806 warning (_("Can not remove current inferior %d."), num);
812 warning (_("Can not remove active inferior %d."), num);
816 delete_inferior (inf);
821 add_inferior_with_spaces (void)
823 struct address_space *aspace;
824 struct program_space *pspace;
825 struct inferior *inf;
826 struct gdbarch_info info;
828 /* If all inferiors share an address space on this system, this
829 doesn't really return a new address space; otherwise, it
831 aspace = maybe_new_address_space ();
832 pspace = add_program_space (aspace);
833 inf = add_inferior (0);
834 inf->pspace = pspace;
835 inf->aspace = pspace->aspace;
837 /* Setup the inferior's initial arch, based on information obtained
838 from the global "set ..." options. */
839 gdbarch_info_init (&info);
840 inf->gdbarch = gdbarch_find_by_info (info);
841 /* The "set ..." options reject invalid settings, so we should
842 always have a valid arch by now. */
843 gdb_assert (inf->gdbarch != NULL);
848 /* add-inferior [-copies N] [-exec FILENAME] */
851 add_inferior_command (char *args, int from_tty)
856 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
860 argv = gdb_buildargv (args);
861 make_cleanup_freeargv (argv);
863 for (; *argv != NULL; argv++)
867 if (strcmp (*argv, "-copies") == 0)
871 error (_("No argument to -copies"));
872 copies = parse_and_eval_long (*argv);
874 else if (strcmp (*argv, "-exec") == 0)
878 error (_("No argument to -exec"));
879 exec = tilde_expand (*argv);
880 make_cleanup (xfree, exec);
884 error (_("Invalid argument"));
888 save_current_space_and_thread ();
890 for (i = 0; i < copies; ++i)
892 struct inferior *inf = add_inferior_with_spaces ();
894 printf_filtered (_("Added inferior %d\n"), inf->num);
898 /* Switch over temporarily, while reading executable and
900 set_current_program_space (inf->pspace);
901 set_current_inferior (inf);
902 switch_to_thread (null_ptid);
904 exec_file_attach (exec, from_tty);
905 symbol_file_add_main (exec, from_tty);
909 do_cleanups (old_chain);
912 /* clone-inferior [-copies N] [ID] */
915 clone_inferior_command (char *args, int from_tty)
919 struct inferior *orginf = NULL;
920 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
924 argv = gdb_buildargv (args);
925 make_cleanup_freeargv (argv);
927 for (; *argv != NULL; argv++)
931 if (strcmp (*argv, "-copies") == 0)
935 error (_("No argument to -copies"));
936 copies = parse_and_eval_long (*argv);
939 error (_("Invalid copies number"));
948 /* The first non-option (-) argument specified the
950 num = parse_and_eval_long (*argv);
951 orginf = find_inferior_id (num);
954 error (_("Inferior ID %d not known."), num);
958 error (_("Invalid argument"));
963 /* If no inferior id was specified, then the user wants to clone the
966 orginf = current_inferior ();
968 save_current_space_and_thread ();
970 for (i = 0; i < copies; ++i)
972 struct address_space *aspace;
973 struct program_space *pspace;
974 struct inferior *inf;
976 /* If all inferiors share an address space on this system, this
977 doesn't really return a new address space; otherwise, it
979 aspace = maybe_new_address_space ();
980 pspace = add_program_space (aspace);
981 inf = add_inferior (0);
982 inf->pspace = pspace;
983 inf->aspace = pspace->aspace;
984 inf->gdbarch = orginf->gdbarch;
986 /* If the original inferior had a user specified target
987 description, make the clone use it too. */
988 if (target_desc_info_from_user_p (inf->tdesc_info))
989 copy_inferior_target_desc_info (inf, orginf);
991 printf_filtered (_("Added inferior %d.\n"), inf->num);
993 set_current_inferior (inf);
994 switch_to_thread (null_ptid);
995 clone_program_space (pspace, orginf->pspace);
998 do_cleanups (old_chain);
1001 /* Print notices when new inferiors are created and die. */
1003 show_print_inferior_events (struct ui_file *file, int from_tty,
1004 struct cmd_list_element *c, const char *value)
1006 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
1009 /* Return a new value for the selected inferior's id. */
1011 static struct value *
1012 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1015 struct inferior *inf = current_inferior ();
1017 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
1020 /* Implementation of `$_inferior' variable. */
1022 static const struct internalvar_funcs inferior_funcs =
1024 inferior_id_make_value,
1032 initialize_inferiors (void)
1034 struct cmd_list_element *c = NULL;
1036 /* There's always one inferior. Note that this function isn't an
1037 automatic _initialize_foo function, since other _initialize_foo
1038 routines may need to install their per-inferior data keys. We
1039 can only allocate an inferior when all those modules have done
1040 that. Do this after initialize_progspace, due to the
1041 current_program_space reference. */
1042 current_inferior_ = add_inferior (0);
1043 current_inferior_->pspace = current_program_space;
1044 current_inferior_->aspace = current_program_space->aspace;
1045 /* The architecture will be initialized shortly, by
1046 initialize_current_architecture. */
1048 add_info ("inferiors", info_inferiors_command,
1049 _("IDs of specified inferiors (all inferiors if no argument)."));
1051 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1052 Add a new inferior.\n\
1053 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1054 N is the optional number of inferiors to add, default is 1.\n\
1055 FILENAME is the file name of the executable to use\n\
1056 as main program."));
1057 set_cmd_completer (c, filename_completer);
1059 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1060 Remove inferior ID (or list of IDs).\n\
1061 Usage: remove-inferiors ID..."));
1063 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1064 Clone inferior ID.\n\
1065 Usage: clone-inferior [-copies <N>] [ID]\n\
1066 Add N copies of inferior ID. The new inferior has the same\n\
1067 executable loaded as the copied inferior. If -copies is not specified,\n\
1068 adds 1 copy. If ID is not specified, it is the current inferior\n\
1071 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1072 Detach from inferior ID (or list of IDS)."),
1075 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1076 Kill inferior ID (or list of IDs)."),
1079 add_cmd ("inferior", class_run, inferior_command, _("\
1080 Use this command to switch between inferiors.\n\
1081 The new inferior ID must be currently known."),
1084 add_setshow_boolean_cmd ("inferior-events", no_class,
1085 &print_inferior_events, _("\
1086 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1087 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1089 show_print_inferior_events,
1090 &setprintlist, &showprintlist);
1092 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);