OSDN Git Service

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