OSDN Git Service

gdb/
[pf3gnuchains/pf3gnuchains3x.git] / gdb / inferior.c
1 /* Multi-process control for GDB, the GNU debugger.
2
3    Copyright (C) 2008, 2009 Free Software Foundation, Inc.
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"
21 #include "inferior.h"
22 #include "target.h"
23 #include "command.h"
24 #include "gdbcmd.h"
25 #include "gdbthread.h"
26 #include "ui-out.h"
27 #include "observer.h"
28 #include "gdbthread.h"
29
30 void _initialize_inferiors (void);
31
32 static struct inferior *inferior_list = NULL;
33 static int highest_inferior_num;
34
35 /* Print notices on inferior events (attach, detach, etc.), set with
36    `set print inferior-events'.  */
37 static int print_inferior_events = 0;
38
39 struct inferior*
40 current_inferior (void)
41 {
42   struct inferior *inf = find_inferior_pid (ptid_get_pid (inferior_ptid));
43   gdb_assert (inf);
44   return inf;
45 }
46
47 static void
48 free_inferior (struct inferior *inf)
49 {
50   discard_all_inferior_continuations (inf);
51   xfree (inf->private);
52   xfree (inf);
53 }
54
55 void
56 init_inferior_list (void)
57 {
58   struct inferior *inf, *infnext;
59
60   highest_inferior_num = 0;
61   if (!inferior_list)
62     return;
63
64   for (inf = inferior_list; inf; inf = infnext)
65     {
66       infnext = inf->next;
67       free_inferior (inf);
68     }
69
70   inferior_list = NULL;
71 }
72
73 struct inferior *
74 add_inferior_silent (int pid)
75 {
76   struct inferior *inf;
77
78   inf = xmalloc (sizeof (*inf));
79   memset (inf, 0, sizeof (*inf));
80   inf->pid = pid;
81
82   inf->stop_soon = NO_STOP_QUIETLY;
83
84   inf->num = ++highest_inferior_num;
85   inf->next = inferior_list;
86   inferior_list = inf;
87
88   observer_notify_new_inferior (pid);
89
90   return inf;
91 }
92
93 struct inferior *
94 add_inferior (int pid)
95 {
96   struct inferior *inf = add_inferior_silent (pid);
97
98   if (print_inferior_events)
99     printf_unfiltered (_("[New inferior %d]\n"), pid);
100
101   return inf;
102 }
103
104 struct delete_thread_of_inferior_arg
105 {
106   int pid;
107   int silent;
108 };
109
110 static int
111 delete_thread_of_inferior (struct thread_info *tp, void *data)
112 {
113   struct delete_thread_of_inferior_arg *arg = data;
114
115   if (ptid_get_pid (tp->ptid) == arg->pid)
116     {
117       if (arg->silent)
118         delete_thread_silent (tp->ptid);
119       else
120         delete_thread (tp->ptid);
121     }
122
123   return 0;
124 }
125
126 /* If SILENT then be quiet -- don't announce a inferior death, or the
127    exit of its threads.  */
128 static void
129 delete_inferior_1 (int pid, int silent)
130 {
131   struct inferior *inf, *infprev;
132   struct delete_thread_of_inferior_arg arg = { pid, silent };
133
134   infprev = NULL;
135
136   for (inf = inferior_list; inf; infprev = inf, inf = inf->next)
137     if (inf->pid == pid)
138       break;
139
140   if (!inf)
141     return;
142
143   arg.pid = pid;
144   arg.silent = silent;
145
146   iterate_over_threads (delete_thread_of_inferior, &arg);
147
148   /* Notify the observers before removing the inferior from the list,
149      so that the observers have a change to look it up.  */
150   observer_notify_inferior_exit (pid);
151
152   if (infprev)
153     infprev->next = inf->next;
154   else
155     inferior_list = inf->next;
156
157   free_inferior (inf);
158 }
159
160 void
161 delete_inferior (int pid)
162 {
163   delete_inferior_1 (pid, 0);
164
165   if (print_inferior_events)
166     printf_unfiltered (_("[Inferior %d exited]\n"), pid);
167 }
168
169 void
170 delete_inferior_silent (int pid)
171 {
172   delete_inferior_1 (pid, 1);
173 }
174
175 void
176 detach_inferior (int pid)
177 {
178   delete_inferior_1 (pid, 1);
179
180   if (print_inferior_events)
181     printf_unfiltered (_("[Inferior %d detached]\n"), pid);
182 }
183
184 void
185 discard_all_inferiors (void)
186 {
187   struct inferior *inf, *infnext;
188
189   for (inf = inferior_list; inf; inf = infnext)
190     {
191       infnext = inf->next;
192       delete_inferior_silent (inf->pid);
193     }
194 }
195
196 static struct inferior *
197 find_inferior_id (int num)
198 {
199   struct inferior *inf;
200
201   for (inf = inferior_list; inf; inf = inf->next)
202     if (inf->num == num)
203       return inf;
204
205   return NULL;
206 }
207
208 struct inferior *
209 find_inferior_pid (int pid)
210 {
211   struct inferior *inf;
212
213   for (inf = inferior_list; inf; inf = inf->next)
214     if (inf->pid == pid)
215       return inf;
216
217   return NULL;
218 }
219
220 struct inferior *
221 iterate_over_inferiors (int (*callback) (struct inferior *, void *),
222                         void *data)
223 {
224   struct inferior *inf, *infnext;
225
226   for (inf = inferior_list; inf; inf = infnext)
227     {
228       infnext = inf->next;
229       if ((*callback) (inf, data))
230         return inf;
231     }
232
233   return NULL;
234 }
235
236 int
237 valid_gdb_inferior_id (int num)
238 {
239   struct inferior *inf;
240
241   for (inf = inferior_list; inf; inf = inf->next)
242     if (inf->num == num)
243       return 1;
244
245   return 0;
246 }
247
248 int
249 pid_to_gdb_inferior_id (int pid)
250 {
251   struct inferior *inf;
252
253   for (inf = inferior_list; inf; inf = inf->next)
254     if (inf->pid == pid)
255       return inf->num;
256
257   return 0;
258 }
259
260 int
261 gdb_inferior_id_to_pid (int num)
262 {
263   struct inferior *inferior = find_inferior_id (num);
264   if (inferior)
265     return inferior->pid;
266   else
267     return -1;
268 }
269
270 int
271 in_inferior_list (int pid)
272 {
273   struct inferior *inf;
274
275   for (inf = inferior_list; inf; inf = inf->next)
276     if (inf->pid == pid)
277       return 1;
278
279   return 0;
280 }
281
282 int
283 have_inferiors (void)
284 {
285   return inferior_list != NULL;
286 }
287
288 int
289 have_live_inferiors (void)
290 {
291   /* The check on stratum suffices, as GDB doesn't currently support
292      multiple target interfaces.  */
293   return (current_target.to_stratum >= process_stratum && have_inferiors ());
294 }
295
296 /* Prints the list of inferiors and their details on UIOUT.  This is a
297    version of 'info_inferior_command' suitable for use from MI.
298
299    If REQUESTED_INFERIOR is not -1, it's the GDB id of the inferior that
300    should be printed.  Otherwise, all inferiors are printed.  */
301 void
302 print_inferior (struct ui_out *uiout, int requested_inferior)
303 {
304   struct inferior *inf;
305   struct cleanup *old_chain;
306   int inf_count = 0;
307
308   /* Compute number of inferiors we will print.  */
309   for (inf = inferior_list; inf; inf = inf->next)
310     {
311       struct cleanup *chain2;
312
313       if (requested_inferior != -1 && inf->num != requested_inferior)
314         continue;
315
316       ++inf_count;
317     }
318
319   if (inf_count == 0)
320     {
321       ui_out_message (uiout, 0, "No inferiors.\n");
322       return;
323     }
324
325   old_chain = make_cleanup_ui_out_table_begin_end (uiout, 3, inf_count,
326                                                    "inferiors");
327   ui_out_table_header (uiout, 3, ui_right, "current", "Cur");
328   ui_out_table_header (uiout, 4, ui_right, "id", "Id");
329   ui_out_table_header (uiout, 7, ui_right, "target-id", "PID");
330   ui_out_table_body (uiout);
331
332   for (inf = inferior_list; inf; inf = inf->next)
333     {
334       struct cleanup *chain2;
335
336       if (requested_inferior != -1 && inf->num != requested_inferior)
337         continue;
338
339       chain2 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
340
341       if (inf->pid == ptid_get_pid (inferior_ptid))
342         ui_out_field_string (uiout, "current", "*");
343       else
344         ui_out_field_skip (uiout, "current");
345
346       ui_out_field_int (uiout, "id", inf->num);
347       ui_out_field_int (uiout, "target-id", inf->pid);
348
349       ui_out_text (uiout, "\n");
350       do_cleanups (chain2);
351     }
352
353   do_cleanups (old_chain);
354 }
355
356 static void
357 detach_inferior_command (char *args, int from_tty)
358 {
359   int num, pid;
360   struct thread_info *tp;
361
362   if (!args || !*args)
363     error (_("Requires argument (inferior id to detach)"));
364
365   num = parse_and_eval_long (args);
366
367   if (!valid_gdb_inferior_id (num))
368     error (_("Inferior ID %d not known."), num);
369
370   pid = gdb_inferior_id_to_pid (num);
371
372   tp = any_thread_of_process (pid);
373   if (!tp)
374     error (_("Inferior has no threads."));
375
376   switch_to_thread (tp->ptid);
377
378   detach_command (NULL, from_tty);
379 }
380
381 static void
382 kill_inferior_command (char *args, int from_tty)
383 {
384   int num, pid;
385   struct thread_info *tp;
386
387   if (!args || !*args)
388     error (_("Requires argument (inferior id to kill)"));
389
390   num = parse_and_eval_long (args);
391
392   if (!valid_gdb_inferior_id (num))
393     error (_("Inferior ID %d not known."), num);
394
395   pid = gdb_inferior_id_to_pid (num);
396
397   tp = any_thread_of_process (pid);
398   if (!tp)
399     error (_("Inferior has no threads."));
400
401   switch_to_thread (tp->ptid);
402
403   target_kill ();
404
405   bfd_cache_close_all ();
406 }
407
408 static void
409 inferior_command (char *args, int from_tty)
410 {
411   int num, pid;
412
413   if (!have_inferiors ())
414     error (_("No inferiors"));
415
416   num = parse_and_eval_long (args);
417
418   if (!valid_gdb_inferior_id (num))
419     error (_("Inferior ID %d not known."), num);
420
421   pid = gdb_inferior_id_to_pid (num);
422
423   if (pid != ptid_get_pid (inferior_ptid))
424     {
425       struct thread_info *tp;
426
427       tp = any_thread_of_process (pid);
428       if (!tp)
429         error (_("Inferior has no threads."));
430
431       switch_to_thread (tp->ptid);
432     }
433
434   printf_filtered (_("[Switching to thread %d (%s)] "),
435                    pid_to_thread_id (inferior_ptid),
436                    target_pid_to_str (inferior_ptid));
437
438   if (is_running (inferior_ptid))
439     ui_out_text (uiout, "(running)\n");
440   else
441     {
442       ui_out_text (uiout, "\n");
443       print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
444     }
445 }
446
447 /* Print information about currently known inferiors.  */
448
449 static void
450 info_inferiors_command (char *args, int from_tty)
451 {
452   int requested = -1;
453
454   if (args && *args)
455     {
456       requested = parse_and_eval_long (args);
457       if (!valid_gdb_inferior_id (requested))
458         error (_("Inferior ID %d not known."), requested);
459     }
460
461   print_inferior (uiout, requested);
462 }
463
464 /* Print notices when new inferiors are created and die.  */
465 static void
466 show_print_inferior_events (struct ui_file *file, int from_tty,
467                            struct cmd_list_element *c, const char *value)
468 {
469   fprintf_filtered (file, _("Printing of inferior events is %s.\n"), value);
470 }
471
472 void
473 _initialize_inferiors (void)
474 {
475   add_info ("inferiors", info_inferiors_command,
476             _("IDs of currently known inferiors."));
477
478   add_setshow_boolean_cmd ("inferior-events", no_class,
479          &print_inferior_events, _("\
480 Set printing of inferior events (e.g., inferior start and exit)."), _("\
481 Show printing of inferior events (e.g., inferior start and exit)."), NULL,
482          NULL,
483          show_print_inferior_events,
484          &setprintlist, &showprintlist);
485
486   add_cmd ("inferior", class_run, detach_inferior_command, _("\
487 Detach from inferior ID."),
488            &detachlist);
489
490   add_cmd ("inferior", class_run, kill_inferior_command, _("\
491 Kill inferior ID."),
492            &killlist);
493
494   add_cmd ("inferior", class_run, inferior_command, _("\
495 Use this command to switch between inferiors.\n\
496 The new inferior ID must be currently known."),
497            &cmdlist);
498 }