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/>. */
26 #include "gdbthread.h"
29 #include "gdbthread.h"
33 #include "cli/cli-utils.h"
34 #include "continuations.h"
36 void _initialize_inferiors (void);
38 static void inferior_alloc_data (struct inferior *inf);
39 static void inferior_free_data (struct inferior *inf);
41 struct inferior *inferior_list = NULL;
42 static int highest_inferior_num;
44 /* Print notices on inferior events (attach, detach, etc.), set with
45 `set print inferior-events'. */
46 static int print_inferior_events = 0;
48 /* The Current Inferior. */
49 static struct inferior *current_inferior_ = NULL;
52 current_inferior (void)
54 return current_inferior_;
58 set_current_inferior (struct inferior *inf)
60 /* There's always an inferior. */
61 gdb_assert (inf != NULL);
63 current_inferior_ = inf;
66 /* A cleanups callback, helper for save_current_program_space
70 restore_inferior (void *arg)
72 struct inferior *saved_inferior = arg;
74 set_current_inferior (saved_inferior);
77 /* Save the current program space so that it may be restored by a later
78 call to do_cleanups. Returns the struct cleanup pointer needed for
79 later doing the cleanup. */
82 save_current_inferior (void)
84 struct cleanup *old_chain = make_cleanup (restore_inferior,
91 free_inferior (struct inferior *inf)
93 discard_all_inferior_continuations (inf);
94 inferior_free_data (inf);
96 xfree (inf->terminal);
97 free_environ (inf->environment);
103 init_inferior_list (void)
105 struct inferior *inf, *infnext;
107 highest_inferior_num = 0;
111 for (inf = inferior_list; inf; inf = infnext)
117 inferior_list = NULL;
121 add_inferior_silent (int pid)
123 struct inferior *inf;
125 inf = xmalloc (sizeof (*inf));
126 memset (inf, 0, sizeof (*inf));
129 inf->control.stop_soon = NO_STOP_QUIETLY;
131 inf->num = ++highest_inferior_num;
132 inf->next = inferior_list;
135 inf->environment = make_environ ();
136 init_environ (inf->environment);
138 inferior_alloc_data (inf);
140 observer_notify_inferior_added (inf);
143 inferior_appeared (inf, pid);
149 add_inferior (int pid)
151 struct inferior *inf = add_inferior_silent (pid);
153 if (print_inferior_events)
154 printf_unfiltered (_("[New inferior %d]\n"), pid);
159 struct delete_thread_of_inferior_arg
166 delete_thread_of_inferior (struct thread_info *tp, void *data)
168 struct delete_thread_of_inferior_arg *arg = data;
170 if (ptid_get_pid (tp->ptid) == arg->pid)
173 delete_thread_silent (tp->ptid);
175 delete_thread (tp->ptid);
181 /* If SILENT then be quiet -- don't announce a inferior death, or the
182 exit of its threads. */
185 delete_inferior_1 (struct inferior *todel, int silent)
187 struct inferior *inf, *infprev;
188 struct delete_thread_of_inferior_arg arg;
192 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
202 iterate_over_threads (delete_thread_of_inferior, &arg);
205 infprev->next = inf->next;
207 inferior_list = inf->next;
209 observer_notify_inferior_removed (inf);
215 delete_inferior (int pid)
217 struct inferior *inf = find_inferior_pid (pid);
219 delete_inferior_1 (inf, 0);
221 if (print_inferior_events)
222 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
226 delete_inferior_silent (int pid)
228 struct inferior *inf = find_inferior_pid (pid);
230 delete_inferior_1 (inf, 1);
234 /* If SILENT then be quiet -- don't announce a inferior exit, or the
235 exit of its threads. */
238 exit_inferior_1 (struct inferior *inftoex, int silent)
240 struct inferior *inf;
241 struct delete_thread_of_inferior_arg arg;
243 for (inf = inferior_list; inf; inf = inf->next)
253 iterate_over_threads (delete_thread_of_inferior, &arg);
255 /* Notify the observers before removing the inferior from the list,
256 so that the observers have a chance to look it up. */
257 observer_notify_inferior_exit (inf);
261 if (inf->vfork_parent != NULL)
263 inf->vfork_parent->vfork_child = NULL;
264 inf->vfork_parent = NULL;
267 inf->has_exit_code = 0;
272 exit_inferior (int pid)
274 struct inferior *inf = find_inferior_pid (pid);
276 exit_inferior_1 (inf, 0);
278 if (print_inferior_events)
279 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
283 exit_inferior_silent (int pid)
285 struct inferior *inf = find_inferior_pid (pid);
287 exit_inferior_1 (inf, 1);
291 exit_inferior_num_silent (int num)
293 struct inferior *inf = find_inferior_id (num);
295 exit_inferior_1 (inf, 1);
299 detach_inferior (int pid)
301 struct inferior *inf = find_inferior_pid (pid);
303 exit_inferior_1 (inf, 1);
305 if (print_inferior_events)
306 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
310 inferior_appeared (struct inferior *inf, int pid)
314 observer_notify_inferior_appeared (inf);
318 discard_all_inferiors (void)
320 struct inferior *inf;
322 for (inf = inferior_list; inf; inf = inf->next)
325 exit_inferior_silent (inf->pid);
330 find_inferior_id (int num)
332 struct inferior *inf;
334 for (inf = inferior_list; inf; inf = inf->next)
342 find_inferior_pid (int pid)
344 struct inferior *inf;
346 /* Looking for inferior pid == 0 is always wrong, and indicative of
347 a bug somewhere else. There may be more than one with pid == 0,
349 gdb_assert (pid != 0);
351 for (inf = inferior_list; inf; inf = inf->next)
358 /* Find an inferior bound to PSPACE. */
361 find_inferior_for_program_space (struct program_space *pspace)
363 struct inferior *inf;
365 for (inf = inferior_list; inf != NULL; inf = inf->next)
367 if (inf->pspace == pspace)
375 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
378 struct inferior *inf, *infnext;
380 for (inf = inferior_list; inf; inf = infnext)
383 if ((*callback) (inf, data))
391 valid_gdb_inferior_id (int num)
393 struct inferior *inf;
395 for (inf = inferior_list; inf; inf = inf->next)
403 pid_to_gdb_inferior_id (int pid)
405 struct inferior *inf;
407 for (inf = inferior_list; inf; inf = inf->next)
415 gdb_inferior_id_to_pid (int num)
417 struct inferior *inferior = find_inferior_id (num);
419 return inferior->pid;
425 in_inferior_list (int pid)
427 struct inferior *inf;
429 for (inf = inferior_list; inf; inf = inf->next)
437 have_inferiors (void)
439 struct inferior *inf;
441 for (inf = inferior_list; inf; inf = inf->next)
449 have_live_inferiors (void)
451 struct inferior *inf;
453 for (inf = inferior_list; inf; inf = inf->next)
456 struct thread_info *tp;
458 tp = any_thread_of_process (inf->pid);
459 if (tp && target_has_execution_1 (tp->ptid))
466 /* Prune away automatically added program spaces that aren't required
470 prune_inferiors (void)
472 struct inferior *ss, **ss_link;
473 struct inferior *current = current_inferior ();
476 ss_link = &inferior_list;
489 delete_inferior_1 (ss, 1);
493 prune_program_spaces ();
496 /* Simply returns the count of inferiors. */
499 number_of_inferiors (void)
501 struct inferior *inf;
504 for (inf = inferior_list; inf != NULL; inf = inf->next)
510 /* Converts an inferior process id to a string. Like
511 target_pid_to_str, but special cases the null process. */
514 inferior_pid_to_str (int pid)
517 return target_pid_to_str (pid_to_ptid (pid));
522 /* Prints the list of inferiors and their details on UIOUT. This is a
523 version of 'info_inferior_command' suitable for use from MI.
525 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
526 inferiors that should be printed. Otherwise, all inferiors are
530 print_inferior (struct ui_out *uiout, char *requested_inferiors)
532 struct inferior *inf;
533 struct cleanup *old_chain;
536 /* Compute number of inferiors we will print. */
537 for (inf = inferior_list; inf; inf = inf->next)
539 if (!number_is_in_list (requested_inferiors, inf->num))
547 ui_out_message (uiout, 0, "No inferiors.\n");
551 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
553 ui_out_table_header (uiout, 1, ui_left, "current", "");
554 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
555 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
556 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
558 ui_out_table_body (uiout);
559 for (inf = inferior_list; inf; inf = inf->next)
561 struct cleanup *chain2;
563 if (!number_is_in_list (requested_inferiors, inf->num))
566 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
568 if (inf == current_inferior ())
569 ui_out_field_string (uiout, "current", "*");
571 ui_out_field_skip (uiout, "current");
573 ui_out_field_int (uiout, "number", inf->num);
575 ui_out_field_string (uiout, "target-id",
576 inferior_pid_to_str (inf->pid));
578 if (inf->pspace->ebfd)
579 ui_out_field_string (uiout, "exec",
580 bfd_get_filename (inf->pspace->ebfd));
582 ui_out_field_skip (uiout, "exec");
584 /* Print extra info that isn't really fit to always present in
585 tabular form. Currently we print the vfork parent/child
586 relationships, if any. */
587 if (inf->vfork_parent)
589 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
590 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
592 if (inf->vfork_child)
594 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
595 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
598 ui_out_text (uiout, "\n");
599 do_cleanups (chain2);
602 do_cleanups (old_chain);
606 detach_inferior_command (char *args, int from_tty)
609 struct thread_info *tp;
610 struct get_number_or_range_state state;
613 error (_("Requires argument (inferior id(s) to detach)"));
615 init_number_or_range (&state, args);
616 while (!state.finished)
618 num = get_number_or_range (&state);
620 if (!valid_gdb_inferior_id (num))
622 warning (_("Inferior ID %d not known."), num);
626 pid = gdb_inferior_id_to_pid (num);
628 tp = any_thread_of_process (pid);
631 warning (_("Inferior ID %d has no threads."), num);
635 switch_to_thread (tp->ptid);
637 detach_command (NULL, from_tty);
642 kill_inferior_command (char *args, int from_tty)
645 struct thread_info *tp;
646 struct get_number_or_range_state state;
649 error (_("Requires argument (inferior id(s) to kill)"));
651 init_number_or_range (&state, args);
652 while (!state.finished)
654 num = get_number_or_range (&state);
656 if (!valid_gdb_inferior_id (num))
658 warning (_("Inferior ID %d not known."), num);
662 pid = gdb_inferior_id_to_pid (num);
664 tp = any_thread_of_process (pid);
667 warning (_("Inferior ID %d has no threads."), num);
671 switch_to_thread (tp->ptid);
676 bfd_cache_close_all ();
680 inferior_command (char *args, int from_tty)
682 struct inferior *inf;
685 num = parse_and_eval_long (args);
687 inf = find_inferior_id (num);
689 error (_("Inferior ID %d not known."), num);
691 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
693 inferior_pid_to_str (inf->pid),
695 ? bfd_get_filename (inf->pspace->ebfd)
700 if (inf->pid != ptid_get_pid (inferior_ptid))
702 struct thread_info *tp;
704 tp = any_thread_of_process (inf->pid);
706 error (_("Inferior has no threads."));
708 switch_to_thread (tp->ptid);
711 printf_filtered (_("[Switching to thread %d (%s)] "),
712 pid_to_thread_id (inferior_ptid),
713 target_pid_to_str (inferior_ptid));
717 struct inferior *inf;
719 inf = find_inferior_id (num);
720 set_current_inferior (inf);
721 switch_to_thread (null_ptid);
722 set_current_program_space (inf->pspace);
725 if (inf->pid != 0 && is_running (inferior_ptid))
726 ui_out_text (current_uiout, "(running)\n");
727 else if (inf->pid != 0)
729 ui_out_text (current_uiout, "\n");
730 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
734 /* Print information about currently known inferiors. */
737 info_inferiors_command (char *args, int from_tty)
739 print_inferior (current_uiout, args);
742 /* remove-inferior ID */
745 remove_inferior_command (char *args, int from_tty)
748 struct inferior *inf;
749 struct get_number_or_range_state state;
751 if (args == NULL || *args == '\0')
752 error (_("Requires an argument (inferior id(s) to remove)"));
754 init_number_or_range (&state, args);
755 while (!state.finished)
757 num = get_number_or_range (&state);
758 inf = find_inferior_id (num);
762 warning (_("Inferior ID %d not known."), num);
766 if (inf == current_inferior ())
768 warning (_("Can not remove current symbol inferior %d."), num);
774 warning (_("Can not remove active inferior %d."), num);
778 delete_inferior_1 (inf, 1);
783 add_inferior_with_spaces (void)
785 struct address_space *aspace;
786 struct program_space *pspace;
787 struct inferior *inf;
789 /* If all inferiors share an address space on this system, this
790 doesn't really return a new address space; otherwise, it
792 aspace = maybe_new_address_space ();
793 pspace = add_program_space (aspace);
794 inf = add_inferior (0);
795 inf->pspace = pspace;
796 inf->aspace = pspace->aspace;
801 /* add-inferior [-copies N] [-exec FILENAME] */
804 add_inferior_command (char *args, int from_tty)
809 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
813 argv = gdb_buildargv (args);
814 make_cleanup_freeargv (argv);
816 for (; *argv != NULL; argv++)
820 if (strcmp (*argv, "-copies") == 0)
824 error (_("No argument to -copies"));
825 copies = parse_and_eval_long (*argv);
827 else if (strcmp (*argv, "-exec") == 0)
831 error (_("No argument to -exec"));
836 error (_("Invalid argument"));
840 save_current_space_and_thread ();
842 for (i = 0; i < copies; ++i)
844 struct inferior *inf = add_inferior_with_spaces ();
846 printf_filtered (_("Added inferior %d\n"), inf->num);
850 /* Switch over temporarily, while reading executable and
852 set_current_program_space (inf->pspace);
853 set_current_inferior (inf);
854 switch_to_thread (null_ptid);
856 exec_file_attach (exec, from_tty);
857 symbol_file_add_main (exec, from_tty);
861 do_cleanups (old_chain);
864 /* clone-inferior [-copies N] [ID] */
867 clone_inferior_command (char *args, int from_tty)
871 struct inferior *orginf = NULL;
872 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
876 argv = gdb_buildargv (args);
877 make_cleanup_freeargv (argv);
879 for (; *argv != NULL; argv++)
883 if (strcmp (*argv, "-copies") == 0)
887 error (_("No argument to -copies"));
888 copies = parse_and_eval_long (*argv);
891 error (_("Invalid copies number"));
900 /* The first non-option (-) argument specified the
902 num = parse_and_eval_long (*argv);
903 orginf = find_inferior_id (num);
906 error (_("Inferior ID %d not known."), num);
910 error (_("Invalid argument"));
915 /* If no inferior id was specified, then the user wants to clone the
918 orginf = current_inferior ();
920 save_current_space_and_thread ();
922 for (i = 0; i < copies; ++i)
924 struct address_space *aspace;
925 struct program_space *pspace;
926 struct inferior *inf;
928 /* If all inferiors share an address space on this system, this
929 doesn't really return a new address space; otherwise, it
931 aspace = maybe_new_address_space ();
932 pspace = add_program_space (aspace);
933 inf = add_inferior (0);
934 inf->pspace = pspace;
935 inf->aspace = pspace->aspace;
937 printf_filtered (_("Added inferior %d.\n"), inf->num);
939 set_current_inferior (inf);
940 switch_to_thread (null_ptid);
941 clone_program_space (pspace, orginf->pspace);
944 do_cleanups (old_chain);
947 /* Print notices when new inferiors are created and die. */
949 show_print_inferior_events (struct ui_file *file, int from_tty,
950 struct cmd_list_element *c, const char *value)
952 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
957 /* Keep a registry of per-inferior data-pointers required by other GDB
963 void (*cleanup) (struct inferior *, void *);
966 struct inferior_data_registration
968 struct inferior_data *data;
969 struct inferior_data_registration *next;
972 struct inferior_data_registry
974 struct inferior_data_registration *registrations;
975 unsigned num_registrations;
978 static struct inferior_data_registry inferior_data_registry
981 const struct inferior_data *
982 register_inferior_data_with_cleanup
983 (void (*cleanup) (struct inferior *, void *))
985 struct inferior_data_registration **curr;
987 /* Append new registration. */
988 for (curr = &inferior_data_registry.registrations;
989 *curr != NULL; curr = &(*curr)->next);
991 *curr = XMALLOC (struct inferior_data_registration);
992 (*curr)->next = NULL;
993 (*curr)->data = XMALLOC (struct inferior_data);
994 (*curr)->data->index = inferior_data_registry.num_registrations++;
995 (*curr)->data->cleanup = cleanup;
997 return (*curr)->data;
1000 const struct inferior_data *
1001 register_inferior_data (void)
1003 return register_inferior_data_with_cleanup (NULL);
1007 inferior_alloc_data (struct inferior *inf)
1009 gdb_assert (inf->data == NULL);
1010 inf->num_data = inferior_data_registry.num_registrations;
1011 inf->data = XCALLOC (inf->num_data, void *);
1015 inferior_free_data (struct inferior *inf)
1017 gdb_assert (inf->data != NULL);
1018 clear_inferior_data (inf);
1024 clear_inferior_data (struct inferior *inf)
1026 struct inferior_data_registration *registration;
1029 gdb_assert (inf->data != NULL);
1031 for (registration = inferior_data_registry.registrations, i = 0;
1033 registration = registration->next, i++)
1034 if (inf->data[i] != NULL && registration->data->cleanup)
1035 registration->data->cleanup (inf, inf->data[i]);
1037 memset (inf->data, 0, inf->num_data * sizeof (void *));
1041 set_inferior_data (struct inferior *inf,
1042 const struct inferior_data *data,
1045 gdb_assert (data->index < inf->num_data);
1046 inf->data[data->index] = value;
1050 inferior_data (struct inferior *inf, const struct inferior_data *data)
1052 gdb_assert (data->index < inf->num_data);
1053 return inf->data[data->index];
1057 initialize_inferiors (void)
1059 /* There's always one inferior. Note that this function isn't an
1060 automatic _initialize_foo function, since other _initialize_foo
1061 routines may need to install their per-inferior data keys. We
1062 can only allocate an inferior when all those modules have done
1063 that. Do this after initialize_progspace, due to the
1064 current_program_space reference. */
1065 current_inferior_ = add_inferior (0);
1066 current_inferior_->pspace = current_program_space;
1067 current_inferior_->aspace = current_program_space->aspace;
1069 add_info ("inferiors", info_inferiors_command,
1070 _("IDs of specified inferiors (all inferiors if no argument)."));
1072 add_com ("add-inferior", no_class, add_inferior_command, _("\
1073 Add a new inferior.\n\
1074 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1075 N is the optional number of inferiors to add, default is 1.\n\
1076 FILENAME is the file name of the executable to use\n\
1077 as main program."));
1079 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1080 Remove inferior ID (or list of IDs).\n\
1081 Usage: remove-inferiors ID..."));
1083 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1084 Clone inferior ID.\n\
1085 Usage: clone-inferior [-copies <N>] [ID]\n\
1086 Add N copies of inferior ID. The new inferior has the same\n\
1087 executable loaded as the copied inferior. If -copies is not specified,\n\
1088 adds 1 copy. If ID is not specified, it is the current inferior\n\
1091 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1092 Detach from inferior ID (or list of IDS)."),
1095 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1096 Kill inferior ID (or list of IDs)."),
1099 add_cmd ("inferior", class_run, inferior_command, _("\
1100 Use this command to switch between inferiors.\n\
1101 The new inferior ID must be currently known."),
1104 add_setshow_boolean_cmd ("inferior-events", no_class,
1105 &print_inferior_events, _("\
1106 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1107 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1109 show_print_inferior_events,
1110 &setprintlist, &showprintlist);