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