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