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"
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. This is a strong reference. I.e., whenever
54 an inferior is the current inferior, its refcount is
56 static struct inferior
*current_inferior_
= NULL
;
59 current_inferior (void)
61 return current_inferior_
;
65 set_current_inferior (struct inferior
*inf
)
67 /* There's always an inferior. */
68 gdb_assert (inf
!= NULL
);
71 current_inferior_
->decref ();
72 current_inferior_
= inf
;
75 /* A cleanups callback, helper for save_current_program_space
79 restore_inferior (void *arg
)
81 struct inferior
*saved_inferior
= (struct inferior
*) arg
;
83 set_current_inferior (saved_inferior
);
86 /* Save the current program space so that it may be restored by a later
87 call to do_cleanups. Returns the struct cleanup pointer needed for
88 later doing the cleanup. */
91 save_current_inferior (void)
93 struct cleanup
*old_chain
= make_cleanup (restore_inferior
,
99 inferior::~inferior ()
101 inferior
*inf
= this;
103 discard_all_inferior_continuations (inf
);
104 inferior_free_data (inf
);
106 xfree (inf
->terminal
);
107 free_environ (inf
->environment
);
108 target_desc_info_free (inf
->tdesc_info
);
112 inferior::inferior (int pid_
)
113 : num (++highest_inferior_num
),
115 environment (make_environ ()),
118 init_environ (this->environment
);
119 inferior_alloc_data (this);
123 add_inferior_silent (int pid
)
125 inferior
*inf
= new inferior (pid
);
127 if (inferior_list
== NULL
)
133 for (last
= inferior_list
; last
->next
!= NULL
; last
= last
->next
)
138 observer_notify_inferior_added (inf
);
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
167 = (struct delete_thread_of_inferior_arg
*) data
;
169 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
172 delete_thread_silent (tp
->ptid
);
174 delete_thread (tp
->ptid
);
181 delete_inferior (struct inferior
*todel
)
183 struct inferior
*inf
, *infprev
;
184 struct delete_thread_of_inferior_arg arg
;
188 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
198 iterate_over_threads (delete_thread_of_inferior
, &arg
);
201 infprev
->next
= inf
->next
;
203 inferior_list
= inf
->next
;
205 observer_notify_inferior_removed (inf
);
207 /* If this program space is rendered useless, remove it. */
208 if (program_space_empty_p (inf
->pspace
))
209 delete_program_space (inf
->pspace
);
214 /* If SILENT then be quiet -- don't announce a inferior exit, or the
215 exit of its threads. */
218 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
220 struct inferior
*inf
;
221 struct delete_thread_of_inferior_arg arg
;
223 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
233 iterate_over_threads (delete_thread_of_inferior
, &arg
);
235 observer_notify_inferior_exit (inf
);
242 if (inf
->vfork_parent
!= NULL
)
244 inf
->vfork_parent
->vfork_child
= NULL
;
245 inf
->vfork_parent
= NULL
;
247 if (inf
->vfork_child
!= NULL
)
249 inf
->vfork_child
->vfork_parent
= NULL
;
250 inf
->vfork_child
= NULL
;
253 inf
->pending_detach
= 0;
257 exit_inferior (int pid
)
259 struct inferior
*inf
= find_inferior_pid (pid
);
261 exit_inferior_1 (inf
, 0);
263 if (print_inferior_events
)
264 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
268 exit_inferior_silent (int pid
)
270 struct inferior
*inf
= find_inferior_pid (pid
);
272 exit_inferior_1 (inf
, 1);
276 exit_inferior_num_silent (int num
)
278 struct inferior
*inf
= find_inferior_id (num
);
280 exit_inferior_1 (inf
, 1);
284 detach_inferior (int pid
)
286 struct inferior
*inf
= find_inferior_pid (pid
);
288 exit_inferior_1 (inf
, 0);
290 if (print_inferior_events
)
291 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
295 inferior_appeared (struct inferior
*inf
, int pid
)
298 inf
->has_exit_code
= 0;
301 observer_notify_inferior_appeared (inf
);
305 discard_all_inferiors (void)
307 struct inferior
*inf
;
309 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
312 exit_inferior_silent (inf
->pid
);
317 find_inferior_id (int num
)
319 struct inferior
*inf
;
321 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
329 find_inferior_pid (int pid
)
331 struct inferior
*inf
;
333 /* Looking for inferior pid == 0 is always wrong, and indicative of
334 a bug somewhere else. There may be more than one with pid == 0,
336 gdb_assert (pid
!= 0);
338 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
348 find_inferior_ptid (ptid_t ptid
)
350 return find_inferior_pid (ptid_get_pid (ptid
));
353 /* See inferior.h. */
356 find_inferior_for_program_space (struct program_space
*pspace
)
358 struct inferior
*inf
= current_inferior ();
360 if (inf
->pspace
== pspace
)
363 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
365 if (inf
->pspace
== pspace
)
373 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
376 struct inferior
*inf
, *infnext
;
378 for (inf
= inferior_list
; inf
; inf
= infnext
)
381 if ((*callback
) (inf
, data
))
389 valid_gdb_inferior_id (int num
)
391 struct inferior
*inf
;
393 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
401 pid_to_gdb_inferior_id (int pid
)
403 struct inferior
*inf
;
405 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
413 gdb_inferior_id_to_pid (int num
)
415 struct inferior
*inferior
= find_inferior_id (num
);
417 return inferior
->pid
;
423 in_inferior_list (int pid
)
425 struct inferior
*inf
;
427 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
435 have_inferiors (void)
437 struct inferior
*inf
;
439 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
446 /* Return the number of live inferiors. We account for the case
447 where an inferior might have a non-zero pid but no threads, as
448 in the middle of a 'mourn' operation. */
451 number_of_live_inferiors (void)
453 struct inferior
*inf
;
456 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
459 struct thread_info
*tp
;
461 ALL_NON_EXITED_THREADS (tp
)
462 if (tp
&& ptid_get_pid (tp
->ptid
) == inf
->pid
)
463 if (target_has_execution_1 (tp
->ptid
))
465 /* Found a live thread in this inferior, go to the next
475 /* Return true if there is at least one live inferior. */
478 have_live_inferiors (void)
480 return number_of_live_inferiors () > 0;
483 /* Prune away any unused inferiors, and then prune away no longer used
487 prune_inferiors (void)
489 struct inferior
*ss
, **ss_link
;
492 ss_link
= &inferior_list
;
495 if (!ss
->deletable ()
505 delete_inferior (ss
);
510 /* Simply returns the count of inferiors. */
513 number_of_inferiors (void)
515 struct inferior
*inf
;
518 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
524 /* Converts an inferior process id to a string. Like
525 target_pid_to_str, but special cases the null process. */
528 inferior_pid_to_str (int pid
)
531 return target_pid_to_str (pid_to_ptid (pid
));
536 /* See inferior.h. */
539 print_selected_inferior (struct ui_out
*uiout
)
541 struct inferior
*inf
= current_inferior ();
542 const char *filename
= inf
->pspace
->pspace_exec_filename
;
544 if (filename
== NULL
)
545 filename
= _("<noexec>");
547 uiout
->message (_("[Switching to inferior %d [%s] (%s)]\n"),
548 inf
->num
, inferior_pid_to_str (inf
->pid
), filename
);
551 /* Prints the list of inferiors and their details on UIOUT. This is a
552 version of 'info_inferior_command' suitable for use from MI.
554 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
555 inferiors that should be printed. Otherwise, all inferiors are
559 print_inferior (struct ui_out
*uiout
, char *requested_inferiors
)
561 struct inferior
*inf
;
562 struct cleanup
*old_chain
;
565 /* Compute number of inferiors we will print. */
566 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
568 if (!number_is_in_list (requested_inferiors
, inf
->num
))
576 uiout
->message ("No inferiors.\n");
580 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
582 uiout
->table_header (1, ui_left
, "current", "");
583 uiout
->table_header (4, ui_left
, "number", "Num");
584 uiout
->table_header (17, ui_left
, "target-id", "Description");
585 uiout
->table_header (17, ui_left
, "exec", "Executable");
587 uiout
->table_body ();
588 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
590 if (!number_is_in_list (requested_inferiors
, inf
->num
))
593 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
595 if (inf
== current_inferior ())
596 uiout
->field_string ("current", "*");
598 uiout
->field_skip ("current");
600 uiout
->field_int ("number", inf
->num
);
602 uiout
->field_string ("target-id", inferior_pid_to_str (inf
->pid
));
604 if (inf
->pspace
->pspace_exec_filename
!= NULL
)
605 uiout
->field_string ("exec", inf
->pspace
->pspace_exec_filename
);
607 uiout
->field_skip ("exec");
609 /* Print extra info that isn't really fit to always present in
610 tabular form. Currently we print the vfork parent/child
611 relationships, if any. */
612 if (inf
->vfork_parent
)
614 uiout
->text (_("\n\tis vfork child of inferior "));
615 uiout
->field_int ("vfork-parent", inf
->vfork_parent
->num
);
617 if (inf
->vfork_child
)
619 uiout
->text (_("\n\tis vfork parent of inferior "));
620 uiout
->field_int ("vfork-child", inf
->vfork_child
->num
);
626 do_cleanups (old_chain
);
630 detach_inferior_command (char *args
, int from_tty
)
632 struct thread_info
*tp
;
635 error (_("Requires argument (inferior id(s) to detach)"));
637 number_or_range_parser
parser (args
);
638 while (!parser
.finished ())
640 int num
= parser
.get_number ();
642 if (!valid_gdb_inferior_id (num
))
644 warning (_("Inferior ID %d not known."), num
);
648 int pid
= gdb_inferior_id_to_pid (num
);
651 warning (_("Inferior ID %d is not running."), num
);
655 tp
= any_thread_of_process (pid
);
658 warning (_("Inferior ID %d has no threads."), num
);
662 switch_to_thread (tp
->ptid
);
664 detach_command (NULL
, from_tty
);
669 kill_inferior_command (char *args
, int from_tty
)
671 struct thread_info
*tp
;
674 error (_("Requires argument (inferior id(s) to kill)"));
676 number_or_range_parser
parser (args
);
677 while (!parser
.finished ())
679 int num
= parser
.get_number ();
681 if (!valid_gdb_inferior_id (num
))
683 warning (_("Inferior ID %d not known."), num
);
687 int pid
= gdb_inferior_id_to_pid (num
);
690 warning (_("Inferior ID %d is not running."), num
);
694 tp
= any_thread_of_process (pid
);
697 warning (_("Inferior ID %d has no threads."), num
);
701 switch_to_thread (tp
->ptid
);
706 bfd_cache_close_all ();
710 inferior_command (char *args
, int from_tty
)
712 struct inferior
*inf
;
715 num
= parse_and_eval_long (args
);
717 inf
= find_inferior_id (num
);
719 error (_("Inferior ID %d not known."), num
);
723 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
725 struct thread_info
*tp
;
727 tp
= any_thread_of_process (inf
->pid
);
729 error (_("Inferior has no threads."));
731 switch_to_thread (tp
->ptid
);
734 observer_notify_user_selected_context_changed
735 (USER_SELECTED_INFERIOR
736 | USER_SELECTED_THREAD
737 | USER_SELECTED_FRAME
);
741 set_current_inferior (inf
);
742 switch_to_thread (null_ptid
);
743 set_current_program_space (inf
->pspace
);
745 observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR
);
749 /* Print information about currently known inferiors. */
752 info_inferiors_command (char *args
, int from_tty
)
754 print_inferior (current_uiout
, args
);
757 /* remove-inferior ID */
760 remove_inferior_command (char *args
, int from_tty
)
762 if (args
== NULL
|| *args
== '\0')
763 error (_("Requires an argument (inferior id(s) to remove)"));
765 number_or_range_parser
parser (args
);
766 while (!parser
.finished ())
768 int num
= parser
.get_number ();
769 struct inferior
*inf
= find_inferior_id (num
);
773 warning (_("Inferior ID %d not known."), num
);
777 if (!inf
->deletable ())
779 warning (_("Can not remove current inferior %d."), num
);
785 warning (_("Can not remove active inferior %d."), num
);
789 delete_inferior (inf
);
794 add_inferior_with_spaces (void)
796 struct address_space
*aspace
;
797 struct program_space
*pspace
;
798 struct inferior
*inf
;
799 struct gdbarch_info info
;
801 /* If all inferiors share an address space on this system, this
802 doesn't really return a new address space; otherwise, it
804 aspace
= maybe_new_address_space ();
805 pspace
= add_program_space (aspace
);
806 inf
= add_inferior (0);
807 inf
->pspace
= pspace
;
808 inf
->aspace
= pspace
->aspace
;
810 /* Setup the inferior's initial arch, based on information obtained
811 from the global "set ..." options. */
812 gdbarch_info_init (&info
);
813 inf
->gdbarch
= gdbarch_find_by_info (info
);
814 /* The "set ..." options reject invalid settings, so we should
815 always have a valid arch by now. */
816 gdb_assert (inf
->gdbarch
!= NULL
);
821 /* add-inferior [-copies N] [-exec FILENAME] */
824 add_inferior_command (char *args
, int from_tty
)
829 symfile_add_flags add_flags
= 0;
830 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
833 add_flags
|= SYMFILE_VERBOSE
;
837 argv
= gdb_buildargv (args
);
838 make_cleanup_freeargv (argv
);
840 for (; *argv
!= NULL
; argv
++)
844 if (strcmp (*argv
, "-copies") == 0)
848 error (_("No argument to -copies"));
849 copies
= parse_and_eval_long (*argv
);
851 else if (strcmp (*argv
, "-exec") == 0)
855 error (_("No argument to -exec"));
856 exec
= tilde_expand (*argv
);
857 make_cleanup (xfree
, exec
);
861 error (_("Invalid argument"));
865 save_current_space_and_thread ();
867 for (i
= 0; i
< copies
; ++i
)
869 struct inferior
*inf
= add_inferior_with_spaces ();
871 printf_filtered (_("Added inferior %d\n"), inf
->num
);
875 /* Switch over temporarily, while reading executable and
877 set_current_program_space (inf
->pspace
);
878 set_current_inferior (inf
);
879 switch_to_thread (null_ptid
);
881 exec_file_attach (exec
, from_tty
);
882 symbol_file_add_main (exec
, add_flags
);
886 do_cleanups (old_chain
);
889 /* clone-inferior [-copies N] [ID] */
892 clone_inferior_command (char *args
, int from_tty
)
896 struct inferior
*orginf
= NULL
;
897 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
901 argv
= gdb_buildargv (args
);
902 make_cleanup_freeargv (argv
);
904 for (; *argv
!= NULL
; argv
++)
908 if (strcmp (*argv
, "-copies") == 0)
912 error (_("No argument to -copies"));
913 copies
= parse_and_eval_long (*argv
);
916 error (_("Invalid copies number"));
925 /* The first non-option (-) argument specified the
927 num
= parse_and_eval_long (*argv
);
928 orginf
= find_inferior_id (num
);
931 error (_("Inferior ID %d not known."), num
);
935 error (_("Invalid argument"));
940 /* If no inferior id was specified, then the user wants to clone the
943 orginf
= current_inferior ();
945 save_current_space_and_thread ();
947 for (i
= 0; i
< copies
; ++i
)
949 struct address_space
*aspace
;
950 struct program_space
*pspace
;
951 struct inferior
*inf
;
953 /* If all inferiors share an address space on this system, this
954 doesn't really return a new address space; otherwise, it
956 aspace
= maybe_new_address_space ();
957 pspace
= add_program_space (aspace
);
958 inf
= add_inferior (0);
959 inf
->pspace
= pspace
;
960 inf
->aspace
= pspace
->aspace
;
961 inf
->gdbarch
= orginf
->gdbarch
;
963 /* If the original inferior had a user specified target
964 description, make the clone use it too. */
965 if (target_desc_info_from_user_p (inf
->tdesc_info
))
966 copy_inferior_target_desc_info (inf
, orginf
);
968 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
970 set_current_inferior (inf
);
971 switch_to_thread (null_ptid
);
972 clone_program_space (pspace
, orginf
->pspace
);
975 do_cleanups (old_chain
);
978 /* Print notices when new inferiors are created and die. */
980 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
981 struct cmd_list_element
*c
, const char *value
)
983 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
986 /* Return a new value for the selected inferior's id. */
988 static struct value
*
989 inferior_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
992 struct inferior
*inf
= current_inferior ();
994 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, inf
->num
);
997 /* Implementation of `$_inferior' variable. */
999 static const struct internalvar_funcs inferior_funcs
=
1001 inferior_id_make_value
,
1009 initialize_inferiors (void)
1011 struct cmd_list_element
*c
= NULL
;
1013 /* There's always one inferior. Note that this function isn't an
1014 automatic _initialize_foo function, since other _initialize_foo
1015 routines may need to install their per-inferior data keys. We
1016 can only allocate an inferior when all those modules have done
1017 that. Do this after initialize_progspace, due to the
1018 current_program_space reference. */
1019 current_inferior_
= add_inferior (0);
1020 current_inferior_
->incref ();
1021 current_inferior_
->pspace
= current_program_space
;
1022 current_inferior_
->aspace
= current_program_space
->aspace
;
1023 /* The architecture will be initialized shortly, by
1024 initialize_current_architecture. */
1026 add_info ("inferiors", info_inferiors_command
,
1027 _("IDs of specified inferiors (all inferiors if no argument)."));
1029 c
= add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1030 Add a new inferior.\n\
1031 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1032 N is the optional number of inferiors to add, default is 1.\n\
1033 FILENAME is the file name of the executable to use\n\
1034 as main program."));
1035 set_cmd_completer (c
, filename_completer
);
1037 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1038 Remove inferior ID (or list of IDs).\n\
1039 Usage: remove-inferiors ID..."));
1041 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1042 Clone inferior ID.\n\
1043 Usage: clone-inferior [-copies <N>] [ID]\n\
1044 Add N copies of inferior ID. The new inferior has the same\n\
1045 executable loaded as the copied inferior. If -copies is not specified,\n\
1046 adds 1 copy. If ID is not specified, it is the current inferior\n\
1049 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1050 Detach from inferior ID (or list of IDS)."),
1053 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1054 Kill inferior ID (or list of IDs)."),
1057 add_cmd ("inferior", class_run
, inferior_command
, _("\
1058 Use this command to switch between inferiors.\n\
1059 The new inferior ID must be currently known."),
1062 add_setshow_boolean_cmd ("inferior-events", no_class
,
1063 &print_inferior_events
, _("\
1064 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1065 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1067 show_print_inferior_events
,
1068 &setprintlist
, &showprintlist
);
1070 create_internalvar_type_lazy ("_inferior", &inferior_funcs
, NULL
);