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