1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2012 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/>. */
26 #include "gdbthread.h"
29 #include "gdbthread.h"
33 #include "cli/cli-utils.h"
34 #include "continuations.h"
36 void _initialize_inferiors (void);
38 static void inferior_alloc_data (struct inferior
*inf
);
39 static void inferior_free_data (struct inferior
*inf
);
41 struct inferior
*inferior_list
= NULL
;
42 static int highest_inferior_num
;
44 /* Print notices on inferior events (attach, detach, etc.), set with
45 `set print inferior-events'. */
46 static int print_inferior_events
= 0;
48 /* The Current Inferior. */
49 static struct inferior
*current_inferior_
= NULL
;
52 current_inferior (void)
54 return current_inferior_
;
58 set_current_inferior (struct inferior
*inf
)
60 /* There's always an inferior. */
61 gdb_assert (inf
!= NULL
);
63 current_inferior_
= inf
;
66 /* A cleanups callback, helper for save_current_program_space
70 restore_inferior (void *arg
)
72 struct inferior
*saved_inferior
= arg
;
74 set_current_inferior (saved_inferior
);
77 /* Save the current program space so that it may be restored by a later
78 call to do_cleanups. Returns the struct cleanup pointer needed for
79 later doing the cleanup. */
82 save_current_inferior (void)
84 struct cleanup
*old_chain
= make_cleanup (restore_inferior
,
91 free_inferior (struct inferior
*inf
)
93 discard_all_inferior_continuations (inf
);
94 inferior_free_data (inf
);
96 xfree (inf
->terminal
);
97 free_environ (inf
->environment
);
103 init_inferior_list (void)
105 struct inferior
*inf
, *infnext
;
107 highest_inferior_num
= 0;
111 for (inf
= inferior_list
; inf
; inf
= infnext
)
117 inferior_list
= NULL
;
121 add_inferior_silent (int pid
)
123 struct inferior
*inf
;
125 inf
= xmalloc (sizeof (*inf
));
126 memset (inf
, 0, sizeof (*inf
));
129 inf
->control
.stop_soon
= NO_STOP_QUIETLY
;
131 inf
->num
= ++highest_inferior_num
;
132 inf
->next
= inferior_list
;
135 inf
->environment
= make_environ ();
136 init_environ (inf
->environment
);
138 inferior_alloc_data (inf
);
140 observer_notify_inferior_added (inf
);
143 inferior_appeared (inf
, pid
);
149 add_inferior (int pid
)
151 struct inferior
*inf
= add_inferior_silent (pid
);
153 if (print_inferior_events
)
154 printf_unfiltered (_("[New inferior %d]\n"), pid
);
159 struct delete_thread_of_inferior_arg
166 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
168 struct delete_thread_of_inferior_arg
*arg
= data
;
170 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
173 delete_thread_silent (tp
->ptid
);
175 delete_thread (tp
->ptid
);
182 delete_threads_of_inferior (int pid
)
184 struct inferior
*inf
;
185 struct delete_thread_of_inferior_arg arg
;
187 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
197 iterate_over_threads (delete_thread_of_inferior
, &arg
);
200 /* If SILENT then be quiet -- don't announce a inferior death, or the
201 exit of its threads. */
204 delete_inferior_1 (struct inferior
*todel
, int silent
)
206 struct inferior
*inf
, *infprev
;
207 struct delete_thread_of_inferior_arg arg
;
211 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
221 iterate_over_threads (delete_thread_of_inferior
, &arg
);
224 infprev
->next
= inf
->next
;
226 inferior_list
= inf
->next
;
228 observer_notify_inferior_removed (inf
);
234 delete_inferior (int pid
)
236 struct inferior
*inf
= find_inferior_pid (pid
);
238 delete_inferior_1 (inf
, 0);
240 if (print_inferior_events
)
241 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
245 delete_inferior_silent (int pid
)
247 struct inferior
*inf
= find_inferior_pid (pid
);
249 delete_inferior_1 (inf
, 1);
253 /* If SILENT then be quiet -- don't announce a inferior exit, or the
254 exit of its threads. */
257 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
259 struct inferior
*inf
;
260 struct delete_thread_of_inferior_arg arg
;
262 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
272 iterate_over_threads (delete_thread_of_inferior
, &arg
);
274 /* Notify the observers before removing the inferior from the list,
275 so that the observers have a chance to look it up. */
276 observer_notify_inferior_exit (inf
);
280 if (inf
->vfork_parent
!= NULL
)
282 inf
->vfork_parent
->vfork_child
= NULL
;
283 inf
->vfork_parent
= NULL
;
286 inf
->has_exit_code
= 0;
291 exit_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 exited]\n"), pid
);
302 exit_inferior_silent (int pid
)
304 struct inferior
*inf
= find_inferior_pid (pid
);
306 exit_inferior_1 (inf
, 1);
310 exit_inferior_num_silent (int num
)
312 struct inferior
*inf
= find_inferior_id (num
);
314 exit_inferior_1 (inf
, 1);
318 detach_inferior (int pid
)
320 struct inferior
*inf
= find_inferior_pid (pid
);
322 exit_inferior_1 (inf
, 1);
324 if (print_inferior_events
)
325 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
329 inferior_appeared (struct inferior
*inf
, int pid
)
333 observer_notify_inferior_appeared (inf
);
337 discard_all_inferiors (void)
339 struct inferior
*inf
;
341 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
344 exit_inferior_silent (inf
->pid
);
349 find_inferior_id (int num
)
351 struct inferior
*inf
;
353 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
361 find_inferior_pid (int pid
)
363 struct inferior
*inf
;
365 /* Looking for inferior pid == 0 is always wrong, and indicative of
366 a bug somewhere else. There may be more than one with pid == 0,
368 gdb_assert (pid
!= 0);
370 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
377 /* Find an inferior bound to PSPACE. */
380 find_inferior_for_program_space (struct program_space
*pspace
)
382 struct inferior
*inf
;
384 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
386 if (inf
->pspace
== pspace
)
394 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
397 struct inferior
*inf
, *infnext
;
399 for (inf
= inferior_list
; inf
; inf
= infnext
)
402 if ((*callback
) (inf
, data
))
410 valid_gdb_inferior_id (int num
)
412 struct inferior
*inf
;
414 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
422 pid_to_gdb_inferior_id (int pid
)
424 struct inferior
*inf
;
426 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
434 gdb_inferior_id_to_pid (int num
)
436 struct inferior
*inferior
= find_inferior_id (num
);
438 return inferior
->pid
;
444 in_inferior_list (int pid
)
446 struct inferior
*inf
;
448 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
456 have_inferiors (void)
458 struct inferior
*inf
;
460 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
468 have_live_inferiors (void)
470 struct inferior
*inf
;
472 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
475 struct thread_info
*tp
;
477 tp
= any_thread_of_process (inf
->pid
);
478 if (tp
&& target_has_execution_1 (tp
->ptid
))
485 /* Prune away automatically added program spaces that aren't required
489 prune_inferiors (void)
491 struct inferior
*ss
, **ss_link
;
492 struct inferior
*current
= current_inferior ();
495 ss_link
= &inferior_list
;
508 delete_inferior_1 (ss
, 1);
512 prune_program_spaces ();
515 /* Simply returns the count of inferiors. */
518 number_of_inferiors (void)
520 struct inferior
*inf
;
523 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
529 /* Converts an inferior process id to a string. Like
530 target_pid_to_str, but special cases the null process. */
533 inferior_pid_to_str (int pid
)
536 return target_pid_to_str (pid_to_ptid (pid
));
541 /* Prints the list of inferiors and their details on UIOUT. This is a
542 version of 'info_inferior_command' suitable for use from MI.
544 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
545 inferiors that should be printed. Otherwise, all inferiors are
549 print_inferior (struct ui_out
*uiout
, char *requested_inferiors
)
551 struct inferior
*inf
;
552 struct cleanup
*old_chain
;
555 /* Compute number of inferiors we will print. */
556 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
558 if (!number_is_in_list (requested_inferiors
, inf
->num
))
566 ui_out_message (uiout
, 0, "No inferiors.\n");
570 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
572 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
573 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
574 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
575 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
577 ui_out_table_body (uiout
);
578 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
580 struct cleanup
*chain2
;
582 if (!number_is_in_list (requested_inferiors
, inf
->num
))
585 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
587 if (inf
== current_inferior ())
588 ui_out_field_string (uiout
, "current", "*");
590 ui_out_field_skip (uiout
, "current");
592 ui_out_field_int (uiout
, "number", inf
->num
);
594 ui_out_field_string (uiout
, "target-id",
595 inferior_pid_to_str (inf
->pid
));
597 if (inf
->pspace
->ebfd
)
598 ui_out_field_string (uiout
, "exec",
599 bfd_get_filename (inf
->pspace
->ebfd
));
601 ui_out_field_skip (uiout
, "exec");
603 /* Print extra info that isn't really fit to always present in
604 tabular form. Currently we print the vfork parent/child
605 relationships, if any. */
606 if (inf
->vfork_parent
)
608 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
609 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
611 if (inf
->vfork_child
)
613 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
614 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
617 ui_out_text (uiout
, "\n");
618 do_cleanups (chain2
);
621 do_cleanups (old_chain
);
625 detach_inferior_command (char *args
, int from_tty
)
628 struct thread_info
*tp
;
629 struct get_number_or_range_state state
;
632 error (_("Requires argument (inferior id(s) to detach)"));
634 init_number_or_range (&state
, args
);
635 while (!state
.finished
)
637 num
= get_number_or_range (&state
);
639 if (!valid_gdb_inferior_id (num
))
641 warning (_("Inferior ID %d not known."), num
);
645 pid
= gdb_inferior_id_to_pid (num
);
647 tp
= any_thread_of_process (pid
);
650 warning (_("Inferior ID %d has no threads."), num
);
654 switch_to_thread (tp
->ptid
);
656 detach_command (NULL
, from_tty
);
661 kill_inferior_command (char *args
, int from_tty
)
664 struct thread_info
*tp
;
665 struct get_number_or_range_state state
;
668 error (_("Requires argument (inferior id(s) to kill)"));
670 init_number_or_range (&state
, args
);
671 while (!state
.finished
)
673 num
= get_number_or_range (&state
);
675 if (!valid_gdb_inferior_id (num
))
677 warning (_("Inferior ID %d not known."), num
);
681 pid
= gdb_inferior_id_to_pid (num
);
683 tp
= any_thread_of_process (pid
);
686 warning (_("Inferior ID %d has no threads."), num
);
690 switch_to_thread (tp
->ptid
);
695 bfd_cache_close_all ();
699 inferior_command (char *args
, int from_tty
)
701 struct inferior
*inf
;
704 num
= parse_and_eval_long (args
);
706 inf
= find_inferior_id (num
);
708 error (_("Inferior ID %d not known."), num
);
710 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
712 inferior_pid_to_str (inf
->pid
),
714 ? bfd_get_filename (inf
->pspace
->ebfd
)
719 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
721 struct thread_info
*tp
;
723 tp
= any_thread_of_process (inf
->pid
);
725 error (_("Inferior has no threads."));
727 switch_to_thread (tp
->ptid
);
730 printf_filtered (_("[Switching to thread %d (%s)] "),
731 pid_to_thread_id (inferior_ptid
),
732 target_pid_to_str (inferior_ptid
));
736 struct inferior
*inf
;
738 inf
= find_inferior_id (num
);
739 set_current_inferior (inf
);
740 switch_to_thread (null_ptid
);
741 set_current_program_space (inf
->pspace
);
744 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
745 ui_out_text (current_uiout
, "(running)\n");
746 else if (inf
->pid
!= 0)
748 ui_out_text (current_uiout
, "\n");
749 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
753 /* Print information about currently known inferiors. */
756 info_inferiors_command (char *args
, int from_tty
)
758 print_inferior (current_uiout
, args
);
761 /* remove-inferior ID */
764 remove_inferior_command (char *args
, int from_tty
)
767 struct inferior
*inf
;
768 struct get_number_or_range_state state
;
770 if (args
== NULL
|| *args
== '\0')
771 error (_("Requires an argument (inferior id(s) to remove)"));
773 init_number_or_range (&state
, args
);
774 while (!state
.finished
)
776 num
= get_number_or_range (&state
);
777 inf
= find_inferior_id (num
);
781 warning (_("Inferior ID %d not known."), num
);
785 if (inf
== current_inferior ())
787 warning (_("Can not remove current symbol inferior %d."), num
);
793 warning (_("Can not remove active inferior %d."), num
);
797 delete_inferior_1 (inf
, 1);
802 add_inferior_with_spaces (void)
804 struct address_space
*aspace
;
805 struct program_space
*pspace
;
806 struct inferior
*inf
;
808 /* If all inferiors share an address space on this system, this
809 doesn't really return a new address space; otherwise, it
811 aspace
= maybe_new_address_space ();
812 pspace
= add_program_space (aspace
);
813 inf
= add_inferior (0);
814 inf
->pspace
= pspace
;
815 inf
->aspace
= pspace
->aspace
;
820 /* add-inferior [-copies N] [-exec FILENAME] */
823 add_inferior_command (char *args
, int from_tty
)
828 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
832 argv
= gdb_buildargv (args
);
833 make_cleanup_freeargv (argv
);
835 for (; *argv
!= NULL
; argv
++)
839 if (strcmp (*argv
, "-copies") == 0)
843 error (_("No argument to -copies"));
844 copies
= parse_and_eval_long (*argv
);
846 else if (strcmp (*argv
, "-exec") == 0)
850 error (_("No argument to -exec"));
855 error (_("Invalid argument"));
859 save_current_space_and_thread ();
861 for (i
= 0; i
< copies
; ++i
)
863 struct inferior
*inf
= add_inferior_with_spaces ();
865 printf_filtered (_("Added inferior %d\n"), inf
->num
);
869 /* Switch over temporarily, while reading executable and
871 set_current_program_space (inf
->pspace
);
872 set_current_inferior (inf
);
873 switch_to_thread (null_ptid
);
875 exec_file_attach (exec
, from_tty
);
876 symbol_file_add_main (exec
, from_tty
);
880 do_cleanups (old_chain
);
883 /* clone-inferior [-copies N] [ID] */
886 clone_inferior_command (char *args
, int from_tty
)
890 struct inferior
*orginf
= NULL
;
891 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
895 argv
= gdb_buildargv (args
);
896 make_cleanup_freeargv (argv
);
898 for (; *argv
!= NULL
; argv
++)
902 if (strcmp (*argv
, "-copies") == 0)
906 error (_("No argument to -copies"));
907 copies
= parse_and_eval_long (*argv
);
910 error (_("Invalid copies number"));
919 /* The first non-option (-) argument specified the
921 num
= parse_and_eval_long (*argv
);
922 orginf
= find_inferior_id (num
);
925 error (_("Inferior ID %d not known."), num
);
929 error (_("Invalid argument"));
934 /* If no inferior id was specified, then the user wants to clone the
937 orginf
= current_inferior ();
939 save_current_space_and_thread ();
941 for (i
= 0; i
< copies
; ++i
)
943 struct address_space
*aspace
;
944 struct program_space
*pspace
;
945 struct inferior
*inf
;
947 /* If all inferiors share an address space on this system, this
948 doesn't really return a new address space; otherwise, it
950 aspace
= maybe_new_address_space ();
951 pspace
= add_program_space (aspace
);
952 inf
= add_inferior (0);
953 inf
->pspace
= pspace
;
954 inf
->aspace
= pspace
->aspace
;
956 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
958 set_current_inferior (inf
);
959 switch_to_thread (null_ptid
);
960 clone_program_space (pspace
, orginf
->pspace
);
963 do_cleanups (old_chain
);
966 /* Print notices when new inferiors are created and die. */
968 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
969 struct cmd_list_element
*c
, const char *value
)
971 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
976 /* Keep a registry of per-inferior data-pointers required by other GDB
982 void (*cleanup
) (struct inferior
*, void *);
985 struct inferior_data_registration
987 struct inferior_data
*data
;
988 struct inferior_data_registration
*next
;
991 struct inferior_data_registry
993 struct inferior_data_registration
*registrations
;
994 unsigned num_registrations
;
997 static struct inferior_data_registry inferior_data_registry
1000 const struct inferior_data
*
1001 register_inferior_data_with_cleanup
1002 (void (*cleanup
) (struct inferior
*, void *))
1004 struct inferior_data_registration
**curr
;
1006 /* Append new registration. */
1007 for (curr
= &inferior_data_registry
.registrations
;
1008 *curr
!= NULL
; curr
= &(*curr
)->next
);
1010 *curr
= XMALLOC (struct inferior_data_registration
);
1011 (*curr
)->next
= NULL
;
1012 (*curr
)->data
= XMALLOC (struct inferior_data
);
1013 (*curr
)->data
->index
= inferior_data_registry
.num_registrations
++;
1014 (*curr
)->data
->cleanup
= cleanup
;
1016 return (*curr
)->data
;
1019 const struct inferior_data
*
1020 register_inferior_data (void)
1022 return register_inferior_data_with_cleanup (NULL
);
1026 inferior_alloc_data (struct inferior
*inf
)
1028 gdb_assert (inf
->data
== NULL
);
1029 inf
->num_data
= inferior_data_registry
.num_registrations
;
1030 inf
->data
= XCALLOC (inf
->num_data
, void *);
1034 inferior_free_data (struct inferior
*inf
)
1036 gdb_assert (inf
->data
!= NULL
);
1037 clear_inferior_data (inf
);
1043 clear_inferior_data (struct inferior
*inf
)
1045 struct inferior_data_registration
*registration
;
1048 gdb_assert (inf
->data
!= NULL
);
1050 for (registration
= inferior_data_registry
.registrations
, i
= 0;
1052 registration
= registration
->next
, i
++)
1053 if (inf
->data
[i
] != NULL
&& registration
->data
->cleanup
)
1054 registration
->data
->cleanup (inf
, inf
->data
[i
]);
1056 memset (inf
->data
, 0, inf
->num_data
* sizeof (void *));
1060 set_inferior_data (struct inferior
*inf
,
1061 const struct inferior_data
*data
,
1064 gdb_assert (data
->index
< inf
->num_data
);
1065 inf
->data
[data
->index
] = value
;
1069 inferior_data (struct inferior
*inf
, const struct inferior_data
*data
)
1071 gdb_assert (data
->index
< inf
->num_data
);
1072 return inf
->data
[data
->index
];
1076 initialize_inferiors (void)
1078 /* There's always one inferior. Note that this function isn't an
1079 automatic _initialize_foo function, since other _initialize_foo
1080 routines may need to install their per-inferior data keys. We
1081 can only allocate an inferior when all those modules have done
1082 that. Do this after initialize_progspace, due to the
1083 current_program_space reference. */
1084 current_inferior_
= add_inferior (0);
1085 current_inferior_
->pspace
= current_program_space
;
1086 current_inferior_
->aspace
= current_program_space
->aspace
;
1088 add_info ("inferiors", info_inferiors_command
,
1089 _("IDs of specified inferiors (all inferiors if no argument)."));
1091 add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1092 Add a new inferior.\n\
1093 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1094 N is the optional number of inferiors to add, default is 1.\n\
1095 FILENAME is the file name of the executable to use\n\
1096 as main program."));
1098 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1099 Remove inferior ID (or list of IDs).\n\
1100 Usage: remove-inferiors ID..."));
1102 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1103 Clone inferior ID.\n\
1104 Usage: clone-inferior [-copies <N>] [ID]\n\
1105 Add N copies of inferior ID. The new inferior has the same\n\
1106 executable loaded as the copied inferior. If -copies is not specified,\n\
1107 adds 1 copy. If ID is not specified, it is the current inferior\n\
1110 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1111 Detach from inferior ID (or list of IDS)."),
1114 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1115 Kill inferior ID (or list of IDs)."),
1118 add_cmd ("inferior", class_run
, inferior_command
, _("\
1119 Use this command to switch between inferiors.\n\
1120 The new inferior ID must be currently known."),
1123 add_setshow_boolean_cmd ("inferior-events", no_class
,
1124 &print_inferior_events
, _("\
1125 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1126 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1128 show_print_inferior_events
,
1129 &setprintlist
, &showprintlist
);