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