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