]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inferior.c
* gdb/score-tdep.c: Delete dead codes.
[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"
2277426b 28#include "gdbthread.h"
b77209e0
PA
29
30void _initialize_inferiors (void);
31
32static struct inferior *inferior_list = NULL;
33static int highest_inferior_num;
34
35/* Print notices on inferior events (attach, detach, etc.), set with
36 `set print inferior-events'. */
37static int print_inferior_events = 0;
38
39struct inferior*
40current_inferior (void)
41{
42 struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
43 gdb_assert (inf);
44 return inf;
45}
46
47static void
48free_inferior (struct inferior *inf)
49{
e0ba6746 50 discard_all_inferior_continuations (inf);
b77209e0
PA
51 xfree (inf->private);
52 xfree (inf);
53}
54
55void
56init_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
73struct inferior *
74add_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
d6b48e9c
PA
82 inf->stop_soon = NO_STOP_QUIETLY;
83
b77209e0
PA
84 inf->num = ++highest_inferior_num;
85 inf->next = inferior_list;
86 inferior_list = inf;
87
a562dc8f
PA
88 observer_notify_new_inferior (pid);
89
b77209e0
PA
90 return inf;
91}
92
93struct inferior *
94add_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
104struct delete_thread_of_inferior_arg
105{
106 int pid;
107 int silent;
108};
109
110static int
111delete_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. */
128static void
129delete_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
b77209e0
PA
143 arg.pid = pid;
144 arg.silent = silent;
145
146 iterate_over_threads (delete_thread_of_inferior, &arg);
4a92f99b 147
7e1789f5
PA
148 /* Notify the observers before removing the inferior from the list,
149 so that the observers have a change to look it up. */
4a92f99b 150 observer_notify_inferior_exit (pid);
7e1789f5
PA
151
152 if (infprev)
153 infprev->next = inf->next;
154 else
155 inferior_list = inf->next;
156
157 free_inferior (inf);
b77209e0
PA
158}
159
160void
161delete_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
169void
170delete_inferior_silent (int pid)
171{
172 delete_inferior_1 (pid, 1);
173}
174
175void
176detach_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
82f73884
PA
184void
185discard_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
b77209e0
PA
196static struct inferior *
197find_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
208struct inferior *
209find_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
220struct inferior *
221iterate_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
236int
237valid_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
248int
249pid_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
260int
261gdb_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
270int
271in_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
282int
283have_inferiors (void)
284{
285 return inferior_list != NULL;
286}
287
c35b1492
PA
288int
289have_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
b77209e0
PA
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. */
301void
302print_inferior (struct ui_out *uiout, int requested_inferior)
303{
304 struct inferior *inf;
305 struct cleanup *old_chain;
8bb318c6 306 int inf_count = 0;
b77209e0 307
8bb318c6
TT
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");
3a1ff0b6
PA
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");
8bb318c6 330 ui_out_table_body (uiout);
b77209e0
PA
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))
8bb318c6 342 ui_out_field_string (uiout, "current", "*");
b77209e0 343 else
8bb318c6 344 ui_out_field_skip (uiout, "current");
b77209e0 345
3a1ff0b6
PA
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)));
b77209e0
PA
349
350 ui_out_text (uiout, "\n");
351 do_cleanups (chain2);
352 }
353
3a1ff0b6
PA
354 if (inferior_list
355 && ptid_equal (inferior_ptid, null_ptid))
356 ui_out_message (uiout, 0, "\n\
357No selected inferior/thread. See `help thread' or `help inferior'.\n");
358
b77209e0
PA
359 do_cleanups (old_chain);
360}
361
2277426b
PA
362static void
363detach_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
387static void
388kill_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
414static void
415inferior_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
b77209e0
PA
453/* Print information about currently known inferiors. */
454
455static void
2277426b 456info_inferiors_command (char *args, int from_tty)
b77209e0 457{
2277426b
PA
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);
b77209e0
PA
468}
469
470/* Print notices when new inferiors are created and die. */
471static void
472show_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
478void
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, _("\
486Set printing of inferior events (e.g., inferior start and exit)."), _("\
487Show printing of inferior events (e.g., inferior start and exit)."), NULL,
488 NULL,
489 show_print_inferior_events,
490 &setprintlist, &showprintlist);
2277426b
PA
491
492 add_cmd ("inferior", class_run, detach_inferior_command, _("\
493Detach from inferior ID."),
494 &detachlist);
495
496 add_cmd ("inferior", class_run, kill_inferior_command, _("\
497Kill inferior ID."),
498 &killlist);
499
500 add_cmd ("inferior", class_run, inferior_command, _("\
501Use this command to switch between inferiors.\n\
502The new inferior ID must be currently known."),
503 &cmdlist);
b77209e0 504}