1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009, 2010 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"
34 void _initialize_inferiors (void);
36 static void inferior_alloc_data (struct inferior *inf);
37 static void inferior_free_data (struct inferior *inf);
39 struct inferior *inferior_list = NULL;
40 static int highest_inferior_num;
42 /* Print notices on inferior events (attach, detach, etc.), set with
43 `set print inferior-events'. */
44 static int print_inferior_events = 0;
46 /* The Current Inferior. */
47 static struct inferior *current_inferior_ = NULL;
50 current_inferior (void)
52 return current_inferior_;
56 set_current_inferior (struct inferior *inf)
58 /* There's always an inferior. */
59 gdb_assert (inf != NULL);
61 current_inferior_ = inf;
64 /* A cleanups callback, helper for save_current_program_space
68 restore_inferior (void *arg)
70 struct inferior *saved_inferior = arg;
72 set_current_inferior (saved_inferior);
75 /* Save the current program space so that it may be restored by a later
76 call to do_cleanups. Returns the struct cleanup pointer needed for
77 later doing the cleanup. */
80 save_current_inferior (void)
82 struct cleanup *old_chain = make_cleanup (restore_inferior,
89 free_inferior (struct inferior *inf)
91 discard_all_inferior_continuations (inf);
92 inferior_free_data (inf);
94 xfree (inf->terminal);
95 free_environ (inf->environment);
101 init_inferior_list (void)
103 struct inferior *inf, *infnext;
105 highest_inferior_num = 0;
109 for (inf = inferior_list; inf; inf = infnext)
115 inferior_list = NULL;
119 add_inferior_silent (int pid)
121 struct inferior *inf;
123 inf = xmalloc (sizeof (*inf));
124 memset (inf, 0, sizeof (*inf));
127 inf->control.stop_soon = NO_STOP_QUIETLY;
129 inf->num = ++highest_inferior_num;
130 inf->next = inferior_list;
133 inf->environment = make_environ ();
134 init_environ (inf->environment);
136 inferior_alloc_data (inf);
138 observer_notify_inferior_added (inf);
141 inferior_appeared (inf, pid);
147 add_inferior (int pid)
149 struct inferior *inf = add_inferior_silent (pid);
151 if (print_inferior_events)
152 printf_unfiltered (_("[New inferior %d]\n"), pid);
157 struct delete_thread_of_inferior_arg
164 delete_thread_of_inferior (struct thread_info *tp, void *data)
166 struct delete_thread_of_inferior_arg *arg = data;
168 if (ptid_get_pid (tp->ptid) == arg->pid)
171 delete_thread_silent (tp->ptid);
173 delete_thread (tp->ptid);
180 delete_threads_of_inferior (int pid)
182 struct inferior *inf;
183 struct delete_thread_of_inferior_arg arg;
185 for (inf = inferior_list; inf; inf = inf->next)
195 iterate_over_threads (delete_thread_of_inferior, &arg);
198 /* If SILENT then be quiet -- don't announce a inferior death, or the
199 exit of its threads. */
202 delete_inferior_1 (struct inferior *todel, int silent)
204 struct inferior *inf, *infprev;
205 struct delete_thread_of_inferior_arg arg;
209 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
219 iterate_over_threads (delete_thread_of_inferior, &arg);
222 infprev->next = inf->next;
224 inferior_list = inf->next;
226 observer_notify_inferior_removed (inf);
232 delete_inferior (int pid)
234 struct inferior *inf = find_inferior_pid (pid);
236 delete_inferior_1 (inf, 0);
238 if (print_inferior_events)
239 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
243 delete_inferior_silent (int pid)
245 struct inferior *inf = find_inferior_pid (pid);
247 delete_inferior_1 (inf, 1);
251 /* If SILENT then be quiet -- don't announce a inferior exit, or the
252 exit of its threads. */
255 exit_inferior_1 (struct inferior *inftoex, int silent)
257 struct inferior *inf;
258 struct delete_thread_of_inferior_arg arg;
260 for (inf = inferior_list; inf; inf = inf->next)
270 iterate_over_threads (delete_thread_of_inferior, &arg);
272 /* Notify the observers before removing the inferior from the list,
273 so that the observers have a chance to look it up. */
274 observer_notify_inferior_exit (inf);
277 if (inf->vfork_parent != NULL)
279 inf->vfork_parent->vfork_child = NULL;
280 inf->vfork_parent = NULL;
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 cleanup *old_chain;
465 struct inferior *inf;
467 old_chain = make_cleanup_restore_current_thread ();
469 for (inf = inferior_list; inf; inf = inf->next)
472 struct thread_info *tp;
474 tp = any_thread_of_process (inf->pid);
477 switch_to_thread (tp->ptid);
479 if (target_has_execution)
484 do_cleanups (old_chain);
489 /* Prune away automatically added program spaces that aren't required
493 prune_inferiors (void)
495 struct inferior *ss, **ss_link;
496 struct inferior *current = current_inferior ();
499 ss_link = &inferior_list;
512 delete_inferior_1 (ss, 1);
516 prune_program_spaces ();
519 /* Simply returns the count of inferiors. */
522 number_of_inferiors (void)
524 struct inferior *inf;
527 for (inf = inferior_list; inf != NULL; inf = inf->next)
533 /* Prints the list of inferiors and their details on UIOUT. This is a
534 version of 'info_inferior_command' suitable for use from MI.
536 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
537 should be printed. Otherwise, all inferiors are printed. */
539 print_inferior (struct ui_out *uiout, int requested_inferior)
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 (requested_inferior != -1 && inf->num != requested_inferior)
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 (requested_inferior != -1 && inf->num != requested_inferior)
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);
585 ui_out_field_string (uiout, "target-id",
586 target_pid_to_str (pid_to_ptid (inf->pid)));
588 ui_out_field_string (uiout, "target-id", "<null>");
590 if (inf->pspace->ebfd)
591 ui_out_field_string (uiout, "exec",
592 bfd_get_filename (inf->pspace->ebfd));
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;
624 error (_("Requires argument (inferior id to detach)"));
626 num = parse_and_eval_long (args);
628 if (!valid_gdb_inferior_id (num))
629 error (_("Inferior ID %d not known."), num);
631 pid = gdb_inferior_id_to_pid (num);
633 tp = any_thread_of_process (pid);
635 error (_("Inferior has no threads."));
637 switch_to_thread (tp->ptid);
639 detach_command (NULL, from_tty);
643 kill_inferior_command (char *args, int from_tty)
646 struct thread_info *tp;
649 error (_("Requires argument (inferior id to kill)"));
651 num = parse_and_eval_long (args);
653 if (!valid_gdb_inferior_id (num))
654 error (_("Inferior ID %d not known."), num);
656 pid = gdb_inferior_id_to_pid (num);
658 tp = any_thread_of_process (pid);
660 error (_("Inferior has no threads."));
662 switch_to_thread (tp->ptid);
666 bfd_cache_close_all ();
670 inferior_command (char *args, int from_tty)
672 struct inferior *inf;
675 num = parse_and_eval_long (args);
677 inf = find_inferior_id (num);
679 error (_("Inferior ID %d not known."), num);
681 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
683 target_pid_to_str (pid_to_ptid (inf->pid)),
685 ? bfd_get_filename (inf->pspace->ebfd)
690 if (inf->pid != ptid_get_pid (inferior_ptid))
692 struct thread_info *tp;
694 tp = any_thread_of_process (inf->pid);
696 error (_("Inferior has no threads."));
698 switch_to_thread (tp->ptid);
701 printf_filtered (_("[Switching to thread %d (%s)] "),
702 pid_to_thread_id (inferior_ptid),
703 target_pid_to_str (inferior_ptid));
707 struct inferior *inf;
709 inf = find_inferior_id (num);
710 set_current_inferior (inf);
711 switch_to_thread (null_ptid);
712 set_current_program_space (inf->pspace);
715 if (inf->pid != 0 && is_running (inferior_ptid))
716 ui_out_text (uiout, "(running)\n");
717 else if (inf->pid != 0)
719 ui_out_text (uiout, "\n");
720 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
724 /* Print information about currently known inferiors. */
727 info_inferiors_command (char *args, int from_tty)
733 requested = parse_and_eval_long (args);
734 if (!valid_gdb_inferior_id (requested))
735 error (_("Inferior ID %d not known."), requested);
738 print_inferior (uiout, requested);
741 /* remove-inferior ID */
744 remove_inferior_command (char *args, int from_tty)
747 struct inferior *inf;
749 num = parse_and_eval_long (args);
750 inf = find_inferior_id (num);
753 error (_("Inferior ID %d not known."), num);
755 if (inf == current_inferior ())
756 error (_("Can not remove current symbol inferior."));
759 error (_("Can not remove an active inferior."));
761 delete_inferior_1 (inf, 1);
765 add_inferior_with_spaces (void)
767 struct address_space *aspace;
768 struct program_space *pspace;
769 struct inferior *inf;
771 /* If all inferiors share an address space on this system, this
772 doesn't really return a new address space; otherwise, it
774 aspace = maybe_new_address_space ();
775 pspace = add_program_space (aspace);
776 inf = add_inferior (0);
777 inf->pspace = pspace;
778 inf->aspace = pspace->aspace;
783 /* add-inferior [-copies N] [-exec FILENAME] */
786 add_inferior_command (char *args, int from_tty)
791 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
795 argv = gdb_buildargv (args);
796 make_cleanup_freeargv (argv);
798 for (; *argv != NULL; argv++)
802 if (strcmp (*argv, "-copies") == 0)
806 error (_("No argument to -copies"));
807 copies = parse_and_eval_long (*argv);
809 else if (strcmp (*argv, "-exec") == 0)
813 error (_("No argument to -exec"));
818 error (_("Invalid argument"));
822 save_current_space_and_thread ();
824 for (i = 0; i < copies; ++i)
826 struct inferior *inf = add_inferior_with_spaces ();
828 printf_filtered (_("Added inferior %d\n"), inf->num);
832 /* Switch over temporarily, while reading executable and
834 set_current_program_space (inf->pspace);
835 set_current_inferior (inf);
836 switch_to_thread (null_ptid);
838 exec_file_attach (exec, from_tty);
839 symbol_file_add_main (exec, from_tty);
843 do_cleanups (old_chain);
846 /* clone-inferior [-copies N] [ID] */
849 clone_inferior_command (char *args, int from_tty)
853 struct inferior *orginf = NULL;
854 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
858 argv = gdb_buildargv (args);
859 make_cleanup_freeargv (argv);
861 for (; *argv != NULL; argv++)
865 if (strcmp (*argv, "-copies") == 0)
869 error (_("No argument to -copies"));
870 copies = parse_and_eval_long (*argv);
873 error (_("Invalid copies number"));
882 /* The first non-option (-) argument specified the
884 num = parse_and_eval_long (*argv);
885 orginf = find_inferior_id (num);
888 error (_("Inferior ID %d not known."), num);
892 error (_("Invalid argument"));
897 /* If no inferior id was specified, then the user wants to clone the
900 orginf = current_inferior ();
902 save_current_space_and_thread ();
904 for (i = 0; i < copies; ++i)
906 struct address_space *aspace;
907 struct program_space *pspace;
908 struct inferior *inf;
910 /* If all inferiors share an address space on this system, this
911 doesn't really return a new address space; otherwise, it
913 aspace = maybe_new_address_space ();
914 pspace = add_program_space (aspace);
915 inf = add_inferior (0);
916 inf->pspace = pspace;
917 inf->aspace = pspace->aspace;
919 printf_filtered (_("Added inferior %d.\n"), inf->num);
921 set_current_inferior (inf);
922 switch_to_thread (null_ptid);
923 clone_program_space (pspace, orginf->pspace);
926 do_cleanups (old_chain);
929 /* Print notices when new inferiors are created and die. */
931 show_print_inferior_events (struct ui_file *file, int from_tty,
932 struct cmd_list_element *c, const char *value)
934 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
939 /* Keep a registry of per-inferior data-pointers required by other GDB
945 void (*cleanup) (struct inferior *, void *);
948 struct inferior_data_registration
950 struct inferior_data *data;
951 struct inferior_data_registration *next;
954 struct inferior_data_registry
956 struct inferior_data_registration *registrations;
957 unsigned num_registrations;
960 static struct inferior_data_registry inferior_data_registry
963 const struct inferior_data *
964 register_inferior_data_with_cleanup
965 (void (*cleanup) (struct inferior *, void *))
967 struct inferior_data_registration **curr;
969 /* Append new registration. */
970 for (curr = &inferior_data_registry.registrations;
971 *curr != NULL; curr = &(*curr)->next);
973 *curr = XMALLOC (struct inferior_data_registration);
974 (*curr)->next = NULL;
975 (*curr)->data = XMALLOC (struct inferior_data);
976 (*curr)->data->index = inferior_data_registry.num_registrations++;
977 (*curr)->data->cleanup = cleanup;
979 return (*curr)->data;
982 const struct inferior_data *
983 register_inferior_data (void)
985 return register_inferior_data_with_cleanup (NULL);
989 inferior_alloc_data (struct inferior *inf)
991 gdb_assert (inf->data == NULL);
992 inf->num_data = inferior_data_registry.num_registrations;
993 inf->data = XCALLOC (inf->num_data, void *);
997 inferior_free_data (struct inferior *inf)
999 gdb_assert (inf->data != NULL);
1000 clear_inferior_data (inf);
1006 clear_inferior_data (struct inferior *inf)
1008 struct inferior_data_registration *registration;
1011 gdb_assert (inf->data != NULL);
1013 for (registration = inferior_data_registry.registrations, i = 0;
1015 registration = registration->next, i++)
1016 if (inf->data[i] != NULL && registration->data->cleanup)
1017 registration->data->cleanup (inf, inf->data[i]);
1019 memset (inf->data, 0, inf->num_data * sizeof (void *));
1023 set_inferior_data (struct inferior *inf,
1024 const struct inferior_data *data,
1027 gdb_assert (data->index < inf->num_data);
1028 inf->data[data->index] = value;
1032 inferior_data (struct inferior *inf, const struct inferior_data *data)
1034 gdb_assert (data->index < inf->num_data);
1035 return inf->data[data->index];
1039 initialize_inferiors (void)
1041 /* There's always one inferior. Note that this function isn't an
1042 automatic _initialize_foo function, since other _initialize_foo
1043 routines may need to install their per-inferior data keys. We
1044 can only allocate an inferior when all those modules have done
1045 that. Do this after initialize_progspace, due to the
1046 current_program_space reference. */
1047 current_inferior_ = add_inferior (0);
1048 current_inferior_->pspace = current_program_space;
1049 current_inferior_->aspace = current_program_space->aspace;
1051 add_info ("inferiors", info_inferiors_command,
1052 _("IDs of currently known inferiors."));
1054 add_com ("add-inferior", no_class, add_inferior_command, _("\
1055 Add a new inferior.\n\
1056 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1057 N is the optional number of inferior to add, default is 1.\n\
1058 FILENAME is the file name of the executable to use\n\
1059 as main program."));
1061 add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1062 Remove inferior ID.\n\
1063 Usage: remove-inferior ID"));
1065 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1066 Clone inferior ID.\n\
1067 Usage: clone-inferior [-copies <N>] [ID]\n\
1068 Add N copies of inferior ID. The new inferior has the same\n\
1069 executable loaded as the copied inferior. If -copies is not specified,\n\
1070 adds 1 copy. If ID is not specified, it is the current inferior\n\
1073 add_cmd ("inferior", class_run, detach_inferior_command, _("\
1074 Detach from inferior ID."),
1077 add_cmd ("inferior", class_run, kill_inferior_command, _("\
1078 Kill inferior ID."),
1081 add_cmd ("inferior", class_run, inferior_command, _("\
1082 Use this command to switch between inferiors.\n\
1083 The new inferior ID must be currently known."),
1086 add_setshow_boolean_cmd ("inferior-events", no_class,
1087 &print_inferior_events, _("\
1088 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1089 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1091 show_print_inferior_events,
1092 &setprintlist, &showprintlist);