]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inferior.c
* gdbarch.sh (current_gdbarch): Remove global variable.
[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
c35b1492
PA
287int
288have_live_inferiors (void)
289{
290 /* The check on stratum suffices, as GDB doesn't currently support
291 multiple target interfaces. */
292 return (current_target.to_stratum >= process_stratum && have_inferiors ());
293}
294
b77209e0
PA
295/* Prints the list of inferiors and their details on UIOUT. This is a
296 version of 'info_inferior_command' suitable for use from MI.
297
298 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
299 should be printed. Otherwise, all inferiors are printed. */
300void
301print_inferior (struct ui_out *uiout, int requested_inferior)
302{
303 struct inferior *inf;
304 struct cleanup *old_chain;
8bb318c6 305 int inf_count = 0;
b77209e0 306
8bb318c6
TT
307 /* Compute number of inferiors we will print. */
308 for (inf = inferior_list; inf; inf = inf->next)
309 {
310 struct cleanup *chain2;
311
312 if (requested_inferior != -1 && inf->num != requested_inferior)
313 continue;
314
315 ++inf_count;
316 }
317
318 if (inf_count == 0)
319 {
320 ui_out_message (uiout, 0, "No inferiors.\n");
321 return;
322 }
323
324 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, inf_count,
325 "inferiors");
326 ui_out_table_header (uiout, 3, ui_right, "current", "Cur");
327 ui_out_table_header (uiout, 4, ui_right, "id", "Id");
328 ui_out_table_header (uiout, 7, ui_right, "target-id", "PID");
329 ui_out_table_body (uiout);
b77209e0
PA
330
331 for (inf = inferior_list; inf; inf = inf->next)
332 {
333 struct cleanup *chain2;
334
335 if (requested_inferior != -1 && inf->num != requested_inferior)
336 continue;
337
338 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
339
340 if (inf->pid == ptid_get_pid (inferior_ptid))
8bb318c6 341 ui_out_field_string (uiout, "current", "*");
b77209e0 342 else
8bb318c6 343 ui_out_field_skip (uiout, "current");
b77209e0
PA
344
345 ui_out_field_int (uiout, "id", inf->num);
b77209e0
PA
346 ui_out_field_int (uiout, "target-id", inf->pid);
347
348 ui_out_text (uiout, "\n");
349 do_cleanups (chain2);
350 }
351
352 do_cleanups (old_chain);
353}
354
355/* Print information about currently known inferiors. */
356
357static void
358info_inferiors_command (char *arg, int from_tty)
359{
360 print_inferior (uiout, -1);
361}
362
363/* Print notices when new inferiors are created and die. */
364static void
365show_print_inferior_events (struct ui_file *file, int from_tty,
366 struct cmd_list_element *c, const char *value)
367{
368 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
369}
370
371void
372_initialize_inferiors (void)
373{
374 add_info ("inferiors", info_inferiors_command,
375 _("IDs of currently known inferiors."));
376
377 add_setshow_boolean_cmd ("inferior-events", no_class,
378 &print_inferior_events, _("\
379Set printing of inferior events (e.g., inferior start and exit)."), _("\
380Show printing of inferior events (e.g., inferior start and exit)."), NULL,
381 NULL,
382 show_print_inferior_events,
383 &setprintlist, &showprintlist);
384}