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