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