1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "cli-interp.h"
23 #include "event-top.h"
26 #include "top.h" /* for "execute_command" */
27 #include "event-top.h"
30 #include "gdbthread.h"
31 #include "thread-fsm.h"
33 /* The console interpreter. */
36 /* The ui_out for the console interpreter. */
37 struct ui_out
*cli_uiout
;
40 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
41 and returns NULL otherwise. */
43 static struct cli_interp
*
44 as_cli_interp (struct interp
*interp
)
46 if (strcmp (interp_name (interp
), INTERP_CONSOLE
) == 0)
47 return (struct cli_interp
*) interp_data (interp
);
51 /* Longjmp-safe wrapper for "execute_command". */
52 static struct gdb_exception
safe_execute_command (struct ui_out
*uiout
,
58 Breakpoint hits should always be mirrored to a console. Deciding
59 what to mirror to a console wrt to breakpoints and random stops
60 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
61 clearly want to mirror the event to the console in this case. But
62 what about more complicated cases like "s&; thread n; s&", and one
63 of those steps spawning a new thread, and that thread hitting a
64 breakpoint? It's impossible in general to track whether the thread
65 had any relation to the commands that had been executed. So we
66 just simplify and always mirror breakpoints and random events to
69 OTOH, we should print the source line to the console when stepping
70 or other similar commands, iff the step was started by that console
71 (or in MI's case, by a console command), but not if it was started
72 with MI's -exec-step or similar. */
75 should_print_stop_to_console (struct interp
*console_interp
,
76 struct thread_info
*tp
)
78 if ((bpstat_what (tp
->control
.stop_bpstat
).main_action
79 == BPSTAT_WHAT_STOP_NOISY
)
80 || tp
->thread_fsm
== NULL
81 || tp
->thread_fsm
->command_interp
== console_interp
82 || !thread_fsm_finished_p (tp
->thread_fsm
))
87 /* Observers for several run control events. If the interpreter is
88 quiet (i.e., another interpreter is being run with
89 interpreter-exec), print nothing. */
91 /* Observer for the normal_stop notification. */
94 cli_on_normal_stop (struct bpstats
*bs
, int print_frame
)
96 struct switch_thru_all_uis state
;
101 SWITCH_THRU_ALL_UIS (state
)
103 struct interp
*interp
= top_level_interpreter ();
104 struct cli_interp
*cli
= as_cli_interp (interp
);
105 struct thread_info
*thread
;
110 thread
= inferior_thread ();
111 if (should_print_stop_to_console (interp
, thread
))
112 print_stop_event (cli
->cli_uiout
);
116 /* Observer for the signal_received notification. */
119 cli_on_signal_received (enum gdb_signal siggnal
)
121 struct switch_thru_all_uis state
;
123 SWITCH_THRU_ALL_UIS (state
)
125 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
130 print_signal_received_reason (cli
->cli_uiout
, siggnal
);
134 /* Observer for the end_stepping_range notification. */
137 cli_on_end_stepping_range (void)
139 struct switch_thru_all_uis state
;
141 SWITCH_THRU_ALL_UIS (state
)
143 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
148 print_end_stepping_range_reason (cli
->cli_uiout
);
152 /* Observer for the signalled notification. */
155 cli_on_signal_exited (enum gdb_signal siggnal
)
157 struct switch_thru_all_uis state
;
159 SWITCH_THRU_ALL_UIS (state
)
161 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
166 print_signal_exited_reason (cli
->cli_uiout
, siggnal
);
170 /* Observer for the exited notification. */
173 cli_on_exited (int exitstatus
)
175 struct switch_thru_all_uis state
;
177 SWITCH_THRU_ALL_UIS (state
)
179 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
184 print_exited_reason (cli
->cli_uiout
, exitstatus
);
188 /* Observer for the no_history notification. */
191 cli_on_no_history (void)
193 struct switch_thru_all_uis state
;
195 SWITCH_THRU_ALL_UIS (state
)
197 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
202 print_no_history_reason (cli
->cli_uiout
);
206 /* Observer for the sync_execution_done notification. */
209 cli_on_sync_execution_done (void)
211 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
216 display_gdb_prompt (NULL
);
219 /* Observer for the command_error notification. */
222 cli_on_command_error (void)
224 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
229 display_gdb_prompt (NULL
);
232 /* pre_command_loop implementation. */
235 cli_interpreter_pre_command_loop (struct interp
*self
)
237 display_gdb_prompt (0);
240 /* These implement the cli out interpreter: */
243 cli_interpreter_init (struct interp
*self
, int top_level
)
245 return interp_data (self
);
249 cli_interpreter_resume (void *data
)
251 struct ui
*ui
= current_ui
;
252 struct cli_interp
*cli
= (struct cli_interp
*) data
;
253 struct ui_file
*stream
;
255 /*sync_execution = 1; */
257 /* gdb_setup_readline will change gdb_stdout. If the CLI was
258 previously writing to gdb_stdout, then set it to the new
259 gdb_stdout afterwards. */
261 stream
= cli_out_set_stream (cli
->cli_uiout
, gdb_stdout
);
262 if (stream
!= gdb_stdout
)
264 cli_out_set_stream (cli
->cli_uiout
, stream
);
268 gdb_setup_readline (1);
270 ui
->input_handler
= command_line_handler
;
273 cli_out_set_stream (cli
->cli_uiout
, gdb_stdout
);
279 cli_interpreter_suspend (void *data
)
281 gdb_disable_readline ();
285 static struct gdb_exception
286 cli_interpreter_exec (void *data
, const char *command_str
)
288 struct cli_interp
*cli
= (struct cli_interp
*) data
;
289 struct ui_file
*old_stream
;
290 struct gdb_exception result
;
292 /* FIXME: cagney/2003-02-01: Need to const char *propogate
293 safe_execute_command. */
294 char *str
= (char *) alloca (strlen (command_str
) + 1);
295 strcpy (str
, command_str
);
297 /* gdb_stdout could change between the time cli_uiout was
298 initialized and now. Since we're probably using a different
299 interpreter which has a new ui_file for gdb_stdout, use that one
300 instead of the default.
302 It is important that it gets reset everytime, since the user
303 could set gdb to use a different interpreter. */
304 old_stream
= cli_out_set_stream (cli
->cli_uiout
, gdb_stdout
);
305 result
= safe_execute_command (cli
->cli_uiout
, str
, 1);
306 cli_out_set_stream (cli
->cli_uiout
, old_stream
);
311 cli_interpreter_supports_command_editing (struct interp
*interp
)
316 static struct gdb_exception
317 safe_execute_command (struct ui_out
*command_uiout
, char *command
, int from_tty
)
319 struct gdb_exception e
= exception_none
;
320 struct ui_out
*saved_uiout
;
322 /* Save and override the global ``struct ui_out'' builder. */
323 saved_uiout
= current_uiout
;
324 current_uiout
= command_uiout
;
328 execute_command (command
, from_tty
);
330 CATCH (exception
, RETURN_MASK_ALL
)
336 /* Restore the global builder. */
337 current_uiout
= saved_uiout
;
339 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
340 caller should print the exception. */
341 exception_print (gdb_stderr
, e
);
345 static struct ui_out
*
346 cli_ui_out (struct interp
*self
)
348 struct cli_interp
*cli
= (struct cli_interp
*) interp_data (self
);
350 return cli
->cli_uiout
;
353 /* The CLI interpreter's vtable. */
355 static const struct interp_procs cli_interp_procs
= {
356 cli_interpreter_init
, /* init_proc */
357 cli_interpreter_resume
, /* resume_proc */
358 cli_interpreter_suspend
, /* suspend_proc */
359 cli_interpreter_exec
, /* exec_proc */
360 cli_ui_out
, /* ui_out_proc */
361 NULL
, /* set_logging_proc */
362 cli_interpreter_pre_command_loop
, /* pre_command_loop_proc */
363 cli_interpreter_supports_command_editing
, /* supports_command_editing_proc */
366 /* Factory for CLI interpreters. */
368 static struct interp
*
369 cli_interp_factory (const char *name
)
371 struct cli_interp
*cli
= XNEW (struct cli_interp
);
373 /* Create a default uiout builder for the CLI. */
374 cli
->cli_uiout
= cli_out_new (gdb_stdout
);
376 return interp_new (name
, &cli_interp_procs
, cli
);
379 /* Standard gdb initialization hook. */
380 extern initialize_file_ftype _initialize_cli_interp
; /* -Wmissing-prototypes */
383 _initialize_cli_interp (void)
385 interp_factory_register (INTERP_CONSOLE
, cli_interp_factory
);
387 /* If changing this, remember to update tui-interp.c as well. */
388 observer_attach_normal_stop (cli_on_normal_stop
);
389 observer_attach_end_stepping_range (cli_on_end_stepping_range
);
390 observer_attach_signal_received (cli_on_signal_received
);
391 observer_attach_signal_exited (cli_on_signal_exited
);
392 observer_attach_exited (cli_on_exited
);
393 observer_attach_no_history (cli_on_no_history
);
394 observer_attach_sync_execution_done (cli_on_sync_execution_done
);
395 observer_attach_command_error (cli_on_command_error
);