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