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, 3, ui_right, "current", "Cur");
328 ui_out_table_header (uiout, 4, ui_right, "id", "Id");
329 ui_out_table_header (uiout, 7, ui_right, "target-id", "PID");
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, "id", inf->num);
347 ui_out_field_int (uiout, "target-id", inf->pid);
349 ui_out_text (uiout, "\n");
350 do_cleanups (chain2);
353 do_cleanups (old_chain);
357 detach_inferior_command (char *args, int from_tty)
360 struct thread_info *tp;
363 error (_("Requires argument (inferior id to detach)"));
365 num = parse_and_eval_long (args);
367 if (!valid_gdb_inferior_id (num))
368 error (_("Inferior ID %d not known."), num);
370 pid = gdb_inferior_id_to_pid (num);
372 tp = any_thread_of_process (pid);
374 error (_("Inferior has no threads."));
376 switch_to_thread (tp->ptid);
378 detach_command (NULL, from_tty);
382 kill_inferior_command (char *args, int from_tty)
385 struct thread_info *tp;
388 error (_("Requires argument (inferior id to kill)"));
390 num = parse_and_eval_long (args);
392 if (!valid_gdb_inferior_id (num))
393 error (_("Inferior ID %d not known."), num);
395 pid = gdb_inferior_id_to_pid (num);
397 tp = any_thread_of_process (pid);
399 error (_("Inferior has no threads."));
401 switch_to_thread (tp->ptid);
405 bfd_cache_close_all ();
409 inferior_command (char *args, int from_tty)
413 if (!have_inferiors ())
414 error (_("No inferiors"));
416 num = parse_and_eval_long (args);
418 if (!valid_gdb_inferior_id (num))
419 error (_("Inferior ID %d not known."), num);
421 pid = gdb_inferior_id_to_pid (num);
423 if (pid != ptid_get_pid (inferior_ptid))
425 struct thread_info *tp;
427 tp = any_thread_of_process (pid);
429 error (_("Inferior has no threads."));
431 switch_to_thread (tp->ptid);
434 printf_filtered (_("[Switching to thread %d (%s)] "),
435 pid_to_thread_id (inferior_ptid),
436 target_pid_to_str (inferior_ptid));
438 if (is_running (inferior_ptid))
439 ui_out_text (uiout, "(running)\n");
442 ui_out_text (uiout, "\n");
443 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
447 /* Print information about currently known inferiors. */
450 info_inferiors_command (char *args, int from_tty)
456 requested = parse_and_eval_long (args);
457 if (!valid_gdb_inferior_id (requested))
458 error (_("Inferior ID %d not known."), requested);
461 print_inferior (uiout, requested);
464 /* Print notices when new inferiors are created and die. */
466 show_print_inferior_events (struct ui_file *file, int from_tty,
467 struct cmd_list_element *c, const char *value)
469 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
473 _initialize_inferiors (void)
475 add_info ("inferiors", info_inferiors_command,
476 _("IDs of currently known inferiors."));
478 add_setshow_boolean_cmd ("inferior-events", no_class,
479 &print_inferior_events, _("\
480 Set printing of inferior events (e.g., inferior start and exit)."), _("\
481 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
483 show_print_inferior_events,
484 &setprintlist, &showprintlist);
486 add_cmd ("inferior", class_run, detach_inferior_command, _("\
487 Detach from inferior ID."),
490 add_cmd ("inferior", class_run, kill_inferior_command, _("\
494 add_cmd ("inferior", class_run, inferior_command, _("\
495 Use this command to switch between inferiors.\n\
496 The new inferior ID must be currently known."),