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