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