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