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