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