1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008-2024 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/>. */
24 #include "completer.h"
25 #include "cli/cli-cmds.h"
26 #include "gdbthread.h"
28 #include "observable.h"
31 #include "gdbsupport/environ.h"
32 #include "cli/cli-utils.h"
33 #include "arch-utils.h"
34 #include "target-descriptions.h"
35 #include "target-connection.h"
36 #include "gdbsupport/gdb_tilde_expand.h"
37 #include "progspace-and-thread.h"
38 #include "gdbsupport/buildargv.h"
39 #include "cli/cli-style.h"
42 intrusive_list
<inferior
> inferior_list
;
43 static int highest_inferior_num
;
46 bool print_inferior_events
= true;
48 /* The Current Inferior. This is a strong reference. I.e., whenever
49 an inferior is the current inferior, its refcount is
51 static inferior_ref current_inferior_
;
54 current_inferior (void)
56 return current_inferior_
.get ();
60 set_current_inferior (struct inferior
*inf
)
62 /* There's always an inferior. */
63 gdb_assert (inf
!= NULL
);
65 current_inferior_
= inferior_ref::new_reference (inf
);
68 private_inferior::~private_inferior () = default;
70 inferior::~inferior ()
72 /* Before the inferior is deleted, all target_ops should be popped from
73 the target stack, this leaves just the dummy_target behind. If this
74 is not done, then any target left in the target stack will be left
75 with an artificially high reference count. As the dummy_target is
76 still on the target stack then we are about to loose a reference to
77 that target, leaving its reference count artificially high. However,
78 this is not critical as the dummy_target is a singleton. */
79 gdb_assert (m_target_stack
.top ()->stratum () == dummy_stratum
);
81 m_continuations
.clear ();
84 inferior::inferior (int pid_
)
85 : num (++highest_inferior_num
),
87 environment (gdb_environ::from_host_environ ())
89 m_target_stack
.push (get_dummy_target ());
95 inferior::unpush_target (struct target_ops
*t
)
97 /* If unpushing the process stratum target from the inferior while threads
98 exist in the inferior, ensure that we don't leave any threads of the
99 inferior in the target's "resumed with pending wait status" list.
101 See also the comment in set_thread_exited. */
102 if (t
->stratum () == process_stratum
)
104 process_stratum_target
*proc_target
= as_process_stratum_target (t
);
106 for (thread_info
*thread
: this->non_exited_threads ())
107 proc_target
->maybe_remove_resumed_with_pending_wait_status (thread
);
110 return m_target_stack
.unpush (t
);
113 /* See inferior.h. */
116 inferior::unpush_target_and_assert (struct target_ops
*target
)
118 gdb_assert (current_inferior () == this);
120 if (!unpush_target (target
))
121 internal_error ("pop_all_targets couldn't find target %s\n",
122 target
->shortname ());
125 /* See inferior.h. */
128 inferior::pop_all_targets_above (enum strata stratum
)
130 /* Unpushing a target might cause it to close. Some targets currently
131 rely on the current_inferior being set for their ::close method, so we
132 temporarily switch inferior now. */
133 scoped_restore_current_pspace_and_thread restore_pspace_and_thread
;
134 switch_to_inferior_no_thread (this);
136 while (top_target ()->stratum () > stratum
)
137 unpush_target_and_assert (top_target ());
140 /* See inferior.h. */
143 inferior::pop_all_targets_at_and_above (enum strata stratum
)
145 /* Unpushing a target might cause it to close. Some targets currently
146 rely on the current_inferior being set for their ::close method, so we
147 temporarily switch inferior now. */
148 scoped_restore_current_pspace_and_thread restore_pspace_and_thread
;
149 switch_to_inferior_no_thread (this);
151 while (top_target ()->stratum () >= stratum
)
152 unpush_target_and_assert (top_target ());
156 inferior::set_tty (std::string terminal_name
)
158 m_terminal
= std::move (terminal_name
);
167 /* See inferior.h. */
170 inferior::set_args (gdb::array_view
<char * const> args
)
172 set_args (construct_inferior_arguments (args
));
176 inferior::set_arch (gdbarch
*arch
)
178 gdb_assert (arch
!= nullptr);
179 gdb_assert (gdbarch_initialized_p (arch
));
182 process_stratum_target
*proc_target
= this->process_target ();
183 if (proc_target
!= nullptr)
184 registers_changed_ptid (proc_target
, ptid_t (this->pid
));
188 inferior::add_continuation (std::function
<void ()> &&cont
)
190 m_continuations
.emplace_front (std::move (cont
));
194 inferior::do_all_continuations ()
196 while (!m_continuations
.empty ())
198 auto iter
= m_continuations
.begin ();
200 m_continuations
.erase (iter
);
204 /* Notify interpreters and observers that inferior INF was added. */
207 notify_inferior_added (inferior
*inf
)
209 interps_notify_inferior_added (inf
);
210 gdb::observers::inferior_added
.notify (inf
);
214 add_inferior_silent (int pid
)
216 inferior
*inf
= new inferior (pid
);
218 inferior_list
.push_back (*inf
);
220 notify_inferior_added (inf
);
223 inferior_appeared (inf
, pid
);
229 add_inferior (int pid
)
231 struct inferior
*inf
= add_inferior_silent (pid
);
233 if (print_inferior_events
)
236 gdb_printf (_("[New inferior %d (%s)]\n"),
238 target_pid_to_str (ptid_t (pid
)).c_str ());
240 gdb_printf (_("[New inferior %d]\n"), inf
->num
);
246 /* See inferior.h. */
249 inferior::find_thread (ptid_t ptid
)
251 auto it
= this->ptid_thread_map
.find (ptid
);
252 if (it
!= this->ptid_thread_map
.end ())
258 /* See inferior.h. */
261 inferior::clear_thread_list ()
263 thread_list
.clear_and_dispose ([=] (thread_info
*thr
)
265 threads_debug_printf ("deleting thread %s",
266 thr
->ptid
.to_string ().c_str ());
267 set_thread_exited (thr
, {}, true /* silent */);
268 if (thr
->deletable ())
271 ptid_thread_map
.clear ();
274 /* Notify interpreters and observers that inferior INF was removed. */
277 notify_inferior_removed (inferior
*inf
)
279 interps_notify_inferior_removed (inf
);
280 gdb::observers::inferior_removed
.notify (inf
);
284 delete_inferior (struct inferior
*inf
)
286 inf
->clear_thread_list ();
288 auto it
= inferior_list
.iterator_to (*inf
);
289 inferior_list
.erase (it
);
291 notify_inferior_removed (inf
);
293 /* Pop all targets now, this ensures that inferior::unpush is called
294 correctly. As pop_all_targets ends up making a temporary switch to
295 inferior INF then we need to make this call before we delete the
296 program space, which we do below. */
297 inf
->pop_all_targets ();
299 /* If this program space is rendered useless, remove it. */
300 if (inf
->pspace
->empty ())
306 /* Notify interpreters and observers that inferior INF disappeared. */
309 notify_inferior_disappeared (inferior
*inf
)
311 interps_notify_inferior_disappeared (inf
);
312 gdb::observers::inferior_exit
.notify (inf
);
315 /* See inferior.h. */
318 exit_inferior (struct inferior
*inf
)
320 inf
->clear_thread_list ();
322 notify_inferior_disappeared (inf
);
325 inf
->fake_pid_p
= false;
328 if (inf
->vfork_parent
!= NULL
)
330 inf
->vfork_parent
->vfork_child
= NULL
;
331 inf
->vfork_parent
= NULL
;
333 if (inf
->vfork_child
!= NULL
)
335 inf
->vfork_child
->vfork_parent
= NULL
;
336 inf
->vfork_child
= NULL
;
339 inf
->pending_detach
= false;
341 inf
->control
= inferior_control_state (NO_STOP_QUIETLY
);
343 /* Clear the register cache and the frame cache. */
344 registers_changed ();
345 reinit_frame_cache ();
348 /* See inferior.h. */
351 detach_inferior (inferior
*inf
)
353 /* Save the pid, since exit_inferior will reset it. */
358 if (print_inferior_events
)
359 gdb_printf (_("[Inferior %d (%s) detached]\n"),
361 target_pid_to_str (ptid_t (pid
)).c_str ());
364 /* Notify interpreters and observers that inferior INF appeared. */
367 notify_inferior_appeared (inferior
*inf
)
369 interps_notify_inferior_appeared (inf
);
370 gdb::observers::inferior_appeared
.notify (inf
);
374 inferior_appeared (struct inferior
*inf
, int pid
)
376 /* If this is the first inferior with threads, reset the global
378 delete_exited_threads ();
379 if (!any_thread_p ())
383 inf
->has_exit_code
= false;
386 notify_inferior_appeared (inf
);
390 find_inferior_id (int num
)
392 for (inferior
*inf
: all_inferiors ())
400 find_inferior_pid (process_stratum_target
*targ
, int pid
)
402 /* Looking for inferior pid == 0 is always wrong, and indicative of
403 a bug somewhere else. There may be more than one with pid == 0,
405 gdb_assert (pid
!= 0);
407 for (inferior
*inf
: all_inferiors (targ
))
417 find_inferior_ptid (process_stratum_target
*targ
, ptid_t ptid
)
419 return find_inferior_pid (targ
, ptid
.pid ());
422 /* See inferior.h. */
425 find_inferior_for_program_space (struct program_space
*pspace
)
427 struct inferior
*cur_inf
= current_inferior ();
429 if (cur_inf
->pspace
== pspace
)
432 for (inferior
*inf
: all_inferiors ())
433 if (inf
->pspace
== pspace
)
440 have_inferiors (void)
442 for (inferior
*inf ATTRIBUTE_UNUSED
: all_non_exited_inferiors ())
448 /* Return the number of live inferiors. We account for the case
449 where an inferior might have a non-zero pid but no threads, as
450 in the middle of a 'mourn' operation. */
453 number_of_live_inferiors (process_stratum_target
*proc_target
)
457 for (inferior
*inf
: all_non_exited_inferiors (proc_target
))
458 if (inf
->has_execution ())
459 for (thread_info
*tp ATTRIBUTE_UNUSED
: inf
->non_exited_threads ())
461 /* Found a live thread in this inferior, go to the next
470 /* Return true if there is at least one live inferior. */
473 have_live_inferiors (void)
475 return number_of_live_inferiors (NULL
) > 0;
478 /* Prune away any unused inferiors, and then prune away no longer used
482 prune_inferiors (void)
484 for (inferior
*inf
: all_inferiors_safe ())
486 if (!inf
->deletable ()
491 delete_inferior (inf
);
495 /* Simply returns the count of inferiors. */
498 number_of_inferiors (void)
500 auto rng
= all_inferiors ();
501 return std::distance (rng
.begin (), rng
.end ());
504 /* Converts an inferior process id to a string. Like
505 target_pid_to_str, but special cases the null process. */
508 inferior_pid_to_str (int pid
)
511 return target_pid_to_str (ptid_t (pid
));
516 /* See inferior.h. */
519 print_selected_inferior (struct ui_out
*uiout
)
521 struct inferior
*inf
= current_inferior ();
522 const char *filename
= inf
->pspace
->exec_filename ();
524 if (filename
== NULL
)
525 filename
= _("<noexec>");
527 uiout
->message (_("[Switching to inferior %d [%s] (%s)]\n"),
528 inf
->num
, inferior_pid_to_str (inf
->pid
).c_str (), filename
);
531 /* Helper for print_inferior. Returns the 'connection-id' string for
535 uiout_field_connection (process_stratum_target
*proc_target
)
537 if (proc_target
== NULL
)
541 std::string conn_str
= make_target_connection_string (proc_target
);
542 return string_printf ("%d (%s)", proc_target
->connection_number
,
547 /* Prints the list of inferiors and their details on UIOUT. This is a
548 version of 'info_inferior_command' suitable for use from MI.
550 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
551 inferiors that should be printed. Otherwise, all inferiors are
555 print_inferior (struct ui_out
*uiout
, const char *requested_inferiors
)
558 size_t connection_id_len
= 20;
560 /* Compute number of inferiors we will print. */
561 for (inferior
*inf
: all_inferiors ())
563 if (!number_is_in_list (requested_inferiors
, inf
->num
))
566 std::string conn
= uiout_field_connection (inf
->process_target ());
567 if (connection_id_len
< conn
.size ())
568 connection_id_len
= conn
.size ();
575 uiout
->message ("No inferiors.\n");
579 ui_out_emit_table
table_emitter (uiout
, 5, inf_count
, "inferiors");
580 uiout
->table_header (1, ui_left
, "current", "");
581 uiout
->table_header (4, ui_left
, "number", "Num");
582 uiout
->table_header (17, ui_left
, "target-id", "Description");
583 uiout
->table_header (connection_id_len
, ui_left
,
584 "connection-id", "Connection");
585 uiout
->table_header (17, ui_left
, "exec", "Executable");
587 uiout
->table_body ();
589 /* Restore the current thread after the loop because we switch the
590 inferior in the loop. */
591 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
592 inferior
*current_inf
= current_inferior ();
593 for (inferior
*inf
: all_inferiors ())
595 if (!number_is_in_list (requested_inferiors
, inf
->num
))
598 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
600 if (inf
== current_inf
)
601 uiout
->field_string ("current", "*");
603 uiout
->field_skip ("current");
605 uiout
->field_signed ("number", inf
->num
);
607 /* Because target_pid_to_str uses the current inferior,
608 switch the inferior. */
609 switch_to_inferior_no_thread (inf
);
611 uiout
->field_string ("target-id", inferior_pid_to_str (inf
->pid
));
613 std::string conn
= uiout_field_connection (inf
->process_target ());
614 uiout
->field_string ("connection-id", conn
);
616 if (inf
->pspace
->exec_filename () != nullptr)
617 uiout
->field_string ("exec", inf
->pspace
->exec_filename (),
618 file_name_style
.style ());
620 uiout
->field_skip ("exec");
622 /* Print extra info that isn't really fit to always present in
623 tabular form. Currently we print the vfork parent/child
624 relationships, if any. */
625 if (inf
->vfork_parent
)
627 uiout
->text (_("\n\tis vfork child of inferior "));
628 uiout
->field_signed ("vfork-parent", inf
->vfork_parent
->num
);
630 if (inf
->vfork_child
)
632 uiout
->text (_("\n\tis vfork parent of inferior "));
633 uiout
->field_signed ("vfork-child", inf
->vfork_child
->num
);
641 detach_inferior_command (const char *args
, int from_tty
)
644 error (_("Requires argument (inferior id(s) to detach)"));
646 scoped_restore_current_thread restore_thread
;
648 number_or_range_parser
parser (args
);
649 while (!parser
.finished ())
651 int num
= parser
.get_number ();
653 inferior
*inf
= find_inferior_id (num
);
656 warning (_("Inferior ID %d not known."), num
);
662 warning (_("Inferior ID %d is not running."), num
);
666 thread_info
*tp
= any_thread_of_inferior (inf
);
669 warning (_("Inferior ID %d has no threads."), num
);
673 switch_to_thread (tp
);
675 detach_command (NULL
, from_tty
);
680 kill_inferior_command (const char *args
, int from_tty
)
683 error (_("Requires argument (inferior id(s) to kill)"));
685 scoped_restore_current_thread restore_thread
;
687 number_or_range_parser
parser (args
);
688 while (!parser
.finished ())
690 int num
= parser
.get_number ();
692 inferior
*inf
= find_inferior_id (num
);
695 warning (_("Inferior ID %d not known."), num
);
701 warning (_("Inferior ID %d is not running."), num
);
705 thread_info
*tp
= any_thread_of_inferior (inf
);
708 warning (_("Inferior ID %d has no threads."), num
);
712 switch_to_thread (tp
);
718 /* See inferior.h. */
721 switch_to_inferior_no_thread (inferior
*inf
)
723 set_current_inferior (inf
);
724 switch_to_no_thread ();
725 set_current_program_space (inf
->pspace
);
728 /* See regcache.h. */
730 std::optional
<scoped_restore_current_thread
>
731 maybe_switch_inferior (inferior
*inf
)
733 std::optional
<scoped_restore_current_thread
> maybe_restore_thread
;
734 if (inf
!= current_inferior ())
736 maybe_restore_thread
.emplace ();
737 switch_to_inferior_no_thread (inf
);
740 return maybe_restore_thread
;
744 inferior_command (const char *args
, int from_tty
)
746 struct inferior
*inf
;
751 inf
= current_inferior ();
752 gdb_assert (inf
!= nullptr);
753 const char *filename
= inf
->pspace
->exec_filename ();
755 if (filename
== nullptr)
756 filename
= _("<noexec>");
758 gdb_printf (_("[Current inferior is %d [%s] (%s)]\n"),
759 inf
->num
, inferior_pid_to_str (inf
->pid
).c_str (),
764 num
= parse_and_eval_long (args
);
766 inf
= find_inferior_id (num
);
768 error (_("Inferior ID %d not known."), num
);
772 if (inf
!= current_inferior ())
774 thread_info
*tp
= any_thread_of_inferior (inf
);
776 error (_("Inferior has no threads."));
778 switch_to_thread (tp
);
781 notify_user_selected_context_changed
782 (USER_SELECTED_INFERIOR
783 | USER_SELECTED_THREAD
784 | USER_SELECTED_FRAME
);
788 switch_to_inferior_no_thread (inf
);
790 notify_user_selected_context_changed
791 (USER_SELECTED_INFERIOR
);
794 /* Switching current inferior may have made one of the inferiors
795 prunable, so prune it. */
800 /* Print information about currently known inferiors. */
803 info_inferiors_command (const char *args
, int from_tty
)
805 print_inferior (current_uiout
, args
);
808 /* remove-inferior ID */
811 remove_inferior_command (const char *args
, int from_tty
)
813 if (args
== NULL
|| *args
== '\0')
814 error (_("Requires an argument (inferior id(s) to remove)"));
816 number_or_range_parser
parser (args
);
817 while (!parser
.finished ())
819 int num
= parser
.get_number ();
820 struct inferior
*inf
= find_inferior_id (num
);
824 warning (_("Inferior ID %d not known."), num
);
828 if (!inf
->deletable ())
830 warning (_("Can not remove current inferior %d."), num
);
836 warning (_("Can not remove active inferior %d."), num
);
840 delete_inferior (inf
);
845 add_inferior_with_spaces (void)
847 struct program_space
*pspace
;
848 struct inferior
*inf
;
850 /* If all inferiors share an address space on this system, this
851 doesn't really return a new address space; otherwise, it
853 pspace
= new program_space (maybe_new_address_space ());
854 inf
= add_inferior (0);
855 inf
->pspace
= pspace
;
856 inf
->aspace
= pspace
->aspace
;
858 /* Setup the inferior's initial arch, based on information obtained
859 from the global "set ..." options. */
861 inf
->set_arch (gdbarch_find_by_info (info
));
862 /* The "set ..." options reject invalid settings, so we should
863 always have a valid arch by now. */
864 gdb_assert (inf
->arch () != nullptr);
869 /* See inferior.h. */
872 switch_to_inferior_and_push_target (inferior
*new_inf
,
873 bool no_connection
, inferior
*org_inf
)
875 process_stratum_target
*proc_target
= org_inf
->process_target ();
877 /* Switch over temporarily, while reading executable and
879 switch_to_inferior_no_thread (new_inf
);
881 /* Reuse the target for new inferior. */
882 if (!no_connection
&& proc_target
!= NULL
)
884 new_inf
->push_target (proc_target
);
885 gdb_printf (_("Added inferior %d on connection %d (%s)\n"),
887 proc_target
->connection_number
,
888 make_target_connection_string (proc_target
).c_str ());
891 gdb_printf (_("Added inferior %d\n"), new_inf
->num
);
894 /* Option values for the "add-inferior" command. */
896 struct add_inferior_opts
898 /* When true the new inferiors are started without a connection. */
899 bool no_connection
= false;
901 /* The number of new inferiors to add. */
902 unsigned int num_copies
= 1;
904 /* When non-empty, this is the executable for the new inferiors. */
905 std::string exec_filename
;
908 /* Option definitions for the "add-inferior" command. */
910 static const gdb::option::option_def add_inferior_option_defs
[] = {
911 gdb::option::uinteger_option_def
<add_inferior_opts
> {
913 [] (add_inferior_opts
*opts
) { return &opts
->num_copies
; },
914 (show_value_ftype
*) nullptr, /* show_cmd_cb */
916 The number of inferiors to add. The default is 1."),
919 gdb::option::filename_option_def
<add_inferior_opts
> {
921 [] (add_inferior_opts
*opts
) { return &opts
->exec_filename
; },
922 nullptr, /* show_cmd_cb */
924 FILENAME is the file name of the executable to use as the\n\
928 gdb::option::flag_option_def
<add_inferior_opts
> {
930 [] (add_inferior_opts
*opts
) { return &opts
->no_connection
; },
932 If specified, the new inferiors begin with no target connection.\n\
933 Without this flag the new inferiors inherit the current inferior's\n\
938 /* Create the option_def_group for the "add-inferior" command. */
940 static inline gdb::option::option_def_group
941 make_add_inferior_options_def_group (add_inferior_opts
*opts
)
943 return {{add_inferior_option_defs
}, opts
};
946 /* Completion for the "add-inferior" command. */
949 add_inferior_completer (struct cmd_list_element
*cmd
,
950 completion_tracker
&tracker
,
951 const char *text
, const char * /* word */)
953 /* The only completion offered is for the command options. */
954 const auto group
= make_add_inferior_options_def_group (nullptr);
955 gdb::option::complete_options
956 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
959 /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
962 add_inferior_command (const char *args
, int from_tty
)
964 add_inferior_opts opts
;
965 const auto group
= make_add_inferior_options_def_group (&opts
);
966 gdb::option::process_options
967 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_ERROR
, group
);
969 /* If an executable was given then perform tilde expansion. */
970 if (!opts
.exec_filename
.empty ())
971 opts
.exec_filename
= gdb_tilde_expand (opts
.exec_filename
);
973 symfile_add_flags add_flags
= 0;
975 add_flags
|= SYMFILE_VERBOSE
;
977 inferior
*orginf
= current_inferior ();
979 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
981 for (unsigned int i
= 0; i
< opts
.num_copies
; ++i
)
983 inferior
*inf
= add_inferior_with_spaces ();
985 switch_to_inferior_and_push_target (inf
, opts
.no_connection
, orginf
);
987 if (!opts
.exec_filename
.empty ())
989 const char *exec
= opts
.exec_filename
.c_str ();
990 exec_file_attach (exec
, from_tty
);
991 symbol_file_add_main (exec
, add_flags
);
996 /* Option values for the "clone-inferior" command. */
998 struct clone_inferior_opts
1000 /* When true the new inferiors are started without a connection. */
1001 bool no_connection
= false;
1003 /* The number of new inferiors to create by cloning. */
1004 unsigned int num_copies
= 1;
1008 /* Option definitions for the "clone-inferior" command. */
1010 static const gdb::option::option_def clone_inferior_option_defs
[] = {
1011 gdb::option::uinteger_option_def
<clone_inferior_opts
> {
1013 [] (clone_inferior_opts
*opts
) { return &opts
->num_copies
; },
1014 (show_value_ftype
*) nullptr, /* show_cmd_cb */
1016 The number of copies of inferior ID to create. The default is 1."),
1019 gdb::option::flag_option_def
<clone_inferior_opts
> {
1021 [] (clone_inferior_opts
*opts
) { return &opts
->no_connection
; },
1023 If specified, the new inferiors begin with no target connection.\n\
1024 Without this flag the new inferiors to inherit the copied inferior's\n\
1029 /* Create the option_def_group for the "clone-inferior" command. */
1031 static inline gdb::option::option_def_group
1032 make_clone_inferior_options_def_group (clone_inferior_opts
*opts
)
1034 return {{clone_inferior_option_defs
}, opts
};
1037 /* Completion for the "clone-inferior" command. */
1040 clone_inferior_completer (struct cmd_list_element
*cmd
,
1041 completion_tracker
&tracker
,
1042 const char *text
, const char * /* word */)
1044 /* The only completion offered is for the command options. */
1045 const auto group
= make_clone_inferior_options_def_group (nullptr);
1046 gdb::option::complete_options
1047 (tracker
, &text
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1050 /* clone-inferior [-copies N] [-no-connection] [ID] */
1053 clone_inferior_command (const char *args
, int from_tty
)
1055 clone_inferior_opts opts
;
1056 const auto group
= make_clone_inferior_options_def_group (&opts
);
1057 gdb::option::process_options
1058 (&args
, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND
, group
);
1060 struct inferior
*orginf
= NULL
;
1061 if (args
!= nullptr && *args
!= '\0')
1063 gdb_argv
argv (args
);
1065 gdb_assert (argv
.count () > 0);
1067 for (const char *arg
: argv
)
1069 if (orginf
== nullptr)
1071 /* The first non-option argument specifies the number of the
1072 inferior to clone. */
1073 int num
= parse_and_eval_long (arg
);
1074 orginf
= find_inferior_id (num
);
1076 if (orginf
== nullptr)
1077 error (_("Inferior ID %d not known."), num
);
1080 error (_("Unexpected argument: %s."), arg
);
1085 /* If no inferior id was specified, then the user wants to clone the
1086 current inferior. */
1087 orginf
= current_inferior ();
1090 gdb_assert (orginf
!= nullptr);
1092 scoped_restore_current_pspace_and_thread restore_pspace_thread
;
1094 for (unsigned int i
= 0; i
< opts
.num_copies
; ++i
)
1096 struct program_space
*pspace
;
1097 struct inferior
*inf
;
1099 /* If all inferiors share an address space on this system, this
1100 doesn't really return a new address space; otherwise, it
1102 pspace
= new program_space (maybe_new_address_space ());
1103 inf
= add_inferior (0);
1104 inf
->pspace
= pspace
;
1105 inf
->aspace
= pspace
->aspace
;
1106 inf
->set_arch (orginf
->arch ());
1108 switch_to_inferior_and_push_target (inf
, opts
.no_connection
, orginf
);
1110 /* If the original inferior had a user specified target
1111 description, make the clone use it too. */
1112 if (inf
->tdesc_info
.from_user_p ())
1113 inf
->tdesc_info
= orginf
->tdesc_info
;
1115 clone_program_space (pspace
, orginf
->pspace
);
1117 /* Copy properties from the original inferior to the new one. */
1118 inf
->set_args (orginf
->args ());
1119 inf
->set_cwd (orginf
->cwd ());
1120 inf
->set_tty (orginf
->tty ());
1121 for (const std::string
&set_var
: orginf
->environment
.user_set_env ())
1123 /* set_var has the form NAME=value. Split on the first '='. */
1124 const std::string::size_type pos
= set_var
.find ('=');
1125 gdb_assert (pos
!= std::string::npos
);
1126 const std::string varname
= set_var
.substr (0, pos
);
1127 inf
->environment
.set
1128 (varname
.c_str (), orginf
->environment
.get (varname
.c_str ()));
1130 for (const std::string
&unset_var
1131 : orginf
->environment
.user_unset_env ())
1132 inf
->environment
.unset (unset_var
.c_str ());
1134 gdb::observers::inferior_cloned
.notify (orginf
, inf
);
1138 /* Print notices when new inferiors are created and die. */
1140 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
1141 struct cmd_list_element
*c
, const char *value
)
1143 gdb_printf (file
, _("Printing of inferior events is %s.\n"), value
);
1146 /* Return a new value for the selected inferior's id. */
1148 static struct value
*
1149 inferior_id_make_value (struct gdbarch
*gdbarch
, struct internalvar
*var
,
1152 struct inferior
*inf
= current_inferior ();
1154 return value_from_longest (builtin_type (gdbarch
)->builtin_int
, inf
->num
);
1157 /* Implementation of `$_inferior' variable. */
1159 static const struct internalvar_funcs inferior_funcs
=
1161 inferior_id_make_value
,
1165 /* See inferior.h. */
1168 initialize_inferiors ()
1170 struct cmd_list_element
*c
= NULL
;
1172 /* There's always one inferior. Note that this function isn't an
1173 automatic _initialize_foo function, since other _initialize_foo
1174 routines may need to install their per-inferior data keys. We
1175 can only allocate an inferior when all those modules have done
1176 that. Do this after initialize_progspace, due to the
1177 current_program_space reference. */
1178 set_current_inferior (add_inferior_silent (0));
1179 current_inferior_
->pspace
= current_program_space
;
1180 current_inferior_
->aspace
= current_program_space
->aspace
;
1181 /* The architecture will be initialized shortly, by
1182 initialize_current_architecture. */
1184 add_info ("inferiors", info_inferiors_command
,
1185 _("Print a list of inferiors being managed.\n\
1186 Usage: info inferiors [ID]...\n\
1187 If IDs are specified, the list is limited to just those inferiors.\n\
1188 By default all inferiors are displayed."));
1190 const auto add_inf_opts
= make_add_inferior_options_def_group (nullptr);
1191 static std::string add_inferior_command_help
1192 = gdb::option::build_help (_("\
1193 Add a new inferior.\n\
1194 Usage: add-inferior [-copies NUMBER] [-exec FILENAME] [-no-connection]\n\
1197 %OPTIONS%"), add_inf_opts
);
1198 c
= add_com ("add-inferior", no_class
, add_inferior_command
,
1199 add_inferior_command_help
.c_str ());
1200 set_cmd_completer_handle_brkchars (c
, add_inferior_completer
);
1202 add_com ("remove-inferiors", no_class
, remove_inferior_command
, _("\
1203 Remove inferior ID (or list of IDs).\n\
1204 Usage: remove-inferiors ID..."));
1206 const auto clone_inf_opts
= make_clone_inferior_options_def_group (nullptr);
1207 static std::string clone_inferior_command_help
1208 = gdb::option::build_help (_("\
1209 Clone an existing inferior.\n\
1210 Usage: clone-inferior [-copies NUMBER] [-no-connection] [ID]\n\
1211 ID is the inferior number to clone, this can be found with the\n\
1212 'info inferiors' command. If no ID is specified, then the current\n\
1213 inferior is cloned.\n\
1216 %OPTIONS%"), clone_inf_opts
);
1217 c
= add_com ("clone-inferior", no_class
, clone_inferior_command
,
1218 clone_inferior_command_help
.c_str ());
1219 set_cmd_completer_handle_brkchars (c
, clone_inferior_completer
);
1221 add_cmd ("inferiors", class_run
, detach_inferior_command
, _("\
1222 Detach from inferior ID (or list of IDS).\n\
1223 Usage; detach inferiors ID..."),
1226 add_cmd ("inferiors", class_run
, kill_inferior_command
, _("\
1227 Kill inferior ID (or list of IDs).\n\
1228 Usage: kill inferiors ID..."),
1231 add_cmd ("inferior", class_run
, inferior_command
, _("\
1232 Use this command to switch between inferiors.\n\
1233 Usage: inferior ID\n\
1234 The new inferior ID must be currently known."),
1237 add_setshow_boolean_cmd ("inferior-events", no_class
,
1238 &print_inferior_events
, _("\
1239 Set printing of inferior events (such as inferior start and exit)."), _("\
1240 Show printing of inferior events (such as inferior start and exit)."), NULL
,
1242 show_print_inferior_events
,
1243 &setprintlist
, &showprintlist
);
1245 create_internalvar_type_lazy ("_inferior", &inferior_funcs
, NULL
);