1 /* Multi-process control for GDB, the GNU debugger.
3 Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
25 #include "gdbthread.h"
29 void _initialize_inferiors (void);
31 static struct inferior
*inferior_list
= NULL
;
32 static int highest_inferior_num
;
34 /* Print notices on inferior events (attach, detach, etc.), set with
35 `set print inferior-events'. */
36 static int print_inferior_events
= 0;
39 current_inferior (void)
41 struct inferior
*inf
= find_inferior_pid (ptid_get_pid (inferior_ptid
));
47 free_inferior (struct inferior
*inf
)
49 discard_all_inferior_continuations (inf
);
55 init_inferior_list (void)
57 struct inferior
*inf
, *infnext
;
59 highest_inferior_num
= 0;
63 for (inf
= inferior_list
; inf
; inf
= infnext
)
73 add_inferior_silent (int pid
)
77 inf
= xmalloc (sizeof (*inf
));
78 memset (inf
, 0, sizeof (*inf
));
81 inf
->stop_soon
= NO_STOP_QUIETLY
;
83 inf
->num
= ++highest_inferior_num
;
84 inf
->next
= inferior_list
;
91 add_inferior (int pid
)
93 struct inferior
*inf
= add_inferior_silent (pid
);
95 observer_notify_new_inferior (pid
);
97 if (print_inferior_events
)
98 printf_unfiltered (_("[New inferior %d]\n"), pid
);
103 struct delete_thread_of_inferior_arg
110 delete_thread_of_inferior (struct thread_info
*tp
, void *data
)
112 struct delete_thread_of_inferior_arg
*arg
= data
;
114 if (ptid_get_pid (tp
->ptid
) == arg
->pid
)
117 delete_thread_silent (tp
->ptid
);
119 delete_thread (tp
->ptid
);
125 /* If SILENT then be quiet -- don't announce a inferior death, or the
126 exit of its threads. */
128 delete_inferior_1 (int pid
, int silent
)
130 struct inferior
*inf
, *infprev
;
131 struct delete_thread_of_inferior_arg arg
= { pid
, silent
};
135 for (inf
= inferior_list
; inf
; infprev
= inf
, inf
= inf
->next
)
143 infprev
->next
= inf
->next
;
145 inferior_list
= inf
->next
;
152 iterate_over_threads (delete_thread_of_inferior
, &arg
);
154 observer_notify_inferior_exit (pid
);
158 delete_inferior (int pid
)
160 delete_inferior_1 (pid
, 0);
162 if (print_inferior_events
)
163 printf_unfiltered (_("[Inferior %d exited]\n"), pid
);
167 delete_inferior_silent (int pid
)
169 delete_inferior_1 (pid
, 1);
173 detach_inferior (int pid
)
175 delete_inferior_1 (pid
, 1);
177 if (print_inferior_events
)
178 printf_unfiltered (_("[Inferior %d detached]\n"), pid
);
182 discard_all_inferiors (void)
184 struct inferior
*inf
, *infnext
;
186 for (inf
= inferior_list
; inf
; inf
= infnext
)
189 delete_inferior_silent (inf
->pid
);
193 static struct inferior
*
194 find_inferior_id (int num
)
196 struct inferior
*inf
;
198 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
206 find_inferior_pid (int pid
)
208 struct inferior
*inf
;
210 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
218 iterate_over_inferiors (int (*callback
) (struct inferior
*, void *),
221 struct inferior
*inf
, *infnext
;
223 for (inf
= inferior_list
; inf
; inf
= infnext
)
226 if ((*callback
) (inf
, data
))
234 valid_gdb_inferior_id (int num
)
236 struct inferior
*inf
;
238 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
246 pid_to_gdb_inferior_id (int pid
)
248 struct inferior
*inf
;
250 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
258 gdb_inferior_id_to_pid (int num
)
260 struct inferior
*inferior
= find_inferior_id (num
);
262 return inferior
->pid
;
268 in_inferior_list (int pid
)
270 struct inferior
*inf
;
272 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
280 have_inferiors (void)
282 return inferior_list
!= NULL
;
285 /* Prints the list of inferiors and their details on UIOUT. This is a
286 version of 'info_inferior_command' suitable for use from MI.
288 If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
289 should be printed. Otherwise, all inferiors are printed. */
291 print_inferior (struct ui_out
*uiout
, int requested_inferior
)
293 struct inferior
*inf
;
294 struct cleanup
*old_chain
;
296 old_chain
= make_cleanup_ui_out_list_begin_end (uiout
, "inferiors");
298 for (inf
= inferior_list
; inf
; inf
= inf
->next
)
300 struct cleanup
*chain2
;
302 if (requested_inferior
!= -1 && inf
->num
!= requested_inferior
)
305 chain2
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
307 if (inf
->pid
== ptid_get_pid (inferior_ptid
))
308 ui_out_text (uiout
, "* ");
310 ui_out_text (uiout
, " ");
312 ui_out_field_int (uiout
, "id", inf
->num
);
313 ui_out_text (uiout
, " ");
314 ui_out_field_int (uiout
, "target-id", inf
->pid
);
316 ui_out_text (uiout
, "\n");
317 do_cleanups (chain2
);
320 do_cleanups (old_chain
);
323 /* Print information about currently known inferiors. */
326 info_inferiors_command (char *arg
, int from_tty
)
328 print_inferior (uiout
, -1);
331 /* Print notices when new inferiors are created and die. */
333 show_print_inferior_events (struct ui_file
*file
, int from_tty
,
334 struct cmd_list_element
*c
, const char *value
)
336 fprintf_filtered (file
, _("Printing of inferior events is %s.\n"), value
);
340 _initialize_inferiors (void)
342 add_info ("inferiors", info_inferiors_command
,
343 _("IDs of currently known inferiors."));
345 add_setshow_boolean_cmd ("inferior-events", no_class
,
346 &print_inferior_events
, _("\
347 Set printing of inferior events (e.g., inferior start and exit)."), _("\
348 Show printing of inferior events (e.g., inferior start and exit)."), NULL
,
350 show_print_inferior_events
,
351 &setprintlist
, &showprintlist
);