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