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