1 /* CLI Definitions for GDB, the GNU debugger.
3 Copyright (C) 2002-2022 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 /* Returns the INTERP's data cast as cli_interp if INTERP is a CLI,
74 and returns NULL otherwise. */
76 static struct cli_interp
*
77 as_cli_interp (struct interp
*interp
)
79 return dynamic_cast<cli_interp
*> (interp
);
82 /* Longjmp-safe wrapper for "execute_command". */
83 static struct gdb_exception
safe_execute_command (struct ui_out
*uiout
,
89 Breakpoint hits should always be mirrored to a console. Deciding
90 what to mirror to a console wrt to breakpoints and random stops
91 gets messy real fast. E.g., say "s" trips on a breakpoint. We'd
92 clearly want to mirror the event to the console in this case. But
93 what about more complicated cases like "s&; thread n; s&", and one
94 of those steps spawning a new thread, and that thread hitting a
95 breakpoint? It's impossible in general to track whether the thread
96 had any relation to the commands that had been executed. So we
97 just simplify and always mirror breakpoints and random events to
100 OTOH, we should print the source line to the console when stepping
101 or other similar commands, iff the step was started by that console
102 (or in MI's case, by a console command), but not if it was started
103 with MI's -exec-step or similar. */
106 should_print_stop_to_console (struct interp
*console_interp
,
107 struct thread_info
*tp
)
109 if ((bpstat_what (tp
->control
.stop_bpstat
).main_action
110 == BPSTAT_WHAT_STOP_NOISY
)
111 || tp
->thread_fsm () == nullptr
112 || tp
->thread_fsm ()->command_interp
== console_interp
113 || !tp
->thread_fsm ()->finished_p ())
118 /* Observers for several run control events. If the interpreter is
119 quiet (i.e., another interpreter is being run with
120 interpreter-exec), print nothing. */
122 /* Observer for the normal_stop notification. */
125 cli_on_normal_stop (struct bpstat
*bs
, int print_frame
)
130 /* This event is suppressed. */
131 if (cli_suppress_notification
.normal_stop
)
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
;
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 (gdb_exception
&exception
)
373 e
= std::move (exception
);
376 /* FIXME: cagney/2005-01-13: This shouldn't be needed. Instead the
377 caller should print the exception. */
378 exception_print (gdb_stderr
, e
);
383 cli_interp::interp_ui_out ()
385 struct cli_interp
*cli
= (struct cli_interp
*) this;
387 return cli
->cli_uiout
;
390 /* These hold the pushed copies of the gdb output files.
391 If NULL then nothing has yet been pushed. */
392 struct saved_output_files
399 ui_file_up file_to_delete
;
400 ui_file_up log_to_delete
;
402 static std::unique_ptr
<saved_output_files
> saved_output
;
404 /* See cli-interp.h. */
407 cli_interp_base::set_logging (ui_file_up logfile
, bool logging_redirect
,
410 if (logfile
!= nullptr)
412 saved_output
.reset (new saved_output_files
);
413 saved_output
->out
= gdb_stdout
;
414 saved_output
->err
= gdb_stderr
;
415 saved_output
->log
= gdb_stdlog
;
416 saved_output
->targ
= gdb_stdtarg
;
417 saved_output
->targerr
= gdb_stdtargerr
;
419 /* If something is not being redirected, then a tee containing both the
420 logfile and stdout. */
421 ui_file
*logfile_p
= logfile
.get ();
422 ui_file
*tee
= nullptr;
423 if (!logging_redirect
|| !debug_redirect
)
425 tee
= new tee_file (gdb_stdout
, std::move (logfile
));
426 saved_output
->file_to_delete
.reset (tee
);
429 saved_output
->file_to_delete
= std::move (logfile
);
431 saved_output
->log_to_delete
.reset
432 (new timestamped_file (debug_redirect
? logfile_p
: tee
));
434 gdb_stdout
= logging_redirect
? logfile_p
: tee
;
435 gdb_stdlog
= saved_output
->log_to_delete
.get ();
436 gdb_stderr
= logging_redirect
? logfile_p
: tee
;
437 gdb_stdtarg
= logging_redirect
? logfile_p
: tee
;
438 gdb_stdtargerr
= logging_redirect
? logfile_p
: tee
;
442 gdb_stdout
= saved_output
->out
;
443 gdb_stderr
= saved_output
->err
;
444 gdb_stdlog
= saved_output
->log
;
445 gdb_stdtarg
= saved_output
->targ
;
446 gdb_stdtargerr
= saved_output
->targerr
;
448 saved_output
.reset (nullptr);
452 /* Factory for CLI interpreters. */
454 static struct interp
*
455 cli_interp_factory (const char *name
)
457 return new cli_interp (name
);
460 /* Standard gdb initialization hook. */
462 void _initialize_cli_interp ();
464 _initialize_cli_interp ()
466 interp_factory_register (INTERP_CONSOLE
, cli_interp_factory
);
468 /* If changing this, remember to update tui-interp.c as well. */
469 gdb::observers::normal_stop
.attach (cli_on_normal_stop
, "cli-interp");
470 gdb::observers::end_stepping_range
.attach (cli_on_end_stepping_range
,
472 gdb::observers::signal_received
.attach (cli_on_signal_received
, "cli-interp");
473 gdb::observers::signal_exited
.attach (cli_on_signal_exited
, "cli-interp");
474 gdb::observers::exited
.attach (cli_on_exited
, "cli-interp");
475 gdb::observers::no_history
.attach (cli_on_no_history
, "cli-interp");
476 gdb::observers::sync_execution_done
.attach (cli_on_sync_execution_done
,
478 gdb::observers::command_error
.attach (cli_on_command_error
, "cli-interp");
479 gdb::observers::user_selected_context_changed
.attach
480 (cli_on_user_selected_context_changed
, "cli-interp");