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