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