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