Target remote mode fork and exec test updates
[deliverable/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{
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;
7e0aa6aa
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
252 /* Notify the observers before removing the inferior from the list,
253 so that the observers have a chance to look it up. */
a79b8f6e 254 observer_notify_inferior_exit (inf);
6c95b8df
PA
255
256 inf->pid = 0;
e714e1bf 257 inf->fake_pid_p = 0;
6c95b8df
PA
258 if (inf->vfork_parent != NULL)
259 {
260 inf->vfork_parent->vfork_child = NULL;
261 inf->vfork_parent = NULL;
262 }
68c9da30
PA
263 if (inf->vfork_child != NULL)
264 {
265 inf->vfork_child->vfork_parent = NULL;
266 inf->vfork_child = NULL;
267 }
8cf64490 268
68c9da30 269 inf->pending_detach = 0;
6c95b8df
PA
270}
271
272void
273exit_inferior (int pid)
274{
275 struct inferior *inf = find_inferior_pid (pid);
abbb1732 276
6c95b8df
PA
277 exit_inferior_1 (inf, 0);
278
279 if (print_inferior_events)
280 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
281}
282
283void
284exit_inferior_silent (int pid)
285{
286 struct inferior *inf = find_inferior_pid (pid);
abbb1732 287
6c95b8df
PA
288 exit_inferior_1 (inf, 1);
289}
290
291void
292exit_inferior_num_silent (int num)
293{
294 struct inferior *inf = find_inferior_id (num);
295
296 exit_inferior_1 (inf, 1);
b77209e0
PA
297}
298
299void
300detach_inferior (int pid)
301{
6c95b8df 302 struct inferior *inf = find_inferior_pid (pid);
abbb1732 303
3b462ec2 304 exit_inferior_1 (inf, 0);
b77209e0
PA
305
306 if (print_inferior_events)
307 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
308}
309
6c95b8df
PA
310void
311inferior_appeared (struct inferior *inf, int pid)
312{
313 inf->pid = pid;
2ddf4301
SM
314 inf->has_exit_code = 0;
315 inf->exit_code = 0;
6c95b8df 316
a79b8f6e 317 observer_notify_inferior_appeared (inf);
6c95b8df
PA
318}
319
82f73884
PA
320void
321discard_all_inferiors (void)
322{
6c95b8df 323 struct inferior *inf;
82f73884 324
6c95b8df 325 for (inf = inferior_list; inf; inf = inf->next)
82f73884 326 {
6c95b8df
PA
327 if (inf->pid != 0)
328 exit_inferior_silent (inf->pid);
82f73884
PA
329 }
330}
331
6c95b8df 332struct inferior *
b77209e0
PA
333find_inferior_id (int num)
334{
335 struct inferior *inf;
336
337 for (inf = inferior_list; inf; inf = inf->next)
338 if (inf->num == num)
339 return inf;
340
341 return NULL;
342}
343
344struct inferior *
345find_inferior_pid (int pid)
346{
347 struct inferior *inf;
348
6c95b8df
PA
349 /* Looking for inferior pid == 0 is always wrong, and indicative of
350 a bug somewhere else. There may be more than one with pid == 0,
351 for instance. */
352 gdb_assert (pid != 0);
353
b77209e0
PA
354 for (inf = inferior_list; inf; inf = inf->next)
355 if (inf->pid == pid)
356 return inf;
357
358 return NULL;
359}
360
c9657e70
SM
361/* See inferior.h */
362
363struct inferior *
364find_inferior_ptid (ptid_t ptid)
365{
366 return find_inferior_pid (ptid_get_pid (ptid));
367}
368
32990ada 369/* See inferior.h. */
6c95b8df
PA
370
371struct inferior *
372find_inferior_for_program_space (struct program_space *pspace)
373{
32990ada
PA
374 struct inferior *inf = current_inferior ();
375
376 if (inf->pspace == pspace)
377 return inf;
6c95b8df
PA
378
379 for (inf = inferior_list; inf != NULL; inf = inf->next)
380 {
381 if (inf->pspace == pspace)
382 return inf;
383 }
384
385 return NULL;
386}
387
b77209e0
PA
388struct inferior *
389iterate_over_inferiors (int (*callback) (struct inferior *, void *),
390 void *data)
391{
392 struct inferior *inf, *infnext;
393
394 for (inf = inferior_list; inf; inf = infnext)
395 {
396 infnext = inf->next;
397 if ((*callback) (inf, data))
398 return inf;
399 }
400
401 return NULL;
402}
403
404int
405valid_gdb_inferior_id (int num)
406{
407 struct inferior *inf;
408
409 for (inf = inferior_list; inf; inf = inf->next)
410 if (inf->num == num)
411 return 1;
412
413 return 0;
414}
415
416int
417pid_to_gdb_inferior_id (int pid)
418{
419 struct inferior *inf;
420
421 for (inf = inferior_list; inf; inf = inf->next)
422 if (inf->pid == pid)
423 return inf->num;
424
425 return 0;
426}
427
428int
429gdb_inferior_id_to_pid (int num)
430{
431 struct inferior *inferior = find_inferior_id (num);
432 if (inferior)
433 return inferior->pid;
434 else
435 return -1;
436}
437
438int
439in_inferior_list (int pid)
440{
441 struct inferior *inf;
442
443 for (inf = inferior_list; inf; inf = inf->next)
444 if (inf->pid == pid)
445 return 1;
446
447 return 0;
448}
449
450int
451have_inferiors (void)
452{
6c95b8df
PA
453 struct inferior *inf;
454
455 for (inf = inferior_list; inf; inf = inf->next)
456 if (inf->pid != 0)
457 return 1;
458
459 return 0;
b77209e0
PA
460}
461
c35b1492
PA
462int
463have_live_inferiors (void)
464{
cd2effb2 465 struct inferior *inf;
6c95b8df 466
cd2effb2
JK
467 for (inf = inferior_list; inf; inf = inf->next)
468 if (inf->pid != 0)
469 {
470 struct thread_info *tp;
471
472 tp = any_thread_of_process (inf->pid);
aeaec162
TT
473 if (tp && target_has_execution_1 (tp->ptid))
474 break;
cd2effb2
JK
475 }
476
cd2effb2 477 return inf != NULL;
6c95b8df
PA
478}
479
bed8455c
DE
480/* Prune away any unused inferiors, and then prune away no longer used
481 program spaces. */
6c95b8df
PA
482
483void
484prune_inferiors (void)
485{
486 struct inferior *ss, **ss_link;
487 struct inferior *current = current_inferior ();
488
489 ss = inferior_list;
490 ss_link = &inferior_list;
491 while (ss)
492 {
493 if (ss == current
494 || !ss->removable
495 || ss->pid != 0)
496 {
497 ss_link = &ss->next;
498 ss = *ss_link;
499 continue;
500 }
501
502 *ss_link = ss->next;
7a41607e 503 delete_inferior (ss);
6c95b8df
PA
504 ss = *ss_link;
505 }
6c95b8df
PA
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 589
1f0c4988
JK
590 if (inf->pspace->pspace_exec_filename != NULL)
591 ui_out_field_string (uiout, "exec", inf->pspace->pspace_exec_filename);
6c95b8df
PA
592 else
593 ui_out_field_skip (uiout, "exec");
594
595 /* Print extra info that isn't really fit to always present in
596 tabular form. Currently we print the vfork parent/child
597 relationships, if any. */
598 if (inf->vfork_parent)
599 {
600 ui_out_text (uiout, _("\n\tis vfork child of inferior "));
601 ui_out_field_int (uiout, "vfork-parent", inf->vfork_parent->num);
602 }
603 if (inf->vfork_child)
604 {
605 ui_out_text (uiout, _("\n\tis vfork parent of inferior "));
606 ui_out_field_int (uiout, "vfork-child", inf->vfork_child->num);
607 }
b77209e0
PA
608
609 ui_out_text (uiout, "\n");
610 do_cleanups (chain2);
611 }
612
613 do_cleanups (old_chain);
614}
615
2277426b
PA
616static void
617detach_inferior_command (char *args, int from_tty)
618{
619 int num, pid;
620 struct thread_info *tp;
197f0a60 621 struct get_number_or_range_state state;
2277426b
PA
622
623 if (!args || !*args)
af624141 624 error (_("Requires argument (inferior id(s) to detach)"));
2277426b 625
197f0a60
TT
626 init_number_or_range (&state, args);
627 while (!state.finished)
af624141 628 {
197f0a60 629 num = get_number_or_range (&state);
2277426b 630
af624141
MS
631 if (!valid_gdb_inferior_id (num))
632 {
633 warning (_("Inferior ID %d not known."), num);
634 continue;
635 }
2277426b 636
af624141 637 pid = gdb_inferior_id_to_pid (num);
e3ae3c43
PP
638 if (pid == 0)
639 {
640 warning (_("Inferior ID %d is not running."), num);
641 continue;
642 }
2277426b 643
af624141
MS
644 tp = any_thread_of_process (pid);
645 if (!tp)
646 {
647 warning (_("Inferior ID %d has no threads."), num);
648 continue;
649 }
2277426b 650
af624141 651 switch_to_thread (tp->ptid);
2277426b 652
af624141
MS
653 detach_command (NULL, from_tty);
654 }
2277426b
PA
655}
656
657static void
658kill_inferior_command (char *args, int from_tty)
659{
660 int num, pid;
661 struct thread_info *tp;
197f0a60 662 struct get_number_or_range_state state;
2277426b
PA
663
664 if (!args || !*args)
af624141 665 error (_("Requires argument (inferior id(s) to kill)"));
2277426b 666
197f0a60
TT
667 init_number_or_range (&state, args);
668 while (!state.finished)
af624141 669 {
197f0a60 670 num = get_number_or_range (&state);
2277426b 671
af624141
MS
672 if (!valid_gdb_inferior_id (num))
673 {
674 warning (_("Inferior ID %d not known."), num);
675 continue;
676 }
2277426b 677
af624141 678 pid = gdb_inferior_id_to_pid (num);
e3ae3c43
PP
679 if (pid == 0)
680 {
681 warning (_("Inferior ID %d is not running."), num);
682 continue;
683 }
2277426b 684
af624141
MS
685 tp = any_thread_of_process (pid);
686 if (!tp)
687 {
688 warning (_("Inferior ID %d has no threads."), num);
689 continue;
690 }
2277426b 691
af624141 692 switch_to_thread (tp->ptid);
2277426b 693
af624141
MS
694 target_kill ();
695 }
2277426b
PA
696
697 bfd_cache_close_all ();
698}
699
700static void
701inferior_command (char *args, int from_tty)
702{
6c95b8df
PA
703 struct inferior *inf;
704 int num;
2277426b
PA
705
706 num = parse_and_eval_long (args);
707
6c95b8df
PA
708 inf = find_inferior_id (num);
709 if (inf == NULL)
2277426b
PA
710 error (_("Inferior ID %d not known."), num);
711
6c95b8df
PA
712 printf_filtered (_("[Switching to inferior %d [%s] (%s)]\n"),
713 inf->num,
db2b9fdd 714 inferior_pid_to_str (inf->pid),
1f0c4988
JK
715 (inf->pspace->pspace_exec_filename != NULL
716 ? inf->pspace->pspace_exec_filename
6c95b8df 717 : _("<noexec>")));
2277426b 718
6c95b8df 719 if (inf->pid != 0)
2277426b 720 {
6c95b8df
PA
721 if (inf->pid != ptid_get_pid (inferior_ptid))
722 {
723 struct thread_info *tp;
2277426b 724
6c95b8df
PA
725 tp = any_thread_of_process (inf->pid);
726 if (!tp)
727 error (_("Inferior has no threads."));
2277426b 728
6c95b8df
PA
729 switch_to_thread (tp->ptid);
730 }
731
732 printf_filtered (_("[Switching to thread %d (%s)] "),
733 pid_to_thread_id (inferior_ptid),
734 target_pid_to_str (inferior_ptid));
2277426b 735 }
6c95b8df
PA
736 else
737 {
738 struct inferior *inf;
2277426b 739
6c95b8df
PA
740 inf = find_inferior_id (num);
741 set_current_inferior (inf);
742 switch_to_thread (null_ptid);
743 set_current_program_space (inf->pspace);
744 }
2277426b 745
6c95b8df 746 if (inf->pid != 0 && is_running (inferior_ptid))
79a45e25 747 ui_out_text (current_uiout, "(running)\n");
6c95b8df 748 else if (inf->pid != 0)
2277426b 749 {
79a45e25 750 ui_out_text (current_uiout, "\n");
08d72866 751 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2277426b
PA
752 }
753}
754
b77209e0
PA
755/* Print information about currently known inferiors. */
756
757static void
2277426b 758info_inferiors_command (char *args, int from_tty)
b77209e0 759{
79a45e25 760 print_inferior (current_uiout, args);
b77209e0
PA
761}
762
6c95b8df
PA
763/* remove-inferior ID */
764
70221824 765static void
6c95b8df
PA
766remove_inferior_command (char *args, int from_tty)
767{
768 int num;
769 struct inferior *inf;
197f0a60 770 struct get_number_or_range_state state;
6c95b8df 771
af624141
MS
772 if (args == NULL || *args == '\0')
773 error (_("Requires an argument (inferior id(s) to remove)"));
6c95b8df 774
197f0a60
TT
775 init_number_or_range (&state, args);
776 while (!state.finished)
af624141 777 {
197f0a60 778 num = get_number_or_range (&state);
af624141 779 inf = find_inferior_id (num);
6c95b8df 780
af624141
MS
781 if (inf == NULL)
782 {
783 warning (_("Inferior ID %d not known."), num);
784 continue;
785 }
786
787 if (inf == current_inferior ())
788 {
789 warning (_("Can not remove current symbol inferior %d."), num);
790 continue;
791 }
8fa067af 792
af624141
MS
793 if (inf->pid != 0)
794 {
795 warning (_("Can not remove active inferior %d."), num);
796 continue;
797 }
6c95b8df 798
7a41607e 799 delete_inferior (inf);
af624141 800 }
6c95b8df
PA
801}
802
a79b8f6e
VP
803struct inferior *
804add_inferior_with_spaces (void)
805{
806 struct address_space *aspace;
807 struct program_space *pspace;
808 struct inferior *inf;
6ecd4729 809 struct gdbarch_info info;
a79b8f6e
VP
810
811 /* If all inferiors share an address space on this system, this
812 doesn't really return a new address space; otherwise, it
813 really does. */
814 aspace = maybe_new_address_space ();
815 pspace = add_program_space (aspace);
816 inf = add_inferior (0);
817 inf->pspace = pspace;
818 inf->aspace = pspace->aspace;
819
6ecd4729
PA
820 /* Setup the inferior's initial arch, based on information obtained
821 from the global "set ..." options. */
822 gdbarch_info_init (&info);
823 inf->gdbarch = gdbarch_find_by_info (info);
824 /* The "set ..." options reject invalid settings, so we should
825 always have a valid arch by now. */
826 gdb_assert (inf->gdbarch != NULL);
827
a79b8f6e
VP
828 return inf;
829}
6c95b8df
PA
830
831/* add-inferior [-copies N] [-exec FILENAME] */
832
70221824 833static void
6c95b8df
PA
834add_inferior_command (char *args, int from_tty)
835{
836 int i, copies = 1;
837 char *exec = NULL;
838 char **argv;
839 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
840
841 if (args)
842 {
843 argv = gdb_buildargv (args);
844 make_cleanup_freeargv (argv);
845
846 for (; *argv != NULL; argv++)
847 {
848 if (**argv == '-')
849 {
850 if (strcmp (*argv, "-copies") == 0)
851 {
852 ++argv;
853 if (!*argv)
854 error (_("No argument to -copies"));
855 copies = parse_and_eval_long (*argv);
856 }
857 else if (strcmp (*argv, "-exec") == 0)
858 {
859 ++argv;
860 if (!*argv)
861 error (_("No argument to -exec"));
47902076
HAQ
862 exec = tilde_expand (*argv);
863 make_cleanup (xfree, exec);
6c95b8df
PA
864 }
865 }
866 else
867 error (_("Invalid argument"));
868 }
869 }
870
871 save_current_space_and_thread ();
872
873 for (i = 0; i < copies; ++i)
874 {
a79b8f6e 875 struct inferior *inf = add_inferior_with_spaces ();
6c95b8df
PA
876
877 printf_filtered (_("Added inferior %d\n"), inf->num);
878
879 if (exec != NULL)
880 {
881 /* Switch over temporarily, while reading executable and
1777feb0 882 symbols.q. */
a79b8f6e 883 set_current_program_space (inf->pspace);
6c95b8df
PA
884 set_current_inferior (inf);
885 switch_to_thread (null_ptid);
886
887 exec_file_attach (exec, from_tty);
888 symbol_file_add_main (exec, from_tty);
889 }
890 }
891
892 do_cleanups (old_chain);
893}
894
895/* clone-inferior [-copies N] [ID] */
896
70221824 897static void
6c95b8df
PA
898clone_inferior_command (char *args, int from_tty)
899{
900 int i, copies = 1;
901 char **argv;
902 struct inferior *orginf = NULL;
903 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
904
905 if (args)
906 {
907 argv = gdb_buildargv (args);
908 make_cleanup_freeargv (argv);
909
910 for (; *argv != NULL; argv++)
911 {
912 if (**argv == '-')
913 {
914 if (strcmp (*argv, "-copies") == 0)
915 {
916 ++argv;
917 if (!*argv)
918 error (_("No argument to -copies"));
919 copies = parse_and_eval_long (*argv);
920
921 if (copies < 0)
922 error (_("Invalid copies number"));
923 }
924 }
925 else
926 {
927 if (orginf == NULL)
928 {
929 int num;
930
931 /* The first non-option (-) argument specified the
932 program space ID. */
933 num = parse_and_eval_long (*argv);
934 orginf = find_inferior_id (num);
935
936 if (orginf == NULL)
937 error (_("Inferior ID %d not known."), num);
938 continue;
939 }
940 else
941 error (_("Invalid argument"));
942 }
943 }
944 }
945
946 /* If no inferior id was specified, then the user wants to clone the
947 current inferior. */
948 if (orginf == NULL)
949 orginf = current_inferior ();
950
951 save_current_space_and_thread ();
952
953 for (i = 0; i < copies; ++i)
954 {
955 struct address_space *aspace;
956 struct program_space *pspace;
957 struct inferior *inf;
958
959 /* If all inferiors share an address space on this system, this
960 doesn't really return a new address space; otherwise, it
961 really does. */
962 aspace = maybe_new_address_space ();
963 pspace = add_program_space (aspace);
964 inf = add_inferior (0);
965 inf->pspace = pspace;
966 inf->aspace = pspace->aspace;
6ecd4729
PA
967 inf->gdbarch = orginf->gdbarch;
968
969 /* If the original inferior had a user specified target
970 description, make the clone use it too. */
971 if (target_desc_info_from_user_p (inf->tdesc_info))
972 copy_inferior_target_desc_info (inf, orginf);
6c95b8df
PA
973
974 printf_filtered (_("Added inferior %d.\n"), inf->num);
975
976 set_current_inferior (inf);
977 switch_to_thread (null_ptid);
978 clone_program_space (pspace, orginf->pspace);
979 }
980
981 do_cleanups (old_chain);
982}
983
b77209e0
PA
984/* Print notices when new inferiors are created and die. */
985static void
986show_print_inferior_events (struct ui_file *file, int from_tty,
987 struct cmd_list_element *c, const char *value)
988{
989 fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
990}
991
6c95b8df
PA
992\f
993
6c95b8df
PA
994void
995initialize_inferiors (void)
996{
06da564e
EZ
997 struct cmd_list_element *c = NULL;
998
6c95b8df
PA
999 /* There's always one inferior. Note that this function isn't an
1000 automatic _initialize_foo function, since other _initialize_foo
1001 routines may need to install their per-inferior data keys. We
1002 can only allocate an inferior when all those modules have done
1003 that. Do this after initialize_progspace, due to the
1004 current_program_space reference. */
1005 current_inferior_ = add_inferior (0);
1006 current_inferior_->pspace = current_program_space;
1007 current_inferior_->aspace = current_program_space->aspace;
6ecd4729
PA
1008 /* The architecture will be initialized shortly, by
1009 initialize_current_architecture. */
6c95b8df 1010
c82c0b55
MS
1011 add_info ("inferiors", info_inferiors_command,
1012 _("IDs of specified inferiors (all inferiors if no argument)."));
b77209e0 1013
06da564e 1014 c = add_com ("add-inferior", no_class, add_inferior_command, _("\
6c95b8df
PA
1015Add a new inferior.\n\
1016Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
af624141 1017N is the optional number of inferiors to add, default is 1.\n\
6c95b8df
PA
1018FILENAME is the file name of the executable to use\n\
1019as main program."));
06da564e 1020 set_cmd_completer (c, filename_completer);
6c95b8df 1021
af624141
MS
1022 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1023Remove inferior ID (or list of IDs).\n\
1024Usage: remove-inferiors ID..."));
6c95b8df
PA
1025
1026 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1027Clone inferior ID.\n\
1028Usage: clone-inferior [-copies <N>] [ID]\n\
1029Add N copies of inferior ID. The new inferior has the same\n\
1030executable loaded as the copied inferior. If -copies is not specified,\n\
1031adds 1 copy. If ID is not specified, it is the current inferior\n\
1032that is cloned."));
2277426b 1033
af624141
MS
1034 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1035Detach from inferior ID (or list of IDS)."),
2277426b
PA
1036 &detachlist);
1037
af624141
MS
1038 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1039Kill inferior ID (or list of IDs)."),
2277426b
PA
1040 &killlist);
1041
1042 add_cmd ("inferior", class_run, inferior_command, _("\
1043Use this command to switch between inferiors.\n\
1044The new inferior ID must be currently known."),
1045 &cmdlist);
6c95b8df
PA
1046
1047 add_setshow_boolean_cmd ("inferior-events", no_class,
1048 &print_inferior_events, _("\
1049Set printing of inferior events (e.g., inferior start and exit)."), _("\
1050Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1051 NULL,
1052 show_print_inferior_events,
1053 &setprintlist, &showprintlist);
1054
b77209e0 1055}
This page took 1.109946 seconds and 4 git commands to generate.