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