]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/inferior.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / inferior.c
1 /* Multi-process control for GDB, the GNU debugger.
2
3 Copyright (C) 2008, 2009 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 #include "observer.h"
28 #include "gdbthread.h"
29
30 void _initialize_inferiors (void);
31
32 static struct inferior *inferior_list = NULL;
33 static int highest_inferior_num;
34
35 /* Print notices on inferior events (attach, detach, etc.), set with
36 `set print inferior-events'. */
37 static int print_inferior_events = 0;
38
39 struct inferior*
40 current_inferior (void)
41 {
42 struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
43 gdb_assert (inf);
44 return inf;
45 }
46
47 static void
48 free_inferior (struct inferior *inf)
49 {
50 discard_all_inferior_continuations (inf);
51 xfree (inf->private);
52 xfree (inf);
53 }
54
55 void
56 init_inferior_list (void)
57 {
58 struct inferior *inf, *infnext;
59
60 highest_inferior_num = 0;
61 if (!inferior_list)
62 return;
63
64 for (inf = inferior_list; inf; inf = infnext)
65 {
66 infnext = inf->next;
67 free_inferior (inf);
68 }
69
70 inferior_list = NULL;
71 }
72
73 struct inferior *
74 add_inferior_silent (int pid)
75 {
76 struct inferior *inf;
77
78 inf = xmalloc (sizeof (*inf));
79 memset (inf, 0, sizeof (*inf));
80 inf->pid = pid;
81
82 inf->stop_soon = NO_STOP_QUIETLY;
83
84 inf->num = ++highest_inferior_num;
85 inf->next = inferior_list;
86 inferior_list = inf;
87
88 observer_notify_new_inferior (pid);
89
90 return inf;
91 }
92
93 struct inferior *
94 add_inferior (int pid)
95 {
96 struct inferior *inf = add_inferior_silent (pid);
97
98 if (print_inferior_events)
99 printf_unfiltered (_("[New inferior %d]\n"), pid);
100
101 return inf;
102 }
103
104 struct delete_thread_of_inferior_arg
105 {
106 int pid;
107 int silent;
108 };
109
110 static int
111 delete_thread_of_inferior (struct thread_info *tp, void *data)
112 {
113 struct delete_thread_of_inferior_arg *arg = data;
114
115 if (ptid_get_pid (tp->ptid) == arg->pid)
116 {
117 if (arg->silent)
118 delete_thread_silent (tp->ptid);
119 else
120 delete_thread (tp->ptid);
121 }
122
123 return 0;
124 }
125
126 /* If SILENT then be quiet -- don't announce a inferior death, or the
127 exit of its threads. */
128 static void
129 delete_inferior_1 (int pid, int silent)
130 {
131 struct inferior *inf, *infprev;
132 struct delete_thread_of_inferior_arg arg = { pid, silent };
133
134 infprev = NULL;
135
136 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
137 if (inf->pid == pid)
138 break;
139
140 if (!inf)
141 return;
142
143 arg.pid = pid;
144 arg.silent = silent;
145
146 iterate_over_threads (delete_thread_of_inferior, &arg);
147
148 /* Notify the observers before removing the inferior from the list,
149 so that the observers have a change to look it up. */
150 observer_notify_inferior_exit (pid);
151
152 if (infprev)
153 infprev->next = inf->next;
154 else
155 inferior_list = inf->next;
156
157 free_inferior (inf);
158 }
159
160 void
161 delete_inferior (int pid)
162 {
163 delete_inferior_1 (pid, 0);
164
165 if (print_inferior_events)
166 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
167 }
168
169 void
170 delete_inferior_silent (int pid)
171 {
172 delete_inferior_1 (pid, 1);
173 }
174
175 void
176 detach_inferior (int pid)
177 {
178 delete_inferior_1 (pid, 1);
179
180 if (print_inferior_events)
181 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
182 }
183
184 void
185 discard_all_inferiors (void)
186 {
187 struct inferior *inf, *infnext;
188
189 for (inf = inferior_list; inf; inf = infnext)
190 {
191 infnext = inf->next;
192 delete_inferior_silent (inf->pid);
193 }
194 }
195
196 static struct inferior *
197 find_inferior_id (int num)
198 {
199 struct inferior *inf;
200
201 for (inf = inferior_list; inf; inf = inf->next)
202 if (inf->num == num)
203 return inf;
204
205 return NULL;
206 }
207
208 struct inferior *
209 find_inferior_pid (int pid)
210 {
211 struct inferior *inf;
212
213 for (inf = inferior_list; inf; inf = inf->next)
214 if (inf->pid == pid)
215 return inf;
216
217 return NULL;
218 }
219
220 struct inferior *
221 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
222 void *data)
223 {
224 struct inferior *inf, *infnext;
225
226 for (inf = inferior_list; inf; inf = infnext)
227 {
228 infnext = inf->next;
229 if ((*callback) (inf, data))
230 return inf;
231 }
232
233 return NULL;
234 }
235
236 int
237 valid_gdb_inferior_id (int num)
238 {
239 struct inferior *inf;
240
241 for (inf = inferior_list; inf; inf = inf->next)
242 if (inf->num == num)
243 return 1;
244
245 return 0;
246 }
247
248 int
249 pid_to_gdb_inferior_id (int pid)
250 {
251 struct inferior *inf;
252
253 for (inf = inferior_list; inf; inf = inf->next)
254 if (inf->pid == pid)
255 return inf->num;
256
257 return 0;
258 }
259
260 int
261 gdb_inferior_id_to_pid (int num)
262 {
263 struct inferior *inferior = find_inferior_id (num);
264 if (inferior)
265 return inferior->pid;
266 else
267 return -1;
268 }
269
270 int
271 in_inferior_list (int pid)
272 {
273 struct inferior *inf;
274
275 for (inf = inferior_list; inf; inf = inf->next)
276 if (inf->pid == pid)
277 return 1;
278
279 return 0;
280 }
281
282 int
283 have_inferiors (void)
284 {
285 return inferior_list != NULL;
286 }
287
288 int
289 have_live_inferiors (void)
290 {
291 /* The check on stratum suffices, as GDB doesn't currently support
292 multiple target interfaces. */
293 return (current_target.to_stratum >= process_stratum && have_inferiors ());
294 }
295
296 /* Prints the list of inferiors and their details on UIOUT. This is a
297 version of 'info_inferior_command' suitable for use from MI.
298
299 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
300 should be printed. Otherwise, all inferiors are printed. */
301 void
302 print_inferior (struct ui_out *uiout, int requested_inferior)
303 {
304 struct inferior *inf;
305 struct cleanup *old_chain;
306 int inf_count = 0;
307
308 /* Compute number of inferiors we will print. */
309 for (inf = inferior_list; inf; inf = inf->next)
310 {
311 struct cleanup *chain2;
312
313 if (requested_inferior != -1 && inf->num != requested_inferior)
314 continue;
315
316 ++inf_count;
317 }
318
319 if (inf_count == 0)
320 {
321 ui_out_message (uiout, 0, "No inferiors.\n");
322 return;
323 }
324
325 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, inf_count,
326 "inferiors");
327 ui_out_table_header (uiout, 1, ui_left, "current", "");
328 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
329 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
330 ui_out_table_body (uiout);
331
332 for (inf = inferior_list; inf; inf = inf->next)
333 {
334 struct cleanup *chain2;
335
336 if (requested_inferior != -1 && inf->num != requested_inferior)
337 continue;
338
339 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
340
341 if (inf->pid == ptid_get_pid (inferior_ptid))
342 ui_out_field_string (uiout, "current", "*");
343 else
344 ui_out_field_skip (uiout, "current");
345
346 ui_out_field_int (uiout, "number", inf->num);
347 ui_out_field_string (uiout, "target-id",
348 target_pid_to_str (pid_to_ptid (inf->pid)));
349
350 ui_out_text (uiout, "\n");
351 do_cleanups (chain2);
352 }
353
354 if (inferior_list
355 && ptid_equal (inferior_ptid, null_ptid))
356 ui_out_message (uiout, 0, "\n\
357 No selected inferior/thread. See `help thread' or `help inferior'.\n");
358
359 do_cleanups (old_chain);
360 }
361
362 static void
363 detach_inferior_command (char *args, int from_tty)
364 {
365 int num, pid;
366 struct thread_info *tp;
367
368 if (!args || !*args)
369 error (_("Requires argument (inferior id to detach)"));
370
371 num = parse_and_eval_long (args);
372
373 if (!valid_gdb_inferior_id (num))
374 error (_("Inferior ID %d not known."), num);
375
376 pid = gdb_inferior_id_to_pid (num);
377
378 tp = any_thread_of_process (pid);
379 if (!tp)
380 error (_("Inferior has no threads."));
381
382 switch_to_thread (tp->ptid);
383
384 detach_command (NULL, from_tty);
385 }
386
387 static void
388 kill_inferior_command (char *args, int from_tty)
389 {
390 int num, pid;
391 struct thread_info *tp;
392
393 if (!args || !*args)
394 error (_("Requires argument (inferior id to kill)"));
395
396 num = parse_and_eval_long (args);
397
398 if (!valid_gdb_inferior_id (num))
399 error (_("Inferior ID %d not known."), num);
400
401 pid = gdb_inferior_id_to_pid (num);
402
403 tp = any_thread_of_process (pid);
404 if (!tp)
405 error (_("Inferior has no threads."));
406
407 switch_to_thread (tp->ptid);
408
409 target_kill ();
410
411 bfd_cache_close_all ();
412 }
413
414 static void
415 inferior_command (char *args, int from_tty)
416 {
417 int num, pid;
418
419 if (!have_inferiors ())
420 error (_("No inferiors"));
421
422 num = parse_and_eval_long (args);
423
424 if (!valid_gdb_inferior_id (num))
425 error (_("Inferior ID %d not known."), num);
426
427 pid = gdb_inferior_id_to_pid (num);
428
429 if (pid != ptid_get_pid (inferior_ptid))
430 {
431 struct thread_info *tp;
432
433 tp = any_thread_of_process (pid);
434 if (!tp)
435 error (_("Inferior has no threads."));
436
437 switch_to_thread (tp->ptid);
438 }
439
440 printf_filtered (_("[Switching to thread %d (%s)] "),
441 pid_to_thread_id (inferior_ptid),
442 target_pid_to_str (inferior_ptid));
443
444 if (is_running (inferior_ptid))
445 ui_out_text (uiout, "(running)\n");
446 else
447 {
448 ui_out_text (uiout, "\n");
449 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
450 }
451 }
452
453 /* Print information about currently known inferiors. */
454
455 static void
456 info_inferiors_command (char *args, int from_tty)
457 {
458 int requested = -1;
459
460 if (args && *args)
461 {
462 requested = parse_and_eval_long (args);
463 if (!valid_gdb_inferior_id (requested))
464 error (_("Inferior ID %d not known."), requested);
465 }
466
467 print_inferior (uiout, requested);
468 }
469
470 /* Print notices when new inferiors are created and die. */
471 static void
472 show_print_inferior_events (struct ui_file *file, int from_tty,
473 struct cmd_list_element *c, const char *value)
474 {
475 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
476 }
477
478 void
479 _initialize_inferiors (void)
480 {
481 add_info ("inferiors", info_inferiors_command,
482 _("IDs of currently known inferiors."));
483
484 add_setshow_boolean_cmd ("inferior-events", no_class,
485 &print_inferior_events, _("\
486 Set printing of inferior events (e.g., inferior start and exit)."), _("\
487 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
488 NULL,
489 show_print_inferior_events,
490 &setprintlist, &showprintlist);
491
492 add_cmd ("inferior", class_run, detach_inferior_command, _("\
493 Detach from inferior ID."),
494 &detachlist);
495
496 add_cmd ("inferior", class_run, kill_inferior_command, _("\
497 Kill inferior ID."),
498 &killlist);
499
500 add_cmd ("inferior", class_run, inferior_command, _("\
501 Use this command to switch between inferiors.\n\
502 The new inferior ID must be currently known."),
503 &cmdlist);
504 }