]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inferior.c
Tiny formatting fix.
[thirdparty/binutils-gdb.git] / gdb / inferior.c
CommitLineData
b77209e0
PA
1/* Multi-process control for GDB, the GNU debugger.
2
0fb0cc75 3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
b77209e0
PA
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
21#include "inferior.h"
22#include "target.h"
23#include "command.h"
24#include "gdbcmd.h"
25#include "gdbthread.h"
26#include "ui-out.h"
4a92f99b 27#include "observer.h"
b77209e0
PA
28
29void _initialize_inferiors (void);
30
31static struct inferior *inferior_list = NULL;
32static int highest_inferior_num;
33
34/* Print notices on inferior events (attach, detach, etc.), set with
35 `set print inferior-events'. */
36static int print_inferior_events = 0;
37
38struct inferior*
39current_inferior (void)
40{
41 struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
42 gdb_assert (inf);
43 return inf;
44}
45
46static void
47free_inferior (struct inferior *inf)
48{
e0ba6746 49 discard_all_inferior_continuations (inf);
b77209e0
PA
50 xfree (inf->private);
51 xfree (inf);
52}
53
54void
55init_inferior_list (void)
56{
57 struct inferior *inf, *infnext;
58
59 highest_inferior_num = 0;
60 if (!inferior_list)
61 return;
62
63 for (inf = inferior_list; inf; inf = infnext)
64 {
65 infnext = inf->next;
66 free_inferior (inf);
67 }
68
69 inferior_list = NULL;
70}
71
72struct inferior *
73add_inferior_silent (int pid)
74{
75 struct inferior *inf;
76
77 inf = xmalloc (sizeof (*inf));
78 memset (inf, 0, sizeof (*inf));
79 inf->pid = pid;
80
d6b48e9c
PA
81 inf->stop_soon = NO_STOP_QUIETLY;
82
b77209e0
PA
83 inf->num = ++highest_inferior_num;
84 inf->next = inferior_list;
85 inferior_list = inf;
86
a562dc8f
PA
87 observer_notify_new_inferior (pid);
88
b77209e0
PA
89 return inf;
90}
91
92struct inferior *
93add_inferior (int pid)
94{
95 struct inferior *inf = add_inferior_silent (pid);
96
97 if (print_inferior_events)
98 printf_unfiltered (_("[New inferior %d]\n"), pid);
99
100 return inf;
101}
102
103struct delete_thread_of_inferior_arg
104{
105 int pid;
106 int silent;
107};
108
109static int
110delete_thread_of_inferior (struct thread_info *tp, void *data)
111{
112 struct delete_thread_of_inferior_arg *arg = data;
113
114 if (ptid_get_pid (tp->ptid) == arg->pid)
115 {
116 if (arg->silent)
117 delete_thread_silent (tp->ptid);
118 else
119 delete_thread (tp->ptid);
120 }
121
122 return 0;
123}
124
125/* If SILENT then be quiet -- don't announce a inferior death, or the
126 exit of its threads. */
127static void
128delete_inferior_1 (int pid, int silent)
129{
130 struct inferior *inf, *infprev;
131 struct delete_thread_of_inferior_arg arg = { pid, silent };
132
133 infprev = NULL;
134
135 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
136 if (inf->pid == pid)
137 break;
138
139 if (!inf)
140 return;
141
b77209e0
PA
142 arg.pid = pid;
143 arg.silent = silent;
144
145 iterate_over_threads (delete_thread_of_inferior, &arg);
4a92f99b 146
7e1789f5
PA
147 /* Notify the observers before removing the inferior from the list,
148 so that the observers have a change to look it up. */
4a92f99b 149 observer_notify_inferior_exit (pid);
7e1789f5
PA
150
151 if (infprev)
152 infprev->next = inf->next;
153 else
154 inferior_list = inf->next;
155
156 free_inferior (inf);
b77209e0
PA
157}
158
159void
160delete_inferior (int pid)
161{
162 delete_inferior_1 (pid, 0);
163
164 if (print_inferior_events)
165 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
166}
167
168void
169delete_inferior_silent (int pid)
170{
171 delete_inferior_1 (pid, 1);
172}
173
174void
175detach_inferior (int pid)
176{
177 delete_inferior_1 (pid, 1);
178
179 if (print_inferior_events)
180 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
181}
182
82f73884
PA
183void
184discard_all_inferiors (void)
185{
186 struct inferior *inf, *infnext;
187
188 for (inf = inferior_list; inf; inf = infnext)
189 {
190 infnext = inf->next;
191 delete_inferior_silent (inf->pid);
192 }
193}
194
b77209e0
PA
195static struct inferior *
196find_inferior_id (int num)
197{
198 struct inferior *inf;
199
200 for (inf = inferior_list; inf; inf = inf->next)
201 if (inf->num == num)
202 return inf;
203
204 return NULL;
205}
206
207struct inferior *
208find_inferior_pid (int pid)
209{
210 struct inferior *inf;
211
212 for (inf = inferior_list; inf; inf = inf->next)
213 if (inf->pid == pid)
214 return inf;
215
216 return NULL;
217}
218
219struct inferior *
220iterate_over_inferiors (int (*callback) (struct inferior *, void *),
221 void *data)
222{
223 struct inferior *inf, *infnext;
224
225 for (inf = inferior_list; inf; inf = infnext)
226 {
227 infnext = inf->next;
228 if ((*callback) (inf, data))
229 return inf;
230 }
231
232 return NULL;
233}
234
235int
236valid_gdb_inferior_id (int num)
237{
238 struct inferior *inf;
239
240 for (inf = inferior_list; inf; inf = inf->next)
241 if (inf->num == num)
242 return 1;
243
244 return 0;
245}
246
247int
248pid_to_gdb_inferior_id (int pid)
249{
250 struct inferior *inf;
251
252 for (inf = inferior_list; inf; inf = inf->next)
253 if (inf->pid == pid)
254 return inf->num;
255
256 return 0;
257}
258
259int
260gdb_inferior_id_to_pid (int num)
261{
262 struct inferior *inferior = find_inferior_id (num);
263 if (inferior)
264 return inferior->pid;
265 else
266 return -1;
267}
268
269int
270in_inferior_list (int pid)
271{
272 struct inferior *inf;
273
274 for (inf = inferior_list; inf; inf = inf->next)
275 if (inf->pid == pid)
276 return 1;
277
278 return 0;
279}
280
281int
282have_inferiors (void)
283{
284 return inferior_list != NULL;
285}
286
287/* Prints the list of inferiors and their details on UIOUT. This is a
288 version of 'info_inferior_command' suitable for use from MI.
289
290 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
291 should be printed. Otherwise, all inferiors are printed. */
292void
293print_inferior (struct ui_out *uiout, int requested_inferior)
294{
295 struct inferior *inf;
296 struct cleanup *old_chain;
297
298 old_chain = make_cleanup_ui_out_list_begin_end (uiout, "inferiors");
299
300 for (inf = inferior_list; inf; inf = inf->next)
301 {
302 struct cleanup *chain2;
303
304 if (requested_inferior != -1 && inf->num != requested_inferior)
305 continue;
306
307 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
308
309 if (inf->pid == ptid_get_pid (inferior_ptid))
310 ui_out_text (uiout, "* ");
311 else
312 ui_out_text (uiout, " ");
313
314 ui_out_field_int (uiout, "id", inf->num);
315 ui_out_text (uiout, " ");
316 ui_out_field_int (uiout, "target-id", inf->pid);
317
318 ui_out_text (uiout, "\n");
319 do_cleanups (chain2);
320 }
321
322 do_cleanups (old_chain);
323}
324
325/* Print information about currently known inferiors. */
326
327static void
328info_inferiors_command (char *arg, int from_tty)
329{
330 print_inferior (uiout, -1);
331}
332
333/* Print notices when new inferiors are created and die. */
334static void
335show_print_inferior_events (struct ui_file *file, int from_tty,
336 struct cmd_list_element *c, const char *value)
337{
338 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
339}
340
341void
342_initialize_inferiors (void)
343{
344 add_info ("inferiors", info_inferiors_command,
345 _("IDs of currently known inferiors."));
346
347 add_setshow_boolean_cmd ("inferior-events", no_class,
348 &print_inferior_events, _("\
349Set printing of inferior events (e.g., inferior start and exit)."), _("\
350Show printing of inferior events (e.g., inferior start and exit)."), NULL,
351 NULL,
352 show_print_inferior_events,
353 &setprintlist, &showprintlist);
354}