1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2017 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/>. */
25 #include "completer.h"
27 #include "gdbthread.h"
33 #include "cli/cli-utils.h"
34 #include "continuations.h"
35 #include "arch-utils.h"
36 #include "target-descriptions.h"
37 #include "readline/tilde.h"
38 #include "progspace-and-thread.h"
40 void _initialize_inferiors (void);
42 /* Keep a registry of per-inferior data-pointers required by other GDB
45 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
47 struct inferior *inferior_list = NULL;
48 static int highest_inferior_num;
50 /* Print notices on inferior events (attach, detach, etc.), set with
51 `set print inferior-events'. */
52 static int print_inferior_events = 0;
54 /* The Current Inferior. This is a strong reference. I.e., whenever
55 an inferior is the current inferior, its refcount is
57 static struct inferior *current_inferior_ = NULL;
60 current_inferior (void)
62 return current_inferior_;
66 set_current_inferior (struct inferior *inf)
68 /* There's always an inferior. */
69 gdb_assert (inf != NULL);
72 current_inferior_->decref ();
73 current_inferior_ = inf;
76 inferior::~inferior ()
80 discard_all_inferior_continuations (inf);
81 inferior_free_data (inf);
83 xfree (inf->terminal);
84 target_desc_info_free (inf->tdesc_info);
88 inferior::inferior (int pid_)
89 : num (++highest_inferior_num),
91 environment (gdb_environ::from_host_environ ()),
94 inferior_alloc_data (this);
98 add_inferior_silent (int pid)
100 inferior *inf = new inferior (pid);
102 if (inferior_list == NULL)
108 for (last = inferior_list; last->next != NULL; last = last->next)
113 observer_notify_inferior_added (inf);
116 inferior_appeared (inf, pid);
122 add_inferior (int pid)
124 struct inferior *inf = add_inferior_silent (pid);
126 if (print_inferior_events)
127 printf_unfiltered (_("[New inferior %d]\n"), pid);
132 struct delete_thread_of_inferior_arg
139 delete_thread_of_inferior (struct thread_info *tp, void *data)
141 struct delete_thread_of_inferior_arg *arg
142 = (struct delete_thread_of_inferior_arg *) data;
144 if (ptid_get_pid (tp->ptid) == arg->pid)
147 delete_thread_silent (tp->ptid);
149 delete_thread (tp->ptid);
156 delete_inferior (struct inferior *todel)
158 struct inferior *inf, *infprev;
159 struct delete_thread_of_inferior_arg arg;
163 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
173 iterate_over_threads (delete_thread_of_inferior, &arg);
176 infprev->next = inf->next;
178 inferior_list = inf->next;
180 observer_notify_inferior_removed (inf);
182 /* If this program space is rendered useless, remove it. */
183 if (program_space_empty_p (inf->pspace))
184 delete_program_space (inf->pspace);
189 /* If SILENT then be quiet -- don't announce a inferior exit, or the
190 exit of its threads. */
193 exit_inferior_1 (struct inferior *inftoex, int silent)
195 struct inferior *inf;
196 struct delete_thread_of_inferior_arg arg;
198 for (inf = inferior_list; inf; inf = inf->next)
208 iterate_over_threads (delete_thread_of_inferior, &arg);
210 observer_notify_inferior_exit (inf);
217 if (inf->vfork_parent != NULL)
219 inf->vfork_parent->vfork_child = NULL;
220 inf->vfork_parent = NULL;
222 if (inf->vfork_child != NULL)
224 inf->vfork_child->vfork_parent = NULL;
225 inf->vfork_child = NULL;
228 inf->pending_detach = 0;
232 exit_inferior (int pid)
234 struct inferior *inf = find_inferior_pid (pid);
236 exit_inferior_1 (inf, 0);
238 if (print_inferior_events)
239 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
243 exit_inferior_silent (int pid)
245 struct inferior *inf = find_inferior_pid (pid);
247 exit_inferior_1 (inf, 1);
251 exit_inferior_num_silent (int num)
253 struct inferior *inf = find_inferior_id (num);
255 exit_inferior_1 (inf, 1);
259 detach_inferior (int pid)
261 struct inferior *inf = find_inferior_pid (pid);
263 exit_inferior_1 (inf, 0);
265 if (print_inferior_events)
266 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
270 inferior_appeared (struct inferior *inf, int pid)
273 inf->has_exit_code = 0;
276 observer_notify_inferior_appeared (inf);
280 discard_all_inferiors (void)
282 struct inferior *inf;
284 for (inf = inferior_list; inf; inf = inf->next)
287 exit_inferior_silent (inf->pid);
292 find_inferior_id (int num)
294 struct inferior *inf;
296 for (inf = inferior_list; inf; inf = inf->next)
304 find_inferior_pid (int pid)
306 struct inferior *inf;
308 /* Looking for inferior pid == 0 is always wrong, and indicative of
309 a bug somewhere else. There may be more than one with pid == 0,
311 gdb_assert (pid != 0);
313 for (inf = inferior_list; inf; inf = inf->next)
323 find_inferior_ptid (ptid_t ptid)
325 return find_inferior_pid (ptid_get_pid (ptid));
328 /* See inferior.h. */
331 find_inferior_for_program_space (struct program_space *pspace)
333 struct inferior *inf = current_inferior ();
335 if (inf->pspace == pspace)
338 for (inf = inferior_list; inf != NULL; inf = inf->next)
340 if (inf->pspace == pspace)
348 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
351 struct inferior *inf, *infnext;
353 for (inf = inferior_list; inf; inf = infnext)
356 if ((*callback) (inf, data))
364 valid_gdb_inferior_id (int num)
366 struct inferior *inf;
368 for (inf = inferior_list; inf; inf = inf->next)
376 pid_to_gdb_inferior_id (int pid)
378 struct inferior *inf;
380 for (inf = inferior_list; inf; inf = inf->next)
388 gdb_inferior_id_to_pid (int num)
390 struct inferior *inferior = find_inferior_id (num);
392 return inferior->pid;
398 in_inferior_list (int pid)
400 struct inferior *inf;
402 for (inf = inferior_list; inf; inf = inf->next)
410 have_inferiors (void)
412 struct inferior *inf;
414 for (inf = inferior_list; inf; inf = inf->next)
421 /* Return the number of live inferiors. We account for the case
422 where an inferior might have a non-zero pid but no threads, as
423 in the middle of a 'mourn' operation. */
426 number_of_live_inferiors (void)
428 struct inferior *inf;
431 for (inf = inferior_list; inf; inf = inf->next)
434 struct thread_info *tp;
436 ALL_NON_EXITED_THREADS (tp)
437 if (tp && ptid_get_pid (tp->ptid) == inf->pid)
438 if (target_has_execution_1 (tp->ptid))
440 /* Found a live thread in this inferior, go to the next
450 /* Return true if there is at least one live inferior. */
453 have_live_inferiors (void)
455 return number_of_live_inferiors () > 0;
458 /* Prune away any unused inferiors, and then prune away no longer used
462 prune_inferiors (void)
464 struct inferior *ss, **ss_link;
467 ss_link = &inferior_list;
470 if (!ss->deletable ()
480 delete_inferior (ss);
485 /* Simply returns the count of inferiors. */
488 number_of_inferiors (void)
490 struct inferior *inf;
493 for (inf = inferior_list; inf != NULL; inf = inf->next)
499 /* Converts an inferior process id to a string. Like
500 target_pid_to_str, but special cases the null process. */
503 inferior_pid_to_str (int pid)
506 return target_pid_to_str (pid_to_ptid (pid));
511 /* See inferior.h. */
514 print_selected_inferior (struct ui_out *uiout)
516 struct inferior *inf = current_inferior ();
517 const char *filename = inf->pspace->pspace_exec_filename;
519 if (filename == NULL)
520 filename = _("<noexec>");
522 uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"),
523 inf->num, inferior_pid_to_str (inf->pid), filename);
526 /* Prints the list of inferiors and their details on UIOUT. This is a
527 version of 'info_inferior_command' suitable for use from MI.
529 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
530 inferiors that should be printed. Otherwise, all inferiors are
534 print_inferior (struct ui_out *uiout, char *requested_inferiors)
536 struct inferior *inf;
537 struct cleanup *old_chain;
540 /* Compute number of inferiors we will print. */
541 for (inf = inferior_list; inf; inf = inf->next)
543 if (!number_is_in_list (requested_inferiors, inf->num))
551 uiout->message ("No inferiors.\n");
555 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
557 uiout->table_header (1, ui_left, "current", "");
558 uiout->table_header (4, ui_left, "number", "Num");
559 uiout->table_header (17, ui_left, "target-id", "Description");
560 uiout->table_header (17, ui_left, "exec", "Executable");
562 uiout->table_body ();
563 for (inf = inferior_list; inf; inf = inf->next)
565 if (!number_is_in_list (requested_inferiors, inf->num))
568 ui_out_emit_tuple tuple_emitter (uiout, NULL);
570 if (inf == current_inferior ())
571 uiout->field_string ("current", "*");
573 uiout->field_skip ("current");
575 uiout->field_int ("number", inf->num);
577 uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
579 if (inf->pspace->pspace_exec_filename != NULL)
580 uiout->field_string ("exec", inf->pspace->pspace_exec_filename);
582 uiout->field_skip ("exec");
584 /* Print extra info that isn't really fit to always present in
585 tabular form. Currently we print the vfork parent/child
586 relationships, if any. */
587 if (inf->vfork_parent)
589 uiout->text (_("\n\tis vfork child of inferior "));
590 uiout->field_int ("vfork-parent", inf->vfork_parent->num);
592 if (inf->vfork_child)
594 uiout->text (_("\n\tis vfork parent of inferior "));
595 uiout->field_int ("vfork-child", inf->vfork_child->num);
601 do_cleanups (old_chain);
605 detach_inferior_command (char *args, int from_tty)
607 struct thread_info *tp;
610 error (_("Requires argument (inferior id(s) to detach)"));
612 number_or_range_parser parser (args);
613 while (!parser.finished ())
615 int num = parser.get_number ();
617 if (!valid_gdb_inferior_id (num))
619 warning (_("Inferior ID %d not known."), num);
623 int pid = gdb_inferior_id_to_pid (num);
626 warning (_("Inferior ID %d is not running."), num);
630 tp = any_thread_of_process (pid);
633 warning (_("Inferior ID %d has no threads."), num);
637 switch_to_thread (tp->ptid);
639 detach_command (NULL, from_tty);
644 kill_inferior_command (char *args, int from_tty)
646 struct thread_info *tp;
649 error (_("Requires argument (inferior id(s) to kill)"));
651 number_or_range_parser parser (args);
652 while (!parser.finished ())
654 int num = parser.get_number ();
656 if (!valid_gdb_inferior_id (num))
658 warning (_("Inferior ID %d not known."), num);
662 int pid = gdb_inferior_id_to_pid (num);
665 warning (_("Inferior ID %d is not running."), num);
669 tp = any_thread_of_process (pid);
672 warning (_("Inferior ID %d has no threads."), num);
676 switch_to_thread (tp->ptid);
681 bfd_cache_close_all ();
685 inferior_command (char *args, int from_tty)
687 struct inferior *inf;
690 num = parse_and_eval_long (args);
692 inf = find_inferior_id (num);
694 error (_("Inferior ID %d not known."), num);
698 if (inf->pid != ptid_get_pid (inferior_ptid))
700 struct thread_info *tp;
702 tp = any_thread_of_process (inf->pid);
704 error (_("Inferior has no threads."));
706 switch_to_thread (tp->ptid);
709 observer_notify_user_selected_context_changed
710 (USER_SELECTED_INFERIOR
711 | USER_SELECTED_THREAD
712 | USER_SELECTED_FRAME);
716 set_current_inferior (inf);
717 switch_to_thread (null_ptid);
718 set_current_program_space (inf->pspace);
720 observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR);
724 /* Print information about currently known inferiors. */
727 info_inferiors_command (char *args, int from_tty)
729 print_inferior (current_uiout, args);
732 /* remove-inferior ID */
735 remove_inferior_command (char *args, int from_tty)
737 if (args == NULL || *args == '\0')
738 error (_("Requires an argument (inferior id(s) to remove)"));
740 number_or_range_parser parser (args);
741 while (!parser.finished ())
743 int num = parser.get_number ();
744 struct inferior *inf = find_inferior_id (num);
748 warning (_("Inferior ID %d not known."), num);
752 if (!inf->deletable ())
754 warning (_("Can not remove current inferior %d."), num);
760 warning (_("Can not remove active inferior %d."), num);
764 delete_inferior (inf);
769 add_inferior_with_spaces (void)
771 struct address_space *aspace;
772 struct program_space *pspace;
773 struct inferior *inf;
774 struct gdbarch_info info;
776 /* If all inferiors share an address space on this system, this
777 doesn't really return a new address space; otherwise, it
779 aspace = maybe_new_address_space ();
780 pspace = add_program_space (aspace);
781 inf = add_inferior (0);
782 inf->pspace = pspace;
783 inf->aspace = pspace->aspace;
785 /* Setup the inferior's initial arch, based on information obtained
786 from the global "set ..." options. */
787 gdbarch_info_init (&info);
788 inf->gdbarch = gdbarch_find_by_info (info);
789 /* The "set ..." options reject invalid settings, so we should
790 always have a valid arch by now. */
791 gdb_assert (inf->gdbarch != NULL);
796 /* add-inferior [-copies N] [-exec FILENAME] */
799 add_inferior_command (char *args, int from_tty)
804 symfile_add_flags add_flags = 0;
805 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
808 add_flags |= SYMFILE_VERBOSE;
812 argv = gdb_buildargv (args);
813 make_cleanup_freeargv (argv);
815 for (; *argv != NULL; argv++)
819 if (strcmp (*argv, "-copies") == 0)
823 error (_("No argument to -copies"));
824 copies = parse_and_eval_long (*argv);
826 else if (strcmp (*argv, "-exec") == 0)
830 error (_("No argument to -exec"));
831 exec = tilde_expand (*argv);
832 make_cleanup (xfree, exec);
836 error (_("Invalid argument"));
840 scoped_restore_current_pspace_and_thread restore_pspace_thread;
842 for (i = 0; i < copies; ++i)
844 struct inferior *inf = add_inferior_with_spaces ();
846 printf_filtered (_("Added inferior %d\n"), inf->num);
850 /* Switch over temporarily, while reading executable and
852 set_current_program_space (inf->pspace);
853 set_current_inferior (inf);
854 switch_to_thread (null_ptid);
856 exec_file_attach (exec, from_tty);
857 symbol_file_add_main (exec, add_flags);
861 do_cleanups (old_chain);
864 /* clone-inferior [-copies N] [ID] */
867 clone_inferior_command (char *args, int from_tty)
871 struct inferior *orginf = NULL;
872 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
876 argv = gdb_buildargv (args);
877 make_cleanup_freeargv (argv);
879 for (; *argv != NULL; argv++)
883 if (strcmp (*argv, "-copies") == 0)
887 error (_("No argument to -copies"));
888 copies = parse_and_eval_long (*argv);
891 error (_("Invalid copies number"));
900 /* The first non-option (-) argument specified the
902 num = parse_and_eval_long (*argv);
903 orginf = find_inferior_id (num);
906 error (_("Inferior ID %d not known."), num);
910 error (_("Invalid argument"));
915 /* If no inferior id was specified, then the user wants to clone the
918 orginf = current_inferior ();
920 scoped_restore_current_pspace_and_thread restore_pspace_thread;
922 for (i = 0; i < copies; ++i)
924 struct address_space *aspace;
925 struct program_space *pspace;
926 struct inferior *inf;
928 /* If all inferiors share an address space on this system, this
929 doesn't really return a new address space; otherwise, it
931 aspace = maybe_new_address_space ();
932 pspace = add_program_space (aspace);
933 inf = add_inferior (0);
934 inf->pspace = pspace;
935 inf->aspace = pspace->aspace;
936 inf->gdbarch = orginf->gdbarch;
938 /* If the original inferior had a user specified target
939 description, make the clone use it too. */
940 if (target_desc_info_from_user_p (inf->tdesc_info))
941 copy_inferior_target_desc_info (inf, orginf);
943 printf_filtered (_("Added inferior %d.\n"), inf->num);
945 set_current_inferior (inf);
946 switch_to_thread (null_ptid);
947 clone_program_space (pspace, orginf->pspace);
950 do_cleanups (old_chain);
953 /* Print notices when new inferiors are created and die. */
955 show_print_inferior_events (struct ui_file *file, int from_tty,
956 struct cmd_list_element *c, const char *value)
958 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
961 /* Return a new value for the selected inferior's id. */
963 static struct value *
964 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
967 struct inferior *inf = current_inferior ();
969 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
972 /* Implementation of `$_inferior' variable. */
974 static const struct internalvar_funcs inferior_funcs =
976 inferior_id_make_value,
984 initialize_inferiors (void)
986 struct cmd_list_element *c = NULL;
988 /* There's always one inferior. Note that this function isn't an
989 automatic _initialize_foo function, since other _initialize_foo
990 routines may need to install their per-inferior data keys. We
991 can only allocate an inferior when all those modules have done
992 that. Do this after initialize_progspace, due to the
993 current_program_space reference. */
994 current_inferior_ = add_inferior (0);
995 current_inferior_->incref ();
996 current_inferior_->pspace = current_program_space;
997 current_inferior_->aspace = current_program_space->aspace;
998 /* The architecture will be initialized shortly, by
999 initialize_current_architecture. */
1001 add_info ("inferiors", info_inferiors_command,
1002 _("IDs of specified inferiors (all inferiors if no argument)."));
1004 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1005 Add a new inferior.\n\
1006 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1007 N is the optional number of inferiors to add, default is 1.\n\
1008 FILENAME is the file name of the executable to use\n\
1009 as main program."));
1010 set_cmd_completer (c, filename_completer);
1012 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1013 Remove inferior ID (or list of IDs).\n\
1014 Usage: remove-inferiors ID..."));
1016 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1017 Clone inferior ID.\n\
1018 Usage: clone-inferior [-copies <N>] [ID]\n\
1019 Add N copies of inferior ID. The new inferior has the same\n\
1020 executable loaded as the copied inferior. If -copies is not specified,\n\
1021 adds 1 copy. If ID is not specified, it is the current inferior\n\
1024 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1025 Detach from inferior ID (or list of IDS)."),
1028 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1029 Kill inferior ID (or list of IDs)."),
1032 add_cmd ("inferior", class_run, inferior_command, _("\
1033 Use this command to switch between inferiors.\n\
1034 The new inferior ID must be currently known."),
1037 add_setshow_boolean_cmd ("inferior-events", no_class,
1038 &print_inferior_events, _("\
1039 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1040 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1042 show_print_inferior_events,
1043 &setprintlist, &showprintlist);
1045 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);