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