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