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