1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "gdbthread.h"
28 #include "gdbthread.h"
30 void _initialize_inferiors (void);
32 static struct inferior
*inferior_list
= NULL
;
33 static int highest_inferior_num
;
35 /* Print notices on inferior events (attach, detach, etc.), set with
36 `set print inferior-events'. */
37 static int print_inferior_events
= 0;
40 current_inferior (void)
42 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (inferior_ptid
));
48 free_inferior (struct inferior
*inf
)
50 discard_all_inferior_continuations (inf
);
56 init_inferior_list (void)
58 struct inferior
*inf
, *infnext
;
60 highest_inferior_num
= 0;
64 for (inf
= inferior_list
; inf
; inf
= infnext
)
74 add_inferior_silent (int pid
)
78 inf
= xmalloc (sizeof (*inf
));
79 memset (inf
, 0, sizeof (*inf
));
82 inf
->stop_soon
= NO_STOP_QUIETLY
;
84 inf
->num
= ++highest_inferior_num
;
85 inf
->next
= inferior_list
;
88 observer_notify_new_inferior (pid
);
94 add_inferior (int pid
)
96 struct inferior
*inf
= add_inferior_silent (pid
);
98 if (print_inferior_events
)
99 printf_unfiltered (_("[New inferior %d]\n"), pid
);
104 struct delete_thread_of_inferior_arg
111 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
113 struct delete_thread_of_inferior_arg
*arg
= data
;
115 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
118 delete_thread_silent (tp
->ptid
);
120 delete_thread (tp
->ptid
);
126 /* If SILENT then be quiet -- don't announce a inferior death, or the
127 exit of its threads. */
129 delete_inferior_1 (int pid
, int silent
)
131 struct inferior
*inf
, *infprev
;
132 struct delete_thread_of_inferior_arg arg
= { pid
, silent
};
136 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
146 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 infprev
->next
= inf
->next
;
155 inferior_list
= inf
->next
;
161 delete_inferior (int pid
)
163 delete_inferior_1 (pid
, 0);
165 if (print_inferior_events
)
166 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
170 delete_inferior_silent (int pid
)
172 delete_inferior_1 (pid
, 1);
176 detach_inferior (int pid
)
178 delete_inferior_1 (pid
, 1);
180 if (print_inferior_events
)
181 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
185 discard_all_inferiors (void)
187 struct inferior
*inf
, *infnext
;
189 for (inf
= inferior_list
; inf
; inf
= infnext
)
192 delete_inferior_silent (inf
->pid
);
196 static struct inferior
*
197 find_inferior_id (int num
)
199 struct inferior
*inf
;
201 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
209 find_inferior_pid (int pid
)
211 struct inferior
*inf
;
213 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
221 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
224 struct inferior
*inf
, *infnext
;
226 for (inf
= inferior_list
; inf
; inf
= infnext
)
229 if ((*callback
) (inf
, data
))
237 valid_gdb_inferior_id (int num
)
239 struct inferior
*inf
;
241 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
249 pid_to_gdb_inferior_id (int pid
)
251 struct inferior
*inf
;
253 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
261 gdb_inferior_id_to_pid (int num
)
263 struct inferior
*inferior
= find_inferior_id (num
);
265 return inferior
->pid
;
271 in_inferior_list (int pid
)
273 struct inferior
*inf
;
275 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
283 have_inferiors (void)
285 return inferior_list
!= NULL
;
289 have_live_inferiors (void)
291 /* The check on stratum suffices, as GDB doesn't currently support
292 multiple target interfaces. */
293 return (current_target
.to_stratum
>= process_stratum
&& have_inferiors ());
296 /* Prints the list of inferiors and their details on UIOUT. This is a
297 version of 'info_inferior_command' suitable for use from MI.
299 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
300 should be printed. Otherwise, all inferiors are printed. */
302 print_inferior (struct ui_out
*uiout
, int requested_inferior
)
304 struct inferior
*inf
;
305 struct cleanup
*old_chain
;
308 /* Compute number of inferiors we will print. */
309 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
311 struct cleanup
*chain2
;
313 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
321 ui_out_message (uiout
, 0, "No inferiors.\n");
325 old_chain
= make_cleanup_ui_out_table_begin_end (uiout
, 3, inf_count
,
327 ui_out_table_header (uiout
, 1, ui_left
, "current", "");
328 ui_out_table_header (uiout
, 4, ui_left
, "number", "Num");
329 ui_out_table_header (uiout
, 17, ui_left
, "target-id", "Description");
330 ui_out_table_body (uiout
);
332 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
334 struct cleanup
*chain2
;
336 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
339 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
341 if (inf
->pid
== ptid_get_pid (inferior_ptid
))
342 ui_out_field_string (uiout
, "current", "*");
344 ui_out_field_skip (uiout
, "current");
346 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
)));
350 ui_out_text (uiout
, "\n");
351 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 do_cleanups (old_chain
);
363 detach_inferior_command (char *args
, int from_tty
)
366 struct thread_info
*tp
;
369 error (_("Requires argument (inferior id to detach)"));
371 num
= parse_and_eval_long (args
);
373 if (!valid_gdb_inferior_id (num
))
374 error (_("Inferior ID %d not known."), num
);
376 pid
= gdb_inferior_id_to_pid (num
);
378 tp
= any_thread_of_process (pid
);
380 error (_("Inferior has no threads."));
382 switch_to_thread (tp
->ptid
);
384 detach_command (NULL
, from_tty
);
388 kill_inferior_command (char *args
, int from_tty
)
391 struct thread_info
*tp
;
394 error (_("Requires argument (inferior id to kill)"));
396 num
= parse_and_eval_long (args
);
398 if (!valid_gdb_inferior_id (num
))
399 error (_("Inferior ID %d not known."), num
);
401 pid
= gdb_inferior_id_to_pid (num
);
403 tp
= any_thread_of_process (pid
);
405 error (_("Inferior has no threads."));
407 switch_to_thread (tp
->ptid
);
411 bfd_cache_close_all ();
415 inferior_command (char *args
, int from_tty
)
419 if (!have_inferiors ())
420 error (_("No inferiors"));
422 num
= parse_and_eval_long (args
);
424 if (!valid_gdb_inferior_id (num
))
425 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
))
445 ui_out_text (uiout
, "(running)\n");
448 ui_out_text (uiout
, "\n");
449 print_stack_frame (get_selected_frame (NULL
), 1, SRC_AND_LOC
);
453 /* Print information about currently known inferiors. */
456 info_inferiors_command (char *args
, int from_tty
)
462 requested
= parse_and_eval_long (args
);
463 if (!valid_gdb_inferior_id (requested
))
464 error (_("Inferior ID %d not known."), requested
);
467 print_inferior (uiout
, requested
);
470 /* Print notices when new inferiors are created and die. */
472 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
473 struct cmd_list_element
*c
, const char *value
)
475 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
479 _initialize_inferiors (void)
481 add_info ("inferiors", info_inferiors_command
,
482 _("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
);
492 add_cmd ("inferior", class_run
, detach_inferior_command
, _("\
493 Detach from inferior ID."),
496 add_cmd ("inferior", class_run
, kill_inferior_command
, _("\
500 add_cmd ("inferior", class_run
, inferior_command
, _("\
501 Use this command to switch between inferiors.\n\
502 The new inferior ID must be currently known."),