1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009, 2010 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"
34 void _initialize_inferiors (void);
36 static void inferior_alloc_data (struct inferior
*inf
);
37 static void inferior_free_data (struct inferior
*inf
);
39 struct inferior
*inferior_list
= NULL
;
40 static int highest_inferior_num
;
42 /* Print notices on inferior events (attach, detach, etc.), set with
43 `set print inferior-events'. */
44 static int print_inferior_events
= 0;
46 /* The Current Inferior. */
47 static struct inferior
*current_inferior_
= NULL
;
50 current_inferior (void)
52 return current_inferior_
;
56 set_current_inferior (struct inferior
*inf
)
58 /* There's always an inferior. */
59 gdb_assert (inf
!= NULL
);
61 current_inferior_
= inf
;
64 /* A cleanups callback, helper for save_current_program_space
68 restore_inferior (void *arg
)
70 struct inferior
*saved_inferior
= arg
;
72 set_current_inferior (saved_inferior
);
75 /* Save the current program space so that it may be restored by a later
76 call to do_cleanups. Returns the struct cleanup pointer needed for
77 later doing the cleanup. */
80 save_current_inferior (void)
82 struct cleanup
*old_chain
= make_cleanup (restore_inferior
,
89 free_inferior (struct inferior
*inf
)
91 discard_all_inferior_continuations (inf
);
92 inferior_free_data (inf
);
94 xfree (inf
->terminal
);
95 free_environ (inf
->environment
);
101 init_inferior_list (void)
103 struct inferior
*inf
, *infnext
;
105 highest_inferior_num
= 0;
109 for (inf
= inferior_list
; inf
; inf
= infnext
)
115 inferior_list
= NULL
;
119 add_inferior_silent (int pid
)
121 struct inferior
*inf
;
123 inf
= xmalloc (sizeof (*inf
));
124 memset (inf
, 0, sizeof (*inf
));
127 inf
->control
.stop_soon
= NO_STOP_QUIETLY
;
129 inf
->num
= ++highest_inferior_num
;
130 inf
->next
= inferior_list
;
133 inf
->environment
= make_environ ();
134 init_environ (inf
->environment
);
136 inferior_alloc_data (inf
);
138 observer_notify_inferior_added (inf
);
141 inferior_appeared (inf
, pid
);
147 add_inferior (int pid
)
149 struct inferior
*inf
= add_inferior_silent (pid
);
151 if (print_inferior_events
)
152 printf_unfiltered (_("[New inferior %d]\n"), pid
);
157 struct delete_thread_of_inferior_arg
164 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
166 struct delete_thread_of_inferior_arg
*arg
= data
;
168 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
171 delete_thread_silent (tp
->ptid
);
173 delete_thread (tp
->ptid
);
180 delete_threads_of_inferior (int pid
)
182 struct inferior
*inf
;
183 struct delete_thread_of_inferior_arg arg
;
185 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
195 iterate_over_threads (delete_thread_of_inferior
, &arg
);
198 /* If SILENT then be quiet -- don't announce a inferior death, or the
199 exit of its threads. */
202 delete_inferior_1 (struct inferior
*todel
, int silent
)
204 struct inferior
*inf
, *infprev
;
205 struct delete_thread_of_inferior_arg arg
;
209 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
219 iterate_over_threads (delete_thread_of_inferior
, &arg
);
222 infprev
->next
= inf
->next
;
224 inferior_list
= inf
->next
;
226 observer_notify_inferior_removed (inf
);
232 delete_inferior (int pid
)
234 struct inferior
*inf
= find_inferior_pid (pid
);
236 delete_inferior_1 (inf
, 0);
238 if (print_inferior_events
)
239 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
243 delete_inferior_silent (int pid
)
245 struct inferior
*inf
= find_inferior_pid (pid
);
247 delete_inferior_1 (inf
, 1);
251 /* If SILENT then be quiet -- don't announce a inferior exit, or the
252 exit of its threads. */
255 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
257 struct inferior
*inf
;
258 struct delete_thread_of_inferior_arg arg
;
260 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
270 iterate_over_threads (delete_thread_of_inferior
, &arg
);
272 /* Notify the observers before removing the inferior from the list,
273 so that the observers have a chance to look it up. */
274 observer_notify_inferior_exit (inf
);
277 if (inf
->vfork_parent
!= NULL
)
279 inf
->vfork_parent
->vfork_child
= NULL
;
280 inf
->vfork_parent
= NULL
;
285 exit_inferior (int pid
)
287 struct inferior
*inf
= find_inferior_pid (pid
);
289 exit_inferior_1 (inf
, 0);
291 if (print_inferior_events
)
292 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
296 exit_inferior_silent (int pid
)
298 struct inferior
*inf
= find_inferior_pid (pid
);
300 exit_inferior_1 (inf
, 1);
304 exit_inferior_num_silent (int num
)
306 struct inferior
*inf
= find_inferior_id (num
);
308 exit_inferior_1 (inf
, 1);
312 detach_inferior (int pid
)
314 struct inferior
*inf
= find_inferior_pid (pid
);
316 exit_inferior_1 (inf
, 1);
318 if (print_inferior_events
)
319 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
323 inferior_appeared (struct inferior
*inf
, int pid
)
327 observer_notify_inferior_appeared (inf
);
331 discard_all_inferiors (void)
333 struct inferior
*inf
;
335 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
338 exit_inferior_silent (inf
->pid
);
343 find_inferior_id (int num
)
345 struct inferior
*inf
;
347 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
355 find_inferior_pid (int pid
)
357 struct inferior
*inf
;
359 /* Looking for inferior pid == 0 is always wrong, and indicative of
360 a bug somewhere else. There may be more than one with pid == 0,
362 gdb_assert (pid
!= 0);
364 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
371 /* Find an inferior bound to PSPACE. */
374 find_inferior_for_program_space (struct program_space
*pspace
)
376 struct inferior
*inf
;
378 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
380 if (inf
->pspace
== pspace
)
388 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
391 struct inferior
*inf
, *infnext
;
393 for (inf
= inferior_list
; inf
; inf
= infnext
)
396 if ((*callback
) (inf
, data
))
404 valid_gdb_inferior_id (int num
)
406 struct inferior
*inf
;
408 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
416 pid_to_gdb_inferior_id (int pid
)
418 struct inferior
*inf
;
420 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
428 gdb_inferior_id_to_pid (int num
)
430 struct inferior
*inferior
= find_inferior_id (num
);
432 return inferior
->pid
;
438 in_inferior_list (int pid
)
440 struct inferior
*inf
;
442 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
450 have_inferiors (void)
452 struct inferior
*inf
;
454 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
462 have_live_inferiors (void)
464 struct cleanup
*old_chain
;
465 struct inferior
*inf
;
467 old_chain
= make_cleanup_restore_current_thread ();
469 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
472 struct thread_info
*tp
;
474 tp
= any_thread_of_process (inf
->pid
);
477 switch_to_thread (tp
->ptid
);
479 if (target_has_execution
)
484 do_cleanups (old_chain
);
489 /* Prune away automatically added program spaces that aren't required
493 prune_inferiors (void)
495 struct inferior
*ss
, **ss_link
;
496 struct inferior
*current
= current_inferior ();
499 ss_link
= &inferior_list
;
512 delete_inferior_1 (ss
, 1);
516 prune_program_spaces ();
519 /* Simply returns the count of inferiors. */
522 number_of_inferiors (void)
524 struct inferior
*inf
;
527 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
533 /* Prints the list of inferiors and their details on UIOUT. This is a
534 version of 'info_inferior_command' suitable for use from MI.
536 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
537 should be printed. Otherwise, all inferiors are printed. */
539 print_inferior (struct ui_out
*uiout
, int requested_inferior
)
541 struct inferior
*inf
;
542 struct cleanup
*old_chain
;
545 /* Compute number of inferiors we will print. */
546 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
548 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
556 ui_out_message (uiout
, 0, "No inferiors.\n");
560 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
562 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
563 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
564 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
565 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
567 ui_out_table_body (uiout
);
568 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
570 struct cleanup
*chain2
;
572 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
575 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
577 if (inf
== current_inferior ())
578 ui_out_field_string (uiout
, "current", "*");
580 ui_out_field_skip (uiout
, "current");
582 ui_out_field_int (uiout
, "number", inf
->num
);
585 ui_out_field_string (uiout
, "target-id",
586 target_pid_to_str (pid_to_ptid (inf
->pid
)));
588 ui_out_field_string (uiout
, "target-id", "<null>");
590 if (inf
->pspace
->ebfd
)
591 ui_out_field_string (uiout
, "exec",
592 bfd_get_filename (inf
->pspace
->ebfd
));
594 ui_out_field_skip (uiout
, "exec");
596 /* Print extra info that isn't really fit to always present in
597 tabular form. Currently we print the vfork parent/child
598 relationships, if any. */
599 if (inf
->vfork_parent
)
601 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
602 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
604 if (inf
->vfork_child
)
606 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
607 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
610 ui_out_text (uiout
, "\n");
611 do_cleanups (chain2
);
614 do_cleanups (old_chain
);
618 detach_inferior_command (char *args
, int from_tty
)
621 struct thread_info
*tp
;
624 error (_("Requires argument (inferior id to detach)"));
626 num
= parse_and_eval_long (args
);
628 if (!valid_gdb_inferior_id (num
))
629 error (_("Inferior ID %d not known."), num
);
631 pid
= gdb_inferior_id_to_pid (num
);
633 tp
= any_thread_of_process (pid
);
635 error (_("Inferior has no threads."));
637 switch_to_thread (tp
->ptid
);
639 detach_command (NULL
, from_tty
);
643 kill_inferior_command (char *args
, int from_tty
)
646 struct thread_info
*tp
;
649 error (_("Requires argument (inferior id to kill)"));
651 num
= parse_and_eval_long (args
);
653 if (!valid_gdb_inferior_id (num
))
654 error (_("Inferior ID %d not known."), num
);
656 pid
= gdb_inferior_id_to_pid (num
);
658 tp
= any_thread_of_process (pid
);
660 error (_("Inferior has no threads."));
662 switch_to_thread (tp
->ptid
);
666 bfd_cache_close_all ();
670 inferior_command (char *args
, int from_tty
)
672 struct inferior
*inf
;
675 num
= parse_and_eval_long (args
);
677 inf
= find_inferior_id (num
);
679 error (_("Inferior ID %d not known."), num
);
681 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
683 target_pid_to_str (pid_to_ptid (inf
->pid
)),
685 ? bfd_get_filename (inf
->pspace
->ebfd
)
690 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
692 struct thread_info
*tp
;
694 tp
= any_thread_of_process (inf
->pid
);
696 error (_("Inferior has no threads."));
698 switch_to_thread (tp
->ptid
);
701 printf_filtered (_("[Switching to thread %d (%s)] "),
702 pid_to_thread_id (inferior_ptid
),
703 target_pid_to_str (inferior_ptid
));
707 struct inferior
*inf
;
709 inf
= find_inferior_id (num
);
710 set_current_inferior (inf
);
711 switch_to_thread (null_ptid
);
712 set_current_program_space (inf
->pspace
);
715 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
716 ui_out_text (uiout
, "(running)\n");
717 else if (inf
->pid
!= 0)
719 ui_out_text (uiout
, "\n");
720 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
724 /* Print information about currently known inferiors. */
727 info_inferiors_command (char *args
, int from_tty
)
733 requested
= parse_and_eval_long (args
);
734 if (!valid_gdb_inferior_id (requested
))
735 error (_("Inferior ID %d not known."), requested
);
738 print_inferior (uiout
, requested
);
741 /* remove-inferior ID */
744 remove_inferior_command (char *args
, int from_tty
)
747 struct inferior
*inf
;
749 num
= parse_and_eval_long (args
);
750 inf
= find_inferior_id (num
);
753 error (_("Inferior ID %d not known."), num
);
755 if (inf
== current_inferior ())
756 error (_("Can not remove current symbol inferior."));
758 delete_inferior_1 (inf
, 1);
762 add_inferior_with_spaces (void)
764 struct address_space
*aspace
;
765 struct program_space
*pspace
;
766 struct inferior
*inf
;
768 /* If all inferiors share an address space on this system, this
769 doesn't really return a new address space; otherwise, it
771 aspace
= maybe_new_address_space ();
772 pspace
= add_program_space (aspace
);
773 inf
= add_inferior (0);
774 inf
->pspace
= pspace
;
775 inf
->aspace
= pspace
->aspace
;
780 /* add-inferior [-copies N] [-exec FILENAME] */
783 add_inferior_command (char *args
, int from_tty
)
788 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
792 argv
= gdb_buildargv (args
);
793 make_cleanup_freeargv (argv
);
795 for (; *argv
!= NULL
; argv
++)
799 if (strcmp (*argv
, "-copies") == 0)
803 error (_("No argument to -copies"));
804 copies
= parse_and_eval_long (*argv
);
806 else if (strcmp (*argv
, "-exec") == 0)
810 error (_("No argument to -exec"));
815 error (_("Invalid argument"));
819 save_current_space_and_thread ();
821 for (i
= 0; i
< copies
; ++i
)
823 struct inferior
*inf
= add_inferior_with_spaces ();
825 printf_filtered (_("Added inferior %d\n"), inf
->num
);
829 /* Switch over temporarily, while reading executable and
831 set_current_program_space (inf
->pspace
);
832 set_current_inferior (inf
);
833 switch_to_thread (null_ptid
);
835 exec_file_attach (exec
, from_tty
);
836 symbol_file_add_main (exec
, from_tty
);
840 do_cleanups (old_chain
);
843 /* clone-inferior [-copies N] [ID] */
846 clone_inferior_command (char *args
, int from_tty
)
850 struct inferior
*orginf
= NULL
;
851 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
855 argv
= gdb_buildargv (args
);
856 make_cleanup_freeargv (argv
);
858 for (; *argv
!= NULL
; argv
++)
862 if (strcmp (*argv
, "-copies") == 0)
866 error (_("No argument to -copies"));
867 copies
= parse_and_eval_long (*argv
);
870 error (_("Invalid copies number"));
879 /* The first non-option (-) argument specified the
881 num
= parse_and_eval_long (*argv
);
882 orginf
= find_inferior_id (num
);
885 error (_("Inferior ID %d not known."), num
);
889 error (_("Invalid argument"));
894 /* If no inferior id was specified, then the user wants to clone the
897 orginf
= current_inferior ();
899 save_current_space_and_thread ();
901 for (i
= 0; i
< copies
; ++i
)
903 struct address_space
*aspace
;
904 struct program_space
*pspace
;
905 struct inferior
*inf
;
907 /* If all inferiors share an address space on this system, this
908 doesn't really return a new address space; otherwise, it
910 aspace
= maybe_new_address_space ();
911 pspace
= add_program_space (aspace
);
912 inf
= add_inferior (0);
913 inf
->pspace
= pspace
;
914 inf
->aspace
= pspace
->aspace
;
916 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
918 set_current_inferior (inf
);
919 switch_to_thread (null_ptid
);
920 clone_program_space (pspace
, orginf
->pspace
);
923 do_cleanups (old_chain
);
926 /* Print notices when new inferiors are created and die. */
928 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
929 struct cmd_list_element
*c
, const char *value
)
931 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
936 /* Keep a registry of per-inferior data-pointers required by other GDB
942 void (*cleanup
) (struct inferior
*, void *);
945 struct inferior_data_registration
947 struct inferior_data
*data
;
948 struct inferior_data_registration
*next
;
951 struct inferior_data_registry
953 struct inferior_data_registration
*registrations
;
954 unsigned num_registrations
;
957 static struct inferior_data_registry inferior_data_registry
960 const struct inferior_data
*
961 register_inferior_data_with_cleanup
962 (void (*cleanup
) (struct inferior
*, void *))
964 struct inferior_data_registration
**curr
;
966 /* Append new registration. */
967 for (curr
= &inferior_data_registry
.registrations
;
968 *curr
!= NULL
; curr
= &(*curr
)->next
);
970 *curr
= XMALLOC (struct inferior_data_registration
);
971 (*curr
)->next
= NULL
;
972 (*curr
)->data
= XMALLOC (struct inferior_data
);
973 (*curr
)->data
->index
= inferior_data_registry
.num_registrations
++;
974 (*curr
)->data
->cleanup
= cleanup
;
976 return (*curr
)->data
;
979 const struct inferior_data
*
980 register_inferior_data (void)
982 return register_inferior_data_with_cleanup (NULL
);
986 inferior_alloc_data (struct inferior
*inf
)
988 gdb_assert (inf
->data
== NULL
);
989 inf
->num_data
= inferior_data_registry
.num_registrations
;
990 inf
->data
= XCALLOC (inf
->num_data
, void *);
994 inferior_free_data (struct inferior
*inf
)
996 gdb_assert (inf
->data
!= NULL
);
997 clear_inferior_data (inf
);
1003 clear_inferior_data (struct inferior
*inf
)
1005 struct inferior_data_registration
*registration
;
1008 gdb_assert (inf
->data
!= NULL
);
1010 for (registration
= inferior_data_registry
.registrations
, i
= 0;
1012 registration
= registration
->next
, i
++)
1013 if (inf
->data
[i
] != NULL
&& registration
->data
->cleanup
)
1014 registration
->data
->cleanup (inf
, inf
->data
[i
]);
1016 memset (inf
->data
, 0, inf
->num_data
* sizeof (void *));
1020 set_inferior_data (struct inferior
*inf
,
1021 const struct inferior_data
*data
,
1024 gdb_assert (data
->index
< inf
->num_data
);
1025 inf
->data
[data
->index
] = value
;
1029 inferior_data (struct inferior
*inf
, const struct inferior_data
*data
)
1031 gdb_assert (data
->index
< inf
->num_data
);
1032 return inf
->data
[data
->index
];
1036 initialize_inferiors (void)
1038 /* There's always one inferior. Note that this function isn't an
1039 automatic _initialize_foo function, since other _initialize_foo
1040 routines may need to install their per-inferior data keys. We
1041 can only allocate an inferior when all those modules have done
1042 that. Do this after initialize_progspace, due to the
1043 current_program_space reference. */
1044 current_inferior_
= add_inferior (0);
1045 current_inferior_
->pspace
= current_program_space
;
1046 current_inferior_
->aspace
= current_program_space
->aspace
;
1048 add_info ("inferiors", info_inferiors_command
,
1049 _("IDs of currently known inferiors."));
1051 add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1052 Add a new inferior.\n\
1053 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1054 N is the optional number of inferior to add, default is 1.\n\
1055 FILENAME is the file name of the executable to use\n\
1056 as main program."));
1058 add_com ("remove-inferior", no_class
, remove_inferior_command
, _("\
1059 Remove inferior ID.\n\
1060 Usage: remove-inferior ID"));
1062 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1063 Clone inferior ID.\n\
1064 Usage: clone-inferior [-copies <N>] [ID]\n\
1065 Add N copies of inferior ID. The new inferior has the same\n\
1066 executable loaded as the copied inferior. If -copies is not specified,\n\
1067 adds 1 copy. If ID is not specified, it is the current inferior\n\
1070 add_cmd ("inferior", class_run
, detach_inferior_command
, _("\
1071 Detach from inferior ID."),
1074 add_cmd ("inferior", class_run
, kill_inferior_command
, _("\
1075 Kill inferior ID."),
1078 add_cmd ("inferior", class_run
, inferior_command
, _("\
1079 Use this command to switch between inferiors.\n\
1080 The new inferior ID must be currently known."),
1083 add_setshow_boolean_cmd ("inferior-events", no_class
,
1084 &print_inferior_events
, _("\
1085 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1086 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1088 show_print_inferior_events
,
1089 &setprintlist
, &showprintlist
);