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
;
71 set_current_inferior (saved_inferior
);
74 /* Save the current program space so that it may be restored by a later
75 call to do_cleanups. Returns the struct cleanup pointer needed for
76 later doing the cleanup. */
79 save_current_inferior (void)
81 struct cleanup
*old_chain
= make_cleanup (restore_inferior
,
87 free_inferior (struct inferior
*inf
)
89 discard_all_inferior_continuations (inf
);
90 inferior_free_data (inf
);
92 xfree (inf
->terminal
);
93 free_environ (inf
->environment
);
99 init_inferior_list (void)
101 struct inferior
*inf
, *infnext
;
103 highest_inferior_num
= 0;
107 for (inf
= inferior_list
; inf
; inf
= infnext
)
113 inferior_list
= NULL
;
117 add_inferior_silent (int pid
)
119 struct inferior
*inf
;
121 inf
= xmalloc (sizeof (*inf
));
122 memset (inf
, 0, sizeof (*inf
));
125 inf
->stop_soon
= NO_STOP_QUIETLY
;
127 inf
->num
= ++highest_inferior_num
;
128 inf
->next
= inferior_list
;
131 inf
->environment
= make_environ ();
132 init_environ (inf
->environment
);
134 inferior_alloc_data (inf
);
136 observer_notify_inferior_added (inf
);
139 inferior_appeared (inf
, pid
);
145 add_inferior (int pid
)
147 struct inferior
*inf
= add_inferior_silent (pid
);
149 if (print_inferior_events
)
150 printf_unfiltered (_("[New inferior %d]\n"), pid
);
155 struct delete_thread_of_inferior_arg
162 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
164 struct delete_thread_of_inferior_arg
*arg
= data
;
166 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
169 delete_thread_silent (tp
->ptid
);
171 delete_thread (tp
->ptid
);
178 delete_threads_of_inferior (int pid
)
180 struct inferior
*inf
;
181 struct delete_thread_of_inferior_arg arg
;
183 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
193 iterate_over_threads (delete_thread_of_inferior
, &arg
);
196 /* If SILENT then be quiet -- don't announce a inferior death, or the
197 exit of its threads. */
200 delete_inferior_1 (struct inferior
*todel
, int silent
)
202 struct inferior
*inf
, *infprev
;
203 struct delete_thread_of_inferior_arg arg
;
207 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
217 iterate_over_threads (delete_thread_of_inferior
, &arg
);
220 infprev
->next
= inf
->next
;
222 inferior_list
= inf
->next
;
224 observer_notify_inferior_removed (inf
);
230 delete_inferior (int pid
)
232 struct inferior
*inf
= find_inferior_pid (pid
);
234 delete_inferior_1 (inf
, 0);
236 if (print_inferior_events
)
237 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
241 delete_inferior_silent (int pid
)
243 struct inferior
*inf
= find_inferior_pid (pid
);
245 delete_inferior_1 (inf
, 1);
249 /* If SILENT then be quiet -- don't announce a inferior exit, or the
250 exit of its threads. */
253 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
255 struct inferior
*inf
;
256 struct delete_thread_of_inferior_arg arg
;
258 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
268 iterate_over_threads (delete_thread_of_inferior
, &arg
);
270 /* Notify the observers before removing the inferior from the list,
271 so that the observers have a chance to look it up. */
272 observer_notify_inferior_exit (inf
);
275 if (inf
->vfork_parent
!= NULL
)
277 inf
->vfork_parent
->vfork_child
= NULL
;
278 inf
->vfork_parent
= NULL
;
283 exit_inferior (int pid
)
285 struct inferior
*inf
= find_inferior_pid (pid
);
286 exit_inferior_1 (inf
, 0);
288 if (print_inferior_events
)
289 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
293 exit_inferior_silent (int pid
)
295 struct inferior
*inf
= find_inferior_pid (pid
);
296 exit_inferior_1 (inf
, 1);
300 exit_inferior_num_silent (int num
)
302 struct inferior
*inf
= find_inferior_id (num
);
304 exit_inferior_1 (inf
, 1);
308 detach_inferior (int pid
)
310 struct inferior
*inf
= find_inferior_pid (pid
);
311 exit_inferior_1 (inf
, 1);
313 if (print_inferior_events
)
314 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
318 inferior_appeared (struct inferior
*inf
, int pid
)
322 observer_notify_inferior_appeared (inf
);
326 discard_all_inferiors (void)
328 struct inferior
*inf
;
330 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
333 exit_inferior_silent (inf
->pid
);
338 find_inferior_id (int num
)
340 struct inferior
*inf
;
342 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
350 find_inferior_pid (int pid
)
352 struct inferior
*inf
;
354 /* Looking for inferior pid == 0 is always wrong, and indicative of
355 a bug somewhere else. There may be more than one with pid == 0,
357 gdb_assert (pid
!= 0);
359 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
366 /* Find an inferior bound to PSPACE. */
369 find_inferior_for_program_space (struct program_space
*pspace
)
371 struct inferior
*inf
;
373 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
375 if (inf
->pspace
== pspace
)
383 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
386 struct inferior
*inf
, *infnext
;
388 for (inf
= inferior_list
; inf
; inf
= infnext
)
391 if ((*callback
) (inf
, data
))
399 valid_gdb_inferior_id (int num
)
401 struct inferior
*inf
;
403 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
411 pid_to_gdb_inferior_id (int pid
)
413 struct inferior
*inf
;
415 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
423 gdb_inferior_id_to_pid (int num
)
425 struct inferior
*inferior
= find_inferior_id (num
);
427 return inferior
->pid
;
433 in_inferior_list (int pid
)
435 struct inferior
*inf
;
437 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
445 have_inferiors (void)
447 struct inferior
*inf
;
449 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
457 have_live_inferiors (void)
459 struct target_ops
*t
;
461 /* The check on stratum suffices, as GDB doesn't currently support
462 multiple target interfaces. */
463 if (have_inferiors ())
464 for (t
= current_target
.beneath
; t
!= NULL
; t
= t
->beneath
)
465 if (t
->to_stratum
== process_stratum
)
471 /* Prune away automatically added program spaces that aren't required
475 prune_inferiors (void)
477 struct inferior
*ss
, **ss_link
;
478 struct inferior
*current
= current_inferior ();
481 ss_link
= &inferior_list
;
494 delete_inferior_1 (ss
, 1);
498 prune_program_spaces ();
501 /* Simply returns the count of inferiors. */
504 number_of_inferiors (void)
506 struct inferior
*inf
;
509 for (inf
= inferior_list
; inf
!= NULL
; inf
= inf
->next
)
515 /* Prints the list of inferiors and their details on UIOUT. This is a
516 version of 'info_inferior_command' suitable for use from MI.
518 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
519 should be printed. Otherwise, all inferiors are printed. */
521 print_inferior (struct ui_out
*uiout
, int requested_inferior
)
523 struct inferior
*inf
;
524 struct cleanup
*old_chain
;
527 /* Compute number of inferiors we will print. */
528 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
530 struct cleanup
*chain2
;
532 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
540 ui_out_message (uiout
, 0, "No inferiors.\n");
544 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 4, inf_count
,
546 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
547 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
548 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
549 ui_out_table_header (uiout
, 17, ui_left
, "exec", "Executable");
551 ui_out_table_body (uiout
);
552 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
554 struct cleanup
*chain2
;
556 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
559 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
561 if (inf
== current_inferior ())
562 ui_out_field_string (uiout
, "current", "*");
564 ui_out_field_skip (uiout
, "current");
566 ui_out_field_int (uiout
, "number", inf
->num
);
569 ui_out_field_string (uiout
, "target-id",
570 target_pid_to_str (pid_to_ptid (inf
->pid
)));
572 ui_out_field_string (uiout
, "target-id", "<null>");
574 if (inf
->pspace
->ebfd
)
575 ui_out_field_string (uiout
, "exec",
576 bfd_get_filename (inf
->pspace
->ebfd
));
578 ui_out_field_skip (uiout
, "exec");
580 /* Print extra info that isn't really fit to always present in
581 tabular form. Currently we print the vfork parent/child
582 relationships, if any. */
583 if (inf
->vfork_parent
)
585 ui_out_text (uiout
, _("\n\tis vfork child of inferior "));
586 ui_out_field_int (uiout
, "vfork-parent", inf
->vfork_parent
->num
);
588 if (inf
->vfork_child
)
590 ui_out_text (uiout
, _("\n\tis vfork parent of inferior "));
591 ui_out_field_int (uiout
, "vfork-child", inf
->vfork_child
->num
);
594 ui_out_text (uiout
, "\n");
595 do_cleanups (chain2
);
598 do_cleanups (old_chain
);
602 detach_inferior_command (char *args
, int from_tty
)
605 struct thread_info
*tp
;
608 error (_("Requires argument (inferior id to detach)"));
610 num
= parse_and_eval_long (args
);
612 if (!valid_gdb_inferior_id (num
))
613 error (_("Inferior ID %d not known."), num
);
615 pid
= gdb_inferior_id_to_pid (num
);
617 tp
= any_thread_of_process (pid
);
619 error (_("Inferior has no threads."));
621 switch_to_thread (tp
->ptid
);
623 detach_command (NULL
, from_tty
);
627 kill_inferior_command (char *args
, int from_tty
)
630 struct thread_info
*tp
;
633 error (_("Requires argument (inferior id to kill)"));
635 num
= parse_and_eval_long (args
);
637 if (!valid_gdb_inferior_id (num
))
638 error (_("Inferior ID %d not known."), num
);
640 pid
= gdb_inferior_id_to_pid (num
);
642 tp
= any_thread_of_process (pid
);
644 error (_("Inferior has no threads."));
646 switch_to_thread (tp
->ptid
);
650 bfd_cache_close_all ();
654 inferior_command (char *args
, int from_tty
)
656 struct inferior
*inf
;
659 num
= parse_and_eval_long (args
);
661 inf
= find_inferior_id (num
);
663 error (_("Inferior ID %d not known."), num
);
665 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
667 target_pid_to_str (pid_to_ptid (inf
->pid
)),
669 ? bfd_get_filename (inf
->pspace
->ebfd
)
674 if (inf
->pid
!= ptid_get_pid (inferior_ptid
))
676 struct thread_info
*tp
;
678 tp
= any_thread_of_process (inf
->pid
);
680 error (_("Inferior has no threads."));
682 switch_to_thread (tp
->ptid
);
685 printf_filtered (_("[Switching to thread %d (%s)] "),
686 pid_to_thread_id (inferior_ptid
),
687 target_pid_to_str (inferior_ptid
));
691 struct inferior
*inf
;
693 inf
= find_inferior_id (num
);
694 set_current_inferior (inf
);
695 switch_to_thread (null_ptid
);
696 set_current_program_space (inf
->pspace
);
699 if (inf
->pid
!= 0 && is_running (inferior_ptid
))
700 ui_out_text (uiout
, "(running)\n");
701 else if (inf
->pid
!= 0)
703 ui_out_text (uiout
, "\n");
704 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
708 /* Print information about currently known inferiors. */
711 info_inferiors_command (char *args
, int from_tty
)
717 requested
= parse_and_eval_long (args
);
718 if (!valid_gdb_inferior_id (requested
))
719 error (_("Inferior ID %d not known."), requested
);
722 print_inferior (uiout
, requested
);
725 /* remove-inferior ID */
728 remove_inferior_command (char *args
, int from_tty
)
731 struct inferior
*inf
;
733 num
= parse_and_eval_long (args
);
734 inf
= find_inferior_id (num
);
737 error (_("Inferior ID %d not known."), num
);
739 if (inf
== current_inferior ())
740 error (_("Can not remove current symbol inferior."));
742 delete_inferior_1 (inf
, 1);
746 add_inferior_with_spaces (void)
748 struct address_space
*aspace
;
749 struct program_space
*pspace
;
750 struct inferior
*inf
;
752 /* If all inferiors share an address space on this system, this
753 doesn't really return a new address space; otherwise, it
755 aspace
= maybe_new_address_space ();
756 pspace
= add_program_space (aspace
);
757 inf
= add_inferior (0);
758 inf
->pspace
= pspace
;
759 inf
->aspace
= pspace
->aspace
;
764 /* add-inferior [-copies N] [-exec FILENAME] */
767 add_inferior_command (char *args
, int from_tty
)
772 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
776 argv
= gdb_buildargv (args
);
777 make_cleanup_freeargv (argv
);
779 for (; *argv
!= NULL
; argv
++)
783 if (strcmp (*argv
, "-copies") == 0)
787 error (_("No argument to -copies"));
788 copies
= parse_and_eval_long (*argv
);
790 else if (strcmp (*argv
, "-exec") == 0)
794 error (_("No argument to -exec"));
799 error (_("Invalid argument"));
803 save_current_space_and_thread ();
805 for (i
= 0; i
< copies
; ++i
)
807 struct inferior
*inf
= add_inferior_with_spaces ();
809 printf_filtered (_("Added inferior %d\n"), inf
->num
);
813 /* Switch over temporarily, while reading executable and
815 set_current_program_space (inf
->pspace
);
816 set_current_inferior (inf
);
817 switch_to_thread (null_ptid
);
819 exec_file_attach (exec
, from_tty
);
820 symbol_file_add_main (exec
, from_tty
);
824 do_cleanups (old_chain
);
827 /* clone-inferior [-copies N] [ID] */
830 clone_inferior_command (char *args
, int from_tty
)
834 struct inferior
*orginf
= NULL
;
835 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
839 argv
= gdb_buildargv (args
);
840 make_cleanup_freeargv (argv
);
842 for (; *argv
!= NULL
; argv
++)
846 if (strcmp (*argv
, "-copies") == 0)
850 error (_("No argument to -copies"));
851 copies
= parse_and_eval_long (*argv
);
854 error (_("Invalid copies number"));
863 /* The first non-option (-) argument specified the
865 num
= parse_and_eval_long (*argv
);
866 orginf
= find_inferior_id (num
);
869 error (_("Inferior ID %d not known."), num
);
873 error (_("Invalid argument"));
878 /* If no inferior id was specified, then the user wants to clone the
881 orginf
= current_inferior ();
883 save_current_space_and_thread ();
885 for (i
= 0; i
< copies
; ++i
)
887 struct address_space
*aspace
;
888 struct program_space
*pspace
;
889 struct inferior
*inf
;
891 /* If all inferiors share an address space on this system, this
892 doesn't really return a new address space; otherwise, it
894 aspace
= maybe_new_address_space ();
895 pspace
= add_program_space (aspace
);
896 inf
= add_inferior (0);
897 inf
->pspace
= pspace
;
898 inf
->aspace
= pspace
->aspace
;
900 printf_filtered (_("Added inferior %d.\n"), inf
->num
);
902 set_current_inferior (inf
);
903 switch_to_thread (null_ptid
);
904 clone_program_space (pspace
, orginf
->pspace
);
907 do_cleanups (old_chain
);
910 /* Print notices when new inferiors are created and die. */
912 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
913 struct cmd_list_element
*c
, const char *value
)
915 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
920 /* Keep a registry of per-inferior data-pointers required by other GDB
926 void (*cleanup
) (struct inferior
*, void *);
929 struct inferior_data_registration
931 struct inferior_data
*data
;
932 struct inferior_data_registration
*next
;
935 struct inferior_data_registry
937 struct inferior_data_registration
*registrations
;
938 unsigned num_registrations
;
941 static struct inferior_data_registry inferior_data_registry
944 const struct inferior_data
*
945 register_inferior_data_with_cleanup
946 (void (*cleanup
) (struct inferior
*, void *))
948 struct inferior_data_registration
**curr
;
950 /* Append new registration. */
951 for (curr
= &inferior_data_registry
.registrations
;
952 *curr
!= NULL
; curr
= &(*curr
)->next
);
954 *curr
= XMALLOC (struct inferior_data_registration
);
955 (*curr
)->next
= NULL
;
956 (*curr
)->data
= XMALLOC (struct inferior_data
);
957 (*curr
)->data
->index
= inferior_data_registry
.num_registrations
++;
958 (*curr
)->data
->cleanup
= cleanup
;
960 return (*curr
)->data
;
963 const struct inferior_data
*
964 register_inferior_data (void)
966 return register_inferior_data_with_cleanup (NULL
);
970 inferior_alloc_data (struct inferior
*inf
)
972 gdb_assert (inf
->data
== NULL
);
973 inf
->num_data
= inferior_data_registry
.num_registrations
;
974 inf
->data
= XCALLOC (inf
->num_data
, void *);
978 inferior_free_data (struct inferior
*inf
)
980 gdb_assert (inf
->data
!= NULL
);
981 clear_inferior_data (inf
);
987 clear_inferior_data (struct inferior
*inf
)
989 struct inferior_data_registration
*registration
;
992 gdb_assert (inf
->data
!= NULL
);
994 for (registration
= inferior_data_registry
.registrations
, i
= 0;
996 registration
= registration
->next
, i
++)
997 if (inf
->data
[i
] != NULL
&& registration
->data
->cleanup
)
998 registration
->data
->cleanup (inf
, inf
->data
[i
]);
1000 memset (inf
->data
, 0, inf
->num_data
* sizeof (void *));
1004 set_inferior_data (struct inferior
*inf
,
1005 const struct inferior_data
*data
,
1008 gdb_assert (data
->index
< inf
->num_data
);
1009 inf
->data
[data
->index
] = value
;
1013 inferior_data (struct inferior
*inf
, const struct inferior_data
*data
)
1015 gdb_assert (data
->index
< inf
->num_data
);
1016 return inf
->data
[data
->index
];
1020 initialize_inferiors (void)
1022 /* There's always one inferior. Note that this function isn't an
1023 automatic _initialize_foo function, since other _initialize_foo
1024 routines may need to install their per-inferior data keys. We
1025 can only allocate an inferior when all those modules have done
1026 that. Do this after initialize_progspace, due to the
1027 current_program_space reference. */
1028 current_inferior_
= add_inferior (0);
1029 current_inferior_
->pspace
= current_program_space
;
1030 current_inferior_
->aspace
= current_program_space
->aspace
;
1032 add_info ("inferiors", info_inferiors_command
,
1033 _("IDs of currently known inferiors."));
1035 add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1036 Add a new inferior.\n\
1037 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1038 N is the optional number of inferior to add, default is 1.\n\
1039 FILENAME is the file name of the executable to use\n\
1040 as main program."));
1042 add_com ("remove-inferior", no_class
, remove_inferior_command
, _("\
1043 Remove inferior ID.\n\
1044 Usage: remove-inferior ID"));
1046 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1047 Clone inferior ID.\n\
1048 Usage: clone-inferior [-copies <N>] [ID]\n\
1049 Add N copies of inferior ID. The new inferior has the same\n\
1050 executable loaded as the copied inferior. If -copies is not specified,\n\
1051 adds 1 copy. If ID is not specified, it is the current inferior\n\
1054 add_cmd ("inferior", class_run
, detach_inferior_command
, _("\
1055 Detach from inferior ID."),
1058 add_cmd ("inferior", class_run
, kill_inferior_command
, _("\
1059 Kill inferior ID."),
1062 add_cmd ("inferior", class_run
, inferior_command
, _("\
1063 Use this command to switch between inferiors.\n\
1064 The new inferior ID must be currently known."),
1067 add_setshow_boolean_cmd ("inferior-events", no_class
,
1068 &print_inferior_events
, _("\
1069 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1070 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
1072 show_print_inferior_events
,
1073 &setprintlist
, &showprintlist
);