1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2012 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"
30 #include "gdbthread.h"
34 #include "cli/cli-utils.h"
35 #include "continuations.h"
37 void _initialize_inferiors (void);
39 /* Keep a registry of per-inferior data-pointers required by other GDB
42 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
44 struct inferior *inferior_list = NULL;
45 static int highest_inferior_num;
47 /* Print notices on inferior events (attach, detach, etc.), set with
48 `set print inferior-events'. */
49 static int print_inferior_events = 0;
51 /* The Current Inferior. */
52 static struct inferior *current_inferior_ = NULL;
55 current_inferior (void)
57 return current_inferior_;
61 set_current_inferior (struct inferior *inf)
63 /* There's always an inferior. */
64 gdb_assert (inf != NULL);
66 current_inferior_ = inf;
69 /* A cleanups callback, helper for save_current_program_space
73 restore_inferior (void *arg)
75 struct inferior *saved_inferior = arg;
77 set_current_inferior (saved_inferior);
80 /* Save the current program space so that it may be restored by a later
81 call to do_cleanups. Returns the struct cleanup pointer needed for
82 later doing the cleanup. */
85 save_current_inferior (void)
87 struct cleanup *old_chain = make_cleanup (restore_inferior,
94 free_inferior (struct inferior *inf)
96 discard_all_inferior_continuations (inf);
97 inferior_free_data (inf);
99 xfree (inf->terminal);
100 free_environ (inf->environment);
101 xfree (inf->private);
106 init_inferior_list (void)
108 struct inferior *inf, *infnext;
110 highest_inferior_num = 0;
114 for (inf = inferior_list; inf; inf = infnext)
120 inferior_list = NULL;
124 add_inferior_silent (int pid)
126 struct inferior *inf;
128 inf = xmalloc (sizeof (*inf));
129 memset (inf, 0, sizeof (*inf));
132 inf->control.stop_soon = NO_STOP_QUIETLY;
134 inf->num = ++highest_inferior_num;
135 inf->next = inferior_list;
138 inf->environment = make_environ ();
139 init_environ (inf->environment);
141 inferior_alloc_data (inf);
143 observer_notify_inferior_added (inf);
146 inferior_appeared (inf, pid);
152 add_inferior (int pid)
154 struct inferior *inf = add_inferior_silent (pid);
156 if (print_inferior_events)
157 printf_unfiltered (_("[New inferior %d]\n"), pid);
162 struct delete_thread_of_inferior_arg
169 delete_thread_of_inferior (struct thread_info *tp, void *data)
171 struct delete_thread_of_inferior_arg *arg = data;
173 if (ptid_get_pid (tp->ptid) == arg->pid)
176 delete_thread_silent (tp->ptid);
178 delete_thread (tp->ptid);
184 /* If SILENT then be quiet -- don't announce a inferior death, or the
185 exit of its threads. */
188 delete_inferior_1 (struct inferior *todel, int silent)
190 struct inferior *inf, *infprev;
191 struct delete_thread_of_inferior_arg arg;
195 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
205 iterate_over_threads (delete_thread_of_inferior, &arg);
208 infprev->next = inf->next;
210 inferior_list = inf->next;
212 observer_notify_inferior_removed (inf);
218 delete_inferior (int pid)
220 struct inferior *inf = find_inferior_pid (pid);
222 delete_inferior_1 (inf, 0);
224 if (print_inferior_events)
225 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
229 delete_inferior_silent (int pid)
231 struct inferior *inf = find_inferior_pid (pid);
233 delete_inferior_1 (inf, 1);
237 /* If SILENT then be quiet -- don't announce a inferior exit, or the
238 exit of its threads. */
241 exit_inferior_1 (struct inferior *inftoex, int silent)
243 struct inferior *inf;
244 struct delete_thread_of_inferior_arg arg;
246 for (inf = inferior_list; inf; inf = inf->next)
256 iterate_over_threads (delete_thread_of_inferior, &arg);
258 /* Notify the observers before removing the inferior from the list,
259 so that the observers have a chance to look it up. */
260 observer_notify_inferior_exit (inf);
264 if (inf->vfork_parent != NULL)
266 inf->vfork_parent->vfork_child = NULL;
267 inf->vfork_parent = NULL;
269 if (inf->vfork_child != NULL)
271 inf->vfork_child->vfork_parent = NULL;
272 inf->vfork_child = NULL;
275 inf->has_exit_code = 0;
277 inf->pending_detach = 0;
281 exit_inferior (int pid)
283 struct inferior *inf = find_inferior_pid (pid);
285 exit_inferior_1 (inf, 0);
287 if (print_inferior_events)
288 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
292 exit_inferior_silent (int pid)
294 struct inferior *inf = find_inferior_pid (pid);
296 exit_inferior_1 (inf, 1);
300 exit_inferior_num_silent (int num)
302 struct inferior *inf = find_inferior_id (num);
304 exit_inferior_1 (inf, 1);
308 detach_inferior (int pid)
310 struct inferior *inf = find_inferior_pid (pid);
312 exit_inferior_1 (inf, 1);
314 if (print_inferior_events)
315 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
319 inferior_appeared (struct inferior *inf, int pid)
323 observer_notify_inferior_appeared (inf);
327 discard_all_inferiors (void)
329 struct inferior *inf;
331 for (inf = inferior_list; inf; inf = inf->next)
334 exit_inferior_silent (inf->pid);
339 find_inferior_id (int num)
341 struct inferior *inf;
343 for (inf = inferior_list; inf; inf = inf->next)
351 find_inferior_pid (int pid)
353 struct inferior *inf;
355 /* Looking for inferior pid == 0 is always wrong, and indicative of
356 a bug somewhere else. There may be more than one with pid == 0,
358 gdb_assert (pid != 0);
360 for (inf = inferior_list; inf; inf = inf->next)
367 /* Find an inferior bound to PSPACE. */
370 find_inferior_for_program_space (struct program_space *pspace)
372 struct inferior *inf;
374 for (inf = inferior_list; inf != NULL; inf = inf->next)
376 if (inf->pspace == pspace)
384 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
387 struct inferior *inf, *infnext;
389 for (inf = inferior_list; inf; inf = infnext)
392 if ((*callback) (inf, data))
400 valid_gdb_inferior_id (int num)
402 struct inferior *inf;
404 for (inf = inferior_list; inf; inf = inf->next)
412 pid_to_gdb_inferior_id (int pid)
414 struct inferior *inf;
416 for (inf = inferior_list; inf; inf = inf->next)
424 gdb_inferior_id_to_pid (int num)
426 struct inferior *inferior = find_inferior_id (num);
428 return inferior->pid;
434 in_inferior_list (int pid)
436 struct inferior *inf;
438 for (inf = inferior_list; inf; inf = inf->next)
446 have_inferiors (void)
448 struct inferior *inf;
450 for (inf = inferior_list; inf; inf = inf->next)
458 have_live_inferiors (void)
460 struct inferior *inf;
462 for (inf = inferior_list; inf; inf = inf->next)
465 struct thread_info *tp;
467 tp = any_thread_of_process (inf->pid);
468 if (tp && target_has_execution_1 (tp->ptid))
475 /* Prune away automatically added program spaces that aren't required
479 prune_inferiors (void)
481 struct inferior *ss, **ss_link;
482 struct inferior *current = current_inferior ();
485 ss_link = &inferior_list;
498 delete_inferior_1 (ss, 1);
502 prune_program_spaces ();
505 /* Simply returns the count of inferiors. */
508 number_of_inferiors (void)
510 struct inferior *inf;
513 for (inf = inferior_list; inf != NULL; inf = inf->next)
519 /* Converts an inferior process id to a string. Like
520 target_pid_to_str, but special cases the null process. */
523 inferior_pid_to_str (int pid)
526 return target_pid_to_str (pid_to_ptid (pid));
531 /* Prints the list of inferiors and their details on UIOUT. This is a
532 version of 'info_inferior_command' suitable for use from MI.
534 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
535 inferiors that should be printed. Otherwise, all inferiors are
539 print_inferior (struct ui_out *uiout, char *requested_inferiors)
541 struct inferior *inf;
542 struct cleanup *old_chain;
545 /* Compute number of inferiors we will print. */
546 for (inf = inferior_list; inf; inf = inf->next)
548 if (!number_is_in_list (requested_inferiors, inf->num))
556 ui_out_message (uiout, 0, "No inferiors.\n");
560 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
562 ui_out_table_header (uiout, 1, ui_left, "current", "");
563 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
564 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
565 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
567 ui_out_table_body (uiout);
568 for (inf = inferior_list; inf; inf = inf->next)
570 struct cleanup *chain2;
572 if (!number_is_in_list (requested_inferiors, inf->num))
575 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
577 if (inf == current_inferior ())
578 ui_out_field_string (uiout, "current", "*");
580 ui_out_field_skip (uiout, "current");
582 ui_out_field_int (uiout, "number", inf->num);
584 ui_out_field_string (uiout, "target-id",
585 inferior_pid_to_str (inf->pid));
587 if (inf->pspace->ebfd)
588 ui_out_field_string (uiout, "exec",
589 bfd_get_filename (inf->pspace->ebfd));
591 ui_out_field_skip (uiout, "exec");
593 /* Print extra info that isn't really fit to always present in
594 tabular form. Currently we print the vfork parent/child
595 relationships, if any. */
596 if (inf->vfork_parent)
598 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
599 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
601 if (inf->vfork_child)
603 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
604 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
607 ui_out_text (uiout, "\n");
608 do_cleanups (chain2);
611 do_cleanups (old_chain);
615 detach_inferior_command (char *args, int from_tty)
618 struct thread_info *tp;
619 struct get_number_or_range_state state;
622 error (_("Requires argument (inferior id(s) to detach)"));
624 init_number_or_range (&state, args);
625 while (!state.finished)
627 num = get_number_or_range (&state);
629 if (!valid_gdb_inferior_id (num))
631 warning (_("Inferior ID %d not known."), num);
635 pid = gdb_inferior_id_to_pid (num);
637 tp = any_thread_of_process (pid);
640 warning (_("Inferior ID %d has no threads."), num);
644 switch_to_thread (tp->ptid);
646 detach_command (NULL, from_tty);
651 kill_inferior_command (char *args, int from_tty)
654 struct thread_info *tp;
655 struct get_number_or_range_state state;
658 error (_("Requires argument (inferior id(s) to kill)"));
660 init_number_or_range (&state, args);
661 while (!state.finished)
663 num = get_number_or_range (&state);
665 if (!valid_gdb_inferior_id (num))
667 warning (_("Inferior ID %d not known."), num);
671 pid = gdb_inferior_id_to_pid (num);
673 tp = any_thread_of_process (pid);
676 warning (_("Inferior ID %d has no threads."), num);
680 switch_to_thread (tp->ptid);
685 bfd_cache_close_all ();
689 inferior_command (char *args, int from_tty)
691 struct inferior *inf;
694 num = parse_and_eval_long (args);
696 inf = find_inferior_id (num);
698 error (_("Inferior ID %d not known."), num);
700 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
702 inferior_pid_to_str (inf->pid),
704 ? bfd_get_filename (inf->pspace->ebfd)
709 if (inf->pid != ptid_get_pid (inferior_ptid))
711 struct thread_info *tp;
713 tp = any_thread_of_process (inf->pid);
715 error (_("Inferior has no threads."));
717 switch_to_thread (tp->ptid);
720 printf_filtered (_("[Switching to thread %d (%s)] "),
721 pid_to_thread_id (inferior_ptid),
722 target_pid_to_str (inferior_ptid));
726 struct inferior *inf;
728 inf = find_inferior_id (num);
729 set_current_inferior (inf);
730 switch_to_thread (null_ptid);
731 set_current_program_space (inf->pspace);
734 if (inf->pid != 0 && is_running (inferior_ptid))
735 ui_out_text (current_uiout, "(running)\n");
736 else if (inf->pid != 0)
738 ui_out_text (current_uiout, "\n");
739 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
743 /* Print information about currently known inferiors. */
746 info_inferiors_command (char *args, int from_tty)
748 print_inferior (current_uiout, args);
751 /* remove-inferior ID */
754 remove_inferior_command (char *args, int from_tty)
757 struct inferior *inf;
758 struct get_number_or_range_state state;
760 if (args == NULL || *args == '\0')
761 error (_("Requires an argument (inferior id(s) to remove)"));
763 init_number_or_range (&state, args);
764 while (!state.finished)
766 num = get_number_or_range (&state);
767 inf = find_inferior_id (num);
771 warning (_("Inferior ID %d not known."), num);
775 if (inf == current_inferior ())
777 warning (_("Can not remove current symbol inferior %d."), num);
783 warning (_("Can not remove active inferior %d."), num);
787 delete_inferior_1 (inf, 1);
792 add_inferior_with_spaces (void)
794 struct address_space *aspace;
795 struct program_space *pspace;
796 struct inferior *inf;
798 /* If all inferiors share an address space on this system, this
799 doesn't really return a new address space; otherwise, it
801 aspace = maybe_new_address_space ();
802 pspace = add_program_space (aspace);
803 inf = add_inferior (0);
804 inf->pspace = pspace;
805 inf->aspace = pspace->aspace;
810 /* add-inferior [-copies N] [-exec FILENAME] */
813 add_inferior_command (char *args, int from_tty)
818 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
822 argv = gdb_buildargv (args);
823 make_cleanup_freeargv (argv);
825 for (; *argv != NULL; argv++)
829 if (strcmp (*argv, "-copies") == 0)
833 error (_("No argument to -copies"));
834 copies = parse_and_eval_long (*argv);
836 else if (strcmp (*argv, "-exec") == 0)
840 error (_("No argument to -exec"));
845 error (_("Invalid argument"));
849 save_current_space_and_thread ();
851 for (i = 0; i < copies; ++i)
853 struct inferior *inf = add_inferior_with_spaces ();
855 printf_filtered (_("Added inferior %d\n"), inf->num);
859 /* Switch over temporarily, while reading executable and
861 set_current_program_space (inf->pspace);
862 set_current_inferior (inf);
863 switch_to_thread (null_ptid);
865 exec_file_attach (exec, from_tty);
866 symbol_file_add_main (exec, from_tty);
870 do_cleanups (old_chain);
873 /* clone-inferior [-copies N] [ID] */
876 clone_inferior_command (char *args, int from_tty)
880 struct inferior *orginf = NULL;
881 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
885 argv = gdb_buildargv (args);
886 make_cleanup_freeargv (argv);
888 for (; *argv != NULL; argv++)
892 if (strcmp (*argv, "-copies") == 0)
896 error (_("No argument to -copies"));
897 copies = parse_and_eval_long (*argv);
900 error (_("Invalid copies number"));
909 /* The first non-option (-) argument specified the
911 num = parse_and_eval_long (*argv);
912 orginf = find_inferior_id (num);
915 error (_("Inferior ID %d not known."), num);
919 error (_("Invalid argument"));
924 /* If no inferior id was specified, then the user wants to clone the
927 orginf = current_inferior ();
929 save_current_space_and_thread ();
931 for (i = 0; i < copies; ++i)
933 struct address_space *aspace;
934 struct program_space *pspace;
935 struct inferior *inf;
937 /* If all inferiors share an address space on this system, this
938 doesn't really return a new address space; otherwise, it
940 aspace = maybe_new_address_space ();
941 pspace = add_program_space (aspace);
942 inf = add_inferior (0);
943 inf->pspace = pspace;
944 inf->aspace = pspace->aspace;
946 printf_filtered (_("Added inferior %d.\n"), inf->num);
948 set_current_inferior (inf);
949 switch_to_thread (null_ptid);
950 clone_program_space (pspace, orginf->pspace);
953 do_cleanups (old_chain);
956 /* Print notices when new inferiors are created and die. */
958 show_print_inferior_events (struct ui_file *file, int from_tty,
959 struct cmd_list_element *c, const char *value)
961 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
967 initialize_inferiors (void)
969 struct cmd_list_element *c = NULL;
971 /* There's always one inferior. Note that this function isn't an
972 automatic _initialize_foo function, since other _initialize_foo
973 routines may need to install their per-inferior data keys. We
974 can only allocate an inferior when all those modules have done
975 that. Do this after initialize_progspace, due to the
976 current_program_space reference. */
977 current_inferior_ = add_inferior (0);
978 current_inferior_->pspace = current_program_space;
979 current_inferior_->aspace = current_program_space->aspace;
981 add_info ("inferiors", info_inferiors_command,
982 _("IDs of specified inferiors (all inferiors if no argument)."));
984 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
985 Add a new inferior.\n\
986 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
987 N is the optional number of inferiors to add, default is 1.\n\
988 FILENAME is the file name of the executable to use\n\
990 set_cmd_completer (c, filename_completer);
992 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
993 Remove inferior ID (or list of IDs).\n\
994 Usage: remove-inferiors ID..."));
996 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
997 Clone inferior ID.\n\
998 Usage: clone-inferior [-copies <N>] [ID]\n\
999 Add N copies of inferior ID. The new inferior has the same\n\
1000 executable loaded as the copied inferior. If -copies is not specified,\n\
1001 adds 1 copy. If ID is not specified, it is the current inferior\n\
1004 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1005 Detach from inferior ID (or list of IDS)."),
1008 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1009 Kill inferior ID (or list of IDs)."),
1012 add_cmd ("inferior", class_run, inferior_command, _("\
1013 Use this command to switch between inferiors.\n\
1014 The new inferior ID must be currently known."),
1017 add_setshow_boolean_cmd ("inferior-events", no_class,
1018 &print_inferior_events, _("\
1019 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1020 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1022 show_print_inferior_events,
1023 &setprintlist, &showprintlist);