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