GDB (xrefs)

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