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;
270 inf->has_exit_code = 0;
275 exit_inferior (int pid)
277 struct inferior *inf = find_inferior_pid (pid);
279 exit_inferior_1 (inf, 0);
281 if (print_inferior_events)
282 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
286 exit_inferior_silent (int pid)
288 struct inferior *inf = find_inferior_pid (pid);
290 exit_inferior_1 (inf, 1);
294 exit_inferior_num_silent (int num)
296 struct inferior *inf = find_inferior_id (num);
298 exit_inferior_1 (inf, 1);
302 detach_inferior (int pid)
304 struct inferior *inf = find_inferior_pid (pid);
306 exit_inferior_1 (inf, 1);
308 if (print_inferior_events)
309 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
313 inferior_appeared (struct inferior *inf, int pid)
317 observer_notify_inferior_appeared (inf);
321 discard_all_inferiors (void)
323 struct inferior *inf;
325 for (inf = inferior_list; inf; inf = inf->next)
328 exit_inferior_silent (inf->pid);
333 find_inferior_id (int num)
335 struct inferior *inf;
337 for (inf = inferior_list; inf; inf = inf->next)
345 find_inferior_pid (int pid)
347 struct inferior *inf;
349 /* Looking for inferior pid == 0 is always wrong, and indicative of
350 a bug somewhere else. There may be more than one with pid == 0,
352 gdb_assert (pid != 0);
354 for (inf = inferior_list; inf; inf = inf->next)
361 /* Find an inferior bound to PSPACE. */
364 find_inferior_for_program_space (struct program_space *pspace)
366 struct inferior *inf;
368 for (inf = inferior_list; inf != NULL; inf = inf->next)
370 if (inf->pspace == pspace)
378 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
381 struct inferior *inf, *infnext;
383 for (inf = inferior_list; inf; inf = infnext)
386 if ((*callback) (inf, data))
394 valid_gdb_inferior_id (int num)
396 struct inferior *inf;
398 for (inf = inferior_list; inf; inf = inf->next)
406 pid_to_gdb_inferior_id (int pid)
408 struct inferior *inf;
410 for (inf = inferior_list; inf; inf = inf->next)
418 gdb_inferior_id_to_pid (int num)
420 struct inferior *inferior = find_inferior_id (num);
422 return inferior->pid;
428 in_inferior_list (int pid)
430 struct inferior *inf;
432 for (inf = inferior_list; inf; inf = inf->next)
440 have_inferiors (void)
442 struct inferior *inf;
444 for (inf = inferior_list; inf; inf = inf->next)
452 have_live_inferiors (void)
454 struct inferior *inf;
456 for (inf = inferior_list; inf; inf = inf->next)
459 struct thread_info *tp;
461 tp = any_thread_of_process (inf->pid);
462 if (tp && target_has_execution_1 (tp->ptid))
469 /* Prune away automatically added program spaces that aren't required
473 prune_inferiors (void)
475 struct inferior *ss, **ss_link;
476 struct inferior *current = current_inferior ();
479 ss_link = &inferior_list;
492 delete_inferior_1 (ss, 1);
496 prune_program_spaces ();
499 /* Simply returns the count of inferiors. */
502 number_of_inferiors (void)
504 struct inferior *inf;
507 for (inf = inferior_list; inf != NULL; inf = inf->next)
513 /* Converts an inferior process id to a string. Like
514 target_pid_to_str, but special cases the null process. */
517 inferior_pid_to_str (int pid)
520 return target_pid_to_str (pid_to_ptid (pid));
525 /* Prints the list of inferiors and their details on UIOUT. This is a
526 version of 'info_inferior_command' suitable for use from MI.
528 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
529 inferiors that should be printed. Otherwise, all inferiors are
533 print_inferior (struct ui_out *uiout, char *requested_inferiors)
535 struct inferior *inf;
536 struct cleanup *old_chain;
539 /* Compute number of inferiors we will print. */
540 for (inf = inferior_list; inf; inf = inf->next)
542 if (!number_is_in_list (requested_inferiors, inf->num))
550 ui_out_message (uiout, 0, "No inferiors.\n");
554 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
556 ui_out_table_header (uiout, 1, ui_left, "current", "");
557 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
558 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
559 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
561 ui_out_table_body (uiout);
562 for (inf = inferior_list; inf; inf = inf->next)
564 struct cleanup *chain2;
566 if (!number_is_in_list (requested_inferiors, inf->num))
569 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
571 if (inf == current_inferior ())
572 ui_out_field_string (uiout, "current", "*");
574 ui_out_field_skip (uiout, "current");
576 ui_out_field_int (uiout, "number", inf->num);
578 ui_out_field_string (uiout, "target-id",
579 inferior_pid_to_str (inf->pid));
581 if (inf->pspace->ebfd)
582 ui_out_field_string (uiout, "exec",
583 bfd_get_filename (inf->pspace->ebfd));
585 ui_out_field_skip (uiout, "exec");
587 /* Print extra info that isn't really fit to always present in
588 tabular form. Currently we print the vfork parent/child
589 relationships, if any. */
590 if (inf->vfork_parent)
592 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
593 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
595 if (inf->vfork_child)
597 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
598 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
601 ui_out_text (uiout, "\n");
602 do_cleanups (chain2);
605 do_cleanups (old_chain);
609 detach_inferior_command (char *args, int from_tty)
612 struct thread_info *tp;
613 struct get_number_or_range_state state;
616 error (_("Requires argument (inferior id(s) to detach)"));
618 init_number_or_range (&state, args);
619 while (!state.finished)
621 num = get_number_or_range (&state);
623 if (!valid_gdb_inferior_id (num))
625 warning (_("Inferior ID %d not known."), num);
629 pid = gdb_inferior_id_to_pid (num);
631 tp = any_thread_of_process (pid);
634 warning (_("Inferior ID %d has no threads."), num);
638 switch_to_thread (tp->ptid);
640 detach_command (NULL, from_tty);
645 kill_inferior_command (char *args, int from_tty)
648 struct thread_info *tp;
649 struct get_number_or_range_state state;
652 error (_("Requires argument (inferior id(s) to kill)"));
654 init_number_or_range (&state, args);
655 while (!state.finished)
657 num = get_number_or_range (&state);
659 if (!valid_gdb_inferior_id (num))
661 warning (_("Inferior ID %d not known."), num);
665 pid = gdb_inferior_id_to_pid (num);
667 tp = any_thread_of_process (pid);
670 warning (_("Inferior ID %d has no threads."), num);
674 switch_to_thread (tp->ptid);
679 bfd_cache_close_all ();
683 inferior_command (char *args, int from_tty)
685 struct inferior *inf;
688 num = parse_and_eval_long (args);
690 inf = find_inferior_id (num);
692 error (_("Inferior ID %d not known."), num);
694 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
696 inferior_pid_to_str (inf->pid),
698 ? bfd_get_filename (inf->pspace->ebfd)
703 if (inf->pid != ptid_get_pid (inferior_ptid))
705 struct thread_info *tp;
707 tp = any_thread_of_process (inf->pid);
709 error (_("Inferior has no threads."));
711 switch_to_thread (tp->ptid);
714 printf_filtered (_("[Switching to thread %d (%s)] "),
715 pid_to_thread_id (inferior_ptid),
716 target_pid_to_str (inferior_ptid));
720 struct inferior *inf;
722 inf = find_inferior_id (num);
723 set_current_inferior (inf);
724 switch_to_thread (null_ptid);
725 set_current_program_space (inf->pspace);
728 if (inf->pid != 0 && is_running (inferior_ptid))
729 ui_out_text (current_uiout, "(running)\n");
730 else if (inf->pid != 0)
732 ui_out_text (current_uiout, "\n");
733 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
737 /* Print information about currently known inferiors. */
740 info_inferiors_command (char *args, int from_tty)
742 print_inferior (current_uiout, args);
745 /* remove-inferior ID */
748 remove_inferior_command (char *args, int from_tty)
751 struct inferior *inf;
752 struct get_number_or_range_state state;
754 if (args == NULL || *args == '\0')
755 error (_("Requires an argument (inferior id(s) to remove)"));
757 init_number_or_range (&state, args);
758 while (!state.finished)
760 num = get_number_or_range (&state);
761 inf = find_inferior_id (num);
765 warning (_("Inferior ID %d not known."), num);
769 if (inf == current_inferior ())
771 warning (_("Can not remove current symbol inferior %d."), num);
777 warning (_("Can not remove active inferior %d."), num);
781 delete_inferior_1 (inf, 1);
786 add_inferior_with_spaces (void)
788 struct address_space *aspace;
789 struct program_space *pspace;
790 struct inferior *inf;
792 /* If all inferiors share an address space on this system, this
793 doesn't really return a new address space; otherwise, it
795 aspace = maybe_new_address_space ();
796 pspace = add_program_space (aspace);
797 inf = add_inferior (0);
798 inf->pspace = pspace;
799 inf->aspace = pspace->aspace;
804 /* add-inferior [-copies N] [-exec FILENAME] */
807 add_inferior_command (char *args, int from_tty)
812 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
816 argv = gdb_buildargv (args);
817 make_cleanup_freeargv (argv);
819 for (; *argv != NULL; argv++)
823 if (strcmp (*argv, "-copies") == 0)
827 error (_("No argument to -copies"));
828 copies = parse_and_eval_long (*argv);
830 else if (strcmp (*argv, "-exec") == 0)
834 error (_("No argument to -exec"));
839 error (_("Invalid argument"));
843 save_current_space_and_thread ();
845 for (i = 0; i < copies; ++i)
847 struct inferior *inf = add_inferior_with_spaces ();
849 printf_filtered (_("Added inferior %d\n"), inf->num);
853 /* Switch over temporarily, while reading executable and
855 set_current_program_space (inf->pspace);
856 set_current_inferior (inf);
857 switch_to_thread (null_ptid);
859 exec_file_attach (exec, from_tty);
860 symbol_file_add_main (exec, from_tty);
864 do_cleanups (old_chain);
867 /* clone-inferior [-copies N] [ID] */
870 clone_inferior_command (char *args, int from_tty)
874 struct inferior *orginf = NULL;
875 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
879 argv = gdb_buildargv (args);
880 make_cleanup_freeargv (argv);
882 for (; *argv != NULL; argv++)
886 if (strcmp (*argv, "-copies") == 0)
890 error (_("No argument to -copies"));
891 copies = parse_and_eval_long (*argv);
894 error (_("Invalid copies number"));
903 /* The first non-option (-) argument specified the
905 num = parse_and_eval_long (*argv);
906 orginf = find_inferior_id (num);
909 error (_("Inferior ID %d not known."), num);
913 error (_("Invalid argument"));
918 /* If no inferior id was specified, then the user wants to clone the
921 orginf = current_inferior ();
923 save_current_space_and_thread ();
925 for (i = 0; i < copies; ++i)
927 struct address_space *aspace;
928 struct program_space *pspace;
929 struct inferior *inf;
931 /* If all inferiors share an address space on this system, this
932 doesn't really return a new address space; otherwise, it
934 aspace = maybe_new_address_space ();
935 pspace = add_program_space (aspace);
936 inf = add_inferior (0);
937 inf->pspace = pspace;
938 inf->aspace = pspace->aspace;
940 printf_filtered (_("Added inferior %d.\n"), inf->num);
942 set_current_inferior (inf);
943 switch_to_thread (null_ptid);
944 clone_program_space (pspace, orginf->pspace);
947 do_cleanups (old_chain);
950 /* Print notices when new inferiors are created and die. */
952 show_print_inferior_events (struct ui_file *file, int from_tty,
953 struct cmd_list_element *c, const char *value)
955 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
961 initialize_inferiors (void)
963 struct cmd_list_element *c = NULL;
965 /* There's always one inferior. Note that this function isn't an
966 automatic _initialize_foo function, since other _initialize_foo
967 routines may need to install their per-inferior data keys. We
968 can only allocate an inferior when all those modules have done
969 that. Do this after initialize_progspace, due to the
970 current_program_space reference. */
971 current_inferior_ = add_inferior (0);
972 current_inferior_->pspace = current_program_space;
973 current_inferior_->aspace = current_program_space->aspace;
975 add_info ("inferiors", info_inferiors_command,
976 _("IDs of specified inferiors (all inferiors if no argument)."));
978 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
979 Add a new inferior.\n\
980 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
981 N is the optional number of inferiors to add, default is 1.\n\
982 FILENAME is the file name of the executable to use\n\
984 set_cmd_completer (c, filename_completer);
986 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
987 Remove inferior ID (or list of IDs).\n\
988 Usage: remove-inferiors ID..."));
990 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
991 Clone inferior ID.\n\
992 Usage: clone-inferior [-copies <N>] [ID]\n\
993 Add N copies of inferior ID. The new inferior has the same\n\
994 executable loaded as the copied inferior. If -copies is not specified,\n\
995 adds 1 copy. If ID is not specified, it is the current inferior\n\
998 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
999 Detach from inferior ID (or list of IDS)."),
1002 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1003 Kill inferior ID (or list of IDs)."),
1006 add_cmd ("inferior", class_run, inferior_command, _("\
1007 Use this command to switch between inferiors.\n\
1008 The new inferior ID must be currently known."),
1011 add_setshow_boolean_cmd ("inferior-events", no_class,
1012 &print_inferior_events, _("\
1013 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1014 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1016 show_print_inferior_events,
1017 &setprintlist, &showprintlist);