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 free_environ (inf->environment);
85 target_desc_info_free (inf->tdesc_info);
89 inferior::inferior (int pid_)
90 : num (++highest_inferior_num),
92 environment (make_environ ()),
95 init_environ (this->environment);
96 inferior_alloc_data (this);
100 add_inferior_silent (int pid)
102 inferior *inf = new inferior (pid);
104 if (inferior_list == NULL)
110 for (last = inferior_list; last->next != NULL; last = last->next)
115 observer_notify_inferior_added (inf);
118 inferior_appeared (inf, pid);
124 add_inferior (int pid)
126 struct inferior *inf = add_inferior_silent (pid);
128 if (print_inferior_events)
129 printf_unfiltered (_("[New inferior %d]\n"), pid);
134 struct delete_thread_of_inferior_arg
141 delete_thread_of_inferior (struct thread_info *tp, void *data)
143 struct delete_thread_of_inferior_arg *arg
144 = (struct delete_thread_of_inferior_arg *) data;
146 if (ptid_get_pid (tp->ptid) == arg->pid)
149 delete_thread_silent (tp->ptid);
151 delete_thread (tp->ptid);
158 delete_inferior (struct inferior *todel)
160 struct inferior *inf, *infprev;
161 struct delete_thread_of_inferior_arg arg;
165 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
175 iterate_over_threads (delete_thread_of_inferior, &arg);
178 infprev->next = inf->next;
180 inferior_list = inf->next;
182 observer_notify_inferior_removed (inf);
184 /* If this program space is rendered useless, remove it. */
185 if (program_space_empty_p (inf->pspace))
186 delete_program_space (inf->pspace);
191 /* If SILENT then be quiet -- don't announce a inferior exit, or the
192 exit of its threads. */
195 exit_inferior_1 (struct inferior *inftoex, int silent)
197 struct inferior *inf;
198 struct delete_thread_of_inferior_arg arg;
200 for (inf = inferior_list; inf; inf = inf->next)
210 iterate_over_threads (delete_thread_of_inferior, &arg);
212 observer_notify_inferior_exit (inf);
219 if (inf->vfork_parent != NULL)
221 inf->vfork_parent->vfork_child = NULL;
222 inf->vfork_parent = NULL;
224 if (inf->vfork_child != NULL)
226 inf->vfork_child->vfork_parent = NULL;
227 inf->vfork_child = NULL;
230 inf->pending_detach = 0;
234 exit_inferior (int pid)
236 struct inferior *inf = find_inferior_pid (pid);
238 exit_inferior_1 (inf, 0);
240 if (print_inferior_events)
241 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
245 exit_inferior_silent (int pid)
247 struct inferior *inf = find_inferior_pid (pid);
249 exit_inferior_1 (inf, 1);
253 exit_inferior_num_silent (int num)
255 struct inferior *inf = find_inferior_id (num);
257 exit_inferior_1 (inf, 1);
261 detach_inferior (int pid)
263 struct inferior *inf = find_inferior_pid (pid);
265 exit_inferior_1 (inf, 0);
267 if (print_inferior_events)
268 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
272 inferior_appeared (struct inferior *inf, int pid)
275 inf->has_exit_code = 0;
278 observer_notify_inferior_appeared (inf);
282 discard_all_inferiors (void)
284 struct inferior *inf;
286 for (inf = inferior_list; inf; inf = inf->next)
289 exit_inferior_silent (inf->pid);
294 find_inferior_id (int num)
296 struct inferior *inf;
298 for (inf = inferior_list; inf; inf = inf->next)
306 find_inferior_pid (int pid)
308 struct inferior *inf;
310 /* Looking for inferior pid == 0 is always wrong, and indicative of
311 a bug somewhere else. There may be more than one with pid == 0,
313 gdb_assert (pid != 0);
315 for (inf = inferior_list; inf; inf = inf->next)
325 find_inferior_ptid (ptid_t ptid)
327 return find_inferior_pid (ptid_get_pid (ptid));
330 /* See inferior.h. */
333 find_inferior_for_program_space (struct program_space *pspace)
335 struct inferior *inf = current_inferior ();
337 if (inf->pspace == pspace)
340 for (inf = inferior_list; inf != NULL; inf = inf->next)
342 if (inf->pspace == pspace)
350 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
353 struct inferior *inf, *infnext;
355 for (inf = inferior_list; inf; inf = infnext)
358 if ((*callback) (inf, data))
366 valid_gdb_inferior_id (int num)
368 struct inferior *inf;
370 for (inf = inferior_list; inf; inf = inf->next)
378 pid_to_gdb_inferior_id (int pid)
380 struct inferior *inf;
382 for (inf = inferior_list; inf; inf = inf->next)
390 gdb_inferior_id_to_pid (int num)
392 struct inferior *inferior = find_inferior_id (num);
394 return inferior->pid;
400 in_inferior_list (int pid)
402 struct inferior *inf;
404 for (inf = inferior_list; inf; inf = inf->next)
412 have_inferiors (void)
414 struct inferior *inf;
416 for (inf = inferior_list; inf; inf = inf->next)
423 /* Return the number of live inferiors. We account for the case
424 where an inferior might have a non-zero pid but no threads, as
425 in the middle of a 'mourn' operation. */
428 number_of_live_inferiors (void)
430 struct inferior *inf;
433 for (inf = inferior_list; inf; inf = inf->next)
436 struct thread_info *tp;
438 ALL_NON_EXITED_THREADS (tp)
439 if (tp && ptid_get_pid (tp->ptid) == inf->pid)
440 if (target_has_execution_1 (tp->ptid))
442 /* Found a live thread in this inferior, go to the next
452 /* Return true if there is at least one live inferior. */
455 have_live_inferiors (void)
457 return number_of_live_inferiors () > 0;
460 /* Prune away any unused inferiors, and then prune away no longer used
464 prune_inferiors (void)
466 struct inferior *ss, **ss_link;
469 ss_link = &inferior_list;
472 if (!ss->deletable ()
482 delete_inferior (ss);
487 /* Simply returns the count of inferiors. */
490 number_of_inferiors (void)
492 struct inferior *inf;
495 for (inf = inferior_list; inf != NULL; inf = inf->next)
501 /* Converts an inferior process id to a string. Like
502 target_pid_to_str, but special cases the null process. */
505 inferior_pid_to_str (int pid)
508 return target_pid_to_str (pid_to_ptid (pid));
513 /* See inferior.h. */
516 print_selected_inferior (struct ui_out *uiout)
518 struct inferior *inf = current_inferior ();
519 const char *filename = inf->pspace->pspace_exec_filename;
521 if (filename == NULL)
522 filename = _("<noexec>");
524 uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"),
525 inf->num, inferior_pid_to_str (inf->pid), filename);
528 /* Prints the list of inferiors and their details on UIOUT. This is a
529 version of 'info_inferior_command' suitable for use from MI.
531 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
532 inferiors that should be printed. Otherwise, all inferiors are
536 print_inferior (struct ui_out *uiout, char *requested_inferiors)
538 struct inferior *inf;
539 struct cleanup *old_chain;
542 /* Compute number of inferiors we will print. */
543 for (inf = inferior_list; inf; inf = inf->next)
545 if (!number_is_in_list (requested_inferiors, inf->num))
553 uiout->message ("No inferiors.\n");
557 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
559 uiout->table_header (1, ui_left, "current", "");
560 uiout->table_header (4, ui_left, "number", "Num");
561 uiout->table_header (17, ui_left, "target-id", "Description");
562 uiout->table_header (17, ui_left, "exec", "Executable");
564 uiout->table_body ();
565 for (inf = inferior_list; inf; inf = inf->next)
567 if (!number_is_in_list (requested_inferiors, inf->num))
570 ui_out_emit_tuple tuple_emitter (uiout, NULL);
572 if (inf == current_inferior ())
573 uiout->field_string ("current", "*");
575 uiout->field_skip ("current");
577 uiout->field_int ("number", inf->num);
579 uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
581 if (inf->pspace->pspace_exec_filename != NULL)
582 uiout->field_string ("exec", inf->pspace->pspace_exec_filename);
584 uiout->field_skip ("exec");
586 /* Print extra info that isn't really fit to always present in
587 tabular form. Currently we print the vfork parent/child
588 relationships, if any. */
589 if (inf->vfork_parent)
591 uiout->text (_("\n\tis vfork child of inferior "));
592 uiout->field_int ("vfork-parent", inf->vfork_parent->num);
594 if (inf->vfork_child)
596 uiout->text (_("\n\tis vfork parent of inferior "));
597 uiout->field_int ("vfork-child", inf->vfork_child->num);
603 do_cleanups (old_chain);
607 detach_inferior_command (char *args, int from_tty)
609 struct thread_info *tp;
612 error (_("Requires argument (inferior id(s) to detach)"));
614 number_or_range_parser parser (args);
615 while (!parser.finished ())
617 int num = parser.get_number ();
619 if (!valid_gdb_inferior_id (num))
621 warning (_("Inferior ID %d not known."), num);
625 int pid = gdb_inferior_id_to_pid (num);
628 warning (_("Inferior ID %d is not running."), num);
632 tp = any_thread_of_process (pid);
635 warning (_("Inferior ID %d has no threads."), num);
639 switch_to_thread (tp->ptid);
641 detach_command (NULL, from_tty);
646 kill_inferior_command (char *args, int from_tty)
648 struct thread_info *tp;
651 error (_("Requires argument (inferior id(s) to kill)"));
653 number_or_range_parser parser (args);
654 while (!parser.finished ())
656 int num = parser.get_number ();
658 if (!valid_gdb_inferior_id (num))
660 warning (_("Inferior ID %d not known."), num);
664 int pid = gdb_inferior_id_to_pid (num);
667 warning (_("Inferior ID %d is not running."), num);
671 tp = any_thread_of_process (pid);
674 warning (_("Inferior ID %d has no threads."), num);
678 switch_to_thread (tp->ptid);
683 bfd_cache_close_all ();
687 inferior_command (char *args, int from_tty)
689 struct inferior *inf;
692 num = parse_and_eval_long (args);
694 inf = find_inferior_id (num);
696 error (_("Inferior ID %d not known."), num);
700 if (inf->pid != ptid_get_pid (inferior_ptid))
702 struct thread_info *tp;
704 tp = any_thread_of_process (inf->pid);
706 error (_("Inferior has no threads."));
708 switch_to_thread (tp->ptid);
711 observer_notify_user_selected_context_changed
712 (USER_SELECTED_INFERIOR
713 | USER_SELECTED_THREAD
714 | USER_SELECTED_FRAME);
718 set_current_inferior (inf);
719 switch_to_thread (null_ptid);
720 set_current_program_space (inf->pspace);
722 observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR);
726 /* Print information about currently known inferiors. */
729 info_inferiors_command (char *args, int from_tty)
731 print_inferior (current_uiout, args);
734 /* remove-inferior ID */
737 remove_inferior_command (char *args, int from_tty)
739 if (args == NULL || *args == '\0')
740 error (_("Requires an argument (inferior id(s) to remove)"));
742 number_or_range_parser parser (args);
743 while (!parser.finished ())
745 int num = parser.get_number ();
746 struct inferior *inf = find_inferior_id (num);
750 warning (_("Inferior ID %d not known."), num);
754 if (!inf->deletable ())
756 warning (_("Can not remove current inferior %d."), num);
762 warning (_("Can not remove active inferior %d."), num);
766 delete_inferior (inf);
771 add_inferior_with_spaces (void)
773 struct address_space *aspace;
774 struct program_space *pspace;
775 struct inferior *inf;
776 struct gdbarch_info info;
778 /* If all inferiors share an address space on this system, this
779 doesn't really return a new address space; otherwise, it
781 aspace = maybe_new_address_space ();
782 pspace = add_program_space (aspace);
783 inf = add_inferior (0);
784 inf->pspace = pspace;
785 inf->aspace = pspace->aspace;
787 /* Setup the inferior's initial arch, based on information obtained
788 from the global "set ..." options. */
789 gdbarch_info_init (&info);
790 inf->gdbarch = gdbarch_find_by_info (info);
791 /* The "set ..." options reject invalid settings, so we should
792 always have a valid arch by now. */
793 gdb_assert (inf->gdbarch != NULL);
798 /* add-inferior [-copies N] [-exec FILENAME] */
801 add_inferior_command (char *args, int from_tty)
806 symfile_add_flags add_flags = 0;
807 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
810 add_flags |= SYMFILE_VERBOSE;
814 argv = gdb_buildargv (args);
815 make_cleanup_freeargv (argv);
817 for (; *argv != NULL; argv++)
821 if (strcmp (*argv, "-copies") == 0)
825 error (_("No argument to -copies"));
826 copies = parse_and_eval_long (*argv);
828 else if (strcmp (*argv, "-exec") == 0)
832 error (_("No argument to -exec"));
833 exec = tilde_expand (*argv);
834 make_cleanup (xfree, exec);
838 error (_("Invalid argument"));
842 scoped_restore_current_pspace_and_thread restore_pspace_thread;
844 for (i = 0; i < copies; ++i)
846 struct inferior *inf = add_inferior_with_spaces ();
848 printf_filtered (_("Added inferior %d\n"), inf->num);
852 /* Switch over temporarily, while reading executable and
854 set_current_program_space (inf->pspace);
855 set_current_inferior (inf);
856 switch_to_thread (null_ptid);
858 exec_file_attach (exec, from_tty);
859 symbol_file_add_main (exec, add_flags);
863 do_cleanups (old_chain);
866 /* clone-inferior [-copies N] [ID] */
869 clone_inferior_command (char *args, int from_tty)
873 struct inferior *orginf = NULL;
874 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
878 argv = gdb_buildargv (args);
879 make_cleanup_freeargv (argv);
881 for (; *argv != NULL; argv++)
885 if (strcmp (*argv, "-copies") == 0)
889 error (_("No argument to -copies"));
890 copies = parse_and_eval_long (*argv);
893 error (_("Invalid copies number"));
902 /* The first non-option (-) argument specified the
904 num = parse_and_eval_long (*argv);
905 orginf = find_inferior_id (num);
908 error (_("Inferior ID %d not known."), num);
912 error (_("Invalid argument"));
917 /* If no inferior id was specified, then the user wants to clone the
920 orginf = current_inferior ();
922 scoped_restore_current_pspace_and_thread restore_pspace_thread;
924 for (i = 0; i < copies; ++i)
926 struct address_space *aspace;
927 struct program_space *pspace;
928 struct inferior *inf;
930 /* If all inferiors share an address space on this system, this
931 doesn't really return a new address space; otherwise, it
933 aspace = maybe_new_address_space ();
934 pspace = add_program_space (aspace);
935 inf = add_inferior (0);
936 inf->pspace = pspace;
937 inf->aspace = pspace->aspace;
938 inf->gdbarch = orginf->gdbarch;
940 /* If the original inferior had a user specified target
941 description, make the clone use it too. */
942 if (target_desc_info_from_user_p (inf->tdesc_info))
943 copy_inferior_target_desc_info (inf, orginf);
945 printf_filtered (_("Added inferior %d.\n"), inf->num);
947 set_current_inferior (inf);
948 switch_to_thread (null_ptid);
949 clone_program_space (pspace, orginf->pspace);
952 do_cleanups (old_chain);
955 /* Print notices when new inferiors are created and die. */
957 show_print_inferior_events (struct ui_file *file, int from_tty,
958 struct cmd_list_element *c, const char *value)
960 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
963 /* Return a new value for the selected inferior's id. */
965 static struct value *
966 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
969 struct inferior *inf = current_inferior ();
971 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
974 /* Implementation of `$_inferior' variable. */
976 static const struct internalvar_funcs inferior_funcs =
978 inferior_id_make_value,
986 initialize_inferiors (void)
988 struct cmd_list_element *c = NULL;
990 /* There's always one inferior. Note that this function isn't an
991 automatic _initialize_foo function, since other _initialize_foo
992 routines may need to install their per-inferior data keys. We
993 can only allocate an inferior when all those modules have done
994 that. Do this after initialize_progspace, due to the
995 current_program_space reference. */
996 current_inferior_ = add_inferior (0);
997 current_inferior_->incref ();
998 current_inferior_->pspace = current_program_space;
999 current_inferior_->aspace = current_program_space->aspace;
1000 /* The architecture will be initialized shortly, by
1001 initialize_current_architecture. */
1003 add_info ("inferiors", info_inferiors_command,
1004 _("IDs of specified inferiors (all inferiors if no argument)."));
1006 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1007 Add a new inferior.\n\
1008 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1009 N is the optional number of inferiors to add, default is 1.\n\
1010 FILENAME is the file name of the executable to use\n\
1011 as main program."));
1012 set_cmd_completer (c, filename_completer);
1014 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1015 Remove inferior ID (or list of IDs).\n\
1016 Usage: remove-inferiors ID..."));
1018 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1019 Clone inferior ID.\n\
1020 Usage: clone-inferior [-copies <N>] [ID]\n\
1021 Add N copies of inferior ID. The new inferior has the same\n\
1022 executable loaded as the copied inferior. If -copies is not specified,\n\
1023 adds 1 copy. If ID is not specified, it is the current inferior\n\
1026 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1027 Detach from inferior ID (or list of IDS)."),
1030 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1031 Kill inferior ID (or list of IDs)."),
1034 add_cmd ("inferior", class_run, inferior_command, _("\
1035 Use this command to switch between inferiors.\n\
1036 The new inferior ID must be currently known."),
1039 add_setshow_boolean_cmd ("inferior-events", no_class,
1040 &print_inferior_events, _("\
1041 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1042 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1044 show_print_inferior_events,
1045 &setprintlist, &showprintlist);
1047 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);