1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2019 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
);
49 void init (bool top_level
) override
;
50 void resume () override
;
51 void suspend () override
;
52 gdb_exception
exec (const char *command_str
) override
;
53 ui_out
*interp_ui_out () override
;
55 /* The ui_out for the console interpreter. */
56 cli_ui_out
*cli_uiout
;
59 cli_interp::cli_interp (const char *name
)
60 : cli_interp_base (name
)
62 /* Create a default uiout builder for the CLI. */
63 this->cli_uiout
= cli_out_new (gdb_stdout
);
66 cli_interp::~cli_interp ()
71 /* Suppress notification struct. */
72 struct cli_suppress_notification cli_suppress_notification
=
74 0 /* user_selected_context_changed */
77 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
78 and returns NULL otherwise. */
80 static struct cli_interp
*
81 as_cli_interp (struct interp
*interp
)
83 return dynamic_cast<cli_interp
*> (interp
);
86 /* Longjmp-safe wrapper for "execute_command". */
87 static struct gdb_exception
safe_execute_command (struct ui_out
*uiout
,
93 Breakpoint hits should always be mirrored to a console. Deciding
94 what to mirror to a console wrt to breakpoints and random stops
95 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
96 clearly want to mirror the event to the console in this case. But
97 what about more complicated cases like "s&; thread n; s&", and one
98 of those steps spawning a new thread, and that thread hitting a
99 breakpoint? It's impossible in general to track whether the thread
100 had any relation to the commands that had been executed. So we
101 just simplify and always mirror breakpoints and random events to
104 OTOH, we should print the source line to the console when stepping
105 or other similar commands, iff the step was started by that console
106 (or in MI's case, by a console command), but not if it was started
107 with MI's -exec-step or similar. */
110 should_print_stop_to_console (struct interp
*console_interp
,
111 struct thread_info
*tp
)
113 if ((bpstat_what (tp
->control
.stop_bpstat
).main_action
114 == BPSTAT_WHAT_STOP_NOISY
)
115 || tp
->thread_fsm
== NULL
116 || tp
->thread_fsm
->command_interp
== console_interp
117 || !thread_fsm_finished_p (tp
->thread_fsm
))
122 /* Observers for several run control events. If the interpreter is
123 quiet (i.e., another interpreter is being run with
124 interpreter-exec), print nothing. */
126 /* Observer for the normal_stop notification. */
129 cli_on_normal_stop (struct bpstats
*bs
, int print_frame
)
134 SWITCH_THRU_ALL_UIS ()
136 struct interp
*interp
= top_level_interpreter ();
137 struct cli_interp
*cli
= as_cli_interp (interp
);
138 struct thread_info
*thread
;
143 thread
= inferior_thread ();
144 if (should_print_stop_to_console (interp
, thread
))
145 print_stop_event (cli
->cli_uiout
);
149 /* Observer for the signal_received notification. */
152 cli_on_signal_received (enum gdb_signal siggnal
)
154 SWITCH_THRU_ALL_UIS ()
156 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
161 print_signal_received_reason (cli
->cli_uiout
, siggnal
);
165 /* Observer for the end_stepping_range notification. */
168 cli_on_end_stepping_range (void)
170 SWITCH_THRU_ALL_UIS ()
172 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
177 print_end_stepping_range_reason (cli
->cli_uiout
);
181 /* Observer for the signalled notification. */
184 cli_on_signal_exited (enum gdb_signal siggnal
)
186 SWITCH_THRU_ALL_UIS ()
188 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
193 print_signal_exited_reason (cli
->cli_uiout
, siggnal
);
197 /* Observer for the exited notification. */
200 cli_on_exited (int exitstatus
)
202 SWITCH_THRU_ALL_UIS ()
204 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
209 print_exited_reason (cli
->cli_uiout
, exitstatus
);
213 /* Observer for the no_history notification. */
216 cli_on_no_history (void)
218 SWITCH_THRU_ALL_UIS ()
220 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
225 print_no_history_reason (cli
->cli_uiout
);
229 /* Observer for the sync_execution_done notification. */
232 cli_on_sync_execution_done (void)
234 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
239 display_gdb_prompt (NULL
);
242 /* Observer for the command_error notification. */
245 cli_on_command_error (void)
247 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
252 display_gdb_prompt (NULL
);
255 /* Observer for the user_selected_context_changed notification. */
258 cli_on_user_selected_context_changed (user_selected_what selection
)
260 /* This event is suppressed. */
261 if (cli_suppress_notification
.user_selected_context
)
264 thread_info
*tp
= inferior_ptid
!= null_ptid
? inferior_thread () : NULL
;
266 SWITCH_THRU_ALL_UIS ()
268 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
273 if (selection
& USER_SELECTED_INFERIOR
)
274 print_selected_inferior (cli
->cli_uiout
);
277 && ((selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
))))
278 print_selected_thread_frame (cli
->cli_uiout
, selection
);
282 /* pre_command_loop implementation. */
285 cli_interp_base::pre_command_loop ()
287 display_gdb_prompt (0);
290 /* These implement the cli out interpreter: */
293 cli_interp::init (bool top_level
)
298 cli_interp::resume ()
300 struct ui
*ui
= current_ui
;
301 struct cli_interp
*cli
= this;
302 struct ui_file
*stream
;
304 /*sync_execution = 1; */
306 /* gdb_setup_readline will change gdb_stdout. If the CLI was
307 previously writing to gdb_stdout, then set it to the new
308 gdb_stdout afterwards. */
310 stream
= cli
->cli_uiout
->set_stream (gdb_stdout
);
311 if (stream
!= gdb_stdout
)
313 cli
->cli_uiout
->set_stream (stream
);
317 gdb_setup_readline (1);
319 ui
->input_handler
= command_line_handler
;
322 cli
->cli_uiout
->set_stream (gdb_stdout
);
326 cli_interp::suspend ()
328 gdb_disable_readline ();
332 cli_interp::exec (const char *command_str
)
334 struct cli_interp
*cli
= this;
335 struct ui_file
*old_stream
;
336 struct gdb_exception result
;
338 /* gdb_stdout could change between the time cli_uiout was
339 initialized and now. Since we're probably using a different
340 interpreter which has a new ui_file for gdb_stdout, use that one
341 instead of the default.
343 It is important that it gets reset everytime, since the user
344 could set gdb to use a different interpreter. */
345 old_stream
= cli
->cli_uiout
->set_stream (gdb_stdout
);
346 result
= safe_execute_command (cli
->cli_uiout
, command_str
, 1);
347 cli
->cli_uiout
->set_stream (old_stream
);
352 cli_interp_base::supports_command_editing ()
357 static struct gdb_exception
358 safe_execute_command (struct ui_out
*command_uiout
, const char *command
,
361 struct gdb_exception e
= exception_none
;
363 /* Save and override the global ``struct ui_out'' builder. */
364 scoped_restore saved_uiout
= make_scoped_restore (¤t_uiout
,
369 execute_command (command
, from_tty
);
371 CATCH (exception
, RETURN_MASK_ALL
)
377 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
378 caller should print the exception. */
379 exception_print (gdb_stderr
, e
);
384 cli_interp::interp_ui_out ()
386 struct cli_interp
*cli
= (struct cli_interp
*) this;
388 return cli
->cli_uiout
;
391 /* These hold the pushed copies of the gdb output files.
392 If NULL then nothing has yet been pushed. */
393 struct saved_output_files
401 static saved_output_files saved_output
;
403 /* See cli-interp.h. */
406 cli_interp_base::set_logging (ui_file_up logfile
, bool logging_redirect
)
410 saved_output
.out
= gdb_stdout
;
411 saved_output
.err
= gdb_stderr
;
412 saved_output
.log
= gdb_stdlog
;
413 saved_output
.targ
= gdb_stdtarg
;
414 saved_output
.targerr
= gdb_stdtargerr
;
416 /* A raw pointer since ownership is transferred to
418 ui_file
*output
= make_logging_output (gdb_stdout
,
424 gdb_stdtarg
= output
;
425 gdb_stdtargerr
= output
;
429 /* Only delete one of the files -- they are all set to the same
433 gdb_stdout
= saved_output
.out
;
434 gdb_stderr
= saved_output
.err
;
435 gdb_stdlog
= saved_output
.log
;
436 gdb_stdtarg
= saved_output
.targ
;
437 gdb_stdtargerr
= saved_output
.targerr
;
439 saved_output
.out
= NULL
;
440 saved_output
.err
= NULL
;
441 saved_output
.log
= NULL
;
442 saved_output
.targ
= NULL
;
443 saved_output
.targerr
= NULL
;
447 /* Factory for CLI interpreters. */
449 static struct interp
*
450 cli_interp_factory (const char *name
)
452 return new cli_interp (name
);
455 /* Standard gdb initialization hook. */
458 _initialize_cli_interp (void)
460 interp_factory_register (INTERP_CONSOLE
, cli_interp_factory
);
462 /* If changing this, remember to update tui-interp.c as well. */
463 gdb::observers::normal_stop
.attach (cli_on_normal_stop
);
464 gdb::observers::end_stepping_range
.attach (cli_on_end_stepping_range
);
465 gdb::observers::signal_received
.attach (cli_on_signal_received
);
466 gdb::observers::signal_exited
.attach (cli_on_signal_exited
);
467 gdb::observers::exited
.attach (cli_on_exited
);
468 gdb::observers::no_history
.attach (cli_on_no_history
);
469 gdb::observers::sync_execution_done
.attach (cli_on_sync_execution_done
);
470 gdb::observers::command_error
.attach (cli_on_command_error
);
471 gdb::observers::user_selected_context_changed
.attach
472 (cli_on_user_selected_context_changed
);