2009-12-02 Tristan Gingold <gingold@adacore.com>
[deliverable/binutils-gdb.git] / gdb / inferior.c
1 /* Multi-process control for GDB, the GNU debugger.
2
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
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"
21 #include "exec.h"
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"
28 #include "observer.h"
29 #include "gdbthread.h"
30 #include "gdbcore.h"
31 #include "symfile.h"
32
33 void _initialize_inferiors (void);
34
35 static void inferior_alloc_data (struct inferior *inf);
36 static void inferior_free_data (struct inferior *inf);
37
38 struct inferior *inferior_list = NULL;
39 static int highest_inferior_num;
40
41 /* Print notices on inferior events (attach, detach, etc.), set with
42 `set print inferior-events'. */
43 static int print_inferior_events = 0;
44
45 /* The Current Inferior. */
46 static struct inferior *current_inferior_ = NULL;
47
48 struct inferior*
49 current_inferior (void)
50 {
51 return current_inferior_;
52 }
53
54 void
55 set_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
66 static void
67 restore_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
77 struct cleanup *
78 save_current_inferior (void)
79 {
80 struct cleanup *old_chain = make_cleanup (restore_inferior,
81 current_inferior_);
82 return old_chain;
83 }
84
85 static void
86 free_inferior (struct inferior *inf)
87 {
88 discard_all_inferior_continuations (inf);
89 inferior_free_data (inf);
90 xfree (inf->private);
91 xfree (inf);
92 }
93
94 void
95 init_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
112 struct inferior *
113 add_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
121 inf->stop_soon = NO_STOP_QUIETLY;
122
123 inf->num = ++highest_inferior_num;
124 inf->next = inferior_list;
125 inferior_list = inf;
126
127 inferior_alloc_data (inf);
128
129 if (pid != 0)
130 inferior_appeared (inf, pid);
131
132 return inf;
133 }
134
135 struct inferior *
136 add_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
146 struct delete_thread_of_inferior_arg
147 {
148 int pid;
149 int silent;
150 };
151
152 static int
153 delete_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
168 void
169 delete_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
187 /* If SILENT then be quiet -- don't announce a inferior death, or the
188 exit of its threads. */
189
190 static void
191 delete_inferior_1 (struct inferior *todel, int silent)
192 {
193 struct inferior *inf, *infprev;
194 struct delete_thread_of_inferior_arg arg;
195
196 infprev = NULL;
197
198 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
199 if (inf == todel)
200 break;
201
202 if (!inf)
203 return;
204
205 arg.pid = inf->pid;
206 arg.silent = silent;
207
208 iterate_over_threads (delete_thread_of_inferior, &arg);
209
210 if (infprev)
211 infprev->next = inf->next;
212 else
213 inferior_list = inf->next;
214
215 free_inferior (inf);
216 }
217
218 void
219 delete_inferior (int pid)
220 {
221 struct inferior *inf = find_inferior_pid (pid);
222
223 delete_inferior_1 (inf, 0);
224
225 if (print_inferior_events)
226 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
227 }
228
229 void
230 delete_inferior_silent (int pid)
231 {
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
241 static void
242 exit_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
271 void
272 exit_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
281 void
282 exit_inferior_silent (int pid)
283 {
284 struct inferior *inf = find_inferior_pid (pid);
285 exit_inferior_1 (inf, 1);
286 }
287
288 void
289 exit_inferior_num_silent (int num)
290 {
291 struct inferior *inf = find_inferior_id (num);
292
293 exit_inferior_1 (inf, 1);
294 }
295
296 void
297 detach_inferior (int pid)
298 {
299 struct inferior *inf = find_inferior_pid (pid);
300 exit_inferior_1 (inf, 1);
301
302 if (print_inferior_events)
303 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
304 }
305
306 void
307 inferior_appeared (struct inferior *inf, int pid)
308 {
309 inf->pid = pid;
310
311 observer_notify_inferior_appeared (pid);
312 }
313
314 void
315 discard_all_inferiors (void)
316 {
317 struct inferior *inf;
318
319 for (inf = inferior_list; inf; inf = inf->next)
320 {
321 if (inf->pid != 0)
322 exit_inferior_silent (inf->pid);
323 }
324 }
325
326 struct inferior *
327 find_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
338 struct inferior *
339 find_inferior_pid (int pid)
340 {
341 struct inferior *inf;
342
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
348 for (inf = inferior_list; inf; inf = inf->next)
349 if (inf->pid == pid)
350 return inf;
351
352 return NULL;
353 }
354
355 /* Find an inferior bound to PSPACE. */
356
357 struct inferior *
358 find_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
371 struct inferior *
372 iterate_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
387 int
388 valid_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
399 int
400 pid_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
411 int
412 gdb_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
421 int
422 in_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
433 int
434 have_inferiors (void)
435 {
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;
443 }
444
445 int
446 have_live_inferiors (void)
447 {
448 struct target_ops *t;
449
450 /* The check on stratum suffices, as GDB doesn't currently support
451 multiple target interfaces. */
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
463 void
464 prune_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
492 int
493 number_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;
502 }
503
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. */
509 void
510 print_inferior (struct ui_out *uiout, int requested_inferior)
511 {
512 struct inferior *inf;
513 struct cleanup *old_chain;
514 int inf_count = 0;
515
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
533 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
534 "inferiors");
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");
538 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
539
540 ui_out_table_body (uiout);
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
550 if (inf == current_inferior ())
551 ui_out_field_string (uiout, "current", "*");
552 else
553 ui_out_field_skip (uiout, "current");
554
555 ui_out_field_int (uiout, "number", inf->num);
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 }
582
583 ui_out_text (uiout, "\n");
584 do_cleanups (chain2);
585 }
586
587 do_cleanups (old_chain);
588 }
589
590 static void
591 detach_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
615 static void
616 kill_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
642 static void
643 inferior_command (char *args, int from_tty)
644 {
645 struct inferior *inf;
646 int num;
647
648 num = parse_and_eval_long (args);
649
650 inf = find_inferior_id (num);
651 if (inf == NULL)
652 error (_("Inferior ID %d not known."), num);
653
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>")));
660
661 if (inf->pid != 0)
662 {
663 if (inf->pid != ptid_get_pid (inferior_ptid))
664 {
665 struct thread_info *tp;
666
667 tp = any_thread_of_process (inf->pid);
668 if (!tp)
669 error (_("Inferior has no threads."));
670
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));
677 }
678 else
679 {
680 struct inferior *inf;
681
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 }
687
688 if (inf->pid != 0 && is_running (inferior_ptid))
689 ui_out_text (uiout, "(running)\n");
690 else if (inf->pid != 0)
691 {
692 ui_out_text (uiout, "\n");
693 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
694 }
695 }
696
697 /* Print information about currently known inferiors. */
698
699 static void
700 info_inferiors_command (char *args, int from_tty)
701 {
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);
712 }
713
714 /* remove-inferior ID */
715
716 void
717 remove_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
737 void
738 add_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
811 void
812 clone_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
892 /* Print notices when new inferiors are created and die. */
893 static void
894 show_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
900 \f
901
902 /* Keep a registry of per-inferior data-pointers required by other GDB
903 modules. */
904
905 struct inferior_data
906 {
907 unsigned index;
908 void (*cleanup) (struct inferior *, void *);
909 };
910
911 struct inferior_data_registration
912 {
913 struct inferior_data *data;
914 struct inferior_data_registration *next;
915 };
916
917 struct inferior_data_registry
918 {
919 struct inferior_data_registration *registrations;
920 unsigned num_registrations;
921 };
922
923 static struct inferior_data_registry inferior_data_registry
924 = { NULL, 0 };
925
926 const struct inferior_data *
927 register_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
945 const struct inferior_data *
946 register_inferior_data (void)
947 {
948 return register_inferior_data_with_cleanup (NULL);
949 }
950
951 static void
952 inferior_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
959 static void
960 inferior_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
968 void
969 clear_inferior_data (struct inferior *inf)
970 {
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
985 void
986 set_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
994 void *
995 inferior_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
1001 void
1002 initialize_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
1014 add_info ("inferiors", info_inferiors_command,
1015 _("IDs of currently known inferiors."));
1016
1017 add_com ("add-inferior", no_class, add_inferior_command, _("\
1018 Add a new inferior.\n\
1019 Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
1020 N is the optional number of inferior to add, default is 1.\n\
1021 FILENAME is the file name of the executable to use\n\
1022 as main program."));
1023
1024 add_com ("remove-inferior", no_class, remove_inferior_command, _("\
1025 Remove inferior ID.\n\
1026 Usage: remove-inferior ID"));
1027
1028 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1029 Clone inferior ID.\n\
1030 Usage: clone-inferior [-copies <N>] [ID]\n\
1031 Add N copies of inferior ID. The new inferior has the same\n\
1032 executable loaded as the copied inferior. If -copies is not specified,\n\
1033 adds 1 copy. If ID is not specified, it is the current inferior\n\
1034 that is cloned."));
1035
1036 add_cmd ("inferior", class_run, detach_inferior_command, _("\
1037 Detach from inferior ID."),
1038 &detachlist);
1039
1040 add_cmd ("inferior", class_run, kill_inferior_command, _("\
1041 Kill inferior ID."),
1042 &killlist);
1043
1044 add_cmd ("inferior", class_run, inferior_command, _("\
1045 Use this command to switch between inferiors.\n\
1046 The new inferior ID must be currently known."),
1047 &cmdlist);
1048
1049 add_setshow_boolean_cmd ("inferior-events", no_class,
1050 &print_inferior_events, _("\
1051 Set printing of inferior events (e.g., inferior start and exit)."), _("\
1052 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1053 NULL,
1054 show_print_inferior_events,
1055 &setprintlist, &showprintlist);
1056
1057 }
This page took 0.078048 seconds and 4 git commands to generate.