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"
33 void _initialize_inferiors (void);
35 static void inferior_alloc_data (struct inferior *inf);
36 static void inferior_free_data (struct inferior *inf);
38 struct inferior *inferior_list = NULL;
39 static int highest_inferior_num;
41 /* Print notices on inferior events (attach, detach, etc.), set with
42 `set print inferior-events'. */
43 static int print_inferior_events = 0;
45 /* The Current Inferior. */
46 static struct inferior *current_inferior_ = NULL;
49 current_inferior (void)
51 return current_inferior_;
55 set_current_inferior (struct inferior *inf)
57 /* There's always an inferior. */
58 gdb_assert (inf != NULL);
60 current_inferior_ = inf;
63 /* A cleanups callback, helper for save_current_program_space
67 restore_inferior (void *arg)
69 struct inferior *saved_inferior = arg;
70 set_current_inferior (saved_inferior);
73 /* Save the current program space so that it may be restored by a later
74 call to do_cleanups. Returns the struct cleanup pointer needed for
75 later doing the cleanup. */
78 save_current_inferior (void)
80 struct cleanup *old_chain = make_cleanup (restore_inferior,
86 free_inferior (struct inferior *inf)
88 discard_all_inferior_continuations (inf);
89 inferior_free_data (inf);
95 init_inferior_list (void)
97 struct inferior *inf, *infnext;
99 highest_inferior_num = 0;
103 for (inf = inferior_list; inf; inf = infnext)
109 inferior_list = NULL;
113 add_inferior_silent (int pid)
115 struct inferior *inf;
117 inf = xmalloc (sizeof (*inf));
118 memset (inf, 0, sizeof (*inf));
121 inf->stop_soon = NO_STOP_QUIETLY;
123 inf->num = ++highest_inferior_num;
124 inf->next = inferior_list;
127 inferior_alloc_data (inf);
130 inferior_appeared (inf, pid);
136 add_inferior (int pid)
138 struct inferior *inf = add_inferior_silent (pid);
140 if (print_inferior_events)
141 printf_unfiltered (_("[New inferior %d]\n"), pid);
146 struct delete_thread_of_inferior_arg
153 delete_thread_of_inferior (struct thread_info *tp, void *data)
155 struct delete_thread_of_inferior_arg *arg = data;
157 if (ptid_get_pid (tp->ptid) == arg->pid)
160 delete_thread_silent (tp->ptid);
162 delete_thread (tp->ptid);
169 delete_threads_of_inferior (int pid)
171 struct inferior *inf;
172 struct delete_thread_of_inferior_arg arg;
174 for (inf = inferior_list; inf; inf = inf->next)
184 iterate_over_threads (delete_thread_of_inferior, &arg);
187 /* If SILENT then be quiet -- don't announce a inferior death, or the
188 exit of its threads. */
191 delete_inferior_1 (struct inferior *todel, int silent)
193 struct inferior *inf, *infprev;
194 struct delete_thread_of_inferior_arg arg;
198 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
208 iterate_over_threads (delete_thread_of_inferior, &arg);
211 infprev->next = inf->next;
213 inferior_list = inf->next;
219 delete_inferior (int pid)
221 struct inferior *inf = find_inferior_pid (pid);
223 delete_inferior_1 (inf, 0);
225 if (print_inferior_events)
226 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
230 delete_inferior_silent (int pid)
232 struct inferior *inf = find_inferior_pid (pid);
234 delete_inferior_1 (inf, 1);
238 /* If SILENT then be quiet -- don't announce a inferior exit, or the
239 exit of its threads. */
242 exit_inferior_1 (struct inferior *inftoex, int silent)
244 struct inferior *inf;
245 struct delete_thread_of_inferior_arg arg;
247 for (inf = inferior_list; inf; inf = inf->next)
257 iterate_over_threads (delete_thread_of_inferior, &arg);
259 /* Notify the observers before removing the inferior from the list,
260 so that the observers have a chance to look it up. */
261 observer_notify_inferior_exit (inf->pid);
264 if (inf->vfork_parent != NULL)
266 inf->vfork_parent->vfork_child = NULL;
267 inf->vfork_parent = NULL;
272 exit_inferior (int pid)
274 struct inferior *inf = find_inferior_pid (pid);
275 exit_inferior_1 (inf, 0);
277 if (print_inferior_events)
278 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
282 exit_inferior_silent (int pid)
284 struct inferior *inf = find_inferior_pid (pid);
285 exit_inferior_1 (inf, 1);
289 exit_inferior_num_silent (int num)
291 struct inferior *inf = find_inferior_id (num);
293 exit_inferior_1 (inf, 1);
297 detach_inferior (int pid)
299 struct inferior *inf = find_inferior_pid (pid);
300 exit_inferior_1 (inf, 1);
302 if (print_inferior_events)
303 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
307 inferior_appeared (struct inferior *inf, int pid)
311 observer_notify_inferior_appeared (pid);
315 discard_all_inferiors (void)
317 struct inferior *inf;
319 for (inf = inferior_list; inf; inf = inf->next)
322 exit_inferior_silent (inf->pid);
327 find_inferior_id (int num)
329 struct inferior *inf;
331 for (inf = inferior_list; inf; inf = inf->next)
339 find_inferior_pid (int pid)
341 struct inferior *inf;
343 /* Looking for inferior pid == 0 is always wrong, and indicative of
344 a bug somewhere else. There may be more than one with pid == 0,
346 gdb_assert (pid != 0);
348 for (inf = inferior_list; inf; inf = inf->next)
355 /* Find an inferior bound to PSPACE. */
358 find_inferior_for_program_space (struct program_space *pspace)
360 struct inferior *inf;
362 for (inf = inferior_list; inf != NULL; inf = inf->next)
364 if (inf->pspace == pspace)
372 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
375 struct inferior *inf, *infnext;
377 for (inf = inferior_list; inf; inf = infnext)
380 if ((*callback) (inf, data))
388 valid_gdb_inferior_id (int num)
390 struct inferior *inf;
392 for (inf = inferior_list; inf; inf = inf->next)
400 pid_to_gdb_inferior_id (int pid)
402 struct inferior *inf;
404 for (inf = inferior_list; inf; inf = inf->next)
412 gdb_inferior_id_to_pid (int num)
414 struct inferior *inferior = find_inferior_id (num);
416 return inferior->pid;
422 in_inferior_list (int pid)
424 struct inferior *inf;
426 for (inf = inferior_list; inf; inf = inf->next)
434 have_inferiors (void)
436 struct inferior *inf;
438 for (inf = inferior_list; inf; inf = inf->next)
446 have_live_inferiors (void)
448 struct target_ops *t;
450 /* The check on stratum suffices, as GDB doesn't currently support
451 multiple target interfaces. */
452 if (have_inferiors ())
453 for (t = current_target.beneath; t != NULL; t = t->beneath)
454 if (t->to_stratum == process_stratum)
460 /* Prune away automatically added program spaces that aren't required
464 prune_inferiors (void)
466 struct inferior *ss, **ss_link;
467 struct inferior *current = current_inferior ();
470 ss_link = &inferior_list;
483 delete_inferior_1 (ss, 1);
487 prune_program_spaces ();
490 /* Simply returns the count of inferiors. */
493 number_of_inferiors (void)
495 struct inferior *inf;
498 for (inf = inferior_list; inf != NULL; inf = inf->next)
504 /* Prints the list of inferiors and their details on UIOUT. This is a
505 version of 'info_inferior_command' suitable for use from MI.
507 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
508 should be printed. Otherwise, all inferiors are printed. */
510 print_inferior (struct ui_out *uiout, int requested_inferior)
512 struct inferior *inf;
513 struct cleanup *old_chain;
516 /* Compute number of inferiors we will print. */
517 for (inf = inferior_list; inf; inf = inf->next)
519 struct cleanup *chain2;
521 if (requested_inferior != -1 && inf->num != requested_inferior)
529 ui_out_message (uiout, 0, "No inferiors.\n");
533 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
535 ui_out_table_header (uiout, 1, ui_left, "current", "");
536 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
537 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
538 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
540 ui_out_table_body (uiout);
541 for (inf = inferior_list; inf; inf = inf->next)
543 struct cleanup *chain2;
545 if (requested_inferior != -1 && inf->num != requested_inferior)
548 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
550 if (inf == current_inferior ())
551 ui_out_field_string (uiout, "current", "*");
553 ui_out_field_skip (uiout, "current");
555 ui_out_field_int (uiout, "number", inf->num);
558 ui_out_field_string (uiout, "target-id",
559 target_pid_to_str (pid_to_ptid (inf->pid)));
561 ui_out_field_string (uiout, "target-id", "<null>");
563 if (inf->pspace->ebfd)
564 ui_out_field_string (uiout, "exec",
565 bfd_get_filename (inf->pspace->ebfd));
567 ui_out_field_skip (uiout, "exec");
569 /* Print extra info that isn't really fit to always present in
570 tabular form. Currently we print the vfork parent/child
571 relationships, if any. */
572 if (inf->vfork_parent)
574 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
575 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
577 if (inf->vfork_child)
579 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
580 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
583 ui_out_text (uiout, "\n");
584 do_cleanups (chain2);
587 do_cleanups (old_chain);
591 detach_inferior_command (char *args, int from_tty)
594 struct thread_info *tp;
597 error (_("Requires argument (inferior id to detach)"));
599 num = parse_and_eval_long (args);
601 if (!valid_gdb_inferior_id (num))
602 error (_("Inferior ID %d not known."), num);
604 pid = gdb_inferior_id_to_pid (num);
606 tp = any_thread_of_process (pid);
608 error (_("Inferior has no threads."));
610 switch_to_thread (tp->ptid);
612 detach_command (NULL, from_tty);
616 kill_inferior_command (char *args, int from_tty)
619 struct thread_info *tp;
622 error (_("Requires argument (inferior id to kill)"));
624 num = parse_and_eval_long (args);
626 if (!valid_gdb_inferior_id (num))
627 error (_("Inferior ID %d not known."), num);
629 pid = gdb_inferior_id_to_pid (num);
631 tp = any_thread_of_process (pid);
633 error (_("Inferior has no threads."));
635 switch_to_thread (tp->ptid);
639 bfd_cache_close_all ();
643 inferior_command (char *args, int from_tty)
645 struct inferior *inf;
648 num = parse_and_eval_long (args);
650 inf = find_inferior_id (num);
652 error (_("Inferior ID %d not known."), num);
654 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
656 target_pid_to_str (pid_to_ptid (inf->pid)),
658 ? bfd_get_filename (inf->pspace->ebfd)
663 if (inf->pid != ptid_get_pid (inferior_ptid))
665 struct thread_info *tp;
667 tp = any_thread_of_process (inf->pid);
669 error (_("Inferior has no threads."));
671 switch_to_thread (tp->ptid);
674 printf_filtered (_("[Switching to thread %d (%s)] "),
675 pid_to_thread_id (inferior_ptid),
676 target_pid_to_str (inferior_ptid));
680 struct inferior *inf;
682 inf = find_inferior_id (num);
683 set_current_inferior (inf);
684 switch_to_thread (null_ptid);
685 set_current_program_space (inf->pspace);
688 if (inf->pid != 0 && is_running (inferior_ptid))
689 ui_out_text (uiout, "(running)\n");
690 else if (inf->pid != 0)
692 ui_out_text (uiout, "\n");
693 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
697 /* Print information about currently known inferiors. */
700 info_inferiors_command (char *args, int from_tty)
706 requested = parse_and_eval_long (args);
707 if (!valid_gdb_inferior_id (requested))
708 error (_("Inferior ID %d not known."), requested);
711 print_inferior (uiout, requested);
714 /* remove-inferior ID */
717 remove_inferior_command (char *args, int from_tty)
720 struct inferior *inf;
722 num = parse_and_eval_long (args);
723 inf = find_inferior_id (num);
726 error (_("Inferior ID %d not known."), num);
728 if (inf == current_inferior ())
729 error (_("Can not remove current symbol inferior."));
731 delete_inferior_1 (inf, 1);
735 /* add-inferior [-copies N] [-exec FILENAME] */
738 add_inferior_command (char *args, int from_tty)
743 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
747 argv = gdb_buildargv (args);
748 make_cleanup_freeargv (argv);
750 for (; *argv != NULL; argv++)
754 if (strcmp (*argv, "-copies") == 0)
758 error (_("No argument to -copies"));
759 copies = parse_and_eval_long (*argv);
761 else if (strcmp (*argv, "-exec") == 0)
765 error (_("No argument to -exec"));
770 error (_("Invalid argument"));
774 save_current_space_and_thread ();
776 for (i = 0; i < copies; ++i)
778 struct address_space *aspace;
779 struct program_space *pspace;
780 struct inferior *inf;
782 /* If all inferiors share an address space on this system, this
783 doesn't really return a new address space; otherwise, it
785 aspace = maybe_new_address_space ();
786 pspace = add_program_space (aspace);
787 inf = add_inferior (0);
788 inf->pspace = pspace;
789 inf->aspace = pspace->aspace;
791 printf_filtered (_("Added inferior %d\n"), inf->num);
795 /* Switch over temporarily, while reading executable and
797 set_current_program_space (pspace);
798 set_current_inferior (inf);
799 switch_to_thread (null_ptid);
801 exec_file_attach (exec, from_tty);
802 symbol_file_add_main (exec, from_tty);
806 do_cleanups (old_chain);
809 /* clone-inferior [-copies N] [ID] */
812 clone_inferior_command (char *args, int from_tty)
816 struct inferior *orginf = NULL;
817 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
821 argv = gdb_buildargv (args);
822 make_cleanup_freeargv (argv);
824 for (; *argv != NULL; argv++)
828 if (strcmp (*argv, "-copies") == 0)
832 error (_("No argument to -copies"));
833 copies = parse_and_eval_long (*argv);
836 error (_("Invalid copies number"));
845 /* The first non-option (-) argument specified the
847 num = parse_and_eval_long (*argv);
848 orginf = find_inferior_id (num);
851 error (_("Inferior ID %d not known."), num);
855 error (_("Invalid argument"));
860 /* If no inferior id was specified, then the user wants to clone the
863 orginf = current_inferior ();
865 save_current_space_and_thread ();
867 for (i = 0; i < copies; ++i)
869 struct address_space *aspace;
870 struct program_space *pspace;
871 struct inferior *inf;
873 /* If all inferiors share an address space on this system, this
874 doesn't really return a new address space; otherwise, it
876 aspace = maybe_new_address_space ();
877 pspace = add_program_space (aspace);
878 inf = add_inferior (0);
879 inf->pspace = pspace;
880 inf->aspace = pspace->aspace;
882 printf_filtered (_("Added inferior %d.\n"), inf->num);
884 set_current_inferior (inf);
885 switch_to_thread (null_ptid);
886 clone_program_space (pspace, orginf->pspace);
889 do_cleanups (old_chain);
892 /* Print notices when new inferiors are created and die. */
894 show_print_inferior_events (struct ui_file *file, int from_tty,
895 struct cmd_list_element *c, const char *value)
897 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
902 /* Keep a registry of per-inferior data-pointers required by other GDB
908 void (*cleanup) (struct inferior *, void *);
911 struct inferior_data_registration
913 struct inferior_data *data;
914 struct inferior_data_registration *next;
917 struct inferior_data_registry
919 struct inferior_data_registration *registrations;
920 unsigned num_registrations;
923 static struct inferior_data_registry inferior_data_registry
926 const struct inferior_data *
927 register_inferior_data_with_cleanup
928 (void (*cleanup) (struct inferior *, void *))
930 struct inferior_data_registration **curr;
932 /* Append new registration. */
933 for (curr = &inferior_data_registry.registrations;
934 *curr != NULL; curr = &(*curr)->next);
936 *curr = XMALLOC (struct inferior_data_registration);
937 (*curr)->next = NULL;
938 (*curr)->data = XMALLOC (struct inferior_data);
939 (*curr)->data->index = inferior_data_registry.num_registrations++;
940 (*curr)->data->cleanup = cleanup;
942 return (*curr)->data;
945 const struct inferior_data *
946 register_inferior_data (void)
948 return register_inferior_data_with_cleanup (NULL);
952 inferior_alloc_data (struct inferior *inf)
954 gdb_assert (inf->data == NULL);
955 inf->num_data = inferior_data_registry.num_registrations;
956 inf->data = XCALLOC (inf->num_data, void *);
960 inferior_free_data (struct inferior *inf)
962 gdb_assert (inf->data != NULL);
963 clear_inferior_data (inf);
969 clear_inferior_data (struct inferior *inf)
971 struct inferior_data_registration *registration;
974 gdb_assert (inf->data != NULL);
976 for (registration = inferior_data_registry.registrations, i = 0;
978 registration = registration->next, i++)
979 if (inf->data[i] != NULL && registration->data->cleanup)
980 registration->data->cleanup (inf, inf->data[i]);
982 memset (inf->data, 0, inf->num_data * sizeof (void *));
986 set_inferior_data (struct inferior *inf,
987 const struct inferior_data *data,
990 gdb_assert (data->index < inf->num_data);
991 inf->data[data->index] = value;
995 inferior_data (struct inferior *inf, const struct inferior_data *data)
997 gdb_assert (data->index < inf->num_data);
998 return inf->data[data->index];
1002 initialize_inferiors (void)
1004 /* There's always one inferior. Note that this function isn't an
1005 automatic _initialize_foo function, since other _initialize_foo
1006 routines may need to install their per-inferior data keys. We
1007 can only allocate an inferior when all those modules have done
1008 that. Do this after initialize_progspace, due to the
1009 current_program_space reference. */
1010 current_inferior_ = add_inferior (0);
1011 current_inferior_->pspace = current_program_space;
1012 current_inferior_->aspace = current_program_space->aspace;
1014 add_info ("inferiors", info_inferiors_command,
1015 _("IDs of currently known inferiors."));
1017 add_com ("add-inferior", no_class, add_inferior_command, _("\
1018 Add a new inferior.\n\
1019 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1020 N is the optional number of inferior to add, default is 1.\n\
1021 FILENAME is the file name of the executable to use\n\
1022 as main program."));
1024 add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1025 Remove inferior ID.\n\
1026 Usage: remove-inferior ID"));
1028 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1029 Clone inferior ID.\n\
1030 Usage: clone-inferior [-copies <N>] [ID]\n\
1031 Add N copies of inferior ID. The new inferior has the same\n\
1032 executable loaded as the copied inferior. If -copies is not specified,\n\
1033 adds 1 copy. If ID is not specified, it is the current inferior\n\
1036 add_cmd ("inferior", class_run, detach_inferior_command, _("\
1037 Detach from inferior ID."),
1040 add_cmd ("inferior", class_run, kill_inferior_command, _("\
1041 Kill inferior ID."),
1044 add_cmd ("inferior", class_run, inferior_command, _("\
1045 Use this command to switch between inferiors.\n\
1046 The new inferior ID must be currently known."),
1049 add_setshow_boolean_cmd ("inferior-events", no_class,
1050 &print_inferior_events, _("\
1051 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1052 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1054 show_print_inferior_events,
1055 &setprintlist, &showprintlist);