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);
136 observer_notify_inferior_added (inf);
139 inferior_appeared (inf, pid);
145 add_inferior (int pid)
147 struct inferior *inf = add_inferior_silent (pid);
149 if (print_inferior_events)
150 printf_unfiltered (_("[New inferior %d]\n"), pid);
155 struct delete_thread_of_inferior_arg
162 delete_thread_of_inferior (struct thread_info *tp, void *data)
164 struct delete_thread_of_inferior_arg *arg = data;
166 if (ptid_get_pid (tp->ptid) == arg->pid)
169 delete_thread_silent (tp->ptid);
171 delete_thread (tp->ptid);
178 delete_threads_of_inferior (int pid)
180 struct inferior *inf;
181 struct delete_thread_of_inferior_arg arg;
183 for (inf = inferior_list; inf; inf = inf->next)
193 iterate_over_threads (delete_thread_of_inferior, &arg);
196 /* If SILENT then be quiet -- don't announce a inferior death, or the
197 exit of its threads. */
200 delete_inferior_1 (struct inferior *todel, int silent)
202 struct inferior *inf, *infprev;
203 struct delete_thread_of_inferior_arg arg;
207 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
217 iterate_over_threads (delete_thread_of_inferior, &arg);
220 infprev->next = inf->next;
222 inferior_list = inf->next;
224 observer_notify_inferior_removed (inf);
230 delete_inferior (int pid)
232 struct inferior *inf = find_inferior_pid (pid);
234 delete_inferior_1 (inf, 0);
236 if (print_inferior_events)
237 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
241 delete_inferior_silent (int pid)
243 struct inferior *inf = find_inferior_pid (pid);
245 delete_inferior_1 (inf, 1);
249 /* If SILENT then be quiet -- don't announce a inferior exit, or the
250 exit of its threads. */
253 exit_inferior_1 (struct inferior *inftoex, int silent)
255 struct inferior *inf;
256 struct delete_thread_of_inferior_arg arg;
258 for (inf = inferior_list; inf; inf = inf->next)
268 iterate_over_threads (delete_thread_of_inferior, &arg);
270 /* Notify the observers before removing the inferior from the list,
271 so that the observers have a chance to look it up. */
272 observer_notify_inferior_exit (inf);
275 if (inf->vfork_parent != NULL)
277 inf->vfork_parent->vfork_child = NULL;
278 inf->vfork_parent = NULL;
283 exit_inferior (int pid)
285 struct inferior *inf = find_inferior_pid (pid);
286 exit_inferior_1 (inf, 0);
288 if (print_inferior_events)
289 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
293 exit_inferior_silent (int pid)
295 struct inferior *inf = find_inferior_pid (pid);
296 exit_inferior_1 (inf, 1);
300 exit_inferior_num_silent (int num)
302 struct inferior *inf = find_inferior_id (num);
304 exit_inferior_1 (inf, 1);
308 detach_inferior (int pid)
310 struct inferior *inf = find_inferior_pid (pid);
311 exit_inferior_1 (inf, 1);
313 if (print_inferior_events)
314 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
318 inferior_appeared (struct inferior *inf, int pid)
322 observer_notify_inferior_appeared (inf);
326 discard_all_inferiors (void)
328 struct inferior *inf;
330 for (inf = inferior_list; inf; inf = inf->next)
333 exit_inferior_silent (inf->pid);
338 find_inferior_id (int num)
340 struct inferior *inf;
342 for (inf = inferior_list; inf; inf = inf->next)
350 find_inferior_pid (int pid)
352 struct inferior *inf;
354 /* Looking for inferior pid == 0 is always wrong, and indicative of
355 a bug somewhere else. There may be more than one with pid == 0,
357 gdb_assert (pid != 0);
359 for (inf = inferior_list; inf; inf = inf->next)
366 /* Find an inferior bound to PSPACE. */
369 find_inferior_for_program_space (struct program_space *pspace)
371 struct inferior *inf;
373 for (inf = inferior_list; inf != NULL; inf = inf->next)
375 if (inf->pspace == pspace)
383 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
386 struct inferior *inf, *infnext;
388 for (inf = inferior_list; inf; inf = infnext)
391 if ((*callback) (inf, data))
399 valid_gdb_inferior_id (int num)
401 struct inferior *inf;
403 for (inf = inferior_list; inf; inf = inf->next)
411 pid_to_gdb_inferior_id (int pid)
413 struct inferior *inf;
415 for (inf = inferior_list; inf; inf = inf->next)
423 gdb_inferior_id_to_pid (int num)
425 struct inferior *inferior = find_inferior_id (num);
427 return inferior->pid;
433 in_inferior_list (int pid)
435 struct inferior *inf;
437 for (inf = inferior_list; inf; inf = inf->next)
445 have_inferiors (void)
447 struct inferior *inf;
449 for (inf = inferior_list; inf; inf = inf->next)
457 have_live_inferiors (void)
459 struct target_ops *t;
461 /* The check on stratum suffices, as GDB doesn't currently support
462 multiple target interfaces. */
463 if (have_inferiors ())
464 for (t = current_target.beneath; t != NULL; t = t->beneath)
465 if (t->to_stratum == process_stratum)
471 /* Prune away automatically added program spaces that aren't required
475 prune_inferiors (void)
477 struct inferior *ss, **ss_link;
478 struct inferior *current = current_inferior ();
481 ss_link = &inferior_list;
494 delete_inferior_1 (ss, 1);
498 prune_program_spaces ();
501 /* Simply returns the count of inferiors. */
504 number_of_inferiors (void)
506 struct inferior *inf;
509 for (inf = inferior_list; inf != NULL; inf = inf->next)
515 /* Prints the list of inferiors and their details on UIOUT. This is a
516 version of 'info_inferior_command' suitable for use from MI.
518 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
519 should be printed. Otherwise, all inferiors are printed. */
521 print_inferior (struct ui_out *uiout, int requested_inferior)
523 struct inferior *inf;
524 struct cleanup *old_chain;
527 /* Compute number of inferiors we will print. */
528 for (inf = inferior_list; inf; inf = inf->next)
530 if (requested_inferior != -1 && inf->num != requested_inferior)
538 ui_out_message (uiout, 0, "No inferiors.\n");
542 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
544 ui_out_table_header (uiout, 1, ui_left, "current", "");
545 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
546 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
547 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
549 ui_out_table_body (uiout);
550 for (inf = inferior_list; inf; inf = inf->next)
552 struct cleanup *chain2;
554 if (requested_inferior != -1 && inf->num != requested_inferior)
557 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
559 if (inf == current_inferior ())
560 ui_out_field_string (uiout, "current", "*");
562 ui_out_field_skip (uiout, "current");
564 ui_out_field_int (uiout, "number", inf->num);
567 ui_out_field_string (uiout, "target-id",
568 target_pid_to_str (pid_to_ptid (inf->pid)));
570 ui_out_field_string (uiout, "target-id", "<null>");
572 if (inf->pspace->ebfd)
573 ui_out_field_string (uiout, "exec",
574 bfd_get_filename (inf->pspace->ebfd));
576 ui_out_field_skip (uiout, "exec");
578 /* Print extra info that isn't really fit to always present in
579 tabular form. Currently we print the vfork parent/child
580 relationships, if any. */
581 if (inf->vfork_parent)
583 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
584 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
586 if (inf->vfork_child)
588 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
589 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
592 ui_out_text (uiout, "\n");
593 do_cleanups (chain2);
596 do_cleanups (old_chain);
600 detach_inferior_command (char *args, int from_tty)
603 struct thread_info *tp;
606 error (_("Requires argument (inferior id to detach)"));
608 num = parse_and_eval_long (args);
610 if (!valid_gdb_inferior_id (num))
611 error (_("Inferior ID %d not known."), num);
613 pid = gdb_inferior_id_to_pid (num);
615 tp = any_thread_of_process (pid);
617 error (_("Inferior has no threads."));
619 switch_to_thread (tp->ptid);
621 detach_command (NULL, from_tty);
625 kill_inferior_command (char *args, int from_tty)
628 struct thread_info *tp;
631 error (_("Requires argument (inferior id to kill)"));
633 num = parse_and_eval_long (args);
635 if (!valid_gdb_inferior_id (num))
636 error (_("Inferior ID %d not known."), num);
638 pid = gdb_inferior_id_to_pid (num);
640 tp = any_thread_of_process (pid);
642 error (_("Inferior has no threads."));
644 switch_to_thread (tp->ptid);
648 bfd_cache_close_all ();
652 inferior_command (char *args, int from_tty)
654 struct inferior *inf;
657 num = parse_and_eval_long (args);
659 inf = find_inferior_id (num);
661 error (_("Inferior ID %d not known."), num);
663 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
665 target_pid_to_str (pid_to_ptid (inf->pid)),
667 ? bfd_get_filename (inf->pspace->ebfd)
672 if (inf->pid != ptid_get_pid (inferior_ptid))
674 struct thread_info *tp;
676 tp = any_thread_of_process (inf->pid);
678 error (_("Inferior has no threads."));
680 switch_to_thread (tp->ptid);
683 printf_filtered (_("[Switching to thread %d (%s)] "),
684 pid_to_thread_id (inferior_ptid),
685 target_pid_to_str (inferior_ptid));
689 struct inferior *inf;
691 inf = find_inferior_id (num);
692 set_current_inferior (inf);
693 switch_to_thread (null_ptid);
694 set_current_program_space (inf->pspace);
697 if (inf->pid != 0 && is_running (inferior_ptid))
698 ui_out_text (uiout, "(running)\n");
699 else if (inf->pid != 0)
701 ui_out_text (uiout, "\n");
702 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
706 /* Print information about currently known inferiors. */
709 info_inferiors_command (char *args, int from_tty)
715 requested = parse_and_eval_long (args);
716 if (!valid_gdb_inferior_id (requested))
717 error (_("Inferior ID %d not known."), requested);
720 print_inferior (uiout, requested);
723 /* remove-inferior ID */
726 remove_inferior_command (char *args, int from_tty)
729 struct inferior *inf;
731 num = parse_and_eval_long (args);
732 inf = find_inferior_id (num);
735 error (_("Inferior ID %d not known."), num);
737 if (inf == current_inferior ())
738 error (_("Can not remove current symbol inferior."));
740 delete_inferior_1 (inf, 1);
744 add_inferior_with_spaces (void)
746 struct address_space *aspace;
747 struct program_space *pspace;
748 struct inferior *inf;
750 /* If all inferiors share an address space on this system, this
751 doesn't really return a new address space; otherwise, it
753 aspace = maybe_new_address_space ();
754 pspace = add_program_space (aspace);
755 inf = add_inferior (0);
756 inf->pspace = pspace;
757 inf->aspace = pspace->aspace;
762 /* add-inferior [-copies N] [-exec FILENAME] */
765 add_inferior_command (char *args, int from_tty)
770 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
774 argv = gdb_buildargv (args);
775 make_cleanup_freeargv (argv);
777 for (; *argv != NULL; argv++)
781 if (strcmp (*argv, "-copies") == 0)
785 error (_("No argument to -copies"));
786 copies = parse_and_eval_long (*argv);
788 else if (strcmp (*argv, "-exec") == 0)
792 error (_("No argument to -exec"));
797 error (_("Invalid argument"));
801 save_current_space_and_thread ();
803 for (i = 0; i < copies; ++i)
805 struct inferior *inf = add_inferior_with_spaces ();
807 printf_filtered (_("Added inferior %d\n"), inf->num);
811 /* Switch over temporarily, while reading executable and
813 set_current_program_space (inf->pspace);
814 set_current_inferior (inf);
815 switch_to_thread (null_ptid);
817 exec_file_attach (exec, from_tty);
818 symbol_file_add_main (exec, from_tty);
822 do_cleanups (old_chain);
825 /* clone-inferior [-copies N] [ID] */
828 clone_inferior_command (char *args, int from_tty)
832 struct inferior *orginf = NULL;
833 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
837 argv = gdb_buildargv (args);
838 make_cleanup_freeargv (argv);
840 for (; *argv != NULL; argv++)
844 if (strcmp (*argv, "-copies") == 0)
848 error (_("No argument to -copies"));
849 copies = parse_and_eval_long (*argv);
852 error (_("Invalid copies number"));
861 /* The first non-option (-) argument specified the
863 num = parse_and_eval_long (*argv);
864 orginf = find_inferior_id (num);
867 error (_("Inferior ID %d not known."), num);
871 error (_("Invalid argument"));
876 /* If no inferior id was specified, then the user wants to clone the
879 orginf = current_inferior ();
881 save_current_space_and_thread ();
883 for (i = 0; i < copies; ++i)
885 struct address_space *aspace;
886 struct program_space *pspace;
887 struct inferior *inf;
889 /* If all inferiors share an address space on this system, this
890 doesn't really return a new address space; otherwise, it
892 aspace = maybe_new_address_space ();
893 pspace = add_program_space (aspace);
894 inf = add_inferior (0);
895 inf->pspace = pspace;
896 inf->aspace = pspace->aspace;
898 printf_filtered (_("Added inferior %d.\n"), inf->num);
900 set_current_inferior (inf);
901 switch_to_thread (null_ptid);
902 clone_program_space (pspace, orginf->pspace);
905 do_cleanups (old_chain);
908 /* Print notices when new inferiors are created and die. */
910 show_print_inferior_events (struct ui_file *file, int from_tty,
911 struct cmd_list_element *c, const char *value)
913 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
918 /* Keep a registry of per-inferior data-pointers required by other GDB
924 void (*cleanup) (struct inferior *, void *);
927 struct inferior_data_registration
929 struct inferior_data *data;
930 struct inferior_data_registration *next;
933 struct inferior_data_registry
935 struct inferior_data_registration *registrations;
936 unsigned num_registrations;
939 static struct inferior_data_registry inferior_data_registry
942 const struct inferior_data *
943 register_inferior_data_with_cleanup
944 (void (*cleanup) (struct inferior *, void *))
946 struct inferior_data_registration **curr;
948 /* Append new registration. */
949 for (curr = &inferior_data_registry.registrations;
950 *curr != NULL; curr = &(*curr)->next);
952 *curr = XMALLOC (struct inferior_data_registration);
953 (*curr)->next = NULL;
954 (*curr)->data = XMALLOC (struct inferior_data);
955 (*curr)->data->index = inferior_data_registry.num_registrations++;
956 (*curr)->data->cleanup = cleanup;
958 return (*curr)->data;
961 const struct inferior_data *
962 register_inferior_data (void)
964 return register_inferior_data_with_cleanup (NULL);
968 inferior_alloc_data (struct inferior *inf)
970 gdb_assert (inf->data == NULL);
971 inf->num_data = inferior_data_registry.num_registrations;
972 inf->data = XCALLOC (inf->num_data, void *);
976 inferior_free_data (struct inferior *inf)
978 gdb_assert (inf->data != NULL);
979 clear_inferior_data (inf);
985 clear_inferior_data (struct inferior *inf)
987 struct inferior_data_registration *registration;
990 gdb_assert (inf->data != NULL);
992 for (registration = inferior_data_registry.registrations, i = 0;
994 registration = registration->next, i++)
995 if (inf->data[i] != NULL && registration->data->cleanup)
996 registration->data->cleanup (inf, inf->data[i]);
998 memset (inf->data, 0, inf->num_data * sizeof (void *));
1002 set_inferior_data (struct inferior *inf,
1003 const struct inferior_data *data,
1006 gdb_assert (data->index < inf->num_data);
1007 inf->data[data->index] = value;
1011 inferior_data (struct inferior *inf, const struct inferior_data *data)
1013 gdb_assert (data->index < inf->num_data);
1014 return inf->data[data->index];
1018 initialize_inferiors (void)
1020 /* There's always one inferior. Note that this function isn't an
1021 automatic _initialize_foo function, since other _initialize_foo
1022 routines may need to install their per-inferior data keys. We
1023 can only allocate an inferior when all those modules have done
1024 that. Do this after initialize_progspace, due to the
1025 current_program_space reference. */
1026 current_inferior_ = add_inferior (0);
1027 current_inferior_->pspace = current_program_space;
1028 current_inferior_->aspace = current_program_space->aspace;
1030 add_info ("inferiors", info_inferiors_command,
1031 _("IDs of currently known inferiors."));
1033 add_com ("add-inferior", no_class, add_inferior_command, _("\
1034 Add a new inferior.\n\
1035 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1036 N is the optional number of inferior to add, default is 1.\n\
1037 FILENAME is the file name of the executable to use\n\
1038 as main program."));
1040 add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1041 Remove inferior ID.\n\
1042 Usage: remove-inferior ID"));
1044 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1045 Clone inferior ID.\n\
1046 Usage: clone-inferior [-copies <N>] [ID]\n\
1047 Add N copies of inferior ID. The new inferior has the same\n\
1048 executable loaded as the copied inferior. If -copies is not specified,\n\
1049 adds 1 copy. If ID is not specified, it is the current inferior\n\
1052 add_cmd ("inferior", class_run, detach_inferior_command, _("\
1053 Detach from inferior ID."),
1056 add_cmd ("inferior", class_run, kill_inferior_command, _("\
1057 Kill inferior ID."),
1060 add_cmd ("inferior", class_run, inferior_command, _("\
1061 Use this command to switch between inferiors.\n\
1062 The new inferior ID must be currently known."),
1065 add_setshow_boolean_cmd ("inferior-events", no_class,
1066 &print_inferior_events, _("\
1067 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1068 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1070 show_print_inferior_events,
1071 &setprintlist, &showprintlist);