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