1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2014 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"
39 void _initialize_inferiors (void);
41 /* Keep a registry of per-inferior data-pointers required by other GDB
44 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
46 struct inferior *inferior_list = NULL;
47 static int highest_inferior_num;
49 /* Print notices on inferior events (attach, detach, etc.), set with
50 `set print inferior-events'. */
51 static int print_inferior_events = 0;
53 /* The Current Inferior. */
54 static struct inferior *current_inferior_ = NULL;
57 current_inferior (void)
59 return current_inferior_;
63 set_current_inferior (struct inferior *inf)
65 /* There's always an inferior. */
66 gdb_assert (inf != NULL);
68 current_inferior_ = inf;
71 /* A cleanups callback, helper for save_current_program_space
75 restore_inferior (void *arg)
77 struct inferior *saved_inferior = arg;
79 set_current_inferior (saved_inferior);
82 /* Save the current program space so that it may be restored by a later
83 call to do_cleanups. Returns the struct cleanup pointer needed for
84 later doing the cleanup. */
87 save_current_inferior (void)
89 struct cleanup *old_chain = make_cleanup (restore_inferior,
96 free_inferior (struct inferior *inf)
98 discard_all_inferior_continuations (inf);
99 inferior_free_data (inf);
101 xfree (inf->terminal);
102 free_environ (inf->environment);
103 target_desc_info_free (inf->tdesc_info);
104 xfree (inf->private);
109 init_inferior_list (void)
111 struct inferior *inf, *infnext;
113 highest_inferior_num = 0;
117 for (inf = inferior_list; inf; inf = infnext)
123 inferior_list = NULL;
127 add_inferior_silent (int pid)
129 struct inferior *inf;
131 inf = xmalloc (sizeof (*inf));
132 memset (inf, 0, sizeof (*inf));
135 inf->control.stop_soon = NO_STOP_QUIETLY;
137 inf->num = ++highest_inferior_num;
138 inf->next = inferior_list;
141 inf->environment = make_environ ();
142 init_environ (inf->environment);
144 inferior_alloc_data (inf);
146 observer_notify_inferior_added (inf);
149 inferior_appeared (inf, pid);
155 add_inferior (int pid)
157 struct inferior *inf = add_inferior_silent (pid);
159 if (print_inferior_events)
160 printf_unfiltered (_("[New inferior %d]\n"), pid);
165 struct delete_thread_of_inferior_arg
172 delete_thread_of_inferior (struct thread_info *tp, void *data)
174 struct delete_thread_of_inferior_arg *arg = data;
176 if (ptid_get_pid (tp->ptid) == arg->pid)
179 delete_thread_silent (tp->ptid);
181 delete_thread (tp->ptid);
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;
215 observer_notify_inferior_removed (inf);
221 delete_inferior (int pid)
223 struct inferior *inf = find_inferior_pid (pid);
225 delete_inferior_1 (inf, 0);
227 if (print_inferior_events)
228 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
232 delete_inferior_silent (int pid)
234 struct inferior *inf = find_inferior_pid (pid);
236 delete_inferior_1 (inf, 1);
240 /* If SILENT then be quiet -- don't announce a inferior exit, or the
241 exit of its threads. */
244 exit_inferior_1 (struct inferior *inftoex, int silent)
246 struct inferior *inf;
247 struct delete_thread_of_inferior_arg arg;
249 for (inf = inferior_list; inf; inf = inf->next)
259 iterate_over_threads (delete_thread_of_inferior, &arg);
261 /* Notify the observers before removing the inferior from the list,
262 so that the observers have a chance to look it up. */
263 observer_notify_inferior_exit (inf);
267 if (inf->vfork_parent != NULL)
269 inf->vfork_parent->vfork_child = NULL;
270 inf->vfork_parent = NULL;
272 if (inf->vfork_child != NULL)
274 inf->vfork_child->vfork_parent = NULL;
275 inf->vfork_child = NULL;
278 inf->pending_detach = 0;
282 exit_inferior (int pid)
284 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);
297 exit_inferior_1 (inf, 1);
301 exit_inferior_num_silent (int num)
303 struct inferior *inf = find_inferior_id (num);
305 exit_inferior_1 (inf, 1);
309 detach_inferior (int pid)
311 struct inferior *inf = find_inferior_pid (pid);
313 exit_inferior_1 (inf, 1);
315 if (print_inferior_events)
316 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
320 inferior_appeared (struct inferior *inf, int pid)
323 inf->has_exit_code = 0;
326 observer_notify_inferior_appeared (inf);
330 discard_all_inferiors (void)
332 struct inferior *inf;
334 for (inf = inferior_list; inf; inf = inf->next)
337 exit_inferior_silent (inf->pid);
342 find_inferior_id (int num)
344 struct inferior *inf;
346 for (inf = inferior_list; inf; inf = inf->next)
354 find_inferior_pid (int pid)
356 struct inferior *inf;
358 /* Looking for inferior pid == 0 is always wrong, and indicative of
359 a bug somewhere else. There may be more than one with pid == 0,
361 gdb_assert (pid != 0);
363 for (inf = inferior_list; inf; inf = inf->next)
370 /* See inferior.h. */
373 find_inferior_for_program_space (struct program_space *pspace)
375 struct inferior *inf = current_inferior ();
377 if (inf->pspace == pspace)
380 for (inf = inferior_list; inf != NULL; inf = inf->next)
382 if (inf->pspace == pspace)
390 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
393 struct inferior *inf, *infnext;
395 for (inf = inferior_list; inf; inf = infnext)
398 if ((*callback) (inf, data))
406 valid_gdb_inferior_id (int num)
408 struct inferior *inf;
410 for (inf = inferior_list; inf; inf = inf->next)
418 pid_to_gdb_inferior_id (int pid)
420 struct inferior *inf;
422 for (inf = inferior_list; inf; inf = inf->next)
430 gdb_inferior_id_to_pid (int num)
432 struct inferior *inferior = find_inferior_id (num);
434 return inferior->pid;
440 in_inferior_list (int pid)
442 struct inferior *inf;
444 for (inf = inferior_list; inf; inf = inf->next)
452 have_inferiors (void)
454 struct inferior *inf;
456 for (inf = inferior_list; inf; inf = inf->next)
464 have_live_inferiors (void)
466 struct inferior *inf;
468 for (inf = inferior_list; inf; inf = inf->next)
471 struct thread_info *tp;
473 tp = any_thread_of_process (inf->pid);
474 if (tp && target_has_execution_1 (tp->ptid))
481 /* Prune away any unused inferiors, and then prune away no longer used
485 prune_inferiors (void)
487 struct inferior *ss, **ss_link;
488 struct inferior *current = current_inferior ();
491 ss_link = &inferior_list;
504 delete_inferior_1 (ss, 1);
508 prune_program_spaces ();
511 /* Simply returns the count of inferiors. */
514 number_of_inferiors (void)
516 struct inferior *inf;
519 for (inf = inferior_list; inf != NULL; inf = inf->next)
525 /* Converts an inferior process id to a string. Like
526 target_pid_to_str, but special cases the null process. */
529 inferior_pid_to_str (int pid)
532 return target_pid_to_str (pid_to_ptid (pid));
537 /* Prints the list of inferiors and their details on UIOUT. This is a
538 version of 'info_inferior_command' suitable for use from MI.
540 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
541 inferiors that should be printed. Otherwise, all inferiors are
545 print_inferior (struct ui_out *uiout, char *requested_inferiors)
547 struct inferior *inf;
548 struct cleanup *old_chain;
551 /* Compute number of inferiors we will print. */
552 for (inf = inferior_list; inf; inf = inf->next)
554 if (!number_is_in_list (requested_inferiors, inf->num))
562 ui_out_message (uiout, 0, "No inferiors.\n");
566 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
568 ui_out_table_header (uiout, 1, ui_left, "current", "");
569 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
570 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
571 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
573 ui_out_table_body (uiout);
574 for (inf = inferior_list; inf; inf = inf->next)
576 struct cleanup *chain2;
578 if (!number_is_in_list (requested_inferiors, inf->num))
581 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
583 if (inf == current_inferior ())
584 ui_out_field_string (uiout, "current", "*");
586 ui_out_field_skip (uiout, "current");
588 ui_out_field_int (uiout, "number", inf->num);
590 ui_out_field_string (uiout, "target-id",
591 inferior_pid_to_str (inf->pid));
593 if (inf->pspace->pspace_exec_filename != NULL)
594 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
596 ui_out_field_skip (uiout, "exec");
598 /* Print extra info that isn't really fit to always present in
599 tabular form. Currently we print the vfork parent/child
600 relationships, if any. */
601 if (inf->vfork_parent)
603 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
604 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
606 if (inf->vfork_child)
608 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
609 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
612 ui_out_text (uiout, "\n");
613 do_cleanups (chain2);
616 do_cleanups (old_chain);
620 detach_inferior_command (char *args, int from_tty)
623 struct thread_info *tp;
624 struct get_number_or_range_state state;
627 error (_("Requires argument (inferior id(s) to detach)"));
629 init_number_or_range (&state, args);
630 while (!state.finished)
632 num = get_number_or_range (&state);
634 if (!valid_gdb_inferior_id (num))
636 warning (_("Inferior ID %d not known."), num);
640 pid = gdb_inferior_id_to_pid (num);
642 tp = any_thread_of_process (pid);
645 warning (_("Inferior ID %d has no threads."), num);
649 switch_to_thread (tp->ptid);
651 detach_command (NULL, from_tty);
656 kill_inferior_command (char *args, int from_tty)
659 struct thread_info *tp;
660 struct get_number_or_range_state state;
663 error (_("Requires argument (inferior id(s) to kill)"));
665 init_number_or_range (&state, args);
666 while (!state.finished)
668 num = get_number_or_range (&state);
670 if (!valid_gdb_inferior_id (num))
672 warning (_("Inferior ID %d not known."), num);
676 pid = gdb_inferior_id_to_pid (num);
678 tp = any_thread_of_process (pid);
681 warning (_("Inferior ID %d has no threads."), num);
685 switch_to_thread (tp->ptid);
690 bfd_cache_close_all ();
694 inferior_command (char *args, int from_tty)
696 struct inferior *inf;
699 num = parse_and_eval_long (args);
701 inf = find_inferior_id (num);
703 error (_("Inferior ID %d not known."), num);
705 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
707 inferior_pid_to_str (inf->pid),
708 (inf->pspace->pspace_exec_filename != NULL
709 ? inf->pspace->pspace_exec_filename
714 if (inf->pid != ptid_get_pid (inferior_ptid))
716 struct thread_info *tp;
718 tp = any_thread_of_process (inf->pid);
720 error (_("Inferior has no threads."));
722 switch_to_thread (tp->ptid);
725 printf_filtered (_("[Switching to thread %d (%s)] "),
726 pid_to_thread_id (inferior_ptid),
727 target_pid_to_str (inferior_ptid));
731 struct inferior *inf;
733 inf = find_inferior_id (num);
734 set_current_inferior (inf);
735 switch_to_thread (null_ptid);
736 set_current_program_space (inf->pspace);
739 if (inf->pid != 0 && is_running (inferior_ptid))
740 ui_out_text (current_uiout, "(running)\n");
741 else if (inf->pid != 0)
743 ui_out_text (current_uiout, "\n");
744 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
748 /* Print information about currently known inferiors. */
751 info_inferiors_command (char *args, int from_tty)
753 print_inferior (current_uiout, args);
756 /* remove-inferior ID */
759 remove_inferior_command (char *args, int from_tty)
762 struct inferior *inf;
763 struct get_number_or_range_state state;
765 if (args == NULL || *args == '\0')
766 error (_("Requires an argument (inferior id(s) to remove)"));
768 init_number_or_range (&state, args);
769 while (!state.finished)
771 num = get_number_or_range (&state);
772 inf = find_inferior_id (num);
776 warning (_("Inferior ID %d not known."), num);
780 if (inf == current_inferior ())
782 warning (_("Can not remove current symbol inferior %d."), num);
788 warning (_("Can not remove active inferior %d."), num);
792 delete_inferior_1 (inf, 1);
795 prune_program_spaces ();
799 add_inferior_with_spaces (void)
801 struct address_space *aspace;
802 struct program_space *pspace;
803 struct inferior *inf;
804 struct gdbarch_info info;
806 /* If all inferiors share an address space on this system, this
807 doesn't really return a new address space; otherwise, it
809 aspace = maybe_new_address_space ();
810 pspace = add_program_space (aspace);
811 inf = add_inferior (0);
812 inf->pspace = pspace;
813 inf->aspace = pspace->aspace;
815 /* Setup the inferior's initial arch, based on information obtained
816 from the global "set ..." options. */
817 gdbarch_info_init (&info);
818 inf->gdbarch = gdbarch_find_by_info (info);
819 /* The "set ..." options reject invalid settings, so we should
820 always have a valid arch by now. */
821 gdb_assert (inf->gdbarch != NULL);
826 /* add-inferior [-copies N] [-exec FILENAME] */
829 add_inferior_command (char *args, int from_tty)
834 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
838 argv = gdb_buildargv (args);
839 make_cleanup_freeargv (argv);
841 for (; *argv != NULL; argv++)
845 if (strcmp (*argv, "-copies") == 0)
849 error (_("No argument to -copies"));
850 copies = parse_and_eval_long (*argv);
852 else if (strcmp (*argv, "-exec") == 0)
856 error (_("No argument to -exec"));
857 exec = tilde_expand (*argv);
858 make_cleanup (xfree, exec);
862 error (_("Invalid argument"));
866 save_current_space_and_thread ();
868 for (i = 0; i < copies; ++i)
870 struct inferior *inf = add_inferior_with_spaces ();
872 printf_filtered (_("Added inferior %d\n"), inf->num);
876 /* Switch over temporarily, while reading executable and
878 set_current_program_space (inf->pspace);
879 set_current_inferior (inf);
880 switch_to_thread (null_ptid);
882 exec_file_attach (exec, from_tty);
883 symbol_file_add_main (exec, from_tty);
887 do_cleanups (old_chain);
890 /* clone-inferior [-copies N] [ID] */
893 clone_inferior_command (char *args, int from_tty)
897 struct inferior *orginf = NULL;
898 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
902 argv = gdb_buildargv (args);
903 make_cleanup_freeargv (argv);
905 for (; *argv != NULL; argv++)
909 if (strcmp (*argv, "-copies") == 0)
913 error (_("No argument to -copies"));
914 copies = parse_and_eval_long (*argv);
917 error (_("Invalid copies number"));
926 /* The first non-option (-) argument specified the
928 num = parse_and_eval_long (*argv);
929 orginf = find_inferior_id (num);
932 error (_("Inferior ID %d not known."), num);
936 error (_("Invalid argument"));
941 /* If no inferior id was specified, then the user wants to clone the
944 orginf = current_inferior ();
946 save_current_space_and_thread ();
948 for (i = 0; i < copies; ++i)
950 struct address_space *aspace;
951 struct program_space *pspace;
952 struct inferior *inf;
954 /* If all inferiors share an address space on this system, this
955 doesn't really return a new address space; otherwise, it
957 aspace = maybe_new_address_space ();
958 pspace = add_program_space (aspace);
959 inf = add_inferior (0);
960 inf->pspace = pspace;
961 inf->aspace = pspace->aspace;
962 inf->gdbarch = orginf->gdbarch;
964 /* If the original inferior had a user specified target
965 description, make the clone use it too. */
966 if (target_desc_info_from_user_p (inf->tdesc_info))
967 copy_inferior_target_desc_info (inf, orginf);
969 printf_filtered (_("Added inferior %d.\n"), inf->num);
971 set_current_inferior (inf);
972 switch_to_thread (null_ptid);
973 clone_program_space (pspace, orginf->pspace);
976 do_cleanups (old_chain);
979 /* Print notices when new inferiors are created and die. */
981 show_print_inferior_events (struct ui_file *file, int from_tty,
982 struct cmd_list_element *c, const char *value)
984 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
990 initialize_inferiors (void)
992 struct cmd_list_element *c = NULL;
994 /* There's always one inferior. Note that this function isn't an
995 automatic _initialize_foo function, since other _initialize_foo
996 routines may need to install their per-inferior data keys. We
997 can only allocate an inferior when all those modules have done
998 that. Do this after initialize_progspace, due to the
999 current_program_space reference. */
1000 current_inferior_ = add_inferior (0);
1001 current_inferior_->pspace = current_program_space;
1002 current_inferior_->aspace = current_program_space->aspace;
1003 /* The architecture will be initialized shortly, by
1004 initialize_current_architecture. */
1006 add_info ("inferiors", info_inferiors_command,
1007 _("IDs of specified inferiors (all inferiors if no argument)."));
1009 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1010 Add a new inferior.\n\
1011 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1012 N is the optional number of inferiors to add, default is 1.\n\
1013 FILENAME is the file name of the executable to use\n\
1014 as main program."));
1015 set_cmd_completer (c, filename_completer);
1017 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1018 Remove inferior ID (or list of IDs).\n\
1019 Usage: remove-inferiors ID..."));
1021 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1022 Clone inferior ID.\n\
1023 Usage: clone-inferior [-copies <N>] [ID]\n\
1024 Add N copies of inferior ID. The new inferior has the same\n\
1025 executable loaded as the copied inferior. If -copies is not specified,\n\
1026 adds 1 copy. If ID is not specified, it is the current inferior\n\
1029 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1030 Detach from inferior ID (or list of IDS)."),
1033 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1034 Kill inferior ID (or list of IDs)."),
1037 add_cmd ("inferior", class_run, inferior_command, _("\
1038 Use this command to switch between inferiors.\n\
1039 The new inferior ID must be currently known."),
1042 add_setshow_boolean_cmd ("inferior-events", no_class,
1043 &print_inferior_events, _("\
1044 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1045 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1047 show_print_inferior_events,
1048 &setprintlist, &showprintlist);