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. */
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
= (struct 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
);
109 add_inferior_silent (int pid
)
111 struct inferior
*inf
;
113 inf
= XNEW (struct inferior
);
114 memset (inf
, 0, sizeof (*inf
));
117 inf
->control
.stop_soon
= NO_STOP_QUIETLY
;
119 inf
->num
= ++highest_inferior_num
;
121 if (inferior_list
== NULL
)
125 struct inferior
*last
;
127 for (last
= inferior_list
; last
->next
!= NULL
; last
= last
->next
)
132 inf
->environment
= make_environ ();
133 init_environ (inf
->environment
);
135 inferior_alloc_data (inf
);
137 observer_notify_inferior_added (inf
);
140 inferior_appeared (inf
, pid
);
146 add_inferior (int pid
)
148 struct inferior
*inf
= add_inferior_silent (pid
);
150 if (print_inferior_events
)
151 printf_unfiltered (_("[New inferior %d]\n"), pid
);
156 struct delete_thread_of_inferior_arg
163 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
165 struct delete_thread_of_inferior_arg
*arg
166 = (struct delete_thread_of_inferior_arg
*) data
;
168 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
171 delete_thread_silent (tp
->ptid
);
173 delete_thread (tp
->ptid
);
180 delete_inferior (struct inferior
*todel
)
182 struct inferior
*inf
, *infprev
;
183 struct delete_thread_of_inferior_arg arg
;
187 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
197 iterate_over_threads (delete_thread_of_inferior
, &arg
);
200 infprev
->next
= inf
->next
;
202 inferior_list
= inf
->next
;
204 observer_notify_inferior_removed (inf
);
206 /* If this program space is rendered useless, remove it. */
207 if (program_space_empty_p (inf
->pspace
))
208 delete_program_space (inf
->pspace
);
213 /* If SILENT then be quiet -- don't announce a inferior exit, or the
214 exit of its threads. */
217 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
219 struct inferior
*inf
;
220 struct delete_thread_of_inferior_arg arg
;
222 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
232 iterate_over_threads (delete_thread_of_inferior
, &arg
);
234 observer_notify_inferior_exit (inf
);
241 if (inf
->vfork_parent
!= NULL
)
243 inf
->vfork_parent
->vfork_child
= NULL
;
244 inf
->vfork_parent
= NULL
;
246 if (inf
->vfork_child
!= NULL
)
248 inf
->vfork_child
->vfork_parent
= NULL
;
249 inf
->vfork_child
= NULL
;
252 inf
->pending_detach
= 0;
256 exit_inferior (int pid
)
258 struct inferior
*inf
= find_inferior_pid (pid
);
260 exit_inferior_1 (inf
, 0);
262 if (print_inferior_events
)
263 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
267 exit_inferior_silent (int pid
)
269 struct inferior
*inf
= find_inferior_pid (pid
);
271 exit_inferior_1 (inf
, 1);
275 exit_inferior_num_silent (int num
)
277 struct inferior
*inf
= find_inferior_id (num
);
279 exit_inferior_1 (inf
, 1);
283 detach_inferior (int pid
)
285 struct inferior
*inf
= find_inferior_pid (pid
);
287 exit_inferior_1 (inf
, 0);
289 if (print_inferior_events
)
290 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
294 inferior_appeared (struct inferior
*inf
, int pid
)
297 inf
->has_exit_code
= 0;
300 observer_notify_inferior_appeared (inf
);
304 discard_all_inferiors (void)
306 struct inferior
*inf
;
308 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
311 exit_inferior_silent (inf
->pid
);
316 find_inferior_id (int num
)
318 struct inferior
*inf
;
320 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
328 find_inferior_pid (int pid
)
330 struct inferior
*inf
;
332 /* Looking for inferior pid == 0 is always wrong, and indicative of
333 a bug somewhere else. There may be more than one with pid == 0,
335 gdb_assert (pid
!= 0);
337 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
347 find_inferior_ptid (ptid_t ptid
)
349 return find_inferior_pid (ptid_get_pid (ptid
));
352 /* See inferior.h. */
355 find_inferior_for_program_space (struct program_space
*pspace
)
357 struct inferior
*inf
= current_inferior ();
359 if (inf
->pspace
== pspace
)
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
)
445 /* Return the number of live inferiors. We account for the case
446 where an inferior might have a non-zero pid but no threads, as
447 in the middle of a 'mourn' operation. */
450 number_of_live_inferiors (void)
452 struct inferior
*inf
;
455 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
458 struct thread_info
*tp
;
460 ALL_NON_EXITED_THREADS (tp
)
461 if (tp
&& ptid_get_pid (tp
->ptid
) == inf
->pid
)
462 if (target_has_execution_1 (tp
->ptid
))
464 /* Found a live thread in this inferior, go to the next
474 /* Return true if there is at least one live inferior. */
477 have_live_inferiors (void)
479 return number_of_live_inferiors () > 0;
482 /* Prune away any unused inferiors, and then prune away no longer used
486 prune_inferiors (void)
488 struct inferior
*ss
, **ss_link
;
489 struct inferior
*current
= current_inferior ();
492 ss_link
= &inferior_list
;
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 struct cleanup
*chain2
;
592 if (!number_is_in_list (requested_inferiors
, inf
->num
))
595 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
597 if (inf
== current_inferior ())
598 uiout
->field_string ("current", "*");
600 uiout
->field_skip ("current");
602 uiout
->field_int ("number", inf
->num
);
604 uiout
->field_string ("target-id", inferior_pid_to_str (inf
->pid
));
606 if (inf
->pspace
->pspace_exec_filename
!= NULL
)
607 uiout
->field_string ("exec", inf
->pspace
->pspace_exec_filename
);
609 uiout
->field_skip ("exec");
611 /* Print extra info that isn't really fit to always present in
612 tabular form. Currently we print the vfork parent/child
613 relationships, if any. */
614 if (inf
->vfork_parent
)
616 uiout
->text (_("\n\tis vfork child of inferior "));
617 uiout
->field_int ("vfork-parent", inf
->vfork_parent
->num
);
619 if (inf
->vfork_child
)
621 uiout
->text (_("\n\tis vfork parent of inferior "));
622 uiout
->field_int ("vfork-child", inf
->vfork_child
->num
);
626 do_cleanups (chain2
);
629 do_cleanups (old_chain
);
633 detach_inferior_command (char *args
, int from_tty
)
635 struct thread_info
*tp
;
638 error (_("Requires argument (inferior id(s) to detach)"));
640 number_or_range_parser
parser (args
);
641 while (!parser
.finished ())
643 int num
= parser
.get_number ();
645 if (!valid_gdb_inferior_id (num
))
647 warning (_("Inferior ID %d not known."), num
);
651 int pid
= gdb_inferior_id_to_pid (num
);
654 warning (_("Inferior ID %d is not running."), num
);
658 tp
= any_thread_of_process (pid
);
661 warning (_("Inferior ID %d has no threads."), num
);
665 switch_to_thread (tp
->ptid
);
667 detach_command (NULL
, from_tty
);
672 kill_inferior_command (char *args
, int from_tty
)
674 struct thread_info
*tp
;
677 error (_("Requires argument (inferior id(s) to kill)"));
679 number_or_range_parser
parser (args
);
680 while (!parser
.finished ())
682 int num
= parser
.get_number ();
684 if (!valid_gdb_inferior_id (num
))
686 warning (_("Inferior ID %d not known."), num
);
690 int pid
= gdb_inferior_id_to_pid (num
);
693 warning (_("Inferior ID %d is not running."), num
);
697 tp
= any_thread_of_process (pid
);
700 warning (_("Inferior ID %d has no threads."), num
);
704 switch_to_thread (tp
->ptid
);
709 bfd_cache_close_all ();
713 inferior_command (char *args
, int from_tty
)
715 struct inferior
*inf
;
718 num
= parse_and_eval_long (args
);
720 inf
= find_inferior_id (num
);
722 error (_("Inferior ID %d not known."), num
);
726 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
728 struct thread_info
*tp
;
730 tp
= any_thread_of_process (inf
->pid
);
732 error (_("Inferior has no threads."));
734 switch_to_thread (tp
->ptid
);
737 observer_notify_user_selected_context_changed
738 (USER_SELECTED_INFERIOR
739 | USER_SELECTED_THREAD
740 | USER_SELECTED_FRAME
);
744 set_current_inferior (inf
);
745 switch_to_thread (null_ptid
);
746 set_current_program_space (inf
->pspace
);
748 observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR
);
752 /* Print information about currently known inferiors. */
755 info_inferiors_command (char *args
, int from_tty
)
757 print_inferior (current_uiout
, args
);
760 /* remove-inferior ID */
763 remove_inferior_command (char *args
, int from_tty
)
765 if (args
== NULL
|| *args
== '\0')
766 error (_("Requires an argument (inferior id(s) to remove)"));
768 number_or_range_parser
parser (args
);
769 while (!parser
.finished ())
771 int num
= parser
.get_number ();
772 struct inferior
*inf
= find_inferior_id (num
);
776 warning (_("Inferior ID %d not known."), num
);
780 if (inf
== current_inferior ())
782 warning (_("Can not remove current inferior %d."), num
);
788 warning (_("Can not remove active inferior %d."), num
);
792 delete_inferior (inf
);
797 add_inferior_with_spaces (void)
799 struct address_space
*aspace
;
800 struct program_space
*pspace
;
801 struct inferior
*inf
;
802 struct gdbarch_info info
;
804 /* If all inferiors share an address space on this system, this
805 doesn't really return a new address space; otherwise, it
807 aspace
= maybe_new_address_space ();
808 pspace
= add_program_space (aspace
);
809 inf
= add_inferior (0);
810 inf
->pspace
= pspace
;
811 inf
->aspace
= pspace
->aspace
;
813 /* Setup the inferior's initial arch, based on information obtained
814 from the global "set ..." options. */
815 gdbarch_info_init (&info
);
816 inf
->gdbarch
= gdbarch_find_by_info (info
);
817 /* The "set ..." options reject invalid settings, so we should
818 always have a valid arch by now. */
819 gdb_assert (inf
->gdbarch
!= NULL
);
824 /* add-inferior [-copies N] [-exec FILENAME] */
827 add_inferior_command (char *args
, int from_tty
)
832 symfile_add_flags add_flags
= 0;
833 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
836 add_flags
|= SYMFILE_VERBOSE
;
840 argv
= gdb_buildargv (args
);
841 make_cleanup_freeargv (argv
);
843 for (; *argv
!= NULL
; argv
++)
847 if (strcmp (*argv
, "-copies") == 0)
851 error (_("No argument to -copies"));
852 copies
= parse_and_eval_long (*argv
);
854 else if (strcmp (*argv
, "-exec") == 0)
858 error (_("No argument to -exec"));
859 exec
= tilde_expand (*argv
);
860 make_cleanup (xfree
, exec
);
864 error (_("Invalid argument"));
868 save_current_space_and_thread ();
870 for (i
= 0; i
< copies
; ++i
)
872 struct inferior
*inf
= add_inferior_with_spaces ();
874 printf_filtered (_("Added inferior %d\n"), inf
->num
);
878 /* Switch over temporarily, while reading executable and
880 set_current_program_space (inf
->pspace
);
881 set_current_inferior (inf
);
882 switch_to_thread (null_ptid
);
884 exec_file_attach (exec
, from_tty
);
885 symbol_file_add_main (exec
, add_flags
);
889 do_cleanups (old_chain
);
892 /* clone-inferior [-copies N] [ID] */
895 clone_inferior_command (char *args
, int from_tty
)
899 struct inferior
*orginf
= NULL
;
900 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
904 argv
= gdb_buildargv (args
);
905 make_cleanup_freeargv (argv
);
907 for (; *argv
!= NULL
; argv
++)
911 if (strcmp (*argv
, "-copies") == 0)
915 error (_("No argument to -copies"));
916 copies
= parse_and_eval_long (*argv
);
919 error (_("Invalid copies number"));
928 /* The first non-option (-) argument specified the
930 num
= parse_and_eval_long (*argv
);
931 orginf
= find_inferior_id (num
);
934 error (_("Inferior ID %d not known."), num
);
938 error (_("Invalid argument"));
943 /* If no inferior id was specified, then the user wants to clone the
946 orginf
= current_inferior ();
948 save_current_space_and_thread ();
950 for (i
= 0; i
< copies
; ++i
)
952 struct address_space
*aspace
;
953 struct program_space
*pspace
;
954 struct inferior
*inf
;
956 /* If all inferiors share an address space on this system, this
957 doesn't really return a new address space; otherwise, it
959 aspace
= maybe_new_address_space ();
960 pspace
= add_program_space (aspace
);
961 inf
= add_inferior (0);
962 inf
->pspace
= pspace
;
963 inf
->aspace
= pspace
->aspace
;
964 inf
->gdbarch
= orginf
->gdbarch
;
966 /* If the original inferior had a user specified target
967 description, make the clone use it too. */
968 if (target_desc_info_from_user_p (inf
->tdesc_info
))
969 copy_inferior_target_desc_info (inf
, orginf
);
971 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
973 set_current_inferior (inf
);
974 switch_to_thread (null_ptid
);
975 clone_program_space (pspace
, orginf
->pspace
);
978 do_cleanups (old_chain
);
981 /* Print notices when new inferiors are created and die. */
983 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
984 struct cmd_list_element
*c
, const char *value
)
986 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
989 /* Return a new value for the selected inferior's id. */
991 static struct value
*
992 inferior_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
995 struct inferior
*inf
= current_inferior ();
997 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, inf
->num
);
1000 /* Implementation of `$_inferior' variable. */
1002 static const struct internalvar_funcs inferior_funcs
=
1004 inferior_id_make_value
,
1012 initialize_inferiors (void)
1014 struct cmd_list_element
*c
= NULL
;
1016 /* There's always one inferior. Note that this function isn't an
1017 automatic _initialize_foo function, since other _initialize_foo
1018 routines may need to install their per-inferior data keys. We
1019 can only allocate an inferior when all those modules have done
1020 that. Do this after initialize_progspace, due to the
1021 current_program_space reference. */
1022 current_inferior_
= add_inferior (0);
1023 current_inferior_
->pspace
= current_program_space
;
1024 current_inferior_
->aspace
= current_program_space
->aspace
;
1025 /* The architecture will be initialized shortly, by
1026 initialize_current_architecture. */
1028 add_info ("inferiors", info_inferiors_command
,
1029 _("IDs of specified inferiors (all inferiors if no argument)."));
1031 c
= add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1032 Add a new inferior.\n\
1033 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1034 N is the optional number of inferiors to add, default is 1.\n\
1035 FILENAME is the file name of the executable to use\n\
1036 as main program."));
1037 set_cmd_completer (c
, filename_completer
);
1039 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1040 Remove inferior ID (or list of IDs).\n\
1041 Usage: remove-inferiors ID..."));
1043 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1044 Clone inferior ID.\n\
1045 Usage: clone-inferior [-copies <N>] [ID]\n\
1046 Add N copies of inferior ID. The new inferior has the same\n\
1047 executable loaded as the copied inferior. If -copies is not specified,\n\
1048 adds 1 copy. If ID is not specified, it is the current inferior\n\
1051 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1052 Detach from inferior ID (or list of IDS)."),
1055 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1056 Kill inferior ID (or list of IDs)."),
1059 add_cmd ("inferior", class_run
, inferior_command
, _("\
1060 Use this command to switch between inferiors.\n\
1061 The new inferior ID must be currently known."),
1064 add_setshow_boolean_cmd ("inferior-events", no_class
,
1065 &print_inferior_events
, _("\
1066 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1067 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1069 show_print_inferior_events
,
1070 &setprintlist
, &showprintlist
);
1072 create_internalvar_type_lazy ("_inferior", &inferior_funcs
, NULL
);