2010-05-17 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / mi / mi-interp.c
CommitLineData
4a8f6654
AC
1/* MI Interpreter Definitions and Commands for GDB, the GNU debugger.
2
4c38e0a4 3 Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
9b254dd1 4 Free Software Foundation, Inc.
4a8f6654
AC
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
4a8f6654
AC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4a8f6654
AC
20
21#include "defs.h"
22#include "gdb_string.h"
23#include "interps.h"
24#include "event-top.h"
25#include "event-loop.h"
26#include "inferior.h"
27#include "ui-out.h"
28#include "top.h"
c1043fc2 29#include "exceptions.h"
4a8f6654
AC
30#include "mi-main.h"
31#include "mi-cmds.h"
32#include "mi-out.h"
33#include "mi-console.h"
66bb093b 34#include "mi-common.h"
683f2885
VP
35#include "observer.h"
36#include "gdbthread.h"
c86cf029 37#include "solist.h"
4a8f6654 38
4a8f6654
AC
39/* These are the interpreter setup, etc. functions for the MI interpreter */
40static void mi_execute_command_wrapper (char *cmd);
41static void mi_command_loop (int mi_version);
4a8f6654
AC
42
43/* These are hooks that we put in place while doing interpreter_exec
44 so we can report interesting things that happened "behind the mi's
45 back" in this command */
bee0189a 46static int mi_interp_query_hook (const char *ctlstr, va_list ap)
a0b31db1 47 ATTRIBUTE_PRINTF (1, 0);
4a8f6654 48
f786f615 49static void mi3_command_loop (void);
4a8f6654
AC
50static void mi2_command_loop (void);
51static void mi1_command_loop (void);
52
53static void mi_insert_notify_hooks (void);
54static void mi_remove_notify_hooks (void);
1d33d6ba 55static void mi_on_normal_stop (struct bpstats *bs, int print_frame);
4a8f6654 56
683f2885 57static void mi_new_thread (struct thread_info *t);
a07daef3 58static void mi_thread_exit (struct thread_info *t, int silent);
a79b8f6e
VP
59static void mi_inferior_added (struct inferior *inf);
60static void mi_inferior_appeared (struct inferior *inf);
61static void mi_inferior_exit (struct inferior *inf);
62static void mi_inferior_removed (struct inferior *inf);
e1ac3328 63static void mi_on_resume (ptid_t ptid);
c86cf029
VP
64static void mi_solib_loaded (struct so_list *solib);
65static void mi_solib_unloaded (struct so_list *solib);
f3b1572e 66static void mi_about_to_proceed (void);
683f2885 67
a79b8f6e
VP
68static int report_initial_inferior (struct inferior *inf, void *closure);
69
4a8f6654 70static void *
683f2885 71mi_interpreter_init (int top_level)
4a8f6654
AC
72{
73 struct mi_interp *mi = XMALLOC (struct mi_interp);
74
4a8f6654
AC
75 /* HACK: We need to force stdout/stderr to point at the console. This avoids
76 any potential side effects caused by legacy code that is still
77 using the TUI / fputs_unfiltered_hook. So we set up output channels for
78 this now, and swap them in when we are run. */
79
80 raw_stdout = stdio_fileopen (stdout);
81
82 /* Create MI channels */
83 mi->out = mi_console_file_new (raw_stdout, "~", '"');
84 mi->err = mi_console_file_new (raw_stdout, "&", '"');
85 mi->log = mi->err;
86 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
87 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
88
683f2885 89 if (top_level)
063bfe2e
VP
90 {
91 observer_attach_new_thread (mi_new_thread);
92 observer_attach_thread_exit (mi_thread_exit);
a79b8f6e 93 observer_attach_inferior_added (mi_inferior_added);
6c95b8df 94 observer_attach_inferior_appeared (mi_inferior_appeared);
4a92f99b 95 observer_attach_inferior_exit (mi_inferior_exit);
a79b8f6e 96 observer_attach_inferior_removed (mi_inferior_removed);
f7f9a841 97 observer_attach_normal_stop (mi_on_normal_stop);
e1ac3328 98 observer_attach_target_resumed (mi_on_resume);
c86cf029
VP
99 observer_attach_solib_loaded (mi_solib_loaded);
100 observer_attach_solib_unloaded (mi_solib_unloaded);
f3b1572e 101 observer_attach_about_to_proceed (mi_about_to_proceed);
a79b8f6e
VP
102
103 /* The initial inferior is created before this function is called, so we
104 need to report it explicitly. Use iteration in case future version
105 of GDB creates more than one inferior up-front. */
106 iterate_over_inferiors (report_initial_inferior, mi);
063bfe2e 107 }
683f2885 108
4a8f6654
AC
109 return mi;
110}
111
112static int
113mi_interpreter_resume (void *data)
114{
115 struct mi_interp *mi = data;
116 /* As per hack note in mi_interpreter_init, swap in the output channels... */
117
118 gdb_setup_readline ();
119
362646f5
AC
120 /* These overwrite some of the initialization done in
121 _intialize_event_loop. */
122 call_readline = gdb_readline2;
123 input_handler = mi_execute_command_wrapper;
124 add_file_handler (input_fd, stdin_event_handler, 0);
125 async_command_editing_p = 0;
126 /* FIXME: This is a total hack for now. PB's use of the MI
127 implicitly relies on a bug in the async support which allows
128 asynchronous commands to leak through the commmand loop. The bug
129 involves (but is not limited to) the fact that sync_execution was
130 erroneously initialized to 0. Duplicate by initializing it thus
131 here... */
132 sync_execution = 0;
4a8f6654
AC
133
134 gdb_stdout = mi->out;
135 /* Route error and log output through the MI */
136 gdb_stderr = mi->err;
137 gdb_stdlog = mi->log;
138 /* Route target output through the MI. */
139 gdb_stdtarg = mi->targ;
1f20321b
FR
140 /* Route target error through the MI as well. */
141 gdb_stdtargerr = mi->targ;
4a8f6654
AC
142
143 /* Replace all the hooks that we know about. There really needs to
144 be a better way of doing this... */
145 clear_interpreter_hooks ();
146
9a4105ab 147 deprecated_show_load_progress = mi_load_progress;
4a8f6654
AC
148
149 /* If we're _the_ interpreter, take control. */
150 if (current_interp_named_p (INTERP_MI1))
9a4105ab 151 deprecated_command_loop_hook = mi1_command_loop;
f786f615 152 else if (current_interp_named_p (INTERP_MI2))
9a4105ab 153 deprecated_command_loop_hook = mi2_command_loop;
f786f615 154 else if (current_interp_named_p (INTERP_MI3))
9a4105ab 155 deprecated_command_loop_hook = mi3_command_loop;
4a8f6654 156 else
9a4105ab 157 deprecated_command_loop_hook = mi2_command_loop;
4a8f6654
AC
158
159 return 1;
160}
161
162static int
163mi_interpreter_suspend (void *data)
164{
165 gdb_disable_readline ();
166 return 1;
167}
168
71fff37b 169static struct gdb_exception
4a8f6654
AC
170mi_interpreter_exec (void *data, const char *command)
171{
172 char *tmp = alloca (strlen (command) + 1);
173 strcpy (tmp, command);
174 mi_execute_command_wrapper (tmp);
c1043fc2 175 return exception_none;
4a8f6654
AC
176}
177
178/* Never display the default gdb prompt in mi case. */
179static int
180mi_interpreter_prompt_p (void *data)
181{
182 return 0;
183}
184
ce8f13f8 185void
4a8f6654
AC
186mi_cmd_interpreter_exec (char *command, char **argv, int argc)
187{
188 struct interp *interp_to_use;
4a8f6654 189 int i;
a13e061a
PA
190 char *mi_error_message = NULL;
191 struct cleanup *old_chain;
4a8f6654
AC
192
193 if (argc < 2)
a13e061a 194 error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
4a8f6654
AC
195
196 interp_to_use = interp_lookup (argv[0]);
197 if (interp_to_use == NULL)
a13e061a 198 error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
4a8f6654
AC
199
200 if (!interp_exec_p (interp_to_use))
a13e061a
PA
201 error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
202 argv[0]);
4a8f6654
AC
203
204 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
205 if it has any. */
206 /* KRS: We shouldn't need this... Events should be installed and they should
207 just ALWAYS fire something out down the MI channel... */
208 mi_insert_notify_hooks ();
209
210 /* Now run the code... */
211
a13e061a 212 old_chain = make_cleanup (null_cleanup, 0);
4a8f6654
AC
213 for (i = 1; i < argc; i++)
214 {
32c1e744
VP
215 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
216 if (e.reason < 0)
217 {
218 mi_error_message = xstrdup (e.message);
a13e061a 219 make_cleanup (xfree, mi_error_message);
32c1e744
VP
220 break;
221 }
4a8f6654
AC
222 }
223
224 mi_remove_notify_hooks ();
225
a13e061a
PA
226 if (mi_error_message != NULL)
227 error ("%s", mi_error_message);
228 do_cleanups (old_chain);
4a8f6654
AC
229}
230
231/*
232 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
233 * async-notify ("=") MI messages while running commands in another interpreter
234 * using mi_interpreter_exec. The canonical use for this is to allow access to
235 * the gdb CLI interpreter from within the MI, while still producing MI style output
236 * when actions in the CLI command change gdb's state.
237*/
238
239static void
240mi_insert_notify_hooks (void)
241{
9a4105ab 242 deprecated_query_hook = mi_interp_query_hook;
4a8f6654
AC
243}
244
245static void
11308a41 246mi_remove_notify_hooks (void)
4a8f6654 247{
9a4105ab 248 deprecated_query_hook = NULL;
4a8f6654
AC
249}
250
251static int
252mi_interp_query_hook (const char *ctlstr, va_list ap)
253{
254 return 1;
255}
256
4a8f6654
AC
257static void
258mi_execute_command_wrapper (char *cmd)
259{
260 mi_execute_command (cmd, stdin == instream);
261}
262
263static void
264mi1_command_loop (void)
265{
266 mi_command_loop (1);
267}
268
269static void
270mi2_command_loop (void)
271{
272 mi_command_loop (2);
273}
274
f786f615
AC
275static void
276mi3_command_loop (void)
277{
278 mi_command_loop (3);
279}
280
4a8f6654
AC
281static void
282mi_command_loop (int mi_version)
283{
4a8f6654
AC
284 /* Turn off 8 bit strings in quoted output. Any character with the
285 high bit set is printed using C's octal format. */
286 sevenbit_strings = 1;
287 /* Tell the world that we're alive */
288 fputs_unfiltered ("(gdb) \n", raw_stdout);
289 gdb_flush (raw_stdout);
362646f5 290 start_event_loop ();
4a8f6654
AC
291}
292
683f2885
VP
293static void
294mi_new_thread (struct thread_info *t)
295{
296 struct mi_interp *mi = top_level_interpreter_data ();
a79b8f6e
VP
297 struct inferior *inf = find_inferior_pid (ptid_get_pid (t->ptid));
298
299 gdb_assert (inf);
683f2885 300
3d043ef6 301 fprintf_unfiltered (mi->event_channel,
a79b8f6e
VP
302 "thread-created,id=\"%d\",group-id=\"i%d\"",
303 t->num, inf->num);
683f2885
VP
304 gdb_flush (mi->event_channel);
305}
306
063bfe2e 307static void
a07daef3 308mi_thread_exit (struct thread_info *t, int silent)
063bfe2e 309{
a07daef3 310 struct mi_interp *mi;
a79b8f6e 311 struct inferior *inf;
a07daef3
PA
312
313 if (silent)
314 return;
315
a79b8f6e
VP
316 inf = find_inferior_pid (ptid_get_pid (t->ptid));
317
a07daef3 318 mi = top_level_interpreter_data ();
063bfe2e 319 target_terminal_ours ();
3d043ef6 320 fprintf_unfiltered (mi->event_channel,
a79b8f6e
VP
321 "thread-exited,id=\"%d\",group-id=\"i%d\"",
322 t->num, inf->num);
063bfe2e
VP
323 gdb_flush (mi->event_channel);
324}
325
a79b8f6e
VP
326static void
327mi_inferior_added (struct inferior *inf)
328{
329 struct mi_interp *mi = top_level_interpreter_data ();
330 target_terminal_ours ();
331 fprintf_unfiltered (mi->event_channel,
332 "thread-group-added,id=\"i%d\"",
333 inf->num);
334 gdb_flush (mi->event_channel);
335}
336
337static void
338mi_inferior_appeared (struct inferior *inf)
4a92f99b
VP
339{
340 struct mi_interp *mi = top_level_interpreter_data ();
341 target_terminal_ours ();
a79b8f6e
VP
342 fprintf_unfiltered (mi->event_channel,
343 "thread-group-started,id=\"i%d\",pid=\"%d\"",
344 inf->num, inf->pid);
4a92f99b
VP
345 gdb_flush (mi->event_channel);
346}
347
348static void
a79b8f6e 349mi_inferior_exit (struct inferior *inf)
4a92f99b
VP
350{
351 struct mi_interp *mi = top_level_interpreter_data ();
352 target_terminal_ours ();
a79b8f6e
VP
353 fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"i%d\"",
354 inf->num);
4a92f99b
VP
355 gdb_flush (mi->event_channel);
356}
357
a79b8f6e
VP
358static void
359mi_inferior_removed (struct inferior *inf)
360{
361 struct mi_interp *mi = top_level_interpreter_data ();
362 target_terminal_ours ();
363 fprintf_unfiltered (mi->event_channel,
364 "thread-group-removed,id=\"i%d\"",
365 inf->num);
366 gdb_flush (mi->event_channel);
367}
368
f7f9a841 369static void
1d33d6ba 370mi_on_normal_stop (struct bpstats *bs, int print_frame)
f7f9a841
VP
371{
372 /* Since this can be called when CLI command is executing,
373 using cli interpreter, be sure to use MI uiout for output,
374 not the current one. */
1d33d6ba 375 struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
f7f9a841 376
1d33d6ba
VP
377 if (print_frame)
378 {
dc146f7c 379 int core;
1d33d6ba
VP
380 if (uiout != mi_uiout)
381 {
382 /* The normal_stop function has printed frame information into
383 CLI uiout, or some other non-MI uiout. There's no way we
384 can extract proper fields from random uiout object, so we print
385 the frame again. In practice, this can only happen when running
386 a CLI command in MI. */
387 struct ui_out *saved_uiout = uiout;
388 uiout = mi_uiout;
389 print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC);
390 uiout = saved_uiout;
391 }
392
393 ui_out_field_int (mi_uiout, "thread-id",
394 pid_to_thread_id (inferior_ptid));
395 if (non_stop)
396 {
397 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
398 (mi_uiout, "stopped-threads");
399 ui_out_field_int (mi_uiout, NULL,
400 pid_to_thread_id (inferior_ptid));
401 do_cleanups (back_to);
402 }
403 else
404 ui_out_field_string (mi_uiout, "stopped-threads", "all");
dc146f7c
VP
405
406 core = target_core_of_thread (inferior_ptid);
407 if (core != -1)
408 ui_out_field_int (mi_uiout, "core", core);
1d33d6ba
VP
409 }
410
f7f9a841 411 fputs_unfiltered ("*stopped", raw_stdout);
1d33d6ba
VP
412 mi_out_put (mi_uiout, raw_stdout);
413 mi_out_rewind (mi_uiout);
4333ada3 414 mi_print_timing_maybe ();
f7f9a841
VP
415 fputs_unfiltered ("\n", raw_stdout);
416 gdb_flush (raw_stdout);
417}
418
f3b1572e
PA
419static void
420mi_about_to_proceed (void)
421{
422 /* Suppress output while calling an inferior function. */
423
424 if (!ptid_equal (inferior_ptid, null_ptid))
425 {
426 struct thread_info *tp = inferior_thread ();
427 if (tp->in_infcall)
428 return;
429 }
430
431 mi_proceeded = 1;
432}
433
d90e17a7
PA
434static int
435mi_output_running_pid (struct thread_info *info, void *arg)
436{
437 ptid_t *ptid = arg;
438
439 if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
440 fprintf_unfiltered (raw_stdout,
441 "*running,thread-id=\"%d\"\n",
442 info->num);
443
444 return 0;
445}
446
447static int
448mi_inferior_count (struct inferior *inf, void *arg)
449{
450 if (inf->pid != 0)
451 {
452 int *count_p = arg;
453 (*count_p)++;
454 }
455
456 return 0;
457}
458
e1ac3328
VP
459static void
460mi_on_resume (ptid_t ptid)
461{
c5a4d20b
PA
462 struct thread_info *tp = NULL;
463
9944e9c2 464 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
c5a4d20b
PA
465 tp = inferior_thread ();
466 else
e09875d4 467 tp = find_thread_ptid (ptid);
c5a4d20b
PA
468
469 /* Suppress output while calling an inferior function. */
470 if (tp->in_infcall)
471 return;
472
a2840c35
VP
473 /* To cater for older frontends, emit ^running, but do it only once
474 per each command. We do it here, since at this point we know
475 that the target was successfully resumed, and in non-async mode,
476 we won't return back to MI interpreter code until the target
477 is done running, so delaying the output of "^running" until then
478 will make it impossible for frontend to know what's going on.
479
480 In future (MI3), we'll be outputting "^done" here. */
f3b1572e 481 if (!running_result_record_printed && mi_proceeded)
a2840c35 482 {
c271b6e2
VP
483 fprintf_unfiltered (raw_stdout, "%s^running\n",
484 current_token ? current_token : "");
a2840c35
VP
485 }
486
e1ac3328
VP
487 if (PIDGET (ptid) == -1)
488 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
d90e17a7 489 else if (ptid_is_pid (ptid))
bb599c81 490 {
ab730e72 491 int count = 0;
d90e17a7
PA
492
493 /* Backwards compatibility. If there's only one inferior,
494 output "all", otherwise, output each resumed thread
495 individually. */
496 iterate_over_inferiors (mi_inferior_count, &count);
497
498 if (count == 1)
499 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
500 else
501 iterate_over_threads (mi_output_running_pid, &ptid);
bb599c81 502 }
e1ac3328
VP
503 else
504 {
e09875d4 505 struct thread_info *ti = find_thread_ptid (ptid);
e1ac3328
VP
506 gdb_assert (ti);
507 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
508 }
a2840c35 509
f3b1572e 510 if (!running_result_record_printed && mi_proceeded)
a2840c35
VP
511 {
512 running_result_record_printed = 1;
513 /* This is what gdb used to do historically -- printing prompt even if
514 it cannot actually accept any input. This will be surely removed
515 for MI3, and may be removed even earler. */
516 /* FIXME: review the use of target_is_async_p here -- is that
517 what we want? */
518 if (!target_is_async_p ())
519 fputs_unfiltered ("(gdb) \n", raw_stdout);
520 }
c1828f25 521 gdb_flush (raw_stdout);
e1ac3328
VP
522}
523
c86cf029
VP
524static void
525mi_solib_loaded (struct so_list *solib)
526{
527 struct mi_interp *mi = top_level_interpreter_data ();
528 target_terminal_ours ();
a79b8f6e
VP
529 if (gdbarch_has_global_solist (target_gdbarch))
530 fprintf_unfiltered (mi->event_channel,
531 "library-loaded,id=\"%s\",target-name=\"%s\","
532 "host-name=\"%s\",symbols-loaded=\"%d\"",
533 solib->so_original_name, solib->so_original_name,
534 solib->so_name, solib->symbols_loaded);
535 else
536 fprintf_unfiltered (mi->event_channel,
537 "library-loaded,id=\"%s\",target-name=\"%s\","
538 "host-name=\"%s\",symbols-loaded=\"%d\","
539 "thread-group=\"i%d\"",
540 solib->so_original_name, solib->so_original_name,
541 solib->so_name, solib->symbols_loaded,
542 current_inferior ()->num);
543
c86cf029
VP
544 gdb_flush (mi->event_channel);
545}
546
547static void
548mi_solib_unloaded (struct so_list *solib)
549{
550 struct mi_interp *mi = top_level_interpreter_data ();
551 target_terminal_ours ();
a79b8f6e
VP
552 if (gdbarch_has_global_solist (target_gdbarch))
553 fprintf_unfiltered (mi->event_channel,
554 "library-unloaded,id=\"%s\",target-name=\"%s\","
555 "host-name=\"%s\"",
556 solib->so_original_name, solib->so_original_name,
557 solib->so_name);
558 else
559 fprintf_unfiltered (mi->event_channel,
560 "library-unloaded,id=\"%s\",target-name=\"%s\","
561 "host-name=\"%s\",thread-group=\"i%d\"",
562 solib->so_original_name, solib->so_original_name,
563 solib->so_name, current_inferior ()->num);
564
c86cf029
VP
565 gdb_flush (mi->event_channel);
566}
567
a79b8f6e
VP
568static int
569report_initial_inferior (struct inferior *inf, void *closure)
570{
571 /* This function is called from mi_intepreter_init, and since
572 mi_inferior_added assumes that inferior is fully initialized
573 and top_level_interpreter_data is set, we cannot call
574 it here. */
575 struct mi_interp *mi = closure;
576 target_terminal_ours ();
577 fprintf_unfiltered (mi->event_channel,
578 "thread-group-added,id=\"i%d\"",
579 inf->num);
580 gdb_flush (mi->event_channel);
581 return 0;
582}
c86cf029 583
b9362cc7
AC
584extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
585
4a8f6654
AC
586void
587_initialize_mi_interp (void)
588{
589 static const struct interp_procs procs =
590 {
591 mi_interpreter_init, /* init_proc */
592 mi_interpreter_resume, /* resume_proc */
593 mi_interpreter_suspend, /* suspend_proc */
594 mi_interpreter_exec, /* exec_proc */
595 mi_interpreter_prompt_p /* prompt_proc_p */
596 };
597
2fcf52f0 598 /* The various interpreter levels. */
4a8f6654 599 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
2fcf52f0
AC
600 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
601 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
4a8f6654 602
2fcf52f0
AC
603 /* "mi" selects the most recent released version. "mi2" was
604 released as part of GDB 6.0. */
605 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
4a8f6654 606}
This page took 0.618235 seconds and 4 git commands to generate.