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