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