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