Move 64-bit BFD files from ALL_TARGET_OBS to ALL_64_TARGET_OBS
[binutils-gdb.git] / gdb / inferior.c
1 /* Multi-process control for GDB, the GNU debugger.
2
3 Copyright (C) 2008-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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 "readline/tilde.h"
37 #include "progspace-and-thread.h"
38 #include "gdbsupport/buildargv.h"
39 #include "cli/cli-style.h"
40
41 /* Keep a registry of per-inferior data-pointers required by other GDB
42 modules. */
43
44 DEFINE_REGISTRY (inferior, REGISTRY_ACCESS_FIELD)
45
46 intrusive_list<inferior> inferior_list;
47 static int highest_inferior_num;
48
49 /* See inferior.h. */
50 bool print_inferior_events = true;
51
52 /* The Current Inferior. This is a strong reference. I.e., whenever
53 an inferior is the current inferior, its refcount is
54 incremented. */
55 static inferior_ref current_inferior_;
56
57 struct inferior*
58 current_inferior (void)
59 {
60 return current_inferior_.get ();
61 }
62
63 void
64 set_current_inferior (struct inferior *inf)
65 {
66 /* There's always an inferior. */
67 gdb_assert (inf != NULL);
68
69 current_inferior_ = inferior_ref::new_reference (inf);
70 }
71
72 private_inferior::~private_inferior () = default;
73
74 inferior::~inferior ()
75 {
76 inferior *inf = this;
77
78 m_continuations.clear ();
79 inferior_free_data (inf);
80 target_desc_info_free (inf->tdesc_info);
81 }
82
83 inferior::inferior (int pid_)
84 : num (++highest_inferior_num),
85 pid (pid_),
86 environment (gdb_environ::from_host_environ ()),
87 registry_data ()
88 {
89 inferior_alloc_data (this);
90
91 m_target_stack.push (get_dummy_target ());
92 }
93
94 /* See inferior.h. */
95
96 int
97 inferior::unpush_target (struct target_ops *t)
98 {
99 /* If unpushing the process stratum target from the inferior while threads
100 exist in the inferior, ensure that we don't leave any threads of the
101 inferior in the target's "resumed with pending wait status" list.
102
103 See also the comment in set_thread_exited. */
104 if (t->stratum () == process_stratum)
105 {
106 process_stratum_target *proc_target = as_process_stratum_target (t);
107
108 for (thread_info *thread : this->non_exited_threads ())
109 proc_target->maybe_remove_resumed_with_pending_wait_status (thread);
110 }
111
112 return m_target_stack.unpush (t);
113 }
114
115 void
116 inferior::set_tty (std::string terminal_name)
117 {
118 m_terminal = std::move (terminal_name);
119 }
120
121 const std::string &
122 inferior::tty ()
123 {
124 return m_terminal;
125 }
126
127 void
128 inferior::add_continuation (std::function<void ()> &&cont)
129 {
130 m_continuations.emplace_front (std::move (cont));
131 }
132
133 void
134 inferior::do_all_continuations ()
135 {
136 while (!m_continuations.empty ())
137 {
138 auto iter = m_continuations.begin ();
139 (*iter) ();
140 m_continuations.erase (iter);
141 }
142 }
143
144 struct inferior *
145 add_inferior_silent (int pid)
146 {
147 inferior *inf = new inferior (pid);
148
149 inferior_list.push_back (*inf);
150
151 gdb::observers::inferior_added.notify (inf);
152
153 if (pid != 0)
154 inferior_appeared (inf, pid);
155
156 return inf;
157 }
158
159 struct inferior *
160 add_inferior (int pid)
161 {
162 struct inferior *inf = add_inferior_silent (pid);
163
164 if (print_inferior_events)
165 {
166 if (pid != 0)
167 gdb_printf (_("[New inferior %d (%s)]\n"),
168 inf->num,
169 target_pid_to_str (ptid_t (pid)).c_str ());
170 else
171 gdb_printf (_("[New inferior %d]\n"), inf->num);
172 }
173
174 return inf;
175 }
176
177 /* See inferior.h. */
178
179 void
180 inferior::clear_thread_list (bool silent)
181 {
182 thread_list.clear_and_dispose ([=] (thread_info *thr)
183 {
184 threads_debug_printf ("deleting thread %s, silent = %d",
185 thr->ptid.to_string ().c_str (), silent);
186 set_thread_exited (thr, silent);
187 if (thr->deletable ())
188 delete thr;
189 });
190 ptid_thread_map.clear ();
191 }
192
193 void
194 delete_inferior (struct inferior *inf)
195 {
196 inf->clear_thread_list (true);
197
198 auto it = inferior_list.iterator_to (*inf);
199 inferior_list.erase (it);
200
201 gdb::observers::inferior_removed.notify (inf);
202
203 /* If this program space is rendered useless, remove it. */
204 if (inf->pspace->empty ())
205 delete inf->pspace;
206
207 delete inf;
208 }
209
210 /* If SILENT then be quiet -- don't announce a inferior exit, or the
211 exit of its threads. */
212
213 static void
214 exit_inferior_1 (struct inferior *inf, int silent)
215 {
216 inf->clear_thread_list (silent);
217
218 gdb::observers::inferior_exit.notify (inf);
219
220 inf->pid = 0;
221 inf->fake_pid_p = false;
222 inf->priv = NULL;
223
224 if (inf->vfork_parent != NULL)
225 {
226 inf->vfork_parent->vfork_child = NULL;
227 inf->vfork_parent = NULL;
228 }
229 if (inf->vfork_child != NULL)
230 {
231 inf->vfork_child->vfork_parent = NULL;
232 inf->vfork_child = NULL;
233 }
234
235 inf->pending_detach = 0;
236 /* Reset it. */
237 inf->control = inferior_control_state (NO_STOP_QUIETLY);
238
239 /* Clear the register cache and the frame cache. */
240 registers_changed ();
241 reinit_frame_cache ();
242 }
243
244 void
245 exit_inferior (inferior *inf)
246 {
247 exit_inferior_1 (inf, 0);
248 }
249
250 void
251 exit_inferior_silent (inferior *inf)
252 {
253 exit_inferior_1 (inf, 1);
254 }
255
256 /* See inferior.h. */
257
258 void
259 detach_inferior (inferior *inf)
260 {
261 /* Save the pid, since exit_inferior_1 will reset it. */
262 int pid = inf->pid;
263
264 exit_inferior_1 (inf, 0);
265
266 if (print_inferior_events)
267 gdb_printf (_("[Inferior %d (%s) detached]\n"),
268 inf->num,
269 target_pid_to_str (ptid_t (pid)).c_str ());
270 }
271
272 void
273 inferior_appeared (struct inferior *inf, int pid)
274 {
275 /* If this is the first inferior with threads, reset the global
276 thread id. */
277 delete_exited_threads ();
278 if (!any_thread_p ())
279 init_thread_list ();
280
281 inf->pid = pid;
282 inf->has_exit_code = 0;
283 inf->exit_code = 0;
284
285 gdb::observers::inferior_appeared.notify (inf);
286 }
287
288 struct inferior *
289 find_inferior_id (int num)
290 {
291 for (inferior *inf : all_inferiors ())
292 if (inf->num == num)
293 return inf;
294
295 return NULL;
296 }
297
298 struct inferior *
299 find_inferior_pid (process_stratum_target *targ, int pid)
300 {
301 /* Looking for inferior pid == 0 is always wrong, and indicative of
302 a bug somewhere else. There may be more than one with pid == 0,
303 for instance. */
304 gdb_assert (pid != 0);
305
306 for (inferior *inf : all_inferiors (targ))
307 if (inf->pid == pid)
308 return inf;
309
310 return NULL;
311 }
312
313 /* See inferior.h */
314
315 struct inferior *
316 find_inferior_ptid (process_stratum_target *targ, ptid_t ptid)
317 {
318 return find_inferior_pid (targ, ptid.pid ());
319 }
320
321 /* See inferior.h. */
322
323 struct inferior *
324 find_inferior_for_program_space (struct program_space *pspace)
325 {
326 struct inferior *cur_inf = current_inferior ();
327
328 if (cur_inf->pspace == pspace)
329 return cur_inf;
330
331 for (inferior *inf : all_inferiors ())
332 if (inf->pspace == pspace)
333 return inf;
334
335 return NULL;
336 }
337
338 int
339 have_inferiors (void)
340 {
341 for (inferior *inf ATTRIBUTE_UNUSED : all_non_exited_inferiors ())
342 return 1;
343
344 return 0;
345 }
346
347 /* Return the number of live inferiors. We account for the case
348 where an inferior might have a non-zero pid but no threads, as
349 in the middle of a 'mourn' operation. */
350
351 int
352 number_of_live_inferiors (process_stratum_target *proc_target)
353 {
354 int num_inf = 0;
355
356 for (inferior *inf : all_non_exited_inferiors (proc_target))
357 if (inf->has_execution ())
358 for (thread_info *tp ATTRIBUTE_UNUSED : inf->non_exited_threads ())
359 {
360 /* Found a live thread in this inferior, go to the next
361 inferior. */
362 ++num_inf;
363 break;
364 }
365
366 return num_inf;
367 }
368
369 /* Return true if there is at least one live inferior. */
370
371 int
372 have_live_inferiors (void)
373 {
374 return number_of_live_inferiors (NULL) > 0;
375 }
376
377 /* Prune away any unused inferiors, and then prune away no longer used
378 program spaces. */
379
380 void
381 prune_inferiors (void)
382 {
383 for (inferior *inf : all_inferiors_safe ())
384 {
385 if (!inf->deletable ()
386 || !inf->removable
387 || inf->pid != 0)
388 continue;
389
390 delete_inferior (inf);
391 }
392 }
393
394 /* Simply returns the count of inferiors. */
395
396 int
397 number_of_inferiors (void)
398 {
399 auto rng = all_inferiors ();
400 return std::distance (rng.begin (), rng.end ());
401 }
402
403 /* Converts an inferior process id to a string. Like
404 target_pid_to_str, but special cases the null process. */
405
406 static std::string
407 inferior_pid_to_str (int pid)
408 {
409 if (pid != 0)
410 return target_pid_to_str (ptid_t (pid));
411 else
412 return _("<null>");
413 }
414
415 /* See inferior.h. */
416
417 void
418 print_selected_inferior (struct ui_out *uiout)
419 {
420 struct inferior *inf = current_inferior ();
421 const char *filename = inf->pspace->exec_filename.get ();
422
423 if (filename == NULL)
424 filename = _("<noexec>");
425
426 uiout->message (_("[Switching to inferior %d [%s] (%s)]\n"),
427 inf->num, inferior_pid_to_str (inf->pid).c_str (), filename);
428 }
429
430 /* Helper for print_inferior. Returns the 'connection-id' string for
431 PROC_TARGET. */
432
433 static std::string
434 uiout_field_connection (process_stratum_target *proc_target)
435 {
436 if (proc_target == NULL)
437 {
438 return {};
439 }
440 else if (proc_target->connection_string () != NULL)
441 {
442 return string_printf ("%d (%s %s)",
443 proc_target->connection_number,
444 proc_target->shortname (),
445 proc_target->connection_string ());
446 }
447 else
448 {
449 return string_printf ("%d (%s)",
450 proc_target->connection_number,
451 proc_target->shortname ());
452 }
453 }
454
455 /* Prints the list of inferiors and their details on UIOUT. This is a
456 version of 'info_inferior_command' suitable for use from MI.
457
458 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
459 inferiors that should be printed. Otherwise, all inferiors are
460 printed. */
461
462 static void
463 print_inferior (struct ui_out *uiout, const char *requested_inferiors)
464 {
465 int inf_count = 0;
466 size_t connection_id_len = 20;
467
468 /* Compute number of inferiors we will print. */
469 for (inferior *inf : all_inferiors ())
470 {
471 if (!number_is_in_list (requested_inferiors, inf->num))
472 continue;
473
474 std::string conn = uiout_field_connection (inf->process_target ());
475 if (connection_id_len < conn.size ())
476 connection_id_len = conn.size ();
477
478 ++inf_count;
479 }
480
481 if (inf_count == 0)
482 {
483 uiout->message ("No inferiors.\n");
484 return;
485 }
486
487 ui_out_emit_table table_emitter (uiout, 5, inf_count, "inferiors");
488 uiout->table_header (1, ui_left, "current", "");
489 uiout->table_header (4, ui_left, "number", "Num");
490 uiout->table_header (17, ui_left, "target-id", "Description");
491 uiout->table_header (connection_id_len, ui_left,
492 "connection-id", "Connection");
493 uiout->table_header (17, ui_left, "exec", "Executable");
494
495 uiout->table_body ();
496
497 /* Restore the current thread after the loop because we switch the
498 inferior in the loop. */
499 scoped_restore_current_pspace_and_thread restore_pspace_thread;
500 inferior *current_inf = current_inferior ();
501 for (inferior *inf : all_inferiors ())
502 {
503 if (!number_is_in_list (requested_inferiors, inf->num))
504 continue;
505
506 ui_out_emit_tuple tuple_emitter (uiout, NULL);
507
508 if (inf == current_inf)
509 uiout->field_string ("current", "*");
510 else
511 uiout->field_skip ("current");
512
513 uiout->field_signed ("number", inf->num);
514
515 /* Because target_pid_to_str uses the current inferior,
516 switch the inferior. */
517 switch_to_inferior_no_thread (inf);
518
519 uiout->field_string ("target-id", inferior_pid_to_str (inf->pid));
520
521 std::string conn = uiout_field_connection (inf->process_target ());
522 uiout->field_string ("connection-id", conn);
523
524 if (inf->pspace->exec_filename != nullptr)
525 uiout->field_string ("exec", inf->pspace->exec_filename.get (),
526 file_name_style.style ());
527 else
528 uiout->field_skip ("exec");
529
530 /* Print extra info that isn't really fit to always present in
531 tabular form. Currently we print the vfork parent/child
532 relationships, if any. */
533 if (inf->vfork_parent)
534 {
535 uiout->text (_("\n\tis vfork child of inferior "));
536 uiout->field_signed ("vfork-parent", inf->vfork_parent->num);
537 }
538 if (inf->vfork_child)
539 {
540 uiout->text (_("\n\tis vfork parent of inferior "));
541 uiout->field_signed ("vfork-child", inf->vfork_child->num);
542 }
543
544 uiout->text ("\n");
545 }
546 }
547
548 static void
549 detach_inferior_command (const char *args, int from_tty)
550 {
551 if (!args || !*args)
552 error (_("Requires argument (inferior id(s) to detach)"));
553
554 scoped_restore_current_thread restore_thread;
555
556 number_or_range_parser parser (args);
557 while (!parser.finished ())
558 {
559 int num = parser.get_number ();
560
561 inferior *inf = find_inferior_id (num);
562 if (inf == NULL)
563 {
564 warning (_("Inferior ID %d not known."), num);
565 continue;
566 }
567
568 if (inf->pid == 0)
569 {
570 warning (_("Inferior ID %d is not running."), num);
571 continue;
572 }
573
574 thread_info *tp = any_thread_of_inferior (inf);
575 if (tp == NULL)
576 {
577 warning (_("Inferior ID %d has no threads."), num);
578 continue;
579 }
580
581 switch_to_thread (tp);
582
583 detach_command (NULL, from_tty);
584 }
585 }
586
587 static void
588 kill_inferior_command (const char *args, int from_tty)
589 {
590 if (!args || !*args)
591 error (_("Requires argument (inferior id(s) to kill)"));
592
593 scoped_restore_current_thread restore_thread;
594
595 number_or_range_parser parser (args);
596 while (!parser.finished ())
597 {
598 int num = parser.get_number ();
599
600 inferior *inf = find_inferior_id (num);
601 if (inf == NULL)
602 {
603 warning (_("Inferior ID %d not known."), num);
604 continue;
605 }
606
607 if (inf->pid == 0)
608 {
609 warning (_("Inferior ID %d is not running."), num);
610 continue;
611 }
612
613 thread_info *tp = any_thread_of_inferior (inf);
614 if (tp == NULL)
615 {
616 warning (_("Inferior ID %d has no threads."), num);
617 continue;
618 }
619
620 switch_to_thread (tp);
621
622 target_kill ();
623 }
624
625 bfd_cache_close_all ();
626 }
627
628 /* See inferior.h. */
629
630 void
631 switch_to_inferior_no_thread (inferior *inf)
632 {
633 set_current_inferior (inf);
634 switch_to_no_thread ();
635 set_current_program_space (inf->pspace);
636 }
637
638 static void
639 inferior_command (const char *args, int from_tty)
640 {
641 struct inferior *inf;
642 int num;
643
644 if (args == nullptr)
645 {
646 inf = current_inferior ();
647 gdb_assert (inf != nullptr);
648 const char *filename = inf->pspace->exec_filename.get ();
649
650 if (filename == nullptr)
651 filename = _("<noexec>");
652
653 gdb_printf (_("[Current inferior is %d [%s] (%s)]\n"),
654 inf->num, inferior_pid_to_str (inf->pid).c_str (),
655 filename);
656 }
657 else
658 {
659 num = parse_and_eval_long (args);
660
661 inf = find_inferior_id (num);
662 if (inf == NULL)
663 error (_("Inferior ID %d not known."), num);
664
665 if (inf->pid != 0)
666 {
667 if (inf != current_inferior ())
668 {
669 thread_info *tp = any_thread_of_inferior (inf);
670 if (tp == NULL)
671 error (_("Inferior has no threads."));
672
673 switch_to_thread (tp);
674 }
675
676 gdb::observers::user_selected_context_changed.notify
677 (USER_SELECTED_INFERIOR
678 | USER_SELECTED_THREAD
679 | USER_SELECTED_FRAME);
680 }
681 else
682 {
683 switch_to_inferior_no_thread (inf);
684
685 gdb::observers::user_selected_context_changed.notify
686 (USER_SELECTED_INFERIOR);
687 }
688 }
689 }
690
691 /* Print information about currently known inferiors. */
692
693 static void
694 info_inferiors_command (const char *args, int from_tty)
695 {
696 print_inferior (current_uiout, args);
697 }
698
699 /* remove-inferior ID */
700
701 static void
702 remove_inferior_command (const char *args, int from_tty)
703 {
704 if (args == NULL || *args == '\0')
705 error (_("Requires an argument (inferior id(s) to remove)"));
706
707 number_or_range_parser parser (args);
708 while (!parser.finished ())
709 {
710 int num = parser.get_number ();
711 struct inferior *inf = find_inferior_id (num);
712
713 if (inf == NULL)
714 {
715 warning (_("Inferior ID %d not known."), num);
716 continue;
717 }
718
719 if (!inf->deletable ())
720 {
721 warning (_("Can not remove current inferior %d."), num);
722 continue;
723 }
724
725 if (inf->pid != 0)
726 {
727 warning (_("Can not remove active inferior %d."), num);
728 continue;
729 }
730
731 delete_inferior (inf);
732 }
733 }
734
735 struct inferior *
736 add_inferior_with_spaces (void)
737 {
738 struct address_space *aspace;
739 struct program_space *pspace;
740 struct inferior *inf;
741
742 /* If all inferiors share an address space on this system, this
743 doesn't really return a new address space; otherwise, it
744 really does. */
745 aspace = maybe_new_address_space ();
746 pspace = new program_space (aspace);
747 inf = add_inferior (0);
748 inf->pspace = pspace;
749 inf->aspace = pspace->aspace;
750
751 /* Setup the inferior's initial arch, based on information obtained
752 from the global "set ..." options. */
753 gdbarch_info info;
754 inf->gdbarch = gdbarch_find_by_info (info);
755 /* The "set ..." options reject invalid settings, so we should
756 always have a valid arch by now. */
757 gdb_assert (inf->gdbarch != NULL);
758
759 return inf;
760 }
761
762 /* See inferior.h. */
763
764 void
765 switch_to_inferior_and_push_target (inferior *new_inf,
766 bool no_connection, inferior *org_inf)
767 {
768 process_stratum_target *proc_target = org_inf->process_target ();
769
770 /* Switch over temporarily, while reading executable and
771 symbols. */
772 switch_to_inferior_no_thread (new_inf);
773
774 /* Reuse the target for new inferior. */
775 if (!no_connection && proc_target != NULL)
776 {
777 new_inf->push_target (proc_target);
778 if (proc_target->connection_string () != NULL)
779 gdb_printf (_("Added inferior %d on connection %d (%s %s)\n"),
780 new_inf->num,
781 proc_target->connection_number,
782 proc_target->shortname (),
783 proc_target->connection_string ());
784 else
785 gdb_printf (_("Added inferior %d on connection %d (%s)\n"),
786 new_inf->num,
787 proc_target->connection_number,
788 proc_target->shortname ());
789 }
790 else
791 gdb_printf (_("Added inferior %d\n"), new_inf->num);
792 }
793
794 /* add-inferior [-copies N] [-exec FILENAME] [-no-connection] */
795
796 static void
797 add_inferior_command (const char *args, int from_tty)
798 {
799 int i, copies = 1;
800 gdb::unique_xmalloc_ptr<char> exec;
801 symfile_add_flags add_flags = 0;
802 bool no_connection = false;
803
804 if (from_tty)
805 add_flags |= SYMFILE_VERBOSE;
806
807 if (args)
808 {
809 gdb_argv built_argv (args);
810
811 for (char **argv = built_argv.get (); *argv != NULL; argv++)
812 {
813 if (**argv == '-')
814 {
815 if (strcmp (*argv, "-copies") == 0)
816 {
817 ++argv;
818 if (!*argv)
819 error (_("No argument to -copies"));
820 copies = parse_and_eval_long (*argv);
821 }
822 else if (strcmp (*argv, "-no-connection") == 0)
823 no_connection = true;
824 else if (strcmp (*argv, "-exec") == 0)
825 {
826 ++argv;
827 if (!*argv)
828 error (_("No argument to -exec"));
829 exec.reset (tilde_expand (*argv));
830 }
831 }
832 else
833 error (_("Invalid argument"));
834 }
835 }
836
837 inferior *orginf = current_inferior ();
838
839 scoped_restore_current_pspace_and_thread restore_pspace_thread;
840
841 for (i = 0; i < copies; ++i)
842 {
843 inferior *inf = add_inferior_with_spaces ();
844
845 switch_to_inferior_and_push_target (inf, no_connection, orginf);
846
847 if (exec != NULL)
848 {
849 exec_file_attach (exec.get (), from_tty);
850 symbol_file_add_main (exec.get (), add_flags);
851 }
852 }
853 }
854
855 /* clone-inferior [-copies N] [ID] [-no-connection] */
856
857 static void
858 clone_inferior_command (const char *args, int from_tty)
859 {
860 int i, copies = 1;
861 struct inferior *orginf = NULL;
862 bool no_connection = false;
863
864 if (args)
865 {
866 gdb_argv built_argv (args);
867
868 char **argv = built_argv.get ();
869 for (; *argv != NULL; argv++)
870 {
871 if (**argv == '-')
872 {
873 if (strcmp (*argv, "-copies") == 0)
874 {
875 ++argv;
876 if (!*argv)
877 error (_("No argument to -copies"));
878 copies = parse_and_eval_long (*argv);
879
880 if (copies < 0)
881 error (_("Invalid copies number"));
882 }
883 else if (strcmp (*argv, "-no-connection") == 0)
884 no_connection = true;
885 }
886 else
887 {
888 if (orginf == NULL)
889 {
890 int num;
891
892 /* The first non-option (-) argument specified the
893 program space ID. */
894 num = parse_and_eval_long (*argv);
895 orginf = find_inferior_id (num);
896
897 if (orginf == NULL)
898 error (_("Inferior ID %d not known."), num);
899 continue;
900 }
901 else
902 error (_("Invalid argument"));
903 }
904 }
905 }
906
907 /* If no inferior id was specified, then the user wants to clone the
908 current inferior. */
909 if (orginf == NULL)
910 orginf = current_inferior ();
911
912 scoped_restore_current_pspace_and_thread restore_pspace_thread;
913
914 for (i = 0; i < copies; ++i)
915 {
916 struct address_space *aspace;
917 struct program_space *pspace;
918 struct inferior *inf;
919
920 /* If all inferiors share an address space on this system, this
921 doesn't really return a new address space; otherwise, it
922 really does. */
923 aspace = maybe_new_address_space ();
924 pspace = new program_space (aspace);
925 inf = add_inferior (0);
926 inf->pspace = pspace;
927 inf->aspace = pspace->aspace;
928 inf->gdbarch = orginf->gdbarch;
929
930 switch_to_inferior_and_push_target (inf, no_connection, orginf);
931
932 /* If the original inferior had a user specified target
933 description, make the clone use it too. */
934 if (target_desc_info_from_user_p (inf->tdesc_info))
935 copy_inferior_target_desc_info (inf, orginf);
936
937 clone_program_space (pspace, orginf->pspace);
938
939 /* Copy properties from the original inferior to the new one. */
940 inf->set_args (orginf->args ());
941 inf->set_cwd (orginf->cwd ());
942 inf->set_tty (orginf->tty ());
943 for (const std::string &set_var : orginf->environment.user_set_env ())
944 {
945 /* set_var has the form NAME=value. Split on the first '='. */
946 const std::string::size_type pos = set_var.find ('=');
947 gdb_assert (pos != std::string::npos);
948 const std::string varname = set_var.substr (0, pos);
949 inf->environment.set
950 (varname.c_str (), orginf->environment.get (varname.c_str ()));
951 }
952 for (const std::string &unset_var
953 : orginf->environment.user_unset_env ())
954 inf->environment.unset (unset_var.c_str ());
955 }
956 }
957
958 /* Print notices when new inferiors are created and die. */
959 static void
960 show_print_inferior_events (struct ui_file *file, int from_tty,
961 struct cmd_list_element *c, const char *value)
962 {
963 gdb_printf (file, _("Printing of inferior events is %s.\n"), value);
964 }
965
966 /* Return a new value for the selected inferior's id. */
967
968 static struct value *
969 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
970 void *ignore)
971 {
972 struct inferior *inf = current_inferior ();
973
974 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
975 }
976
977 /* Implementation of `$_inferior' variable. */
978
979 static const struct internalvar_funcs inferior_funcs =
980 {
981 inferior_id_make_value,
982 NULL,
983 };
984
985 \f
986
987 void
988 initialize_inferiors (void)
989 {
990 struct cmd_list_element *c = NULL;
991
992 /* There's always one inferior. Note that this function isn't an
993 automatic _initialize_foo function, since other _initialize_foo
994 routines may need to install their per-inferior data keys. We
995 can only allocate an inferior when all those modules have done
996 that. Do this after initialize_progspace, due to the
997 current_program_space reference. */
998 set_current_inferior (add_inferior_silent (0));
999 current_inferior_->pspace = current_program_space;
1000 current_inferior_->aspace = current_program_space->aspace;
1001 /* The architecture will be initialized shortly, by
1002 initialize_current_architecture. */
1003
1004 add_info ("inferiors", info_inferiors_command,
1005 _("Print a list of inferiors being managed.\n\
1006 Usage: info inferiors [ID]...\n\
1007 If IDs are specified, the list is limited to just those inferiors.\n\
1008 By default all inferiors are displayed."));
1009
1010 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1011 Add a new inferior.\n\
1012 Usage: add-inferior [-copies N] [-exec FILENAME] [-no-connection]\n\
1013 N is the optional number of inferiors to add, default is 1.\n\
1014 FILENAME is the file name of the executable to use\n\
1015 as main program.\n\
1016 By default, the new inferior inherits the current inferior's connection.\n\
1017 If -no-connection is specified, the new inferior begins with\n\
1018 no target connection yet."));
1019 set_cmd_completer (c, filename_completer);
1020
1021 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1022 Remove inferior ID (or list of IDs).\n\
1023 Usage: remove-inferiors ID..."));
1024
1025 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1026 Clone inferior ID.\n\
1027 Usage: clone-inferior [-copies N] [-no-connection] [ID]\n\
1028 Add N copies of inferior ID. The new inferiors have the same\n\
1029 executable loaded as the copied inferior. If -copies is not specified,\n\
1030 adds 1 copy. If ID is not specified, it is the current inferior\n\
1031 that is cloned.\n\
1032 By default, the new inferiors inherit the copied inferior's connection.\n\
1033 If -no-connection is specified, the new inferiors begin with\n\
1034 no target connection yet."));
1035
1036 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1037 Detach from inferior ID (or list of IDS).\n\
1038 Usage; detach inferiors ID..."),
1039 &detachlist);
1040
1041 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1042 Kill inferior ID (or list of IDs).\n\
1043 Usage: kill inferiors ID..."),
1044 &killlist);
1045
1046 add_cmd ("inferior", class_run, inferior_command, _("\
1047 Use this command to switch between inferiors.\n\
1048 Usage: inferior ID\n\
1049 The new inferior ID must be currently known."),
1050 &cmdlist);
1051
1052 add_setshow_boolean_cmd ("inferior-events", no_class,
1053 &print_inferior_events, _("\
1054 Set printing of inferior events (such as inferior start and exit)."), _("\
1055 Show printing of inferior events (such as inferior start and exit)."), NULL,
1056 NULL,
1057 show_print_inferior_events,
1058 &setprintlist, &showprintlist);
1059
1060 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);
1061 }