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