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"
35 void _initialize_inferiors (void);
37 static void inferior_alloc_data (struct inferior
*inf
);
38 static void inferior_free_data (struct inferior
*inf
);
40 struct inferior
*inferior_list
= NULL
;
41 static int highest_inferior_num
;
43 /* Print notices on inferior events (attach, detach, etc.), set with
44 `set print inferior-events'. */
45 static int print_inferior_events
= 0;
47 /* The Current Inferior. */
48 static struct inferior
*current_inferior_
= NULL
;
51 current_inferior (void)
53 return current_inferior_
;
57 set_current_inferior (struct inferior
*inf
)
59 /* There's always an inferior. */
60 gdb_assert (inf
!= NULL
);
62 current_inferior_
= inf
;
65 /* A cleanups callback, helper for save_current_program_space
69 restore_inferior (void *arg
)
71 struct inferior
*saved_inferior
= arg
;
73 set_current_inferior (saved_inferior
);
76 /* Save the current program space so that it may be restored by a later
77 call to do_cleanups. Returns the struct cleanup pointer needed for
78 later doing the cleanup. */
81 save_current_inferior (void)
83 struct cleanup
*old_chain
= make_cleanup (restore_inferior
,
90 free_inferior (struct inferior
*inf
)
92 discard_all_inferior_continuations (inf
);
93 inferior_free_data (inf
);
95 xfree (inf
->terminal
);
96 free_environ (inf
->environment
);
102 init_inferior_list (void)
104 struct inferior
*inf
, *infnext
;
106 highest_inferior_num
= 0;
110 for (inf
= inferior_list
; inf
; inf
= infnext
)
116 inferior_list
= NULL
;
120 add_inferior_silent (int pid
)
122 struct inferior
*inf
;
124 inf
= xmalloc (sizeof (*inf
));
125 memset (inf
, 0, sizeof (*inf
));
128 inf
->control
.stop_soon
= NO_STOP_QUIETLY
;
130 inf
->num
= ++highest_inferior_num
;
131 inf
->next
= inferior_list
;
134 inf
->environment
= make_environ ();
135 init_environ (inf
->environment
);
137 inferior_alloc_data (inf
);
139 observer_notify_inferior_added (inf
);
142 inferior_appeared (inf
, pid
);
148 add_inferior (int pid
)
150 struct inferior
*inf
= add_inferior_silent (pid
);
152 if (print_inferior_events
)
153 printf_unfiltered (_("[New inferior %d]\n"), pid
);
158 struct delete_thread_of_inferior_arg
165 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
167 struct delete_thread_of_inferior_arg
*arg
= data
;
169 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
172 delete_thread_silent (tp
->ptid
);
174 delete_thread (tp
->ptid
);
181 delete_threads_of_inferior (int pid
)
183 struct inferior
*inf
;
184 struct delete_thread_of_inferior_arg arg
;
186 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
196 iterate_over_threads (delete_thread_of_inferior
, &arg
);
199 /* If SILENT then be quiet -- don't announce a inferior death, or the
200 exit of its threads. */
203 delete_inferior_1 (struct inferior
*todel
, int silent
)
205 struct inferior
*inf
, *infprev
;
206 struct delete_thread_of_inferior_arg arg
;
210 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
220 iterate_over_threads (delete_thread_of_inferior
, &arg
);
223 infprev
->next
= inf
->next
;
225 inferior_list
= inf
->next
;
227 observer_notify_inferior_removed (inf
);
233 delete_inferior (int pid
)
235 struct inferior
*inf
= find_inferior_pid (pid
);
237 delete_inferior_1 (inf
, 0);
239 if (print_inferior_events
)
240 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
244 delete_inferior_silent (int pid
)
246 struct inferior
*inf
= find_inferior_pid (pid
);
248 delete_inferior_1 (inf
, 1);
252 /* If SILENT then be quiet -- don't announce a inferior exit, or the
253 exit of its threads. */
256 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
258 struct inferior
*inf
;
259 struct delete_thread_of_inferior_arg arg
;
261 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
271 iterate_over_threads (delete_thread_of_inferior
, &arg
);
273 /* Notify the observers before removing the inferior from the list,
274 so that the observers have a chance to look it up. */
275 observer_notify_inferior_exit (inf
);
278 if (inf
->vfork_parent
!= NULL
)
280 inf
->vfork_parent
->vfork_child
= NULL
;
281 inf
->vfork_parent
= NULL
;
286 exit_inferior (int pid
)
288 struct inferior
*inf
= find_inferior_pid (pid
);
290 exit_inferior_1 (inf
, 0);
292 if (print_inferior_events
)
293 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
297 exit_inferior_silent (int pid
)
299 struct inferior
*inf
= find_inferior_pid (pid
);
301 exit_inferior_1 (inf
, 1);
305 exit_inferior_num_silent (int num
)
307 struct inferior
*inf
= find_inferior_id (num
);
309 exit_inferior_1 (inf
, 1);
313 detach_inferior (int pid
)
315 struct inferior
*inf
= find_inferior_pid (pid
);
317 exit_inferior_1 (inf
, 1);
319 if (print_inferior_events
)
320 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
324 inferior_appeared (struct inferior
*inf
, int pid
)
328 observer_notify_inferior_appeared (inf
);
332 discard_all_inferiors (void)
334 struct inferior
*inf
;
336 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
339 exit_inferior_silent (inf
->pid
);
344 find_inferior_id (int num
)
346 struct inferior
*inf
;
348 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
356 find_inferior_pid (int pid
)
358 struct inferior
*inf
;
360 /* Looking for inferior pid == 0 is always wrong, and indicative of
361 a bug somewhere else. There may be more than one with pid == 0,
363 gdb_assert (pid
!= 0);
365 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
372 /* Find an inferior bound to PSPACE. */
375 find_inferior_for_program_space (struct program_space
*pspace
)
377 struct inferior
*inf
;
379 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
381 if (inf
->pspace
== pspace
)
389 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
392 struct inferior
*inf
, *infnext
;
394 for (inf
= inferior_list
; inf
; inf
= infnext
)
397 if ((*callback
) (inf
, data
))
405 valid_gdb_inferior_id (int num
)
407 struct inferior
*inf
;
409 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
417 pid_to_gdb_inferior_id (int pid
)
419 struct inferior
*inf
;
421 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
429 gdb_inferior_id_to_pid (int num
)
431 struct inferior
*inferior
= find_inferior_id (num
);
433 return inferior
->pid
;
439 in_inferior_list (int pid
)
441 struct inferior
*inf
;
443 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
451 have_inferiors (void)
453 struct inferior
*inf
;
455 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
463 have_live_inferiors (void)
465 struct inferior
*inf
;
467 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
470 struct thread_info
*tp
;
472 tp
= any_thread_of_process (inf
->pid
);
473 if (tp
&& target_has_execution_1 (tp
->ptid
))
480 /* Prune away automatically added program spaces that aren't required
484 prune_inferiors (void)
486 struct inferior
*ss
, **ss_link
;
487 struct inferior
*current
= current_inferior ();
490 ss_link
= &inferior_list
;
503 delete_inferior_1 (ss
, 1);
507 prune_program_spaces ();
510 /* Simply returns the count of inferiors. */
513 number_of_inferiors (void)
515 struct inferior
*inf
;
518 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
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
);
578 ui_out_field_string (uiout
, "target-id",
579 target_pid_to_str (pid_to_ptid (inf
->pid
)));
581 ui_out_field_string (uiout
, "target-id", "<null>");
583 if (inf
->pspace
->ebfd
)
584 ui_out_field_string (uiout
, "exec",
585 bfd_get_filename (inf
->pspace
->ebfd
));
587 ui_out_field_skip (uiout
, "exec");
589 /* Print extra info that isn't really fit to always present in
590 tabular form. Currently we print the vfork parent/child
591 relationships, if any. */
592 if (inf
->vfork_parent
)
594 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
595 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
597 if (inf
->vfork_child
)
599 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
600 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
603 ui_out_text (uiout
, "\n");
604 do_cleanups (chain2
);
607 do_cleanups (old_chain
);
611 detach_inferior_command (char *args
, int from_tty
)
614 struct thread_info
*tp
;
617 error (_("Requires argument (inferior id(s) to detach)"));
619 while (*args
!= '\0')
621 num
= get_number_or_range (&args
);
623 if (!valid_gdb_inferior_id (num
))
625 warning (_("Inferior ID %d not known."), num
);
629 pid
= gdb_inferior_id_to_pid (num
);
631 tp
= any_thread_of_process (pid
);
634 warning (_("Inferior ID %d has no threads."), num
);
638 switch_to_thread (tp
->ptid
);
640 detach_command (NULL
, from_tty
);
645 kill_inferior_command (char *args
, int from_tty
)
648 struct thread_info
*tp
;
651 error (_("Requires argument (inferior id(s) to kill)"));
653 while (*args
!= '\0')
655 num
= get_number_or_range (&args
);
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 target_pid_to_str (pid_to_ptid (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 (uiout
, "(running)\n");
728 else if (inf
->pid
!= 0)
730 ui_out_text (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 (uiout
, args
);
743 /* remove-inferior ID */
746 remove_inferior_command (char *args
, int from_tty
)
749 struct inferior
*inf
;
751 if (args
== NULL
|| *args
== '\0')
752 error (_("Requires an argument (inferior id(s) to remove)"));
754 while (*args
!= '\0')
756 num
= get_number_or_range (&args
);
757 inf
= find_inferior_id (num
);
761 warning (_("Inferior ID %d not known."), num
);
765 if (inf
== current_inferior ())
767 warning (_("Can not remove current symbol inferior %d."), num
);
773 warning (_("Can not remove active inferior %d."), num
);
777 delete_inferior_1 (inf
, 1);
782 add_inferior_with_spaces (void)
784 struct address_space
*aspace
;
785 struct program_space
*pspace
;
786 struct inferior
*inf
;
788 /* If all inferiors share an address space on this system, this
789 doesn't really return a new address space; otherwise, it
791 aspace
= maybe_new_address_space ();
792 pspace
= add_program_space (aspace
);
793 inf
= add_inferior (0);
794 inf
->pspace
= pspace
;
795 inf
->aspace
= pspace
->aspace
;
800 /* add-inferior [-copies N] [-exec FILENAME] */
803 add_inferior_command (char *args
, int from_tty
)
808 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
812 argv
= gdb_buildargv (args
);
813 make_cleanup_freeargv (argv
);
815 for (; *argv
!= NULL
; argv
++)
819 if (strcmp (*argv
, "-copies") == 0)
823 error (_("No argument to -copies"));
824 copies
= parse_and_eval_long (*argv
);
826 else if (strcmp (*argv
, "-exec") == 0)
830 error (_("No argument to -exec"));
835 error (_("Invalid argument"));
839 save_current_space_and_thread ();
841 for (i
= 0; i
< copies
; ++i
)
843 struct inferior
*inf
= add_inferior_with_spaces ();
845 printf_filtered (_("Added inferior %d\n"), inf
->num
);
849 /* Switch over temporarily, while reading executable and
851 set_current_program_space (inf
->pspace
);
852 set_current_inferior (inf
);
853 switch_to_thread (null_ptid
);
855 exec_file_attach (exec
, from_tty
);
856 symbol_file_add_main (exec
, from_tty
);
860 do_cleanups (old_chain
);
863 /* clone-inferior [-copies N] [ID] */
866 clone_inferior_command (char *args
, int from_tty
)
870 struct inferior
*orginf
= NULL
;
871 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
875 argv
= gdb_buildargv (args
);
876 make_cleanup_freeargv (argv
);
878 for (; *argv
!= NULL
; argv
++)
882 if (strcmp (*argv
, "-copies") == 0)
886 error (_("No argument to -copies"));
887 copies
= parse_and_eval_long (*argv
);
890 error (_("Invalid copies number"));
899 /* The first non-option (-) argument specified the
901 num
= parse_and_eval_long (*argv
);
902 orginf
= find_inferior_id (num
);
905 error (_("Inferior ID %d not known."), num
);
909 error (_("Invalid argument"));
914 /* If no inferior id was specified, then the user wants to clone the
917 orginf
= current_inferior ();
919 save_current_space_and_thread ();
921 for (i
= 0; i
< copies
; ++i
)
923 struct address_space
*aspace
;
924 struct program_space
*pspace
;
925 struct inferior
*inf
;
927 /* If all inferiors share an address space on this system, this
928 doesn't really return a new address space; otherwise, it
930 aspace
= maybe_new_address_space ();
931 pspace
= add_program_space (aspace
);
932 inf
= add_inferior (0);
933 inf
->pspace
= pspace
;
934 inf
->aspace
= pspace
->aspace
;
936 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
938 set_current_inferior (inf
);
939 switch_to_thread (null_ptid
);
940 clone_program_space (pspace
, orginf
->pspace
);
943 do_cleanups (old_chain
);
946 /* Print notices when new inferiors are created and die. */
948 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
949 struct cmd_list_element
*c
, const char *value
)
951 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
956 /* Keep a registry of per-inferior data-pointers required by other GDB
962 void (*cleanup
) (struct inferior
*, void *);
965 struct inferior_data_registration
967 struct inferior_data
*data
;
968 struct inferior_data_registration
*next
;
971 struct inferior_data_registry
973 struct inferior_data_registration
*registrations
;
974 unsigned num_registrations
;
977 static struct inferior_data_registry inferior_data_registry
980 const struct inferior_data
*
981 register_inferior_data_with_cleanup
982 (void (*cleanup
) (struct inferior
*, void *))
984 struct inferior_data_registration
**curr
;
986 /* Append new registration. */
987 for (curr
= &inferior_data_registry
.registrations
;
988 *curr
!= NULL
; curr
= &(*curr
)->next
);
990 *curr
= XMALLOC (struct inferior_data_registration
);
991 (*curr
)->next
= NULL
;
992 (*curr
)->data
= XMALLOC (struct inferior_data
);
993 (*curr
)->data
->index
= inferior_data_registry
.num_registrations
++;
994 (*curr
)->data
->cleanup
= cleanup
;
996 return (*curr
)->data
;
999 const struct inferior_data
*
1000 register_inferior_data (void)
1002 return register_inferior_data_with_cleanup (NULL
);
1006 inferior_alloc_data (struct inferior
*inf
)
1008 gdb_assert (inf
->data
== NULL
);
1009 inf
->num_data
= inferior_data_registry
.num_registrations
;
1010 inf
->data
= XCALLOC (inf
->num_data
, void *);
1014 inferior_free_data (struct inferior
*inf
)
1016 gdb_assert (inf
->data
!= NULL
);
1017 clear_inferior_data (inf
);
1023 clear_inferior_data (struct inferior
*inf
)
1025 struct inferior_data_registration
*registration
;
1028 gdb_assert (inf
->data
!= NULL
);
1030 for (registration
= inferior_data_registry
.registrations
, i
= 0;
1032 registration
= registration
->next
, i
++)
1033 if (inf
->data
[i
] != NULL
&& registration
->data
->cleanup
)
1034 registration
->data
->cleanup (inf
, inf
->data
[i
]);
1036 memset (inf
->data
, 0, inf
->num_data
* sizeof (void *));
1040 set_inferior_data (struct inferior
*inf
,
1041 const struct inferior_data
*data
,
1044 gdb_assert (data
->index
< inf
->num_data
);
1045 inf
->data
[data
->index
] = value
;
1049 inferior_data (struct inferior
*inf
, const struct inferior_data
*data
)
1051 gdb_assert (data
->index
< inf
->num_data
);
1052 return inf
->data
[data
->index
];
1056 initialize_inferiors (void)
1058 /* There's always one inferior. Note that this function isn't an
1059 automatic _initialize_foo function, since other _initialize_foo
1060 routines may need to install their per-inferior data keys. We
1061 can only allocate an inferior when all those modules have done
1062 that. Do this after initialize_progspace, due to the
1063 current_program_space reference. */
1064 current_inferior_
= add_inferior (0);
1065 current_inferior_
->pspace
= current_program_space
;
1066 current_inferior_
->aspace
= current_program_space
->aspace
;
1068 add_info ("inferiors", info_inferiors_command
,
1069 _("IDs of specified inferiors (all inferiors if no argument)."));
1071 add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1072 Add a new inferior.\n\
1073 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1074 N is the optional number of inferiors to add, default is 1.\n\
1075 FILENAME is the file name of the executable to use\n\
1076 as main program."));
1078 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1079 Remove inferior ID (or list of IDs).\n\
1080 Usage: remove-inferiors ID..."));
1082 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1083 Clone inferior ID.\n\
1084 Usage: clone-inferior [-copies <N>] [ID]\n\
1085 Add N copies of inferior ID. The new inferior has the same\n\
1086 executable loaded as the copied inferior. If -copies is not specified,\n\
1087 adds 1 copy. If ID is not specified, it is the current inferior\n\
1090 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1091 Detach from inferior ID (or list of IDS)."),
1094 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1095 Kill inferior ID (or list of IDs)."),
1098 add_cmd ("inferior", class_run
, inferior_command
, _("\
1099 Use this command to switch between inferiors.\n\
1100 The new inferior ID must be currently known."),
1103 add_setshow_boolean_cmd ("inferior-events", no_class
,
1104 &print_inferior_events
, _("\
1105 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1106 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1108 show_print_inferior_events
,
1109 &setprintlist
, &showprintlist
);