]>
Commit | Line | Data |
---|---|---|
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 | |
29 | void _initialize_inferiors (void); | |
30 | ||
31 | static struct inferior *inferior_list = NULL; | |
32 | static int highest_inferior_num; | |
33 | ||
34 | /* Print notices on inferior events (attach, detach, etc.), set with | |
35 | `set print inferior-events'. */ | |
36 | static int print_inferior_events = 0; | |
37 | ||
38 | struct inferior* | |
39 | current_inferior (void) | |
40 | { | |
41 | struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid)); | |
42 | gdb_assert (inf); | |
43 | return inf; | |
44 | } | |
45 | ||
46 | static void | |
47 | free_inferior (struct inferior *inf) | |
48 | { | |
e0ba6746 | 49 | discard_all_inferior_continuations (inf); |
b77209e0 PA |
50 | xfree (inf->private); |
51 | xfree (inf); | |
52 | } | |
53 | ||
54 | void | |
55 | init_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 | ||
72 | struct inferior * | |
73 | add_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 | ||
92 | struct inferior * | |
93 | add_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 | ||
103 | struct delete_thread_of_inferior_arg | |
104 | { | |
105 | int pid; | |
106 | int silent; | |
107 | }; | |
108 | ||
109 | static int | |
110 | delete_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. */ | |
127 | static void | |
128 | delete_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 | ||
159 | void | |
160 | delete_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 | ||
168 | void | |
169 | delete_inferior_silent (int pid) | |
170 | { | |
171 | delete_inferior_1 (pid, 1); | |
172 | } | |
173 | ||
174 | void | |
175 | detach_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 |
183 | void |
184 | discard_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 |
195 | static struct inferior * |
196 | find_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 | ||
207 | struct inferior * | |
208 | find_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 | ||
219 | struct inferior * | |
220 | iterate_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 | ||
235 | int | |
236 | valid_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 | ||
247 | int | |
248 | pid_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 | ||
259 | int | |
260 | gdb_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 | ||
269 | int | |
270 | in_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 | ||
281 | int | |
282 | have_inferiors (void) | |
283 | { | |
284 | return inferior_list != NULL; | |
285 | } | |
286 | ||
c35b1492 PA |
287 | int |
288 | have_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. */ | |
300 | void | |
301 | print_inferior (struct ui_out *uiout, int requested_inferior) | |
302 | { | |
303 | struct inferior *inf; | |
304 | struct cleanup *old_chain; | |
305 | ||
306 | old_chain = make_cleanup_ui_out_list_begin_end (uiout, "inferiors"); | |
307 | ||
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 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
316 | ||
317 | if (inf->pid == ptid_get_pid (inferior_ptid)) | |
318 | ui_out_text (uiout, "* "); | |
319 | else | |
320 | ui_out_text (uiout, " "); | |
321 | ||
322 | ui_out_field_int (uiout, "id", inf->num); | |
323 | ui_out_text (uiout, " "); | |
324 | ui_out_field_int (uiout, "target-id", inf->pid); | |
325 | ||
326 | ui_out_text (uiout, "\n"); | |
327 | do_cleanups (chain2); | |
328 | } | |
329 | ||
330 | do_cleanups (old_chain); | |
331 | } | |
332 | ||
333 | /* Print information about currently known inferiors. */ | |
334 | ||
335 | static void | |
336 | info_inferiors_command (char *arg, int from_tty) | |
337 | { | |
338 | print_inferior (uiout, -1); | |
339 | } | |
340 | ||
341 | /* Print notices when new inferiors are created and die. */ | |
342 | static void | |
343 | show_print_inferior_events (struct ui_file *file, int from_tty, | |
344 | struct cmd_list_element *c, const char *value) | |
345 | { | |
346 | fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value); | |
347 | } | |
348 | ||
349 | void | |
350 | _initialize_inferiors (void) | |
351 | { | |
352 | add_info ("inferiors", info_inferiors_command, | |
353 | _("IDs of currently known inferiors.")); | |
354 | ||
355 | add_setshow_boolean_cmd ("inferior-events", no_class, | |
356 | &print_inferior_events, _("\ | |
357 | Set printing of inferior events (e.g., inferior start and exit)."), _("\ | |
358 | Show printing of inferior events (e.g., inferior start and exit)."), NULL, | |
359 | NULL, | |
360 | show_print_inferior_events, | |
361 | &setprintlist, &showprintlist); | |
362 | } |