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