* target.h (struct target_ops) <to_has_execution>: Add ptid_t
[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"
c82c0b55 33#include "cli/cli-utils.h"
b77209e0
PA
34
35void _initialize_inferiors (void);
36
6c95b8df
PA
37static void inferior_alloc_data (struct inferior *inf);
38static void inferior_free_data (struct inferior *inf);
39
40struct inferior *inferior_list = NULL;
b77209e0
PA
41static int highest_inferior_num;
42
43/* Print notices on inferior events (attach, detach, etc.), set with
44 `set print inferior-events'. */
45static int print_inferior_events = 0;
46
6c95b8df
PA
47/* The Current Inferior. */
48static struct inferior *current_inferior_ = NULL;
49
b77209e0
PA
50struct inferior*
51current_inferior (void)
52{
6c95b8df
PA
53 return current_inferior_;
54}
55
56void
57set_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
68static void
69restore_inferior (void *arg)
70{
71 struct inferior *saved_inferior = arg;
abbb1732 72
6c95b8df
PA
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
80struct cleanup *
81save_current_inferior (void)
82{
83 struct cleanup *old_chain = make_cleanup (restore_inferior,
84 current_inferior_);
abbb1732 85
6c95b8df 86 return old_chain;
b77209e0
PA
87}
88
89static void
90free_inferior (struct inferior *inf)
91{
e0ba6746 92 discard_all_inferior_continuations (inf);
6c95b8df 93 inferior_free_data (inf);
3f81c18a
VP
94 xfree (inf->args);
95 xfree (inf->terminal);
96 free_environ (inf->environment);
b77209e0
PA
97 xfree (inf->private);
98 xfree (inf);
99}
100
101void
102init_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
119struct inferior *
120add_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
16c381f0 128 inf->control.stop_soon = NO_STOP_QUIETLY;
d6b48e9c 129
b77209e0
PA
130 inf->num = ++highest_inferior_num;
131 inf->next = inferior_list;
132 inferior_list = inf;
133
3f81c18a
VP
134 inf->environment = make_environ ();
135 init_environ (inf->environment);
136
6c95b8df
PA
137 inferior_alloc_data (inf);
138
a79b8f6e
VP
139 observer_notify_inferior_added (inf);
140
6c95b8df
PA
141 if (pid != 0)
142 inferior_appeared (inf, pid);
a562dc8f 143
b77209e0
PA
144 return inf;
145}
146
147struct inferior *
148add_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
158struct delete_thread_of_inferior_arg
159{
160 int pid;
161 int silent;
162};
163
164static int
165delete_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
6c95b8df
PA
180void
181delete_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
b77209e0
PA
199/* If SILENT then be quiet -- don't announce a inferior death, or the
200 exit of its threads. */
6c95b8df 201
a79b8f6e 202void
6c95b8df 203delete_inferior_1 (struct inferior *todel, int silent)
b77209e0
PA
204{
205 struct inferior *inf, *infprev;
6c95b8df 206 struct delete_thread_of_inferior_arg arg;
b77209e0
PA
207
208 infprev = NULL;
209
210 for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
6c95b8df 211 if (inf == todel)
b77209e0
PA
212 break;
213
214 if (!inf)
215 return;
216
6c95b8df 217 arg.pid = inf->pid;
b77209e0
PA
218 arg.silent = silent;
219
220 iterate_over_threads (delete_thread_of_inferior, &arg);
4a92f99b 221
7e1789f5
PA
222 if (infprev)
223 infprev->next = inf->next;
224 else
225 inferior_list = inf->next;
226
a79b8f6e
VP
227 observer_notify_inferior_removed (inf);
228
7e1789f5 229 free_inferior (inf);
b77209e0
PA
230}
231
232void
233delete_inferior (int pid)
234{
6c95b8df
PA
235 struct inferior *inf = find_inferior_pid (pid);
236
237 delete_inferior_1 (inf, 0);
b77209e0
PA
238
239 if (print_inferior_events)
240 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
241}
242
243void
244delete_inferior_silent (int pid)
245{
6c95b8df
PA
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
255static void
256exit_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. */
a79b8f6e 275 observer_notify_inferior_exit (inf);
6c95b8df
PA
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
285void
286exit_inferior (int pid)
287{
288 struct inferior *inf = find_inferior_pid (pid);
abbb1732 289
6c95b8df
PA
290 exit_inferior_1 (inf, 0);
291
292 if (print_inferior_events)
293 printf_unfiltered (_("[Inferior %d exited]\n"), pid);
294}
295
296void
297exit_inferior_silent (int pid)
298{
299 struct inferior *inf = find_inferior_pid (pid);
abbb1732 300
6c95b8df
PA
301 exit_inferior_1 (inf, 1);
302}
303
304void
305exit_inferior_num_silent (int num)
306{
307 struct inferior *inf = find_inferior_id (num);
308
309 exit_inferior_1 (inf, 1);
b77209e0
PA
310}
311
312void
313detach_inferior (int pid)
314{
6c95b8df 315 struct inferior *inf = find_inferior_pid (pid);
abbb1732 316
6c95b8df 317 exit_inferior_1 (inf, 1);
b77209e0
PA
318
319 if (print_inferior_events)
320 printf_unfiltered (_("[Inferior %d detached]\n"), pid);
321}
322
6c95b8df
PA
323void
324inferior_appeared (struct inferior *inf, int pid)
325{
326 inf->pid = pid;
327
a79b8f6e 328 observer_notify_inferior_appeared (inf);
6c95b8df
PA
329}
330
82f73884
PA
331void
332discard_all_inferiors (void)
333{
6c95b8df 334 struct inferior *inf;
82f73884 335
6c95b8df 336 for (inf = inferior_list; inf; inf = inf->next)
82f73884 337 {
6c95b8df
PA
338 if (inf->pid != 0)
339 exit_inferior_silent (inf->pid);
82f73884
PA
340 }
341}
342
6c95b8df 343struct inferior *
b77209e0
PA
344find_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
355struct inferior *
356find_inferior_pid (int pid)
357{
358 struct inferior *inf;
359
6c95b8df
PA
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
b77209e0
PA
365 for (inf = inferior_list; inf; inf = inf->next)
366 if (inf->pid == pid)
367 return inf;
368
369 return NULL;
370}
371
6c95b8df
PA
372/* Find an inferior bound to PSPACE. */
373
374struct inferior *
375find_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
b77209e0
PA
388struct inferior *
389iterate_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
404int
405valid_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
416int
417pid_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
428int
429gdb_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
438int
439in_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
450int
451have_inferiors (void)
452{
6c95b8df
PA
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;
b77209e0
PA
460}
461
c35b1492
PA
462int
463have_live_inferiors (void)
464{
cd2effb2 465 struct inferior *inf;
6c95b8df 466
cd2effb2
JK
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);
aeaec162
TT
473 if (tp && target_has_execution_1 (tp->ptid))
474 break;
cd2effb2
JK
475 }
476
cd2effb2 477 return inf != NULL;
6c95b8df
PA
478}
479
480/* Prune away automatically added program spaces that aren't required
481 anymore. */
482
483void
484prune_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
512int
513number_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;
c35b1492
PA
522}
523
b77209e0
PA
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
c82c0b55
MS
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
531static void
532print_inferior (struct ui_out *uiout, char *requested_inferiors)
b77209e0
PA
533{
534 struct inferior *inf;
535 struct cleanup *old_chain;
8bb318c6 536 int inf_count = 0;
b77209e0 537
8bb318c6
TT
538 /* Compute number of inferiors we will print. */
539 for (inf = inferior_list; inf; inf = inf->next)
540 {
c82c0b55 541 if (!number_is_in_list (requested_inferiors, inf->num))
8bb318c6
TT
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
6c95b8df 553 old_chain = make_cleanup_ui_out_table_begin_end (uiout, 4, inf_count,
8bb318c6 554 "inferiors");
3a1ff0b6
PA
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");
6c95b8df 558 ui_out_table_header (uiout, 17, ui_left, "exec", "Executable");
b77209e0 559
6c95b8df 560 ui_out_table_body (uiout);
b77209e0
PA
561 for (inf = inferior_list; inf; inf = inf->next)
562 {
563 struct cleanup *chain2;
564
c82c0b55 565 if (!number_is_in_list (requested_inferiors, inf->num))
b77209e0
PA
566 continue;
567
568 chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
569
6c95b8df 570 if (inf == current_inferior ())
8bb318c6 571 ui_out_field_string (uiout, "current", "*");
b77209e0 572 else
8bb318c6 573 ui_out_field_skip (uiout, "current");
b77209e0 574
3a1ff0b6 575 ui_out_field_int (uiout, "number", inf->num);
6c95b8df
PA
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 }
b77209e0
PA
602
603 ui_out_text (uiout, "\n");
604 do_cleanups (chain2);
605 }
606
607 do_cleanups (old_chain);
608}
609
2277426b
PA
610static void
611detach_inferior_command (char *args, int from_tty)
612{
613 int num, pid;
614 struct thread_info *tp;
615
616 if (!args || !*args)
af624141 617 error (_("Requires argument (inferior id(s) to detach)"));
2277426b 618
af624141
MS
619 while (*args != '\0')
620 {
621 num = get_number_or_range (&args);
2277426b 622
af624141
MS
623 if (!valid_gdb_inferior_id (num))
624 {
625 warning (_("Inferior ID %d not known."), num);
626 continue;
627 }
2277426b 628
af624141 629 pid = gdb_inferior_id_to_pid (num);
2277426b 630
af624141
MS
631 tp = any_thread_of_process (pid);
632 if (!tp)
633 {
634 warning (_("Inferior ID %d has no threads."), num);
635 continue;
636 }
2277426b 637
af624141 638 switch_to_thread (tp->ptid);
2277426b 639
af624141
MS
640 detach_command (NULL, from_tty);
641 }
2277426b
PA
642}
643
644static void
645kill_inferior_command (char *args, int from_tty)
646{
647 int num, pid;
648 struct thread_info *tp;
649
650 if (!args || !*args)
af624141 651 error (_("Requires argument (inferior id(s) to kill)"));
2277426b 652
af624141
MS
653 while (*args != '\0')
654 {
655 num = get_number_or_range (&args);
2277426b 656
af624141
MS
657 if (!valid_gdb_inferior_id (num))
658 {
659 warning (_("Inferior ID %d not known."), num);
660 continue;
661 }
2277426b 662
af624141 663 pid = gdb_inferior_id_to_pid (num);
2277426b 664
af624141
MS
665 tp = any_thread_of_process (pid);
666 if (!tp)
667 {
668 warning (_("Inferior ID %d has no threads."), num);
669 continue;
670 }
2277426b 671
af624141 672 switch_to_thread (tp->ptid);
2277426b 673
af624141
MS
674 target_kill ();
675 }
2277426b
PA
676
677 bfd_cache_close_all ();
678}
679
680static void
681inferior_command (char *args, int from_tty)
682{
6c95b8df
PA
683 struct inferior *inf;
684 int num;
2277426b
PA
685
686 num = parse_and_eval_long (args);
687
6c95b8df
PA
688 inf = find_inferior_id (num);
689 if (inf == NULL)
2277426b
PA
690 error (_("Inferior ID %d not known."), num);
691
6c95b8df
PA
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>")));
2277426b 698
6c95b8df 699 if (inf->pid != 0)
2277426b 700 {
6c95b8df
PA
701 if (inf->pid != ptid_get_pid (inferior_ptid))
702 {
703 struct thread_info *tp;
2277426b 704
6c95b8df
PA
705 tp = any_thread_of_process (inf->pid);
706 if (!tp)
707 error (_("Inferior has no threads."));
2277426b 708
6c95b8df
PA
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));
2277426b 715 }
6c95b8df
PA
716 else
717 {
718 struct inferior *inf;
2277426b 719
6c95b8df
PA
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 }
2277426b 725
6c95b8df 726 if (inf->pid != 0 && is_running (inferior_ptid))
2277426b 727 ui_out_text (uiout, "(running)\n");
6c95b8df 728 else if (inf->pid != 0)
2277426b
PA
729 {
730 ui_out_text (uiout, "\n");
731 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
732 }
733}
734
b77209e0
PA
735/* Print information about currently known inferiors. */
736
737static void
2277426b 738info_inferiors_command (char *args, int from_tty)
b77209e0 739{
c82c0b55 740 print_inferior (uiout, args);
b77209e0
PA
741}
742
6c95b8df
PA
743/* remove-inferior ID */
744
745void
746remove_inferior_command (char *args, int from_tty)
747{
748 int num;
749 struct inferior *inf;
750
af624141
MS
751 if (args == NULL || *args == '\0')
752 error (_("Requires an argument (inferior id(s) to remove)"));
6c95b8df 753
af624141
MS
754 while (*args != '\0')
755 {
756 num = get_number_or_range (&args);
757 inf = find_inferior_id (num);
6c95b8df 758
af624141
MS
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 }
8fa067af 770
af624141
MS
771 if (inf->pid != 0)
772 {
773 warning (_("Can not remove active inferior %d."), num);
774 continue;
775 }
6c95b8df 776
af624141
MS
777 delete_inferior_1 (inf, 1);
778 }
6c95b8df
PA
779}
780
a79b8f6e
VP
781struct inferior *
782add_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}
6c95b8df
PA
799
800/* add-inferior [-copies N] [-exec FILENAME] */
801
802void
803add_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 {
a79b8f6e 843 struct inferior *inf = add_inferior_with_spaces ();
6c95b8df
PA
844
845 printf_filtered (_("Added inferior %d\n"), inf->num);
846
847 if (exec != NULL)
848 {
849 /* Switch over temporarily, while reading executable and
1777feb0 850 symbols.q. */
a79b8f6e 851 set_current_program_space (inf->pspace);
6c95b8df
PA
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
865void
866clone_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
b77209e0
PA
946/* Print notices when new inferiors are created and die. */
947static void
948show_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
6c95b8df
PA
954\f
955
956/* Keep a registry of per-inferior data-pointers required by other GDB
957 modules. */
958
959struct inferior_data
960{
961 unsigned index;
962 void (*cleanup) (struct inferior *, void *);
963};
964
965struct inferior_data_registration
966{
967 struct inferior_data *data;
968 struct inferior_data_registration *next;
969};
970
971struct inferior_data_registry
972{
973 struct inferior_data_registration *registrations;
974 unsigned num_registrations;
975};
976
977static struct inferior_data_registry inferior_data_registry
978 = { NULL, 0 };
979
980const struct inferior_data *
981register_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
999const struct inferior_data *
1000register_inferior_data (void)
1001{
1002 return register_inferior_data_with_cleanup (NULL);
1003}
1004
1005static void
1006inferior_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
1013static void
1014inferior_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
b77209e0 1022void
6c95b8df 1023clear_inferior_data (struct inferior *inf)
b77209e0 1024{
6c95b8df
PA
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
1039void
1040set_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
1048void *
1049inferior_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
1055void
1056initialize_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
c82c0b55
MS
1068 add_info ("inferiors", info_inferiors_command,
1069 _("IDs of specified inferiors (all inferiors if no argument)."));
b77209e0 1070
6c95b8df
PA
1071 add_com ("add-inferior", no_class, add_inferior_command, _("\
1072Add a new inferior.\n\
1073Usage: add-inferior [-copies <N>] [-exec <FILENAME>]\n\
af624141 1074N is the optional number of inferiors to add, default is 1.\n\
6c95b8df
PA
1075FILENAME is the file name of the executable to use\n\
1076as main program."));
1077
af624141
MS
1078 add_com ("remove-inferiors", no_class, remove_inferior_command, _("\
1079Remove inferior ID (or list of IDs).\n\
1080Usage: remove-inferiors ID..."));
6c95b8df
PA
1081
1082 add_com ("clone-inferior", no_class, clone_inferior_command, _("\
1083Clone inferior ID.\n\
1084Usage: clone-inferior [-copies <N>] [ID]\n\
1085Add N copies of inferior ID. The new inferior has the same\n\
1086executable loaded as the copied inferior. If -copies is not specified,\n\
1087adds 1 copy. If ID is not specified, it is the current inferior\n\
1088that is cloned."));
2277426b 1089
af624141
MS
1090 add_cmd ("inferiors", class_run, detach_inferior_command, _("\
1091Detach from inferior ID (or list of IDS)."),
2277426b
PA
1092 &detachlist);
1093
af624141
MS
1094 add_cmd ("inferiors", class_run, kill_inferior_command, _("\
1095Kill inferior ID (or list of IDs)."),
2277426b
PA
1096 &killlist);
1097
1098 add_cmd ("inferior", class_run, inferior_command, _("\
1099Use this command to switch between inferiors.\n\
1100The new inferior ID must be currently known."),
1101 &cmdlist);
6c95b8df
PA
1102
1103 add_setshow_boolean_cmd ("inferior-events", no_class,
1104 &print_inferior_events, _("\
1105Set printing of inferior events (e.g., inferior start and exit)."), _("\
1106Show printing of inferior events (e.g., inferior start and exit)."), NULL,
1107 NULL,
1108 show_print_inferior_events,
1109 &setprintlist, &showprintlist);
1110
b77209e0 1111}
This page took 0.314502 seconds and 4 git commands to generate.