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/>. */
25 #include "completer.h"
27 #include "gdbthread.h"
30 #include "gdbthread.h"
34 #include "cli/cli-utils.h"
35 #include "continuations.h"
37 void _initialize_inferiors (void);
39 static void inferior_alloc_data (struct inferior
*inf
);
40 static void inferior_free_data (struct inferior
*inf
);
42 struct inferior
*inferior_list
= NULL
;
43 static int highest_inferior_num
;
45 /* Print notices on inferior events (attach, detach, etc.), set with
46 `set print inferior-events'. */
47 static int print_inferior_events
= 0;
49 /* The Current Inferior. */
50 static struct inferior
*current_inferior_
= NULL
;
53 current_inferior (void)
55 return current_inferior_
;
59 set_current_inferior (struct inferior
*inf
)
61 /* There's always an inferior. */
62 gdb_assert (inf
!= NULL
);
64 current_inferior_
= inf
;
67 /* A cleanups callback, helper for save_current_program_space
71 restore_inferior (void *arg
)
73 struct inferior
*saved_inferior
= arg
;
75 set_current_inferior (saved_inferior
);
78 /* Save the current program space so that it may be restored by a later
79 call to do_cleanups. Returns the struct cleanup pointer needed for
80 later doing the cleanup. */
83 save_current_inferior (void)
85 struct cleanup
*old_chain
= make_cleanup (restore_inferior
,
92 free_inferior (struct inferior
*inf
)
94 discard_all_inferior_continuations (inf
);
95 inferior_free_data (inf
);
97 xfree (inf
->terminal
);
98 free_environ (inf
->environment
);
104 init_inferior_list (void)
106 struct inferior
*inf
, *infnext
;
108 highest_inferior_num
= 0;
112 for (inf
= inferior_list
; inf
; inf
= infnext
)
118 inferior_list
= NULL
;
122 add_inferior_silent (int pid
)
124 struct inferior
*inf
;
126 inf
= xmalloc (sizeof (*inf
));
127 memset (inf
, 0, sizeof (*inf
));
130 inf
->control
.stop_soon
= NO_STOP_QUIETLY
;
132 inf
->num
= ++highest_inferior_num
;
133 inf
->next
= inferior_list
;
136 inf
->environment
= make_environ ();
137 init_environ (inf
->environment
);
139 inferior_alloc_data (inf
);
141 observer_notify_inferior_added (inf
);
144 inferior_appeared (inf
, pid
);
150 add_inferior (int pid
)
152 struct inferior
*inf
= add_inferior_silent (pid
);
154 if (print_inferior_events
)
155 printf_unfiltered (_("[New inferior %d]\n"), pid
);
160 struct delete_thread_of_inferior_arg
167 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
169 struct delete_thread_of_inferior_arg
*arg
= data
;
171 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
174 delete_thread_silent (tp
->ptid
);
176 delete_thread (tp
->ptid
);
182 /* If SILENT then be quiet -- don't announce a inferior death, or the
183 exit of its threads. */
186 delete_inferior_1 (struct inferior
*todel
, int silent
)
188 struct inferior
*inf
, *infprev
;
189 struct delete_thread_of_inferior_arg arg
;
193 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
203 iterate_over_threads (delete_thread_of_inferior
, &arg
);
206 infprev
->next
= inf
->next
;
208 inferior_list
= inf
->next
;
210 observer_notify_inferior_removed (inf
);
216 delete_inferior (int pid
)
218 struct inferior
*inf
= find_inferior_pid (pid
);
220 delete_inferior_1 (inf
, 0);
222 if (print_inferior_events
)
223 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
227 delete_inferior_silent (int pid
)
229 struct inferior
*inf
= find_inferior_pid (pid
);
231 delete_inferior_1 (inf
, 1);
235 /* If SILENT then be quiet -- don't announce a inferior exit, or the
236 exit of its threads. */
239 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
241 struct inferior
*inf
;
242 struct delete_thread_of_inferior_arg arg
;
244 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
254 iterate_over_threads (delete_thread_of_inferior
, &arg
);
256 /* Notify the observers before removing the inferior from the list,
257 so that the observers have a chance to look it up. */
258 observer_notify_inferior_exit (inf
);
262 if (inf
->vfork_parent
!= NULL
)
264 inf
->vfork_parent
->vfork_child
= NULL
;
265 inf
->vfork_parent
= NULL
;
268 inf
->has_exit_code
= 0;
273 exit_inferior (int pid
)
275 struct inferior
*inf
= find_inferior_pid (pid
);
277 exit_inferior_1 (inf
, 0);
279 if (print_inferior_events
)
280 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
284 exit_inferior_silent (int pid
)
286 struct inferior
*inf
= find_inferior_pid (pid
);
288 exit_inferior_1 (inf
, 1);
292 exit_inferior_num_silent (int num
)
294 struct inferior
*inf
= find_inferior_id (num
);
296 exit_inferior_1 (inf
, 1);
300 detach_inferior (int pid
)
302 struct inferior
*inf
= find_inferior_pid (pid
);
304 exit_inferior_1 (inf
, 1);
306 if (print_inferior_events
)
307 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
311 inferior_appeared (struct inferior
*inf
, int pid
)
315 observer_notify_inferior_appeared (inf
);
319 discard_all_inferiors (void)
321 struct inferior
*inf
;
323 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
326 exit_inferior_silent (inf
->pid
);
331 find_inferior_id (int num
)
333 struct inferior
*inf
;
335 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
343 find_inferior_pid (int pid
)
345 struct inferior
*inf
;
347 /* Looking for inferior pid == 0 is always wrong, and indicative of
348 a bug somewhere else. There may be more than one with pid == 0,
350 gdb_assert (pid
!= 0);
352 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
359 /* Find an inferior bound to PSPACE. */
362 find_inferior_for_program_space (struct program_space
*pspace
)
364 struct inferior
*inf
;
366 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
368 if (inf
->pspace
== pspace
)
376 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
379 struct inferior
*inf
, *infnext
;
381 for (inf
= inferior_list
; inf
; inf
= infnext
)
384 if ((*callback
) (inf
, data
))
392 valid_gdb_inferior_id (int num
)
394 struct inferior
*inf
;
396 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
404 pid_to_gdb_inferior_id (int pid
)
406 struct inferior
*inf
;
408 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
416 gdb_inferior_id_to_pid (int num
)
418 struct inferior
*inferior
= find_inferior_id (num
);
420 return inferior
->pid
;
426 in_inferior_list (int pid
)
428 struct inferior
*inf
;
430 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
438 have_inferiors (void)
440 struct inferior
*inf
;
442 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
450 have_live_inferiors (void)
452 struct inferior
*inf
;
454 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
457 struct thread_info
*tp
;
459 tp
= any_thread_of_process (inf
->pid
);
460 if (tp
&& target_has_execution_1 (tp
->ptid
))
467 /* Prune away automatically added program spaces that aren't required
471 prune_inferiors (void)
473 struct inferior
*ss
, **ss_link
;
474 struct inferior
*current
= current_inferior ();
477 ss_link
= &inferior_list
;
490 delete_inferior_1 (ss
, 1);
494 prune_program_spaces ();
497 /* Simply returns the count of inferiors. */
500 number_of_inferiors (void)
502 struct inferior
*inf
;
505 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
511 /* Converts an inferior process id to a string. Like
512 target_pid_to_str, but special cases the null process. */
515 inferior_pid_to_str (int pid
)
518 return target_pid_to_str (pid_to_ptid (pid
));
523 /* Prints the list of inferiors and their details on UIOUT. This is a
524 version of 'info_inferior_command' suitable for use from MI.
526 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
527 inferiors that should be printed. Otherwise, all inferiors are
531 print_inferior (struct ui_out
*uiout
, char *requested_inferiors
)
533 struct inferior
*inf
;
534 struct cleanup
*old_chain
;
537 /* Compute number of inferiors we will print. */
538 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
540 if (!number_is_in_list (requested_inferiors
, inf
->num
))
548 ui_out_message (uiout
, 0, "No inferiors.\n");
552 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
554 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
555 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
556 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
557 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
559 ui_out_table_body (uiout
);
560 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
562 struct cleanup
*chain2
;
564 if (!number_is_in_list (requested_inferiors
, inf
->num
))
567 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
569 if (inf
== current_inferior ())
570 ui_out_field_string (uiout
, "current", "*");
572 ui_out_field_skip (uiout
, "current");
574 ui_out_field_int (uiout
, "number", inf
->num
);
576 ui_out_field_string (uiout
, "target-id",
577 inferior_pid_to_str (inf
->pid
));
579 if (inf
->pspace
->ebfd
)
580 ui_out_field_string (uiout
, "exec",
581 bfd_get_filename (inf
->pspace
->ebfd
));
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
);
629 tp
= any_thread_of_process (pid
);
632 warning (_("Inferior ID %d has no threads."), num
);
636 switch_to_thread (tp
->ptid
);
638 detach_command (NULL
, from_tty
);
643 kill_inferior_command (char *args
, int from_tty
)
646 struct thread_info
*tp
;
647 struct get_number_or_range_state state
;
650 error (_("Requires argument (inferior id(s) to kill)"));
652 init_number_or_range (&state
, args
);
653 while (!state
.finished
)
655 num
= get_number_or_range (&state
);
657 if (!valid_gdb_inferior_id (num
))
659 warning (_("Inferior ID %d not known."), num
);
663 pid
= gdb_inferior_id_to_pid (num
);
665 tp
= any_thread_of_process (pid
);
668 warning (_("Inferior ID %d has no threads."), num
);
672 switch_to_thread (tp
->ptid
);
677 bfd_cache_close_all ();
681 inferior_command (char *args
, int from_tty
)
683 struct inferior
*inf
;
686 num
= parse_and_eval_long (args
);
688 inf
= find_inferior_id (num
);
690 error (_("Inferior ID %d not known."), num
);
692 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
694 inferior_pid_to_str (inf
->pid
),
696 ? bfd_get_filename (inf
->pspace
->ebfd
)
701 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
703 struct thread_info
*tp
;
705 tp
= any_thread_of_process (inf
->pid
);
707 error (_("Inferior has no threads."));
709 switch_to_thread (tp
->ptid
);
712 printf_filtered (_("[Switching to thread %d (%s)] "),
713 pid_to_thread_id (inferior_ptid
),
714 target_pid_to_str (inferior_ptid
));
718 struct inferior
*inf
;
720 inf
= find_inferior_id (num
);
721 set_current_inferior (inf
);
722 switch_to_thread (null_ptid
);
723 set_current_program_space (inf
->pspace
);
726 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
727 ui_out_text (current_uiout
, "(running)\n");
728 else if (inf
->pid
!= 0)
730 ui_out_text (current_uiout
, "\n");
731 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
735 /* Print information about currently known inferiors. */
738 info_inferiors_command (char *args
, int from_tty
)
740 print_inferior (current_uiout
, args
);
743 /* remove-inferior ID */
746 remove_inferior_command (char *args
, int from_tty
)
749 struct inferior
*inf
;
750 struct get_number_or_range_state state
;
752 if (args
== NULL
|| *args
== '\0')
753 error (_("Requires an argument (inferior id(s) to remove)"));
755 init_number_or_range (&state
, args
);
756 while (!state
.finished
)
758 num
= get_number_or_range (&state
);
759 inf
= find_inferior_id (num
);
763 warning (_("Inferior ID %d not known."), num
);
767 if (inf
== current_inferior ())
769 warning (_("Can not remove current symbol inferior %d."), num
);
775 warning (_("Can not remove active inferior %d."), num
);
779 delete_inferior_1 (inf
, 1);
784 add_inferior_with_spaces (void)
786 struct address_space
*aspace
;
787 struct program_space
*pspace
;
788 struct inferior
*inf
;
790 /* If all inferiors share an address space on this system, this
791 doesn't really return a new address space; otherwise, it
793 aspace
= maybe_new_address_space ();
794 pspace
= add_program_space (aspace
);
795 inf
= add_inferior (0);
796 inf
->pspace
= pspace
;
797 inf
->aspace
= pspace
->aspace
;
802 /* add-inferior [-copies N] [-exec FILENAME] */
805 add_inferior_command (char *args
, int from_tty
)
810 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
814 argv
= gdb_buildargv (args
);
815 make_cleanup_freeargv (argv
);
817 for (; *argv
!= NULL
; argv
++)
821 if (strcmp (*argv
, "-copies") == 0)
825 error (_("No argument to -copies"));
826 copies
= parse_and_eval_long (*argv
);
828 else if (strcmp (*argv
, "-exec") == 0)
832 error (_("No argument to -exec"));
837 error (_("Invalid argument"));
841 save_current_space_and_thread ();
843 for (i
= 0; i
< copies
; ++i
)
845 struct inferior
*inf
= add_inferior_with_spaces ();
847 printf_filtered (_("Added inferior %d\n"), inf
->num
);
851 /* Switch over temporarily, while reading executable and
853 set_current_program_space (inf
->pspace
);
854 set_current_inferior (inf
);
855 switch_to_thread (null_ptid
);
857 exec_file_attach (exec
, from_tty
);
858 symbol_file_add_main (exec
, from_tty
);
862 do_cleanups (old_chain
);
865 /* clone-inferior [-copies N] [ID] */
868 clone_inferior_command (char *args
, int from_tty
)
872 struct inferior
*orginf
= NULL
;
873 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
877 argv
= gdb_buildargv (args
);
878 make_cleanup_freeargv (argv
);
880 for (; *argv
!= NULL
; argv
++)
884 if (strcmp (*argv
, "-copies") == 0)
888 error (_("No argument to -copies"));
889 copies
= parse_and_eval_long (*argv
);
892 error (_("Invalid copies number"));
901 /* The first non-option (-) argument specified the
903 num
= parse_and_eval_long (*argv
);
904 orginf
= find_inferior_id (num
);
907 error (_("Inferior ID %d not known."), num
);
911 error (_("Invalid argument"));
916 /* If no inferior id was specified, then the user wants to clone the
919 orginf
= current_inferior ();
921 save_current_space_and_thread ();
923 for (i
= 0; i
< copies
; ++i
)
925 struct address_space
*aspace
;
926 struct program_space
*pspace
;
927 struct inferior
*inf
;
929 /* If all inferiors share an address space on this system, this
930 doesn't really return a new address space; otherwise, it
932 aspace
= maybe_new_address_space ();
933 pspace
= add_program_space (aspace
);
934 inf
= add_inferior (0);
935 inf
->pspace
= pspace
;
936 inf
->aspace
= pspace
->aspace
;
938 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
940 set_current_inferior (inf
);
941 switch_to_thread (null_ptid
);
942 clone_program_space (pspace
, orginf
->pspace
);
945 do_cleanups (old_chain
);
948 /* Print notices when new inferiors are created and die. */
950 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
951 struct cmd_list_element
*c
, const char *value
)
953 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
958 /* Keep a registry of per-inferior data-pointers required by other GDB
964 void (*cleanup
) (struct inferior
*, void *);
967 struct inferior_data_registration
969 struct inferior_data
*data
;
970 struct inferior_data_registration
*next
;
973 struct inferior_data_registry
975 struct inferior_data_registration
*registrations
;
976 unsigned num_registrations
;
979 static struct inferior_data_registry inferior_data_registry
982 const struct inferior_data
*
983 register_inferior_data_with_cleanup
984 (void (*cleanup
) (struct inferior
*, void *))
986 struct inferior_data_registration
**curr
;
988 /* Append new registration. */
989 for (curr
= &inferior_data_registry
.registrations
;
990 *curr
!= NULL
; curr
= &(*curr
)->next
);
992 *curr
= XMALLOC (struct inferior_data_registration
);
993 (*curr
)->next
= NULL
;
994 (*curr
)->data
= XMALLOC (struct inferior_data
);
995 (*curr
)->data
->index
= inferior_data_registry
.num_registrations
++;
996 (*curr
)->data
->cleanup
= cleanup
;
998 return (*curr
)->data
;
1001 const struct inferior_data
*
1002 register_inferior_data (void)
1004 return register_inferior_data_with_cleanup (NULL
);
1008 inferior_alloc_data (struct inferior
*inf
)
1010 gdb_assert (inf
->data
== NULL
);
1011 inf
->num_data
= inferior_data_registry
.num_registrations
;
1012 inf
->data
= XCALLOC (inf
->num_data
, void *);
1016 inferior_free_data (struct inferior
*inf
)
1018 gdb_assert (inf
->data
!= NULL
);
1019 clear_inferior_data (inf
);
1025 clear_inferior_data (struct inferior
*inf
)
1027 struct inferior_data_registration
*registration
;
1030 gdb_assert (inf
->data
!= NULL
);
1032 for (registration
= inferior_data_registry
.registrations
, i
= 0;
1034 registration
= registration
->next
, i
++)
1035 if (inf
->data
[i
] != NULL
&& registration
->data
->cleanup
)
1036 registration
->data
->cleanup (inf
, inf
->data
[i
]);
1038 memset (inf
->data
, 0, inf
->num_data
* sizeof (void *));
1042 set_inferior_data (struct inferior
*inf
,
1043 const struct inferior_data
*data
,
1046 gdb_assert (data
->index
< inf
->num_data
);
1047 inf
->data
[data
->index
] = value
;
1051 inferior_data (struct inferior
*inf
, const struct inferior_data
*data
)
1053 gdb_assert (data
->index
< inf
->num_data
);
1054 return inf
->data
[data
->index
];
1058 initialize_inferiors (void)
1060 struct cmd_list_element
*c
= NULL
;
1062 /* There's always one inferior. Note that this function isn't an
1063 automatic _initialize_foo function, since other _initialize_foo
1064 routines may need to install their per-inferior data keys. We
1065 can only allocate an inferior when all those modules have done
1066 that. Do this after initialize_progspace, due to the
1067 current_program_space reference. */
1068 current_inferior_
= add_inferior (0);
1069 current_inferior_
->pspace
= current_program_space
;
1070 current_inferior_
->aspace
= current_program_space
->aspace
;
1072 add_info ("inferiors", info_inferiors_command
,
1073 _("IDs of specified inferiors (all inferiors if no argument)."));
1075 c
= add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1076 Add a new inferior.\n\
1077 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1078 N is the optional number of inferiors to add, default is 1.\n\
1079 FILENAME is the file name of the executable to use\n\
1080 as main program."));
1081 set_cmd_completer (c
, filename_completer
);
1083 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1084 Remove inferior ID (or list of IDs).\n\
1085 Usage: remove-inferiors ID..."));
1087 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1088 Clone inferior ID.\n\
1089 Usage: clone-inferior [-copies <N>] [ID]\n\
1090 Add N copies of inferior ID. The new inferior has the same\n\
1091 executable loaded as the copied inferior. If -copies is not specified,\n\
1092 adds 1 copy. If ID is not specified, it is the current inferior\n\
1095 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1096 Detach from inferior ID (or list of IDS)."),
1099 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1100 Kill inferior ID (or list of IDs)."),
1103 add_cmd ("inferior", class_run
, inferior_command
, _("\
1104 Use this command to switch between inferiors.\n\
1105 The new inferior ID must be currently known."),
1108 add_setshow_boolean_cmd ("inferior-events", no_class
,
1109 &print_inferior_events
, _("\
1110 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1111 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1113 show_print_inferior_events
,
1114 &setprintlist
, &showprintlist
);