Automatic date update in version.in
[binutils-gdb.git] / gdb / inferior.c
blob5ff5eb9895529bfcb64d7a90507b63ff5a5047a4
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/>. */
20 #include "defs.h"
21 #include "exec.h"
22 #include "inferior.h"
23 #include "target.h"
24 #include "command.h"
25 #include "completer.h"
26 #include "gdbcmd.h"
27 #include "gdbthread.h"
28 #include "ui-out.h"
29 #include "observable.h"
30 #include "gdbcore.h"
31 #include "symfile.h"
32 #include "gdbsupport/environ.h"
33 #include "cli/cli-utils.h"
34 #include "arch-utils.h"
35 #include "target-descriptions.h"
36 #include "target-connection.h"
37 #include "readline/tilde.h"
38 #include "progspace-and-thread.h"
39 #include "gdbsupport/buildargv.h"
40 #include "cli/cli-style.h"
41 #include "interps.h"
43 intrusive_list<inferior> inferior_list;
44 static int highest_inferior_num;
46 /* See inferior.h. */
47 bool print_inferior_events = true;
49 /* The Current Inferior. This is a strong reference. I.e., whenever
50 an inferior is the current inferior, its refcount is
51 incremented. */
52 static inferior_ref current_inferior_;
54 struct inferior*
55 current_inferior (void)
57 return current_inferior_.get ();
60 void
61 set_current_inferior (struct inferior *inf)
63 /* There's always an inferior. */
64 gdb_assert (inf != NULL);
66 current_inferior_ = inferior_ref::new_reference (inf);
69 private_inferior::~private_inferior () = default;
71 inferior::~inferior ()
73 /* Before the inferior is deleted, all target_ops should be popped from
74 the target stack, this leaves just the dummy_target behind. If this
75 is not done, then any target left in the target stack will be left
76 with an artificially high reference count. As the dummy_target is
77 still on the target stack then we are about to loose a reference to
78 that target, leaving its reference count artificially high. However,
79 this is not critical as the dummy_target is a singleton. */
80 gdb_assert (m_target_stack.top ()->stratum () == dummy_stratum);
82 m_continuations.clear ();
85 inferior::inferior (int pid_)
86 : num (++highest_inferior_num),
87 pid (pid_),
88 environment (gdb_environ::from_host_environ ())
90 m_target_stack.push (get_dummy_target ());
93 /* See inferior.h. */
95 int
96 inferior::unpush_target (struct target_ops *t)
98 /* If unpushing the process stratum target from the inferior while threads
99 exist in the inferior, ensure that we don't leave any threads of the
100 inferior in the target's "resumed with pending wait status" list.
102 See also the comment in set_thread_exited. */
103 if (t->stratum () == process_stratum)
105 process_stratum_target *proc_target = as_process_stratum_target (t);
107 for (thread_info *thread : this->non_exited_threads ())
108 proc_target->maybe_remove_resumed_with_pending_wait_status (thread);
111 return m_target_stack.unpush (t);
114 /* See inferior.h. */
116 void
117 inferior::unpush_target_and_assert (struct target_ops *target)
119 gdb_assert (current_inferior () == this);
121 if (!unpush_target (target))
122 internal_error ("pop_all_targets couldn't find target %s\n",
123 target->shortname ());
126 /* See inferior.h. */
128 void
129 inferior::pop_all_targets_above (enum strata stratum)
131 /* Unpushing a target might cause it to close. Some targets currently
132 rely on the current_inferior being set for their ::close method, so we
133 temporarily switch inferior now. */
134 scoped_restore_current_pspace_and_thread restore_pspace_and_thread;
135 switch_to_inferior_no_thread (this);
137 while (top_target ()->stratum () > stratum)
138 unpush_target_and_assert (top_target ());
141 /* See inferior.h. */
143 void
144 inferior::pop_all_targets_at_and_above (enum strata stratum)
146 /* Unpushing a target might cause it to close. Some targets currently
147 rely on the current_inferior being set for their ::close method, so we
148 temporarily switch inferior now. */
149 scoped_restore_current_pspace_and_thread restore_pspace_and_thread;
150 switch_to_inferior_no_thread (this);
152 while (top_target ()->stratum () >= stratum)
153 unpush_target_and_assert (top_target ());
156 void
157 inferior::set_tty (std::string terminal_name)
159 m_terminal = std::move (terminal_name);
162 const std::string &
163 inferior::tty ()
165 return m_terminal;
168 /* See inferior.h. */
170 void
171 inferior::set_args (gdb::array_view<char * const> args)
173 set_args (construct_inferior_arguments (args));
176 void
177 inferior::set_arch (gdbarch *arch)
179 gdb_assert (arch != nullptr);
180 gdb_assert (gdbarch_initialized_p (arch));
181 m_gdbarch = arch;
183 process_stratum_target *proc_target = this->process_target ();
184 if (proc_target != nullptr)
185 registers_changed_ptid (proc_target, ptid_t (this->pid));
188 void
189 inferior::add_continuation (std::function<void ()> &&cont)
191 m_continuations.emplace_front (std::move (cont));
194 void
195 inferior::do_all_continuations ()
197 while (!m_continuations.empty ())
199 auto iter = m_continuations.begin ();
200 (*iter) ();
201 m_continuations.erase (iter);
205 /* Notify interpreters and observers that inferior INF was added. */
207 static void
208 notify_inferior_added (inferior *inf)
210 interps_notify_inferior_added (inf);
211 gdb::observers::inferior_added.notify (inf);
214 struct inferior *
215 add_inferior_silent (int pid)
217 inferior *inf = new inferior (pid);
219 inferior_list.push_back (*inf);
221 notify_inferior_added (inf);
223 if (pid != 0)
224 inferior_appeared (inf, pid);
226 return inf;
229 struct inferior *
230 add_inferior (int pid)
232 struct inferior *inf = add_inferior_silent (pid);
234 if (print_inferior_events)
236 if (pid != 0)
237 gdb_printf (_("[New inferior %d (%s)]\n"),
238 inf->num,
239 target_pid_to_str (ptid_t (pid)).c_str ());
240 else
241 gdb_printf (_("[New inferior %d]\n"), inf->num);
244 return inf;
247 /* See inferior.h. */
249 thread_info *
250 inferior::find_thread (ptid_t ptid)
252 auto it = this->ptid_thread_map.find (ptid);
253 if (it != this->ptid_thread_map.end ())
254 return it->second;
255 else
256 return nullptr;
259 /* See inferior.h. */
261 void
262 inferior::clear_thread_list ()
264 thread_list.clear_and_dispose ([=] (thread_info *thr)
266 threads_debug_printf ("deleting thread %s",
267 thr->ptid.to_string ().c_str ());
268 set_thread_exited (thr, {}, true /* silent */);
269 if (thr->deletable ())
270 delete thr;
272 ptid_thread_map.clear ();
275 /* Notify interpreters and observers that inferior INF was removed. */
277 static void
278 notify_inferior_removed (inferior *inf)
280 interps_notify_inferior_removed (inf);
281 gdb::observers::inferior_removed.notify (inf);
284 void
285 delete_inferior (struct inferior *inf)
287 inf->clear_thread_list ();
289 auto it = inferior_list.iterator_to (*inf);
290 inferior_list.erase (it);
292 notify_inferior_removed (inf);
294 /* Pop all targets now, this ensures that inferior::unpush is called
295 correctly. As pop_all_targets ends up making a temporary switch to
296 inferior INF then we need to make this call before we delete the
297 program space, which we do below. */
298 inf->pop_all_targets ();
300 /* If this program space is rendered useless, remove it. */
301 if (inf->pspace->empty ())
302 delete inf->pspace;
304 delete inf;
307 /* Notify interpreters and observers that inferior INF disappeared. */
309 static void
310 notify_inferior_disappeared (inferior *inf)
312 interps_notify_inferior_disappeared (inf);
313 gdb::observers::inferior_exit.notify (inf);
316 /* See inferior.h. */
318 void
319 exit_inferior (struct inferior *inf)
321 inf->clear_thread_list ();
323 notify_inferior_disappeared (inf);
325 inf->pid = 0;
326 inf->fake_pid_p = false;
327 inf->priv = NULL;
329 if (inf->vfork_parent != NULL)
331 inf->vfork_parent->vfork_child = NULL;
332 inf->vfork_parent = NULL;
334 if (inf->vfork_child != NULL)
336 inf->vfork_child->vfork_parent = NULL;
337 inf->vfork_child = NULL;
340 inf->pending_detach = false;
341 /* Reset it. */
342 inf->control = inferior_control_state (NO_STOP_QUIETLY);
344 /* Clear the register cache and the frame cache. */
345 registers_changed ();
346 reinit_frame_cache ();
349 /* See inferior.h. */
351 void
352 detach_inferior (inferior *inf)
354 /* Save the pid, since exit_inferior will reset it. */
355 int pid = inf->pid;
357 exit_inferior (inf);
359 if (print_inferior_events)
360 gdb_printf (_("[Inferior %d (%s) detached]\n"),
361 inf->num,
362 target_pid_to_str (ptid_t (pid)).c_str ());
365 /* Notify interpreters and observers that inferior INF appeared. */
367 static void
368 notify_inferior_appeared (inferior *inf)
370 interps_notify_inferior_appeared (inf);
371 gdb::observers::inferior_appeared.notify (inf);
374 void
375 inferior_appeared (struct inferior *inf, int pid)
377 /* If this is the first inferior with threads, reset the global
378 thread id. */
379 delete_exited_threads ();
380 if (!any_thread_p ())
381 init_thread_list ();
383 inf->pid = pid;
384 inf->has_exit_code = false;
385 inf->exit_code = 0;
387 notify_inferior_appeared (inf);
390 struct inferior *
391 find_inferior_id (int num)
393 for (inferior *inf : all_inferiors ())
394 if (inf->num == num)
395 return inf;
397 return NULL;
400 struct inferior *
401 find_inferior_pid (process_stratum_target *targ, int pid)
403 /* Looking for inferior pid == 0 is always wrong, and indicative of
404 a bug somewhere else. There may be more than one with pid == 0,
405 for instance. */
406 gdb_assert (pid != 0);
408 for (inferior *inf : all_inferiors (targ))
409 if (inf->pid == pid)
410 return inf;
412 return NULL;
415 /* See inferior.h */
417 struct inferior *
418 find_inferior_ptid (process_stratum_target *targ, ptid_t ptid)
420 return find_inferior_pid (targ, ptid.pid ());
423 /* See inferior.h. */
425 struct inferior *
426 find_inferior_for_program_space (struct program_space *pspace)
428 struct inferior *cur_inf = current_inferior ();
430 if (cur_inf->pspace == pspace)
431 return cur_inf;
433 for (inferior *inf : all_inferiors ())
434 if (inf->pspace == pspace)
435 return inf;
437 return NULL;
441 have_inferiors (void)
443 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
444 return 1;
446 return 0;
449 /* Return the number of live inferiors. We account for the case
450 where an inferior might have a non-zero pid but no threads, as
451 in the middle of a 'mourn' operation. */
454 number_of_live_inferiors (process_stratum_target *proc_target)
456 int num_inf = 0;
458 for (inferior *inf : all_non_exited_inferiors (proc_target))
459 if (inf->has_execution ())
460 for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
462 /* Found a live thread in this inferior, go to the next
463 inferior. */
464 ++num_inf;
465 break;
468 return num_inf;
471 /* Return true if there is at least one live inferior. */
474 have_live_inferiors (void)
476 return number_of_live_inferiors (NULL) > 0;
479 /* Prune away any unused inferiors, and then prune away no longer used
480 program spaces. */
482 void
483 prune_inferiors (void)
485 for (inferior *inf : all_inferiors_safe ())
487 if (!inf->deletable ()
488 || !inf->removable
489 || inf->pid != 0)
490 continue;
492 delete_inferior (inf);
496 /* Simply returns the count of inferiors. */
499 number_of_inferiors (void)
501 auto rng = all_inferiors ();
502 return std::distance (rng.begin (), rng.end ());
505 /* Converts an inferior process id to a string. Like
506 target_pid_to_str, but special cases the null process. */
508 static std::string
509 inferior_pid_to_str (int pid)
511 if (pid != 0)
512 return target_pid_to_str (ptid_t (pid));
513 else
514 return _("<null>");
517 /* See inferior.h. */
519 void
520 print_selected_inferior (struct ui_out *uiout)
522 struct inferior *inf = current_inferior ();
523 const char *filename = inf->pspace->exec_filename.get ();
525 if (filename == NULL)
526 filename = _("<noexec>");
528 uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"),
529 inf->num, inferior_pid_to_str (inf->pid).c_str (), filename);
532 /* Helper for print_inferior. Returns the 'connection-id' string for
533 PROC_TARGET. */
535 static std::string
536 uiout_field_connection (process_stratum_target *proc_target)
538 if (proc_target == NULL)
539 return {};
540 else
542 std::string conn_str = make_target_connection_string (proc_target);
543 return string_printf ("%d (%s)", proc_target->connection_number,
544 conn_str.c_str ());
548 /* Prints the list of inferiors and their details on UIOUT. This is a
549 version of 'info_inferior_command' suitable for use from MI.
551 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
552 inferiors that should be printed. Otherwise, all inferiors are
553 printed. */
555 static void
556 print_inferior (struct ui_out *uiout, const char *requested_inferiors)
558 int inf_count = 0;
559 size_t connection_id_len = 20;
561 /* Compute number of inferiors we will print. */
562 for (inferior *inf : all_inferiors ())
564 if (!number_is_in_list (requested_inferiors, inf->num))
565 continue;
567 std::string conn = uiout_field_connection (inf->process_target ());
568 if (connection_id_len < conn.size ())
569 connection_id_len = conn.size ();
571 ++inf_count;
574 if (inf_count == 0)
576 uiout->message ("No inferiors.\n");
577 return;
580 ui_out_emit_table table_emitter (uiout, 5, inf_count, "inferiors");
581 uiout->table_header (1, ui_left, "current", "");
582 uiout->table_header (4, ui_left, "number", "Num");
583 uiout->table_header (17, ui_left, "target-id", "Description");
584 uiout->table_header (connection_id_len, ui_left,
585 "connection-id", "Connection");
586 uiout->table_header (17, ui_left, "exec", "Executable");
588 uiout->table_body ();
590 /* Restore the current thread after the loop because we switch the
591 inferior in the loop. */
592 scoped_restore_current_pspace_and_thread restore_pspace_thread;
593 inferior *current_inf = current_inferior ();
594 for (inferior *inf : all_inferiors ())
596 if (!number_is_in_list (requested_inferiors, inf->num))
597 continue;
599 ui_out_emit_tuple tuple_emitter (uiout, NULL);
601 if (inf == current_inf)
602 uiout->field_string ("current", "*");
603 else
604 uiout->field_skip ("current");
606 uiout->field_signed ("number", inf->num);
608 /* Because target_pid_to_str uses the current inferior,
609 switch the inferior. */
610 switch_to_inferior_no_thread (inf);
612 uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
614 std::string conn = uiout_field_connection (inf->process_target ());
615 uiout->field_string ("connection-id", conn);
617 if (inf->pspace->exec_filename != nullptr)
618 uiout->field_string ("exec", inf->pspace->exec_filename.get (),
619 file_name_style.style ());
620 else
621 uiout->field_skip ("exec");
623 /* Print extra info that isn't really fit to always present in
624 tabular form. Currently we print the vfork parent/child
625 relationships, if any. */
626 if (inf->vfork_parent)
628 uiout->text (_("\n\tis vfork child of inferior "));
629 uiout->field_signed ("vfork-parent", inf->vfork_parent->num);
631 if (inf->vfork_child)
633 uiout->text (_("\n\tis vfork parent of inferior "));
634 uiout->field_signed ("vfork-child", inf->vfork_child->num);
637 uiout->text ("\n");
641 static void
642 detach_inferior_command (const char *args, int from_tty)
644 if (!args || !*args)
645 error (_("Requires argument (inferior id(s) to detach)"));
647 scoped_restore_current_thread restore_thread;
649 number_or_range_parser parser (args);
650 while (!parser.finished ())
652 int num = parser.get_number ();
654 inferior *inf = find_inferior_id (num);
655 if (inf == NULL)
657 warning (_("Inferior ID %d not known."), num);
658 continue;
661 if (inf->pid == 0)
663 warning (_("Inferior ID %d is not running."), num);
664 continue;
667 thread_info *tp = any_thread_of_inferior (inf);
668 if (tp == NULL)
670 warning (_("Inferior ID %d has no threads."), num);
671 continue;
674 switch_to_thread (tp);
676 detach_command (NULL, from_tty);
680 static void
681 kill_inferior_command (const char *args, int from_tty)
683 if (!args || !*args)
684 error (_("Requires argument (inferior id(s) to kill)"));
686 scoped_restore_current_thread restore_thread;
688 number_or_range_parser parser (args);
689 while (!parser.finished ())
691 int num = parser.get_number ();
693 inferior *inf = find_inferior_id (num);
694 if (inf == NULL)
696 warning (_("Inferior ID %d not known."), num);
697 continue;
700 if (inf->pid == 0)
702 warning (_("Inferior ID %d is not running."), num);
703 continue;
706 thread_info *tp = any_thread_of_inferior (inf);
707 if (tp == NULL)
709 warning (_("Inferior ID %d has no threads."), num);
710 continue;
713 switch_to_thread (tp);
715 target_kill ();
719 /* See inferior.h. */
721 void
722 switch_to_inferior_no_thread (inferior *inf)
724 set_current_inferior (inf);
725 switch_to_no_thread ();
726 set_current_program_space (inf->pspace);
729 /* See regcache.h. */
731 std::optional<scoped_restore_current_thread>
732 maybe_switch_inferior (inferior *inf)
734 std::optional<scoped_restore_current_thread> maybe_restore_thread;
735 if (inf != current_inferior ())
737 maybe_restore_thread.emplace ();
738 switch_to_inferior_no_thread (inf);
741 return maybe_restore_thread;
744 static void
745 inferior_command (const char *args, int from_tty)
747 struct inferior *inf;
748 int num;
750 if (args == nullptr)
752 inf = current_inferior ();
753 gdb_assert (inf != nullptr);
754 const char *filename = inf->pspace->exec_filename.get ();
756 if (filename == nullptr)
757 filename = _("<noexec>");
759 gdb_printf (_("[Current inferior is %d [%s] (%s)]\n"),
760 inf->num, inferior_pid_to_str (inf->pid).c_str (),
761 filename);
763 else
765 num = parse_and_eval_long (args);
767 inf = find_inferior_id (num);
768 if (inf == NULL)
769 error (_("Inferior ID %d not known."), num);
771 if (inf->pid != 0)
773 if (inf != current_inferior ())
775 thread_info *tp = any_thread_of_inferior (inf);
776 if (tp == NULL)
777 error (_("Inferior has no threads."));
779 switch_to_thread (tp);
782 notify_user_selected_context_changed
783 (USER_SELECTED_INFERIOR
784 | USER_SELECTED_THREAD
785 | USER_SELECTED_FRAME);
787 else
789 switch_to_inferior_no_thread (inf);
791 notify_user_selected_context_changed
792 (USER_SELECTED_INFERIOR);
797 /* Print information about currently known inferiors. */
799 static void
800 info_inferiors_command (const char *args, int from_tty)
802 print_inferior (current_uiout, args);
805 /* remove-inferior ID */
807 static void
808 remove_inferior_command (const char *args, int from_tty)
810 if (args == NULL || *args == '\0')
811 error (_("Requires an argument (inferior id(s) to remove)"));
813 number_or_range_parser parser (args);
814 while (!parser.finished ())
816 int num = parser.get_number ();
817 struct inferior *inf = find_inferior_id (num);
819 if (inf == NULL)
821 warning (_("Inferior ID %d not known."), num);
822 continue;
825 if (!inf->deletable ())
827 warning (_("Can not remove current inferior %d."), num);
828 continue;
831 if (inf->pid != 0)
833 warning (_("Can not remove active inferior %d."), num);
834 continue;
837 delete_inferior (inf);
841 struct inferior *
842 add_inferior_with_spaces (void)
844 struct program_space *pspace;
845 struct inferior *inf;
847 /* If all inferiors share an address space on this system, this
848 doesn't really return a new address space; otherwise, it
849 really does. */
850 pspace = new program_space (maybe_new_address_space ());
851 inf = add_inferior (0);
852 inf->pspace = pspace;
853 inf->aspace = pspace->aspace;
855 /* Setup the inferior's initial arch, based on information obtained
856 from the global "set ..." options. */
857 gdbarch_info info;
858 inf->set_arch (gdbarch_find_by_info (info));
859 /* The "set ..." options reject invalid settings, so we should
860 always have a valid arch by now. */
861 gdb_assert (inf->arch () != nullptr);
863 return inf;
866 /* See inferior.h. */
868 void
869 switch_to_inferior_and_push_target (inferior *new_inf,
870 bool no_connection, inferior *org_inf)
872 process_stratum_target *proc_target = org_inf->process_target ();
874 /* Switch over temporarily, while reading executable and
875 symbols. */
876 switch_to_inferior_no_thread (new_inf);
878 /* Reuse the target for new inferior. */
879 if (!no_connection && proc_target != NULL)
881 new_inf->push_target (proc_target);
882 gdb_printf (_("Added inferior %d on connection %d (%s)\n"),
883 new_inf->num,
884 proc_target->connection_number,
885 make_target_connection_string (proc_target).c_str ());
887 else
888 gdb_printf (_("Added inferior %d\n"), new_inf->num);
891 /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
893 static void
894 add_inferior_command (const char *args, int from_tty)
896 int i, copies = 1;
897 gdb::unique_xmalloc_ptr<char> exec;
898 symfile_add_flags add_flags = 0;
899 bool no_connection = false;
901 if (from_tty)
902 add_flags |= SYMFILE_VERBOSE;
904 if (args)
906 gdb_argv built_argv (args);
908 for (char **argv = built_argv.get (); *argv != NULL; argv++)
910 if (**argv == '-')
912 if (strcmp (*argv, "-copies") == 0)
914 ++argv;
915 if (!*argv)
916 error (_("No argument to -copies"));
917 copies = parse_and_eval_long (*argv);
919 else if (strcmp (*argv, "-no-connection") == 0)
920 no_connection = true;
921 else if (strcmp (*argv, "-exec") == 0)
923 ++argv;
924 if (!*argv)
925 error (_("No argument to -exec"));
926 exec.reset (tilde_expand (*argv));
929 else
930 error (_("Invalid argument"));
934 inferior *orginf = current_inferior ();
936 scoped_restore_current_pspace_and_thread restore_pspace_thread;
938 for (i = 0; i < copies; ++i)
940 inferior *inf = add_inferior_with_spaces ();
942 switch_to_inferior_and_push_target (inf, no_connection, orginf);
944 if (exec != NULL)
946 exec_file_attach (exec.get (), from_tty);
947 symbol_file_add_main (exec.get (), add_flags);
952 /* clone-inferior [-copies N] [ID] [-no-connection] */
954 static void
955 clone_inferior_command (const char *args, int from_tty)
957 int i, copies = 1;
958 struct inferior *orginf = NULL;
959 bool no_connection = false;
961 if (args)
963 gdb_argv built_argv (args);
965 char **argv = built_argv.get ();
966 for (; *argv != NULL; argv++)
968 if (**argv == '-')
970 if (strcmp (*argv, "-copies") == 0)
972 ++argv;
973 if (!*argv)
974 error (_("No argument to -copies"));
975 copies = parse_and_eval_long (*argv);
977 if (copies < 0)
978 error (_("Invalid copies number"));
980 else if (strcmp (*argv, "-no-connection") == 0)
981 no_connection = true;
983 else
985 if (orginf == NULL)
987 int num;
989 /* The first non-option (-) argument specified the
990 program space ID. */
991 num = parse_and_eval_long (*argv);
992 orginf = find_inferior_id (num);
994 if (orginf == NULL)
995 error (_("Inferior ID %d not known."), num);
996 continue;
998 else
999 error (_("Invalid argument"));
1004 /* If no inferior id was specified, then the user wants to clone the
1005 current inferior. */
1006 if (orginf == NULL)
1007 orginf = current_inferior ();
1009 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1011 for (i = 0; i < copies; ++i)
1013 struct program_space *pspace;
1014 struct inferior *inf;
1016 /* If all inferiors share an address space on this system, this
1017 doesn't really return a new address space; otherwise, it
1018 really does. */
1019 pspace = new program_space (maybe_new_address_space ());
1020 inf = add_inferior (0);
1021 inf->pspace = pspace;
1022 inf->aspace = pspace->aspace;
1023 inf->set_arch (orginf->arch ());
1025 switch_to_inferior_and_push_target (inf, no_connection, orginf);
1027 /* If the original inferior had a user specified target
1028 description, make the clone use it too. */
1029 if (inf->tdesc_info.from_user_p ())
1030 inf->tdesc_info = orginf->tdesc_info;
1032 clone_program_space (pspace, orginf->pspace);
1034 /* Copy properties from the original inferior to the new one. */
1035 inf->set_args (orginf->args ());
1036 inf->set_cwd (orginf->cwd ());
1037 inf->set_tty (orginf->tty ());
1038 for (const std::string &set_var : orginf->environment.user_set_env ())
1040 /* set_var has the form NAME=value. Split on the first '='. */
1041 const std::string::size_type pos = set_var.find ('=');
1042 gdb_assert (pos != std::string::npos);
1043 const std::string varname = set_var.substr (0, pos);
1044 inf->environment.set
1045 (varname.c_str (), orginf->environment.get (varname.c_str ()));
1047 for (const std::string &unset_var
1048 : orginf->environment.user_unset_env ())
1049 inf->environment.unset (unset_var.c_str ());
1051 gdb::observers::inferior_cloned.notify (orginf, inf);
1055 /* Print notices when new inferiors are created and die. */
1056 static void
1057 show_print_inferior_events (struct ui_file *file, int from_tty,
1058 struct cmd_list_element *c, const char *value)
1060 gdb_printf (file, _("Printing of inferior events is %s.\n"), value);
1063 /* Return a new value for the selected inferior's id. */
1065 static struct value *
1066 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1067 void *ignore)
1069 struct inferior *inf = current_inferior ();
1071 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
1074 /* Implementation of `$_inferior' variable. */
1076 static const struct internalvar_funcs inferior_funcs =
1078 inferior_id_make_value,
1079 NULL,
1084 void
1085 initialize_inferiors (void)
1087 struct cmd_list_element *c = NULL;
1089 /* There's always one inferior. Note that this function isn't an
1090 automatic _initialize_foo function, since other _initialize_foo
1091 routines may need to install their per-inferior data keys. We
1092 can only allocate an inferior when all those modules have done
1093 that. Do this after initialize_progspace, due to the
1094 current_program_space reference. */
1095 set_current_inferior (add_inferior_silent (0));
1096 current_inferior_->pspace = current_program_space;
1097 current_inferior_->aspace = current_program_space->aspace;
1098 /* The architecture will be initialized shortly, by
1099 initialize_current_architecture. */
1101 add_info ("inferiors", info_inferiors_command,
1102 _("Print a list of inferiors being managed.\n\
1103 Usage: info inferiors [ID]...\n\
1104 If IDs are specified, the list is limited to just those inferiors.\n\
1105 By default all inferiors are displayed."));
1107 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1108 Add a new inferior.\n\
1109 Usage: add-inferior [-copies N] [-exec FILENAME] [-no-connection]\n\
1110 N is the optional number of inferiors to add, default is 1.\n\
1111 FILENAME is the file name of the executable to use\n\
1112 as main program.\n\
1113 By default, the new inferior inherits the current inferior's connection.\n\
1114 If -no-connection is specified, the new inferior begins with\n\
1115 no target connection yet."));
1116 set_cmd_completer (c, filename_completer);
1118 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1119 Remove inferior ID (or list of IDs).\n\
1120 Usage: remove-inferiors ID..."));
1122 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1123 Clone inferior ID.\n\
1124 Usage: clone-inferior [-copies N] [-no-connection] [ID]\n\
1125 Add N copies of inferior ID. The new inferiors have the same\n\
1126 executable loaded as the copied inferior. If -copies is not specified,\n\
1127 adds 1 copy. If ID is not specified, it is the current inferior\n\
1128 that is cloned.\n\
1129 By default, the new inferiors inherit the copied inferior's connection.\n\
1130 If -no-connection is specified, the new inferiors begin with\n\
1131 no target connection yet."));
1133 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1134 Detach from inferior ID (or list of IDS).\n\
1135 Usage; detach inferiors ID..."),
1136 &detachlist);
1138 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1139 Kill inferior ID (or list of IDs).\n\
1140 Usage: kill inferiors ID..."),
1141 &killlist);
1143 add_cmd ("inferior", class_run, inferior_command, _("\
1144 Use this command to switch between inferiors.\n\
1145 Usage: inferior ID\n\
1146 The new inferior ID must be currently known."),
1147 &cmdlist);
1149 add_setshow_boolean_cmd ("inferior-events", no_class,
1150 &print_inferior_events, _("\
1151 Set printing of inferior events (such as inferior start and exit)."), _("\
1152 Show printing of inferior events (such as inferior start and exit)."), NULL,
1153 NULL,
1154 show_print_inferior_events,
1155 &setprintlist, &showprintlist);
1157 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);