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