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