Commit | Line | Data |
---|---|---|
4a8f6654 AC |
1 | /* MI Interpreter Definitions and Commands for GDB, the GNU debugger. |
2 | ||
7b6bb8da | 3 | Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 |
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" |
8d3788bd | 38 | #include "gdb.h" |
4a8f6654 | 39 | |
4a8f6654 AC |
40 | /* These are the interpreter setup, etc. functions for the MI interpreter */ |
41 | static void mi_execute_command_wrapper (char *cmd); | |
42 | static void mi_command_loop (int mi_version); | |
4a8f6654 AC |
43 | |
44 | /* These are hooks that we put in place while doing interpreter_exec | |
45 | so we can report interesting things that happened "behind the mi's | |
46 | back" in this command */ | |
bee0189a | 47 | static int mi_interp_query_hook (const char *ctlstr, va_list ap) |
a0b31db1 | 48 | ATTRIBUTE_PRINTF (1, 0); |
4a8f6654 | 49 | |
f786f615 | 50 | static void mi3_command_loop (void); |
4a8f6654 AC |
51 | static void mi2_command_loop (void); |
52 | static void mi1_command_loop (void); | |
53 | ||
54 | static void mi_insert_notify_hooks (void); | |
55 | static void mi_remove_notify_hooks (void); | |
1d33d6ba | 56 | static void mi_on_normal_stop (struct bpstats *bs, int print_frame); |
4a8f6654 | 57 | |
683f2885 | 58 | static void mi_new_thread (struct thread_info *t); |
a07daef3 | 59 | static void mi_thread_exit (struct thread_info *t, int silent); |
a79b8f6e VP |
60 | static void mi_inferior_added (struct inferior *inf); |
61 | static void mi_inferior_appeared (struct inferior *inf); | |
62 | static void mi_inferior_exit (struct inferior *inf); | |
63 | static void mi_inferior_removed (struct inferior *inf); | |
e1ac3328 | 64 | static void mi_on_resume (ptid_t ptid); |
c86cf029 VP |
65 | static void mi_solib_loaded (struct so_list *solib); |
66 | static void mi_solib_unloaded (struct so_list *solib); | |
f3b1572e | 67 | static void mi_about_to_proceed (void); |
8d3788bd VP |
68 | static void mi_breakpoint_created (struct breakpoint *b); |
69 | static void mi_breakpoint_deleted (struct breakpoint *b); | |
70 | static void mi_breakpoint_modified (struct breakpoint *b); | |
683f2885 | 71 | |
a79b8f6e VP |
72 | static int report_initial_inferior (struct inferior *inf, void *closure); |
73 | ||
4a8f6654 | 74 | static void * |
4801a9a3 | 75 | mi_interpreter_init (struct interp *interp, int top_level) |
4a8f6654 AC |
76 | { |
77 | struct mi_interp *mi = XMALLOC (struct mi_interp); | |
4801a9a3 PA |
78 | const char *name; |
79 | int mi_version; | |
4a8f6654 | 80 | |
4a8f6654 AC |
81 | /* HACK: We need to force stdout/stderr to point at the console. This avoids |
82 | any potential side effects caused by legacy code that is still | |
83 | using the TUI / fputs_unfiltered_hook. So we set up output channels for | |
84 | this now, and swap them in when we are run. */ | |
85 | ||
86 | raw_stdout = stdio_fileopen (stdout); | |
87 | ||
88 | /* Create MI channels */ | |
89 | mi->out = mi_console_file_new (raw_stdout, "~", '"'); | |
90 | mi->err = mi_console_file_new (raw_stdout, "&", '"'); | |
91 | mi->log = mi->err; | |
92 | mi->targ = mi_console_file_new (raw_stdout, "@", '"'); | |
93 | mi->event_channel = mi_console_file_new (raw_stdout, "=", 0); | |
94 | ||
4801a9a3 PA |
95 | name = interp_name (interp); |
96 | /* INTERP_MI selects the most recent released version. "mi2" was | |
97 | released as part of GDB 6.0. */ | |
98 | if (strcmp (name, INTERP_MI) == 0) | |
99 | mi_version = 2; | |
100 | else if (strcmp (name, INTERP_MI1) == 0) | |
101 | mi_version = 1; | |
102 | else if (strcmp (name, INTERP_MI2) == 0) | |
103 | mi_version = 2; | |
104 | else if (strcmp (name, INTERP_MI3) == 0) | |
105 | mi_version = 3; | |
106 | else | |
107 | gdb_assert_not_reached ("unhandled MI version"); | |
108 | ||
109 | mi->uiout = mi_out_new (mi_version); | |
110 | ||
683f2885 | 111 | if (top_level) |
063bfe2e VP |
112 | { |
113 | observer_attach_new_thread (mi_new_thread); | |
114 | observer_attach_thread_exit (mi_thread_exit); | |
a79b8f6e | 115 | observer_attach_inferior_added (mi_inferior_added); |
6c95b8df | 116 | observer_attach_inferior_appeared (mi_inferior_appeared); |
4a92f99b | 117 | observer_attach_inferior_exit (mi_inferior_exit); |
a79b8f6e | 118 | observer_attach_inferior_removed (mi_inferior_removed); |
f7f9a841 | 119 | observer_attach_normal_stop (mi_on_normal_stop); |
e1ac3328 | 120 | observer_attach_target_resumed (mi_on_resume); |
c86cf029 VP |
121 | observer_attach_solib_loaded (mi_solib_loaded); |
122 | observer_attach_solib_unloaded (mi_solib_unloaded); | |
f3b1572e | 123 | observer_attach_about_to_proceed (mi_about_to_proceed); |
8d3788bd VP |
124 | observer_attach_breakpoint_created (mi_breakpoint_created); |
125 | observer_attach_breakpoint_deleted (mi_breakpoint_deleted); | |
126 | observer_attach_breakpoint_modified (mi_breakpoint_modified); | |
a79b8f6e VP |
127 | |
128 | /* The initial inferior is created before this function is called, so we | |
129 | need to report it explicitly. Use iteration in case future version | |
130 | of GDB creates more than one inferior up-front. */ | |
131 | iterate_over_inferiors (report_initial_inferior, mi); | |
063bfe2e | 132 | } |
683f2885 | 133 | |
4a8f6654 AC |
134 | return mi; |
135 | } | |
136 | ||
137 | static int | |
138 | mi_interpreter_resume (void *data) | |
139 | { | |
140 | struct mi_interp *mi = data; | |
4a8f6654 | 141 | |
102040f0 | 142 | /* As per hack note in mi_interpreter_init, swap in the output channels... */ |
4a8f6654 AC |
143 | gdb_setup_readline (); |
144 | ||
362646f5 AC |
145 | /* These overwrite some of the initialization done in |
146 | _intialize_event_loop. */ | |
147 | call_readline = gdb_readline2; | |
148 | input_handler = mi_execute_command_wrapper; | |
149 | add_file_handler (input_fd, stdin_event_handler, 0); | |
150 | async_command_editing_p = 0; | |
151 | /* FIXME: This is a total hack for now. PB's use of the MI | |
152 | implicitly relies on a bug in the async support which allows | |
153 | asynchronous commands to leak through the commmand loop. The bug | |
154 | involves (but is not limited to) the fact that sync_execution was | |
155 | erroneously initialized to 0. Duplicate by initializing it thus | |
156 | here... */ | |
157 | sync_execution = 0; | |
4a8f6654 AC |
158 | |
159 | gdb_stdout = mi->out; | |
160 | /* Route error and log output through the MI */ | |
161 | gdb_stderr = mi->err; | |
162 | gdb_stdlog = mi->log; | |
163 | /* Route target output through the MI. */ | |
164 | gdb_stdtarg = mi->targ; | |
1f20321b FR |
165 | /* Route target error through the MI as well. */ |
166 | gdb_stdtargerr = mi->targ; | |
4a8f6654 AC |
167 | |
168 | /* Replace all the hooks that we know about. There really needs to | |
169 | be a better way of doing this... */ | |
170 | clear_interpreter_hooks (); | |
171 | ||
9a4105ab | 172 | deprecated_show_load_progress = mi_load_progress; |
4a8f6654 AC |
173 | |
174 | /* If we're _the_ interpreter, take control. */ | |
175 | if (current_interp_named_p (INTERP_MI1)) | |
9a4105ab | 176 | deprecated_command_loop_hook = mi1_command_loop; |
f786f615 | 177 | else if (current_interp_named_p (INTERP_MI2)) |
9a4105ab | 178 | deprecated_command_loop_hook = mi2_command_loop; |
f786f615 | 179 | else if (current_interp_named_p (INTERP_MI3)) |
9a4105ab | 180 | deprecated_command_loop_hook = mi3_command_loop; |
4a8f6654 | 181 | else |
9a4105ab | 182 | deprecated_command_loop_hook = mi2_command_loop; |
4a8f6654 AC |
183 | |
184 | return 1; | |
185 | } | |
186 | ||
187 | static int | |
188 | mi_interpreter_suspend (void *data) | |
189 | { | |
190 | gdb_disable_readline (); | |
191 | return 1; | |
192 | } | |
193 | ||
71fff37b | 194 | static struct gdb_exception |
4a8f6654 AC |
195 | mi_interpreter_exec (void *data, const char *command) |
196 | { | |
197 | char *tmp = alloca (strlen (command) + 1); | |
102040f0 | 198 | |
4a8f6654 AC |
199 | strcpy (tmp, command); |
200 | mi_execute_command_wrapper (tmp); | |
c1043fc2 | 201 | return exception_none; |
4a8f6654 AC |
202 | } |
203 | ||
204 | /* Never display the default gdb prompt in mi case. */ | |
205 | static int | |
206 | mi_interpreter_prompt_p (void *data) | |
207 | { | |
208 | return 0; | |
209 | } | |
210 | ||
ce8f13f8 | 211 | void |
4a8f6654 AC |
212 | mi_cmd_interpreter_exec (char *command, char **argv, int argc) |
213 | { | |
214 | struct interp *interp_to_use; | |
4a8f6654 | 215 | int i; |
a13e061a PA |
216 | char *mi_error_message = NULL; |
217 | struct cleanup *old_chain; | |
4a8f6654 AC |
218 | |
219 | if (argc < 2) | |
1b05df00 | 220 | error (_("-interpreter-exec: " |
9b20d036 | 221 | "Usage: -interpreter-exec interp command")); |
4a8f6654 AC |
222 | |
223 | interp_to_use = interp_lookup (argv[0]); | |
224 | if (interp_to_use == NULL) | |
1b05df00 | 225 | error (_("-interpreter-exec: could not find interpreter \"%s\""), |
9a2b4c1b | 226 | argv[0]); |
4a8f6654 AC |
227 | |
228 | if (!interp_exec_p (interp_to_use)) | |
1b05df00 | 229 | error (_("-interpreter-exec: interpreter \"%s\" " |
9b20d036 | 230 | "does not support command execution"), |
a13e061a | 231 | argv[0]); |
4a8f6654 AC |
232 | |
233 | /* Insert the MI out hooks, making sure to also call the interpreter's hooks | |
234 | if it has any. */ | |
235 | /* KRS: We shouldn't need this... Events should be installed and they should | |
236 | just ALWAYS fire something out down the MI channel... */ | |
237 | mi_insert_notify_hooks (); | |
238 | ||
239 | /* Now run the code... */ | |
240 | ||
a13e061a | 241 | old_chain = make_cleanup (null_cleanup, 0); |
4a8f6654 AC |
242 | for (i = 1; i < argc; i++) |
243 | { | |
32c1e744 | 244 | struct gdb_exception e = interp_exec (interp_to_use, argv[i]); |
102040f0 | 245 | |
32c1e744 VP |
246 | if (e.reason < 0) |
247 | { | |
248 | mi_error_message = xstrdup (e.message); | |
a13e061a | 249 | make_cleanup (xfree, mi_error_message); |
32c1e744 VP |
250 | break; |
251 | } | |
4a8f6654 AC |
252 | } |
253 | ||
254 | mi_remove_notify_hooks (); | |
255 | ||
a13e061a PA |
256 | if (mi_error_message != NULL) |
257 | error ("%s", mi_error_message); | |
258 | do_cleanups (old_chain); | |
4a8f6654 AC |
259 | } |
260 | ||
261 | /* | |
9a2b4c1b MS |
262 | * mi_insert_notify_hooks - This inserts a number of hooks that are |
263 | * meant to produce async-notify ("=") MI messages while running | |
264 | * commands in another interpreter using mi_interpreter_exec. The | |
265 | * canonical use for this is to allow access to the gdb CLI | |
266 | * interpreter from within the MI, while still producing MI style | |
267 | * output when actions in the CLI command change gdb's state. | |
4a8f6654 AC |
268 | */ |
269 | ||
270 | static void | |
271 | mi_insert_notify_hooks (void) | |
272 | { | |
9a4105ab | 273 | deprecated_query_hook = mi_interp_query_hook; |
4a8f6654 AC |
274 | } |
275 | ||
276 | static void | |
11308a41 | 277 | mi_remove_notify_hooks (void) |
4a8f6654 | 278 | { |
9a4105ab | 279 | deprecated_query_hook = NULL; |
4a8f6654 AC |
280 | } |
281 | ||
282 | static int | |
283 | mi_interp_query_hook (const char *ctlstr, va_list ap) | |
284 | { | |
285 | return 1; | |
286 | } | |
287 | ||
4a8f6654 AC |
288 | static void |
289 | mi_execute_command_wrapper (char *cmd) | |
290 | { | |
291 | mi_execute_command (cmd, stdin == instream); | |
292 | } | |
293 | ||
294 | static void | |
295 | mi1_command_loop (void) | |
296 | { | |
297 | mi_command_loop (1); | |
298 | } | |
299 | ||
300 | static void | |
301 | mi2_command_loop (void) | |
302 | { | |
303 | mi_command_loop (2); | |
304 | } | |
305 | ||
f786f615 AC |
306 | static void |
307 | mi3_command_loop (void) | |
308 | { | |
309 | mi_command_loop (3); | |
310 | } | |
311 | ||
4a8f6654 AC |
312 | static void |
313 | mi_command_loop (int mi_version) | |
314 | { | |
4a8f6654 AC |
315 | /* Turn off 8 bit strings in quoted output. Any character with the |
316 | high bit set is printed using C's octal format. */ | |
317 | sevenbit_strings = 1; | |
318 | /* Tell the world that we're alive */ | |
319 | fputs_unfiltered ("(gdb) \n", raw_stdout); | |
320 | gdb_flush (raw_stdout); | |
362646f5 | 321 | start_event_loop (); |
4a8f6654 AC |
322 | } |
323 | ||
683f2885 VP |
324 | static void |
325 | mi_new_thread (struct thread_info *t) | |
326 | { | |
327 | struct mi_interp *mi = top_level_interpreter_data (); | |
a79b8f6e VP |
328 | struct inferior *inf = find_inferior_pid (ptid_get_pid (t->ptid)); |
329 | ||
330 | gdb_assert (inf); | |
683f2885 | 331 | |
3d043ef6 | 332 | fprintf_unfiltered (mi->event_channel, |
a79b8f6e VP |
333 | "thread-created,id=\"%d\",group-id=\"i%d\"", |
334 | t->num, inf->num); | |
683f2885 VP |
335 | gdb_flush (mi->event_channel); |
336 | } | |
337 | ||
063bfe2e | 338 | static void |
a07daef3 | 339 | mi_thread_exit (struct thread_info *t, int silent) |
063bfe2e | 340 | { |
a07daef3 | 341 | struct mi_interp *mi; |
a79b8f6e | 342 | struct inferior *inf; |
a07daef3 PA |
343 | |
344 | if (silent) | |
345 | return; | |
346 | ||
a79b8f6e VP |
347 | inf = find_inferior_pid (ptid_get_pid (t->ptid)); |
348 | ||
a07daef3 | 349 | mi = top_level_interpreter_data (); |
063bfe2e | 350 | target_terminal_ours (); |
3d043ef6 | 351 | fprintf_unfiltered (mi->event_channel, |
a79b8f6e VP |
352 | "thread-exited,id=\"%d\",group-id=\"i%d\"", |
353 | t->num, inf->num); | |
063bfe2e VP |
354 | gdb_flush (mi->event_channel); |
355 | } | |
356 | ||
a79b8f6e VP |
357 | static void |
358 | mi_inferior_added (struct inferior *inf) | |
359 | { | |
360 | struct mi_interp *mi = top_level_interpreter_data (); | |
102040f0 | 361 | |
a79b8f6e VP |
362 | target_terminal_ours (); |
363 | fprintf_unfiltered (mi->event_channel, | |
364 | "thread-group-added,id=\"i%d\"", | |
365 | inf->num); | |
366 | gdb_flush (mi->event_channel); | |
367 | } | |
368 | ||
369 | static void | |
370 | mi_inferior_appeared (struct inferior *inf) | |
4a92f99b VP |
371 | { |
372 | struct mi_interp *mi = top_level_interpreter_data (); | |
102040f0 | 373 | |
4a92f99b | 374 | target_terminal_ours (); |
a79b8f6e VP |
375 | fprintf_unfiltered (mi->event_channel, |
376 | "thread-group-started,id=\"i%d\",pid=\"%d\"", | |
377 | inf->num, inf->pid); | |
4a92f99b VP |
378 | gdb_flush (mi->event_channel); |
379 | } | |
380 | ||
381 | static void | |
a79b8f6e | 382 | mi_inferior_exit (struct inferior *inf) |
4a92f99b VP |
383 | { |
384 | struct mi_interp *mi = top_level_interpreter_data (); | |
102040f0 | 385 | |
4a92f99b | 386 | target_terminal_ours (); |
8cf64490 TT |
387 | if (inf->has_exit_code) |
388 | fprintf_unfiltered (mi->event_channel, | |
389 | "thread-group-exited,id=\"i%d\",exit-code=\"%s\"", | |
390 | inf->num, int_string (inf->exit_code, 8, 0, 0, 1)); | |
391 | else | |
392 | fprintf_unfiltered (mi->event_channel, | |
393 | "thread-group-exited,id=\"i%d\"", inf->num); | |
394 | ||
4a92f99b VP |
395 | gdb_flush (mi->event_channel); |
396 | } | |
397 | ||
a79b8f6e VP |
398 | static void |
399 | mi_inferior_removed (struct inferior *inf) | |
400 | { | |
401 | struct mi_interp *mi = top_level_interpreter_data (); | |
102040f0 | 402 | |
a79b8f6e VP |
403 | target_terminal_ours (); |
404 | fprintf_unfiltered (mi->event_channel, | |
405 | "thread-group-removed,id=\"i%d\"", | |
406 | inf->num); | |
407 | gdb_flush (mi->event_channel); | |
408 | } | |
409 | ||
f7f9a841 | 410 | static void |
1d33d6ba | 411 | mi_on_normal_stop (struct bpstats *bs, int print_frame) |
f7f9a841 VP |
412 | { |
413 | /* Since this can be called when CLI command is executing, | |
414 | using cli interpreter, be sure to use MI uiout for output, | |
415 | not the current one. */ | |
1d33d6ba | 416 | struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ()); |
f7f9a841 | 417 | |
1d33d6ba VP |
418 | if (print_frame) |
419 | { | |
dc146f7c | 420 | int core; |
102040f0 | 421 | |
79a45e25 | 422 | if (current_uiout != mi_uiout) |
1d33d6ba VP |
423 | { |
424 | /* The normal_stop function has printed frame information into | |
425 | CLI uiout, or some other non-MI uiout. There's no way we | |
426 | can extract proper fields from random uiout object, so we print | |
427 | the frame again. In practice, this can only happen when running | |
428 | a CLI command in MI. */ | |
79a45e25 | 429 | struct ui_out *saved_uiout = current_uiout; |
102040f0 | 430 | |
79a45e25 | 431 | current_uiout = mi_uiout; |
5bd6aa83 | 432 | bpstat_print (bs); |
1d33d6ba | 433 | print_stack_frame (get_selected_frame (NULL), 0, SRC_AND_LOC); |
79a45e25 | 434 | current_uiout = saved_uiout; |
1d33d6ba VP |
435 | } |
436 | ||
437 | ui_out_field_int (mi_uiout, "thread-id", | |
438 | pid_to_thread_id (inferior_ptid)); | |
439 | if (non_stop) | |
440 | { | |
441 | struct cleanup *back_to = make_cleanup_ui_out_list_begin_end | |
442 | (mi_uiout, "stopped-threads"); | |
102040f0 | 443 | |
1d33d6ba | 444 | ui_out_field_int (mi_uiout, NULL, |
102040f0 | 445 | pid_to_thread_id (inferior_ptid)); |
1d33d6ba VP |
446 | do_cleanups (back_to); |
447 | } | |
448 | else | |
449 | ui_out_field_string (mi_uiout, "stopped-threads", "all"); | |
dc146f7c VP |
450 | |
451 | core = target_core_of_thread (inferior_ptid); | |
452 | if (core != -1) | |
453 | ui_out_field_int (mi_uiout, "core", core); | |
1d33d6ba VP |
454 | } |
455 | ||
f7f9a841 | 456 | fputs_unfiltered ("*stopped", raw_stdout); |
1d33d6ba VP |
457 | mi_out_put (mi_uiout, raw_stdout); |
458 | mi_out_rewind (mi_uiout); | |
4333ada3 | 459 | mi_print_timing_maybe (); |
f7f9a841 VP |
460 | fputs_unfiltered ("\n", raw_stdout); |
461 | gdb_flush (raw_stdout); | |
462 | } | |
463 | ||
f3b1572e PA |
464 | static void |
465 | mi_about_to_proceed (void) | |
466 | { | |
467 | /* Suppress output while calling an inferior function. */ | |
468 | ||
469 | if (!ptid_equal (inferior_ptid, null_ptid)) | |
470 | { | |
471 | struct thread_info *tp = inferior_thread (); | |
102040f0 | 472 | |
16c381f0 | 473 | if (tp->control.in_infcall) |
f3b1572e PA |
474 | return; |
475 | } | |
476 | ||
477 | mi_proceeded = 1; | |
478 | } | |
479 | ||
8d3788bd VP |
480 | /* When non-zero, no MI notifications will be emitted in |
481 | response to breakpoint change observers. */ | |
482 | int mi_suppress_breakpoint_notifications = 0; | |
483 | ||
484 | /* Emit notification about a created breakpoint. */ | |
485 | static void | |
486 | mi_breakpoint_created (struct breakpoint *b) | |
487 | { | |
488 | struct mi_interp *mi = top_level_interpreter_data (); | |
489 | struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ()); | |
490 | struct gdb_exception e; | |
491 | ||
492 | if (mi_suppress_breakpoint_notifications) | |
493 | return; | |
494 | ||
495 | if (b->number <= 0) | |
496 | return; | |
497 | ||
498 | target_terminal_ours (); | |
499 | fprintf_unfiltered (mi->event_channel, | |
500 | "breakpoint-created"); | |
501 | /* We want the output from gdb_breakpoint_query to go to | |
502 | mi->event_channel. One approach would be to just | |
503 | call gdb_breakpoint_query, and then use mi_out_put to | |
504 | send the current content of mi_outout into mi->event_channel. | |
505 | However, that will break if anything is output to mi_uiout | |
506 | prior the calling the breakpoint_created notifications. | |
507 | So, we use ui_out_redirect. */ | |
508 | ui_out_redirect (mi_uiout, mi->event_channel); | |
509 | TRY_CATCH (e, RETURN_MASK_ERROR) | |
510 | gdb_breakpoint_query (mi_uiout, b->number, NULL); | |
511 | ui_out_redirect (mi_uiout, NULL); | |
512 | ||
513 | gdb_flush (mi->event_channel); | |
514 | } | |
515 | ||
516 | /* Emit notification about deleted breakpoint. */ | |
517 | static void | |
518 | mi_breakpoint_deleted (struct breakpoint *b) | |
519 | { | |
520 | struct mi_interp *mi = top_level_interpreter_data (); | |
521 | ||
522 | if (mi_suppress_breakpoint_notifications) | |
523 | return; | |
524 | ||
525 | if (b->number <= 0) | |
526 | return; | |
527 | ||
528 | target_terminal_ours (); | |
529 | ||
530 | fprintf_unfiltered (mi->event_channel, "breakpoint-deleted,id=\"%d\"", | |
531 | b->number); | |
532 | ||
533 | gdb_flush (mi->event_channel); | |
534 | } | |
535 | ||
536 | /* Emit notification about modified breakpoint. */ | |
537 | static void | |
538 | mi_breakpoint_modified (struct breakpoint *b) | |
539 | { | |
540 | struct mi_interp *mi = top_level_interpreter_data (); | |
541 | struct ui_out *mi_uiout = interp_ui_out (top_level_interpreter ()); | |
542 | struct gdb_exception e; | |
543 | ||
544 | if (mi_suppress_breakpoint_notifications) | |
545 | return; | |
546 | ||
547 | if (b->number <= 0) | |
548 | return; | |
549 | ||
550 | target_terminal_ours (); | |
551 | fprintf_unfiltered (mi->event_channel, | |
552 | "breakpoint-modified"); | |
553 | /* We want the output from gdb_breakpoint_query to go to | |
554 | mi->event_channel. One approach would be to just | |
555 | call gdb_breakpoint_query, and then use mi_out_put to | |
556 | send the current content of mi_outout into mi->event_channel. | |
557 | However, that will break if anything is output to mi_uiout | |
558 | prior the calling the breakpoint_created notifications. | |
559 | So, we use ui_out_redirect. */ | |
560 | ui_out_redirect (mi_uiout, mi->event_channel); | |
561 | TRY_CATCH (e, RETURN_MASK_ERROR) | |
562 | gdb_breakpoint_query (mi_uiout, b->number, NULL); | |
563 | ui_out_redirect (mi_uiout, NULL); | |
564 | ||
565 | gdb_flush (mi->event_channel); | |
566 | } | |
567 | ||
568 | ||
d90e17a7 PA |
569 | static int |
570 | mi_output_running_pid (struct thread_info *info, void *arg) | |
571 | { | |
572 | ptid_t *ptid = arg; | |
573 | ||
574 | if (ptid_get_pid (*ptid) == ptid_get_pid (info->ptid)) | |
575 | fprintf_unfiltered (raw_stdout, | |
576 | "*running,thread-id=\"%d\"\n", | |
577 | info->num); | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
582 | static int | |
583 | mi_inferior_count (struct inferior *inf, void *arg) | |
584 | { | |
585 | if (inf->pid != 0) | |
586 | { | |
587 | int *count_p = arg; | |
588 | (*count_p)++; | |
589 | } | |
590 | ||
591 | return 0; | |
592 | } | |
593 | ||
e1ac3328 VP |
594 | static void |
595 | mi_on_resume (ptid_t ptid) | |
596 | { | |
c5a4d20b PA |
597 | struct thread_info *tp = NULL; |
598 | ||
9944e9c2 | 599 | if (ptid_equal (ptid, minus_one_ptid) || ptid_is_pid (ptid)) |
c5a4d20b PA |
600 | tp = inferior_thread (); |
601 | else | |
e09875d4 | 602 | tp = find_thread_ptid (ptid); |
c5a4d20b PA |
603 | |
604 | /* Suppress output while calling an inferior function. */ | |
16c381f0 | 605 | if (tp->control.in_infcall) |
c5a4d20b PA |
606 | return; |
607 | ||
a2840c35 VP |
608 | /* To cater for older frontends, emit ^running, but do it only once |
609 | per each command. We do it here, since at this point we know | |
610 | that the target was successfully resumed, and in non-async mode, | |
611 | we won't return back to MI interpreter code until the target | |
612 | is done running, so delaying the output of "^running" until then | |
613 | will make it impossible for frontend to know what's going on. | |
614 | ||
615 | In future (MI3), we'll be outputting "^done" here. */ | |
f3b1572e | 616 | if (!running_result_record_printed && mi_proceeded) |
a2840c35 | 617 | { |
c271b6e2 VP |
618 | fprintf_unfiltered (raw_stdout, "%s^running\n", |
619 | current_token ? current_token : ""); | |
a2840c35 VP |
620 | } |
621 | ||
e1ac3328 VP |
622 | if (PIDGET (ptid) == -1) |
623 | fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n"); | |
d90e17a7 | 624 | else if (ptid_is_pid (ptid)) |
bb599c81 | 625 | { |
ab730e72 | 626 | int count = 0; |
d90e17a7 PA |
627 | |
628 | /* Backwards compatibility. If there's only one inferior, | |
629 | output "all", otherwise, output each resumed thread | |
630 | individually. */ | |
631 | iterate_over_inferiors (mi_inferior_count, &count); | |
632 | ||
633 | if (count == 1) | |
634 | fprintf_unfiltered (raw_stdout, "*running,thread-id=\"all\"\n"); | |
635 | else | |
636 | iterate_over_threads (mi_output_running_pid, &ptid); | |
bb599c81 | 637 | } |
e1ac3328 VP |
638 | else |
639 | { | |
e09875d4 | 640 | struct thread_info *ti = find_thread_ptid (ptid); |
102040f0 | 641 | |
e1ac3328 VP |
642 | gdb_assert (ti); |
643 | fprintf_unfiltered (raw_stdout, "*running,thread-id=\"%d\"\n", ti->num); | |
644 | } | |
a2840c35 | 645 | |
f3b1572e | 646 | if (!running_result_record_printed && mi_proceeded) |
a2840c35 VP |
647 | { |
648 | running_result_record_printed = 1; | |
649 | /* This is what gdb used to do historically -- printing prompt even if | |
650 | it cannot actually accept any input. This will be surely removed | |
651 | for MI3, and may be removed even earler. */ | |
652 | /* FIXME: review the use of target_is_async_p here -- is that | |
653 | what we want? */ | |
654 | if (!target_is_async_p ()) | |
655 | fputs_unfiltered ("(gdb) \n", raw_stdout); | |
656 | } | |
c1828f25 | 657 | gdb_flush (raw_stdout); |
e1ac3328 VP |
658 | } |
659 | ||
c86cf029 VP |
660 | static void |
661 | mi_solib_loaded (struct so_list *solib) | |
662 | { | |
663 | struct mi_interp *mi = top_level_interpreter_data (); | |
102040f0 | 664 | |
c86cf029 | 665 | target_terminal_ours (); |
a79b8f6e VP |
666 | if (gdbarch_has_global_solist (target_gdbarch)) |
667 | fprintf_unfiltered (mi->event_channel, | |
668 | "library-loaded,id=\"%s\",target-name=\"%s\"," | |
669 | "host-name=\"%s\",symbols-loaded=\"%d\"", | |
670 | solib->so_original_name, solib->so_original_name, | |
671 | solib->so_name, solib->symbols_loaded); | |
672 | else | |
673 | fprintf_unfiltered (mi->event_channel, | |
674 | "library-loaded,id=\"%s\",target-name=\"%s\"," | |
675 | "host-name=\"%s\",symbols-loaded=\"%d\"," | |
676 | "thread-group=\"i%d\"", | |
677 | solib->so_original_name, solib->so_original_name, | |
678 | solib->so_name, solib->symbols_loaded, | |
679 | current_inferior ()->num); | |
680 | ||
c86cf029 VP |
681 | gdb_flush (mi->event_channel); |
682 | } | |
683 | ||
684 | static void | |
685 | mi_solib_unloaded (struct so_list *solib) | |
686 | { | |
687 | struct mi_interp *mi = top_level_interpreter_data (); | |
102040f0 | 688 | |
c86cf029 | 689 | target_terminal_ours (); |
a79b8f6e VP |
690 | if (gdbarch_has_global_solist (target_gdbarch)) |
691 | fprintf_unfiltered (mi->event_channel, | |
692 | "library-unloaded,id=\"%s\",target-name=\"%s\"," | |
693 | "host-name=\"%s\"", | |
694 | solib->so_original_name, solib->so_original_name, | |
695 | solib->so_name); | |
696 | else | |
697 | fprintf_unfiltered (mi->event_channel, | |
698 | "library-unloaded,id=\"%s\",target-name=\"%s\"," | |
699 | "host-name=\"%s\",thread-group=\"i%d\"", | |
700 | solib->so_original_name, solib->so_original_name, | |
701 | solib->so_name, current_inferior ()->num); | |
702 | ||
c86cf029 VP |
703 | gdb_flush (mi->event_channel); |
704 | } | |
705 | ||
a79b8f6e VP |
706 | static int |
707 | report_initial_inferior (struct inferior *inf, void *closure) | |
708 | { | |
709 | /* This function is called from mi_intepreter_init, and since | |
710 | mi_inferior_added assumes that inferior is fully initialized | |
711 | and top_level_interpreter_data is set, we cannot call | |
712 | it here. */ | |
713 | struct mi_interp *mi = closure; | |
102040f0 | 714 | |
a79b8f6e VP |
715 | target_terminal_ours (); |
716 | fprintf_unfiltered (mi->event_channel, | |
717 | "thread-group-added,id=\"i%d\"", | |
718 | inf->num); | |
719 | gdb_flush (mi->event_channel); | |
720 | return 0; | |
721 | } | |
c86cf029 | 722 | |
4801a9a3 PA |
723 | static struct ui_out * |
724 | mi_ui_out (struct interp *interp) | |
725 | { | |
726 | struct mi_interp *mi = interp_data (interp); | |
727 | ||
728 | return mi->uiout; | |
729 | } | |
730 | ||
b9362cc7 AC |
731 | extern initialize_file_ftype _initialize_mi_interp; /* -Wmissing-prototypes */ |
732 | ||
4a8f6654 AC |
733 | void |
734 | _initialize_mi_interp (void) | |
735 | { | |
736 | static const struct interp_procs procs = | |
737 | { | |
738 | mi_interpreter_init, /* init_proc */ | |
739 | mi_interpreter_resume, /* resume_proc */ | |
740 | mi_interpreter_suspend, /* suspend_proc */ | |
741 | mi_interpreter_exec, /* exec_proc */ | |
4801a9a3 PA |
742 | mi_interpreter_prompt_p, /* prompt_proc_p */ |
743 | mi_ui_out /* ui_out_proc */ | |
4a8f6654 AC |
744 | }; |
745 | ||
2fcf52f0 | 746 | /* The various interpreter levels. */ |
4801a9a3 PA |
747 | interp_add (interp_new (INTERP_MI1, &procs)); |
748 | interp_add (interp_new (INTERP_MI2, &procs)); | |
749 | interp_add (interp_new (INTERP_MI3, &procs)); | |
750 | interp_add (interp_new (INTERP_MI, &procs)); | |
4a8f6654 | 751 | } |