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