1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009, 2010, 2011 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
);
279 if (inf
->vfork_parent
!= NULL
)
281 inf
->vfork_parent
->vfork_child
= NULL
;
282 inf
->vfork_parent
= NULL
;
285 inf
->has_exit_code
= 0;
290 exit_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 exited]\n"), pid
);
301 exit_inferior_silent (int pid
)
303 struct inferior
*inf
= find_inferior_pid (pid
);
305 exit_inferior_1 (inf
, 1);
309 exit_inferior_num_silent (int num
)
311 struct inferior
*inf
= find_inferior_id (num
);
313 exit_inferior_1 (inf
, 1);
317 detach_inferior (int pid
)
319 struct inferior
*inf
= find_inferior_pid (pid
);
321 exit_inferior_1 (inf
, 1);
323 if (print_inferior_events
)
324 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
328 inferior_appeared (struct inferior
*inf
, int pid
)
332 observer_notify_inferior_appeared (inf
);
336 discard_all_inferiors (void)
338 struct inferior
*inf
;
340 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
343 exit_inferior_silent (inf
->pid
);
348 find_inferior_id (int num
)
350 struct inferior
*inf
;
352 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
360 find_inferior_pid (int pid
)
362 struct inferior
*inf
;
364 /* Looking for inferior pid == 0 is always wrong, and indicative of
365 a bug somewhere else. There may be more than one with pid == 0,
367 gdb_assert (pid
!= 0);
369 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
376 /* Find an inferior bound to PSPACE. */
379 find_inferior_for_program_space (struct program_space
*pspace
)
381 struct inferior
*inf
;
383 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
385 if (inf
->pspace
== pspace
)
393 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
396 struct inferior
*inf
, *infnext
;
398 for (inf
= inferior_list
; inf
; inf
= infnext
)
401 if ((*callback
) (inf
, data
))
409 valid_gdb_inferior_id (int num
)
411 struct inferior
*inf
;
413 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
421 pid_to_gdb_inferior_id (int pid
)
423 struct inferior
*inf
;
425 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
433 gdb_inferior_id_to_pid (int num
)
435 struct inferior
*inferior
= find_inferior_id (num
);
437 return inferior
->pid
;
443 in_inferior_list (int pid
)
445 struct inferior
*inf
;
447 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
455 have_inferiors (void)
457 struct inferior
*inf
;
459 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
467 have_live_inferiors (void)
469 struct inferior
*inf
;
471 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
474 struct thread_info
*tp
;
476 tp
= any_thread_of_process (inf
->pid
);
477 if (tp
&& target_has_execution_1 (tp
->ptid
))
484 /* Prune away automatically added program spaces that aren't required
488 prune_inferiors (void)
490 struct inferior
*ss
, **ss_link
;
491 struct inferior
*current
= current_inferior ();
494 ss_link
= &inferior_list
;
507 delete_inferior_1 (ss
, 1);
511 prune_program_spaces ();
514 /* Simply returns the count of inferiors. */
517 number_of_inferiors (void)
519 struct inferior
*inf
;
522 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
528 /* Prints the list of inferiors and their details on UIOUT. This is a
529 version of 'info_inferior_command' suitable for use from MI.
531 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
532 inferiors that should be printed. Otherwise, all inferiors are
536 print_inferior (struct ui_out
*uiout
, char *requested_inferiors
)
538 struct inferior
*inf
;
539 struct cleanup
*old_chain
;
542 /* Compute number of inferiors we will print. */
543 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
545 if (!number_is_in_list (requested_inferiors
, inf
->num
))
553 ui_out_message (uiout
, 0, "No inferiors.\n");
557 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
559 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
560 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
561 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
562 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
564 ui_out_table_body (uiout
);
565 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
567 struct cleanup
*chain2
;
569 if (!number_is_in_list (requested_inferiors
, inf
->num
))
572 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
574 if (inf
== current_inferior ())
575 ui_out_field_string (uiout
, "current", "*");
577 ui_out_field_skip (uiout
, "current");
579 ui_out_field_int (uiout
, "number", inf
->num
);
582 ui_out_field_string (uiout
, "target-id",
583 target_pid_to_str (pid_to_ptid (inf
->pid
)));
585 ui_out_field_string (uiout
, "target-id", "<null>");
587 if (inf
->pspace
->ebfd
)
588 ui_out_field_string (uiout
, "exec",
589 bfd_get_filename (inf
->pspace
->ebfd
));
591 ui_out_field_skip (uiout
, "exec");
593 /* Print extra info that isn't really fit to always present in
594 tabular form. Currently we print the vfork parent/child
595 relationships, if any. */
596 if (inf
->vfork_parent
)
598 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
599 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
601 if (inf
->vfork_child
)
603 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
604 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
607 ui_out_text (uiout
, "\n");
608 do_cleanups (chain2
);
611 do_cleanups (old_chain
);
615 detach_inferior_command (char *args
, int from_tty
)
618 struct thread_info
*tp
;
619 struct get_number_or_range_state state
;
622 error (_("Requires argument (inferior id(s) to detach)"));
624 init_number_or_range (&state
, args
);
625 while (!state
.finished
)
627 num
= get_number_or_range (&state
);
629 if (!valid_gdb_inferior_id (num
))
631 warning (_("Inferior ID %d not known."), num
);
635 pid
= gdb_inferior_id_to_pid (num
);
637 tp
= any_thread_of_process (pid
);
640 warning (_("Inferior ID %d has no threads."), num
);
644 switch_to_thread (tp
->ptid
);
646 detach_command (NULL
, from_tty
);
651 kill_inferior_command (char *args
, int from_tty
)
654 struct thread_info
*tp
;
655 struct get_number_or_range_state state
;
658 error (_("Requires argument (inferior id(s) to kill)"));
660 init_number_or_range (&state
, args
);
661 while (!state
.finished
)
663 num
= get_number_or_range (&state
);
665 if (!valid_gdb_inferior_id (num
))
667 warning (_("Inferior ID %d not known."), num
);
671 pid
= gdb_inferior_id_to_pid (num
);
673 tp
= any_thread_of_process (pid
);
676 warning (_("Inferior ID %d has no threads."), num
);
680 switch_to_thread (tp
->ptid
);
685 bfd_cache_close_all ();
689 inferior_command (char *args
, int from_tty
)
691 struct inferior
*inf
;
694 num
= parse_and_eval_long (args
);
696 inf
= find_inferior_id (num
);
698 error (_("Inferior ID %d not known."), num
);
700 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
702 target_pid_to_str (pid_to_ptid (inf
->pid
)),
704 ? bfd_get_filename (inf
->pspace
->ebfd
)
709 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
711 struct thread_info
*tp
;
713 tp
= any_thread_of_process (inf
->pid
);
715 error (_("Inferior has no threads."));
717 switch_to_thread (tp
->ptid
);
720 printf_filtered (_("[Switching to thread %d (%s)] "),
721 pid_to_thread_id (inferior_ptid
),
722 target_pid_to_str (inferior_ptid
));
726 struct inferior
*inf
;
728 inf
= find_inferior_id (num
);
729 set_current_inferior (inf
);
730 switch_to_thread (null_ptid
);
731 set_current_program_space (inf
->pspace
);
734 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
735 ui_out_text (uiout
, "(running)\n");
736 else if (inf
->pid
!= 0)
738 ui_out_text (uiout
, "\n");
739 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
743 /* Print information about currently known inferiors. */
746 info_inferiors_command (char *args
, int from_tty
)
748 print_inferior (uiout
, args
);
751 /* remove-inferior ID */
754 remove_inferior_command (char *args
, int from_tty
)
757 struct inferior
*inf
;
758 struct get_number_or_range_state state
;
760 if (args
== NULL
|| *args
== '\0')
761 error (_("Requires an argument (inferior id(s) to remove)"));
763 init_number_or_range (&state
, args
);
764 while (!state
.finished
)
766 num
= get_number_or_range (&state
);
767 inf
= find_inferior_id (num
);
771 warning (_("Inferior ID %d not known."), num
);
775 if (inf
== current_inferior ())
777 warning (_("Can not remove current symbol inferior %d."), num
);
783 warning (_("Can not remove active inferior %d."), num
);
787 delete_inferior_1 (inf
, 1);
792 add_inferior_with_spaces (void)
794 struct address_space
*aspace
;
795 struct program_space
*pspace
;
796 struct inferior
*inf
;
798 /* If all inferiors share an address space on this system, this
799 doesn't really return a new address space; otherwise, it
801 aspace
= maybe_new_address_space ();
802 pspace
= add_program_space (aspace
);
803 inf
= add_inferior (0);
804 inf
->pspace
= pspace
;
805 inf
->aspace
= pspace
->aspace
;
810 /* add-inferior [-copies N] [-exec FILENAME] */
813 add_inferior_command (char *args
, int from_tty
)
818 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
822 argv
= gdb_buildargv (args
);
823 make_cleanup_freeargv (argv
);
825 for (; *argv
!= NULL
; argv
++)
829 if (strcmp (*argv
, "-copies") == 0)
833 error (_("No argument to -copies"));
834 copies
= parse_and_eval_long (*argv
);
836 else if (strcmp (*argv
, "-exec") == 0)
840 error (_("No argument to -exec"));
845 error (_("Invalid argument"));
849 save_current_space_and_thread ();
851 for (i
= 0; i
< copies
; ++i
)
853 struct inferior
*inf
= add_inferior_with_spaces ();
855 printf_filtered (_("Added inferior %d\n"), inf
->num
);
859 /* Switch over temporarily, while reading executable and
861 set_current_program_space (inf
->pspace
);
862 set_current_inferior (inf
);
863 switch_to_thread (null_ptid
);
865 exec_file_attach (exec
, from_tty
);
866 symbol_file_add_main (exec
, from_tty
);
870 do_cleanups (old_chain
);
873 /* clone-inferior [-copies N] [ID] */
876 clone_inferior_command (char *args
, int from_tty
)
880 struct inferior
*orginf
= NULL
;
881 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
885 argv
= gdb_buildargv (args
);
886 make_cleanup_freeargv (argv
);
888 for (; *argv
!= NULL
; argv
++)
892 if (strcmp (*argv
, "-copies") == 0)
896 error (_("No argument to -copies"));
897 copies
= parse_and_eval_long (*argv
);
900 error (_("Invalid copies number"));
909 /* The first non-option (-) argument specified the
911 num
= parse_and_eval_long (*argv
);
912 orginf
= find_inferior_id (num
);
915 error (_("Inferior ID %d not known."), num
);
919 error (_("Invalid argument"));
924 /* If no inferior id was specified, then the user wants to clone the
927 orginf
= current_inferior ();
929 save_current_space_and_thread ();
931 for (i
= 0; i
< copies
; ++i
)
933 struct address_space
*aspace
;
934 struct program_space
*pspace
;
935 struct inferior
*inf
;
937 /* If all inferiors share an address space on this system, this
938 doesn't really return a new address space; otherwise, it
940 aspace
= maybe_new_address_space ();
941 pspace
= add_program_space (aspace
);
942 inf
= add_inferior (0);
943 inf
->pspace
= pspace
;
944 inf
->aspace
= pspace
->aspace
;
946 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
948 set_current_inferior (inf
);
949 switch_to_thread (null_ptid
);
950 clone_program_space (pspace
, orginf
->pspace
);
953 do_cleanups (old_chain
);
956 /* Print notices when new inferiors are created and die. */
958 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
959 struct cmd_list_element
*c
, const char *value
)
961 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
966 /* Keep a registry of per-inferior data-pointers required by other GDB
972 void (*cleanup
) (struct inferior
*, void *);
975 struct inferior_data_registration
977 struct inferior_data
*data
;
978 struct inferior_data_registration
*next
;
981 struct inferior_data_registry
983 struct inferior_data_registration
*registrations
;
984 unsigned num_registrations
;
987 static struct inferior_data_registry inferior_data_registry
990 const struct inferior_data
*
991 register_inferior_data_with_cleanup
992 (void (*cleanup
) (struct inferior
*, void *))
994 struct inferior_data_registration
**curr
;
996 /* Append new registration. */
997 for (curr
= &inferior_data_registry
.registrations
;
998 *curr
!= NULL
; curr
= &(*curr
)->next
);
1000 *curr
= XMALLOC (struct inferior_data_registration
);
1001 (*curr
)->next
= NULL
;
1002 (*curr
)->data
= XMALLOC (struct inferior_data
);
1003 (*curr
)->data
->index
= inferior_data_registry
.num_registrations
++;
1004 (*curr
)->data
->cleanup
= cleanup
;
1006 return (*curr
)->data
;
1009 const struct inferior_data
*
1010 register_inferior_data (void)
1012 return register_inferior_data_with_cleanup (NULL
);
1016 inferior_alloc_data (struct inferior
*inf
)
1018 gdb_assert (inf
->data
== NULL
);
1019 inf
->num_data
= inferior_data_registry
.num_registrations
;
1020 inf
->data
= XCALLOC (inf
->num_data
, void *);
1024 inferior_free_data (struct inferior
*inf
)
1026 gdb_assert (inf
->data
!= NULL
);
1027 clear_inferior_data (inf
);
1033 clear_inferior_data (struct inferior
*inf
)
1035 struct inferior_data_registration
*registration
;
1038 gdb_assert (inf
->data
!= NULL
);
1040 for (registration
= inferior_data_registry
.registrations
, i
= 0;
1042 registration
= registration
->next
, i
++)
1043 if (inf
->data
[i
] != NULL
&& registration
->data
->cleanup
)
1044 registration
->data
->cleanup (inf
, inf
->data
[i
]);
1046 memset (inf
->data
, 0, inf
->num_data
* sizeof (void *));
1050 set_inferior_data (struct inferior
*inf
,
1051 const struct inferior_data
*data
,
1054 gdb_assert (data
->index
< inf
->num_data
);
1055 inf
->data
[data
->index
] = value
;
1059 inferior_data (struct inferior
*inf
, const struct inferior_data
*data
)
1061 gdb_assert (data
->index
< inf
->num_data
);
1062 return inf
->data
[data
->index
];
1066 initialize_inferiors (void)
1068 /* There's always one inferior. Note that this function isn't an
1069 automatic _initialize_foo function, since other _initialize_foo
1070 routines may need to install their per-inferior data keys. We
1071 can only allocate an inferior when all those modules have done
1072 that. Do this after initialize_progspace, due to the
1073 current_program_space reference. */
1074 current_inferior_
= add_inferior (0);
1075 current_inferior_
->pspace
= current_program_space
;
1076 current_inferior_
->aspace
= current_program_space
->aspace
;
1078 add_info ("inferiors", info_inferiors_command
,
1079 _("IDs of specified inferiors (all inferiors if no argument)."));
1081 add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1082 Add a new inferior.\n\
1083 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1084 N is the optional number of inferiors to add, default is 1.\n\
1085 FILENAME is the file name of the executable to use\n\
1086 as main program."));
1088 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1089 Remove inferior ID (or list of IDs).\n\
1090 Usage: remove-inferiors ID..."));
1092 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1093 Clone inferior ID.\n\
1094 Usage: clone-inferior [-copies <N>] [ID]\n\
1095 Add N copies of inferior ID. The new inferior has the same\n\
1096 executable loaded as the copied inferior. If -copies is not specified,\n\
1097 adds 1 copy. If ID is not specified, it is the current inferior\n\
1100 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1101 Detach from inferior ID (or list of IDS)."),
1104 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1105 Kill inferior ID (or list of IDs)."),
1108 add_cmd ("inferior", class_run
, inferior_command
, _("\
1109 Use this command to switch between inferiors.\n\
1110 The new inferior ID must be currently known."),
1113 add_setshow_boolean_cmd ("inferior-events", no_class
,
1114 &print_inferior_events
, _("\
1115 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1116 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1118 show_print_inferior_events
,
1119 &setprintlist
, &showprintlist
);