Commit | Line | Data |
---|---|---|
4a8f6654 AC |
1 | /* MI Interpreter Definitions and Commands for GDB, the GNU debugger. |
2 | ||
3 | Copyright 2002, 2003 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 2 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, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "gdb_string.h" | |
24 | #include "interps.h" | |
25 | #include "event-top.h" | |
26 | #include "event-loop.h" | |
27 | #include "inferior.h" | |
28 | #include "ui-out.h" | |
29 | #include "top.h" | |
30 | ||
31 | #include "mi-main.h" | |
32 | #include "mi-cmds.h" | |
33 | #include "mi-out.h" | |
34 | #include "mi-console.h" | |
35 | ||
36 | struct mi_interp | |
37 | { | |
38 | /* MI's output channels */ | |
39 | struct ui_file *out; | |
40 | struct ui_file *err; | |
41 | struct ui_file *log; | |
42 | struct ui_file *targ; | |
43 | struct ui_file *event_channel; | |
44 | ||
45 | /* This is the interpreter for the mi... */ | |
46 | struct interp *mi2_interp; | |
47 | struct interp *mi1_interp; | |
48 | struct interp *mi_interp; | |
49 | }; | |
50 | ||
51 | /* These are the interpreter setup, etc. functions for the MI interpreter */ | |
52 | static void mi_execute_command_wrapper (char *cmd); | |
53 | static void mi_command_loop (int mi_version); | |
54 | static char *mi_input (char *); | |
55 | ||
56 | /* These are hooks that we put in place while doing interpreter_exec | |
57 | so we can report interesting things that happened "behind the mi's | |
58 | back" in this command */ | |
59 | static int mi_interp_query_hook (const char *ctlstr, va_list ap); | |
60 | static char *mi_interp_read_one_line_hook (char *prompt, int repeat, | |
61 | char *anno); | |
62 | ||
63 | static void mi2_command_loop (void); | |
64 | static void mi1_command_loop (void); | |
65 | ||
66 | static void mi_insert_notify_hooks (void); | |
67 | static void mi_remove_notify_hooks (void); | |
68 | ||
69 | static void * | |
70 | mi_interpreter_init (void) | |
71 | { | |
72 | struct mi_interp *mi = XMALLOC (struct mi_interp); | |
73 | ||
74 | /* Why is this a part of the mi architecture? */ | |
75 | ||
76 | mi_setup_architecture_data (); | |
77 | ||
78 | /* HACK: We need to force stdout/stderr to point at the console. This avoids | |
79 | any potential side effects caused by legacy code that is still | |
80 | using the TUI / fputs_unfiltered_hook. So we set up output channels for | |
81 | this now, and swap them in when we are run. */ | |
82 | ||
83 | raw_stdout = stdio_fileopen (stdout); | |
84 | ||
85 | /* Create MI channels */ | |
86 | mi->out = mi_console_file_new (raw_stdout, "~", '"'); | |
87 | mi->err = mi_console_file_new (raw_stdout, "&", '"'); | |
88 | mi->log = mi->err; | |
89 | mi->targ = mi_console_file_new (raw_stdout, "@", '"'); | |
90 | mi->event_channel = mi_console_file_new (raw_stdout, "=", 0); | |
91 | ||
92 | return mi; | |
93 | } | |
94 | ||
95 | static int | |
96 | mi_interpreter_resume (void *data) | |
97 | { | |
98 | struct mi_interp *mi = data; | |
99 | /* As per hack note in mi_interpreter_init, swap in the output channels... */ | |
100 | ||
101 | gdb_setup_readline (); | |
102 | ||
103 | if (event_loop_p) | |
104 | { | |
105 | /* These overwrite some of the initialization done in | |
106 | _intialize_event_loop. */ | |
107 | call_readline = gdb_readline2; | |
108 | input_handler = mi_execute_command_wrapper; | |
109 | add_file_handler (input_fd, stdin_event_handler, 0); | |
110 | async_command_editing_p = 0; | |
111 | /* FIXME: This is a total hack for now. PB's use of the MI implicitly | |
112 | relies on a bug in the async support which allows asynchronous | |
113 | commands to leak through the commmand loop. The bug involves | |
114 | (but is not limited to) the fact that sync_execution was | |
115 | erroneously initialized to 0. Duplicate by initializing it | |
116 | thus here... */ | |
117 | sync_execution = 0; | |
118 | } | |
119 | ||
120 | gdb_stdout = mi->out; | |
121 | /* Route error and log output through the MI */ | |
122 | gdb_stderr = mi->err; | |
123 | gdb_stdlog = mi->log; | |
124 | /* Route target output through the MI. */ | |
125 | gdb_stdtarg = mi->targ; | |
126 | ||
127 | /* Replace all the hooks that we know about. There really needs to | |
128 | be a better way of doing this... */ | |
129 | clear_interpreter_hooks (); | |
130 | ||
131 | show_load_progress = mi_load_progress; | |
132 | ||
133 | /* If we're _the_ interpreter, take control. */ | |
134 | if (current_interp_named_p (INTERP_MI1)) | |
135 | command_loop_hook = mi1_command_loop; | |
136 | else if (current_interp_named_p (INTERP_MI)) | |
137 | command_loop_hook = mi2_command_loop; | |
138 | else | |
139 | return 0; | |
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 | ||
151 | static int | |
152 | mi_interpreter_exec (void *data, const char *command) | |
153 | { | |
154 | char *tmp = alloca (strlen (command) + 1); | |
155 | strcpy (tmp, command); | |
156 | mi_execute_command_wrapper (tmp); | |
157 | return 1; | |
158 | } | |
159 | ||
160 | /* Never display the default gdb prompt in mi case. */ | |
161 | static int | |
162 | mi_interpreter_prompt_p (void *data) | |
163 | { | |
164 | return 0; | |
165 | } | |
166 | ||
167 | static void | |
168 | mi_interpreter_exec_continuation (struct continuation_arg *arg) | |
169 | { | |
170 | bpstat_do_actions (&stop_bpstat); | |
171 | if (!target_executing) | |
172 | { | |
173 | fputs_unfiltered ("*stopped", raw_stdout); | |
174 | mi_out_put (uiout, raw_stdout); | |
175 | fputs_unfiltered ("\n", raw_stdout); | |
176 | fputs_unfiltered ("(gdb) \n", raw_stdout); | |
177 | gdb_flush (raw_stdout); | |
178 | do_exec_cleanups (ALL_CLEANUPS); | |
179 | } | |
180 | else if (target_can_async_p ()) | |
181 | { | |
182 | add_continuation (mi_interpreter_exec_continuation, NULL); | |
183 | } | |
184 | } | |
185 | ||
186 | enum mi_cmd_result | |
187 | mi_cmd_interpreter_exec (char *command, char **argv, int argc) | |
188 | { | |
189 | struct interp *interp_to_use; | |
190 | enum mi_cmd_result result = MI_CMD_DONE; | |
191 | int i; | |
192 | struct interp_procs *procs; | |
193 | ||
194 | if (argc < 2) | |
195 | { | |
196 | xasprintf (&mi_error_message, | |
197 | "mi_cmd_interpreter_exec: Usage: -interpreter-exec interp command"); | |
198 | return MI_CMD_ERROR; | |
199 | } | |
200 | ||
201 | interp_to_use = interp_lookup (argv[0]); | |
202 | if (interp_to_use == NULL) | |
203 | { | |
204 | xasprintf (&mi_error_message, | |
205 | "mi_cmd_interpreter_exec: could not find interpreter \"%s\"", | |
206 | argv[0]); | |
207 | return MI_CMD_ERROR; | |
208 | } | |
209 | ||
210 | if (!interp_exec_p (interp_to_use)) | |
211 | { | |
212 | xasprintf (&mi_error_message, | |
213 | "mi_cmd_interpreter_exec: interpreter \"%s\" does not support command execution", | |
214 | argv[0]); | |
215 | return MI_CMD_ERROR; | |
216 | } | |
217 | ||
218 | /* Insert the MI out hooks, making sure to also call the interpreter's hooks | |
219 | if it has any. */ | |
220 | /* KRS: We shouldn't need this... Events should be installed and they should | |
221 | just ALWAYS fire something out down the MI channel... */ | |
222 | mi_insert_notify_hooks (); | |
223 | ||
224 | /* Now run the code... */ | |
225 | ||
226 | for (i = 1; i < argc; i++) | |
227 | { | |
228 | char *buff = NULL; | |
229 | /* Do this in a cleaner way... We want to force execution to be | |
230 | asynchronous for commands that run the target. */ | |
231 | if (target_can_async_p () && (strcmp (argv[0], "console") == 0)) | |
232 | { | |
233 | int len = strlen (argv[i]); | |
234 | buff = xmalloc (len + 2); | |
235 | memcpy (buff, argv[i], len); | |
236 | buff[len] = '&'; | |
237 | buff[len + 1] = '\0'; | |
238 | } | |
239 | ||
240 | /* We had to set sync_execution = 0 for the mi (well really for Project | |
241 | Builder's use of the mi - particularly so interrupting would work. | |
242 | But for console commands to work, we need to initialize it to 1 - | |
243 | since that is what the cli expects - before running the command, | |
244 | and then set it back to 0 when we are done. */ | |
245 | sync_execution = 1; | |
246 | if (interp_exec (interp_to_use, argv[i]) < 0) | |
247 | { | |
248 | mi_error_last_message (); | |
249 | result = MI_CMD_ERROR; | |
250 | break; | |
251 | } | |
252 | xfree (buff); | |
253 | do_exec_error_cleanups (ALL_CLEANUPS); | |
254 | sync_execution = 0; | |
255 | } | |
256 | ||
257 | mi_remove_notify_hooks (); | |
258 | ||
259 | /* Okay, now let's see if the command set the inferior going... | |
260 | Tricky point - have to do this AFTER resetting the interpreter, since | |
261 | changing the interpreter will clear out all the continuations for | |
262 | that interpreter... */ | |
263 | ||
264 | if (target_can_async_p () && target_executing) | |
265 | { | |
266 | fputs_unfiltered ("^running\n", raw_stdout); | |
267 | add_continuation (mi_interpreter_exec_continuation, NULL); | |
268 | } | |
269 | ||
270 | return result; | |
271 | } | |
272 | ||
273 | /* | |
274 | * mi_insert_notify_hooks - This inserts a number of hooks that are meant to produce | |
275 | * async-notify ("=") MI messages while running commands in another interpreter | |
276 | * using mi_interpreter_exec. The canonical use for this is to allow access to | |
277 | * the gdb CLI interpreter from within the MI, while still producing MI style output | |
278 | * when actions in the CLI command change gdb's state. | |
279 | */ | |
280 | ||
281 | static void | |
282 | mi_insert_notify_hooks (void) | |
283 | { | |
284 | query_hook = mi_interp_query_hook; | |
285 | } | |
286 | ||
287 | static void | |
288 | mi_remove_notify_hooks () | |
289 | { | |
290 | query_hook = NULL; | |
291 | } | |
292 | ||
293 | static int | |
294 | mi_interp_query_hook (const char *ctlstr, va_list ap) | |
295 | { | |
296 | return 1; | |
297 | } | |
298 | ||
299 | static char * | |
300 | mi_interp_read_one_line_hook (char *prompt, int repeat, char *anno) | |
301 | { | |
302 | static char buff[256]; | |
303 | printf_unfiltered ("=read-one-line,prompt=\"%s\"\n", prompt); | |
304 | gdb_flush (gdb_stdout); | |
305 | (void) fgets (buff, sizeof (buff), stdin); | |
306 | buff[(strlen (buff) - 1)] = 0; | |
307 | return buff; | |
308 | } | |
309 | ||
310 | static void | |
311 | output_control_change_notification (char *notification) | |
312 | { | |
313 | printf_unfiltered ("^"); | |
314 | printf_unfiltered ("%s\n", notification); | |
315 | gdb_flush (gdb_stdout); | |
316 | } | |
317 | ||
318 | static void | |
319 | mi_execute_command_wrapper (char *cmd) | |
320 | { | |
321 | mi_execute_command (cmd, stdin == instream); | |
322 | } | |
323 | ||
324 | static void | |
325 | mi1_command_loop (void) | |
326 | { | |
327 | mi_command_loop (1); | |
328 | } | |
329 | ||
330 | static void | |
331 | mi2_command_loop (void) | |
332 | { | |
333 | mi_command_loop (2); | |
334 | } | |
335 | ||
336 | static void | |
337 | mi_command_loop (int mi_version) | |
338 | { | |
339 | #if 0 | |
340 | /* HACK: Force stdout/stderr to point at the console. This avoids | |
341 | any potential side effects caused by legacy code that is still | |
342 | using the TUI / fputs_unfiltered_hook */ | |
343 | raw_stdout = stdio_fileopen (stdout); | |
344 | /* Route normal output through the MIx */ | |
345 | gdb_stdout = mi_console_file_new (raw_stdout, "~", '"'); | |
346 | /* Route error and log output through the MI */ | |
347 | gdb_stderr = mi_console_file_new (raw_stdout, "&", '"'); | |
348 | gdb_stdlog = gdb_stderr; | |
349 | /* Route target output through the MI. */ | |
350 | gdb_stdtarg = mi_console_file_new (raw_stdout, "@", '"'); | |
351 | /* HACK: Poke the ui_out table directly. Should we be creating a | |
352 | mi_out object wired up to the above gdb_stdout / gdb_stderr? */ | |
353 | uiout = mi_out_new (mi_version); | |
354 | /* HACK: Override any other interpreter hooks. We need to create a | |
355 | real event table and pass in that. */ | |
356 | init_ui_hook = 0; | |
357 | /* command_loop_hook = 0; */ | |
358 | print_frame_info_listing_hook = 0; | |
359 | query_hook = 0; | |
360 | warning_hook = 0; | |
361 | create_breakpoint_hook = 0; | |
362 | delete_breakpoint_hook = 0; | |
363 | modify_breakpoint_hook = 0; | |
364 | interactive_hook = 0; | |
365 | registers_changed_hook = 0; | |
366 | readline_begin_hook = 0; | |
367 | readline_hook = 0; | |
368 | readline_end_hook = 0; | |
369 | register_changed_hook = 0; | |
370 | memory_changed_hook = 0; | |
371 | context_hook = 0; | |
372 | target_wait_hook = 0; | |
373 | call_command_hook = 0; | |
374 | error_hook = 0; | |
375 | error_begin_hook = 0; | |
376 | show_load_progress = mi_load_progress; | |
377 | #endif | |
378 | /* Turn off 8 bit strings in quoted output. Any character with the | |
379 | high bit set is printed using C's octal format. */ | |
380 | sevenbit_strings = 1; | |
381 | /* Tell the world that we're alive */ | |
382 | fputs_unfiltered ("(gdb) \n", raw_stdout); | |
383 | gdb_flush (raw_stdout); | |
384 | if (!event_loop_p) | |
385 | simplified_command_loop (mi_input, mi_execute_command); | |
386 | else | |
387 | start_event_loop (); | |
388 | } | |
389 | ||
390 | static char * | |
391 | mi_input (char *buf) | |
392 | { | |
393 | return gdb_readline (NULL); | |
394 | } | |
395 | ||
396 | void | |
397 | _initialize_mi_interp (void) | |
398 | { | |
399 | static const struct interp_procs procs = | |
400 | { | |
401 | mi_interpreter_init, /* init_proc */ | |
402 | mi_interpreter_resume, /* resume_proc */ | |
403 | mi_interpreter_suspend, /* suspend_proc */ | |
404 | mi_interpreter_exec, /* exec_proc */ | |
405 | mi_interpreter_prompt_p /* prompt_proc_p */ | |
406 | }; | |
407 | ||
408 | /* Create MI1 interpreter */ | |
409 | interp_add (interp_new (INTERP_MI1, NULL, mi_out_new (1), &procs)); | |
410 | ||
411 | interp_add (interp_new (INTERP_MI, NULL, mi_out_new (3), &procs)); | |
412 | } |