Commit | Line | Data |
---|---|---|
b77209e0 PA |
1 | /* Multi-process control for GDB, the GNU debugger. |
2 | ||
0b302171 | 3 | Copyright (C) 2008-2012 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" | |
6c95b8df | 21 | #include "exec.h" |
b77209e0 PA |
22 | #include "inferior.h" |
23 | #include "target.h" | |
24 | #include "command.h" | |
25 | #include "gdbcmd.h" | |
26 | #include "gdbthread.h" | |
27 | #include "ui-out.h" | |
4a92f99b | 28 | #include "observer.h" |
2277426b | 29 | #include "gdbthread.h" |
6c95b8df PA |
30 | #include "gdbcore.h" |
31 | #include "symfile.h" | |
3f81c18a | 32 | #include "environ.h" |
c82c0b55 | 33 | #include "cli/cli-utils.h" |
be34f849 | 34 | #include "continuations.h" |
b77209e0 PA |
35 | |
36 | void _initialize_inferiors (void); | |
37 | ||
6c95b8df PA |
38 | static void inferior_alloc_data (struct inferior *inf); |
39 | static void inferior_free_data (struct inferior *inf); | |
40 | ||
41 | struct inferior *inferior_list = NULL; | |
b77209e0 PA |
42 | static int highest_inferior_num; |
43 | ||
44 | /* Print notices on inferior events (attach, detach, etc.), set with | |
45 | `set print inferior-events'. */ | |
46 | static int print_inferior_events = 0; | |
47 | ||
6c95b8df PA |
48 | /* The Current Inferior. */ |
49 | static struct inferior *current_inferior_ = NULL; | |
50 | ||
b77209e0 PA |
51 | struct inferior* |
52 | current_inferior (void) | |
53 | { | |
6c95b8df PA |
54 | return current_inferior_; |
55 | } | |
56 | ||
57 | void | |
58 | set_current_inferior (struct inferior *inf) | |
59 | { | |
60 | /* There's always an inferior. */ | |
61 | gdb_assert (inf != NULL); | |
62 | ||
63 | current_inferior_ = inf; | |
64 | } | |
65 | ||
66 | /* A cleanups callback, helper for save_current_program_space | |
67 | below. */ | |
68 | ||
69 | static void | |
70 | restore_inferior (void *arg) | |
71 | { | |
72 | struct inferior *saved_inferior = arg; | |
abbb1732 | 73 | |
6c95b8df PA |
74 | set_current_inferior (saved_inferior); |
75 | } | |
76 | ||
77 | /* Save the current program space so that it may be restored by a later | |
78 | call to do_cleanups. Returns the struct cleanup pointer needed for | |
79 | later doing the cleanup. */ | |
80 | ||
81 | struct cleanup * | |
82 | save_current_inferior (void) | |
83 | { | |
84 | struct cleanup *old_chain = make_cleanup (restore_inferior, | |
85 | current_inferior_); | |
abbb1732 | 86 | |
6c95b8df | 87 | return old_chain; |
b77209e0 PA |
88 | } |
89 | ||
90 | static void | |
91 | free_inferior (struct inferior *inf) | |
92 | { | |
e0ba6746 | 93 | discard_all_inferior_continuations (inf); |
6c95b8df | 94 | inferior_free_data (inf); |
3f81c18a VP |
95 | xfree (inf->args); |
96 | xfree (inf->terminal); | |
97 | free_environ (inf->environment); | |
b77209e0 PA |
98 | xfree (inf->private); |
99 | xfree (inf); | |
100 | } | |
101 | ||
102 | void | |
103 | init_inferior_list (void) | |
104 | { | |
105 | struct inferior *inf, *infnext; | |
106 | ||
107 | highest_inferior_num = 0; | |
108 | if (!inferior_list) | |
109 | return; | |
110 | ||
111 | for (inf = inferior_list; inf; inf = infnext) | |
112 | { | |
113 | infnext = inf->next; | |
114 | free_inferior (inf); | |
115 | } | |
116 | ||
117 | inferior_list = NULL; | |
118 | } | |
119 | ||
120 | struct inferior * | |
121 | add_inferior_silent (int pid) | |
122 | { | |
123 | struct inferior *inf; | |
124 | ||
125 | inf = xmalloc (sizeof (*inf)); | |
126 | memset (inf, 0, sizeof (*inf)); | |
127 | inf->pid = pid; | |
128 | ||
16c381f0 | 129 | inf->control.stop_soon = NO_STOP_QUIETLY; |
d6b48e9c | 130 | |
b77209e0 PA |
131 | inf->num = ++highest_inferior_num; |
132 | inf->next = inferior_list; | |
133 | inferior_list = inf; | |
134 | ||
3f81c18a VP |
135 | inf->environment = make_environ (); |
136 | init_environ (inf->environment); | |
137 | ||
6c95b8df PA |
138 | inferior_alloc_data (inf); |
139 | ||
a79b8f6e VP |
140 | observer_notify_inferior_added (inf); |
141 | ||
6c95b8df PA |
142 | if (pid != 0) |
143 | inferior_appeared (inf, pid); | |
a562dc8f | 144 | |
b77209e0 PA |
145 | return inf; |
146 | } | |
147 | ||
148 | struct inferior * | |
149 | add_inferior (int pid) | |
150 | { | |
151 | struct inferior *inf = add_inferior_silent (pid); | |
152 | ||
153 | if (print_inferior_events) | |
154 | printf_unfiltered (_("[New inferior %d]\n"), pid); | |
155 | ||
156 | return inf; | |
157 | } | |
158 | ||
159 | struct delete_thread_of_inferior_arg | |
160 | { | |
161 | int pid; | |
162 | int silent; | |
163 | }; | |
164 | ||
165 | static int | |
166 | delete_thread_of_inferior (struct thread_info *tp, void *data) | |
167 | { | |
168 | struct delete_thread_of_inferior_arg *arg = data; | |
169 | ||
170 | if (ptid_get_pid (tp->ptid) == arg->pid) | |
171 | { | |
172 | if (arg->silent) | |
173 | delete_thread_silent (tp->ptid); | |
174 | else | |
175 | delete_thread (tp->ptid); | |
176 | } | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | /* If SILENT then be quiet -- don't announce a inferior death, or the | |
182 | exit of its threads. */ | |
6c95b8df | 183 | |
a79b8f6e | 184 | void |
6c95b8df | 185 | delete_inferior_1 (struct inferior *todel, int silent) |
b77209e0 PA |
186 | { |
187 | struct inferior *inf, *infprev; | |
6c95b8df | 188 | struct delete_thread_of_inferior_arg arg; |
b77209e0 PA |
189 | |
190 | infprev = NULL; | |
191 | ||
192 | for (inf = inferior_list; inf; infprev = inf, inf = inf->next) | |
6c95b8df | 193 | if (inf == todel) |
b77209e0 PA |
194 | break; |
195 | ||
196 | if (!inf) | |
197 | return; | |
198 | ||
6c95b8df | 199 | arg.pid = inf->pid; |
b77209e0 PA |
200 | arg.silent = silent; |
201 | ||
202 | iterate_over_threads (delete_thread_of_inferior, &arg); | |
4a92f99b | 203 | |
7e1789f5 PA |
204 | if (infprev) |
205 | infprev->next = inf->next; | |
206 | else | |
207 | inferior_list = inf->next; | |
208 | ||
a79b8f6e VP |
209 | observer_notify_inferior_removed (inf); |
210 | ||
7e1789f5 | 211 | free_inferior (inf); |
b77209e0 PA |
212 | } |
213 | ||
214 | void | |
215 | delete_inferior (int pid) | |
216 | { | |
6c95b8df PA |
217 | struct inferior *inf = find_inferior_pid (pid); |
218 | ||
219 | delete_inferior_1 (inf, 0); | |
b77209e0 PA |
220 | |
221 | if (print_inferior_events) | |
222 | printf_unfiltered (_("[Inferior %d exited]\n"), pid); | |
223 | } | |
224 | ||
225 | void | |
226 | delete_inferior_silent (int pid) | |
227 | { | |
6c95b8df PA |
228 | struct inferior *inf = find_inferior_pid (pid); |
229 | ||
230 | delete_inferior_1 (inf, 1); | |
231 | } | |
232 | ||
233 | ||
234 | /* If SILENT then be quiet -- don't announce a inferior exit, or the | |
235 | exit of its threads. */ | |
236 | ||
237 | static void | |
238 | exit_inferior_1 (struct inferior *inftoex, int silent) | |
239 | { | |
240 | struct inferior *inf; | |
241 | struct delete_thread_of_inferior_arg arg; | |
242 | ||
243 | for (inf = inferior_list; inf; inf = inf->next) | |
244 | if (inf == inftoex) | |
245 | break; | |
246 | ||
247 | if (!inf) | |
248 | return; | |
249 | ||
250 | arg.pid = inf->pid; | |
251 | arg.silent = silent; | |
252 | ||
253 | iterate_over_threads (delete_thread_of_inferior, &arg); | |
254 | ||
255 | /* Notify the observers before removing the inferior from the list, | |
256 | so that the observers have a chance to look it up. */ | |
a79b8f6e | 257 | observer_notify_inferior_exit (inf); |
6c95b8df PA |
258 | |
259 | inf->pid = 0; | |
e714e1bf | 260 | inf->fake_pid_p = 0; |
6c95b8df PA |
261 | if (inf->vfork_parent != NULL) |
262 | { | |
263 | inf->vfork_parent->vfork_child = NULL; | |
264 | inf->vfork_parent = NULL; | |
265 | } | |
8cf64490 TT |
266 | |
267 | inf->has_exit_code = 0; | |
268 | inf->exit_code = 0; | |
6c95b8df PA |
269 | } |
270 | ||
271 | void | |
272 | exit_inferior (int pid) | |
273 | { | |
274 | struct inferior *inf = find_inferior_pid (pid); | |
abbb1732 | 275 | |
6c95b8df PA |
276 | exit_inferior_1 (inf, 0); |
277 | ||
278 | if (print_inferior_events) | |
279 | printf_unfiltered (_("[Inferior %d exited]\n"), pid); | |
280 | } | |
281 | ||
282 | void | |
283 | exit_inferior_silent (int pid) | |
284 | { | |
285 | struct inferior *inf = find_inferior_pid (pid); | |
abbb1732 | 286 | |
6c95b8df PA |
287 | exit_inferior_1 (inf, 1); |
288 | } | |
289 | ||
290 | void | |
291 | exit_inferior_num_silent (int num) | |
292 | { | |
293 | struct inferior *inf = find_inferior_id (num); | |
294 | ||
295 | exit_inferior_1 (inf, 1); | |
b77209e0 PA |
296 | } |
297 | ||
298 | void | |
299 | detach_inferior (int pid) | |
300 | { | |
6c95b8df | 301 | struct inferior *inf = find_inferior_pid (pid); |
abbb1732 | 302 | |
6c95b8df | 303 | exit_inferior_1 (inf, 1); |
b77209e0 PA |
304 | |
305 | if (print_inferior_events) | |
306 | printf_unfiltered (_("[Inferior %d detached]\n"), pid); | |
307 | } | |
308 | ||
6c95b8df PA |
309 | void |
310 | inferior_appeared (struct inferior *inf, int pid) | |
311 | { | |
312 | inf->pid = pid; | |
313 | ||
a79b8f6e | 314 | observer_notify_inferior_appeared (inf); |
6c95b8df PA |
315 | } |
316 | ||
82f73884 PA |
317 | void |
318 | discard_all_inferiors (void) | |
319 | { | |
6c95b8df | 320 | struct inferior *inf; |
82f73884 | 321 | |
6c95b8df | 322 | for (inf = inferior_list; inf; inf = inf->next) |
82f73884 | 323 | { |
6c95b8df PA |
324 | if (inf->pid != 0) |
325 | exit_inferior_silent (inf->pid); | |
82f73884 PA |
326 | } |
327 | } | |
328 | ||
6c95b8df | 329 | struct inferior * |
b77209e0 PA |
330 | find_inferior_id (int num) |
331 | { | |
332 | struct inferior *inf; | |
333 | ||
334 | for (inf = inferior_list; inf; inf = inf->next) | |
335 | if (inf->num == num) | |
336 | return inf; | |
337 | ||
338 | return NULL; | |
339 | } | |
340 | ||
341 | struct inferior * | |
342 | find_inferior_pid (int pid) | |
343 | { | |
344 | struct inferior *inf; | |
345 | ||
6c95b8df PA |
346 | /* Looking for inferior pid == 0 is always wrong, and indicative of |
347 | a bug somewhere else. There may be more than one with pid == 0, | |
348 | for instance. */ | |
349 | gdb_assert (pid != 0); | |
350 | ||
b77209e0 PA |
351 | for (inf = inferior_list; inf; inf = inf->next) |
352 | if (inf->pid == pid) | |
353 | return inf; | |
354 | ||
355 | return NULL; | |
356 | } | |
357 | ||
6c95b8df PA |
358 | /* Find an inferior bound to PSPACE. */ |
359 | ||
360 | struct inferior * | |
361 | find_inferior_for_program_space (struct program_space *pspace) | |
362 | { | |
363 | struct inferior *inf; | |
364 | ||
365 | for (inf = inferior_list; inf != NULL; inf = inf->next) | |
366 | { | |
367 | if (inf->pspace == pspace) | |
368 | return inf; | |
369 | } | |
370 | ||
371 | return NULL; | |
372 | } | |
373 | ||
b77209e0 PA |
374 | struct inferior * |
375 | iterate_over_inferiors (int (*callback) (struct inferior *, void *), | |
376 | void *data) | |
377 | { | |
378 | struct inferior *inf, *infnext; | |
379 | ||
380 | for (inf = inferior_list; inf; inf = infnext) | |
381 | { | |
382 | infnext = inf->next; | |
383 | if ((*callback) (inf, data)) | |
384 | return inf; | |
385 | } | |
386 | ||
387 | return NULL; | |
388 | } | |
389 | ||
390 | int | |
391 | valid_gdb_inferior_id (int num) | |
392 | { | |
393 | struct inferior *inf; | |
394 | ||
395 | for (inf = inferior_list; inf; inf = inf->next) | |
396 | if (inf->num == num) | |
397 | return 1; | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
402 | int | |
403 | pid_to_gdb_inferior_id (int pid) | |
404 | { | |
405 | struct inferior *inf; | |
406 | ||
407 | for (inf = inferior_list; inf; inf = inf->next) | |
408 | if (inf->pid == pid) | |
409 | return inf->num; | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | int | |
415 | gdb_inferior_id_to_pid (int num) | |
416 | { | |
417 | struct inferior *inferior = find_inferior_id (num); | |
418 | if (inferior) | |
419 | return inferior->pid; | |
420 | else | |
421 | return -1; | |
422 | } | |
423 | ||
424 | int | |
425 | in_inferior_list (int pid) | |
426 | { | |
427 | struct inferior *inf; | |
428 | ||
429 | for (inf = inferior_list; inf; inf = inf->next) | |
430 | if (inf->pid == pid) | |
431 | return 1; | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
436 | int | |
437 | have_inferiors (void) | |
438 | { | |
6c95b8df PA |
439 | struct inferior *inf; |
440 | ||
441 | for (inf = inferior_list; inf; inf = inf->next) | |
442 | if (inf->pid != 0) | |
443 | return 1; | |
444 | ||
445 | return 0; | |
b77209e0 PA |
446 | } |
447 | ||
c35b1492 PA |
448 | int |
449 | have_live_inferiors (void) | |
450 | { | |
cd2effb2 | 451 | struct inferior *inf; |
6c95b8df | 452 | |
cd2effb2 JK |
453 | for (inf = inferior_list; inf; inf = inf->next) |
454 | if (inf->pid != 0) | |
455 | { | |
456 | struct thread_info *tp; | |
457 | ||
458 | tp = any_thread_of_process (inf->pid); | |
aeaec162 TT |
459 | if (tp && target_has_execution_1 (tp->ptid)) |
460 | break; | |
cd2effb2 JK |
461 | } |
462 | ||
cd2effb2 | 463 | return inf != NULL; |
6c95b8df PA |
464 | } |
465 | ||
466 | /* Prune away automatically added program spaces that aren't required | |
467 | anymore. */ | |
468 | ||
469 | void | |
470 | prune_inferiors (void) | |
471 | { | |
472 | struct inferior *ss, **ss_link; | |
473 | struct inferior *current = current_inferior (); | |
474 | ||
475 | ss = inferior_list; | |
476 | ss_link = &inferior_list; | |
477 | while (ss) | |
478 | { | |
479 | if (ss == current | |
480 | || !ss->removable | |
481 | || ss->pid != 0) | |
482 | { | |
483 | ss_link = &ss->next; | |
484 | ss = *ss_link; | |
485 | continue; | |
486 | } | |
487 | ||
488 | *ss_link = ss->next; | |
489 | delete_inferior_1 (ss, 1); | |
490 | ss = *ss_link; | |
491 | } | |
492 | ||
493 | prune_program_spaces (); | |
494 | } | |
495 | ||
496 | /* Simply returns the count of inferiors. */ | |
497 | ||
498 | int | |
499 | number_of_inferiors (void) | |
500 | { | |
501 | struct inferior *inf; | |
502 | int count = 0; | |
503 | ||
504 | for (inf = inferior_list; inf != NULL; inf = inf->next) | |
505 | count++; | |
506 | ||
507 | return count; | |
c35b1492 PA |
508 | } |
509 | ||
db2b9fdd PA |
510 | /* Converts an inferior process id to a string. Like |
511 | target_pid_to_str, but special cases the null process. */ | |
512 | ||
513 | static char * | |
514 | inferior_pid_to_str (int pid) | |
515 | { | |
516 | if (pid != 0) | |
517 | return target_pid_to_str (pid_to_ptid (pid)); | |
518 | else | |
519 | return _("<null>"); | |
520 | } | |
521 | ||
b77209e0 PA |
522 | /* Prints the list of inferiors and their details on UIOUT. This is a |
523 | version of 'info_inferior_command' suitable for use from MI. | |
524 | ||
c82c0b55 MS |
525 | If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the |
526 | inferiors that should be printed. Otherwise, all inferiors are | |
527 | printed. */ | |
528 | ||
529 | static void | |
530 | print_inferior (struct ui_out *uiout, char *requested_inferiors) | |
b77209e0 PA |
531 | { |
532 | struct inferior *inf; | |
533 | struct cleanup *old_chain; | |
8bb318c6 | 534 | int inf_count = 0; |
b77209e0 | 535 | |
8bb318c6 TT |
536 | /* Compute number of inferiors we will print. */ |
537 | for (inf = inferior_list; inf; inf = inf->next) | |
538 | { | |
c82c0b55 | 539 | if (!number_is_in_list (requested_inferiors, inf->num)) |
8bb318c6 TT |
540 | continue; |
541 | ||
542 | ++inf_count; | |
543 | } | |
544 | ||
545 | if (inf_count == 0) | |
546 | { | |
547 | ui_out_message (uiout, 0, "No inferiors.\n"); | |
548 | return; | |
549 | } | |
550 | ||
6c95b8df | 551 | old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count, |
8bb318c6 | 552 | "inferiors"); |
3a1ff0b6 PA |
553 | ui_out_table_header (uiout, 1, ui_left, "current", ""); |
554 | ui_out_table_header (uiout, 4, ui_left, "number", "Num"); | |
555 | ui_out_table_header (uiout, 17, ui_left, "target-id", "Description"); | |
6c95b8df | 556 | ui_out_table_header (uiout, 17, ui_left, "exec", "Executable"); |
b77209e0 | 557 | |
6c95b8df | 558 | ui_out_table_body (uiout); |
b77209e0 PA |
559 | for (inf = inferior_list; inf; inf = inf->next) |
560 | { | |
561 | struct cleanup *chain2; | |
562 | ||
c82c0b55 | 563 | if (!number_is_in_list (requested_inferiors, inf->num)) |
b77209e0 PA |
564 | continue; |
565 | ||
566 | chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
567 | ||
6c95b8df | 568 | if (inf == current_inferior ()) |
8bb318c6 | 569 | ui_out_field_string (uiout, "current", "*"); |
b77209e0 | 570 | else |
8bb318c6 | 571 | ui_out_field_skip (uiout, "current"); |
b77209e0 | 572 | |
3a1ff0b6 | 573 | ui_out_field_int (uiout, "number", inf->num); |
6c95b8df | 574 | |
db2b9fdd PA |
575 | ui_out_field_string (uiout, "target-id", |
576 | inferior_pid_to_str (inf->pid)); | |
6c95b8df PA |
577 | |
578 | if (inf->pspace->ebfd) | |
579 | ui_out_field_string (uiout, "exec", | |
580 | bfd_get_filename (inf->pspace->ebfd)); | |
581 | else | |
582 | ui_out_field_skip (uiout, "exec"); | |
583 | ||
584 | /* Print extra info that isn't really fit to always present in | |
585 | tabular form. Currently we print the vfork parent/child | |
586 | relationships, if any. */ | |
587 | if (inf->vfork_parent) | |
588 | { | |
589 | ui_out_text (uiout, _("\n\tis vfork child of inferior ")); | |
590 | ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num); | |
591 | } | |
592 | if (inf->vfork_child) | |
593 | { | |
594 | ui_out_text (uiout, _("\n\tis vfork parent of inferior ")); | |
595 | ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num); | |
596 | } | |
b77209e0 PA |
597 | |
598 | ui_out_text (uiout, "\n"); | |
599 | do_cleanups (chain2); | |
600 | } | |
601 | ||
602 | do_cleanups (old_chain); | |
603 | } | |
604 | ||
2277426b PA |
605 | static void |
606 | detach_inferior_command (char *args, int from_tty) | |
607 | { | |
608 | int num, pid; | |
609 | struct thread_info *tp; | |
197f0a60 | 610 | struct get_number_or_range_state state; |
2277426b PA |
611 | |
612 | if (!args || !*args) | |
af624141 | 613 | error (_("Requires argument (inferior id(s) to detach)")); |
2277426b | 614 | |
197f0a60 TT |
615 | init_number_or_range (&state, args); |
616 | while (!state.finished) | |
af624141 | 617 | { |
197f0a60 | 618 | num = get_number_or_range (&state); |
2277426b | 619 | |
af624141 MS |
620 | if (!valid_gdb_inferior_id (num)) |
621 | { | |
622 | warning (_("Inferior ID %d not known."), num); | |
623 | continue; | |
624 | } | |
2277426b | 625 | |
af624141 | 626 | pid = gdb_inferior_id_to_pid (num); |
2277426b | 627 | |
af624141 MS |
628 | tp = any_thread_of_process (pid); |
629 | if (!tp) | |
630 | { | |
631 | warning (_("Inferior ID %d has no threads."), num); | |
632 | continue; | |
633 | } | |
2277426b | 634 | |
af624141 | 635 | switch_to_thread (tp->ptid); |
2277426b | 636 | |
af624141 MS |
637 | detach_command (NULL, from_tty); |
638 | } | |
2277426b PA |
639 | } |
640 | ||
641 | static void | |
642 | kill_inferior_command (char *args, int from_tty) | |
643 | { | |
644 | int num, pid; | |
645 | struct thread_info *tp; | |
197f0a60 | 646 | struct get_number_or_range_state state; |
2277426b PA |
647 | |
648 | if (!args || !*args) | |
af624141 | 649 | error (_("Requires argument (inferior id(s) to kill)")); |
2277426b | 650 | |
197f0a60 TT |
651 | init_number_or_range (&state, args); |
652 | while (!state.finished) | |
af624141 | 653 | { |
197f0a60 | 654 | num = get_number_or_range (&state); |
2277426b | 655 | |
af624141 MS |
656 | if (!valid_gdb_inferior_id (num)) |
657 | { | |
658 | warning (_("Inferior ID %d not known."), num); | |
659 | continue; | |
660 | } | |
2277426b | 661 | |
af624141 | 662 | pid = gdb_inferior_id_to_pid (num); |
2277426b | 663 | |
af624141 MS |
664 | tp = any_thread_of_process (pid); |
665 | if (!tp) | |
666 | { | |
667 | warning (_("Inferior ID %d has no threads."), num); | |
668 | continue; | |
669 | } | |
2277426b | 670 | |
af624141 | 671 | switch_to_thread (tp->ptid); |
2277426b | 672 | |
af624141 MS |
673 | target_kill (); |
674 | } | |
2277426b PA |
675 | |
676 | bfd_cache_close_all (); | |
677 | } | |
678 | ||
679 | static void | |
680 | inferior_command (char *args, int from_tty) | |
681 | { | |
6c95b8df PA |
682 | struct inferior *inf; |
683 | int num; | |
2277426b PA |
684 | |
685 | num = parse_and_eval_long (args); | |
686 | ||
6c95b8df PA |
687 | inf = find_inferior_id (num); |
688 | if (inf == NULL) | |
2277426b PA |
689 | error (_("Inferior ID %d not known."), num); |
690 | ||
6c95b8df PA |
691 | printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"), |
692 | inf->num, | |
db2b9fdd | 693 | inferior_pid_to_str (inf->pid), |
6c95b8df PA |
694 | (inf->pspace->ebfd |
695 | ? bfd_get_filename (inf->pspace->ebfd) | |
696 | : _("<noexec>"))); | |
2277426b | 697 | |
6c95b8df | 698 | if (inf->pid != 0) |
2277426b | 699 | { |
6c95b8df PA |
700 | if (inf->pid != ptid_get_pid (inferior_ptid)) |
701 | { | |
702 | struct thread_info *tp; | |
2277426b | 703 | |
6c95b8df PA |
704 | tp = any_thread_of_process (inf->pid); |
705 | if (!tp) | |
706 | error (_("Inferior has no threads.")); | |
2277426b | 707 | |
6c95b8df PA |
708 | switch_to_thread (tp->ptid); |
709 | } | |
710 | ||
711 | printf_filtered (_("[Switching to thread %d (%s)] "), | |
712 | pid_to_thread_id (inferior_ptid), | |
713 | target_pid_to_str (inferior_ptid)); | |
2277426b | 714 | } |
6c95b8df PA |
715 | else |
716 | { | |
717 | struct inferior *inf; | |
2277426b | 718 | |
6c95b8df PA |
719 | inf = find_inferior_id (num); |
720 | set_current_inferior (inf); | |
721 | switch_to_thread (null_ptid); | |
722 | set_current_program_space (inf->pspace); | |
723 | } | |
2277426b | 724 | |
6c95b8df | 725 | if (inf->pid != 0 && is_running (inferior_ptid)) |
79a45e25 | 726 | ui_out_text (current_uiout, "(running)\n"); |
6c95b8df | 727 | else if (inf->pid != 0) |
2277426b | 728 | { |
79a45e25 | 729 | ui_out_text (current_uiout, "\n"); |
2277426b PA |
730 | print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC); |
731 | } | |
732 | } | |
733 | ||
b77209e0 PA |
734 | /* Print information about currently known inferiors. */ |
735 | ||
736 | static void | |
2277426b | 737 | info_inferiors_command (char *args, int from_tty) |
b77209e0 | 738 | { |
79a45e25 | 739 | print_inferior (current_uiout, args); |
b77209e0 PA |
740 | } |
741 | ||
6c95b8df PA |
742 | /* remove-inferior ID */ |
743 | ||
70221824 | 744 | static void |
6c95b8df PA |
745 | remove_inferior_command (char *args, int from_tty) |
746 | { | |
747 | int num; | |
748 | struct inferior *inf; | |
197f0a60 | 749 | struct get_number_or_range_state state; |
6c95b8df | 750 | |
af624141 MS |
751 | if (args == NULL || *args == '\0') |
752 | error (_("Requires an argument (inferior id(s) to remove)")); | |
6c95b8df | 753 | |
197f0a60 TT |
754 | init_number_or_range (&state, args); |
755 | while (!state.finished) | |
af624141 | 756 | { |
197f0a60 | 757 | num = get_number_or_range (&state); |
af624141 | 758 | inf = find_inferior_id (num); |
6c95b8df | 759 | |
af624141 MS |
760 | if (inf == NULL) |
761 | { | |
762 | warning (_("Inferior ID %d not known."), num); | |
763 | continue; | |
764 | } | |
765 | ||
766 | if (inf == current_inferior ()) | |
767 | { | |
768 | warning (_("Can not remove current symbol inferior %d."), num); | |
769 | continue; | |
770 | } | |
8fa067af | 771 | |
af624141 MS |
772 | if (inf->pid != 0) |
773 | { | |
774 | warning (_("Can not remove active inferior %d."), num); | |
775 | continue; | |
776 | } | |
6c95b8df | 777 | |
af624141 MS |
778 | delete_inferior_1 (inf, 1); |
779 | } | |
6c95b8df PA |
780 | } |
781 | ||
a79b8f6e VP |
782 | struct inferior * |
783 | add_inferior_with_spaces (void) | |
784 | { | |
785 | struct address_space *aspace; | |
786 | struct program_space *pspace; | |
787 | struct inferior *inf; | |
788 | ||
789 | /* If all inferiors share an address space on this system, this | |
790 | doesn't really return a new address space; otherwise, it | |
791 | really does. */ | |
792 | aspace = maybe_new_address_space (); | |
793 | pspace = add_program_space (aspace); | |
794 | inf = add_inferior (0); | |
795 | inf->pspace = pspace; | |
796 | inf->aspace = pspace->aspace; | |
797 | ||
798 | return inf; | |
799 | } | |
6c95b8df PA |
800 | |
801 | /* add-inferior [-copies N] [-exec FILENAME] */ | |
802 | ||
70221824 | 803 | static void |
6c95b8df PA |
804 | add_inferior_command (char *args, int from_tty) |
805 | { | |
806 | int i, copies = 1; | |
807 | char *exec = NULL; | |
808 | char **argv; | |
809 | struct cleanup *old_chain = make_cleanup (null_cleanup, NULL); | |
810 | ||
811 | if (args) | |
812 | { | |
813 | argv = gdb_buildargv (args); | |
814 | make_cleanup_freeargv (argv); | |
815 | ||
816 | for (; *argv != NULL; argv++) | |
817 | { | |
818 | if (**argv == '-') | |
819 | { | |
820 | if (strcmp (*argv, "-copies") == 0) | |
821 | { | |
822 | ++argv; | |
823 | if (!*argv) | |
824 | error (_("No argument to -copies")); | |
825 | copies = parse_and_eval_long (*argv); | |
826 | } | |
827 | else if (strcmp (*argv, "-exec") == 0) | |
828 | { | |
829 | ++argv; | |
830 | if (!*argv) | |
831 | error (_("No argument to -exec")); | |
832 | exec = *argv; | |
833 | } | |
834 | } | |
835 | else | |
836 | error (_("Invalid argument")); | |
837 | } | |
838 | } | |
839 | ||
840 | save_current_space_and_thread (); | |
841 | ||
842 | for (i = 0; i < copies; ++i) | |
843 | { | |
a79b8f6e | 844 | struct inferior *inf = add_inferior_with_spaces (); |
6c95b8df PA |
845 | |
846 | printf_filtered (_("Added inferior %d\n"), inf->num); | |
847 | ||
848 | if (exec != NULL) | |
849 | { | |
850 | /* Switch over temporarily, while reading executable and | |
1777feb0 | 851 | symbols.q. */ |
a79b8f6e | 852 | set_current_program_space (inf->pspace); |
6c95b8df PA |
853 | set_current_inferior (inf); |
854 | switch_to_thread (null_ptid); | |
855 | ||
856 | exec_file_attach (exec, from_tty); | |
857 | symbol_file_add_main (exec, from_tty); | |
858 | } | |
859 | } | |
860 | ||
861 | do_cleanups (old_chain); | |
862 | } | |
863 | ||
864 | /* clone-inferior [-copies N] [ID] */ | |
865 | ||
70221824 | 866 | static void |
6c95b8df PA |
867 | clone_inferior_command (char *args, int from_tty) |
868 | { | |
869 | int i, copies = 1; | |
870 | char **argv; | |
871 | struct inferior *orginf = NULL; | |
872 | struct cleanup *old_chain = make_cleanup (null_cleanup, NULL); | |
873 | ||
874 | if (args) | |
875 | { | |
876 | argv = gdb_buildargv (args); | |
877 | make_cleanup_freeargv (argv); | |
878 | ||
879 | for (; *argv != NULL; argv++) | |
880 | { | |
881 | if (**argv == '-') | |
882 | { | |
883 | if (strcmp (*argv, "-copies") == 0) | |
884 | { | |
885 | ++argv; | |
886 | if (!*argv) | |
887 | error (_("No argument to -copies")); | |
888 | copies = parse_and_eval_long (*argv); | |
889 | ||
890 | if (copies < 0) | |
891 | error (_("Invalid copies number")); | |
892 | } | |
893 | } | |
894 | else | |
895 | { | |
896 | if (orginf == NULL) | |
897 | { | |
898 | int num; | |
899 | ||
900 | /* The first non-option (-) argument specified the | |
901 | program space ID. */ | |
902 | num = parse_and_eval_long (*argv); | |
903 | orginf = find_inferior_id (num); | |
904 | ||
905 | if (orginf == NULL) | |
906 | error (_("Inferior ID %d not known."), num); | |
907 | continue; | |
908 | } | |
909 | else | |
910 | error (_("Invalid argument")); | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | /* If no inferior id was specified, then the user wants to clone the | |
916 | current inferior. */ | |
917 | if (orginf == NULL) | |
918 | orginf = current_inferior (); | |
919 | ||
920 | save_current_space_and_thread (); | |
921 | ||
922 | for (i = 0; i < copies; ++i) | |
923 | { | |
924 | struct address_space *aspace; | |
925 | struct program_space *pspace; | |
926 | struct inferior *inf; | |
927 | ||
928 | /* If all inferiors share an address space on this system, this | |
929 | doesn't really return a new address space; otherwise, it | |
930 | really does. */ | |
931 | aspace = maybe_new_address_space (); | |
932 | pspace = add_program_space (aspace); | |
933 | inf = add_inferior (0); | |
934 | inf->pspace = pspace; | |
935 | inf->aspace = pspace->aspace; | |
936 | ||
937 | printf_filtered (_("Added inferior %d.\n"), inf->num); | |
938 | ||
939 | set_current_inferior (inf); | |
940 | switch_to_thread (null_ptid); | |
941 | clone_program_space (pspace, orginf->pspace); | |
942 | } | |
943 | ||
944 | do_cleanups (old_chain); | |
945 | } | |
946 | ||
b77209e0 PA |
947 | /* Print notices when new inferiors are created and die. */ |
948 | static void | |
949 | show_print_inferior_events (struct ui_file *file, int from_tty, | |
950 | struct cmd_list_element *c, const char *value) | |
951 | { | |
952 | fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value); | |
953 | } | |
954 | ||
6c95b8df PA |
955 | \f |
956 | ||
957 | /* Keep a registry of per-inferior data-pointers required by other GDB | |
958 | modules. */ | |
959 | ||
960 | struct inferior_data | |
961 | { | |
962 | unsigned index; | |
963 | void (*cleanup) (struct inferior *, void *); | |
964 | }; | |
965 | ||
966 | struct inferior_data_registration | |
967 | { | |
968 | struct inferior_data *data; | |
969 | struct inferior_data_registration *next; | |
970 | }; | |
971 | ||
972 | struct inferior_data_registry | |
973 | { | |
974 | struct inferior_data_registration *registrations; | |
975 | unsigned num_registrations; | |
976 | }; | |
977 | ||
978 | static struct inferior_data_registry inferior_data_registry | |
979 | = { NULL, 0 }; | |
980 | ||
981 | const struct inferior_data * | |
982 | register_inferior_data_with_cleanup | |
983 | (void (*cleanup) (struct inferior *, void *)) | |
984 | { | |
985 | struct inferior_data_registration **curr; | |
986 | ||
987 | /* Append new registration. */ | |
988 | for (curr = &inferior_data_registry.registrations; | |
989 | *curr != NULL; curr = &(*curr)->next); | |
990 | ||
991 | *curr = XMALLOC (struct inferior_data_registration); | |
992 | (*curr)->next = NULL; | |
993 | (*curr)->data = XMALLOC (struct inferior_data); | |
994 | (*curr)->data->index = inferior_data_registry.num_registrations++; | |
995 | (*curr)->data->cleanup = cleanup; | |
996 | ||
997 | return (*curr)->data; | |
998 | } | |
999 | ||
1000 | const struct inferior_data * | |
1001 | register_inferior_data (void) | |
1002 | { | |
1003 | return register_inferior_data_with_cleanup (NULL); | |
1004 | } | |
1005 | ||
1006 | static void | |
1007 | inferior_alloc_data (struct inferior *inf) | |
1008 | { | |
1009 | gdb_assert (inf->data == NULL); | |
1010 | inf->num_data = inferior_data_registry.num_registrations; | |
1011 | inf->data = XCALLOC (inf->num_data, void *); | |
1012 | } | |
1013 | ||
1014 | static void | |
1015 | inferior_free_data (struct inferior *inf) | |
1016 | { | |
1017 | gdb_assert (inf->data != NULL); | |
1018 | clear_inferior_data (inf); | |
1019 | xfree (inf->data); | |
1020 | inf->data = NULL; | |
1021 | } | |
1022 | ||
b77209e0 | 1023 | void |
6c95b8df | 1024 | clear_inferior_data (struct inferior *inf) |
b77209e0 | 1025 | { |
6c95b8df PA |
1026 | struct inferior_data_registration *registration; |
1027 | int i; | |
1028 | ||
1029 | gdb_assert (inf->data != NULL); | |
1030 | ||
1031 | for (registration = inferior_data_registry.registrations, i = 0; | |
1032 | i < inf->num_data; | |
1033 | registration = registration->next, i++) | |
1034 | if (inf->data[i] != NULL && registration->data->cleanup) | |
1035 | registration->data->cleanup (inf, inf->data[i]); | |
1036 | ||
1037 | memset (inf->data, 0, inf->num_data * sizeof (void *)); | |
1038 | } | |
1039 | ||
1040 | void | |
1041 | set_inferior_data (struct inferior *inf, | |
1042 | const struct inferior_data *data, | |
1043 | void *value) | |
1044 | { | |
1045 | gdb_assert (data->index < inf->num_data); | |
1046 | inf->data[data->index] = value; | |
1047 | } | |
1048 | ||
1049 | void * | |
1050 | inferior_data (struct inferior *inf, const struct inferior_data *data) | |
1051 | { | |
1052 | gdb_assert (data->index < inf->num_data); | |
1053 | return inf->data[data->index]; | |
1054 | } | |
1055 | ||
1056 | void | |
1057 | initialize_inferiors (void) | |
1058 | { | |
1059 | /* There's always one inferior. Note that this function isn't an | |
1060 | automatic _initialize_foo function, since other _initialize_foo | |
1061 | routines may need to install their per-inferior data keys. We | |
1062 | can only allocate an inferior when all those modules have done | |
1063 | that. Do this after initialize_progspace, due to the | |
1064 | current_program_space reference. */ | |
1065 | current_inferior_ = add_inferior (0); | |
1066 | current_inferior_->pspace = current_program_space; | |
1067 | current_inferior_->aspace = current_program_space->aspace; | |
1068 | ||
c82c0b55 MS |
1069 | add_info ("inferiors", info_inferiors_command, |
1070 | _("IDs of specified inferiors (all inferiors if no argument).")); | |
b77209e0 | 1071 | |
6c95b8df PA |
1072 | add_com ("add-inferior", no_class, add_inferior_command, _("\ |
1073 | Add a new inferior.\n\ | |
1074 | Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\ | |
af624141 | 1075 | N is the optional number of inferiors to add, default is 1.\n\ |
6c95b8df PA |
1076 | FILENAME is the file name of the executable to use\n\ |
1077 | as main program.")); | |
1078 | ||
af624141 MS |
1079 | add_com ("remove-inferiors", no_class, remove_inferior_command, _("\ |
1080 | Remove inferior ID (or list of IDs).\n\ | |
1081 | Usage: remove-inferiors ID...")); | |
6c95b8df PA |
1082 | |
1083 | add_com ("clone-inferior", no_class, clone_inferior_command, _("\ | |
1084 | Clone inferior ID.\n\ | |
1085 | Usage: clone-inferior [-copies <N>] [ID]\n\ | |
1086 | Add N copies of inferior ID. The new inferior has the same\n\ | |
1087 | executable loaded as the copied inferior. If -copies is not specified,\n\ | |
1088 | adds 1 copy. If ID is not specified, it is the current inferior\n\ | |
1089 | that is cloned.")); | |
2277426b | 1090 | |
af624141 MS |
1091 | add_cmd ("inferiors", class_run, detach_inferior_command, _("\ |
1092 | Detach from inferior ID (or list of IDS)."), | |
2277426b PA |
1093 | &detachlist); |
1094 | ||
af624141 MS |
1095 | add_cmd ("inferiors", class_run, kill_inferior_command, _("\ |
1096 | Kill inferior ID (or list of IDs)."), | |
2277426b PA |
1097 | &killlist); |
1098 | ||
1099 | add_cmd ("inferior", class_run, inferior_command, _("\ | |
1100 | Use this command to switch between inferiors.\n\ | |
1101 | The new inferior ID must be currently known."), | |
1102 | &cmdlist); | |
6c95b8df PA |
1103 | |
1104 | add_setshow_boolean_cmd ("inferior-events", no_class, | |
1105 | &print_inferior_events, _("\ | |
1106 | Set printing of inferior events (e.g., inferior start and exit)."), _("\ | |
1107 | Show printing of inferior events (e.g., inferior start and exit)."), NULL, | |
1108 | NULL, | |
1109 | show_print_inferior_events, | |
1110 | &setprintlist, &showprintlist); | |
1111 | ||
b77209e0 | 1112 | } |