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);
259 if (inf->vfork_parent != NULL)
261 inf->vfork_parent->vfork_child = NULL;
262 inf->vfork_parent = NULL;
264 if (inf->vfork_child != NULL)
266 inf->vfork_child->vfork_parent = NULL;
267 inf->vfork_child = NULL;
270 inf->pending_detach = 0;
274 exit_inferior (int pid)
276 struct inferior *inf = find_inferior_pid (pid);
278 exit_inferior_1 (inf, 0);
280 if (print_inferior_events)
281 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
285 exit_inferior_silent (int pid)
287 struct inferior *inf = find_inferior_pid (pid);
289 exit_inferior_1 (inf, 1);
293 exit_inferior_num_silent (int num)
295 struct inferior *inf = find_inferior_id (num);
297 exit_inferior_1 (inf, 1);
301 detach_inferior (int pid)
303 struct inferior *inf = find_inferior_pid (pid);
305 exit_inferior_1 (inf, 0);
307 if (print_inferior_events)
308 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
312 inferior_appeared (struct inferior *inf, int pid)
315 inf->has_exit_code = 0;
318 observer_notify_inferior_appeared (inf);
322 discard_all_inferiors (void)
324 struct inferior *inf;
326 for (inf = inferior_list; inf; inf = inf->next)
329 exit_inferior_silent (inf->pid);
334 find_inferior_id (int num)
336 struct inferior *inf;
338 for (inf = inferior_list; inf; inf = inf->next)
346 find_inferior_pid (int pid)
348 struct inferior *inf;
350 /* Looking for inferior pid == 0 is always wrong, and indicative of
351 a bug somewhere else. There may be more than one with pid == 0,
353 gdb_assert (pid != 0);
355 for (inf = inferior_list; inf; inf = inf->next)
365 find_inferior_ptid (ptid_t ptid)
367 return find_inferior_pid (ptid_get_pid (ptid));
370 /* See inferior.h. */
373 find_inferior_for_program_space (struct program_space *pspace)
375 struct inferior *inf = current_inferior ();
377 if (inf->pspace == pspace)
380 for (inf = inferior_list; inf != NULL; inf = inf->next)
382 if (inf->pspace == pspace)
390 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
393 struct inferior *inf, *infnext;
395 for (inf = inferior_list; inf; inf = infnext)
398 if ((*callback) (inf, data))
406 valid_gdb_inferior_id (int num)
408 struct inferior *inf;
410 for (inf = inferior_list; inf; inf = inf->next)
418 pid_to_gdb_inferior_id (int pid)
420 struct inferior *inf;
422 for (inf = inferior_list; inf; inf = inf->next)
430 gdb_inferior_id_to_pid (int num)
432 struct inferior *inferior = find_inferior_id (num);
434 return inferior->pid;
440 in_inferior_list (int pid)
442 struct inferior *inf;
444 for (inf = inferior_list; inf; inf = inf->next)
452 have_inferiors (void)
454 struct inferior *inf;
456 for (inf = inferior_list; inf; inf = inf->next)
463 /* Return the number of live inferiors. We account for the case
464 where an inferior might have a non-zero pid but no threads, as
465 in the middle of a 'mourn' operation. */
468 number_of_live_inferiors (void)
470 struct inferior *inf;
473 for (inf = inferior_list; inf; inf = inf->next)
476 struct thread_info *tp;
478 ALL_NON_EXITED_THREADS (tp)
479 if (tp && ptid_get_pid (tp->ptid) == inf->pid)
480 if (target_has_execution_1 (tp->ptid))
482 /* Found a live thread in this inferior, go to the next
492 /* Return true if there is at least one live inferior. */
495 have_live_inferiors (void)
497 return number_of_live_inferiors () > 0;
500 /* Prune away any unused inferiors, and then prune away no longer used
504 prune_inferiors (void)
506 struct inferior *ss, **ss_link;
507 struct inferior *current = current_inferior ();
510 ss_link = &inferior_list;
523 delete_inferior (ss);
528 /* Simply returns the count of inferiors. */
531 number_of_inferiors (void)
533 struct inferior *inf;
536 for (inf = inferior_list; inf != NULL; inf = inf->next)
542 /* Converts an inferior process id to a string. Like
543 target_pid_to_str, but special cases the null process. */
546 inferior_pid_to_str (int pid)
549 return target_pid_to_str (pid_to_ptid (pid));
554 /* See inferior.h. */
557 print_selected_inferior (struct ui_out *uiout)
559 char buf[PATH_MAX + 256];
560 struct inferior *inf = current_inferior ();
562 xsnprintf (buf, sizeof (buf),
563 _("[Switching to inferior %d [%s] (%s)]\n"),
565 inferior_pid_to_str (inf->pid),
566 (inf->pspace->pspace_exec_filename != NULL
567 ? inf->pspace->pspace_exec_filename
569 ui_out_text (uiout, buf);
572 /* Prints the list of inferiors and their details on UIOUT. This is a
573 version of 'info_inferior_command' suitable for use from MI.
575 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
576 inferiors that should be printed. Otherwise, all inferiors are
580 print_inferior (struct ui_out *uiout, char *requested_inferiors)
582 struct inferior *inf;
583 struct cleanup *old_chain;
586 /* Compute number of inferiors we will print. */
587 for (inf = inferior_list; inf; inf = inf->next)
589 if (!number_is_in_list (requested_inferiors, inf->num))
597 ui_out_message (uiout, 0, "No inferiors.\n");
601 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
603 ui_out_table_header (uiout, 1, ui_left, "current", "");
604 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
605 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
606 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
608 ui_out_table_body (uiout);
609 for (inf = inferior_list; inf; inf = inf->next)
611 struct cleanup *chain2;
613 if (!number_is_in_list (requested_inferiors, inf->num))
616 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
618 if (inf == current_inferior ())
619 ui_out_field_string (uiout, "current", "*");
621 ui_out_field_skip (uiout, "current");
623 ui_out_field_int (uiout, "number", inf->num);
625 ui_out_field_string (uiout, "target-id",
626 inferior_pid_to_str (inf->pid));
628 if (inf->pspace->pspace_exec_filename != NULL)
629 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
631 ui_out_field_skip (uiout, "exec");
633 /* Print extra info that isn't really fit to always present in
634 tabular form. Currently we print the vfork parent/child
635 relationships, if any. */
636 if (inf->vfork_parent)
638 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
639 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
641 if (inf->vfork_child)
643 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
644 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
647 ui_out_text (uiout, "\n");
648 do_cleanups (chain2);
651 do_cleanups (old_chain);
655 detach_inferior_command (char *args, int from_tty)
657 struct thread_info *tp;
660 error (_("Requires argument (inferior id(s) to detach)"));
662 number_or_range_parser parser (args);
663 while (!parser.finished ())
665 int num = parser.get_number ();
667 if (!valid_gdb_inferior_id (num))
669 warning (_("Inferior ID %d not known."), num);
673 int pid = gdb_inferior_id_to_pid (num);
676 warning (_("Inferior ID %d is not running."), num);
680 tp = any_thread_of_process (pid);
683 warning (_("Inferior ID %d has no threads."), num);
687 switch_to_thread (tp->ptid);
689 detach_command (NULL, from_tty);
694 kill_inferior_command (char *args, int from_tty)
696 struct thread_info *tp;
699 error (_("Requires argument (inferior id(s) to kill)"));
701 number_or_range_parser parser (args);
702 while (!parser.finished ())
704 int num = parser.get_number ();
706 if (!valid_gdb_inferior_id (num))
708 warning (_("Inferior ID %d not known."), num);
712 int pid = gdb_inferior_id_to_pid (num);
715 warning (_("Inferior ID %d is not running."), num);
719 tp = any_thread_of_process (pid);
722 warning (_("Inferior ID %d has no threads."), num);
726 switch_to_thread (tp->ptid);
731 bfd_cache_close_all ();
735 inferior_command (char *args, int from_tty)
737 struct inferior *inf;
740 num = parse_and_eval_long (args);
742 inf = find_inferior_id (num);
744 error (_("Inferior ID %d not known."), num);
748 if (inf->pid != ptid_get_pid (inferior_ptid))
750 struct thread_info *tp;
752 tp = any_thread_of_process (inf->pid);
754 error (_("Inferior has no threads."));
756 switch_to_thread (tp->ptid);
759 observer_notify_user_selected_context_changed
760 (USER_SELECTED_INFERIOR
761 | USER_SELECTED_THREAD
762 | USER_SELECTED_FRAME);
766 struct inferior *inf;
768 inf = find_inferior_id (num);
769 set_current_inferior (inf);
770 switch_to_thread (null_ptid);
771 set_current_program_space (inf->pspace);
773 observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR);
777 /* Print information about currently known inferiors. */
780 info_inferiors_command (char *args, int from_tty)
782 print_inferior (current_uiout, args);
785 /* remove-inferior ID */
788 remove_inferior_command (char *args, int from_tty)
790 if (args == NULL || *args == '\0')
791 error (_("Requires an argument (inferior id(s) to remove)"));
793 number_or_range_parser parser (args);
794 while (!parser.finished ())
796 int num = parser.get_number ();
797 struct inferior *inf = find_inferior_id (num);
801 warning (_("Inferior ID %d not known."), num);
805 if (inf == current_inferior ())
807 warning (_("Can not remove current inferior %d."), num);
813 warning (_("Can not remove active inferior %d."), num);
817 delete_inferior (inf);
822 add_inferior_with_spaces (void)
824 struct address_space *aspace;
825 struct program_space *pspace;
826 struct inferior *inf;
827 struct gdbarch_info info;
829 /* If all inferiors share an address space on this system, this
830 doesn't really return a new address space; otherwise, it
832 aspace = maybe_new_address_space ();
833 pspace = add_program_space (aspace);
834 inf = add_inferior (0);
835 inf->pspace = pspace;
836 inf->aspace = pspace->aspace;
838 /* Setup the inferior's initial arch, based on information obtained
839 from the global "set ..." options. */
840 gdbarch_info_init (&info);
841 inf->gdbarch = gdbarch_find_by_info (info);
842 /* The "set ..." options reject invalid settings, so we should
843 always have a valid arch by now. */
844 gdb_assert (inf->gdbarch != NULL);
849 /* add-inferior [-copies N] [-exec FILENAME] */
852 add_inferior_command (char *args, int from_tty)
857 symfile_add_flags add_flags = 0;
858 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
861 add_flags |= SYMFILE_VERBOSE;
865 argv = gdb_buildargv (args);
866 make_cleanup_freeargv (argv);
868 for (; *argv != NULL; argv++)
872 if (strcmp (*argv, "-copies") == 0)
876 error (_("No argument to -copies"));
877 copies = parse_and_eval_long (*argv);
879 else if (strcmp (*argv, "-exec") == 0)
883 error (_("No argument to -exec"));
884 exec = tilde_expand (*argv);
885 make_cleanup (xfree, exec);
889 error (_("Invalid argument"));
893 save_current_space_and_thread ();
895 for (i = 0; i < copies; ++i)
897 struct inferior *inf = add_inferior_with_spaces ();
899 printf_filtered (_("Added inferior %d\n"), inf->num);
903 /* Switch over temporarily, while reading executable and
905 set_current_program_space (inf->pspace);
906 set_current_inferior (inf);
907 switch_to_thread (null_ptid);
909 exec_file_attach (exec, from_tty);
910 symbol_file_add_main (exec, add_flags);
914 do_cleanups (old_chain);
917 /* clone-inferior [-copies N] [ID] */
920 clone_inferior_command (char *args, int from_tty)
924 struct inferior *orginf = NULL;
925 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
929 argv = gdb_buildargv (args);
930 make_cleanup_freeargv (argv);
932 for (; *argv != NULL; argv++)
936 if (strcmp (*argv, "-copies") == 0)
940 error (_("No argument to -copies"));
941 copies = parse_and_eval_long (*argv);
944 error (_("Invalid copies number"));
953 /* The first non-option (-) argument specified the
955 num = parse_and_eval_long (*argv);
956 orginf = find_inferior_id (num);
959 error (_("Inferior ID %d not known."), num);
963 error (_("Invalid argument"));
968 /* If no inferior id was specified, then the user wants to clone the
971 orginf = current_inferior ();
973 save_current_space_and_thread ();
975 for (i = 0; i < copies; ++i)
977 struct address_space *aspace;
978 struct program_space *pspace;
979 struct inferior *inf;
981 /* If all inferiors share an address space on this system, this
982 doesn't really return a new address space; otherwise, it
984 aspace = maybe_new_address_space ();
985 pspace = add_program_space (aspace);
986 inf = add_inferior (0);
987 inf->pspace = pspace;
988 inf->aspace = pspace->aspace;
989 inf->gdbarch = orginf->gdbarch;
991 /* If the original inferior had a user specified target
992 description, make the clone use it too. */
993 if (target_desc_info_from_user_p (inf->tdesc_info))
994 copy_inferior_target_desc_info (inf, orginf);
996 printf_filtered (_("Added inferior %d.\n"), inf->num);
998 set_current_inferior (inf);
999 switch_to_thread (null_ptid);
1000 clone_program_space (pspace, orginf->pspace);
1003 do_cleanups (old_chain);
1006 /* Print notices when new inferiors are created and die. */
1008 show_print_inferior_events (struct ui_file *file, int from_tty,
1009 struct cmd_list_element *c, const char *value)
1011 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
1014 /* Return a new value for the selected inferior's id. */
1016 static struct value *
1017 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1020 struct inferior *inf = current_inferior ();
1022 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
1025 /* Implementation of `$_inferior' variable. */
1027 static const struct internalvar_funcs inferior_funcs =
1029 inferior_id_make_value,
1037 initialize_inferiors (void)
1039 struct cmd_list_element *c = NULL;
1041 /* There's always one inferior. Note that this function isn't an
1042 automatic _initialize_foo function, since other _initialize_foo
1043 routines may need to install their per-inferior data keys. We
1044 can only allocate an inferior when all those modules have done
1045 that. Do this after initialize_progspace, due to the
1046 current_program_space reference. */
1047 current_inferior_ = add_inferior (0);
1048 current_inferior_->pspace = current_program_space;
1049 current_inferior_->aspace = current_program_space->aspace;
1050 /* The architecture will be initialized shortly, by
1051 initialize_current_architecture. */
1053 add_info ("inferiors", info_inferiors_command,
1054 _("IDs of specified inferiors (all inferiors if no argument)."));
1056 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1057 Add a new inferior.\n\
1058 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1059 N is the optional number of inferiors to add, default is 1.\n\
1060 FILENAME is the file name of the executable to use\n\
1061 as main program."));
1062 set_cmd_completer (c, filename_completer);
1064 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1065 Remove inferior ID (or list of IDs).\n\
1066 Usage: remove-inferiors ID..."));
1068 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1069 Clone inferior ID.\n\
1070 Usage: clone-inferior [-copies <N>] [ID]\n\
1071 Add N copies of inferior ID. The new inferior has the same\n\
1072 executable loaded as the copied inferior. If -copies is not specified,\n\
1073 adds 1 copy. If ID is not specified, it is the current inferior\n\
1076 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1077 Detach from inferior ID (or list of IDS)."),
1080 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1081 Kill inferior ID (or list of IDs)."),
1084 add_cmd ("inferior", class_run, inferior_command, _("\
1085 Use this command to switch between inferiors.\n\
1086 The new inferior ID must be currently known."),
1089 add_setshow_boolean_cmd ("inferior-events", no_class,
1090 &print_inferior_events, _("\
1091 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1092 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1094 show_print_inferior_events,
1095 &setprintlist, &showprintlist);
1097 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);