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