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