26
27
#include "ui-out.h"
27
28
#include "observer.h"
28
29
#include "gdbthread.h"
30
34
void _initialize_inferiors (void);
32
static struct inferior *inferior_list = NULL;
36
static void inferior_alloc_data (struct inferior *inf);
37
static void inferior_free_data (struct inferior *inf);
39
struct inferior *inferior_list = NULL;
33
40
static int highest_inferior_num;
35
42
/* Print notices on inferior events (attach, detach, etc.), set with
36
43
`set print inferior-events'. */
37
44
static int print_inferior_events = 0;
46
/* The Current Inferior. */
47
static struct inferior *current_inferior_ = NULL;
40
50
current_inferior (void)
42
struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
52
return current_inferior_;
56
set_current_inferior (struct inferior *inf)
58
/* There's always an inferior. */
59
gdb_assert (inf != NULL);
61
current_inferior_ = inf;
64
/* A cleanups callback, helper for save_current_program_space
68
restore_inferior (void *arg)
70
struct inferior *saved_inferior = arg;
71
set_current_inferior (saved_inferior);
74
/* Save the current program space so that it may be restored by a later
75
call to do_cleanups. Returns the struct cleanup pointer needed for
76
later doing the cleanup. */
79
save_current_inferior (void)
81
struct cleanup *old_chain = make_cleanup (restore_inferior,
48
87
free_inferior (struct inferior *inf)
50
89
discard_all_inferior_continuations (inf);
90
inferior_free_data (inf);
92
xfree (inf->terminal);
93
free_environ (inf->environment);
51
94
xfree (inf->private);
176
delete_threads_of_inferior (int pid)
178
struct inferior *inf;
179
struct delete_thread_of_inferior_arg arg;
181
for (inf = inferior_list; inf; inf = inf->next)
191
iterate_over_threads (delete_thread_of_inferior, &arg);
126
194
/* If SILENT then be quiet -- don't announce a inferior death, or the
127
195
exit of its threads. */
129
delete_inferior_1 (int pid, int silent)
198
delete_inferior_1 (struct inferior *todel, int silent)
131
200
struct inferior *inf, *infprev;
132
struct delete_thread_of_inferior_arg arg = { pid, silent };
201
struct delete_thread_of_inferior_arg arg;
136
205
for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
144
213
arg.silent = silent;
146
215
iterate_over_threads (delete_thread_of_inferior, &arg);
148
/* Notify the observers before removing the inferior from the list,
149
so that the observers have a change to look it up. */
150
observer_notify_inferior_exit (pid);
153
218
infprev->next = inf->next;
170
237
delete_inferior_silent (int pid)
172
delete_inferior_1 (pid, 1);
239
struct inferior *inf = find_inferior_pid (pid);
241
delete_inferior_1 (inf, 1);
245
/* If SILENT then be quiet -- don't announce a inferior exit, or the
246
exit of its threads. */
249
exit_inferior_1 (struct inferior *inftoex, int silent)
251
struct inferior *inf;
252
struct delete_thread_of_inferior_arg arg;
254
for (inf = inferior_list; inf; inf = inf->next)
264
iterate_over_threads (delete_thread_of_inferior, &arg);
266
/* Notify the observers before removing the inferior from the list,
267
so that the observers have a chance to look it up. */
268
observer_notify_inferior_exit (inf->pid);
271
if (inf->vfork_parent != NULL)
273
inf->vfork_parent->vfork_child = NULL;
274
inf->vfork_parent = NULL;
279
exit_inferior (int pid)
281
struct inferior *inf = find_inferior_pid (pid);
282
exit_inferior_1 (inf, 0);
284
if (print_inferior_events)
285
printf_unfiltered (_("[Inferior %d exited]\n"), pid);
289
exit_inferior_silent (int pid)
291
struct inferior *inf = find_inferior_pid (pid);
292
exit_inferior_1 (inf, 1);
296
exit_inferior_num_silent (int num)
298
struct inferior *inf = find_inferior_id (num);
300
exit_inferior_1 (inf, 1);
176
304
detach_inferior (int pid)
178
delete_inferior_1 (pid, 1);
306
struct inferior *inf = find_inferior_pid (pid);
307
exit_inferior_1 (inf, 1);
180
309
if (print_inferior_events)
181
310
printf_unfiltered (_("[Inferior %d detached]\n"), pid);
314
inferior_appeared (struct inferior *inf, int pid)
318
observer_notify_inferior_appeared (pid);
185
322
discard_all_inferiors (void)
187
struct inferior *inf, *infnext;
324
struct inferior *inf;
189
for (inf = inferior_list; inf; inf = infnext)
326
for (inf = inferior_list; inf; inf = inf->next)
192
delete_inferior_silent (inf->pid);
329
exit_inferior_silent (inf->pid);
196
static struct inferior *
197
334
find_inferior_id (int num)
199
336
struct inferior *inf;
283
441
have_inferiors (void)
285
return inferior_list != NULL;
443
struct inferior *inf;
445
for (inf = inferior_list; inf; inf = inf->next)
289
453
have_live_inferiors (void)
455
struct target_ops *t;
291
457
/* The check on stratum suffices, as GDB doesn't currently support
292
458
multiple target interfaces. */
293
return (current_target.to_stratum >= process_stratum && have_inferiors ());
459
if (have_inferiors ())
460
for (t = current_target.beneath; t != NULL; t = t->beneath)
461
if (t->to_stratum == process_stratum)
467
/* Prune away automatically added program spaces that aren't required
471
prune_inferiors (void)
473
struct inferior *ss, **ss_link;
474
struct inferior *current = current_inferior ();
477
ss_link = &inferior_list;
490
delete_inferior_1 (ss, 1);
494
prune_program_spaces ();
497
/* Simply returns the count of inferiors. */
500
number_of_inferiors (void)
502
struct inferior *inf;
505
for (inf = inferior_list; inf != NULL; inf = inf->next)
296
511
/* Prints the list of inferiors and their details on UIOUT. This is a
325
old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, inf_count,
540
old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
327
542
ui_out_table_header (uiout, 1, ui_left, "current", "");
328
543
ui_out_table_header (uiout, 4, ui_left, "number", "Num");
329
544
ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
545
ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
330
547
ui_out_table_body (uiout);
332
548
for (inf = inferior_list; inf; inf = inf->next)
334
550
struct cleanup *chain2;
339
555
chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
341
if (inf->pid == ptid_get_pid (inferior_ptid))
557
if (inf == current_inferior ())
342
558
ui_out_field_string (uiout, "current", "*");
344
560
ui_out_field_skip (uiout, "current");
346
562
ui_out_field_int (uiout, "number", inf->num);
347
ui_out_field_string (uiout, "target-id",
348
target_pid_to_str (pid_to_ptid (inf->pid)));
565
ui_out_field_string (uiout, "target-id",
566
target_pid_to_str (pid_to_ptid (inf->pid)));
568
ui_out_field_string (uiout, "target-id", "<null>");
570
if (inf->pspace->ebfd)
571
ui_out_field_string (uiout, "exec",
572
bfd_get_filename (inf->pspace->ebfd));
574
ui_out_field_skip (uiout, "exec");
576
/* Print extra info that isn't really fit to always present in
577
tabular form. Currently we print the vfork parent/child
578
relationships, if any. */
579
if (inf->vfork_parent)
581
ui_out_text (uiout, _("\n\tis vfork child of inferior "));
582
ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
584
if (inf->vfork_child)
586
ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
587
ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
350
590
ui_out_text (uiout, "\n");
351
591
do_cleanups (chain2);
355
&& ptid_equal (inferior_ptid, null_ptid))
356
ui_out_message (uiout, 0, "\n\
357
No selected inferior/thread. See `help thread' or `help inferior'.\n");
359
594
do_cleanups (old_chain);
415
650
inferior_command (char *args, int from_tty)
419
if (!have_inferiors ())
420
error (_("No inferiors"));
652
struct inferior *inf;
422
655
num = parse_and_eval_long (args);
424
if (!valid_gdb_inferior_id (num))
657
inf = find_inferior_id (num);
425
659
error (_("Inferior ID %d not known."), num);
427
pid = gdb_inferior_id_to_pid (num);
429
if (pid != ptid_get_pid (inferior_ptid))
431
struct thread_info *tp;
433
tp = any_thread_of_process (pid);
435
error (_("Inferior has no threads."));
437
switch_to_thread (tp->ptid);
440
printf_filtered (_("[Switching to thread %d (%s)] "),
441
pid_to_thread_id (inferior_ptid),
442
target_pid_to_str (inferior_ptid));
444
if (is_running (inferior_ptid))
661
printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
663
target_pid_to_str (pid_to_ptid (inf->pid)),
665
? bfd_get_filename (inf->pspace->ebfd)
670
if (inf->pid != ptid_get_pid (inferior_ptid))
672
struct thread_info *tp;
674
tp = any_thread_of_process (inf->pid);
676
error (_("Inferior has no threads."));
678
switch_to_thread (tp->ptid);
681
printf_filtered (_("[Switching to thread %d (%s)] "),
682
pid_to_thread_id (inferior_ptid),
683
target_pid_to_str (inferior_ptid));
687
struct inferior *inf;
689
inf = find_inferior_id (num);
690
set_current_inferior (inf);
691
switch_to_thread (null_ptid);
692
set_current_program_space (inf->pspace);
695
if (inf->pid != 0 && is_running (inferior_ptid))
445
696
ui_out_text (uiout, "(running)\n");
697
else if (inf->pid != 0)
448
699
ui_out_text (uiout, "\n");
449
700
print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
467
718
print_inferior (uiout, requested);
721
/* remove-inferior ID */
724
remove_inferior_command (char *args, int from_tty)
727
struct inferior *inf;
729
num = parse_and_eval_long (args);
730
inf = find_inferior_id (num);
733
error (_("Inferior ID %d not known."), num);
735
if (inf == current_inferior ())
736
error (_("Can not remove current symbol inferior."));
738
delete_inferior_1 (inf, 1);
742
/* add-inferior [-copies N] [-exec FILENAME] */
745
add_inferior_command (char *args, int from_tty)
750
struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
754
argv = gdb_buildargv (args);
755
make_cleanup_freeargv (argv);
757
for (; *argv != NULL; argv++)
761
if (strcmp (*argv, "-copies") == 0)
765
error (_("No argument to -copies"));
766
copies = parse_and_eval_long (*argv);
768
else if (strcmp (*argv, "-exec") == 0)
772
error (_("No argument to -exec"));
777
error (_("Invalid argument"));
781
save_current_space_and_thread ();
783
for (i = 0; i < copies; ++i)
785
struct address_space *aspace;
786
struct program_space *pspace;
787
struct inferior *inf;
789
/* If all inferiors share an address space on this system, this
790
doesn't really return a new address space; otherwise, it
792
aspace = maybe_new_address_space ();
793
pspace = add_program_space (aspace);
794
inf = add_inferior (0);
795
inf->pspace = pspace;
796
inf->aspace = pspace->aspace;
798
printf_filtered (_("Added inferior %d\n"), inf->num);
802
/* Switch over temporarily, while reading executable and
804
set_current_program_space (pspace);
805
set_current_inferior (inf);
806
switch_to_thread (null_ptid);
808
exec_file_attach (exec, from_tty);
809
symbol_file_add_main (exec, from_tty);
813
do_cleanups (old_chain);
816
/* clone-inferior [-copies N] [ID] */
819
clone_inferior_command (char *args, int from_tty)
823
struct inferior *orginf = NULL;
824
struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
828
argv = gdb_buildargv (args);
829
make_cleanup_freeargv (argv);
831
for (; *argv != NULL; argv++)
835
if (strcmp (*argv, "-copies") == 0)
839
error (_("No argument to -copies"));
840
copies = parse_and_eval_long (*argv);
843
error (_("Invalid copies number"));
852
/* The first non-option (-) argument specified the
854
num = parse_and_eval_long (*argv);
855
orginf = find_inferior_id (num);
858
error (_("Inferior ID %d not known."), num);
862
error (_("Invalid argument"));
867
/* If no inferior id was specified, then the user wants to clone the
870
orginf = current_inferior ();
872
save_current_space_and_thread ();
874
for (i = 0; i < copies; ++i)
876
struct address_space *aspace;
877
struct program_space *pspace;
878
struct inferior *inf;
880
/* If all inferiors share an address space on this system, this
881
doesn't really return a new address space; otherwise, it
883
aspace = maybe_new_address_space ();
884
pspace = add_program_space (aspace);
885
inf = add_inferior (0);
886
inf->pspace = pspace;
887
inf->aspace = pspace->aspace;
889
printf_filtered (_("Added inferior %d.\n"), inf->num);
891
set_current_inferior (inf);
892
switch_to_thread (null_ptid);
893
clone_program_space (pspace, orginf->pspace);
896
do_cleanups (old_chain);
470
899
/* Print notices when new inferiors are created and die. */
472
901
show_print_inferior_events (struct ui_file *file, int from_tty,
475
904
fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
479
_initialize_inferiors (void)
909
/* Keep a registry of per-inferior data-pointers required by other GDB
915
void (*cleanup) (struct inferior *, void *);
918
struct inferior_data_registration
920
struct inferior_data *data;
921
struct inferior_data_registration *next;
924
struct inferior_data_registry
926
struct inferior_data_registration *registrations;
927
unsigned num_registrations;
930
static struct inferior_data_registry inferior_data_registry
933
const struct inferior_data *
934
register_inferior_data_with_cleanup
935
(void (*cleanup) (struct inferior *, void *))
937
struct inferior_data_registration **curr;
939
/* Append new registration. */
940
for (curr = &inferior_data_registry.registrations;
941
*curr != NULL; curr = &(*curr)->next);
943
*curr = XMALLOC (struct inferior_data_registration);
944
(*curr)->next = NULL;
945
(*curr)->data = XMALLOC (struct inferior_data);
946
(*curr)->data->index = inferior_data_registry.num_registrations++;
947
(*curr)->data->cleanup = cleanup;
949
return (*curr)->data;
952
const struct inferior_data *
953
register_inferior_data (void)
955
return register_inferior_data_with_cleanup (NULL);
959
inferior_alloc_data (struct inferior *inf)
961
gdb_assert (inf->data == NULL);
962
inf->num_data = inferior_data_registry.num_registrations;
963
inf->data = XCALLOC (inf->num_data, void *);
967
inferior_free_data (struct inferior *inf)
969
gdb_assert (inf->data != NULL);
970
clear_inferior_data (inf);
976
clear_inferior_data (struct inferior *inf)
978
struct inferior_data_registration *registration;
981
gdb_assert (inf->data != NULL);
983
for (registration = inferior_data_registry.registrations, i = 0;
985
registration = registration->next, i++)
986
if (inf->data[i] != NULL && registration->data->cleanup)
987
registration->data->cleanup (inf, inf->data[i]);
989
memset (inf->data, 0, inf->num_data * sizeof (void *));
993
set_inferior_data (struct inferior *inf,
994
const struct inferior_data *data,
997
gdb_assert (data->index < inf->num_data);
998
inf->data[data->index] = value;
1002
inferior_data (struct inferior *inf, const struct inferior_data *data)
1004
gdb_assert (data->index < inf->num_data);
1005
return inf->data[data->index];
1009
initialize_inferiors (void)
1011
/* There's always one inferior. Note that this function isn't an
1012
automatic _initialize_foo function, since other _initialize_foo
1013
routines may need to install their per-inferior data keys. We
1014
can only allocate an inferior when all those modules have done
1015
that. Do this after initialize_progspace, due to the
1016
current_program_space reference. */
1017
current_inferior_ = add_inferior (0);
1018
current_inferior_->pspace = current_program_space;
1019
current_inferior_->aspace = current_program_space->aspace;
481
1021
add_info ("inferiors", info_inferiors_command,
482
1022
_("IDs of currently known inferiors."));
484
add_setshow_boolean_cmd ("inferior-events", no_class,
485
&print_inferior_events, _("\
486
Set printing of inferior events (e.g., inferior start and exit)."), _("\
487
Show printing of inferior events (e.g., inferior start and exit)."), NULL,
489
show_print_inferior_events,
490
&setprintlist, &showprintlist);
1024
add_com ("add-inferior", no_class, add_inferior_command, _("\
1025
Add a new inferior.\n\
1026
Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1027
N is the optional number of inferior to add, default is 1.\n\
1028
FILENAME is the file name of the executable to use\n\
1029
as main program."));
1031
add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1032
Remove inferior ID.\n\
1033
Usage: remove-inferior ID"));
1035
add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1036
Clone inferior ID.\n\
1037
Usage: clone-inferior [-copies <N>] [ID]\n\
1038
Add N copies of inferior ID. The new inferior has the same\n\
1039
executable loaded as the copied inferior. If -copies is not specified,\n\
1040
adds 1 copy. If ID is not specified, it is the current inferior\n\
492
1043
add_cmd ("inferior", class_run, detach_inferior_command, _("\
493
1044
Detach from inferior ID."),