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"
29 void _initialize_inferiors (void);
31 static struct inferior *inferior_list = NULL;
32 static int highest_inferior_num;
34 /* Print notices on inferior events (attach, detach, etc.), set with
35 `set print inferior-events'. */
36 static int print_inferior_events = 0;
39 current_inferior (void)
41 struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
47 free_inferior (struct inferior *inf)
49 discard_all_inferior_continuations (inf);
55 init_inferior_list (void)
57 struct inferior *inf, *infnext;
59 highest_inferior_num = 0;
63 for (inf = inferior_list; inf; inf = infnext)
73 add_inferior_silent (int pid)
77 inf = xmalloc (sizeof (*inf));
78 memset (inf, 0, sizeof (*inf));
81 inf->stop_soon = NO_STOP_QUIETLY;
83 inf->num = ++highest_inferior_num;
84 inf->next = inferior_list;
87 observer_notify_new_inferior (pid);
93 add_inferior (int pid)
95 struct inferior *inf = add_inferior_silent (pid);
97 if (print_inferior_events)
98 printf_unfiltered (_("[New inferior %d]\n"), pid);
103 struct delete_thread_of_inferior_arg
110 delete_thread_of_inferior (struct thread_info *tp, void *data)
112 struct delete_thread_of_inferior_arg *arg = data;
114 if (ptid_get_pid (tp->ptid) == arg->pid)
117 delete_thread_silent (tp->ptid);
119 delete_thread (tp->ptid);
125 /* If SILENT then be quiet -- don't announce a inferior death, or the
126 exit of its threads. */
128 delete_inferior_1 (int pid, int silent)
130 struct inferior *inf, *infprev;
131 struct delete_thread_of_inferior_arg arg = { pid, silent };
135 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
145 iterate_over_threads (delete_thread_of_inferior, &arg);
147 /* Notify the observers before removing the inferior from the list,
148 so that the observers have a change to look it up. */
149 observer_notify_inferior_exit (pid);
152 infprev->next = inf->next;
154 inferior_list = inf->next;
160 delete_inferior (int pid)
162 delete_inferior_1 (pid, 0);
164 if (print_inferior_events)
165 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
169 delete_inferior_silent (int pid)
171 delete_inferior_1 (pid, 1);
175 detach_inferior (int pid)
177 delete_inferior_1 (pid, 1);
179 if (print_inferior_events)
180 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
184 discard_all_inferiors (void)
186 struct inferior *inf, *infnext;
188 for (inf = inferior_list; inf; inf = infnext)
191 delete_inferior_silent (inf->pid);
195 static struct inferior *
196 find_inferior_id (int num)
198 struct inferior *inf;
200 for (inf = inferior_list; inf; inf = inf->next)
208 find_inferior_pid (int pid)
210 struct inferior *inf;
212 for (inf = inferior_list; inf; inf = inf->next)
220 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
223 struct inferior *inf, *infnext;
225 for (inf = inferior_list; inf; inf = infnext)
228 if ((*callback) (inf, data))
236 valid_gdb_inferior_id (int num)
238 struct inferior *inf;
240 for (inf = inferior_list; inf; inf = inf->next)
248 pid_to_gdb_inferior_id (int pid)
250 struct inferior *inf;
252 for (inf = inferior_list; inf; inf = inf->next)
260 gdb_inferior_id_to_pid (int num)
262 struct inferior *inferior = find_inferior_id (num);
264 return inferior->pid;
270 in_inferior_list (int pid)
272 struct inferior *inf;
274 for (inf = inferior_list; inf; inf = inf->next)
282 have_inferiors (void)
284 return inferior_list != NULL;
288 have_live_inferiors (void)
290 /* The check on stratum suffices, as GDB doesn't currently support
291 multiple target interfaces. */
292 return (current_target.to_stratum >= process_stratum && have_inferiors ());
295 /* Prints the list of inferiors and their details on UIOUT. This is a
296 version of 'info_inferior_command' suitable for use from MI.
298 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
299 should be printed. Otherwise, all inferiors are printed. */
301 print_inferior (struct ui_out *uiout, int requested_inferior)
303 struct inferior *inf;
304 struct cleanup *old_chain;
307 /* Compute number of inferiors we will print. */
308 for (inf = inferior_list; inf; inf = inf->next)
310 struct cleanup *chain2;
312 if (requested_inferior != -1 && inf->num != requested_inferior)
320 ui_out_message (uiout, 0, "No inferiors.\n");
324 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, inf_count,
326 ui_out_table_header (uiout, 3, ui_right, "current", "Cur");
327 ui_out_table_header (uiout, 4, ui_right, "id", "Id");
328 ui_out_table_header (uiout, 7, ui_right, "target-id", "PID");
329 ui_out_table_body (uiout);
331 for (inf = inferior_list; inf; inf = inf->next)
333 struct cleanup *chain2;
335 if (requested_inferior != -1 && inf->num != requested_inferior)
338 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
340 if (inf->pid == ptid_get_pid (inferior_ptid))
341 ui_out_field_string (uiout, "current", "*");
343 ui_out_field_skip (uiout, "current");
345 ui_out_field_int (uiout, "id", inf->num);
346 ui_out_field_int (uiout, "target-id", inf->pid);
348 ui_out_text (uiout, "\n");
349 do_cleanups (chain2);
352 do_cleanups (old_chain);
355 /* Print information about currently known inferiors. */
358 info_inferiors_command (char *arg, int from_tty)
360 print_inferior (uiout, -1);
363 /* Print notices when new inferiors are created and die. */
365 show_print_inferior_events (struct ui_file *file, int from_tty,
366 struct cmd_list_element *c, const char *value)
368 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
372 _initialize_inferiors (void)
374 add_info ("inferiors", info_inferiors_command,
375 _("IDs of currently known inferiors."));
377 add_setshow_boolean_cmd ("inferior-events", no_class,
378 &print_inferior_events, _("\
379 Set printing of inferior events (e.g., inferior start and exit)."), _("\
380 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
382 show_print_inferior_events,
383 &setprintlist, &showprintlist);