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;
71 set_current_inferior (saved_inferior);
74 /* Save the current program space so that it may be restored by a later
75 call to do_cleanups. Returns the struct cleanup pointer needed for
76 later doing the cleanup. */
79 save_current_inferior (void)
81 struct cleanup *old_chain = make_cleanup (restore_inferior,
87 free_inferior (struct inferior *inf)
89 discard_all_inferior_continuations (inf);
90 inferior_free_data (inf);
92 xfree (inf->terminal);
93 free_environ (inf->environment);
99 init_inferior_list (void)
101 struct inferior *inf, *infnext;
103 highest_inferior_num = 0;
107 for (inf = inferior_list; inf; inf = infnext)
113 inferior_list = NULL;
117 add_inferior_silent (int pid)
119 struct inferior *inf;
121 inf = xmalloc (sizeof (*inf));
122 memset (inf, 0, sizeof (*inf));
125 inf->stop_soon = NO_STOP_QUIETLY;
127 inf->num = ++highest_inferior_num;
128 inf->next = inferior_list;
131 inf->environment = make_environ ();
132 init_environ (inf->environment);
134 inferior_alloc_data (inf);
137 inferior_appeared (inf, pid);
143 add_inferior (int pid)
145 struct inferior *inf = add_inferior_silent (pid);
147 if (print_inferior_events)
148 printf_unfiltered (_("[New inferior %d]\n"), pid);
153 struct delete_thread_of_inferior_arg
160 delete_thread_of_inferior (struct thread_info *tp, void *data)
162 struct delete_thread_of_inferior_arg *arg = data;
164 if (ptid_get_pid (tp->ptid) == arg->pid)
167 delete_thread_silent (tp->ptid);
169 delete_thread (tp->ptid);
176 delete_threads_of_inferior (int pid)
178 struct inferior *inf;
179 struct delete_thread_of_inferior_arg arg;
181 for (inf = inferior_list; inf; inf = inf->next)
191 iterate_over_threads (delete_thread_of_inferior, &arg);
194 /* If SILENT then be quiet -- don't announce a inferior death, or the
195 exit of its threads. */
198 delete_inferior_1 (struct inferior *todel, int silent)
200 struct inferior *inf, *infprev;
201 struct delete_thread_of_inferior_arg arg;
205 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
215 iterate_over_threads (delete_thread_of_inferior, &arg);
218 infprev->next = inf->next;
220 inferior_list = inf->next;
226 delete_inferior (int pid)
228 struct inferior *inf = find_inferior_pid (pid);
230 delete_inferior_1 (inf, 0);
232 if (print_inferior_events)
233 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
237 delete_inferior_silent (int pid)
239 struct inferior *inf = find_inferior_pid (pid);
241 delete_inferior_1 (inf, 1);
245 /* If SILENT then be quiet -- don't announce a inferior exit, or the
246 exit of its threads. */
249 exit_inferior_1 (struct inferior *inftoex, int silent)
251 struct inferior *inf;
252 struct delete_thread_of_inferior_arg arg;
254 for (inf = inferior_list; inf; inf = inf->next)
264 iterate_over_threads (delete_thread_of_inferior, &arg);
266 /* Notify the observers before removing the inferior from the list,
267 so that the observers have a chance to look it up. */
268 observer_notify_inferior_exit (inf->pid);
271 if (inf->vfork_parent != NULL)
273 inf->vfork_parent->vfork_child = NULL;
274 inf->vfork_parent = NULL;
279 exit_inferior (int pid)
281 struct inferior *inf = find_inferior_pid (pid);
282 exit_inferior_1 (inf, 0);
284 if (print_inferior_events)
285 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
289 exit_inferior_silent (int pid)
291 struct inferior *inf = find_inferior_pid (pid);
292 exit_inferior_1 (inf, 1);
296 exit_inferior_num_silent (int num)
298 struct inferior *inf = find_inferior_id (num);
300 exit_inferior_1 (inf, 1);
304 detach_inferior (int pid)
306 struct inferior *inf = find_inferior_pid (pid);
307 exit_inferior_1 (inf, 1);
309 if (print_inferior_events)
310 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
314 inferior_appeared (struct inferior *inf, int pid)
318 observer_notify_inferior_appeared (pid);
322 discard_all_inferiors (void)
324 struct inferior *inf;
326 for (inf = inferior_list; inf; inf = inf->next)
329 exit_inferior_silent (inf->pid);
334 find_inferior_id (int num)
336 struct inferior *inf;
338 for (inf = inferior_list; inf; inf = inf->next)
346 find_inferior_pid (int pid)
348 struct inferior *inf;
350 /* Looking for inferior pid == 0 is always wrong, and indicative of
351 a bug somewhere else. There may be more than one with pid == 0,
353 gdb_assert (pid != 0);
355 for (inf = inferior_list; inf; inf = inf->next)
362 /* Find an inferior bound to PSPACE. */
365 find_inferior_for_program_space (struct program_space *pspace)
367 struct inferior *inf;
369 for (inf = inferior_list; inf != NULL; inf = inf->next)
371 if (inf->pspace == pspace)
379 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
382 struct inferior *inf, *infnext;
384 for (inf = inferior_list; inf; inf = infnext)
387 if ((*callback) (inf, data))
395 valid_gdb_inferior_id (int num)
397 struct inferior *inf;
399 for (inf = inferior_list; inf; inf = inf->next)
407 pid_to_gdb_inferior_id (int pid)
409 struct inferior *inf;
411 for (inf = inferior_list; inf; inf = inf->next)
419 gdb_inferior_id_to_pid (int num)
421 struct inferior *inferior = find_inferior_id (num);
423 return inferior->pid;
429 in_inferior_list (int pid)
431 struct inferior *inf;
433 for (inf = inferior_list; inf; inf = inf->next)
441 have_inferiors (void)
443 struct inferior *inf;
445 for (inf = inferior_list; inf; inf = inf->next)
453 have_live_inferiors (void)
455 struct target_ops *t;
457 /* The check on stratum suffices, as GDB doesn't currently support
458 multiple target interfaces. */
459 if (have_inferiors ())
460 for (t = current_target.beneath; t != NULL; t = t->beneath)
461 if (t->to_stratum == process_stratum)
467 /* Prune away automatically added program spaces that aren't required
471 prune_inferiors (void)
473 struct inferior *ss, **ss_link;
474 struct inferior *current = current_inferior ();
477 ss_link = &inferior_list;
490 delete_inferior_1 (ss, 1);
494 prune_program_spaces ();
497 /* Simply returns the count of inferiors. */
500 number_of_inferiors (void)
502 struct inferior *inf;
505 for (inf = inferior_list; inf != NULL; inf = inf->next)
511 /* Prints the list of inferiors and their details on UIOUT. This is a
512 version of 'info_inferior_command' suitable for use from MI.
514 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
515 should be printed. Otherwise, all inferiors are printed. */
517 print_inferior (struct ui_out *uiout, int requested_inferior)
519 struct inferior *inf;
520 struct cleanup *old_chain;
523 /* Compute number of inferiors we will print. */
524 for (inf = inferior_list; inf; inf = inf->next)
526 struct cleanup *chain2;
528 if (requested_inferior != -1 && inf->num != requested_inferior)
536 ui_out_message (uiout, 0, "No inferiors.\n");
540 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
542 ui_out_table_header (uiout, 1, ui_left, "current", "");
543 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
544 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
545 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
547 ui_out_table_body (uiout);
548 for (inf = inferior_list; inf; inf = inf->next)
550 struct cleanup *chain2;
552 if (requested_inferior != -1 && inf->num != requested_inferior)
555 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
557 if (inf == current_inferior ())
558 ui_out_field_string (uiout, "current", "*");
560 ui_out_field_skip (uiout, "current");
562 ui_out_field_int (uiout, "number", inf->num);
565 ui_out_field_string (uiout, "target-id",
566 target_pid_to_str (pid_to_ptid (inf->pid)));
568 ui_out_field_string (uiout, "target-id", "<null>");
570 if (inf->pspace->ebfd)
571 ui_out_field_string (uiout, "exec",
572 bfd_get_filename (inf->pspace->ebfd));
574 ui_out_field_skip (uiout, "exec");
576 /* Print extra info that isn't really fit to always present in
577 tabular form. Currently we print the vfork parent/child
578 relationships, if any. */
579 if (inf->vfork_parent)
581 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
582 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
584 if (inf->vfork_child)
586 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
587 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
590 ui_out_text (uiout, "\n");
591 do_cleanups (chain2);
594 do_cleanups (old_chain);
598 detach_inferior_command (char *args, int from_tty)
601 struct thread_info *tp;
604 error (_("Requires argument (inferior id to detach)"));
606 num = parse_and_eval_long (args);
608 if (!valid_gdb_inferior_id (num))
609 error (_("Inferior ID %d not known."), num);
611 pid = gdb_inferior_id_to_pid (num);
613 tp = any_thread_of_process (pid);
615 error (_("Inferior has no threads."));
617 switch_to_thread (tp->ptid);
619 detach_command (NULL, from_tty);
623 kill_inferior_command (char *args, int from_tty)
626 struct thread_info *tp;
629 error (_("Requires argument (inferior id to kill)"));
631 num = parse_and_eval_long (args);
633 if (!valid_gdb_inferior_id (num))
634 error (_("Inferior ID %d not known."), num);
636 pid = gdb_inferior_id_to_pid (num);
638 tp = any_thread_of_process (pid);
640 error (_("Inferior has no threads."));
642 switch_to_thread (tp->ptid);
646 bfd_cache_close_all ();
650 inferior_command (char *args, int from_tty)
652 struct inferior *inf;
655 num = parse_and_eval_long (args);
657 inf = find_inferior_id (num);
659 error (_("Inferior ID %d not known."), num);
661 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
663 target_pid_to_str (pid_to_ptid (inf->pid)),
665 ? bfd_get_filename (inf->pspace->ebfd)
670 if (inf->pid != ptid_get_pid (inferior_ptid))
672 struct thread_info *tp;
674 tp = any_thread_of_process (inf->pid);
676 error (_("Inferior has no threads."));
678 switch_to_thread (tp->ptid);
681 printf_filtered (_("[Switching to thread %d (%s)] "),
682 pid_to_thread_id (inferior_ptid),
683 target_pid_to_str (inferior_ptid));
687 struct inferior *inf;
689 inf = find_inferior_id (num);
690 set_current_inferior (inf);
691 switch_to_thread (null_ptid);
692 set_current_program_space (inf->pspace);
695 if (inf->pid != 0 && is_running (inferior_ptid))
696 ui_out_text (uiout, "(running)\n");
697 else if (inf->pid != 0)
699 ui_out_text (uiout, "\n");
700 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
704 /* Print information about currently known inferiors. */
707 info_inferiors_command (char *args, int from_tty)
713 requested = parse_and_eval_long (args);
714 if (!valid_gdb_inferior_id (requested))
715 error (_("Inferior ID %d not known."), requested);
718 print_inferior (uiout, requested);
721 /* remove-inferior ID */
724 remove_inferior_command (char *args, int from_tty)
727 struct inferior *inf;
729 num = parse_and_eval_long (args);
730 inf = find_inferior_id (num);
733 error (_("Inferior ID %d not known."), num);
735 if (inf == current_inferior ())
736 error (_("Can not remove current symbol inferior."));
738 delete_inferior_1 (inf, 1);
742 /* add-inferior [-copies N] [-exec FILENAME] */
745 add_inferior_command (char *args, int from_tty)
750 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
754 argv = gdb_buildargv (args);
755 make_cleanup_freeargv (argv);
757 for (; *argv != NULL; argv++)
761 if (strcmp (*argv, "-copies") == 0)
765 error (_("No argument to -copies"));
766 copies = parse_and_eval_long (*argv);
768 else if (strcmp (*argv, "-exec") == 0)
772 error (_("No argument to -exec"));
777 error (_("Invalid argument"));
781 save_current_space_and_thread ();
783 for (i = 0; i < copies; ++i)
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;
798 printf_filtered (_("Added inferior %d\n"), inf->num);
802 /* Switch over temporarily, while reading executable and
804 set_current_program_space (pspace);
805 set_current_inferior (inf);
806 switch_to_thread (null_ptid);
808 exec_file_attach (exec, from_tty);
809 symbol_file_add_main (exec, from_tty);
813 do_cleanups (old_chain);
816 /* clone-inferior [-copies N] [ID] */
819 clone_inferior_command (char *args, int from_tty)
823 struct inferior *orginf = NULL;
824 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
828 argv = gdb_buildargv (args);
829 make_cleanup_freeargv (argv);
831 for (; *argv != NULL; argv++)
835 if (strcmp (*argv, "-copies") == 0)
839 error (_("No argument to -copies"));
840 copies = parse_and_eval_long (*argv);
843 error (_("Invalid copies number"));
852 /* The first non-option (-) argument specified the
854 num = parse_and_eval_long (*argv);
855 orginf = find_inferior_id (num);
858 error (_("Inferior ID %d not known."), num);
862 error (_("Invalid argument"));
867 /* If no inferior id was specified, then the user wants to clone the
870 orginf = current_inferior ();
872 save_current_space_and_thread ();
874 for (i = 0; i < copies; ++i)
876 struct address_space *aspace;
877 struct program_space *pspace;
878 struct inferior *inf;
880 /* If all inferiors share an address space on this system, this
881 doesn't really return a new address space; otherwise, it
883 aspace = maybe_new_address_space ();
884 pspace = add_program_space (aspace);
885 inf = add_inferior (0);
886 inf->pspace = pspace;
887 inf->aspace = pspace->aspace;
889 printf_filtered (_("Added inferior %d.\n"), inf->num);
891 set_current_inferior (inf);
892 switch_to_thread (null_ptid);
893 clone_program_space (pspace, orginf->pspace);
896 do_cleanups (old_chain);
899 /* Print notices when new inferiors are created and die. */
901 show_print_inferior_events (struct ui_file *file, int from_tty,
902 struct cmd_list_element *c, const char *value)
904 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
909 /* Keep a registry of per-inferior data-pointers required by other GDB
915 void (*cleanup) (struct inferior *, void *);
918 struct inferior_data_registration
920 struct inferior_data *data;
921 struct inferior_data_registration *next;
924 struct inferior_data_registry
926 struct inferior_data_registration *registrations;
927 unsigned num_registrations;
930 static struct inferior_data_registry inferior_data_registry
933 const struct inferior_data *
934 register_inferior_data_with_cleanup
935 (void (*cleanup) (struct inferior *, void *))
937 struct inferior_data_registration **curr;
939 /* Append new registration. */
940 for (curr = &inferior_data_registry.registrations;
941 *curr != NULL; curr = &(*curr)->next);
943 *curr = XMALLOC (struct inferior_data_registration);
944 (*curr)->next = NULL;
945 (*curr)->data = XMALLOC (struct inferior_data);
946 (*curr)->data->index = inferior_data_registry.num_registrations++;
947 (*curr)->data->cleanup = cleanup;
949 return (*curr)->data;
952 const struct inferior_data *
953 register_inferior_data (void)
955 return register_inferior_data_with_cleanup (NULL);
959 inferior_alloc_data (struct inferior *inf)
961 gdb_assert (inf->data == NULL);
962 inf->num_data = inferior_data_registry.num_registrations;
963 inf->data = XCALLOC (inf->num_data, void *);
967 inferior_free_data (struct inferior *inf)
969 gdb_assert (inf->data != NULL);
970 clear_inferior_data (inf);
976 clear_inferior_data (struct inferior *inf)
978 struct inferior_data_registration *registration;
981 gdb_assert (inf->data != NULL);
983 for (registration = inferior_data_registry.registrations, i = 0;
985 registration = registration->next, i++)
986 if (inf->data[i] != NULL && registration->data->cleanup)
987 registration->data->cleanup (inf, inf->data[i]);
989 memset (inf->data, 0, inf->num_data * sizeof (void *));
993 set_inferior_data (struct inferior *inf,
994 const struct inferior_data *data,
997 gdb_assert (data->index < inf->num_data);
998 inf->data[data->index] = value;
1002 inferior_data (struct inferior *inf, const struct inferior_data *data)
1004 gdb_assert (data->index < inf->num_data);
1005 return inf->data[data->index];
1009 initialize_inferiors (void)
1011 /* There's always one inferior. Note that this function isn't an
1012 automatic _initialize_foo function, since other _initialize_foo
1013 routines may need to install their per-inferior data keys. We
1014 can only allocate an inferior when all those modules have done
1015 that. Do this after initialize_progspace, due to the
1016 current_program_space reference. */
1017 current_inferior_ = add_inferior (0);
1018 current_inferior_->pspace = current_program_space;
1019 current_inferior_->aspace = current_program_space->aspace;
1021 add_info ("inferiors", info_inferiors_command,
1022 _("IDs of currently known inferiors."));
1024 add_com ("add-inferior", no_class, add_inferior_command, _("\
1025 Add a new inferior.\n\
1026 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1027 N is the optional number of inferior to add, default is 1.\n\
1028 FILENAME is the file name of the executable to use\n\
1029 as main program."));
1031 add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1032 Remove inferior ID.\n\
1033 Usage: remove-inferior ID"));
1035 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1036 Clone inferior ID.\n\
1037 Usage: clone-inferior [-copies <N>] [ID]\n\
1038 Add N copies of inferior ID. The new inferior has the same\n\
1039 executable loaded as the copied inferior. If -copies is not specified,\n\
1040 adds 1 copy. If ID is not specified, it is the current inferior\n\
1043 add_cmd ("inferior", class_run, detach_inferior_command, _("\
1044 Detach from inferior ID."),
1047 add_cmd ("inferior", class_run, kill_inferior_command, _("\
1048 Kill inferior ID."),
1051 add_cmd ("inferior", class_run, inferior_command, _("\
1052 Use this command to switch between inferiors.\n\
1053 The new inferior ID must be currently known."),
1056 add_setshow_boolean_cmd ("inferior-events", no_class,
1057 &print_inferior_events, _("\
1058 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1059 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1061 show_print_inferior_events,
1062 &setprintlist, &showprintlist);