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