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