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)
48 discard_all_inferior_continuations (inf);
54 init_inferior_list (void)
56 struct inferior *inf, *infnext;
58 highest_inferior_num = 0;
62 for (inf = inferior_list; inf; inf = infnext)
72 add_inferior_silent (int pid)
76 inf = xmalloc (sizeof (*inf));
77 memset (inf, 0, sizeof (*inf));
80 inf->stop_soon = NO_STOP_QUIETLY;
82 inf->num = ++highest_inferior_num;
83 inf->next = inferior_list;
90 add_inferior (int pid)
92 struct inferior *inf = add_inferior_silent (pid);
94 if (print_inferior_events)
95 printf_unfiltered (_("[New inferior %d]\n"), pid);
100 struct delete_thread_of_inferior_arg
107 delete_thread_of_inferior (struct thread_info *tp, void *data)
109 struct delete_thread_of_inferior_arg *arg = data;
111 if (ptid_get_pid (tp->ptid) == arg->pid)
114 delete_thread_silent (tp->ptid);
116 delete_thread (tp->ptid);
122 /* If SILENT then be quiet -- don't announce a inferior death, or the
123 exit of its threads. */
125 delete_inferior_1 (int pid, int silent)
127 struct inferior *inf, *infprev;
128 struct delete_thread_of_inferior_arg arg = { pid, silent };
132 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
140 infprev->next = inf->next;
142 inferior_list = inf->next;
149 iterate_over_threads (delete_thread_of_inferior, &arg);
153 delete_inferior (int pid)
155 delete_inferior_1 (pid, 0);
157 if (print_inferior_events)
158 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
162 delete_inferior_silent (int pid)
164 delete_inferior_1 (pid, 1);
168 detach_inferior (int pid)
170 delete_inferior_1 (pid, 1);
172 if (print_inferior_events)
173 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
177 discard_all_inferiors (void)
179 struct inferior *inf, *infnext;
181 for (inf = inferior_list; inf; inf = infnext)
184 delete_inferior_silent (inf->pid);
188 static struct inferior *
189 find_inferior_id (int num)
191 struct inferior *inf;
193 for (inf = inferior_list; inf; inf = inf->next)
201 find_inferior_pid (int pid)
203 struct inferior *inf;
205 for (inf = inferior_list; inf; inf = inf->next)
213 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
216 struct inferior *inf, *infnext;
218 for (inf = inferior_list; inf; inf = infnext)
221 if ((*callback) (inf, data))
229 valid_gdb_inferior_id (int num)
231 struct inferior *inf;
233 for (inf = inferior_list; inf; inf = inf->next)
241 pid_to_gdb_inferior_id (int pid)
243 struct inferior *inf;
245 for (inf = inferior_list; inf; inf = inf->next)
253 gdb_inferior_id_to_pid (int num)
255 struct inferior *inferior = find_inferior_id (num);
257 return inferior->pid;
263 in_inferior_list (int pid)
265 struct inferior *inf;
267 for (inf = inferior_list; inf; inf = inf->next)
275 have_inferiors (void)
277 return inferior_list != NULL;
280 /* Prints the list of inferiors and their details on UIOUT. This is a
281 version of 'info_inferior_command' suitable for use from MI.
283 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
284 should be printed. Otherwise, all inferiors are printed. */
286 print_inferior (struct ui_out *uiout, int requested_inferior)
288 struct inferior *inf;
289 struct cleanup *old_chain;
291 old_chain = make_cleanup_ui_out_list_begin_end (uiout, "inferiors");
293 for (inf = inferior_list; inf; inf = inf->next)
295 struct cleanup *chain2;
297 if (requested_inferior != -1 && inf->num != requested_inferior)
300 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
302 if (inf->pid == ptid_get_pid (inferior_ptid))
303 ui_out_text (uiout, "* ");
305 ui_out_text (uiout, " ");
307 ui_out_field_int (uiout, "id", inf->num);
308 ui_out_text (uiout, " ");
309 ui_out_field_int (uiout, "target-id", inf->pid);
311 ui_out_text (uiout, "\n");
312 do_cleanups (chain2);
315 do_cleanups (old_chain);
318 /* Print information about currently known inferiors. */
321 info_inferiors_command (char *arg, int from_tty)
323 print_inferior (uiout, -1);
326 /* Print notices when new inferiors are created and die. */
328 show_print_inferior_events (struct ui_file *file, int from_tty,
329 struct cmd_list_element *c, const char *value)
331 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
335 _initialize_inferiors (void)
337 add_info ("inferiors", info_inferiors_command,
338 _("IDs of currently known inferiors."));
340 add_setshow_boolean_cmd ("inferior-events", no_class,
341 &print_inferior_events, _("\
342 Set printing of inferior events (e.g., inferior start and exit)."), _("\
343 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
345 show_print_inferior_events,
346 &setprintlist, &showprintlist);