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