1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2015 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
);
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
= data
;
176 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
179 delete_thread_silent (tp
->ptid
);
181 delete_thread (tp
->ptid
);
188 delete_inferior (struct inferior
*todel
)
190 struct inferior
*inf
, *infprev
;
191 struct delete_thread_of_inferior_arg arg
;
195 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
205 iterate_over_threads (delete_thread_of_inferior
, &arg
);
208 infprev
->next
= inf
->next
;
210 inferior_list
= inf
->next
;
212 observer_notify_inferior_removed (inf
);
214 /* If this program space is rendered useless, remove it. */
215 if (program_space_empty_p (inf
->pspace
))
216 delete_program_space (inf
->pspace
);
221 /* If SILENT then be quiet -- don't announce a inferior exit, or the
222 exit of its threads. */
225 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
227 struct inferior
*inf
;
228 struct delete_thread_of_inferior_arg arg
;
230 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
240 iterate_over_threads (delete_thread_of_inferior
, &arg
);
242 /* Notify the observers before removing the inferior from the list,
243 so that the observers have a chance to look it up. */
244 observer_notify_inferior_exit (inf
);
248 if (inf
->vfork_parent
!= NULL
)
250 inf
->vfork_parent
->vfork_child
= NULL
;
251 inf
->vfork_parent
= NULL
;
253 if (inf
->vfork_child
!= NULL
)
255 inf
->vfork_child
->vfork_parent
= NULL
;
256 inf
->vfork_child
= NULL
;
259 inf
->pending_detach
= 0;
263 exit_inferior (int pid
)
265 struct inferior
*inf
= find_inferior_pid (pid
);
267 exit_inferior_1 (inf
, 0);
269 if (print_inferior_events
)
270 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
274 exit_inferior_silent (int pid
)
276 struct inferior
*inf
= find_inferior_pid (pid
);
278 exit_inferior_1 (inf
, 1);
282 exit_inferior_num_silent (int num
)
284 struct inferior
*inf
= find_inferior_id (num
);
286 exit_inferior_1 (inf
, 1);
290 detach_inferior (int pid
)
292 struct inferior
*inf
= find_inferior_pid (pid
);
294 exit_inferior_1 (inf
, 0);
296 if (print_inferior_events
)
297 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
301 inferior_appeared (struct inferior
*inf
, int pid
)
304 inf
->has_exit_code
= 0;
307 observer_notify_inferior_appeared (inf
);
311 discard_all_inferiors (void)
313 struct inferior
*inf
;
315 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
318 exit_inferior_silent (inf
->pid
);
323 find_inferior_id (int num
)
325 struct inferior
*inf
;
327 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
335 find_inferior_pid (int pid
)
337 struct inferior
*inf
;
339 /* Looking for inferior pid == 0 is always wrong, and indicative of
340 a bug somewhere else. There may be more than one with pid == 0,
342 gdb_assert (pid
!= 0);
344 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
354 find_inferior_ptid (ptid_t ptid
)
356 return find_inferior_pid (ptid_get_pid (ptid
));
359 /* See inferior.h. */
362 find_inferior_for_program_space (struct program_space
*pspace
)
364 struct inferior
*inf
= current_inferior ();
366 if (inf
->pspace
== pspace
)
369 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
371 if (inf
->pspace
== pspace
)
379 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
382 struct inferior
*inf
, *infnext
;
384 for (inf
= inferior_list
; inf
; inf
= infnext
)
387 if ((*callback
) (inf
, data
))
395 valid_gdb_inferior_id (int num
)
397 struct inferior
*inf
;
399 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
407 pid_to_gdb_inferior_id (int pid
)
409 struct inferior
*inf
;
411 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
419 gdb_inferior_id_to_pid (int num
)
421 struct inferior
*inferior
= find_inferior_id (num
);
423 return inferior
->pid
;
429 in_inferior_list (int pid
)
431 struct inferior
*inf
;
433 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
441 have_inferiors (void)
443 struct inferior
*inf
;
445 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
453 have_live_inferiors (void)
455 struct inferior
*inf
;
457 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
460 struct thread_info
*tp
;
462 tp
= any_thread_of_process (inf
->pid
);
463 if (tp
&& target_has_execution_1 (tp
->ptid
))
470 /* Prune away any unused inferiors, and then prune away no longer used
474 prune_inferiors (void)
476 struct inferior
*ss
, **ss_link
;
477 struct inferior
*current
= current_inferior ();
480 ss_link
= &inferior_list
;
493 delete_inferior (ss
);
498 /* Simply returns the count of inferiors. */
501 number_of_inferiors (void)
503 struct inferior
*inf
;
506 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
512 /* Converts an inferior process id to a string. Like
513 target_pid_to_str, but special cases the null process. */
516 inferior_pid_to_str (int pid
)
519 return target_pid_to_str (pid_to_ptid (pid
));
524 /* Prints the list of inferiors and their details on UIOUT. This is a
525 version of 'info_inferior_command' suitable for use from MI.
527 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
528 inferiors that should be printed. Otherwise, all inferiors are
532 print_inferior (struct ui_out
*uiout
, char *requested_inferiors
)
534 struct inferior
*inf
;
535 struct cleanup
*old_chain
;
538 /* Compute number of inferiors we will print. */
539 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
541 if (!number_is_in_list (requested_inferiors
, inf
->num
))
549 ui_out_message (uiout
, 0, "No inferiors.\n");
553 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
555 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
556 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
557 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
558 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
560 ui_out_table_body (uiout
);
561 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
563 struct cleanup
*chain2
;
565 if (!number_is_in_list (requested_inferiors
, inf
->num
))
568 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
570 if (inf
== current_inferior ())
571 ui_out_field_string (uiout
, "current", "*");
573 ui_out_field_skip (uiout
, "current");
575 ui_out_field_int (uiout
, "number", inf
->num
);
577 ui_out_field_string (uiout
, "target-id",
578 inferior_pid_to_str (inf
->pid
));
580 if (inf
->pspace
->pspace_exec_filename
!= NULL
)
581 ui_out_field_string (uiout
, "exec", inf
->pspace
->pspace_exec_filename
);
583 ui_out_field_skip (uiout
, "exec");
585 /* Print extra info that isn't really fit to always present in
586 tabular form. Currently we print the vfork parent/child
587 relationships, if any. */
588 if (inf
->vfork_parent
)
590 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
591 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
593 if (inf
->vfork_child
)
595 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
596 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
599 ui_out_text (uiout
, "\n");
600 do_cleanups (chain2
);
603 do_cleanups (old_chain
);
607 detach_inferior_command (char *args
, int from_tty
)
610 struct thread_info
*tp
;
611 struct get_number_or_range_state state
;
614 error (_("Requires argument (inferior id(s) to detach)"));
616 init_number_or_range (&state
, args
);
617 while (!state
.finished
)
619 num
= get_number_or_range (&state
);
621 if (!valid_gdb_inferior_id (num
))
623 warning (_("Inferior ID %d not known."), num
);
627 pid
= gdb_inferior_id_to_pid (num
);
630 warning (_("Inferior ID %d is not running."), num
);
634 tp
= any_thread_of_process (pid
);
637 warning (_("Inferior ID %d has no threads."), num
);
641 switch_to_thread (tp
->ptid
);
643 detach_command (NULL
, from_tty
);
648 kill_inferior_command (char *args
, int from_tty
)
651 struct thread_info
*tp
;
652 struct get_number_or_range_state state
;
655 error (_("Requires argument (inferior id(s) to kill)"));
657 init_number_or_range (&state
, args
);
658 while (!state
.finished
)
660 num
= get_number_or_range (&state
);
662 if (!valid_gdb_inferior_id (num
))
664 warning (_("Inferior ID %d not known."), num
);
668 pid
= gdb_inferior_id_to_pid (num
);
671 warning (_("Inferior ID %d is not running."), num
);
675 tp
= any_thread_of_process (pid
);
678 warning (_("Inferior ID %d has no threads."), num
);
682 switch_to_thread (tp
->ptid
);
687 bfd_cache_close_all ();
691 inferior_command (char *args
, int from_tty
)
693 struct inferior
*inf
;
696 num
= parse_and_eval_long (args
);
698 inf
= find_inferior_id (num
);
700 error (_("Inferior ID %d not known."), num
);
702 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
704 inferior_pid_to_str (inf
->pid
),
705 (inf
->pspace
->pspace_exec_filename
!= NULL
706 ? inf
->pspace
->pspace_exec_filename
711 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
713 struct thread_info
*tp
;
715 tp
= any_thread_of_process (inf
->pid
);
717 error (_("Inferior has no threads."));
719 switch_to_thread (tp
->ptid
);
722 printf_filtered (_("[Switching to thread %d (%s)] "),
723 pid_to_thread_id (inferior_ptid
),
724 target_pid_to_str (inferior_ptid
));
728 struct inferior
*inf
;
730 inf
= find_inferior_id (num
);
731 set_current_inferior (inf
);
732 switch_to_thread (null_ptid
);
733 set_current_program_space (inf
->pspace
);
736 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
737 ui_out_text (current_uiout
, "(running)\n");
738 else if (inf
->pid
!= 0)
740 ui_out_text (current_uiout
, "\n");
741 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
, 1);
745 /* Print information about currently known inferiors. */
748 info_inferiors_command (char *args
, int from_tty
)
750 print_inferior (current_uiout
, args
);
753 /* remove-inferior ID */
756 remove_inferior_command (char *args
, int from_tty
)
759 struct inferior
*inf
;
760 struct get_number_or_range_state state
;
762 if (args
== NULL
|| *args
== '\0')
763 error (_("Requires an argument (inferior id(s) to remove)"));
765 init_number_or_range (&state
, args
);
766 while (!state
.finished
)
768 num
= get_number_or_range (&state
);
769 inf
= find_inferior_id (num
);
773 warning (_("Inferior ID %d not known."), num
);
777 if (inf
== current_inferior ())
779 warning (_("Can not remove current symbol 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 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
833 argv
= gdb_buildargv (args
);
834 make_cleanup_freeargv (argv
);
836 for (; *argv
!= NULL
; argv
++)
840 if (strcmp (*argv
, "-copies") == 0)
844 error (_("No argument to -copies"));
845 copies
= parse_and_eval_long (*argv
);
847 else if (strcmp (*argv
, "-exec") == 0)
851 error (_("No argument to -exec"));
852 exec
= tilde_expand (*argv
);
853 make_cleanup (xfree
, exec
);
857 error (_("Invalid argument"));
861 save_current_space_and_thread ();
863 for (i
= 0; i
< copies
; ++i
)
865 struct inferior
*inf
= add_inferior_with_spaces ();
867 printf_filtered (_("Added inferior %d\n"), inf
->num
);
871 /* Switch over temporarily, while reading executable and
873 set_current_program_space (inf
->pspace
);
874 set_current_inferior (inf
);
875 switch_to_thread (null_ptid
);
877 exec_file_attach (exec
, from_tty
);
878 symbol_file_add_main (exec
, from_tty
);
882 do_cleanups (old_chain
);
885 /* clone-inferior [-copies N] [ID] */
888 clone_inferior_command (char *args
, int from_tty
)
892 struct inferior
*orginf
= NULL
;
893 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
897 argv
= gdb_buildargv (args
);
898 make_cleanup_freeargv (argv
);
900 for (; *argv
!= NULL
; argv
++)
904 if (strcmp (*argv
, "-copies") == 0)
908 error (_("No argument to -copies"));
909 copies
= parse_and_eval_long (*argv
);
912 error (_("Invalid copies number"));
921 /* The first non-option (-) argument specified the
923 num
= parse_and_eval_long (*argv
);
924 orginf
= find_inferior_id (num
);
927 error (_("Inferior ID %d not known."), num
);
931 error (_("Invalid argument"));
936 /* If no inferior id was specified, then the user wants to clone the
939 orginf
= current_inferior ();
941 save_current_space_and_thread ();
943 for (i
= 0; i
< copies
; ++i
)
945 struct address_space
*aspace
;
946 struct program_space
*pspace
;
947 struct inferior
*inf
;
949 /* If all inferiors share an address space on this system, this
950 doesn't really return a new address space; otherwise, it
952 aspace
= maybe_new_address_space ();
953 pspace
= add_program_space (aspace
);
954 inf
= add_inferior (0);
955 inf
->pspace
= pspace
;
956 inf
->aspace
= pspace
->aspace
;
957 inf
->gdbarch
= orginf
->gdbarch
;
959 /* If the original inferior had a user specified target
960 description, make the clone use it too. */
961 if (target_desc_info_from_user_p (inf
->tdesc_info
))
962 copy_inferior_target_desc_info (inf
, orginf
);
964 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
966 set_current_inferior (inf
);
967 switch_to_thread (null_ptid
);
968 clone_program_space (pspace
, orginf
->pspace
);
971 do_cleanups (old_chain
);
974 /* Print notices when new inferiors are created and die. */
976 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
977 struct cmd_list_element
*c
, const char *value
)
979 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
985 initialize_inferiors (void)
987 struct cmd_list_element
*c
= NULL
;
989 /* There's always one inferior. Note that this function isn't an
990 automatic _initialize_foo function, since other _initialize_foo
991 routines may need to install their per-inferior data keys. We
992 can only allocate an inferior when all those modules have done
993 that. Do this after initialize_progspace, due to the
994 current_program_space reference. */
995 current_inferior_
= add_inferior (0);
996 current_inferior_
->pspace
= current_program_space
;
997 current_inferior_
->aspace
= current_program_space
->aspace
;
998 /* The architecture will be initialized shortly, by
999 initialize_current_architecture. */
1001 add_info ("inferiors", info_inferiors_command
,
1002 _("IDs of specified inferiors (all inferiors if no argument)."));
1004 c
= add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1005 Add a new inferior.\n\
1006 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1007 N is the optional number of inferiors to add, default is 1.\n\
1008 FILENAME is the file name of the executable to use\n\
1009 as main program."));
1010 set_cmd_completer (c
, filename_completer
);
1012 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1013 Remove inferior ID (or list of IDs).\n\
1014 Usage: remove-inferiors ID..."));
1016 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1017 Clone inferior ID.\n\
1018 Usage: clone-inferior [-copies <N>] [ID]\n\
1019 Add N copies of inferior ID. The new inferior has the same\n\
1020 executable loaded as the copied inferior. If -copies is not specified,\n\
1021 adds 1 copy. If ID is not specified, it is the current inferior\n\
1024 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1025 Detach from inferior ID (or list of IDS)."),
1028 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1029 Kill inferior ID (or list of IDs)."),
1032 add_cmd ("inferior", class_run
, inferior_command
, _("\
1033 Use this command to switch between inferiors.\n\
1034 The new inferior ID must be currently known."),
1037 add_setshow_boolean_cmd ("inferior-events", no_class
,
1038 &print_inferior_events
, _("\
1039 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1040 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1042 show_print_inferior_events
,
1043 &setprintlist
, &showprintlist
);