1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2021 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"
29 #include "observable.h"
32 #include "gdbsupport/environ.h"
33 #include "cli/cli-utils.h"
34 #include "arch-utils.h"
35 #include "target-descriptions.h"
36 #include "readline/tilde.h"
37 #include "progspace-and-thread.h"
39 /* Keep a registry of per-inferior data-pointers required by other GDB
42 DEFINE_REGISTRY (inferior
, REGISTRY_ACCESS_FIELD
)
44 struct inferior
*inferior_list
= NULL
;
45 static int highest_inferior_num
;
48 bool print_inferior_events
= true;
50 /* The Current Inferior. This is a strong reference. I.e., whenever
51 an inferior is the current inferior, its refcount is
53 static inferior_ref current_inferior_
;
56 current_inferior (void)
58 return current_inferior_
.get ();
62 set_current_inferior (struct inferior
*inf
)
64 /* There's always an inferior. */
65 gdb_assert (inf
!= NULL
);
67 current_inferior_
= inferior_ref::new_reference (inf
);
70 private_inferior::~private_inferior () = default;
72 inferior::~inferior ()
76 m_continuations
.clear ();
77 inferior_free_data (inf
);
79 target_desc_info_free (inf
->tdesc_info
);
82 inferior::inferior (int pid_
)
83 : num (++highest_inferior_num
),
85 environment (gdb_environ::from_host_environ ()),
88 inferior_alloc_data (this);
90 m_target_stack
.push (get_dummy_target ());
94 inferior::set_tty (const char *terminal_name
)
96 if (terminal_name
!= nullptr && *terminal_name
!= '\0')
97 m_terminal
= make_unique_xstrdup (terminal_name
);
105 return m_terminal
.get ();
109 inferior::add_continuation (std::function
<void ()> &&cont
)
111 m_continuations
.emplace_front (std::move (cont
));
115 inferior::do_all_continuations ()
117 while (!m_continuations
.empty ())
119 auto iter
= m_continuations
.begin ();
121 m_continuations
.erase (iter
);
126 add_inferior_silent (int pid
)
128 inferior
*inf
= new inferior (pid
);
130 if (inferior_list
== NULL
)
136 for (last
= inferior_list
; last
->next
!= NULL
; last
= last
->next
)
141 gdb::observers::inferior_added
.notify (inf
);
144 inferior_appeared (inf
, pid
);
150 add_inferior (int pid
)
152 struct inferior
*inf
= add_inferior_silent (pid
);
154 if (print_inferior_events
)
157 printf_unfiltered (_("[New inferior %d (%s)]\n"),
159 target_pid_to_str (ptid_t (pid
)).c_str ());
161 printf_unfiltered (_("[New inferior %d]\n"), inf
->num
);
168 delete_inferior (struct inferior
*todel
)
170 struct inferior
*inf
, *infprev
;
174 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
181 for (thread_info
*tp
: inf
->threads_safe ())
182 delete_thread_silent (tp
);
185 infprev
->next
= inf
->next
;
187 inferior_list
= inf
->next
;
189 gdb::observers::inferior_removed
.notify (inf
);
191 /* If this program space is rendered useless, remove it. */
192 if (inf
->pspace
->empty ())
198 /* If SILENT then be quiet -- don't announce a inferior exit, or the
199 exit of its threads. */
202 exit_inferior_1 (struct inferior
*inftoex
, int silent
)
204 struct inferior
*inf
;
206 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
213 for (thread_info
*tp
: inf
->threads_safe ())
216 delete_thread_silent (tp
);
221 gdb::observers::inferior_exit
.notify (inf
);
224 inf
->fake_pid_p
= false;
227 if (inf
->vfork_parent
!= NULL
)
229 inf
->vfork_parent
->vfork_child
= NULL
;
230 inf
->vfork_parent
= NULL
;
232 if (inf
->vfork_child
!= NULL
)
234 inf
->vfork_child
->vfork_parent
= NULL
;
235 inf
->vfork_child
= NULL
;
238 inf
->pending_detach
= 0;
240 inf
->control
= inferior_control_state (NO_STOP_QUIETLY
);
242 /* Clear the register cache and the frame cache. */
243 registers_changed ();
244 reinit_frame_cache ();
248 exit_inferior (inferior
*inf
)
250 exit_inferior_1 (inf
, 0);
254 exit_inferior_silent (inferior
*inf
)
256 exit_inferior_1 (inf
, 1);
259 /* See inferior.h. */
262 detach_inferior (inferior
*inf
)
264 /* Save the pid, since exit_inferior_1 will reset it. */
267 exit_inferior_1 (inf
, 0);
269 if (print_inferior_events
)
270 printf_unfiltered (_("[Inferior %d (%s) detached]\n"),
272 target_pid_to_str (ptid_t (pid
)).c_str ());
276 inferior_appeared (struct inferior
*inf
, int pid
)
278 /* If this is the first inferior with threads, reset the global
280 delete_exited_threads ();
281 if (!any_thread_p ())
285 inf
->has_exit_code
= 0;
288 gdb::observers::inferior_appeared
.notify (inf
);
292 find_inferior_id (int num
)
294 for (inferior
*inf
: all_inferiors ())
302 find_inferior_pid (process_stratum_target
*targ
, int pid
)
304 /* Looking for inferior pid == 0 is always wrong, and indicative of
305 a bug somewhere else. There may be more than one with pid == 0,
307 gdb_assert (pid
!= 0);
309 for (inferior
*inf
: all_inferiors (targ
))
319 find_inferior_ptid (process_stratum_target
*targ
, ptid_t ptid
)
321 return find_inferior_pid (targ
, ptid
.pid ());
324 /* See inferior.h. */
327 find_inferior_for_program_space (struct program_space
*pspace
)
329 struct inferior
*cur_inf
= current_inferior ();
331 if (cur_inf
->pspace
== pspace
)
334 for (inferior
*inf
: all_inferiors ())
335 if (inf
->pspace
== pspace
)
342 have_inferiors (void)
344 for (inferior
*inf ATTRIBUTE_UNUSED
: all_non_exited_inferiors ())
350 /* Return the number of live inferiors. We account for the case
351 where an inferior might have a non-zero pid but no threads, as
352 in the middle of a 'mourn' operation. */
355 number_of_live_inferiors (process_stratum_target
*proc_target
)
359 for (inferior
*inf
: all_non_exited_inferiors (proc_target
))
360 if (inf
->has_execution ())
361 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
363 /* Found a live thread in this inferior, go to the next
372 /* Return true if there is at least one live inferior. */
375 have_live_inferiors (void)
377 return number_of_live_inferiors (NULL
) > 0;
380 /* Prune away any unused inferiors, and then prune away no longer used
384 prune_inferiors (void)
391 if (!ss
->deletable ()
399 inferior
*ss_next
= ss
->next
;
400 delete_inferior (ss
);
405 /* Simply returns the count of inferiors. */
408 number_of_inferiors (void)
410 auto rng
= all_inferiors ();
411 return std::distance (rng
.begin (), rng
.end ());
414 /* Converts an inferior process id to a string. Like
415 target_pid_to_str, but special cases the null process. */
418 inferior_pid_to_str (int pid
)
421 return target_pid_to_str (ptid_t (pid
));
426 /* See inferior.h. */
429 print_selected_inferior (struct ui_out
*uiout
)
431 struct inferior
*inf
= current_inferior ();
432 const char *filename
= inf
->pspace
->exec_filename
.get ();
434 if (filename
== NULL
)
435 filename
= _("<noexec>");
437 uiout
->message (_("[Switching to inferior %d [%s] (%s)]\n"),
438 inf
->num
, inferior_pid_to_str (inf
->pid
).c_str (), filename
);
441 /* Helper for print_inferior. Returns the 'connection-id' string for
445 uiout_field_connection (process_stratum_target
*proc_target
)
447 if (proc_target
== NULL
)
451 else if (proc_target
->connection_string () != NULL
)
453 return string_printf ("%d (%s %s)",
454 proc_target
->connection_number
,
455 proc_target
->shortname (),
456 proc_target
->connection_string ());
460 return string_printf ("%d (%s)",
461 proc_target
->connection_number
,
462 proc_target
->shortname ());
466 /* Prints the list of inferiors and their details on UIOUT. This is a
467 version of 'info_inferior_command' suitable for use from MI.
469 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
470 inferiors that should be printed. Otherwise, all inferiors are
474 print_inferior (struct ui_out
*uiout
, const char *requested_inferiors
)
477 size_t connection_id_len
= 20;
479 /* Compute number of inferiors we will print. */
480 for (inferior
*inf
: all_inferiors ())
482 if (!number_is_in_list (requested_inferiors
, inf
->num
))
485 std::string conn
= uiout_field_connection (inf
->process_target ());
486 if (connection_id_len
< conn
.size ())
487 connection_id_len
= conn
.size ();
494 uiout
->message ("No inferiors.\n");
498 ui_out_emit_table
table_emitter (uiout
, 5, inf_count
, "inferiors");
499 uiout
->table_header (1, ui_left
, "current", "");
500 uiout
->table_header (4, ui_left
, "number", "Num");
501 uiout
->table_header (17, ui_left
, "target-id", "Description");
502 uiout
->table_header (connection_id_len
, ui_left
,
503 "connection-id", "Connection");
504 uiout
->table_header (17, ui_left
, "exec", "Executable");
506 uiout
->table_body ();
508 /* Restore the current thread after the loop because we switch the
509 inferior in the loop. */
510 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
511 inferior
*current_inf
= current_inferior ();
512 for (inferior
*inf
: all_inferiors ())
514 if (!number_is_in_list (requested_inferiors
, inf
->num
))
517 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
519 if (inf
== current_inf
)
520 uiout
->field_string ("current", "*");
522 uiout
->field_skip ("current");
524 uiout
->field_signed ("number", inf
->num
);
526 /* Because target_pid_to_str uses the current inferior,
527 switch the inferior. */
528 switch_to_inferior_no_thread (inf
);
530 uiout
->field_string ("target-id", inferior_pid_to_str (inf
->pid
));
532 std::string conn
= uiout_field_connection (inf
->process_target ());
533 uiout
->field_string ("connection-id", conn
.c_str ());
535 if (inf
->pspace
->exec_filename
!= nullptr)
536 uiout
->field_string ("exec", inf
->pspace
->exec_filename
.get ());
538 uiout
->field_skip ("exec");
540 /* Print extra info that isn't really fit to always present in
541 tabular form. Currently we print the vfork parent/child
542 relationships, if any. */
543 if (inf
->vfork_parent
)
545 uiout
->text (_("\n\tis vfork child of inferior "));
546 uiout
->field_signed ("vfork-parent", inf
->vfork_parent
->num
);
548 if (inf
->vfork_child
)
550 uiout
->text (_("\n\tis vfork parent of inferior "));
551 uiout
->field_signed ("vfork-child", inf
->vfork_child
->num
);
559 detach_inferior_command (const char *args
, int from_tty
)
562 error (_("Requires argument (inferior id(s) to detach)"));
564 scoped_restore_current_thread restore_thread
;
566 number_or_range_parser
parser (args
);
567 while (!parser
.finished ())
569 int num
= parser
.get_number ();
571 inferior
*inf
= find_inferior_id (num
);
574 warning (_("Inferior ID %d not known."), num
);
580 warning (_("Inferior ID %d is not running."), num
);
584 thread_info
*tp
= any_thread_of_inferior (inf
);
587 warning (_("Inferior ID %d has no threads."), num
);
591 switch_to_thread (tp
);
593 detach_command (NULL
, from_tty
);
598 kill_inferior_command (const char *args
, int from_tty
)
601 error (_("Requires argument (inferior id(s) to kill)"));
603 scoped_restore_current_thread restore_thread
;
605 number_or_range_parser
parser (args
);
606 while (!parser
.finished ())
608 int num
= parser
.get_number ();
610 inferior
*inf
= find_inferior_id (num
);
613 warning (_("Inferior ID %d not known."), num
);
619 warning (_("Inferior ID %d is not running."), num
);
623 thread_info
*tp
= any_thread_of_inferior (inf
);
626 warning (_("Inferior ID %d has no threads."), num
);
630 switch_to_thread (tp
);
635 bfd_cache_close_all ();
638 /* See inferior.h. */
641 switch_to_inferior_no_thread (inferior
*inf
)
643 set_current_inferior (inf
);
644 switch_to_no_thread ();
645 set_current_program_space (inf
->pspace
);
649 inferior_command (const char *args
, int from_tty
)
651 struct inferior
*inf
;
656 inf
= current_inferior ();
657 gdb_assert (inf
!= nullptr);
658 const char *filename
= inf
->pspace
->exec_filename
.get ();
660 if (filename
== nullptr)
661 filename
= _("<noexec>");
663 printf_filtered (_("[Current inferior is %d [%s] (%s)]\n"),
664 inf
->num
, inferior_pid_to_str (inf
->pid
).c_str (),
669 num
= parse_and_eval_long (args
);
671 inf
= find_inferior_id (num
);
673 error (_("Inferior ID %d not known."), num
);
677 if (inf
!= current_inferior ())
679 thread_info
*tp
= any_thread_of_inferior (inf
);
681 error (_("Inferior has no threads."));
683 switch_to_thread (tp
);
686 gdb::observers::user_selected_context_changed
.notify
687 (USER_SELECTED_INFERIOR
688 | USER_SELECTED_THREAD
689 | USER_SELECTED_FRAME
);
693 switch_to_inferior_no_thread (inf
);
695 gdb::observers::user_selected_context_changed
.notify
696 (USER_SELECTED_INFERIOR
);
701 /* Print information about currently known inferiors. */
704 info_inferiors_command (const char *args
, int from_tty
)
706 print_inferior (current_uiout
, args
);
709 /* remove-inferior ID */
712 remove_inferior_command (const char *args
, int from_tty
)
714 if (args
== NULL
|| *args
== '\0')
715 error (_("Requires an argument (inferior id(s) to remove)"));
717 number_or_range_parser
parser (args
);
718 while (!parser
.finished ())
720 int num
= parser
.get_number ();
721 struct inferior
*inf
= find_inferior_id (num
);
725 warning (_("Inferior ID %d not known."), num
);
729 if (!inf
->deletable ())
731 warning (_("Can not remove current inferior %d."), num
);
737 warning (_("Can not remove active inferior %d."), num
);
741 delete_inferior (inf
);
746 add_inferior_with_spaces (void)
748 struct address_space
*aspace
;
749 struct program_space
*pspace
;
750 struct inferior
*inf
;
751 struct gdbarch_info info
;
753 /* If all inferiors share an address space on this system, this
754 doesn't really return a new address space; otherwise, it
756 aspace
= maybe_new_address_space ();
757 pspace
= new program_space (aspace
);
758 inf
= add_inferior (0);
759 inf
->pspace
= pspace
;
760 inf
->aspace
= pspace
->aspace
;
762 /* Setup the inferior's initial arch, based on information obtained
763 from the global "set ..." options. */
764 gdbarch_info_init (&info
);
765 inf
->gdbarch
= gdbarch_find_by_info (info
);
766 /* The "set ..." options reject invalid settings, so we should
767 always have a valid arch by now. */
768 gdb_assert (inf
->gdbarch
!= NULL
);
773 /* Switch to inferior NEW_INF, a new inferior, and unless
774 NO_CONNECTION is true, push the process_stratum_target of ORG_INF
778 switch_to_inferior_and_push_target (inferior
*new_inf
,
779 bool no_connection
, inferior
*org_inf
)
781 process_stratum_target
*proc_target
= org_inf
->process_target ();
783 /* Switch over temporarily, while reading executable and
785 switch_to_inferior_no_thread (new_inf
);
787 /* Reuse the target for new inferior. */
788 if (!no_connection
&& proc_target
!= NULL
)
790 new_inf
->push_target (proc_target
);
791 if (proc_target
->connection_string () != NULL
)
792 printf_filtered (_("Added inferior %d on connection %d (%s %s)\n"),
794 proc_target
->connection_number
,
795 proc_target
->shortname (),
796 proc_target
->connection_string ());
798 printf_filtered (_("Added inferior %d on connection %d (%s)\n"),
800 proc_target
->connection_number
,
801 proc_target
->shortname ());
804 printf_filtered (_("Added inferior %d\n"), new_inf
->num
);
807 /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
810 add_inferior_command (const char *args
, int from_tty
)
813 gdb::unique_xmalloc_ptr
<char> exec
;
814 symfile_add_flags add_flags
= 0;
815 bool no_connection
= false;
818 add_flags
|= SYMFILE_VERBOSE
;
822 gdb_argv
built_argv (args
);
824 for (char **argv
= built_argv
.get (); *argv
!= NULL
; argv
++)
828 if (strcmp (*argv
, "-copies") == 0)
832 error (_("No argument to -copies"));
833 copies
= parse_and_eval_long (*argv
);
835 else if (strcmp (*argv
, "-no-connection") == 0)
836 no_connection
= true;
837 else if (strcmp (*argv
, "-exec") == 0)
841 error (_("No argument to -exec"));
842 exec
.reset (tilde_expand (*argv
));
846 error (_("Invalid argument"));
850 inferior
*orginf
= current_inferior ();
852 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
854 for (i
= 0; i
< copies
; ++i
)
856 inferior
*inf
= add_inferior_with_spaces ();
858 switch_to_inferior_and_push_target (inf
, no_connection
, orginf
);
862 exec_file_attach (exec
.get (), from_tty
);
863 symbol_file_add_main (exec
.get (), add_flags
);
868 /* clone-inferior [-copies N] [ID] [-no-connection] */
871 clone_inferior_command (const char *args
, int from_tty
)
874 struct inferior
*orginf
= NULL
;
875 bool no_connection
= false;
879 gdb_argv
built_argv (args
);
881 char **argv
= built_argv
.get ();
882 for (; *argv
!= NULL
; argv
++)
886 if (strcmp (*argv
, "-copies") == 0)
890 error (_("No argument to -copies"));
891 copies
= parse_and_eval_long (*argv
);
894 error (_("Invalid copies number"));
896 else if (strcmp (*argv
, "-no-connection") == 0)
897 no_connection
= true;
905 /* The first non-option (-) argument specified the
907 num
= parse_and_eval_long (*argv
);
908 orginf
= find_inferior_id (num
);
911 error (_("Inferior ID %d not known."), num
);
915 error (_("Invalid argument"));
920 /* If no inferior id was specified, then the user wants to clone the
923 orginf
= current_inferior ();
925 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
927 for (i
= 0; i
< copies
; ++i
)
929 struct address_space
*aspace
;
930 struct program_space
*pspace
;
931 struct inferior
*inf
;
933 /* If all inferiors share an address space on this system, this
934 doesn't really return a new address space; otherwise, it
936 aspace
= maybe_new_address_space ();
937 pspace
= new program_space (aspace
);
938 inf
= add_inferior (0);
939 inf
->pspace
= pspace
;
940 inf
->aspace
= pspace
->aspace
;
941 inf
->gdbarch
= orginf
->gdbarch
;
943 switch_to_inferior_and_push_target (inf
, no_connection
, orginf
);
945 /* If the original inferior had a user specified target
946 description, make the clone use it too. */
947 if (target_desc_info_from_user_p (inf
->tdesc_info
))
948 copy_inferior_target_desc_info (inf
, orginf
);
950 clone_program_space (pspace
, orginf
->pspace
);
954 /* Print notices when new inferiors are created and die. */
956 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
957 struct cmd_list_element
*c
, const char *value
)
959 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
962 /* Return a new value for the selected inferior's id. */
964 static struct value
*
965 inferior_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
968 struct inferior
*inf
= current_inferior ();
970 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, inf
->num
);
973 /* Implementation of `$_inferior' variable. */
975 static const struct internalvar_funcs inferior_funcs
=
977 inferior_id_make_value
,
985 initialize_inferiors (void)
987 struct cmd_list_element
*c
= NULL
;
989 /* There's always one inferior. Note that this function isn't an
990 automatic _initialize_foo function, since other _initialize_foo
991 routines may need to install their per-inferior data keys. We
992 can only allocate an inferior when all those modules have done
993 that. Do this after initialize_progspace, due to the
994 current_program_space reference. */
995 set_current_inferior (add_inferior_silent (0));
996 current_inferior_
->pspace
= current_program_space
;
997 current_inferior_
->aspace
= current_program_space
->aspace
;
998 /* The architecture will be initialized shortly, by
999 initialize_current_architecture. */
1001 add_info ("inferiors", info_inferiors_command
,
1002 _("Print a list of inferiors being managed.\n\
1003 Usage: info inferiors [ID]...\n\
1004 If IDs are specified, the list is limited to just those inferiors.\n\
1005 By default all inferiors are displayed."));
1007 c
= add_com ("add-inferior", no_class
, add_inferior_command
, _("\
1008 Add a new inferior.\n\
1009 Usage: add-inferior [-copies N] [-exec FILENAME] [-no-connection]\n\
1010 N is the optional number of inferiors to add, default is 1.\n\
1011 FILENAME is the file name of the executable to use\n\
1013 By default, the new inferior inherits the current inferior's connection.\n\
1014 If -no-connection is specified, the new inferior begins with\n\
1015 no target connection yet."));
1016 set_cmd_completer (c
, filename_completer
);
1018 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1019 Remove inferior ID (or list of IDs).\n\
1020 Usage: remove-inferiors ID..."));
1022 add_com ("clone-inferior", no_class
, clone_inferior_command
, _("\
1023 Clone inferior ID.\n\
1024 Usage: clone-inferior [-copies N] [-no-connection] [ID]\n\
1025 Add N copies of inferior ID. The new inferiors have the same\n\
1026 executable loaded as the copied inferior. If -copies is not specified,\n\
1027 adds 1 copy. If ID is not specified, it is the current inferior\n\
1029 By default, the new inferiors inherit the copied inferior's connection.\n\
1030 If -no-connection is specified, the new inferiors begin with\n\
1031 no target connection yet."));
1033 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1034 Detach from inferior ID (or list of IDS).\n\
1035 Usage; detach inferiors ID..."),
1038 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1039 Kill inferior ID (or list of IDs).\n\
1040 Usage: kill inferiors ID..."),
1043 add_cmd ("inferior", class_run
, inferior_command
, _("\
1044 Use this command to switch between inferiors.\n\
1045 Usage: inferior ID\n\
1046 The new inferior ID must be currently known."),
1049 add_setshow_boolean_cmd ("inferior-events", no_class
,
1050 &print_inferior_events
, _("\
1051 Set printing of inferior events (such as inferior start and exit)."), _("\
1052 Show printing of inferior events (such as inferior start and exit)."), NULL
,
1054 show_print_inferior_events
,
1055 &setprintlist
, &showprintlist
);
1057 create_internalvar_type_lazy ("_inferior", &inferior_funcs
, NULL
);