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