1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008 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 void _initialize_inferiors (void);
30 static struct inferior *inferior_list = NULL;
31 static int highest_inferior_num;
33 /* Print notices on inferior events (attach, detach, etc.), set with
34 `set print inferior-events'. */
35 static int print_inferior_events = 0;
38 current_inferior (void)
40 struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
46 free_inferior (struct inferior *inf)
53 init_inferior_list (void)
55 struct inferior *inf, *infnext;
57 highest_inferior_num = 0;
61 for (inf = inferior_list; inf; inf = infnext)
71 add_inferior_silent (int pid)
75 inf = xmalloc (sizeof (*inf));
76 memset (inf, 0, sizeof (*inf));
79 inf->stop_soon = NO_STOP_QUIETLY;
81 inf->num = ++highest_inferior_num;
82 inf->next = inferior_list;
89 add_inferior (int pid)
91 struct inferior *inf = add_inferior_silent (pid);
93 if (print_inferior_events)
94 printf_unfiltered (_("[New inferior %d]\n"), pid);
99 struct delete_thread_of_inferior_arg
106 delete_thread_of_inferior (struct thread_info *tp, void *data)
108 struct delete_thread_of_inferior_arg *arg = data;
110 if (ptid_get_pid (tp->ptid) == arg->pid)
113 delete_thread_silent (tp->ptid);
115 delete_thread (tp->ptid);
121 /* If SILENT then be quiet -- don't announce a inferior death, or the
122 exit of its threads. */
124 delete_inferior_1 (int pid, int silent)
126 struct inferior *inf, *infprev;
127 struct delete_thread_of_inferior_arg arg = { pid, silent };
131 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
139 infprev->next = inf->next;
141 inferior_list = inf->next;
148 iterate_over_threads (delete_thread_of_inferior, &arg);
152 delete_inferior (int pid)
154 delete_inferior_1 (pid, 0);
156 if (print_inferior_events)
157 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
161 delete_inferior_silent (int pid)
163 delete_inferior_1 (pid, 1);
167 detach_inferior (int pid)
169 delete_inferior_1 (pid, 1);
171 if (print_inferior_events)
172 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
176 discard_all_inferiors (void)
178 struct inferior *inf, *infnext;
180 for (inf = inferior_list; inf; inf = infnext)
183 delete_inferior_silent (inf->pid);
187 static struct inferior *
188 find_inferior_id (int num)
190 struct inferior *inf;
192 for (inf = inferior_list; inf; inf = inf->next)
200 find_inferior_pid (int pid)
202 struct inferior *inf;
204 for (inf = inferior_list; inf; inf = inf->next)
212 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
215 struct inferior *inf, *infnext;
217 for (inf = inferior_list; inf; inf = infnext)
220 if ((*callback) (inf, data))
228 valid_gdb_inferior_id (int num)
230 struct inferior *inf;
232 for (inf = inferior_list; inf; inf = inf->next)
240 pid_to_gdb_inferior_id (int pid)
242 struct inferior *inf;
244 for (inf = inferior_list; inf; inf = inf->next)
252 gdb_inferior_id_to_pid (int num)
254 struct inferior *inferior = find_inferior_id (num);
256 return inferior->pid;
262 in_inferior_list (int pid)
264 struct inferior *inf;
266 for (inf = inferior_list; inf; inf = inf->next)
274 have_inferiors (void)
276 return inferior_list != NULL;
279 /* Prints the list of inferiors and their details on UIOUT. This is a
280 version of 'info_inferior_command' suitable for use from MI.
282 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
283 should be printed. Otherwise, all inferiors are printed. */
285 print_inferior (struct ui_out *uiout, int requested_inferior)
287 struct inferior *inf;
288 struct cleanup *old_chain;
290 old_chain = make_cleanup_ui_out_list_begin_end (uiout, "inferiors");
292 for (inf = inferior_list; inf; inf = inf->next)
294 struct cleanup *chain2;
296 if (requested_inferior != -1 && inf->num != requested_inferior)
299 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
301 if (inf->pid == ptid_get_pid (inferior_ptid))
302 ui_out_text (uiout, "* ");
304 ui_out_text (uiout, " ");
306 ui_out_field_int (uiout, "id", inf->num);
307 ui_out_text (uiout, " ");
308 ui_out_field_int (uiout, "target-id", inf->pid);
310 ui_out_text (uiout, "\n");
311 do_cleanups (chain2);
314 do_cleanups (old_chain);
317 /* Print information about currently known inferiors. */
320 info_inferiors_command (char *arg, int from_tty)
322 print_inferior (uiout, -1);
325 /* Print notices when new inferiors are created and die. */
327 show_print_inferior_events (struct ui_file *file, int from_tty,
328 struct cmd_list_element *c, const char *value)
330 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
334 _initialize_inferiors (void)
336 add_info ("inferiors", info_inferiors_command,
337 _("IDs of currently known inferiors."));
339 add_setshow_boolean_cmd ("inferior-events", no_class,
340 &print_inferior_events, _("\
341 Set printing of inferior events (e.g., inferior start and exit)."), _("\
342 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
344 show_print_inferior_events,
345 &setprintlist, &showprintlist);