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" */
28 #include "observable.h"
29 #include "gdbthread.h"
30 #include "thread-fsm.h"
33 cli_interp_base::cli_interp_base (const char *name
)
37 cli_interp_base::~cli_interp_base ()
40 /* The console interpreter. */
42 class cli_interp final
: public cli_interp_base
45 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 cli_interp::~cli_interp ()
70 /* Suppress notification struct. */
71 struct cli_suppress_notification cli_suppress_notification
=
73 0 /* user_selected_context_changed */
76 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
77 and returns NULL otherwise. */
79 static struct cli_interp
*
80 as_cli_interp (struct interp
*interp
)
82 return dynamic_cast<cli_interp
*> (interp
);
85 /* Longjmp-safe wrapper for "execute_command". */
86 static struct gdb_exception
safe_execute_command (struct ui_out
*uiout
,
92 Breakpoint hits should always be mirrored to a console. Deciding
93 what to mirror to a console wrt to breakpoints and random stops
94 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
95 clearly want to mirror the event to the console in this case. But
96 what about more complicated cases like "s&; thread n; s&", and one
97 of those steps spawning a new thread, and that thread hitting a
98 breakpoint? It's impossible in general to track whether the thread
99 had any relation to the commands that had been executed. So we
100 just simplify and always mirror breakpoints and random events to
103 OTOH, we should print the source line to the console when stepping
104 or other similar commands, iff the step was started by that console
105 (or in MI's case, by a console command), but not if it was started
106 with MI's -exec-step or similar. */
109 should_print_stop_to_console (struct interp
*console_interp
,
110 struct thread_info
*tp
)
112 if ((bpstat_what (tp
->control
.stop_bpstat
).main_action
113 == BPSTAT_WHAT_STOP_NOISY
)
114 || tp
->thread_fsm
== NULL
115 || tp
->thread_fsm
->command_interp
== console_interp
116 || !tp
->thread_fsm
->finished_p ())
121 /* Observers for several run control events. If the interpreter is
122 quiet (i.e., another interpreter is being run with
123 interpreter-exec), print nothing. */
125 /* Observer for the normal_stop notification. */
128 cli_on_normal_stop (struct bpstats
*bs
, int print_frame
)
133 SWITCH_THRU_ALL_UIS ()
135 struct interp
*interp
= top_level_interpreter ();
136 struct cli_interp
*cli
= as_cli_interp (interp
);
137 struct thread_info
*thread
;
142 thread
= inferior_thread ();
143 if (should_print_stop_to_console (interp
, thread
))
144 print_stop_event (cli
->cli_uiout
);
148 /* Observer for the signal_received notification. */
151 cli_on_signal_received (enum gdb_signal siggnal
)
153 SWITCH_THRU_ALL_UIS ()
155 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
160 print_signal_received_reason (cli
->cli_uiout
, siggnal
);
164 /* Observer for the end_stepping_range notification. */
167 cli_on_end_stepping_range (void)
169 SWITCH_THRU_ALL_UIS ()
171 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
176 print_end_stepping_range_reason (cli
->cli_uiout
);
180 /* Observer for the signalled notification. */
183 cli_on_signal_exited (enum gdb_signal siggnal
)
185 SWITCH_THRU_ALL_UIS ()
187 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
192 print_signal_exited_reason (cli
->cli_uiout
, siggnal
);
196 /* Observer for the exited notification. */
199 cli_on_exited (int exitstatus
)
201 SWITCH_THRU_ALL_UIS ()
203 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
208 print_exited_reason (cli
->cli_uiout
, exitstatus
);
212 /* Observer for the no_history notification. */
215 cli_on_no_history (void)
217 SWITCH_THRU_ALL_UIS ()
219 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
224 print_no_history_reason (cli
->cli_uiout
);
228 /* Observer for the sync_execution_done notification. */
231 cli_on_sync_execution_done (void)
233 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
238 display_gdb_prompt (NULL
);
241 /* Observer for the command_error notification. */
244 cli_on_command_error (void)
246 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
251 display_gdb_prompt (NULL
);
254 /* Observer for the user_selected_context_changed notification. */
257 cli_on_user_selected_context_changed (user_selected_what selection
)
259 /* This event is suppressed. */
260 if (cli_suppress_notification
.user_selected_context
)
263 thread_info
*tp
= inferior_ptid
!= null_ptid
? inferior_thread () : NULL
;
265 SWITCH_THRU_ALL_UIS ()
267 struct cli_interp
*cli
= as_cli_interp (top_level_interpreter ());
272 if (selection
& USER_SELECTED_INFERIOR
)
273 print_selected_inferior (cli
->cli_uiout
);
276 && ((selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
))))
277 print_selected_thread_frame (cli
->cli_uiout
, selection
);
281 /* pre_command_loop implementation. */
284 cli_interp_base::pre_command_loop ()
286 display_gdb_prompt (0);
289 /* These implement the cli out interpreter: */
292 cli_interp::init (bool top_level
)
297 cli_interp::resume ()
299 struct ui
*ui
= current_ui
;
300 struct cli_interp
*cli
= this;
301 struct ui_file
*stream
;
303 /*sync_execution = 1; */
305 /* gdb_setup_readline will change gdb_stdout. If the CLI was
306 previously writing to gdb_stdout, then set it to the new
307 gdb_stdout afterwards. */
309 stream
= cli
->cli_uiout
->set_stream (gdb_stdout
);
310 if (stream
!= gdb_stdout
)
312 cli
->cli_uiout
->set_stream (stream
);
316 gdb_setup_readline (1);
318 ui
->input_handler
= command_line_handler
;
321 cli
->cli_uiout
->set_stream (gdb_stdout
);
325 cli_interp::suspend ()
327 gdb_disable_readline ();
331 cli_interp::exec (const char *command_str
)
333 struct cli_interp
*cli
= this;
334 struct ui_file
*old_stream
;
335 struct gdb_exception result
;
337 /* gdb_stdout could change between the time cli_uiout was
338 initialized and now. Since we're probably using a different
339 interpreter which has a new ui_file for gdb_stdout, use that one
340 instead of the default.
342 It is important that it gets reset everytime, since the user
343 could set gdb to use a different interpreter. */
344 old_stream
= cli
->cli_uiout
->set_stream (gdb_stdout
);
345 result
= safe_execute_command (cli
->cli_uiout
, command_str
, 1);
346 cli
->cli_uiout
->set_stream (old_stream
);
351 cli_interp_base::supports_command_editing ()
356 static struct gdb_exception
357 safe_execute_command (struct ui_out
*command_uiout
, const char *command
,
360 struct gdb_exception e
;
362 /* Save and override the global ``struct ui_out'' builder. */
363 scoped_restore saved_uiout
= make_scoped_restore (¤t_uiout
,
368 execute_command (command
, from_tty
);
370 catch (gdb_exception
&exception
)
372 e
= std::move (exception
);
375 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
376 caller should print the exception. */
377 exception_print (gdb_stderr
, e
);
382 cli_interp::interp_ui_out ()
384 struct cli_interp
*cli
= (struct cli_interp
*) this;
386 return cli
->cli_uiout
;
389 /* These hold the pushed copies of the gdb output files.
390 If NULL then nothing has yet been pushed. */
391 struct saved_output_files
399 static saved_output_files saved_output
;
401 /* See cli-interp.h. */
404 cli_interp_base::set_logging (ui_file_up logfile
, bool logging_redirect
)
406 if (logfile
!= nullptr)
408 saved_output
.out
= gdb_stdout
;
409 saved_output
.err
= gdb_stderr
;
410 saved_output
.log
= gdb_stdlog
;
411 saved_output
.targ
= gdb_stdtarg
;
412 saved_output
.targerr
= gdb_stdtargerr
;
414 /* If logging is being redirected, then grab logfile. */
415 ui_file
*logfile_p
= nullptr;
416 if (logging_redirect
)
417 logfile_p
= logfile
.release ();
419 /* If logging is not being redirected, then a tee containing both the
420 logfile and stdout. */
421 ui_file
*tee
= nullptr;
422 if (!logging_redirect
)
423 tee
= new tee_file (gdb_stdout
, std::move (logfile
));
425 gdb_stdout
= logging_redirect
? logfile_p
: tee
;
426 gdb_stdlog
= logging_redirect
? logfile_p
: tee
;
427 gdb_stderr
= logging_redirect
? logfile_p
: tee
;
428 gdb_stdtarg
= logging_redirect
? logfile_p
: tee
;
429 gdb_stdtargerr
= logging_redirect
? logfile_p
: tee
;
433 /* Only delete one of the files -- they are all set to the same
437 gdb_stdout
= saved_output
.out
;
438 gdb_stderr
= saved_output
.err
;
439 gdb_stdlog
= saved_output
.log
;
440 gdb_stdtarg
= saved_output
.targ
;
441 gdb_stdtargerr
= saved_output
.targerr
;
443 saved_output
.out
= nullptr;
444 saved_output
.err
= nullptr;
445 saved_output
.log
= nullptr;
446 saved_output
.targ
= nullptr;
447 saved_output
.targerr
= nullptr;
451 /* Factory for CLI interpreters. */
453 static struct interp
*
454 cli_interp_factory (const char *name
)
456 return new cli_interp (name
);
459 /* Standard gdb initialization hook. */
462 _initialize_cli_interp (void)
464 interp_factory_register (INTERP_CONSOLE
, cli_interp_factory
);
466 /* If changing this, remember to update tui-interp.c as well. */
467 gdb::observers::normal_stop
.attach (cli_on_normal_stop
);
468 gdb::observers::end_stepping_range
.attach (cli_on_end_stepping_range
);
469 gdb::observers::signal_received
.attach (cli_on_signal_received
);
470 gdb::observers::signal_exited
.attach (cli_on_signal_exited
);
471 gdb::observers::exited
.attach (cli_on_exited
);
472 gdb::observers::no_history
.attach (cli_on_no_history
);
473 gdb::observers::sync_execution_done
.attach (cli_on_sync_execution_done
);
474 gdb::observers::command_error
.attach (cli_on_command_error
);
475 gdb::observers::user_selected_context_changed
.attach
476 (cli_on_user_selected_context_changed
);