Remove unnecessary null_cleanup
[deliverable/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 observer_notify_inferior_exit (inf);
253
254 inf->pid = 0;
255 inf->fake_pid_p = 0;
256 if (inf->vfork_parent != NULL)
257 {
258 inf->vfork_parent->vfork_child = NULL;
259 inf->vfork_parent = NULL;
260 }
261 if (inf->vfork_child != NULL)
262 {
263 inf->vfork_child->vfork_parent = NULL;
264 inf->vfork_child = NULL;
265 }
266
267 inf->pending_detach = 0;
268 }
269
270 void
271 exit_inferior (int pid)
272 {
273 struct inferior *inf = find_inferior_pid (pid);
274
275 exit_inferior_1 (inf, 0);
276
277 if (print_inferior_events)
278 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
279 }
280
281 void
282 exit_inferior_silent (int pid)
283 {
284 struct inferior *inf = find_inferior_pid (pid);
285
286 exit_inferior_1 (inf, 1);
287 }
288
289 void
290 exit_inferior_num_silent (int num)
291 {
292 struct inferior *inf = find_inferior_id (num);
293
294 exit_inferior_1 (inf, 1);
295 }
296
297 void
298 detach_inferior (int pid)
299 {
300 struct inferior *inf = find_inferior_pid (pid);
301
302 exit_inferior_1 (inf, 0);
303
304 if (print_inferior_events)
305 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
306 }
307
308 void
309 inferior_appeared (struct inferior *inf, int pid)
310 {
311 inf->pid = pid;
312 inf->has_exit_code = 0;
313 inf->exit_code = 0;
314
315 observer_notify_inferior_appeared (inf);
316 }
317
318 void
319 discard_all_inferiors (void)
320 {
321 struct inferior *inf;
322
323 for (inf = inferior_list; inf; inf = inf->next)
324 {
325 if (inf->pid != 0)
326 exit_inferior_silent (inf->pid);
327 }
328 }
329
330 struct inferior *
331 find_inferior_id (int num)
332 {
333 struct inferior *inf;
334
335 for (inf = inferior_list; inf; inf = inf->next)
336 if (inf->num == num)
337 return inf;
338
339 return NULL;
340 }
341
342 struct inferior *
343 find_inferior_pid (int pid)
344 {
345 struct inferior *inf;
346
347 /* Looking for inferior pid == 0 is always wrong, and indicative of
348 a bug somewhere else. There may be more than one with pid == 0,
349 for instance. */
350 gdb_assert (pid != 0);
351
352 for (inf = inferior_list; inf; inf = inf->next)
353 if (inf->pid == pid)
354 return inf;
355
356 return NULL;
357 }
358
359 /* See inferior.h */
360
361 struct inferior *
362 find_inferior_ptid (ptid_t ptid)
363 {
364 return find_inferior_pid (ptid_get_pid (ptid));
365 }
366
367 /* See inferior.h. */
368
369 struct inferior *
370 find_inferior_for_program_space (struct program_space *pspace)
371 {
372 struct inferior *inf = current_inferior ();
373
374 if (inf->pspace == pspace)
375 return inf;
376
377 for (inf = inferior_list; inf != NULL; inf = inf->next)
378 {
379 if (inf->pspace == pspace)
380 return inf;
381 }
382
383 return NULL;
384 }
385
386 struct inferior *
387 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
388 void *data)
389 {
390 struct inferior *inf, *infnext;
391
392 for (inf = inferior_list; inf; inf = infnext)
393 {
394 infnext = inf->next;
395 if ((*callback) (inf, data))
396 return inf;
397 }
398
399 return NULL;
400 }
401
402 int
403 valid_gdb_inferior_id (int num)
404 {
405 struct inferior *inf;
406
407 for (inf = inferior_list; inf; inf = inf->next)
408 if (inf->num == num)
409 return 1;
410
411 return 0;
412 }
413
414 int
415 pid_to_gdb_inferior_id (int pid)
416 {
417 struct inferior *inf;
418
419 for (inf = inferior_list; inf; inf = inf->next)
420 if (inf->pid == pid)
421 return inf->num;
422
423 return 0;
424 }
425
426 int
427 gdb_inferior_id_to_pid (int num)
428 {
429 struct inferior *inferior = find_inferior_id (num);
430 if (inferior)
431 return inferior->pid;
432 else
433 return -1;
434 }
435
436 int
437 in_inferior_list (int pid)
438 {
439 struct inferior *inf;
440
441 for (inf = inferior_list; inf; inf = inf->next)
442 if (inf->pid == pid)
443 return 1;
444
445 return 0;
446 }
447
448 int
449 have_inferiors (void)
450 {
451 struct inferior *inf;
452
453 for (inf = inferior_list; inf; inf = inf->next)
454 if (inf->pid != 0)
455 return 1;
456
457 return 0;
458 }
459
460 /* Return the number of live inferiors. We account for the case
461 where an inferior might have a non-zero pid but no threads, as
462 in the middle of a 'mourn' operation. */
463
464 int
465 number_of_live_inferiors (void)
466 {
467 struct inferior *inf;
468 int num_inf = 0;
469
470 for (inf = inferior_list; inf; inf = inf->next)
471 if (inf->pid != 0)
472 {
473 struct thread_info *tp;
474
475 ALL_NON_EXITED_THREADS (tp)
476 if (tp && ptid_get_pid (tp->ptid) == inf->pid)
477 if (target_has_execution_1 (tp->ptid))
478 {
479 /* Found a live thread in this inferior, go to the next
480 inferior. */
481 ++num_inf;
482 break;
483 }
484 }
485
486 return num_inf;
487 }
488
489 /* Return true if there is at least one live inferior. */
490
491 int
492 have_live_inferiors (void)
493 {
494 return number_of_live_inferiors () > 0;
495 }
496
497 /* Prune away any unused inferiors, and then prune away no longer used
498 program spaces. */
499
500 void
501 prune_inferiors (void)
502 {
503 struct inferior *ss, **ss_link;
504 struct inferior *current = current_inferior ();
505
506 ss = inferior_list;
507 ss_link = &inferior_list;
508 while (ss)
509 {
510 if (ss == current
511 || !ss->removable
512 || ss->pid != 0)
513 {
514 ss_link = &ss->next;
515 ss = *ss_link;
516 continue;
517 }
518
519 *ss_link = ss->next;
520 delete_inferior (ss);
521 ss = *ss_link;
522 }
523 }
524
525 /* Simply returns the count of inferiors. */
526
527 int
528 number_of_inferiors (void)
529 {
530 struct inferior *inf;
531 int count = 0;
532
533 for (inf = inferior_list; inf != NULL; inf = inf->next)
534 count++;
535
536 return count;
537 }
538
539 /* Converts an inferior process id to a string. Like
540 target_pid_to_str, but special cases the null process. */
541
542 static char *
543 inferior_pid_to_str (int pid)
544 {
545 if (pid != 0)
546 return target_pid_to_str (pid_to_ptid (pid));
547 else
548 return _("<null>");
549 }
550
551 /* See inferior.h. */
552
553 void
554 print_selected_inferior (struct ui_out *uiout)
555 {
556 char buf[PATH_MAX + 256];
557 struct inferior *inf = current_inferior ();
558
559 xsnprintf (buf, sizeof (buf),
560 _("[Switching to inferior %d [%s] (%s)]\n"),
561 inf->num,
562 inferior_pid_to_str (inf->pid),
563 (inf->pspace->pspace_exec_filename != NULL
564 ? inf->pspace->pspace_exec_filename
565 : _("<noexec>")));
566 ui_out_text (uiout, buf);
567 }
568
569 /* Prints the list of inferiors and their details on UIOUT. This is a
570 version of 'info_inferior_command' suitable for use from MI.
571
572 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
573 inferiors that should be printed. Otherwise, all inferiors are
574 printed. */
575
576 static void
577 print_inferior (struct ui_out *uiout, char *requested_inferiors)
578 {
579 struct inferior *inf;
580 struct cleanup *old_chain;
581 int inf_count = 0;
582
583 /* Compute number of inferiors we will print. */
584 for (inf = inferior_list; inf; inf = inf->next)
585 {
586 if (!number_is_in_list (requested_inferiors, inf->num))
587 continue;
588
589 ++inf_count;
590 }
591
592 if (inf_count == 0)
593 {
594 ui_out_message (uiout, 0, "No inferiors.\n");
595 return;
596 }
597
598 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
599 "inferiors");
600 ui_out_table_header (uiout, 1, ui_left, "current", "");
601 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
602 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
603 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
604
605 ui_out_table_body (uiout);
606 for (inf = inferior_list; inf; inf = inf->next)
607 {
608 struct cleanup *chain2;
609
610 if (!number_is_in_list (requested_inferiors, inf->num))
611 continue;
612
613 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
614
615 if (inf == current_inferior ())
616 ui_out_field_string (uiout, "current", "*");
617 else
618 ui_out_field_skip (uiout, "current");
619
620 ui_out_field_int (uiout, "number", inf->num);
621
622 ui_out_field_string (uiout, "target-id",
623 inferior_pid_to_str (inf->pid));
624
625 if (inf->pspace->pspace_exec_filename != NULL)
626 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
627 else
628 ui_out_field_skip (uiout, "exec");
629
630 /* Print extra info that isn't really fit to always present in
631 tabular form. Currently we print the vfork parent/child
632 relationships, if any. */
633 if (inf->vfork_parent)
634 {
635 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
636 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
637 }
638 if (inf->vfork_child)
639 {
640 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
641 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
642 }
643
644 ui_out_text (uiout, "\n");
645 do_cleanups (chain2);
646 }
647
648 do_cleanups (old_chain);
649 }
650
651 static void
652 detach_inferior_command (char *args, int from_tty)
653 {
654 struct thread_info *tp;
655
656 if (!args || !*args)
657 error (_("Requires argument (inferior id(s) to detach)"));
658
659 number_or_range_parser parser (args);
660 while (!parser.finished ())
661 {
662 int num = parser.get_number ();
663
664 if (!valid_gdb_inferior_id (num))
665 {
666 warning (_("Inferior ID %d not known."), num);
667 continue;
668 }
669
670 int pid = gdb_inferior_id_to_pid (num);
671 if (pid == 0)
672 {
673 warning (_("Inferior ID %d is not running."), num);
674 continue;
675 }
676
677 tp = any_thread_of_process (pid);
678 if (!tp)
679 {
680 warning (_("Inferior ID %d has no threads."), num);
681 continue;
682 }
683
684 switch_to_thread (tp->ptid);
685
686 detach_command (NULL, from_tty);
687 }
688 }
689
690 static void
691 kill_inferior_command (char *args, int from_tty)
692 {
693 struct thread_info *tp;
694
695 if (!args || !*args)
696 error (_("Requires argument (inferior id(s) to kill)"));
697
698 number_or_range_parser parser (args);
699 while (!parser.finished ())
700 {
701 int num = parser.get_number ();
702
703 if (!valid_gdb_inferior_id (num))
704 {
705 warning (_("Inferior ID %d not known."), num);
706 continue;
707 }
708
709 int pid = gdb_inferior_id_to_pid (num);
710 if (pid == 0)
711 {
712 warning (_("Inferior ID %d is not running."), num);
713 continue;
714 }
715
716 tp = any_thread_of_process (pid);
717 if (!tp)
718 {
719 warning (_("Inferior ID %d has no threads."), num);
720 continue;
721 }
722
723 switch_to_thread (tp->ptid);
724
725 target_kill ();
726 }
727
728 bfd_cache_close_all ();
729 }
730
731 static void
732 inferior_command (char *args, int from_tty)
733 {
734 struct inferior *inf;
735 int num;
736
737 num = parse_and_eval_long (args);
738
739 inf = find_inferior_id (num);
740 if (inf == NULL)
741 error (_("Inferior ID %d not known."), num);
742
743 if (inf->pid != 0)
744 {
745 if (inf->pid != ptid_get_pid (inferior_ptid))
746 {
747 struct thread_info *tp;
748
749 tp = any_thread_of_process (inf->pid);
750 if (!tp)
751 error (_("Inferior has no threads."));
752
753 switch_to_thread (tp->ptid);
754 }
755
756 observer_notify_user_selected_context_changed
757 (USER_SELECTED_INFERIOR
758 | USER_SELECTED_THREAD
759 | USER_SELECTED_FRAME);
760 }
761 else
762 {
763 struct inferior *inf;
764
765 inf = find_inferior_id (num);
766 set_current_inferior (inf);
767 switch_to_thread (null_ptid);
768 set_current_program_space (inf->pspace);
769
770 observer_notify_user_selected_context_changed (USER_SELECTED_INFERIOR);
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 if (args == NULL || *args == '\0')
788 error (_("Requires an argument (inferior id(s) to remove)"));
789
790 number_or_range_parser parser (args);
791 while (!parser.finished ())
792 {
793 int num = parser.get_number ();
794 struct inferior *inf = find_inferior_id (num);
795
796 if (inf == NULL)
797 {
798 warning (_("Inferior ID %d not known."), num);
799 continue;
800 }
801
802 if (inf == current_inferior ())
803 {
804 warning (_("Can not remove current inferior %d."), num);
805 continue;
806 }
807
808 if (inf->pid != 0)
809 {
810 warning (_("Can not remove active inferior %d."), num);
811 continue;
812 }
813
814 delete_inferior (inf);
815 }
816 }
817
818 struct inferior *
819 add_inferior_with_spaces (void)
820 {
821 struct address_space *aspace;
822 struct program_space *pspace;
823 struct inferior *inf;
824 struct gdbarch_info info;
825
826 /* If all inferiors share an address space on this system, this
827 doesn't really return a new address space; otherwise, it
828 really does. */
829 aspace = maybe_new_address_space ();
830 pspace = add_program_space (aspace);
831 inf = add_inferior (0);
832 inf->pspace = pspace;
833 inf->aspace = pspace->aspace;
834
835 /* Setup the inferior's initial arch, based on information obtained
836 from the global "set ..." options. */
837 gdbarch_info_init (&info);
838 inf->gdbarch = gdbarch_find_by_info (info);
839 /* The "set ..." options reject invalid settings, so we should
840 always have a valid arch by now. */
841 gdb_assert (inf->gdbarch != NULL);
842
843 return inf;
844 }
845
846 /* add-inferior [-copies N] [-exec FILENAME] */
847
848 static void
849 add_inferior_command (char *args, int from_tty)
850 {
851 int i, copies = 1;
852 char *exec = NULL;
853 char **argv;
854 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
855
856 if (args)
857 {
858 argv = gdb_buildargv (args);
859 make_cleanup_freeargv (argv);
860
861 for (; *argv != NULL; argv++)
862 {
863 if (**argv == '-')
864 {
865 if (strcmp (*argv, "-copies") == 0)
866 {
867 ++argv;
868 if (!*argv)
869 error (_("No argument to -copies"));
870 copies = parse_and_eval_long (*argv);
871 }
872 else if (strcmp (*argv, "-exec") == 0)
873 {
874 ++argv;
875 if (!*argv)
876 error (_("No argument to -exec"));
877 exec = tilde_expand (*argv);
878 make_cleanup (xfree, exec);
879 }
880 }
881 else
882 error (_("Invalid argument"));
883 }
884 }
885
886 save_current_space_and_thread ();
887
888 for (i = 0; i < copies; ++i)
889 {
890 struct inferior *inf = add_inferior_with_spaces ();
891
892 printf_filtered (_("Added inferior %d\n"), inf->num);
893
894 if (exec != NULL)
895 {
896 /* Switch over temporarily, while reading executable and
897 symbols.q. */
898 set_current_program_space (inf->pspace);
899 set_current_inferior (inf);
900 switch_to_thread (null_ptid);
901
902 exec_file_attach (exec, from_tty);
903 symbol_file_add_main (exec, from_tty);
904 }
905 }
906
907 do_cleanups (old_chain);
908 }
909
910 /* clone-inferior [-copies N] [ID] */
911
912 static void
913 clone_inferior_command (char *args, int from_tty)
914 {
915 int i, copies = 1;
916 char **argv;
917 struct inferior *orginf = NULL;
918 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
919
920 if (args)
921 {
922 argv = gdb_buildargv (args);
923 make_cleanup_freeargv (argv);
924
925 for (; *argv != NULL; argv++)
926 {
927 if (**argv == '-')
928 {
929 if (strcmp (*argv, "-copies") == 0)
930 {
931 ++argv;
932 if (!*argv)
933 error (_("No argument to -copies"));
934 copies = parse_and_eval_long (*argv);
935
936 if (copies < 0)
937 error (_("Invalid copies number"));
938 }
939 }
940 else
941 {
942 if (orginf == NULL)
943 {
944 int num;
945
946 /* The first non-option (-) argument specified the
947 program space ID. */
948 num = parse_and_eval_long (*argv);
949 orginf = find_inferior_id (num);
950
951 if (orginf == NULL)
952 error (_("Inferior ID %d not known."), num);
953 continue;
954 }
955 else
956 error (_("Invalid argument"));
957 }
958 }
959 }
960
961 /* If no inferior id was specified, then the user wants to clone the
962 current inferior. */
963 if (orginf == NULL)
964 orginf = current_inferior ();
965
966 save_current_space_and_thread ();
967
968 for (i = 0; i < copies; ++i)
969 {
970 struct address_space *aspace;
971 struct program_space *pspace;
972 struct inferior *inf;
973
974 /* If all inferiors share an address space on this system, this
975 doesn't really return a new address space; otherwise, it
976 really does. */
977 aspace = maybe_new_address_space ();
978 pspace = add_program_space (aspace);
979 inf = add_inferior (0);
980 inf->pspace = pspace;
981 inf->aspace = pspace->aspace;
982 inf->gdbarch = orginf->gdbarch;
983
984 /* If the original inferior had a user specified target
985 description, make the clone use it too. */
986 if (target_desc_info_from_user_p (inf->tdesc_info))
987 copy_inferior_target_desc_info (inf, orginf);
988
989 printf_filtered (_("Added inferior %d.\n"), inf->num);
990
991 set_current_inferior (inf);
992 switch_to_thread (null_ptid);
993 clone_program_space (pspace, orginf->pspace);
994 }
995
996 do_cleanups (old_chain);
997 }
998
999 /* Print notices when new inferiors are created and die. */
1000 static void
1001 show_print_inferior_events (struct ui_file *file, int from_tty,
1002 struct cmd_list_element *c, const char *value)
1003 {
1004 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
1005 }
1006
1007 /* Return a new value for the selected inferior's id. */
1008
1009 static struct value *
1010 inferior_id_make_value (struct gdbarch *gdbarch, struct internalvar *var,
1011 void *ignore)
1012 {
1013 struct inferior *inf = current_inferior ();
1014
1015 return value_from_longest (builtin_type (gdbarch)->builtin_int, inf->num);
1016 }
1017
1018 /* Implementation of `$_inferior' variable. */
1019
1020 static const struct internalvar_funcs inferior_funcs =
1021 {
1022 inferior_id_make_value,
1023 NULL,
1024 NULL
1025 };
1026
1027 \f
1028
1029 void
1030 initialize_inferiors (void)
1031 {
1032 struct cmd_list_element *c = NULL;
1033
1034 /* There's always one inferior. Note that this function isn't an
1035 automatic _initialize_foo function, since other _initialize_foo
1036 routines may need to install their per-inferior data keys. We
1037 can only allocate an inferior when all those modules have done
1038 that. Do this after initialize_progspace, due to the
1039 current_program_space reference. */
1040 current_inferior_ = add_inferior (0);
1041 current_inferior_->pspace = current_program_space;
1042 current_inferior_->aspace = current_program_space->aspace;
1043 /* The architecture will be initialized shortly, by
1044 initialize_current_architecture. */
1045
1046 add_info ("inferiors", info_inferiors_command,
1047 _("IDs of specified inferiors (all inferiors if no argument)."));
1048
1049 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
1050 Add a new inferior.\n\
1051 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1052 N is the optional number of inferiors to add, default is 1.\n\
1053 FILENAME is the file name of the executable to use\n\
1054 as main program."));
1055 set_cmd_completer (c, filename_completer);
1056
1057 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1058 Remove inferior ID (or list of IDs).\n\
1059 Usage: remove-inferiors ID..."));
1060
1061 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1062 Clone inferior ID.\n\
1063 Usage: clone-inferior [-copies <N>] [ID]\n\
1064 Add N copies of inferior ID. The new inferior has the same\n\
1065 executable loaded as the copied inferior. If -copies is not specified,\n\
1066 adds 1 copy. If ID is not specified, it is the current inferior\n\
1067 that is cloned."));
1068
1069 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1070 Detach from inferior ID (or list of IDS)."),
1071 &detachlist);
1072
1073 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1074 Kill inferior ID (or list of IDs)."),
1075 &killlist);
1076
1077 add_cmd ("inferior", class_run, inferior_command, _("\
1078 Use this command to switch between inferiors.\n\
1079 The new inferior ID must be currently known."),
1080 &cmdlist);
1081
1082 add_setshow_boolean_cmd ("inferior-events", no_class,
1083 &print_inferior_events, _("\
1084 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1085 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1086 NULL,
1087 show_print_inferior_events,
1088 &setprintlist, &showprintlist);
1089
1090 create_internalvar_type_lazy ("_inferior", &inferior_funcs, NULL);
1091 }
This page took 0.061294 seconds and 4 git commands to generate.