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