* target-reloc.h (visibility_error): New inline function.
[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
DJ
46static int mi_interp_query_hook (const char *ctlstr, va_list ap)
47 ATTR_FORMAT (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);
6c95b8df 59static void mi_inferior_appeared (int pid);
4a92f99b 60static void mi_inferior_exit (int pid);
e1ac3328 61static void mi_on_resume (ptid_t ptid);
c86cf029
VP
62static void mi_solib_loaded (struct so_list *solib);
63static void mi_solib_unloaded (struct so_list *solib);
f3b1572e 64static void mi_about_to_proceed (void);
683f2885 65
4a8f6654 66static void *
683f2885 67mi_interpreter_init (int top_level)
4a8f6654
AC
68{
69 struct mi_interp *mi = XMALLOC (struct mi_interp);
70
4a8f6654
AC
71 /* HACK: We need to force stdout/stderr to point at the console. This avoids
72 any potential side effects caused by legacy code that is still
73 using the TUI / fputs_unfiltered_hook. So we set up output channels for
74 this now, and swap them in when we are run. */
75
76 raw_stdout = stdio_fileopen (stdout);
77
78 /* Create MI channels */
79 mi->out = mi_console_file_new (raw_stdout, "~", '"');
80 mi->err = mi_console_file_new (raw_stdout, "&", '"');
81 mi->log = mi->err;
82 mi->targ = mi_console_file_new (raw_stdout, "@", '"');
83 mi->event_channel = mi_console_file_new (raw_stdout, "=", 0);
84
683f2885 85 if (top_level)
063bfe2e
VP
86 {
87 observer_attach_new_thread (mi_new_thread);
88 observer_attach_thread_exit (mi_thread_exit);
6c95b8df 89 observer_attach_inferior_appeared (mi_inferior_appeared);
4a92f99b 90 observer_attach_inferior_exit (mi_inferior_exit);
f7f9a841 91 observer_attach_normal_stop (mi_on_normal_stop);
e1ac3328 92 observer_attach_target_resumed (mi_on_resume);
c86cf029
VP
93 observer_attach_solib_loaded (mi_solib_loaded);
94 observer_attach_solib_unloaded (mi_solib_unloaded);
f3b1572e 95 observer_attach_about_to_proceed (mi_about_to_proceed);
063bfe2e 96 }
683f2885 97
4a8f6654
AC
98 return mi;
99}
100
101static int
102mi_interpreter_resume (void *data)
103{
104 struct mi_interp *mi = data;
105 /* As per hack note in mi_interpreter_init, swap in the output channels... */
106
107 gdb_setup_readline ();
108
362646f5
AC
109 /* These overwrite some of the initialization done in
110 _intialize_event_loop. */
111 call_readline = gdb_readline2;
112 input_handler = mi_execute_command_wrapper;
113 add_file_handler (input_fd, stdin_event_handler, 0);
114 async_command_editing_p = 0;
115 /* FIXME: This is a total hack for now. PB's use of the MI
116 implicitly relies on a bug in the async support which allows
117 asynchronous commands to leak through the commmand loop. The bug
118 involves (but is not limited to) the fact that sync_execution was
119 erroneously initialized to 0. Duplicate by initializing it thus
120 here... */
121 sync_execution = 0;
4a8f6654
AC
122
123 gdb_stdout = mi->out;
124 /* Route error and log output through the MI */
125 gdb_stderr = mi->err;
126 gdb_stdlog = mi->log;
127 /* Route target output through the MI. */
128 gdb_stdtarg = mi->targ;
1f20321b
FR
129 /* Route target error through the MI as well. */
130 gdb_stdtargerr = mi->targ;
4a8f6654
AC
131
132 /* Replace all the hooks that we know about. There really needs to
133 be a better way of doing this... */
134 clear_interpreter_hooks ();
135
9a4105ab 136 deprecated_show_load_progress = mi_load_progress;
4a8f6654
AC
137
138 /* If we're _the_ interpreter, take control. */
139 if (current_interp_named_p (INTERP_MI1))
9a4105ab 140 deprecated_command_loop_hook = mi1_command_loop;
f786f615 141 else if (current_interp_named_p (INTERP_MI2))
9a4105ab 142 deprecated_command_loop_hook = mi2_command_loop;
f786f615 143 else if (current_interp_named_p (INTERP_MI3))
9a4105ab 144 deprecated_command_loop_hook = mi3_command_loop;
4a8f6654 145 else
9a4105ab 146 deprecated_command_loop_hook = mi2_command_loop;
4a8f6654
AC
147
148 return 1;
149}
150
151static int
152mi_interpreter_suspend (void *data)
153{
154 gdb_disable_readline ();
155 return 1;
156}
157
71fff37b 158static struct gdb_exception
4a8f6654
AC
159mi_interpreter_exec (void *data, const char *command)
160{
71fff37b 161 static struct gdb_exception ok;
4a8f6654
AC
162 char *tmp = alloca (strlen (command) + 1);
163 strcpy (tmp, command);
164 mi_execute_command_wrapper (tmp);
c1043fc2 165 return exception_none;
4a8f6654
AC
166}
167
168/* Never display the default gdb prompt in mi case. */
169static int
170mi_interpreter_prompt_p (void *data)
171{
172 return 0;
173}
174
ce8f13f8 175void
4a8f6654
AC
176mi_cmd_interpreter_exec (char *command, char **argv, int argc)
177{
178 struct interp *interp_to_use;
4a8f6654
AC
179 int i;
180 struct interp_procs *procs;
a13e061a
PA
181 char *mi_error_message = NULL;
182 struct cleanup *old_chain;
4a8f6654
AC
183
184 if (argc < 2)
a13e061a 185 error ("mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command");
4a8f6654
AC
186
187 interp_to_use = interp_lookup (argv[0]);
188 if (interp_to_use == NULL)
a13e061a 189 error ("mi_cmd_interpreter_exec: could not find interpreter \"%s\"", argv[0]);
4a8f6654
AC
190
191 if (!interp_exec_p (interp_to_use))
a13e061a
PA
192 error ("mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution",
193 argv[0]);
4a8f6654
AC
194
195 /* Insert the MI out hooks, making sure to also call the interpreter's hooks
196 if it has any. */
197 /* KRS: We shouldn't need this... Events should be installed and they should
198 just ALWAYS fire something out down the MI channel... */
199 mi_insert_notify_hooks ();
200
201 /* Now run the code... */
202
a13e061a 203 old_chain = make_cleanup (null_cleanup, 0);
4a8f6654
AC
204 for (i = 1; i < argc; i++)
205 {
32c1e744
VP
206 struct gdb_exception e = interp_exec (interp_to_use, argv[i]);
207 if (e.reason < 0)
208 {
209 mi_error_message = xstrdup (e.message);
a13e061a 210 make_cleanup (xfree, mi_error_message);
32c1e744
VP
211 break;
212 }
4a8f6654
AC
213 }
214
215 mi_remove_notify_hooks ();
216
a13e061a
PA
217 if (mi_error_message != NULL)
218 error ("%s", mi_error_message);
219 do_cleanups (old_chain);
4a8f6654
AC
220}
221
222/*
223 * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce
224 * async-notify ("=") MI messages while running commands in another interpreter
225 * using mi_interpreter_exec. The canonical use for this is to allow access to
226 * the gdb CLI interpreter from within the MI, while still producing MI style output
227 * when actions in the CLI command change gdb's state.
228*/
229
230static void
231mi_insert_notify_hooks (void)
232{
9a4105ab 233 deprecated_query_hook = mi_interp_query_hook;
4a8f6654
AC
234}
235
236static void
11308a41 237mi_remove_notify_hooks (void)
4a8f6654 238{
9a4105ab 239 deprecated_query_hook = NULL;
4a8f6654
AC
240}
241
242static int
243mi_interp_query_hook (const char *ctlstr, va_list ap)
244{
245 return 1;
246}
247
4a8f6654
AC
248static void
249mi_execute_command_wrapper (char *cmd)
250{
251 mi_execute_command (cmd, stdin == instream);
252}
253
254static void
255mi1_command_loop (void)
256{
257 mi_command_loop (1);
258}
259
260static void
261mi2_command_loop (void)
262{
263 mi_command_loop (2);
264}
265
f786f615
AC
266static void
267mi3_command_loop (void)
268{
269 mi_command_loop (3);
270}
271
4a8f6654
AC
272static void
273mi_command_loop (int mi_version)
274{
4a8f6654
AC
275 /* Turn off 8 bit strings in quoted output. Any character with the
276 high bit set is printed using C's octal format. */
277 sevenbit_strings = 1;
278 /* Tell the world that we're alive */
279 fputs_unfiltered ("(gdb) \n", raw_stdout);
280 gdb_flush (raw_stdout);
362646f5 281 start_event_loop ();
4a8f6654
AC
282}
283
683f2885
VP
284static void
285mi_new_thread (struct thread_info *t)
286{
287 struct mi_interp *mi = top_level_interpreter_data ();
288
3d043ef6
VP
289 fprintf_unfiltered (mi->event_channel,
290 "thread-created,id=\"%d\",group-id=\"%d\"",
291 t->num, t->ptid.pid);
683f2885
VP
292 gdb_flush (mi->event_channel);
293}
294
063bfe2e 295static void
a07daef3 296mi_thread_exit (struct thread_info *t, int silent)
063bfe2e 297{
a07daef3
PA
298 struct mi_interp *mi;
299
300 if (silent)
301 return;
302
303 mi = top_level_interpreter_data ();
063bfe2e 304 target_terminal_ours ();
3d043ef6
VP
305 fprintf_unfiltered (mi->event_channel,
306 "thread-exited,id=\"%d\",group-id=\"%d\"",
307 t->num,t->ptid.pid);
063bfe2e
VP
308 gdb_flush (mi->event_channel);
309}
310
6c95b8df
PA
311void
312mi_inferior_appeared (int pid)
4a92f99b
VP
313{
314 struct mi_interp *mi = top_level_interpreter_data ();
315 target_terminal_ours ();
6c95b8df 316 fprintf_unfiltered (mi->event_channel, "thread-group-created,id=\"%d\"",
4a92f99b
VP
317 pid);
318 gdb_flush (mi->event_channel);
319}
320
321static void
322mi_inferior_exit (int pid)
323{
324 struct mi_interp *mi = top_level_interpreter_data ();
325 target_terminal_ours ();
326 fprintf_unfiltered (mi->event_channel, "thread-group-exited,id=\"%d\"",
327 pid);
328 gdb_flush (mi->event_channel);
329}
330
f7f9a841 331static void
1d33d6ba 332mi_on_normal_stop (struct bpstats *bs, int print_frame)
f7f9a841
VP
333{
334 /* Since this can be called when CLI command is executing,
335 using cli interpreter, be sure to use MI uiout for output,
336 not the current one. */
1d33d6ba 337 struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ());
f7f9a841
VP
338 struct mi_interp *mi = top_level_interpreter_data ();
339
1d33d6ba
VP
340 if (print_frame)
341 {
342 if (uiout != mi_uiout)
343 {
344 /* The normal_stop function has printed frame information into
345 CLI uiout, or some other non-MI uiout. There's no way we
346 can extract proper fields from random uiout object, so we print
347 the frame again. In practice, this can only happen when running
348 a CLI command in MI. */
349 struct ui_out *saved_uiout = uiout;
350 uiout = mi_uiout;
351 print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC);
352 uiout = saved_uiout;
353 }
354
355 ui_out_field_int (mi_uiout, "thread-id",
356 pid_to_thread_id (inferior_ptid));
357 if (non_stop)
358 {
359 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end
360 (mi_uiout, "stopped-threads");
361 ui_out_field_int (mi_uiout, NULL,
362 pid_to_thread_id (inferior_ptid));
363 do_cleanups (back_to);
364 }
365 else
366 ui_out_field_string (mi_uiout, "stopped-threads", "all");
367 }
368
f7f9a841 369 fputs_unfiltered ("*stopped", raw_stdout);
1d33d6ba
VP
370 mi_out_put (mi_uiout, raw_stdout);
371 mi_out_rewind (mi_uiout);
4333ada3 372 mi_print_timing_maybe ();
f7f9a841
VP
373 fputs_unfiltered ("\n", raw_stdout);
374 gdb_flush (raw_stdout);
375}
376
f3b1572e
PA
377static void
378mi_about_to_proceed (void)
379{
380 /* Suppress output while calling an inferior function. */
381
382 if (!ptid_equal (inferior_ptid, null_ptid))
383 {
384 struct thread_info *tp = inferior_thread ();
385 if (tp->in_infcall)
386 return;
387 }
388
389 mi_proceeded = 1;
390}
391
d90e17a7
PA
392static int
393mi_output_running_pid (struct thread_info *info, void *arg)
394{
395 ptid_t *ptid = arg;
396
397 if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid))
398 fprintf_unfiltered (raw_stdout,
399 "*running,thread-id=\"%d\"\n",
400 info->num);
401
402 return 0;
403}
404
405static int
406mi_inferior_count (struct inferior *inf, void *arg)
407{
408 if (inf->pid != 0)
409 {
410 int *count_p = arg;
411 (*count_p)++;
412 }
413
414 return 0;
415}
416
e1ac3328
VP
417static void
418mi_on_resume (ptid_t ptid)
419{
c5a4d20b
PA
420 struct thread_info *tp = NULL;
421
9944e9c2 422 if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid))
c5a4d20b
PA
423 tp = inferior_thread ();
424 else
e09875d4 425 tp = find_thread_ptid (ptid);
c5a4d20b
PA
426
427 /* Suppress output while calling an inferior function. */
428 if (tp->in_infcall)
429 return;
430
a2840c35
VP
431 /* To cater for older frontends, emit ^running, but do it only once
432 per each command. We do it here, since at this point we know
433 that the target was successfully resumed, and in non-async mode,
434 we won't return back to MI interpreter code until the target
435 is done running, so delaying the output of "^running" until then
436 will make it impossible for frontend to know what's going on.
437
438 In future (MI3), we'll be outputting "^done" here. */
f3b1572e 439 if (!running_result_record_printed && mi_proceeded)
a2840c35 440 {
c271b6e2
VP
441 fprintf_unfiltered (raw_stdout, "%s^running\n",
442 current_token ? current_token : "");
a2840c35
VP
443 }
444
e1ac3328
VP
445 if (PIDGET (ptid) == -1)
446 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
d90e17a7 447 else if (ptid_is_pid (ptid))
bb599c81 448 {
ab730e72 449 int count = 0;
d90e17a7
PA
450
451 /* Backwards compatibility. If there's only one inferior,
452 output "all", otherwise, output each resumed thread
453 individually. */
454 iterate_over_inferiors (mi_inferior_count, &count);
455
456 if (count == 1)
457 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n");
458 else
459 iterate_over_threads (mi_output_running_pid, &ptid);
bb599c81 460 }
e1ac3328
VP
461 else
462 {
e09875d4 463 struct thread_info *ti = find_thread_ptid (ptid);
e1ac3328
VP
464 gdb_assert (ti);
465 fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num);
466 }
a2840c35 467
f3b1572e 468 if (!running_result_record_printed && mi_proceeded)
a2840c35
VP
469 {
470 running_result_record_printed = 1;
471 /* This is what gdb used to do historically -- printing prompt even if
472 it cannot actually accept any input. This will be surely removed
473 for MI3, and may be removed even earler. */
474 /* FIXME: review the use of target_is_async_p here -- is that
475 what we want? */
476 if (!target_is_async_p ())
477 fputs_unfiltered ("(gdb) \n", raw_stdout);
478 }
c1828f25 479 gdb_flush (raw_stdout);
e1ac3328
VP
480}
481
c86cf029
VP
482static void
483mi_solib_loaded (struct so_list *solib)
484{
485 struct mi_interp *mi = top_level_interpreter_data ();
486 target_terminal_ours ();
487 fprintf_unfiltered (mi->event_channel,
488 "library-loaded,id=\"%s\",target-name=\"%s\",host-name=\"%s\",symbols-loaded=\"%d\"",
489 solib->so_original_name, solib->so_original_name,
490 solib->so_name, solib->symbols_loaded);
491 gdb_flush (mi->event_channel);
492}
493
494static void
495mi_solib_unloaded (struct so_list *solib)
496{
497 struct mi_interp *mi = top_level_interpreter_data ();
498 target_terminal_ours ();
499 fprintf_unfiltered (mi->event_channel,
500 "library-unloaded,id=\"%s\",target-name=\"%s\",host-name=\"%s\"",
501 solib->so_original_name, solib->so_original_name,
502 solib->so_name);
503 gdb_flush (mi->event_channel);
504}
505
506
b9362cc7
AC
507extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */
508
4a8f6654
AC
509void
510_initialize_mi_interp (void)
511{
512 static const struct interp_procs procs =
513 {
514 mi_interpreter_init, /* init_proc */
515 mi_interpreter_resume, /* resume_proc */
516 mi_interpreter_suspend, /* suspend_proc */
517 mi_interpreter_exec, /* exec_proc */
518 mi_interpreter_prompt_p /* prompt_proc_p */
519 };
520
2fcf52f0 521 /* The various interpreter levels. */
4a8f6654 522 interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs));
2fcf52f0
AC
523 interp_add (interp_new (INTERP_MI2, NULL, mi_out_new (2), &procs));
524 interp_add (interp_new (INTERP_MI3, NULL, mi_out_new (3), &procs));
4a8f6654 525
2fcf52f0
AC
526 /* "mi" selects the most recent released version. "mi2" was
527 released as part of GDB 6.0. */
528 interp_add (interp_new (INTERP_MI, NULL, mi_out_new (2), &procs));
4a8f6654 529}
This page took 0.653838 seconds and 4 git commands to generate.