1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2018 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"
29 #include "observable.h"
30 #include "gdbthread.h"
31 #include "thread-fsm.h"
34 cli_interp_base::cli_interp_base (const char *name
)
38 cli_interp_base::~cli_interp_base ()
41 /* The console interpreter. */
43 class cli_interp final
: public cli_interp_base
46 explicit cli_interp (const char *name
);
48 void init (bool top_level
) override
;
49 void resume () override
;
50 void suspend () override
;
51 gdb_exception
exec (const char *command_str
) override
;
52 ui_out
*interp_ui_out () override
;
54 /* The ui_out for the console interpreter. */
55 cli_ui_out
*cli_uiout
;
58 cli_interp::cli_interp (const char *name
)
59 : cli_interp_base (name
)
61 /* Create a default uiout builder for the CLI. */
62 this->cli_uiout
= cli_out_new (gdb_stdout
);
65 /* Suppress notification struct. */
66 struct cli_suppress_notification cli_suppress_notification
=
68 0 /* user_selected_context_changed */
71 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
72 and returns NULL otherwise. */
74 static struct cli_interp
*
75 as_cli_interp (struct interp
*interp
)
77 return dynamic_cast<cli_interp
*> (interp
);
80 /* Longjmp-safe wrapper for "execute_command". */
81 static struct gdb_exception
safe_execute_command (struct ui_out
*uiout
,
87 Breakpoint hits should always be mirrored to a console. Deciding
88 what to mirror to a console wrt to breakpoints and random stops
89 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
90 clearly want to mirror the event to the console in this case. But
91 what about more complicated cases like "s&; thread n; s&", and one
92 of those steps spawning a new thread, and that thread hitting a
93 breakpoint? It's impossible in general to track whether the thread
94 had any relation to the commands that had been executed. So we
95 just simplify and always mirror breakpoints and random events to
98 OTOH, we should print the source line to the console when stepping
99 or other similar commands, iff the step was started by that console
100 (or in MI's case, by a console command), but not if it was started
101 with MI's -exec-step or similar. */
104 should_print_stop_to_console (struct interp
*console_interp
,
105 struct thread_info
*tp
)
107 if ((bpstat_what (tp
->control
.stop_bpstat
).main_action
108 == BPSTAT_WHAT_STOP_NOISY
)
109 || tp
->thread_fsm
== NULL
110 || tp
->thread_fsm
->command_interp
== console_interp
111 || !thread_fsm_finished_p (tp
->thread_fsm
))
116 /* Observers for several run control events. If the interpreter is
117 quiet (i.e., another interpreter is being run with
118 interpreter-exec), print nothing. */
120 /* Observer for the normal_stop notification. */
123 cli_on_normal_stop (struct bpstats
*bs
, int print_frame
)
128 SWITCH_THRU_ALL_UIS ()
130 struct interp
*interp
= top_level_interpreter ();
131 struct cli_interp
*cli
= as_cli_interp (interp
);
132 struct thread_info
*thread
;
137 thread
= inferior_thread ();
138 if (should_print_stop_to_console (interp
, thread
))
139 print_stop_event (cli
->cli_uiout
);
143 /* Observer for the signal_received notification. */
146 cli_on_signal_received (enum gdb_signal siggnal
)
148 SWITCH_THRU_ALL_UIS ()
150 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
155 print_signal_received_reason (cli
->cli_uiout
, siggnal
);
159 /* Observer for the end_stepping_range notification. */
162 cli_on_end_stepping_range (void)
164 SWITCH_THRU_ALL_UIS ()
166 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
171 print_end_stepping_range_reason (cli
->cli_uiout
);
175 /* Observer for the signalled notification. */
178 cli_on_signal_exited (enum gdb_signal siggnal
)
180 SWITCH_THRU_ALL_UIS ()
182 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
187 print_signal_exited_reason (cli
->cli_uiout
, siggnal
);
191 /* Observer for the exited notification. */
194 cli_on_exited (int exitstatus
)
196 SWITCH_THRU_ALL_UIS ()
198 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
203 print_exited_reason (cli
->cli_uiout
, exitstatus
);
207 /* Observer for the no_history notification. */
210 cli_on_no_history (void)
212 SWITCH_THRU_ALL_UIS ()
214 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
219 print_no_history_reason (cli
->cli_uiout
);
223 /* Observer for the sync_execution_done notification. */
226 cli_on_sync_execution_done (void)
228 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
233 display_gdb_prompt (NULL
);
236 /* Observer for the command_error notification. */
239 cli_on_command_error (void)
241 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
246 display_gdb_prompt (NULL
);
249 /* Observer for the user_selected_context_changed notification. */
252 cli_on_user_selected_context_changed (user_selected_what selection
)
254 struct thread_info
*tp
;
256 /* This event is suppressed. */
257 if (cli_suppress_notification
.user_selected_context
)
260 tp
= find_thread_ptid (inferior_ptid
);
262 SWITCH_THRU_ALL_UIS ()
264 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
269 if (selection
& USER_SELECTED_INFERIOR
)
270 print_selected_inferior (cli
->cli_uiout
);
273 && ((selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
))))
274 print_selected_thread_frame (cli
->cli_uiout
, selection
);
278 /* pre_command_loop implementation. */
281 cli_interp_base::pre_command_loop ()
283 display_gdb_prompt (0);
286 /* These implement the cli out interpreter: */
289 cli_interp::init (bool top_level
)
294 cli_interp::resume ()
296 struct ui
*ui
= current_ui
;
297 struct cli_interp
*cli
= this;
298 struct ui_file
*stream
;
300 /*sync_execution = 1; */
302 /* gdb_setup_readline will change gdb_stdout. If the CLI was
303 previously writing to gdb_stdout, then set it to the new
304 gdb_stdout afterwards. */
306 stream
= cli
->cli_uiout
->set_stream (gdb_stdout
);
307 if (stream
!= gdb_stdout
)
309 cli
->cli_uiout
->set_stream (stream
);
313 gdb_setup_readline (1);
315 ui
->input_handler
= command_line_handler
;
318 cli
->cli_uiout
->set_stream (gdb_stdout
);
322 cli_interp::suspend ()
324 gdb_disable_readline ();
328 cli_interp::exec (const char *command_str
)
330 struct cli_interp
*cli
= this;
331 struct ui_file
*old_stream
;
332 struct gdb_exception result
;
334 /* gdb_stdout could change between the time cli_uiout was
335 initialized and now. Since we're probably using a different
336 interpreter which has a new ui_file for gdb_stdout, use that one
337 instead of the default.
339 It is important that it gets reset everytime, since the user
340 could set gdb to use a different interpreter. */
341 old_stream
= cli
->cli_uiout
->set_stream (gdb_stdout
);
342 result
= safe_execute_command (cli
->cli_uiout
, command_str
, 1);
343 cli
->cli_uiout
->set_stream (old_stream
);
348 cli_interp_base::supports_command_editing ()
353 static struct gdb_exception
354 safe_execute_command (struct ui_out
*command_uiout
, const char *command
,
357 struct gdb_exception e
= exception_none
;
359 /* Save and override the global ``struct ui_out'' builder. */
360 scoped_restore saved_uiout
= make_scoped_restore (¤t_uiout
,
365 execute_command (command
, from_tty
);
367 CATCH (exception
, RETURN_MASK_ALL
)
373 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
374 caller should print the exception. */
375 exception_print (gdb_stderr
, e
);
380 cli_interp::interp_ui_out ()
382 struct cli_interp
*cli
= (struct cli_interp
*) this;
384 return cli
->cli_uiout
;
387 /* These hold the pushed copies of the gdb output files.
388 If NULL then nothing has yet been pushed. */
389 struct saved_output_files
397 static saved_output_files saved_output
;
399 /* See cli-interp.h. */
402 cli_interp_base::set_logging (ui_file_up logfile
, bool logging_redirect
)
406 saved_output
.out
= gdb_stdout
;
407 saved_output
.err
= gdb_stderr
;
408 saved_output
.log
= gdb_stdlog
;
409 saved_output
.targ
= gdb_stdtarg
;
410 saved_output
.targerr
= gdb_stdtargerr
;
412 /* A raw pointer since ownership is transferred to
414 ui_file
*output
= make_logging_output (gdb_stdout
,
420 gdb_stdtarg
= output
;
421 gdb_stdtargerr
= output
;
425 /* Only delete one of the files -- they are all set to the same
429 gdb_stdout
= saved_output
.out
;
430 gdb_stderr
= saved_output
.err
;
431 gdb_stdlog
= saved_output
.log
;
432 gdb_stdtarg
= saved_output
.targ
;
433 gdb_stdtargerr
= saved_output
.targerr
;
435 saved_output
.out
= NULL
;
436 saved_output
.err
= NULL
;
437 saved_output
.log
= NULL
;
438 saved_output
.targ
= NULL
;
439 saved_output
.targerr
= NULL
;
443 /* Factory for CLI interpreters. */
445 static struct interp
*
446 cli_interp_factory (const char *name
)
448 return new cli_interp (name
);
451 /* Standard gdb initialization hook. */
454 _initialize_cli_interp (void)
456 interp_factory_register (INTERP_CONSOLE
, cli_interp_factory
);
458 /* If changing this, remember to update tui-interp.c as well. */
459 gdb::observers::normal_stop
.attach (cli_on_normal_stop
);
460 gdb::observers::end_stepping_range
.attach (cli_on_end_stepping_range
);
461 gdb::observers::signal_received
.attach (cli_on_signal_received
);
462 gdb::observers::signal_exited
.attach (cli_on_signal_exited
);
463 gdb::observers::exited
.attach (cli_on_exited
);
464 gdb::observers::no_history
.attach (cli_on_no_history
);
465 gdb::observers::sync_execution_done
.attach (cli_on_sync_execution_done
);
466 gdb::observers::command_error
.attach (cli_on_command_error
);
467 gdb::observers::user_selected_context_changed
.attach
468 (cli_on_user_selected_context_changed
);