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