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