1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2016 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 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
= XNEW (struct inferior
);
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
175 = (struct delete_thread_of_inferior_arg
*) data
;
177 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
180 delete_thread_silent (tp
->ptid
);
182 delete_thread (tp
->ptid
);
189 delete_inferior (struct inferior
*todel
)
191 struct inferior
*inf
, *infprev
;
192 struct delete_thread_of_inferior_arg arg
;
196 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
206 iterate_over_threads (delete_thread_of_inferior
, &arg
);
209 infprev
->next
= inf
->next
;
211 inferior_list
= inf
->next
;
213 observer_notify_inferior_removed (inf
);
215 /* If this program space is rendered useless, remove it. */
216 if (program_space_empty_p (inf
->pspace
))
217 delete_program_space (inf
->pspace
);
222 /* If SILENT then be quiet -- don't announce a inferior exit, or the
223 exit of its threads. */
226 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
228 struct inferior
*inf
;
229 struct delete_thread_of_inferior_arg arg
;
231 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
241 iterate_over_threads (delete_thread_of_inferior
, &arg
);
243 /* Notify the observers before removing the inferior from the list,
244 so that the observers have a chance to look it up. */
245 observer_notify_inferior_exit (inf
);
249 if (inf
->vfork_parent
!= NULL
)
251 inf
->vfork_parent
->vfork_child
= NULL
;
252 inf
->vfork_parent
= NULL
;
254 if (inf
->vfork_child
!= NULL
)
256 inf
->vfork_child
->vfork_parent
= NULL
;
257 inf
->vfork_child
= NULL
;
260 inf
->pending_detach
= 0;
264 exit_inferior (int pid
)
266 struct inferior
*inf
= find_inferior_pid (pid
);
268 exit_inferior_1 (inf
, 0);
270 if (print_inferior_events
)
271 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
275 exit_inferior_silent (int pid
)
277 struct inferior
*inf
= find_inferior_pid (pid
);
279 exit_inferior_1 (inf
, 1);
283 exit_inferior_num_silent (int num
)
285 struct inferior
*inf
= find_inferior_id (num
);
287 exit_inferior_1 (inf
, 1);
291 detach_inferior (int pid
)
293 struct inferior
*inf
= find_inferior_pid (pid
);
295 exit_inferior_1 (inf
, 0);
297 if (print_inferior_events
)
298 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
302 inferior_appeared (struct inferior
*inf
, int pid
)
305 inf
->has_exit_code
= 0;
308 observer_notify_inferior_appeared (inf
);
312 discard_all_inferiors (void)
314 struct inferior
*inf
;
316 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
319 exit_inferior_silent (inf
->pid
);
324 find_inferior_id (int num
)
326 struct inferior
*inf
;
328 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
336 find_inferior_pid (int pid
)
338 struct inferior
*inf
;
340 /* Looking for inferior pid == 0 is always wrong, and indicative of
341 a bug somewhere else. There may be more than one with pid == 0,
343 gdb_assert (pid
!= 0);
345 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
355 find_inferior_ptid (ptid_t ptid
)
357 return find_inferior_pid (ptid_get_pid (ptid
));
360 /* See inferior.h. */
363 find_inferior_for_program_space (struct program_space
*pspace
)
365 struct inferior
*inf
= current_inferior ();
367 if (inf
->pspace
== pspace
)
370 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
372 if (inf
->pspace
== pspace
)
380 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
383 struct inferior
*inf
, *infnext
;
385 for (inf
= inferior_list
; inf
; inf
= infnext
)
388 if ((*callback
) (inf
, data
))
396 valid_gdb_inferior_id (int num
)
398 struct inferior
*inf
;
400 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
408 pid_to_gdb_inferior_id (int pid
)
410 struct inferior
*inf
;
412 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
420 gdb_inferior_id_to_pid (int num
)
422 struct inferior
*inferior
= find_inferior_id (num
);
424 return inferior
->pid
;
430 in_inferior_list (int pid
)
432 struct inferior
*inf
;
434 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
442 have_inferiors (void)
444 struct inferior
*inf
;
446 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
453 /* Return the number of live inferiors. We account for the case
454 where an inferior might have a non-zero pid but no threads, as
455 in the middle of a 'mourn' operation. */
458 number_of_live_inferiors (void)
460 struct inferior
*inf
;
463 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
466 struct thread_info
*tp
;
468 ALL_NON_EXITED_THREADS (tp
)
469 if (tp
&& ptid_get_pid (tp
->ptid
) == inf
->pid
)
470 if (target_has_execution_1 (tp
->ptid
))
472 /* Found a live thread in this inferior, go to the next
482 /* Return true if there is at least one live inferior. */
485 have_live_inferiors (void)
487 return number_of_live_inferiors () > 0;
490 /* Prune away any unused inferiors, and then prune away no longer used
494 prune_inferiors (void)
496 struct inferior
*ss
, **ss_link
;
497 struct inferior
*current
= current_inferior ();
500 ss_link
= &inferior_list
;
513 delete_inferior (ss
);
518 /* Simply returns the count of inferiors. */
521 number_of_inferiors (void)
523 struct inferior
*inf
;
526 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
532 /* Converts an inferior process id to a string. Like
533 target_pid_to_str, but special cases the null process. */
536 inferior_pid_to_str (int pid
)
539 return target_pid_to_str (pid_to_ptid (pid
));
544 /* Prints the list of inferiors and their details on UIOUT. This is a
545 version of 'info_inferior_command' suitable for use from MI.
547 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
548 inferiors that should be printed. Otherwise, all inferiors are
552 print_inferior (struct ui_out
*uiout
, char *requested_inferiors
)
554 struct inferior
*inf
;
555 struct cleanup
*old_chain
;
558 /* Compute number of inferiors we will print. */
559 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
561 if (!number_is_in_list (requested_inferiors
, inf
->num
))
569 ui_out_message (uiout
, 0, "No inferiors.\n");
573 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
575 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
576 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
577 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
578 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
580 ui_out_table_body (uiout
);
581 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
583 struct cleanup
*chain2
;
585 if (!number_is_in_list (requested_inferiors
, inf
->num
))
588 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
590 if (inf
== current_inferior ())
591 ui_out_field_string (uiout
, "current", "*");
593 ui_out_field_skip (uiout
, "current");
595 ui_out_field_int (uiout
, "number", inf
->num
);
597 ui_out_field_string (uiout
, "target-id",
598 inferior_pid_to_str (inf
->pid
));
600 if (inf
->pspace
->pspace_exec_filename
!= NULL
)
601 ui_out_field_string (uiout
, "exec", inf
->pspace
->pspace_exec_filename
);
603 ui_out_field_skip (uiout
, "exec");
605 /* Print extra info that isn't really fit to always present in
606 tabular form. Currently we print the vfork parent/child
607 relationships, if any. */
608 if (inf
->vfork_parent
)
610 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
611 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
613 if (inf
->vfork_child
)
615 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
616 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
619 ui_out_text (uiout
, "\n");
620 do_cleanups (chain2
);
623 do_cleanups (old_chain
);
627 detach_inferior_command (char *args
, int from_tty
)
630 struct thread_info
*tp
;
631 struct get_number_or_range_state state
;
634 error (_("Requires argument (inferior id(s) to detach)"));
636 init_number_or_range (&state
, args
);
637 while (!state
.finished
)
639 num
= get_number_or_range (&state
);
641 if (!valid_gdb_inferior_id (num
))
643 warning (_("Inferior ID %d not known."), num
);
647 pid
= gdb_inferior_id_to_pid (num
);
650 warning (_("Inferior ID %d is not running."), num
);
654 tp
= any_thread_of_process (pid
);
657 warning (_("Inferior ID %d has no threads."), num
);
661 switch_to_thread (tp
->ptid
);
663 detach_command (NULL
, from_tty
);
668 kill_inferior_command (char *args
, int from_tty
)
671 struct thread_info
*tp
;
672 struct get_number_or_range_state state
;
675 error (_("Requires argument (inferior id(s) to kill)"));
677 init_number_or_range (&state
, args
);
678 while (!state
.finished
)
680 num
= get_number_or_range (&state
);
682 if (!valid_gdb_inferior_id (num
))
684 warning (_("Inferior ID %d not known."), num
);
688 pid
= gdb_inferior_id_to_pid (num
);
691 warning (_("Inferior ID %d is not running."), num
);
695 tp
= any_thread_of_process (pid
);
698 warning (_("Inferior ID %d has no threads."), num
);
702 switch_to_thread (tp
->ptid
);
707 bfd_cache_close_all ();
711 inferior_command (char *args
, int from_tty
)
713 struct inferior
*inf
;
716 num
= parse_and_eval_long (args
);
718 inf
= find_inferior_id (num
);
720 error (_("Inferior ID %d not known."), num
);
722 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
724 inferior_pid_to_str (inf
->pid
),
725 (inf
->pspace
->pspace_exec_filename
!= NULL
726 ? inf
->pspace
->pspace_exec_filename
731 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
733 struct thread_info
*tp
;
735 tp
= any_thread_of_process (inf
->pid
);
737 error (_("Inferior has no threads."));
739 switch_to_thread (tp
->ptid
);
742 printf_filtered (_("[Switching to thread %d (%s)] "),
743 pid_to_thread_id (inferior_ptid
),
744 target_pid_to_str (inferior_ptid
));
748 struct inferior
*inf
;
750 inf
= find_inferior_id (num
);
751 set_current_inferior (inf
);
752 switch_to_thread (null_ptid
);
753 set_current_program_space (inf
->pspace
);
756 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
757 ui_out_text (current_uiout
, "(running)\n");
758 else if (inf
->pid
!= 0)
760 ui_out_text (current_uiout
, "\n");
761 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
765 /* Print information about currently known inferiors. */
768 info_inferiors_command (char *args
, int from_tty
)
770 print_inferior (current_uiout
, args
);
773 /* remove-inferior ID */
776 remove_inferior_command (char *args
, int from_tty
)
779 struct inferior
*inf
;
780 struct get_number_or_range_state state
;
782 if (args
== NULL
|| *args
== '\0')
783 error (_("Requires an argument (inferior id(s) to remove)"));
785 init_number_or_range (&state
, args
);
786 while (!state
.finished
)
788 num
= get_number_or_range (&state
);
789 inf
= find_inferior_id (num
);
793 warning (_("Inferior ID %d not known."), num
);
797 if (inf
== current_inferior ())
799 warning (_("Can not remove current symbol inferior %d."), num
);
805 warning (_("Can not remove active inferior %d."), num
);
809 delete_inferior (inf
);
814 add_inferior_with_spaces (void)
816 struct address_space
*aspace
;
817 struct program_space
*pspace
;
818 struct inferior
*inf
;
819 struct gdbarch_info info
;
821 /* If all inferiors share an address space on this system, this
822 doesn't really return a new address space; otherwise, it
824 aspace
= maybe_new_address_space ();
825 pspace
= add_program_space (aspace
);
826 inf
= add_inferior (0);
827 inf
->pspace
= pspace
;
828 inf
->aspace
= pspace
->aspace
;
830 /* Setup the inferior's initial arch, based on information obtained
831 from the global "set ..." options. */
832 gdbarch_info_init (&info
);
833 inf
->gdbarch
= gdbarch_find_by_info (info
);
834 /* The "set ..." options reject invalid settings, so we should
835 always have a valid arch by now. */
836 gdb_assert (inf
->gdbarch
!= NULL
);
841 /* add-inferior [-copies N] [-exec FILENAME] */
844 add_inferior_command (char *args
, int from_tty
)
849 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
853 argv
= gdb_buildargv (args
);
854 make_cleanup_freeargv (argv
);
856 for (; *argv
!= NULL
; argv
++)
860 if (strcmp (*argv
, "-copies") == 0)
864 error (_("No argument to -copies"));
865 copies
= parse_and_eval_long (*argv
);
867 else if (strcmp (*argv
, "-exec") == 0)
871 error (_("No argument to -exec"));
872 exec
= tilde_expand (*argv
);
873 make_cleanup (xfree
, exec
);
877 error (_("Invalid argument"));
881 save_current_space_and_thread ();
883 for (i
= 0; i
< copies
; ++i
)
885 struct inferior
*inf
= add_inferior_with_spaces ();
887 printf_filtered (_("Added inferior %d\n"), inf
->num
);
891 /* Switch over temporarily, while reading executable and
893 set_current_program_space (inf
->pspace
);
894 set_current_inferior (inf
);
895 switch_to_thread (null_ptid
);
897 exec_file_attach (exec
, from_tty
);
898 symbol_file_add_main (exec
, from_tty
);
902 do_cleanups (old_chain
);
905 /* clone-inferior [-copies N] [ID] */
908 clone_inferior_command (char *args
, int from_tty
)
912 struct inferior
*orginf
= NULL
;
913 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
917 argv
= gdb_buildargv (args
);
918 make_cleanup_freeargv (argv
);
920 for (; *argv
!= NULL
; argv
++)
924 if (strcmp (*argv
, "-copies") == 0)
928 error (_("No argument to -copies"));
929 copies
= parse_and_eval_long (*argv
);
932 error (_("Invalid copies number"));
941 /* The first non-option (-) argument specified the
943 num
= parse_and_eval_long (*argv
);
944 orginf
= find_inferior_id (num
);
947 error (_("Inferior ID %d not known."), num
);
951 error (_("Invalid argument"));
956 /* If no inferior id was specified, then the user wants to clone the
959 orginf
= current_inferior ();
961 save_current_space_and_thread ();
963 for (i
= 0; i
< copies
; ++i
)
965 struct address_space
*aspace
;
966 struct program_space
*pspace
;
967 struct inferior
*inf
;
969 /* If all inferiors share an address space on this system, this
970 doesn't really return a new address space; otherwise, it
972 aspace
= maybe_new_address_space ();
973 pspace
= add_program_space (aspace
);
974 inf
= add_inferior (0);
975 inf
->pspace
= pspace
;
976 inf
->aspace
= pspace
->aspace
;
977 inf
->gdbarch
= orginf
->gdbarch
;
979 /* If the original inferior had a user specified target
980 description, make the clone use it too. */
981 if (target_desc_info_from_user_p (inf
->tdesc_info
))
982 copy_inferior_target_desc_info (inf
, orginf
);
984 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
986 set_current_inferior (inf
);
987 switch_to_thread (null_ptid
);
988 clone_program_space (pspace
, orginf
->pspace
);
991 do_cleanups (old_chain
);
994 /* Print notices when new inferiors are created and die. */
996 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
997 struct cmd_list_element
*c
, const char *value
)
999 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
1005 initialize_inferiors (void)
1007 struct cmd_list_element
*c
= NULL
;
1009 /* There's always one inferior. Note that this function isn't an
1010 automatic _initialize_foo function, since other _initialize_foo
1011 routines may need to install their per-inferior data keys. We
1012 can only allocate an inferior when all those modules have done
1013 that. Do this after initialize_progspace, due to the
1014 current_program_space reference. */
1015 current_inferior_
= add_inferior (0);
1016 current_inferior_
->pspace
= current_program_space
;
1017 current_inferior_
->aspace
= current_program_space
->aspace
;
1018 /* The architecture will be initialized shortly, by
1019 initialize_current_architecture. */
1021 add_info ("inferiors", info_inferiors_command
,
1022 _("IDs of specified inferiors (all inferiors if no argument)."));
1024 c
= add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1025 Add a new inferior.\n\
1026 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1027 N is the optional number of inferiors to add, default is 1.\n\
1028 FILENAME is the file name of the executable to use\n\
1029 as main program."));
1030 set_cmd_completer (c
, filename_completer
);
1032 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1033 Remove inferior ID (or list of IDs).\n\
1034 Usage: remove-inferiors ID..."));
1036 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1037 Clone inferior ID.\n\
1038 Usage: clone-inferior [-copies <N>] [ID]\n\
1039 Add N copies of inferior ID. The new inferior has the same\n\
1040 executable loaded as the copied inferior. If -copies is not specified,\n\
1041 adds 1 copy. If ID is not specified, it is the current inferior\n\
1044 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1045 Detach from inferior ID (or list of IDS)."),
1048 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1049 Kill inferior ID (or list of IDs)."),
1052 add_cmd ("inferior", class_run
, inferior_command
, _("\
1053 Use this command to switch between inferiors.\n\
1054 The new inferior ID must be currently known."),
1057 add_setshow_boolean_cmd ("inferior-events", no_class
,
1058 &print_inferior_events
, _("\
1059 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1060 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1062 show_print_inferior_events
,
1063 &setprintlist
, &showprintlist
);