1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2013 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"
36 #include "arch-utils.h"
37 #include "target-descriptions.h"
38 #include "readline/tilde.h"
40 void _initialize_inferiors (void);
42 /* Keep a registry of per-inferior data-pointers required by other GDB
45 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
47 struct inferior *inferior_list = NULL;
48 static int highest_inferior_num;
50 /* Print notices on inferior events (attach, detach, etc.), set with
51 `set print inferior-events'. */
52 static int print_inferior_events = 0;
54 /* The Current Inferior. */
55 static struct inferior *current_inferior_ = NULL;
58 current_inferior (void)
60 return current_inferior_;
64 set_current_inferior (struct inferior *inf)
66 /* There's always an inferior. */
67 gdb_assert (inf != NULL);
69 current_inferior_ = inf;
72 /* A cleanups callback, helper for save_current_program_space
76 restore_inferior (void *arg)
78 struct inferior *saved_inferior = arg;
80 set_current_inferior (saved_inferior);
83 /* Save the current program space so that it may be restored by a later
84 call to do_cleanups. Returns the struct cleanup pointer needed for
85 later doing the cleanup. */
88 save_current_inferior (void)
90 struct cleanup *old_chain = make_cleanup (restore_inferior,
97 free_inferior (struct inferior *inf)
99 discard_all_inferior_continuations (inf);
100 inferior_free_data (inf);
102 xfree (inf->terminal);
103 free_environ (inf->environment);
104 target_desc_info_free (inf->tdesc_info);
105 xfree (inf->private);
110 init_inferior_list (void)
112 struct inferior *inf, *infnext;
114 highest_inferior_num = 0;
118 for (inf = inferior_list; inf; inf = infnext)
124 inferior_list = NULL;
128 add_inferior_silent (int pid)
130 struct inferior *inf;
132 inf = xmalloc (sizeof (*inf));
133 memset (inf, 0, sizeof (*inf));
136 inf->control.stop_soon = NO_STOP_QUIETLY;
138 inf->num = ++highest_inferior_num;
139 inf->next = inferior_list;
142 inf->environment = make_environ ();
143 init_environ (inf->environment);
145 inferior_alloc_data (inf);
147 observer_notify_inferior_added (inf);
150 inferior_appeared (inf, pid);
156 add_inferior (int pid)
158 struct inferior *inf = add_inferior_silent (pid);
160 if (print_inferior_events)
161 printf_unfiltered (_("[New inferior %d]\n"), pid);
166 struct delete_thread_of_inferior_arg
173 delete_thread_of_inferior (struct thread_info *tp, void *data)
175 struct delete_thread_of_inferior_arg *arg = data;
177 if (ptid_get_pid (tp->ptid) == arg->pid)
180 delete_thread_silent (tp->ptid);
182 delete_thread (tp->ptid);
188 /* If SILENT then be quiet -- don't announce a inferior death, or the
189 exit of its threads. */
192 delete_inferior_1 (struct inferior *todel, int silent)
194 struct inferior *inf, *infprev;
195 struct delete_thread_of_inferior_arg arg;
199 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
209 iterate_over_threads (delete_thread_of_inferior, &arg);
212 infprev->next = inf->next;
214 inferior_list = inf->next;
216 observer_notify_inferior_removed (inf);
222 delete_inferior (int pid)
224 struct inferior *inf = find_inferior_pid (pid);
226 delete_inferior_1 (inf, 0);
228 if (print_inferior_events)
229 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
233 delete_inferior_silent (int pid)
235 struct inferior *inf = find_inferior_pid (pid);
237 delete_inferior_1 (inf, 1);
241 /* If SILENT then be quiet -- don't announce a inferior exit, or the
242 exit of its threads. */
245 exit_inferior_1 (struct inferior *inftoex, int silent)
247 struct inferior *inf;
248 struct delete_thread_of_inferior_arg arg;
250 for (inf = inferior_list; inf; inf = inf->next)
260 iterate_over_threads (delete_thread_of_inferior, &arg);
262 /* Notify the observers before removing the inferior from the list,
263 so that the observers have a chance to look it up. */
264 observer_notify_inferior_exit (inf);
268 if (inf->vfork_parent != NULL)
270 inf->vfork_parent->vfork_child = NULL;
271 inf->vfork_parent = NULL;
273 if (inf->vfork_child != NULL)
275 inf->vfork_child->vfork_parent = NULL;
276 inf->vfork_child = NULL;
279 inf->has_exit_code = 0;
281 inf->pending_detach = 0;
285 exit_inferior (int pid)
287 struct inferior *inf = find_inferior_pid (pid);
289 exit_inferior_1 (inf, 0);
291 if (print_inferior_events)
292 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
296 exit_inferior_silent (int pid)
298 struct inferior *inf = find_inferior_pid (pid);
300 exit_inferior_1 (inf, 1);
304 exit_inferior_num_silent (int num)
306 struct inferior *inf = find_inferior_id (num);
308 exit_inferior_1 (inf, 1);
312 detach_inferior (int pid)
314 struct inferior *inf = find_inferior_pid (pid);
316 exit_inferior_1 (inf, 1);
318 if (print_inferior_events)
319 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
323 inferior_appeared (struct inferior *inf, int pid)
327 observer_notify_inferior_appeared (inf);
331 discard_all_inferiors (void)
333 struct inferior *inf;
335 for (inf = inferior_list; inf; inf = inf->next)
338 exit_inferior_silent (inf->pid);
343 find_inferior_id (int num)
345 struct inferior *inf;
347 for (inf = inferior_list; inf; inf = inf->next)
355 find_inferior_pid (int pid)
357 struct inferior *inf;
359 /* Looking for inferior pid == 0 is always wrong, and indicative of
360 a bug somewhere else. There may be more than one with pid == 0,
362 gdb_assert (pid != 0);
364 for (inf = inferior_list; inf; inf = inf->next)
371 /* Find an inferior bound to PSPACE. */
374 find_inferior_for_program_space (struct program_space *pspace)
376 struct inferior *inf;
378 for (inf = inferior_list; inf != NULL; inf = inf->next)
380 if (inf->pspace == pspace)
388 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
391 struct inferior *inf, *infnext;
393 for (inf = inferior_list; inf; inf = infnext)
396 if ((*callback) (inf, data))
404 valid_gdb_inferior_id (int num)
406 struct inferior *inf;
408 for (inf = inferior_list; inf; inf = inf->next)
416 pid_to_gdb_inferior_id (int pid)
418 struct inferior *inf;
420 for (inf = inferior_list; inf; inf = inf->next)
428 gdb_inferior_id_to_pid (int num)
430 struct inferior *inferior = find_inferior_id (num);
432 return inferior->pid;
438 in_inferior_list (int pid)
440 struct inferior *inf;
442 for (inf = inferior_list; inf; inf = inf->next)
450 have_inferiors (void)
452 struct inferior *inf;
454 for (inf = inferior_list; inf; inf = inf->next)
462 have_live_inferiors (void)
464 struct inferior *inf;
466 for (inf = inferior_list; inf; inf = inf->next)
469 struct thread_info *tp;
471 tp = any_thread_of_process (inf->pid);
472 if (tp && target_has_execution_1 (tp->ptid))
479 /* Prune away automatically added program spaces that aren't required
483 prune_inferiors (void)
485 struct inferior *ss, **ss_link;
486 struct inferior *current = current_inferior ();
489 ss_link = &inferior_list;
502 delete_inferior_1 (ss, 1);
506 prune_program_spaces ();
509 /* Simply returns the count of inferiors. */
512 number_of_inferiors (void)
514 struct inferior *inf;
517 for (inf = inferior_list; inf != NULL; inf = inf->next)
523 /* Converts an inferior process id to a string. Like
524 target_pid_to_str, but special cases the null process. */
527 inferior_pid_to_str (int pid)
530 return target_pid_to_str (pid_to_ptid (pid));
535 /* Prints the list of inferiors and their details on UIOUT. This is a
536 version of 'info_inferior_command' suitable for use from MI.
538 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
539 inferiors that should be printed. Otherwise, all inferiors are
543 print_inferior (struct ui_out *uiout, char *requested_inferiors)
545 struct inferior *inf;
546 struct cleanup *old_chain;
549 /* Compute number of inferiors we will print. */
550 for (inf = inferior_list; inf; inf = inf->next)
552 if (!number_is_in_list (requested_inferiors, inf->num))
560 ui_out_message (uiout, 0, "No inferiors.\n");
564 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
566 ui_out_table_header (uiout, 1, ui_left, "current", "");
567 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
568 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
569 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
571 ui_out_table_body (uiout);
572 for (inf = inferior_list; inf; inf = inf->next)
574 struct cleanup *chain2;
576 if (!number_is_in_list (requested_inferiors, inf->num))
579 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
581 if (inf == current_inferior ())
582 ui_out_field_string (uiout, "current", "*");
584 ui_out_field_skip (uiout, "current");
586 ui_out_field_int (uiout, "number", inf->num);
588 ui_out_field_string (uiout, "target-id",
589 inferior_pid_to_str (inf->pid));
591 if (inf->pspace->pspace_exec_filename != NULL)
592 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
594 ui_out_field_skip (uiout, "exec");
596 /* Print extra info that isn't really fit to always present in
597 tabular form. Currently we print the vfork parent/child
598 relationships, if any. */
599 if (inf->vfork_parent)
601 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
602 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
604 if (inf->vfork_child)
606 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
607 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
610 ui_out_text (uiout, "\n");
611 do_cleanups (chain2);
614 do_cleanups (old_chain);
618 detach_inferior_command (char *args, int from_tty)
621 struct thread_info *tp;
622 struct get_number_or_range_state state;
625 error (_("Requires argument (inferior id(s) to detach)"));
627 init_number_or_range (&state, args);
628 while (!state.finished)
630 num = get_number_or_range (&state);
632 if (!valid_gdb_inferior_id (num))
634 warning (_("Inferior ID %d not known."), num);
638 pid = gdb_inferior_id_to_pid (num);
640 tp = any_thread_of_process (pid);
643 warning (_("Inferior ID %d has no threads."), num);
647 switch_to_thread (tp->ptid);
649 detach_command (NULL, from_tty);
654 kill_inferior_command (char *args, int from_tty)
657 struct thread_info *tp;
658 struct get_number_or_range_state state;
661 error (_("Requires argument (inferior id(s) to kill)"));
663 init_number_or_range (&state, args);
664 while (!state.finished)
666 num = get_number_or_range (&state);
668 if (!valid_gdb_inferior_id (num))
670 warning (_("Inferior ID %d not known."), num);
674 pid = gdb_inferior_id_to_pid (num);
676 tp = any_thread_of_process (pid);
679 warning (_("Inferior ID %d has no threads."), num);
683 switch_to_thread (tp->ptid);
688 bfd_cache_close_all ();
692 inferior_command (char *args, int from_tty)
694 struct inferior *inf;
697 num = parse_and_eval_long (args);
699 inf = find_inferior_id (num);
701 error (_("Inferior ID %d not known."), num);
703 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
705 inferior_pid_to_str (inf->pid),
706 (inf->pspace->pspace_exec_filename != NULL
707 ? inf->pspace->pspace_exec_filename
712 if (inf->pid != ptid_get_pid (inferior_ptid))
714 struct thread_info *tp;
716 tp = any_thread_of_process (inf->pid);
718 error (_("Inferior has no threads."));
720 switch_to_thread (tp->ptid);
723 printf_filtered (_("[Switching to thread %d (%s)] "),
724 pid_to_thread_id (inferior_ptid),
725 target_pid_to_str (inferior_ptid));
729 struct inferior *inf;
731 inf = find_inferior_id (num);
732 set_current_inferior (inf);
733 switch_to_thread (null_ptid);
734 set_current_program_space (inf->pspace);
737 if (inf->pid != 0 && is_running (inferior_ptid))
738 ui_out_text (current_uiout, "(running)\n");
739 else if (inf->pid != 0)
741 ui_out_text (current_uiout, "\n");
742 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
746 /* Print information about currently known inferiors. */
749 info_inferiors_command (char *args, int from_tty)
751 print_inferior (current_uiout, args);
754 /* remove-inferior ID */
757 remove_inferior_command (char *args, int from_tty)
760 struct inferior *inf;
761 struct get_number_or_range_state state;
763 if (args == NULL || *args == '\0')
764 error (_("Requires an argument (inferior id(s) to remove)"));
766 init_number_or_range (&state, args);
767 while (!state.finished)
769 num = get_number_or_range (&state);
770 inf = find_inferior_id (num);
774 warning (_("Inferior ID %d not known."), num);
778 if (inf == current_inferior ())
780 warning (_("Can not remove current symbol inferior %d."), num);
786 warning (_("Can not remove active inferior %d."), num);
790 delete_inferior_1 (inf, 1);
795 add_inferior_with_spaces (void)
797 struct address_space *aspace;
798 struct program_space *pspace;
799 struct inferior *inf;
800 struct gdbarch_info info;
802 /* If all inferiors share an address space on this system, this
803 doesn't really return a new address space; otherwise, it
805 aspace = maybe_new_address_space ();
806 pspace = add_program_space (aspace);
807 inf = add_inferior (0);
808 inf->pspace = pspace;
809 inf->aspace = pspace->aspace;
811 /* Setup the inferior's initial arch, based on information obtained
812 from the global "set ..." options. */
813 gdbarch_info_init (&info);
814 inf->gdbarch = gdbarch_find_by_info (info);
815 /* The "set ..." options reject invalid settings, so we should
816 always have a valid arch by now. */
817 gdb_assert (inf->gdbarch != NULL);
822 /* add-inferior [-copies N] [-exec FILENAME] */
825 add_inferior_command (char *args, int from_tty)
830 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
834 argv = gdb_buildargv (args);
835 make_cleanup_freeargv (argv);
837 for (; *argv != NULL; argv++)
841 if (strcmp (*argv, "-copies") == 0)
845 error (_("No argument to -copies"));
846 copies = parse_and_eval_long (*argv);
848 else if (strcmp (*argv, "-exec") == 0)
852 error (_("No argument to -exec"));
853 exec = tilde_expand (*argv);
854 make_cleanup (xfree, exec);
858 error (_("Invalid argument"));
862 save_current_space_and_thread ();
864 for (i = 0; i < copies; ++i)
866 struct inferior *inf = add_inferior_with_spaces ();
868 printf_filtered (_("Added inferior %d\n"), inf->num);
872 /* Switch over temporarily, while reading executable and
874 set_current_program_space (inf->pspace);
875 set_current_inferior (inf);
876 switch_to_thread (null_ptid);
878 exec_file_attach (exec, from_tty);
879 symbol_file_add_main (exec, from_tty);
883 do_cleanups (old_chain);
886 /* clone-inferior [-copies N] [ID] */
889 clone_inferior_command (char *args, int from_tty)
893 struct inferior *orginf = NULL;
894 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
898 argv = gdb_buildargv (args);
899 make_cleanup_freeargv (argv);
901 for (; *argv != NULL; argv++)
905 if (strcmp (*argv, "-copies") == 0)
909 error (_("No argument to -copies"));
910 copies = parse_and_eval_long (*argv);
913 error (_("Invalid copies number"));
922 /* The first non-option (-) argument specified the
924 num = parse_and_eval_long (*argv);
925 orginf = find_inferior_id (num);
928 error (_("Inferior ID %d not known."), num);
932 error (_("Invalid argument"));
937 /* If no inferior id was specified, then the user wants to clone the
940 orginf = current_inferior ();
942 save_current_space_and_thread ();
944 for (i = 0; i < copies; ++i)
946 struct address_space *aspace;
947 struct program_space *pspace;
948 struct inferior *inf;
950 /* If all inferiors share an address space on this system, this
951 doesn't really return a new address space; otherwise, it
953 aspace = maybe_new_address_space ();
954 pspace = add_program_space (aspace);
955 inf = add_inferior (0);
956 inf->pspace = pspace;
957 inf->aspace = pspace->aspace;
958 inf->gdbarch = orginf->gdbarch;
960 /* If the original inferior had a user specified target
961 description, make the clone use it too. */
962 if (target_desc_info_from_user_p (inf->tdesc_info))
963 copy_inferior_target_desc_info (inf, orginf);
965 printf_filtered (_("Added inferior %d.\n"), inf->num);
967 set_current_inferior (inf);
968 switch_to_thread (null_ptid);
969 clone_program_space (pspace, orginf->pspace);
972 do_cleanups (old_chain);
975 /* Print notices when new inferiors are created and die. */
977 show_print_inferior_events (struct ui_file *file, int from_tty,
978 struct cmd_list_element *c, const char *value)
980 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
986 initialize_inferiors (void)
988 struct cmd_list_element *c = NULL;
990 /* There's always one inferior. Note that this function isn't an
991 automatic _initialize_foo function, since other _initialize_foo
992 routines may need to install their per-inferior data keys. We
993 can only allocate an inferior when all those modules have done
994 that. Do this after initialize_progspace, due to the
995 current_program_space reference. */
996 current_inferior_ = add_inferior (0);
997 current_inferior_->pspace = current_program_space;
998 current_inferior_->aspace = current_program_space->aspace;
999 /* The architecture will be initialized shortly, by
1000 initialize_current_architecture. */
1002 add_info ("inferiors", info_inferiors_command,
1003 _("IDs of specified inferiors (all inferiors if no argument)."));
1005 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1006 Add a new inferior.\n\
1007 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1008 N is the optional number of inferiors to add, default is 1.\n\
1009 FILENAME is the file name of the executable to use\n\
1010 as main program."));
1011 set_cmd_completer (c, filename_completer);
1013 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1014 Remove inferior ID (or list of IDs).\n\
1015 Usage: remove-inferiors ID..."));
1017 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1018 Clone inferior ID.\n\
1019 Usage: clone-inferior [-copies <N>] [ID]\n\
1020 Add N copies of inferior ID. The new inferior has the same\n\
1021 executable loaded as the copied inferior. If -copies is not specified,\n\
1022 adds 1 copy. If ID is not specified, it is the current inferior\n\
1025 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1026 Detach from inferior ID (or list of IDS)."),
1029 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1030 Kill inferior ID (or list of IDs)."),
1033 add_cmd ("inferior", class_run, inferior_command, _("\
1034 Use this command to switch between inferiors.\n\
1035 The new inferior ID must be currently known."),
1038 add_setshow_boolean_cmd ("inferior-events", no_class,
1039 &print_inferior_events, _("\
1040 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1041 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1043 show_print_inferior_events,
1044 &setprintlist, &showprintlist);