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