]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/inferior.c
Clarify case requirements for gas pseudo-ops
[thirdparty/binutils-gdb.git] / gdb / inferior.c
CommitLineData
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
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);
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
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
ef3f321b
SM
187/* If SILENT then be quiet -- don't announce a inferior death, or the
188 exit of its threads. */
189
a79b8f6e 190void
ef3f321b 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;
ef3f321b 206 arg.silent = silent;
b77209e0
PA
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
0560c645 217 free_inferior (inf);
6c95b8df
PA
218}
219
ef3f321b
SM
220void
221delete_inferior (int pid)
222{
223 struct inferior *inf = find_inferior_pid (pid);
224
225 delete_inferior_1 (inf, 0);
226
227 if (print_inferior_events)
228 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
229}
230
231void
232delete_inferior_silent (int pid)
233{
234 struct inferior *inf = find_inferior_pid (pid);
235
236 delete_inferior_1 (inf, 1);
237}
238
239
6c95b8df
PA
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 277
68c9da30 278 inf->pending_detach = 0;
6c95b8df
PA
279}
280
281void
282exit_inferior (int pid)
283{
284 struct inferior *inf = find_inferior_pid (pid);
abbb1732 285
6c95b8df
PA
286 exit_inferior_1 (inf, 0);
287
288 if (print_inferior_events)
289 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
290}
291
292void
293exit_inferior_silent (int pid)
294{
295 struct inferior *inf = find_inferior_pid (pid);
abbb1732 296
6c95b8df
PA
297 exit_inferior_1 (inf, 1);
298}
299
300void
301exit_inferior_num_silent (int num)
302{
303 struct inferior *inf = find_inferior_id (num);
304
305 exit_inferior_1 (inf, 1);
b77209e0
PA
306}
307
308void
309detach_inferior (int pid)
310{
6c95b8df 311 struct inferior *inf = find_inferior_pid (pid);
abbb1732 312
3b462ec2 313 exit_inferior_1 (inf, 0);
b77209e0
PA
314
315 if (print_inferior_events)
316 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
317}
318
6c95b8df
PA
319void
320inferior_appeared (struct inferior *inf, int pid)
321{
322 inf->pid = pid;
2ddf4301
SM
323 inf->has_exit_code = 0;
324 inf->exit_code = 0;
6c95b8df 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
c9657e70
SM
370/* See inferior.h */
371
372struct inferior *
373find_inferior_ptid (ptid_t ptid)
374{
375 return find_inferior_pid (ptid_get_pid (ptid));
376}
377
32990ada 378/* See inferior.h. */
6c95b8df
PA
379
380struct inferior *
381find_inferior_for_program_space (struct program_space *pspace)
382{
32990ada
PA
383 struct inferior *inf = current_inferior ();
384
385 if (inf->pspace == pspace)
386 return inf;
6c95b8df
PA
387
388 for (inf = inferior_list; inf != NULL; inf = inf->next)
389 {
390 if (inf->pspace == pspace)
391 return inf;
392 }
393
394 return NULL;
395}
396
b77209e0
PA
397struct inferior *
398iterate_over_inferiors (int (*callback) (struct inferior *, void *),
399 void *data)
400{
401 struct inferior *inf, *infnext;
402
403 for (inf = inferior_list; inf; inf = infnext)
404 {
405 infnext = inf->next;
406 if ((*callback) (inf, data))
407 return inf;
408 }
409
410 return NULL;
411}
412
413int
414valid_gdb_inferior_id (int num)
415{
416 struct inferior *inf;
417
418 for (inf = inferior_list; inf; inf = inf->next)
419 if (inf->num == num)
420 return 1;
421
422 return 0;
423}
424
425int
426pid_to_gdb_inferior_id (int pid)
427{
428 struct inferior *inf;
429
430 for (inf = inferior_list; inf; inf = inf->next)
431 if (inf->pid == pid)
432 return inf->num;
433
434 return 0;
435}
436
437int
438gdb_inferior_id_to_pid (int num)
439{
440 struct inferior *inferior = find_inferior_id (num);
441 if (inferior)
442 return inferior->pid;
443 else
444 return -1;
445}
446
447int
448in_inferior_list (int pid)
449{
450 struct inferior *inf;
451
452 for (inf = inferior_list; inf; inf = inf->next)
453 if (inf->pid == pid)
454 return 1;
455
456 return 0;
457}
458
459int
460have_inferiors (void)
461{
6c95b8df
PA
462 struct inferior *inf;
463
464 for (inf = inferior_list; inf; inf = inf->next)
465 if (inf->pid != 0)
466 return 1;
467
468 return 0;
b77209e0
PA
469}
470
c35b1492
PA
471int
472have_live_inferiors (void)
473{
cd2effb2 474 struct inferior *inf;
6c95b8df 475
cd2effb2
JK
476 for (inf = inferior_list; inf; inf = inf->next)
477 if (inf->pid != 0)
478 {
479 struct thread_info *tp;
480
481 tp = any_thread_of_process (inf->pid);
aeaec162
TT
482 if (tp && target_has_execution_1 (tp->ptid))
483 break;
cd2effb2
JK
484 }
485
cd2effb2 486 return inf != NULL;
6c95b8df
PA
487}
488
bed8455c
DE
489/* Prune away any unused inferiors, and then prune away no longer used
490 program spaces. */
6c95b8df
PA
491
492void
493prune_inferiors (void)
494{
495 struct inferior *ss, **ss_link;
496 struct inferior *current = current_inferior ();
497
498 ss = inferior_list;
499 ss_link = &inferior_list;
500 while (ss)
501 {
502 if (ss == current
503 || !ss->removable
504 || ss->pid != 0)
505 {
506 ss_link = &ss->next;
507 ss = *ss_link;
508 continue;
509 }
510
511 *ss_link = ss->next;
ef3f321b 512 delete_inferior_1 (ss, 1);
6c95b8df
PA
513 ss = *ss_link;
514 }
ef3f321b
SM
515
516 prune_program_spaces ();
6c95b8df
PA
517}
518
519/* Simply returns the count of inferiors. */
520
521int
522number_of_inferiors (void)
523{
524 struct inferior *inf;
525 int count = 0;
526
527 for (inf = inferior_list; inf != NULL; inf = inf->next)
528 count++;
529
530 return count;
c35b1492
PA
531}
532
db2b9fdd
PA
533/* Converts an inferior process id to a string. Like
534 target_pid_to_str, but special cases the null process. */
535
536static char *
537inferior_pid_to_str (int pid)
538{
539 if (pid != 0)
540 return target_pid_to_str (pid_to_ptid (pid));
541 else
542 return _("<null>");
543}
544
b77209e0
PA
545/* Prints the list of inferiors and their details on UIOUT. This is a
546 version of 'info_inferior_command' suitable for use from MI.
547
c82c0b55
MS
548 If REQUESTED_INFERIORS is not NULL, it's a list of GDB ids of the
549 inferiors that should be printed. Otherwise, all inferiors are
550 printed. */
551
552static void
553print_inferior (struct ui_out *uiout, char *requested_inferiors)
b77209e0
PA
554{
555 struct inferior *inf;
556 struct cleanup *old_chain;
8bb318c6 557 int inf_count = 0;
b77209e0 558
8bb318c6
TT
559 /* Compute number of inferiors we will print. */
560 for (inf = inferior_list; inf; inf = inf->next)
561 {
c82c0b55 562 if (!number_is_in_list (requested_inferiors, inf->num))
8bb318c6
TT
563 continue;
564
565 ++inf_count;
566 }
567
568 if (inf_count == 0)
569 {
570 ui_out_message (uiout, 0, "No inferiors.\n");
571 return;
572 }
573
6c95b8df 574 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
8bb318c6 575 "inferiors");
3a1ff0b6
PA
576 ui_out_table_header (uiout, 1, ui_left, "current", "");
577 ui_out_table_header (uiout, 4, ui_left, "number", "Num");
578 ui_out_table_header (uiout, 17, ui_left, "target-id", "Description");
6c95b8df 579 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
b77209e0 580
6c95b8df 581 ui_out_table_body (uiout);
b77209e0
PA
582 for (inf = inferior_list; inf; inf = inf->next)
583 {
584 struct cleanup *chain2;
585
c82c0b55 586 if (!number_is_in_list (requested_inferiors, inf->num))
b77209e0
PA
587 continue;
588
589 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
590
6c95b8df 591 if (inf == current_inferior ())
8bb318c6 592 ui_out_field_string (uiout, "current", "*");
b77209e0 593 else
8bb318c6 594 ui_out_field_skip (uiout, "current");
b77209e0 595
3a1ff0b6 596 ui_out_field_int (uiout, "number", inf->num);
6c95b8df 597
db2b9fdd
PA
598 ui_out_field_string (uiout, "target-id",
599 inferior_pid_to_str (inf->pid));
6c95b8df 600
1f0c4988
JK
601 if (inf->pspace->pspace_exec_filename != NULL)
602 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
6c95b8df
PA
603 else
604 ui_out_field_skip (uiout, "exec");
605
606 /* Print extra info that isn't really fit to always present in
607 tabular form. Currently we print the vfork parent/child
608 relationships, if any. */
609 if (inf->vfork_parent)
610 {
611 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
612 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
613 }
614 if (inf->vfork_child)
615 {
616 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
617 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
618 }
b77209e0
PA
619
620 ui_out_text (uiout, "\n");
621 do_cleanups (chain2);
622 }
623
624 do_cleanups (old_chain);
625}
626
2277426b
PA
627static void
628detach_inferior_command (char *args, int from_tty)
629{
630 int num, pid;
631 struct thread_info *tp;
197f0a60 632 struct get_number_or_range_state state;
2277426b
PA
633
634 if (!args || !*args)
af624141 635 error (_("Requires argument (inferior id(s) to detach)"));
2277426b 636
197f0a60
TT
637 init_number_or_range (&state, args);
638 while (!state.finished)
af624141 639 {
197f0a60 640 num = get_number_or_range (&state);
2277426b 641
af624141
MS
642 if (!valid_gdb_inferior_id (num))
643 {
644 warning (_("Inferior ID %d not known."), num);
645 continue;
646 }
2277426b 647
af624141 648 pid = gdb_inferior_id_to_pid (num);
2277426b 649
af624141
MS
650 tp = any_thread_of_process (pid);
651 if (!tp)
652 {
653 warning (_("Inferior ID %d has no threads."), num);
654 continue;
655 }
2277426b 656
af624141 657 switch_to_thread (tp->ptid);
2277426b 658
af624141
MS
659 detach_command (NULL, from_tty);
660 }
2277426b
PA
661}
662
663static void
664kill_inferior_command (char *args, int from_tty)
665{
666 int num, pid;
667 struct thread_info *tp;
197f0a60 668 struct get_number_or_range_state state;
2277426b
PA
669
670 if (!args || !*args)
af624141 671 error (_("Requires argument (inferior id(s) to kill)"));
2277426b 672
197f0a60
TT
673 init_number_or_range (&state, args);
674 while (!state.finished)
af624141 675 {
197f0a60 676 num = get_number_or_range (&state);
2277426b 677
af624141
MS
678 if (!valid_gdb_inferior_id (num))
679 {
680 warning (_("Inferior ID %d not known."), num);
681 continue;
682 }
2277426b 683
af624141 684 pid = gdb_inferior_id_to_pid (num);
2277426b 685
af624141
MS
686 tp = any_thread_of_process (pid);
687 if (!tp)
688 {
689 warning (_("Inferior ID %d has no threads."), num);
690 continue;
691 }
2277426b 692
af624141 693 switch_to_thread (tp->ptid);
2277426b 694
af624141
MS
695 target_kill ();
696 }
2277426b
PA
697
698 bfd_cache_close_all ();
699}
700
701static void
702inferior_command (char *args, int from_tty)
703{
6c95b8df
PA
704 struct inferior *inf;
705 int num;
2277426b
PA
706
707 num = parse_and_eval_long (args);
708
6c95b8df
PA
709 inf = find_inferior_id (num);
710 if (inf == NULL)
2277426b
PA
711 error (_("Inferior ID %d not known."), num);
712
6c95b8df
PA
713 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
714 inf->num,
db2b9fdd 715 inferior_pid_to_str (inf->pid),
1f0c4988
JK
716 (inf->pspace->pspace_exec_filename != NULL
717 ? inf->pspace->pspace_exec_filename
6c95b8df 718 : _("<noexec>")));
2277426b 719
6c95b8df 720 if (inf->pid != 0)
2277426b 721 {
6c95b8df
PA
722 if (inf->pid != ptid_get_pid (inferior_ptid))
723 {
724 struct thread_info *tp;
2277426b 725
6c95b8df
PA
726 tp = any_thread_of_process (inf->pid);
727 if (!tp)
728 error (_("Inferior has no threads."));
2277426b 729
6c95b8df
PA
730 switch_to_thread (tp->ptid);
731 }
732
733 printf_filtered (_("[Switching to thread %d (%s)] "),
734 pid_to_thread_id (inferior_ptid),
735 target_pid_to_str (inferior_ptid));
2277426b 736 }
6c95b8df
PA
737 else
738 {
739 struct inferior *inf;
2277426b 740
6c95b8df
PA
741 inf = find_inferior_id (num);
742 set_current_inferior (inf);
743 switch_to_thread (null_ptid);
744 set_current_program_space (inf->pspace);
745 }
2277426b 746
6c95b8df 747 if (inf->pid != 0 && is_running (inferior_ptid))
79a45e25 748 ui_out_text (current_uiout, "(running)\n");
6c95b8df 749 else if (inf->pid != 0)
2277426b 750 {
79a45e25 751 ui_out_text (current_uiout, "\n");
08d72866 752 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2277426b
PA
753 }
754}
755
b77209e0
PA
756/* Print information about currently known inferiors. */
757
758static void
2277426b 759info_inferiors_command (char *args, int from_tty)
b77209e0 760{
79a45e25 761 print_inferior (current_uiout, args);
b77209e0
PA
762}
763
6c95b8df
PA
764/* remove-inferior ID */
765
70221824 766static void
6c95b8df
PA
767remove_inferior_command (char *args, int from_tty)
768{
769 int num;
770 struct inferior *inf;
197f0a60 771 struct get_number_or_range_state state;
6c95b8df 772
af624141
MS
773 if (args == NULL || *args == '\0')
774 error (_("Requires an argument (inferior id(s) to remove)"));
6c95b8df 775
197f0a60
TT
776 init_number_or_range (&state, args);
777 while (!state.finished)
af624141 778 {
197f0a60 779 num = get_number_or_range (&state);
af624141 780 inf = find_inferior_id (num);
6c95b8df 781
af624141
MS
782 if (inf == NULL)
783 {
784 warning (_("Inferior ID %d not known."), num);
785 continue;
786 }
787
788 if (inf == current_inferior ())
789 {
790 warning (_("Can not remove current symbol inferior %d."), num);
791 continue;
792 }
8fa067af 793
af624141
MS
794 if (inf->pid != 0)
795 {
796 warning (_("Can not remove active inferior %d."), num);
797 continue;
798 }
6c95b8df 799
ef3f321b 800 delete_inferior_1 (inf, 1);
af624141 801 }
ef3f321b
SM
802
803 prune_program_spaces ();
6c95b8df
PA
804}
805
a79b8f6e
VP
806struct inferior *
807add_inferior_with_spaces (void)
808{
809 struct address_space *aspace;
810 struct program_space *pspace;
811 struct inferior *inf;
6ecd4729 812 struct gdbarch_info info;
a79b8f6e
VP
813
814 /* If all inferiors share an address space on this system, this
815 doesn't really return a new address space; otherwise, it
816 really does. */
817 aspace = maybe_new_address_space ();
818 pspace = add_program_space (aspace);
819 inf = add_inferior (0);
820 inf->pspace = pspace;
821 inf->aspace = pspace->aspace;
822
6ecd4729
PA
823 /* Setup the inferior's initial arch, based on information obtained
824 from the global "set ..." options. */
825 gdbarch_info_init (&info);
826 inf->gdbarch = gdbarch_find_by_info (info);
827 /* The "set ..." options reject invalid settings, so we should
828 always have a valid arch by now. */
829 gdb_assert (inf->gdbarch != NULL);
830
a79b8f6e
VP
831 return inf;
832}
6c95b8df
PA
833
834/* add-inferior [-copies N] [-exec FILENAME] */
835
70221824 836static void
6c95b8df
PA
837add_inferior_command (char *args, int from_tty)
838{
839 int i, copies = 1;
840 char *exec = NULL;
841 char **argv;
842 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
843
844 if (args)
845 {
846 argv = gdb_buildargv (args);
847 make_cleanup_freeargv (argv);
848
849 for (; *argv != NULL; argv++)
850 {
851 if (**argv == '-')
852 {
853 if (strcmp (*argv, "-copies") == 0)
854 {
855 ++argv;
856 if (!*argv)
857 error (_("No argument to -copies"));
858 copies = parse_and_eval_long (*argv);
859 }
860 else if (strcmp (*argv, "-exec") == 0)
861 {
862 ++argv;
863 if (!*argv)
864 error (_("No argument to -exec"));
47902076
HAQ
865 exec = tilde_expand (*argv);
866 make_cleanup (xfree, exec);
6c95b8df
PA
867 }
868 }
869 else
870 error (_("Invalid argument"));
871 }
872 }
873
874 save_current_space_and_thread ();
875
876 for (i = 0; i < copies; ++i)
877 {
a79b8f6e 878 struct inferior *inf = add_inferior_with_spaces ();
6c95b8df
PA
879
880 printf_filtered (_("Added inferior %d\n"), inf->num);
881
882 if (exec != NULL)
883 {
884 /* Switch over temporarily, while reading executable and
1777feb0 885 symbols.q. */
a79b8f6e 886 set_current_program_space (inf->pspace);
6c95b8df
PA
887 set_current_inferior (inf);
888 switch_to_thread (null_ptid);
889
890 exec_file_attach (exec, from_tty);
891 symbol_file_add_main (exec, from_tty);
892 }
893 }
894
895 do_cleanups (old_chain);
896}
897
898/* clone-inferior [-copies N] [ID] */
899
70221824 900static void
6c95b8df
PA
901clone_inferior_command (char *args, int from_tty)
902{
903 int i, copies = 1;
904 char **argv;
905 struct inferior *orginf = NULL;
906 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
907
908 if (args)
909 {
910 argv = gdb_buildargv (args);
911 make_cleanup_freeargv (argv);
912
913 for (; *argv != NULL; argv++)
914 {
915 if (**argv == '-')
916 {
917 if (strcmp (*argv, "-copies") == 0)
918 {
919 ++argv;
920 if (!*argv)
921 error (_("No argument to -copies"));
922 copies = parse_and_eval_long (*argv);
923
924 if (copies < 0)
925 error (_("Invalid copies number"));
926 }
927 }
928 else
929 {
930 if (orginf == NULL)
931 {
932 int num;
933
934 /* The first non-option (-) argument specified the
935 program space ID. */
936 num = parse_and_eval_long (*argv);
937 orginf = find_inferior_id (num);
938
939 if (orginf == NULL)
940 error (_("Inferior ID %d not known."), num);
941 continue;
942 }
943 else
944 error (_("Invalid argument"));
945 }
946 }
947 }
948
949 /* If no inferior id was specified, then the user wants to clone the
950 current inferior. */
951 if (orginf == NULL)
952 orginf = current_inferior ();
953
954 save_current_space_and_thread ();
955
956 for (i = 0; i < copies; ++i)
957 {
958 struct address_space *aspace;
959 struct program_space *pspace;
960 struct inferior *inf;
961
962 /* If all inferiors share an address space on this system, this
963 doesn't really return a new address space; otherwise, it
964 really does. */
965 aspace = maybe_new_address_space ();
966 pspace = add_program_space (aspace);
967 inf = add_inferior (0);
968 inf->pspace = pspace;
969 inf->aspace = pspace->aspace;
6ecd4729
PA
970 inf->gdbarch = orginf->gdbarch;
971
972 /* If the original inferior had a user specified target
973 description, make the clone use it too. */
974 if (target_desc_info_from_user_p (inf->tdesc_info))
975 copy_inferior_target_desc_info (inf, orginf);
6c95b8df
PA
976
977 printf_filtered (_("Added inferior %d.\n"), inf->num);
978
979 set_current_inferior (inf);
980 switch_to_thread (null_ptid);
981 clone_program_space (pspace, orginf->pspace);
982 }
983
984 do_cleanups (old_chain);
985}
986
b77209e0
PA
987/* Print notices when new inferiors are created and die. */
988static void
989show_print_inferior_events (struct ui_file *file, int from_tty,
990 struct cmd_list_element *c, const char *value)
991{
992 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
993}
994
6c95b8df
PA
995\f
996
6c95b8df
PA
997void
998initialize_inferiors (void)
999{
06da564e
EZ
1000 struct cmd_list_element *c = NULL;
1001
6c95b8df
PA
1002 /* There's always one inferior. Note that this function isn't an
1003 automatic _initialize_foo function, since other _initialize_foo
1004 routines may need to install their per-inferior data keys. We
1005 can only allocate an inferior when all those modules have done
1006 that. Do this after initialize_progspace, due to the
1007 current_program_space reference. */
1008 current_inferior_ = add_inferior (0);
1009 current_inferior_->pspace = current_program_space;
1010 current_inferior_->aspace = current_program_space->aspace;
6ecd4729
PA
1011 /* The architecture will be initialized shortly, by
1012 initialize_current_architecture. */
6c95b8df 1013
c82c0b55
MS
1014 add_info ("inferiors", info_inferiors_command,
1015 _("IDs of specified inferiors (all inferiors if no argument)."));
b77209e0 1016
06da564e 1017 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
6c95b8df
PA
1018Add a new inferior.\n\
1019Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
af624141 1020N is the optional number of inferiors to add, default is 1.\n\
6c95b8df
PA
1021FILENAME is the file name of the executable to use\n\
1022as main program."));
06da564e 1023 set_cmd_completer (c, filename_completer);
6c95b8df 1024
af624141
MS
1025 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1026Remove inferior ID (or list of IDs).\n\
1027Usage: remove-inferiors ID..."));
6c95b8df
PA
1028
1029 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1030Clone inferior ID.\n\
1031Usage: clone-inferior [-copies <N>] [ID]\n\
1032Add N copies of inferior ID. The new inferior has the same\n\
1033executable loaded as the copied inferior. If -copies is not specified,\n\
1034adds 1 copy. If ID is not specified, it is the current inferior\n\
1035that is cloned."));
2277426b 1036
af624141
MS
1037 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1038Detach from inferior ID (or list of IDS)."),
2277426b
PA
1039 &detachlist);
1040
af624141
MS
1041 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1042Kill inferior ID (or list of IDs)."),
2277426b
PA
1043 &killlist);
1044
1045 add_cmd ("inferior", class_run, inferior_command, _("\
1046Use this command to switch between inferiors.\n\
1047The new inferior ID must be currently known."),
1048 &cmdlist);
6c95b8df
PA
1049
1050 add_setshow_boolean_cmd ("inferior-events", no_class,
1051 &print_inferior_events, _("\
1052Set printing of inferior events (e.g., inferior start and exit)."), _("\
1053Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1054 NULL,
1055 show_print_inferior_events,
1056 &setprintlist, &showprintlist);
1057
b77209e0 1058}