]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inferior.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / inferior.c
CommitLineData
b77209e0
PA
1/* Multi-process control for GDB, the GNU debugger.
2
0b302171 3 Copyright (C) 2008-2012 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"
2277426b 30#include "gdbthread.h"
6c95b8df
PA
31#include "gdbcore.h"
32#include "symfile.h"
3f81c18a 33#include "environ.h"
c82c0b55 34#include "cli/cli-utils.h"
be34f849 35#include "continuations.h"
6ecd4729
PA
36#include "arch-utils.h"
37#include "target-descriptions.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{
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
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);
b77209e0
PA
104 xfree (inf->private);
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
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
154struct inferior *
155add_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
165struct delete_thread_of_inferior_arg
166{
167 int pid;
168 int silent;
169};
170
171static int
172delete_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
187/* If SILENT then be quiet -- don't announce a inferior death, or the
188 exit of its threads. */
6c95b8df 189
a79b8f6e 190void
6c95b8df 191delete_inferior_1 (struct inferior *todel, int silent)
b77209e0
PA
192{
193 struct inferior *inf, *infprev;
6c95b8df 194 struct delete_thread_of_inferior_arg arg;
b77209e0
PA
195
196 infprev = NULL;
197
198 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
6c95b8df 199 if (inf == todel)
b77209e0
PA
200 break;
201
202 if (!inf)
203 return;
204
6c95b8df 205 arg.pid = inf->pid;
b77209e0
PA
206 arg.silent = silent;
207
208 iterate_over_threads (delete_thread_of_inferior, &arg);
4a92f99b 209
7e1789f5
PA
210 if (infprev)
211 infprev->next = inf->next;
212 else
213 inferior_list = inf->next;
214
a79b8f6e
VP
215 observer_notify_inferior_removed (inf);
216
7e1789f5 217 free_inferior (inf);
b77209e0
PA
218}
219
220void
221delete_inferior (int pid)
222{
6c95b8df
PA
223 struct inferior *inf = find_inferior_pid (pid);
224
225 delete_inferior_1 (inf, 0);
b77209e0
PA
226
227 if (print_inferior_events)
228 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
229}
230
231void
232delete_inferior_silent (int pid)
233{
6c95b8df
PA
234 struct inferior *inf = find_inferior_pid (pid);
235
236 delete_inferior_1 (inf, 1);
237}
238
239
240/* If SILENT then be quiet -- don't announce a inferior exit, or the
241 exit of its threads. */
242
243static void
244exit_inferior_1 (struct inferior *inftoex, int silent)
245{
246 struct inferior *inf;
247 struct delete_thread_of_inferior_arg arg;
248
249 for (inf = inferior_list; inf; inf = inf->next)
250 if (inf == inftoex)
251 break;
252
253 if (!inf)
254 return;
255
256 arg.pid = inf->pid;
257 arg.silent = silent;
258
259 iterate_over_threads (delete_thread_of_inferior, &arg);
260
261 /* Notify the observers before removing the inferior from the list,
262 so that the observers have a chance to look it up. */
a79b8f6e 263 observer_notify_inferior_exit (inf);
6c95b8df
PA
264
265 inf->pid = 0;
e714e1bf 266 inf->fake_pid_p = 0;
6c95b8df
PA
267 if (inf->vfork_parent != NULL)
268 {
269 inf->vfork_parent->vfork_child = NULL;
270 inf->vfork_parent = NULL;
271 }
68c9da30
PA
272 if (inf->vfork_child != NULL)
273 {
274 inf->vfork_child->vfork_parent = NULL;
275 inf->vfork_child = NULL;
276 }
8cf64490
TT
277
278 inf->has_exit_code = 0;
279 inf->exit_code = 0;
68c9da30 280 inf->pending_detach = 0;
6c95b8df
PA
281}
282
283void
284exit_inferior (int pid)
285{
286 struct inferior *inf = find_inferior_pid (pid);
abbb1732 287
6c95b8df
PA
288 exit_inferior_1 (inf, 0);
289
290 if (print_inferior_events)
291 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
292}
293
294void
295exit_inferior_silent (int pid)
296{
297 struct inferior *inf = find_inferior_pid (pid);
abbb1732 298
6c95b8df
PA
299 exit_inferior_1 (inf, 1);
300}
301
302void
303exit_inferior_num_silent (int num)
304{
305 struct inferior *inf = find_inferior_id (num);
306
307 exit_inferior_1 (inf, 1);
b77209e0
PA
308}
309
310void
311detach_inferior (int pid)
312{
6c95b8df 313 struct inferior *inf = find_inferior_pid (pid);
abbb1732 314
6c95b8df 315 exit_inferior_1 (inf, 1);
b77209e0
PA
316
317 if (print_inferior_events)
318 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
319}
320
6c95b8df
PA
321void
322inferior_appeared (struct inferior *inf, int pid)
323{
324 inf->pid = pid;
325
a79b8f6e 326 observer_notify_inferior_appeared (inf);
6c95b8df
PA
327}
328
82f73884
PA
329void
330discard_all_inferiors (void)
331{
6c95b8df 332 struct inferior *inf;
82f73884 333
6c95b8df 334 for (inf = inferior_list; inf; inf = inf->next)
82f73884 335 {
6c95b8df
PA
336 if (inf->pid != 0)
337 exit_inferior_silent (inf->pid);
82f73884
PA
338 }
339}
340
6c95b8df 341struct inferior *
b77209e0
PA
342find_inferior_id (int num)
343{
344 struct inferior *inf;
345
346 for (inf = inferior_list; inf; inf = inf->next)
347 if (inf->num == num)
348 return inf;
349
350 return NULL;
351}
352
353struct inferior *
354find_inferior_pid (int pid)
355{
356 struct inferior *inf;
357
6c95b8df
PA
358 /* Looking for inferior pid == 0 is always wrong, and indicative of
359 a bug somewhere else. There may be more than one with pid == 0,
360 for instance. */
361 gdb_assert (pid != 0);
362
b77209e0
PA
363 for (inf = inferior_list; inf; inf = inf->next)
364 if (inf->pid == pid)
365 return inf;
366
367 return NULL;
368}
369
6c95b8df
PA
370/* Find an inferior bound to PSPACE. */
371
372struct inferior *
373find_inferior_for_program_space (struct program_space *pspace)
374{
375 struct inferior *inf;
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
c35b1492
PA
460int
461have_live_inferiors (void)
462{
cd2effb2 463 struct inferior *inf;
6c95b8df 464
cd2effb2
JK
465 for (inf = inferior_list; inf; inf = inf->next)
466 if (inf->pid != 0)
467 {
468 struct thread_info *tp;
469
470 tp = any_thread_of_process (inf->pid);
aeaec162
TT
471 if (tp && target_has_execution_1 (tp->ptid))
472 break;
cd2effb2
JK
473 }
474
cd2effb2 475 return inf != NULL;
6c95b8df
PA
476}
477
478/* Prune away automatically added program spaces that aren't required
479 anymore. */
480
481void
482prune_inferiors (void)
483{
484 struct inferior *ss, **ss_link;
485 struct inferior *current = current_inferior ();
486
487 ss = inferior_list;
488 ss_link = &inferior_list;
489 while (ss)
490 {
491 if (ss == current
492 || !ss->removable
493 || ss->pid != 0)
494 {
495 ss_link = &ss->next;
496 ss = *ss_link;
497 continue;
498 }
499
500 *ss_link = ss->next;
501 delete_inferior_1 (ss, 1);
502 ss = *ss_link;
503 }
504
505 prune_program_spaces ();
506}
507
508/* Simply returns the count of inferiors. */
509
510int
511number_of_inferiors (void)
512{
513 struct inferior *inf;
514 int count = 0;
515
516 for (inf = inferior_list; inf != NULL; inf = inf->next)
517 count++;
518
519 return count;
c35b1492
PA
520}
521
db2b9fdd
PA
522/* Converts an inferior process id to a string. Like
523 target_pid_to_str, but special cases the null process. */
524
525static char *
526inferior_pid_to_str (int pid)
527{
528 if (pid != 0)
529 return target_pid_to_str (pid_to_ptid (pid));
530 else
531 return _("<null>");
532}
533
b77209e0
PA
534/* Prints the list of inferiors and their details on UIOUT. This is a
535 version of 'info_inferior_command' suitable for use from MI.
536
c82c0b55
MS
537 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
538 inferiors that should be printed. Otherwise, all inferiors are
539 printed. */
540
541static void
542print_inferior (struct ui_out *uiout, char *requested_inferiors)
b77209e0
PA
543{
544 struct inferior *inf;
545 struct cleanup *old_chain;
8bb318c6 546 int inf_count = 0;
b77209e0 547
8bb318c6
TT
548 /* Compute number of inferiors we will print. */
549 for (inf = inferior_list; inf; inf = inf->next)
550 {
c82c0b55 551 if (!number_is_in_list (requested_inferiors, inf->num))
8bb318c6
TT
552 continue;
553
554 ++inf_count;
555 }
556
557 if (inf_count == 0)
558 {
559 ui_out_message (uiout, 0, "No inferiors.\n");
560 return;
561 }
562
6c95b8df 563 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
8bb318c6 564 "inferiors");
3a1ff0b6
PA
565 ui_out_table_header (uiout, 1, ui_left, "current", "");
566 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
567 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
6c95b8df 568 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
b77209e0 569
6c95b8df 570 ui_out_table_body (uiout);
b77209e0
PA
571 for (inf = inferior_list; inf; inf = inf->next)
572 {
573 struct cleanup *chain2;
574
c82c0b55 575 if (!number_is_in_list (requested_inferiors, inf->num))
b77209e0
PA
576 continue;
577
578 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
579
6c95b8df 580 if (inf == current_inferior ())
8bb318c6 581 ui_out_field_string (uiout, "current", "*");
b77209e0 582 else
8bb318c6 583 ui_out_field_skip (uiout, "current");
b77209e0 584
3a1ff0b6 585 ui_out_field_int (uiout, "number", inf->num);
6c95b8df 586
db2b9fdd
PA
587 ui_out_field_string (uiout, "target-id",
588 inferior_pid_to_str (inf->pid));
6c95b8df
PA
589
590 if (inf->pspace->ebfd)
591 ui_out_field_string (uiout, "exec",
592 bfd_get_filename (inf->pspace->ebfd));
593 else
594 ui_out_field_skip (uiout, "exec");
595
596 /* Print extra info that isn't really fit to always present in
597 tabular form. Currently we print the vfork parent/child
598 relationships, if any. */
599 if (inf->vfork_parent)
600 {
601 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
602 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
603 }
604 if (inf->vfork_child)
605 {
606 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
607 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
608 }
b77209e0
PA
609
610 ui_out_text (uiout, "\n");
611 do_cleanups (chain2);
612 }
613
614 do_cleanups (old_chain);
615}
616
2277426b
PA
617static void
618detach_inferior_command (char *args, int from_tty)
619{
620 int num, pid;
621 struct thread_info *tp;
197f0a60 622 struct get_number_or_range_state state;
2277426b
PA
623
624 if (!args || !*args)
af624141 625 error (_("Requires argument (inferior id(s) to detach)"));
2277426b 626
197f0a60
TT
627 init_number_or_range (&state, args);
628 while (!state.finished)
af624141 629 {
197f0a60 630 num = get_number_or_range (&state);
2277426b 631
af624141
MS
632 if (!valid_gdb_inferior_id (num))
633 {
634 warning (_("Inferior ID %d not known."), num);
635 continue;
636 }
2277426b 637
af624141 638 pid = gdb_inferior_id_to_pid (num);
2277426b 639
af624141
MS
640 tp = any_thread_of_process (pid);
641 if (!tp)
642 {
643 warning (_("Inferior ID %d has no threads."), num);
644 continue;
645 }
2277426b 646
af624141 647 switch_to_thread (tp->ptid);
2277426b 648
af624141
MS
649 detach_command (NULL, from_tty);
650 }
2277426b
PA
651}
652
653static void
654kill_inferior_command (char *args, int from_tty)
655{
656 int num, pid;
657 struct thread_info *tp;
197f0a60 658 struct get_number_or_range_state state;
2277426b
PA
659
660 if (!args || !*args)
af624141 661 error (_("Requires argument (inferior id(s) to kill)"));
2277426b 662
197f0a60
TT
663 init_number_or_range (&state, args);
664 while (!state.finished)
af624141 665 {
197f0a60 666 num = get_number_or_range (&state);
2277426b 667
af624141
MS
668 if (!valid_gdb_inferior_id (num))
669 {
670 warning (_("Inferior ID %d not known."), num);
671 continue;
672 }
2277426b 673
af624141 674 pid = gdb_inferior_id_to_pid (num);
2277426b 675
af624141
MS
676 tp = any_thread_of_process (pid);
677 if (!tp)
678 {
679 warning (_("Inferior ID %d has no threads."), num);
680 continue;
681 }
2277426b 682
af624141 683 switch_to_thread (tp->ptid);
2277426b 684
af624141
MS
685 target_kill ();
686 }
2277426b
PA
687
688 bfd_cache_close_all ();
689}
690
691static void
692inferior_command (char *args, int from_tty)
693{
6c95b8df
PA
694 struct inferior *inf;
695 int num;
2277426b
PA
696
697 num = parse_and_eval_long (args);
698
6c95b8df
PA
699 inf = find_inferior_id (num);
700 if (inf == NULL)
2277426b
PA
701 error (_("Inferior ID %d not known."), num);
702
6c95b8df
PA
703 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
704 inf->num,
db2b9fdd 705 inferior_pid_to_str (inf->pid),
6c95b8df
PA
706 (inf->pspace->ebfd
707 ? bfd_get_filename (inf->pspace->ebfd)
708 : _("<noexec>")));
2277426b 709
6c95b8df 710 if (inf->pid != 0)
2277426b 711 {
6c95b8df
PA
712 if (inf->pid != ptid_get_pid (inferior_ptid))
713 {
714 struct thread_info *tp;
2277426b 715
6c95b8df
PA
716 tp = any_thread_of_process (inf->pid);
717 if (!tp)
718 error (_("Inferior has no threads."));
2277426b 719
6c95b8df
PA
720 switch_to_thread (tp->ptid);
721 }
722
723 printf_filtered (_("[Switching to thread %d (%s)] "),
724 pid_to_thread_id (inferior_ptid),
725 target_pid_to_str (inferior_ptid));
2277426b 726 }
6c95b8df
PA
727 else
728 {
729 struct inferior *inf;
2277426b 730
6c95b8df
PA
731 inf = find_inferior_id (num);
732 set_current_inferior (inf);
733 switch_to_thread (null_ptid);
734 set_current_program_space (inf->pspace);
735 }
2277426b 736
6c95b8df 737 if (inf->pid != 0 && is_running (inferior_ptid))
79a45e25 738 ui_out_text (current_uiout, "(running)\n");
6c95b8df 739 else if (inf->pid != 0)
2277426b 740 {
79a45e25 741 ui_out_text (current_uiout, "\n");
2277426b
PA
742 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
743 }
744}
745
b77209e0
PA
746/* Print information about currently known inferiors. */
747
748static void
2277426b 749info_inferiors_command (char *args, int from_tty)
b77209e0 750{
79a45e25 751 print_inferior (current_uiout, args);
b77209e0
PA
752}
753
6c95b8df
PA
754/* remove-inferior ID */
755
70221824 756static void
6c95b8df
PA
757remove_inferior_command (char *args, int from_tty)
758{
759 int num;
760 struct inferior *inf;
197f0a60 761 struct get_number_or_range_state state;
6c95b8df 762
af624141
MS
763 if (args == NULL || *args == '\0')
764 error (_("Requires an argument (inferior id(s) to remove)"));
6c95b8df 765
197f0a60
TT
766 init_number_or_range (&state, args);
767 while (!state.finished)
af624141 768 {
197f0a60 769 num = get_number_or_range (&state);
af624141 770 inf = find_inferior_id (num);
6c95b8df 771
af624141
MS
772 if (inf == NULL)
773 {
774 warning (_("Inferior ID %d not known."), num);
775 continue;
776 }
777
778 if (inf == current_inferior ())
779 {
780 warning (_("Can not remove current symbol inferior %d."), num);
781 continue;
782 }
8fa067af 783
af624141
MS
784 if (inf->pid != 0)
785 {
786 warning (_("Can not remove active inferior %d."), num);
787 continue;
788 }
6c95b8df 789
af624141
MS
790 delete_inferior_1 (inf, 1);
791 }
6c95b8df
PA
792}
793
a79b8f6e
VP
794struct inferior *
795add_inferior_with_spaces (void)
796{
797 struct address_space *aspace;
798 struct program_space *pspace;
799 struct inferior *inf;
6ecd4729 800 struct gdbarch_info info;
a79b8f6e
VP
801
802 /* If all inferiors share an address space on this system, this
803 doesn't really return a new address space; otherwise, it
804 really does. */
805 aspace = maybe_new_address_space ();
806 pspace = add_program_space (aspace);
807 inf = add_inferior (0);
808 inf->pspace = pspace;
809 inf->aspace = pspace->aspace;
810
6ecd4729
PA
811 /* Setup the inferior's initial arch, based on information obtained
812 from the global "set ..." options. */
813 gdbarch_info_init (&info);
814 inf->gdbarch = gdbarch_find_by_info (info);
815 /* The "set ..." options reject invalid settings, so we should
816 always have a valid arch by now. */
817 gdb_assert (inf->gdbarch != NULL);
818
a79b8f6e
VP
819 return inf;
820}
6c95b8df
PA
821
822/* add-inferior [-copies N] [-exec FILENAME] */
823
70221824 824static void
6c95b8df
PA
825add_inferior_command (char *args, int from_tty)
826{
827 int i, copies = 1;
828 char *exec = NULL;
829 char **argv;
830 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
831
832 if (args)
833 {
834 argv = gdb_buildargv (args);
835 make_cleanup_freeargv (argv);
836
837 for (; *argv != NULL; argv++)
838 {
839 if (**argv == '-')
840 {
841 if (strcmp (*argv, "-copies") == 0)
842 {
843 ++argv;
844 if (!*argv)
845 error (_("No argument to -copies"));
846 copies = parse_and_eval_long (*argv);
847 }
848 else if (strcmp (*argv, "-exec") == 0)
849 {
850 ++argv;
851 if (!*argv)
852 error (_("No argument to -exec"));
853 exec = *argv;
854 }
855 }
856 else
857 error (_("Invalid argument"));
858 }
859 }
860
861 save_current_space_and_thread ();
862
863 for (i = 0; i < copies; ++i)
864 {
a79b8f6e 865 struct inferior *inf = add_inferior_with_spaces ();
6c95b8df
PA
866
867 printf_filtered (_("Added inferior %d\n"), inf->num);
868
869 if (exec != NULL)
870 {
871 /* Switch over temporarily, while reading executable and
1777feb0 872 symbols.q. */
a79b8f6e 873 set_current_program_space (inf->pspace);
6c95b8df
PA
874 set_current_inferior (inf);
875 switch_to_thread (null_ptid);
876
877 exec_file_attach (exec, from_tty);
878 symbol_file_add_main (exec, from_tty);
879 }
880 }
881
882 do_cleanups (old_chain);
883}
884
885/* clone-inferior [-copies N] [ID] */
886
70221824 887static void
6c95b8df
PA
888clone_inferior_command (char *args, int from_tty)
889{
890 int i, copies = 1;
891 char **argv;
892 struct inferior *orginf = NULL;
893 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
894
895 if (args)
896 {
897 argv = gdb_buildargv (args);
898 make_cleanup_freeargv (argv);
899
900 for (; *argv != NULL; argv++)
901 {
902 if (**argv == '-')
903 {
904 if (strcmp (*argv, "-copies") == 0)
905 {
906 ++argv;
907 if (!*argv)
908 error (_("No argument to -copies"));
909 copies = parse_and_eval_long (*argv);
910
911 if (copies < 0)
912 error (_("Invalid copies number"));
913 }
914 }
915 else
916 {
917 if (orginf == NULL)
918 {
919 int num;
920
921 /* The first non-option (-) argument specified the
922 program space ID. */
923 num = parse_and_eval_long (*argv);
924 orginf = find_inferior_id (num);
925
926 if (orginf == NULL)
927 error (_("Inferior ID %d not known."), num);
928 continue;
929 }
930 else
931 error (_("Invalid argument"));
932 }
933 }
934 }
935
936 /* If no inferior id was specified, then the user wants to clone the
937 current inferior. */
938 if (orginf == NULL)
939 orginf = current_inferior ();
940
941 save_current_space_and_thread ();
942
943 for (i = 0; i < copies; ++i)
944 {
945 struct address_space *aspace;
946 struct program_space *pspace;
947 struct inferior *inf;
948
949 /* If all inferiors share an address space on this system, this
950 doesn't really return a new address space; otherwise, it
951 really does. */
952 aspace = maybe_new_address_space ();
953 pspace = add_program_space (aspace);
954 inf = add_inferior (0);
955 inf->pspace = pspace;
956 inf->aspace = pspace->aspace;
6ecd4729
PA
957 inf->gdbarch = orginf->gdbarch;
958
959 /* If the original inferior had a user specified target
960 description, make the clone use it too. */
961 if (target_desc_info_from_user_p (inf->tdesc_info))
962 copy_inferior_target_desc_info (inf, orginf);
6c95b8df
PA
963
964 printf_filtered (_("Added inferior %d.\n"), inf->num);
965
966 set_current_inferior (inf);
967 switch_to_thread (null_ptid);
968 clone_program_space (pspace, orginf->pspace);
969 }
970
971 do_cleanups (old_chain);
972}
973
b77209e0
PA
974/* Print notices when new inferiors are created and die. */
975static void
976show_print_inferior_events (struct ui_file *file, int from_tty,
977 struct cmd_list_element *c, const char *value)
978{
979 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
980}
981
6c95b8df
PA
982\f
983
6c95b8df
PA
984void
985initialize_inferiors (void)
986{
06da564e
EZ
987 struct cmd_list_element *c = NULL;
988
6c95b8df
PA
989 /* There's always one inferior. Note that this function isn't an
990 automatic _initialize_foo function, since other _initialize_foo
991 routines may need to install their per-inferior data keys. We
992 can only allocate an inferior when all those modules have done
993 that. Do this after initialize_progspace, due to the
994 current_program_space reference. */
995 current_inferior_ = add_inferior (0);
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
1005Add a new inferior.\n\
1006Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
af624141 1007N is the optional number of inferiors to add, default is 1.\n\
6c95b8df
PA
1008FILENAME is the file name of the executable to use\n\
1009as main program."));
06da564e 1010 set_cmd_completer (c, filename_completer);
6c95b8df 1011
af624141
MS
1012 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1013Remove inferior ID (or list of IDs).\n\
1014Usage: remove-inferiors ID..."));
6c95b8df
PA
1015
1016 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1017Clone inferior ID.\n\
1018Usage: clone-inferior [-copies <N>] [ID]\n\
1019Add N copies of inferior ID. The new inferior has the same\n\
1020executable loaded as the copied inferior. If -copies is not specified,\n\
1021adds 1 copy. If ID is not specified, it is the current inferior\n\
1022that is cloned."));
2277426b 1023
af624141
MS
1024 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1025Detach from inferior ID (or list of IDS)."),
2277426b
PA
1026 &detachlist);
1027
af624141
MS
1028 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1029Kill inferior ID (or list of IDs)."),
2277426b
PA
1030 &killlist);
1031
1032 add_cmd ("inferior", class_run, inferior_command, _("\
1033Use this command to switch between inferiors.\n\
1034The 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, _("\
1039Set printing of inferior events (e.g., inferior start and exit)."), _("\
1040Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1041 NULL,
1042 show_print_inferior_events,
1043 &setprintlist, &showprintlist);
1044
b77209e0 1045}