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