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