1 /* TUI Interpreter definitions for GDB, the GNU debugger.
3 Copyright (C) 2003-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/cli-interp.h"
24 #include "event-top.h"
25 #include "event-loop.h"
28 #include "tui/tui-data.h"
29 #include "readline/readline.h"
30 #include "tui/tui-win.h"
32 #include "tui/tui-io.h"
34 #include "observable.h"
35 #include "gdbthread.h"
39 /* Set to 1 when the TUI mode must be activated when we first start
41 static int tui_start_enabled
= 0;
43 class tui_interp final
: public cli_interp_base
46 explicit tui_interp (const char *name
)
47 : cli_interp_base (name
)
50 void init (bool top_level
) override
;
51 void resume () override
;
52 void suspend () override
;
53 gdb_exception
exec (const char *command_str
) override
;
54 ui_out
*interp_ui_out () override
;
57 /* Returns the INTERP if the INTERP is a TUI, and returns NULL
61 as_tui_interp (struct interp
*interp
)
63 return dynamic_cast<tui_interp
*> (interp
);
66 /* Cleanup the tui before exiting. */
71 /* Disable the tui. Curses mode is left leaving the screen in a
72 clean state (see endwin()). */
76 /* Observers for several run control events. If the interpreter is
77 quiet (i.e., another interpreter is being run with
78 interpreter-exec), print nothing. */
80 /* Observer for the normal_stop notification. */
83 tui_on_normal_stop (struct bpstats
*bs
, int print_frame
)
88 SWITCH_THRU_ALL_UIS ()
90 struct interp
*interp
= top_level_interpreter ();
91 struct interp
*tui
= as_tui_interp (interp
);
92 struct thread_info
*thread
;
97 thread
= inferior_thread ();
98 if (should_print_stop_to_console (interp
, thread
))
99 print_stop_event (tui
->interp_ui_out ());
103 /* Observer for the signal_received notification. */
106 tui_on_signal_received (enum gdb_signal siggnal
)
108 SWITCH_THRU_ALL_UIS ()
110 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
115 print_signal_received_reason (tui
->interp_ui_out (), siggnal
);
119 /* Observer for the end_stepping_range notification. */
122 tui_on_end_stepping_range (void)
124 SWITCH_THRU_ALL_UIS ()
126 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
131 print_end_stepping_range_reason (tui
->interp_ui_out ());
135 /* Observer for the signal_exited notification. */
138 tui_on_signal_exited (enum gdb_signal siggnal
)
140 SWITCH_THRU_ALL_UIS ()
142 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
147 print_signal_exited_reason (tui
->interp_ui_out (), siggnal
);
151 /* Observer for the exited notification. */
154 tui_on_exited (int exitstatus
)
156 SWITCH_THRU_ALL_UIS ()
158 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
163 print_exited_reason (tui
->interp_ui_out (), exitstatus
);
167 /* Observer for the no_history notification. */
170 tui_on_no_history (void)
172 SWITCH_THRU_ALL_UIS ()
174 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
179 print_no_history_reason (tui
->interp_ui_out ());
183 /* Observer for the sync_execution_done notification. */
186 tui_on_sync_execution_done (void)
188 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
193 display_gdb_prompt (NULL
);
196 /* Observer for the command_error notification. */
199 tui_on_command_error (void)
201 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
206 display_gdb_prompt (NULL
);
209 /* Observer for the user_selected_context_changed notification. */
212 tui_on_user_selected_context_changed (user_selected_what selection
)
214 /* This event is suppressed. */
215 if (cli_suppress_notification
.user_selected_context
)
218 thread_info
*tp
= inferior_ptid
!= null_ptid
? inferior_thread () : NULL
;
220 SWITCH_THRU_ALL_UIS ()
222 struct interp
*tui
= as_tui_interp (top_level_interpreter ());
227 if (selection
& USER_SELECTED_INFERIOR
)
228 print_selected_inferior (tui
->interp_ui_out ());
231 && ((selection
& (USER_SELECTED_THREAD
| USER_SELECTED_FRAME
))))
232 print_selected_thread_frame (tui
->interp_ui_out (), selection
);
237 /* These implement the TUI interpreter. */
240 tui_interp::init (bool top_level
)
242 /* Install exit handler to leave the screen in a good shape. */
245 tui_initialize_static_data ();
247 tui_initialize_io ();
248 tui_initialize_win ();
249 if (ui_file_isatty (gdb_stdout
))
250 tui_initialize_readline ();
254 tui_interp::resume ()
256 struct ui
*ui
= current_ui
;
257 struct ui_file
*stream
;
259 /* gdb_setup_readline will change gdb_stdout. If the TUI was
260 previously writing to gdb_stdout, then set it to the new
261 gdb_stdout afterwards. */
263 stream
= tui_old_uiout
->set_stream (gdb_stdout
);
264 if (stream
!= gdb_stdout
)
266 tui_old_uiout
->set_stream (stream
);
270 gdb_setup_readline (1);
272 ui
->input_handler
= command_line_handler
;
275 tui_old_uiout
->set_stream (gdb_stdout
);
277 if (tui_start_enabled
)
282 tui_interp::suspend ()
284 tui_start_enabled
= tui_active
;
289 tui_interp::interp_ui_out ()
294 return tui_old_uiout
;
298 tui_interp::exec (const char *command_str
)
300 internal_error (__FILE__
, __LINE__
, _("tui_exec called"));
304 /* Factory for TUI interpreters. */
306 static struct interp
*
307 tui_interp_factory (const char *name
)
309 return new tui_interp (name
);
313 _initialize_tui_interp (void)
315 interp_factory_register (INTERP_TUI
, tui_interp_factory
);
317 if (interpreter_p
&& strcmp (interpreter_p
, INTERP_TUI
) == 0)
318 tui_start_enabled
= 1;
320 if (interpreter_p
&& strcmp (interpreter_p
, INTERP_CONSOLE
) == 0)
322 xfree (interpreter_p
);
323 interpreter_p
= xstrdup (INTERP_TUI
);
326 /* If changing this, remember to update cli-interp.c as well. */
327 gdb::observers::normal_stop
.attach (tui_on_normal_stop
);
328 gdb::observers::signal_received
.attach (tui_on_signal_received
);
329 gdb::observers::end_stepping_range
.attach (tui_on_end_stepping_range
);
330 gdb::observers::signal_exited
.attach (tui_on_signal_exited
);
331 gdb::observers::exited
.attach (tui_on_exited
);
332 gdb::observers::no_history
.attach (tui_on_no_history
);
333 gdb::observers::sync_execution_done
.attach (tui_on_sync_execution_done
);
334 gdb::observers::command_error
.attach (tui_on_command_error
);
335 gdb::observers::user_selected_context_changed
.attach
336 (tui_on_user_selected_context_changed
);