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->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);
140 if (kernel_debugger || (pid != 0))
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)
337 if (kernel_debugger || (inf->pid != 0))
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 (kernel_debugger || (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)
455 if (kernel_debugger || (inf->pid != 0))
462 have_live_inferiors (void)
464 struct target_ops *t;
466 /* The check on stratum suffices, as GDB doesn't currently support
467 multiple target interfaces. */
468 if (have_inferiors ())
469 for (t = current_target.beneath; t != NULL; t = t->beneath)
470 if (t->to_stratum == process_stratum)
476 /* Prune away automatically added program spaces that aren't required
480 prune_inferiors (void)
482 struct inferior *ss, **ss_link;
483 struct inferior *current = current_inferior ();
486 ss_link = &inferior_list;
491 || (kernel_debugger || (ss->pid != 0)))
499 delete_inferior_1 (ss, 1);
503 prune_program_spaces ();
506 /* Simply returns the count of inferiors. */
509 number_of_inferiors (void)
511 struct inferior *inf;
514 for (inf = inferior_list; inf != NULL; inf = inf->next)
520 /* Prints the list of inferiors and their details on UIOUT. This is a
521 version of 'info_inferior_command' suitable for use from MI.
523 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
524 should be printed. Otherwise, all inferiors are printed. */
526 print_inferior (struct ui_out *uiout, int requested_inferior)
528 struct inferior *inf;
529 struct cleanup *old_chain;
532 /* Compute number of inferiors we will print. */
533 for (inf = inferior_list; inf; inf = inf->next)
535 if (requested_inferior != -1 && inf->num != requested_inferior)
543 ui_out_message (uiout, 0, "No inferiors.\n");
547 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
549 ui_out_table_header (uiout, 1, ui_left, "current", "");
550 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
551 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
552 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
554 ui_out_table_body (uiout);
555 for (inf = inferior_list; inf; inf = inf->next)
557 struct cleanup *chain2;
559 if (requested_inferior != -1 && inf->num != requested_inferior)
562 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
564 if (inf == current_inferior ())
565 ui_out_field_string (uiout, "current", "*");
567 ui_out_field_skip (uiout, "current");
569 ui_out_field_int (uiout, "number", inf->num);
571 if (kernel_debugger || (inf->pid != 0))
572 ui_out_field_string (uiout, "target-id",
573 target_pid_to_str (pid_to_ptid (inf->pid)));
575 ui_out_field_string (uiout, "target-id", "<null>");
577 if (inf->pspace->ebfd)
578 ui_out_field_string (uiout, "exec",
579 bfd_get_filename (inf->pspace->ebfd));
581 ui_out_field_skip (uiout, "exec");
583 /* Print extra info that isn't really fit to always present in
584 tabular form. Currently we print the vfork parent/child
585 relationships, if any. */
586 if (inf->vfork_parent)
588 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
589 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
591 if (inf->vfork_child)
593 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
594 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
597 ui_out_text (uiout, "\n");
598 do_cleanups (chain2);
601 do_cleanups (old_chain);
605 detach_inferior_command (char *args, int from_tty)
608 struct thread_info *tp;
611 error (_("Requires argument (inferior id to detach)"));
613 num = parse_and_eval_long (args);
615 if (!valid_gdb_inferior_id (num))
616 error (_("Inferior ID %d not known."), num);
618 pid = gdb_inferior_id_to_pid (num);
620 tp = any_thread_of_process (pid);
622 error (_("Inferior has no threads."));
624 switch_to_thread (tp->ptid);
626 detach_command (NULL, from_tty);
630 kill_inferior_command (char *args, int from_tty)
633 struct thread_info *tp;
636 error (_("Requires argument (inferior id to kill)"));
638 num = parse_and_eval_long (args);
640 if (!valid_gdb_inferior_id (num))
641 error (_("Inferior ID %d not known."), num);
643 pid = gdb_inferior_id_to_pid (num);
645 tp = any_thread_of_process (pid);
647 error (_("Inferior has no threads."));
649 switch_to_thread (tp->ptid);
653 bfd_cache_close_all ();
657 inferior_command (char *args, int from_tty)
659 struct inferior *inf;
662 num = parse_and_eval_long (args);
664 inf = find_inferior_id (num);
666 error (_("Inferior ID %d not known."), num);
668 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
670 target_pid_to_str (pid_to_ptid (inf->pid)),
672 ? bfd_get_filename (inf->pspace->ebfd)
677 if (inf->pid != ptid_get_pid (inferior_ptid))
679 struct thread_info *tp;
681 tp = any_thread_of_process (inf->pid);
683 error (_("Inferior has no threads."));
685 switch_to_thread (tp->ptid);
688 printf_filtered (_("[Switching to thread %d (%s)] "),
689 pid_to_thread_id (inferior_ptid),
690 target_pid_to_str (inferior_ptid));
694 struct inferior *inf;
696 inf = find_inferior_id (num);
697 set_current_inferior (inf);
698 switch_to_thread (null_ptid);
699 set_current_program_space (inf->pspace);
702 if (inf->pid != 0 && is_running (inferior_ptid))
703 ui_out_text (uiout, "(running)\n");
704 else if (inf->pid != 0)
706 ui_out_text (uiout, "\n");
707 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
711 /* Print information about currently known inferiors. */
714 info_inferiors_command (char *args, int from_tty)
720 requested = parse_and_eval_long (args);
721 if (!valid_gdb_inferior_id (requested))
722 error (_("Inferior ID %d not known."), requested);
725 print_inferior (uiout, requested);
728 /* remove-inferior ID */
731 remove_inferior_command (char *args, int from_tty)
734 struct inferior *inf;
736 num = parse_and_eval_long (args);
737 inf = find_inferior_id (num);
740 error (_("Inferior ID %d not known."), num);
742 if (inf == current_inferior ())
743 error (_("Can not remove current symbol inferior."));
745 delete_inferior_1 (inf, 1);
749 add_inferior_with_spaces (void)
751 struct address_space *aspace;
752 struct program_space *pspace;
753 struct inferior *inf;
755 /* If all inferiors share an address space on this system, this
756 doesn't really return a new address space; otherwise, it
758 aspace = maybe_new_address_space ();
759 pspace = add_program_space (aspace);
760 inf = add_inferior (0);
761 inf->pspace = pspace;
762 inf->aspace = pspace->aspace;
767 /* add-inferior [-copies N] [-exec FILENAME] */
770 add_inferior_command (char *args, int from_tty)
775 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
779 argv = gdb_buildargv (args);
780 make_cleanup_freeargv (argv);
782 for (; *argv != NULL; argv++)
786 if (strcmp (*argv, "-copies") == 0)
790 error (_("No argument to -copies"));
791 copies = parse_and_eval_long (*argv);
793 else if (strcmp (*argv, "-exec") == 0)
797 error (_("No argument to -exec"));
802 error (_("Invalid argument"));
806 save_current_space_and_thread ();
808 for (i = 0; i < copies; ++i)
810 struct inferior *inf = add_inferior_with_spaces ();
812 printf_filtered (_("Added inferior %d\n"), inf->num);
816 /* Switch over temporarily, while reading executable and
818 set_current_program_space (inf->pspace);
819 set_current_inferior (inf);
820 switch_to_thread (null_ptid);
822 exec_file_attach (exec, from_tty);
823 symbol_file_add_main (exec, from_tty);
827 do_cleanups (old_chain);
830 /* clone-inferior [-copies N] [ID] */
833 clone_inferior_command (char *args, int from_tty)
837 struct inferior *orginf = NULL;
838 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
842 argv = gdb_buildargv (args);
843 make_cleanup_freeargv (argv);
845 for (; *argv != NULL; argv++)
849 if (strcmp (*argv, "-copies") == 0)
853 error (_("No argument to -copies"));
854 copies = parse_and_eval_long (*argv);
857 error (_("Invalid copies number"));
866 /* The first non-option (-) argument specified the
868 num = parse_and_eval_long (*argv);
869 orginf = find_inferior_id (num);
872 error (_("Inferior ID %d not known."), num);
876 error (_("Invalid argument"));
881 /* If no inferior id was specified, then the user wants to clone the
884 orginf = current_inferior ();
886 save_current_space_and_thread ();
888 for (i = 0; i < copies; ++i)
890 struct address_space *aspace;
891 struct program_space *pspace;
892 struct inferior *inf;
894 /* If all inferiors share an address space on this system, this
895 doesn't really return a new address space; otherwise, it
897 aspace = maybe_new_address_space ();
898 pspace = add_program_space (aspace);
899 inf = add_inferior (0);
900 inf->pspace = pspace;
901 inf->aspace = pspace->aspace;
903 printf_filtered (_("Added inferior %d.\n"), inf->num);
905 set_current_inferior (inf);
906 switch_to_thread (null_ptid);
907 clone_program_space (pspace, orginf->pspace);
910 do_cleanups (old_chain);
913 /* Print notices when new inferiors are created and die. */
915 show_print_inferior_events (struct ui_file *file, int from_tty,
916 struct cmd_list_element *c, const char *value)
918 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
923 /* Keep a registry of per-inferior data-pointers required by other GDB
929 void (*cleanup) (struct inferior *, void *);
932 struct inferior_data_registration
934 struct inferior_data *data;
935 struct inferior_data_registration *next;
938 struct inferior_data_registry
940 struct inferior_data_registration *registrations;
941 unsigned num_registrations;
944 static struct inferior_data_registry inferior_data_registry
947 const struct inferior_data *
948 register_inferior_data_with_cleanup
949 (void (*cleanup) (struct inferior *, void *))
951 struct inferior_data_registration **curr;
953 /* Append new registration. */
954 for (curr = &inferior_data_registry.registrations;
955 *curr != NULL; curr = &(*curr)->next);
957 *curr = XMALLOC (struct inferior_data_registration);
958 (*curr)->next = NULL;
959 (*curr)->data = XMALLOC (struct inferior_data);
960 (*curr)->data->index = inferior_data_registry.num_registrations++;
961 (*curr)->data->cleanup = cleanup;
963 return (*curr)->data;
966 const struct inferior_data *
967 register_inferior_data (void)
969 return register_inferior_data_with_cleanup (NULL);
973 inferior_alloc_data (struct inferior *inf)
975 gdb_assert (inf->data == NULL);
976 inf->num_data = inferior_data_registry.num_registrations;
977 inf->data = XCALLOC (inf->num_data, void *);
981 inferior_free_data (struct inferior *inf)
983 gdb_assert (inf->data != NULL);
984 clear_inferior_data (inf);
990 clear_inferior_data (struct inferior *inf)
992 struct inferior_data_registration *registration;
995 gdb_assert (inf->data != NULL);
997 for (registration = inferior_data_registry.registrations, i = 0;
999 registration = registration->next, i++)
1000 if (inf->data[i] != NULL && registration->data->cleanup)
1001 registration->data->cleanup (inf, inf->data[i]);
1003 memset (inf->data, 0, inf->num_data * sizeof (void *));
1007 set_inferior_data (struct inferior *inf,
1008 const struct inferior_data *data,
1011 gdb_assert (data->index < inf->num_data);
1012 inf->data[data->index] = value;
1016 inferior_data (struct inferior *inf, const struct inferior_data *data)
1018 gdb_assert (data->index < inf->num_data);
1019 return inf->data[data->index];
1023 initialize_inferiors (void)
1025 /* There's always one inferior. Note that this function isn't an
1026 automatic _initialize_foo function, since other _initialize_foo
1027 routines may need to install their per-inferior data keys. We
1028 can only allocate an inferior when all those modules have done
1029 that. Do this after initialize_progspace, due to the
1030 current_program_space reference. */
1032 /* However, when invoked by DragonFly kgdb which always has many inferiors,
1033 the default inferior will not be defined. The swapper process always has
1034 pid 0, which conflicts with the default. */
1036 if (!kernel_debugger) {
1037 current_inferior_ = add_inferior (0);
1038 current_inferior_->pspace = current_program_space;
1039 current_inferior_->aspace = current_program_space->aspace;
1042 add_info ("inferiors", info_inferiors_command,
1043 _("IDs of currently known inferiors."));
1045 add_com ("add-inferior", no_class, add_inferior_command, _("\
1046 Add a new inferior.\n\
1047 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1048 N is the optional number of inferior to add, default is 1.\n\
1049 FILENAME is the file name of the executable to use\n\
1050 as main program."));
1052 add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1053 Remove inferior ID.\n\
1054 Usage: remove-inferior ID"));
1056 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1057 Clone inferior ID.\n\
1058 Usage: clone-inferior [-copies <N>] [ID]\n\
1059 Add N copies of inferior ID. The new inferior has the same\n\
1060 executable loaded as the copied inferior. If -copies is not specified,\n\
1061 adds 1 copy. If ID is not specified, it is the current inferior\n\
1064 add_cmd ("inferior", class_run, detach_inferior_command, _("\
1065 Detach from inferior ID."),
1068 add_cmd ("inferior", class_run, kill_inferior_command, _("\
1069 Kill inferior ID."),
1072 add_cmd ("inferior", class_run, inferior_command, _("\
1073 Use this command to switch between inferiors.\n\
1074 The new inferior ID must be currently known."),
1077 add_setshow_boolean_cmd ("inferior-events", no_class,
1078 &print_inferior_events, _("\
1079 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1080 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1082 show_print_inferior_events,
1083 &setprintlist, &showprintlist);