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