1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009, 2010, 2011 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);
182 delete_threads_of_inferior (int pid)
184 struct inferior *inf;
185 struct delete_thread_of_inferior_arg arg;
187 for (inf = inferior_list; inf; inf = inf->next)
197 iterate_over_threads (delete_thread_of_inferior, &arg);
200 /* If SILENT then be quiet -- don't announce a inferior death, or the
201 exit of its threads. */
204 delete_inferior_1 (struct inferior *todel, int silent)
206 struct inferior *inf, *infprev;
207 struct delete_thread_of_inferior_arg arg;
211 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
221 iterate_over_threads (delete_thread_of_inferior, &arg);
224 infprev->next = inf->next;
226 inferior_list = inf->next;
228 observer_notify_inferior_removed (inf);
234 delete_inferior (int pid)
236 struct inferior *inf = find_inferior_pid (pid);
238 delete_inferior_1 (inf, 0);
240 if (print_inferior_events)
241 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
245 delete_inferior_silent (int pid)
247 struct inferior *inf = find_inferior_pid (pid);
249 delete_inferior_1 (inf, 1);
253 /* If SILENT then be quiet -- don't announce a inferior exit, or the
254 exit of its threads. */
257 exit_inferior_1 (struct inferior *inftoex, int silent)
259 struct inferior *inf;
260 struct delete_thread_of_inferior_arg arg;
262 for (inf = inferior_list; inf; inf = inf->next)
272 iterate_over_threads (delete_thread_of_inferior, &arg);
274 /* Notify the observers before removing the inferior from the list,
275 so that the observers have a chance to look it up. */
276 observer_notify_inferior_exit (inf);
279 if (inf->vfork_parent != NULL)
281 inf->vfork_parent->vfork_child = NULL;
282 inf->vfork_parent = NULL;
287 exit_inferior (int pid)
289 struct inferior *inf = find_inferior_pid (pid);
291 exit_inferior_1 (inf, 0);
293 if (print_inferior_events)
294 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
298 exit_inferior_silent (int pid)
300 struct inferior *inf = find_inferior_pid (pid);
302 exit_inferior_1 (inf, 1);
306 exit_inferior_num_silent (int num)
308 struct inferior *inf = find_inferior_id (num);
310 exit_inferior_1 (inf, 1);
314 detach_inferior (int pid)
316 struct inferior *inf = find_inferior_pid (pid);
318 exit_inferior_1 (inf, 1);
320 if (print_inferior_events)
321 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
325 inferior_appeared (struct inferior *inf, int pid)
329 observer_notify_inferior_appeared (inf);
333 discard_all_inferiors (void)
335 struct inferior *inf;
337 for (inf = inferior_list; inf; inf = inf->next)
340 exit_inferior_silent (inf->pid);
345 find_inferior_id (int num)
347 struct inferior *inf;
349 for (inf = inferior_list; inf; inf = inf->next)
357 find_inferior_pid (int pid)
359 struct inferior *inf;
361 /* Looking for inferior pid == 0 is always wrong, and indicative of
362 a bug somewhere else. There may be more than one with pid == 0,
364 gdb_assert (pid != 0);
366 for (inf = inferior_list; inf; inf = inf->next)
373 /* Find an inferior bound to PSPACE. */
376 find_inferior_for_program_space (struct program_space *pspace)
378 struct inferior *inf;
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)
464 have_live_inferiors (void)
466 struct inferior *inf;
468 for (inf = inferior_list; inf; inf = inf->next)
471 struct thread_info *tp;
473 tp = any_thread_of_process (inf->pid);
474 if (tp && target_has_execution_1 (tp->ptid))
481 /* Prune away automatically added program spaces that aren't required
485 prune_inferiors (void)
487 struct inferior *ss, **ss_link;
488 struct inferior *current = current_inferior ();
491 ss_link = &inferior_list;
504 delete_inferior_1 (ss, 1);
508 prune_program_spaces ();
511 /* Simply returns the count of inferiors. */
514 number_of_inferiors (void)
516 struct inferior *inf;
519 for (inf = inferior_list; inf != NULL; inf = inf->next)
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);
579 ui_out_field_string (uiout, "target-id",
580 target_pid_to_str (pid_to_ptid (inf->pid)));
582 ui_out_field_string (uiout, "target-id", "<null>");
584 if (inf->pspace->ebfd)
585 ui_out_field_string (uiout, "exec",
586 bfd_get_filename (inf->pspace->ebfd));
588 ui_out_field_skip (uiout, "exec");
590 /* Print extra info that isn't really fit to always present in
591 tabular form. Currently we print the vfork parent/child
592 relationships, if any. */
593 if (inf->vfork_parent)
595 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
596 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
598 if (inf->vfork_child)
600 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
601 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
604 ui_out_text (uiout, "\n");
605 do_cleanups (chain2);
608 do_cleanups (old_chain);
612 detach_inferior_command (char *args, int from_tty)
615 struct thread_info *tp;
616 struct get_number_or_range_state state;
619 error (_("Requires argument (inferior id(s) to detach)"));
621 init_number_or_range (&state, args);
622 while (!state.finished)
624 num = get_number_or_range (&state);
626 if (!valid_gdb_inferior_id (num))
628 warning (_("Inferior ID %d not known."), num);
632 pid = gdb_inferior_id_to_pid (num);
634 tp = any_thread_of_process (pid);
637 warning (_("Inferior ID %d has no threads."), num);
641 switch_to_thread (tp->ptid);
643 detach_command (NULL, from_tty);
648 kill_inferior_command (char *args, int from_tty)
651 struct thread_info *tp;
652 struct get_number_or_range_state state;
655 error (_("Requires argument (inferior id(s) to kill)"));
657 init_number_or_range (&state, args);
658 while (!state.finished)
660 num = get_number_or_range (&state);
662 if (!valid_gdb_inferior_id (num))
664 warning (_("Inferior ID %d not known."), num);
668 pid = gdb_inferior_id_to_pid (num);
670 tp = any_thread_of_process (pid);
673 warning (_("Inferior ID %d has no threads."), num);
677 switch_to_thread (tp->ptid);
682 bfd_cache_close_all ();
686 inferior_command (char *args, int from_tty)
688 struct inferior *inf;
691 num = parse_and_eval_long (args);
693 inf = find_inferior_id (num);
695 error (_("Inferior ID %d not known."), num);
697 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
699 target_pid_to_str (pid_to_ptid (inf->pid)),
701 ? bfd_get_filename (inf->pspace->ebfd)
706 if (inf->pid != ptid_get_pid (inferior_ptid))
708 struct thread_info *tp;
710 tp = any_thread_of_process (inf->pid);
712 error (_("Inferior has no threads."));
714 switch_to_thread (tp->ptid);
717 printf_filtered (_("[Switching to thread %d (%s)] "),
718 pid_to_thread_id (inferior_ptid),
719 target_pid_to_str (inferior_ptid));
723 struct inferior *inf;
725 inf = find_inferior_id (num);
726 set_current_inferior (inf);
727 switch_to_thread (null_ptid);
728 set_current_program_space (inf->pspace);
731 if (inf->pid != 0 && is_running (inferior_ptid))
732 ui_out_text (uiout, "(running)\n");
733 else if (inf->pid != 0)
735 ui_out_text (uiout, "\n");
736 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
740 /* Print information about currently known inferiors. */
743 info_inferiors_command (char *args, int from_tty)
745 print_inferior (uiout, args);
748 /* remove-inferior ID */
751 remove_inferior_command (char *args, int from_tty)
754 struct inferior *inf;
755 struct get_number_or_range_state state;
757 if (args == NULL || *args == '\0')
758 error (_("Requires an argument (inferior id(s) to remove)"));
760 init_number_or_range (&state, args);
761 while (!state.finished)
763 num = get_number_or_range (&state);
764 inf = find_inferior_id (num);
768 warning (_("Inferior ID %d not known."), num);
772 if (inf == current_inferior ())
774 warning (_("Can not remove current symbol inferior %d."), num);
780 warning (_("Can not remove active inferior %d."), num);
784 delete_inferior_1 (inf, 1);
789 add_inferior_with_spaces (void)
791 struct address_space *aspace;
792 struct program_space *pspace;
793 struct inferior *inf;
795 /* If all inferiors share an address space on this system, this
796 doesn't really return a new address space; otherwise, it
798 aspace = maybe_new_address_space ();
799 pspace = add_program_space (aspace);
800 inf = add_inferior (0);
801 inf->pspace = pspace;
802 inf->aspace = pspace->aspace;
807 /* add-inferior [-copies N] [-exec FILENAME] */
810 add_inferior_command (char *args, int from_tty)
815 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
819 argv = gdb_buildargv (args);
820 make_cleanup_freeargv (argv);
822 for (; *argv != NULL; argv++)
826 if (strcmp (*argv, "-copies") == 0)
830 error (_("No argument to -copies"));
831 copies = parse_and_eval_long (*argv);
833 else if (strcmp (*argv, "-exec") == 0)
837 error (_("No argument to -exec"));
842 error (_("Invalid argument"));
846 save_current_space_and_thread ();
848 for (i = 0; i < copies; ++i)
850 struct inferior *inf = add_inferior_with_spaces ();
852 printf_filtered (_("Added inferior %d\n"), inf->num);
856 /* Switch over temporarily, while reading executable and
858 set_current_program_space (inf->pspace);
859 set_current_inferior (inf);
860 switch_to_thread (null_ptid);
862 exec_file_attach (exec, from_tty);
863 symbol_file_add_main (exec, from_tty);
867 do_cleanups (old_chain);
870 /* clone-inferior [-copies N] [ID] */
873 clone_inferior_command (char *args, int from_tty)
877 struct inferior *orginf = NULL;
878 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
882 argv = gdb_buildargv (args);
883 make_cleanup_freeargv (argv);
885 for (; *argv != NULL; argv++)
889 if (strcmp (*argv, "-copies") == 0)
893 error (_("No argument to -copies"));
894 copies = parse_and_eval_long (*argv);
897 error (_("Invalid copies number"));
906 /* The first non-option (-) argument specified the
908 num = parse_and_eval_long (*argv);
909 orginf = find_inferior_id (num);
912 error (_("Inferior ID %d not known."), num);
916 error (_("Invalid argument"));
921 /* If no inferior id was specified, then the user wants to clone the
924 orginf = current_inferior ();
926 save_current_space_and_thread ();
928 for (i = 0; i < copies; ++i)
930 struct address_space *aspace;
931 struct program_space *pspace;
932 struct inferior *inf;
934 /* If all inferiors share an address space on this system, this
935 doesn't really return a new address space; otherwise, it
937 aspace = maybe_new_address_space ();
938 pspace = add_program_space (aspace);
939 inf = add_inferior (0);
940 inf->pspace = pspace;
941 inf->aspace = pspace->aspace;
943 printf_filtered (_("Added inferior %d.\n"), inf->num);
945 set_current_inferior (inf);
946 switch_to_thread (null_ptid);
947 clone_program_space (pspace, orginf->pspace);
950 do_cleanups (old_chain);
953 /* Print notices when new inferiors are created and die. */
955 show_print_inferior_events (struct ui_file *file, int from_tty,
956 struct cmd_list_element *c, const char *value)
958 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
963 /* Keep a registry of per-inferior data-pointers required by other GDB
969 void (*cleanup) (struct inferior *, void *);
972 struct inferior_data_registration
974 struct inferior_data *data;
975 struct inferior_data_registration *next;
978 struct inferior_data_registry
980 struct inferior_data_registration *registrations;
981 unsigned num_registrations;
984 static struct inferior_data_registry inferior_data_registry
987 const struct inferior_data *
988 register_inferior_data_with_cleanup
989 (void (*cleanup) (struct inferior *, void *))
991 struct inferior_data_registration **curr;
993 /* Append new registration. */
994 for (curr = &inferior_data_registry.registrations;
995 *curr != NULL; curr = &(*curr)->next);
997 *curr = XMALLOC (struct inferior_data_registration);
998 (*curr)->next = NULL;
999 (*curr)->data = XMALLOC (struct inferior_data);
1000 (*curr)->data->index = inferior_data_registry.num_registrations++;
1001 (*curr)->data->cleanup = cleanup;
1003 return (*curr)->data;
1006 const struct inferior_data *
1007 register_inferior_data (void)
1009 return register_inferior_data_with_cleanup (NULL);
1013 inferior_alloc_data (struct inferior *inf)
1015 gdb_assert (inf->data == NULL);
1016 inf->num_data = inferior_data_registry.num_registrations;
1017 inf->data = XCALLOC (inf->num_data, void *);
1021 inferior_free_data (struct inferior *inf)
1023 gdb_assert (inf->data != NULL);
1024 clear_inferior_data (inf);
1030 clear_inferior_data (struct inferior *inf)
1032 struct inferior_data_registration *registration;
1035 gdb_assert (inf->data != NULL);
1037 for (registration = inferior_data_registry.registrations, i = 0;
1039 registration = registration->next, i++)
1040 if (inf->data[i] != NULL && registration->data->cleanup)
1041 registration->data->cleanup (inf, inf->data[i]);
1043 memset (inf->data, 0, inf->num_data * sizeof (void *));
1047 set_inferior_data (struct inferior *inf,
1048 const struct inferior_data *data,
1051 gdb_assert (data->index < inf->num_data);
1052 inf->data[data->index] = value;
1056 inferior_data (struct inferior *inf, const struct inferior_data *data)
1058 gdb_assert (data->index < inf->num_data);
1059 return inf->data[data->index];
1063 initialize_inferiors (void)
1065 /* There's always one inferior. Note that this function isn't an
1066 automatic _initialize_foo function, since other _initialize_foo
1067 routines may need to install their per-inferior data keys. We
1068 can only allocate an inferior when all those modules have done
1069 that. Do this after initialize_progspace, due to the
1070 current_program_space reference. */
1071 current_inferior_ = add_inferior (0);
1072 current_inferior_->pspace = current_program_space;
1073 current_inferior_->aspace = current_program_space->aspace;
1075 add_info ("inferiors", info_inferiors_command,
1076 _("IDs of specified inferiors (all inferiors if no argument)."));
1078 add_com ("add-inferior", no_class, add_inferior_command, _("\
1079 Add a new inferior.\n\
1080 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1081 N is the optional number of inferiors to add, default is 1.\n\
1082 FILENAME is the file name of the executable to use\n\
1083 as main program."));
1085 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1086 Remove inferior ID (or list of IDs).\n\
1087 Usage: remove-inferiors ID..."));
1089 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1090 Clone inferior ID.\n\
1091 Usage: clone-inferior [-copies <N>] [ID]\n\
1092 Add N copies of inferior ID. The new inferior has the same\n\
1093 executable loaded as the copied inferior. If -copies is not specified,\n\
1094 adds 1 copy. If ID is not specified, it is the current inferior\n\
1097 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1098 Detach from inferior ID (or list of IDS)."),
1101 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1102 Kill inferior ID (or list of IDs)."),
1105 add_cmd ("inferior", class_run, inferior_command, _("\
1106 Use this command to switch between inferiors.\n\
1107 The new inferior ID must be currently known."),
1110 add_setshow_boolean_cmd ("inferior-events", no_class,
1111 &print_inferior_events, _("\
1112 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1113 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1115 show_print_inferior_events,
1116 &setprintlist, &showprintlist);