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