GDB (xrefs)
Loading...
Searching...
No Matches
tui.c
Go to the documentation of this file.
1/* General functions for the WDB TUI.
2
3 Copyright (C) 1998-2023 Free Software Foundation, Inc.
4
5 Contributed by Hewlett-Packard Company.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "gdbcmd.h"
24#include "tui/tui.h"
25#include "tui/tui-hooks.h"
26#include "tui/tui-command.h"
27#include "tui/tui-data.h"
28#include "tui/tui-layout.h"
29#include "tui/tui-io.h"
30#include "tui/tui-regs.h"
31#include "tui/tui-stack.h"
32#include "tui/tui-win.h"
33#include "tui/tui-wingeneral.h"
34#include "tui/tui-winsource.h"
35#include "tui/tui-source.h"
36#include "target.h"
37#include "frame.h"
38#include "breakpoint.h"
39#include "inferior.h"
40#include "symtab.h"
41#include "source.h"
42#include "terminal.h"
43#include "top.h"
44#include "ui.h"
45
46#include <ctype.h>
47#include <signal.h>
48#include <fcntl.h>
49#include <setjmp.h>
50
51#include "gdb_curses.h"
52#include "interps.h"
53
54/* See tui.h. */
55
56bool debug_tui = false;
57
58/* Implement 'show debug tui'. */
59
60static void
61show_tui_debug (struct ui_file *file, int from_tty,
62 struct cmd_list_element *c, const char *value)
63{
64 gdb_printf (file, _("TUI debugging is \"%s\".\n"), value);
65}
66
67/* This redefines CTRL if it is not already defined, so it must come
68 after terminal state releated include files like <term.h> and
69 "gdb_curses.h". */
70#include "readline/readline.h"
71
72/* Tells whether the TUI is active or not. */
73bool tui_active = false;
74static bool tui_finish_init = true;
75
77
79{
80 unsigned char key;
81 const char *cmd;
82};
83
84/* Key mapping to gdb commands when the TUI is using the single key
85 mode. */
86static const struct tui_char_command tui_commands[] = {
87 { 'c', "continue" },
88 { 'd', "down" },
89 { 'f', "finish" },
90 { 'n', "next" },
91 { 'o', "nexti" },
92 { 'r', "run" },
93 { 's', "step" },
94 { 'i', "stepi" },
95 { 'u', "up" },
96 { 'v', "info locals" },
97 { 'w', "where" },
98 { 0, 0 },
99};
100
101static Keymap tui_keymap;
103
104/* TUI readline command.
105 Switch the output mode between TUI/standard gdb. */
106static int
107tui_rl_switch_mode (int notused1, int notused2)
108{
109
110 /* Don't let exceptions escape. We're in the middle of a readline
111 callback that isn't prepared for that. */
112 try
113 {
114 if (tui_active)
115 {
116 tui_disable ();
117 rl_prep_terminal (0);
118 }
119 else
120 {
121 /* If tui_enable throws, we'll re-prep below. */
122 rl_deprep_terminal ();
123 tui_enable ();
124 }
125 }
126 catch (const gdb_exception_forced_quit &ex)
127 {
128 /* Ideally, we'd do a 'throw' here, but as noted above, we can't
129 do that, so, instead, we'll set the necessary flags so that
130 a later QUIT check will restart the forced quit. */
132 }
133 catch (const gdb_exception &ex)
134 {
136
137 if (!tui_active)
138 rl_prep_terminal (0);
139 }
140
141 /* Clear the readline in case switching occurred in middle of
142 something. */
143 if (rl_end)
144 rl_kill_text (0, rl_end);
145
146 /* Since we left the curses mode, the terminal mode is restored to
147 some previous state. That state may not be suitable for readline
148 to work correctly (it may be restored in line mode). We force an
149 exit of the current readline so that readline is re-entered and
150 it will be able to setup the terminal for its needs. By
151 re-entering in readline, we also redisplay its prompt in the
152 non-curses mode. */
153 rl_newline (1, '\n');
154
155 /* Make sure the \n we are returning does not repeat the last
156 command. */
157 dont_repeat ();
158 return 0;
159}
160
161/* TUI readline command.
162 Change the TUI layout to show a next layout.
163 This function is bound to CTRL-X 2. It is intended to provide
164 a functionality close to the Emacs split-window command. */
165static int
166tui_rl_change_windows (int notused1, int notused2)
167{
168 if (!tui_active)
169 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
170
171 if (tui_active)
173
174 return 0;
175}
176
177/* TUI readline command.
178 Delete the second TUI window to only show one. */
179static int
180tui_rl_delete_other_windows (int notused1, int notused2)
181{
182 if (!tui_active)
183 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
184
185 if (tui_active)
187
188 return 0;
189}
190
191/* TUI readline command.
192 Switch the active window to give the focus to a next window. */
193static int
194tui_rl_other_window (int count, int key)
195{
196 struct tui_win_info *win_info;
197
198 if (!tui_active)
199 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
200
201 win_info = tui_next_win (tui_win_with_focus ());
202 if (win_info)
203 tui_set_win_focus_to (win_info);
204 return 0;
205}
206
207/* TUI readline command.
208 Execute the gdb command bound to the specified key. */
209static int
210tui_rl_command_key (int count, int key)
211{
212 int i;
213
215 for (i = 0; tui_commands[i].cmd; i++)
216 {
217 if (tui_commands[i].key == key)
218 {
219 /* Insert the command in the readline buffer.
220 Avoid calling the gdb command here since it creates
221 a possible recursion on readline if prompt_for_continue
222 is called (See PR 9584). The command will also appear
223 in the readline history which turns out to be better. */
224 rl_insert_text (tui_commands[i].cmd);
225 rl_newline (1, '\n');
226
227 /* Switch to gdb command mode while executing the command.
228 This way the gdb's continue prompt will be displayed. */
230 return 0;
231 }
232 }
233 return 0;
234}
235
236/* TUI readline command.
237 Temporarily leave the TUI SingleKey mode to allow editing
238 a gdb command with the normal readline. Once the command
239 is executed, the TUI SingleKey mode is installed back. */
240static int
241tui_rl_command_mode (int count, int key)
242{
244 return rl_insert (count, key);
245}
246
247/* TUI readline command.
248 Switch between TUI SingleKey mode and gdb readline editing. */
249static int
250tui_rl_next_keymap (int notused1, int notused2)
251{
252 if (!tui_active)
253 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
254
255 if (rl_end)
256 {
257 rl_end = 0;
258 rl_point = 0;
259 rl_mark = 0;
260 }
261
264 return 0;
265}
266
267/* Readline hook to redisplay ourself the gdb prompt.
268 In the SingleKey mode, the prompt is not printed so that
269 the command window is cleaner. It will be displayed if
270 we temporarily leave the SingleKey mode. */
271static int
279
280/* Change the TUI key mode by installing the appropriate readline
281 keymap. */
282void
290
291/* Initialize readline and configure the keymap for the switching
292 key shortcut. */
293void
295{
296 static bool initialized;
297
298 if (initialized)
299 return;
300 initialized = true;
301
302 int i;
303 Keymap tui_ctlx_keymap;
304
305 rl_add_defun ("tui-switch-mode", tui_rl_switch_mode, -1);
306 rl_add_defun ("next-keymap", tui_rl_next_keymap, -1);
307 rl_add_defun ("tui-delete-other-windows", tui_rl_delete_other_windows, -1);
308 rl_add_defun ("tui-change-windows", tui_rl_change_windows, -1);
309 rl_add_defun ("tui-other-window", tui_rl_other_window, -1);
310
311 tui_keymap = rl_make_bare_keymap ();
312
313 /* The named keymap feature was added in Readline 8.0. */
314#if RL_READLINE_VERSION >= 0x800
315 rl_set_keymap_name ("SingleKey", tui_keymap);
316#endif
317
318 tui_ctlx_keymap = rl_make_bare_keymap ();
319 tui_readline_standard_keymap = rl_get_keymap ();
320
321 for (i = 0; tui_commands[i].cmd; i++)
322 rl_bind_key_in_map (tui_commands[i].key, tui_rl_command_key, tui_keymap);
323
324 rl_generic_bind (ISKMAP, "\\C-x", (char*) tui_ctlx_keymap, tui_keymap);
325
326 /* Bind all other keys to tui_rl_command_mode so that we switch
327 temporarily from SingleKey mode and can enter a gdb command. */
328 for (i = ' '; i < 0x7f; i++)
329 {
330 int j;
331
332 for (j = 0; tui_commands[j].cmd; j++)
333 if (tui_commands[j].key == i)
334 break;
335
336 if (tui_commands[j].cmd)
337 continue;
338
339 rl_bind_key_in_map (i, tui_rl_command_mode, tui_keymap);
340 }
341
342 rl_bind_key_in_map ('a', tui_rl_switch_mode, emacs_ctlx_keymap);
343 rl_bind_key_in_map ('a', tui_rl_switch_mode, tui_ctlx_keymap);
344 rl_bind_key_in_map ('A', tui_rl_switch_mode, emacs_ctlx_keymap);
345 rl_bind_key_in_map ('A', tui_rl_switch_mode, tui_ctlx_keymap);
346 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, emacs_ctlx_keymap);
347 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, tui_ctlx_keymap);
348 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, emacs_ctlx_keymap);
349 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, tui_ctlx_keymap);
350 rl_bind_key_in_map ('2', tui_rl_change_windows, emacs_ctlx_keymap);
351 rl_bind_key_in_map ('2', tui_rl_change_windows, tui_ctlx_keymap);
352 rl_bind_key_in_map ('o', tui_rl_other_window, emacs_ctlx_keymap);
353 rl_bind_key_in_map ('o', tui_rl_other_window, tui_ctlx_keymap);
354 rl_bind_key_in_map ('q', tui_rl_next_keymap, tui_keymap);
355 rl_bind_key_in_map ('s', tui_rl_next_keymap, emacs_ctlx_keymap);
356 rl_bind_key_in_map ('s', tui_rl_next_keymap, tui_ctlx_keymap);
357
358 /* Initialize readline after the above. */
359 rl_initialize ();
360}
361
362/* Return the TERM variable from the environment, or "<unset>"
363 if not set. */
364
365static const char *
367{
368 const char *term;
369
370 term = getenv ("TERM");
371 if (term != NULL)
372 return term;
373 return "<unset>";
374}
375
376/* Enter in the tui mode (curses).
377 When in normal mode, it installs the tui hooks in gdb, redirects
378 the gdb output, configures the readline to work in tui mode.
379 When in curses mode, it does nothing. */
380void
382{
384
385 if (tui_active)
386 return;
387
388 /* To avoid to initialize curses when gdb starts, there is a deferred
389 curses initialization. This initialization is made only once
390 and the first time the curses mode is entered. */
391 if (tui_finish_init)
392 {
393 WINDOW *w;
394 SCREEN *s;
395#ifndef __MINGW32__
396 const char *cap;
397#endif
398 const char *interp;
399
400 /* If the top level interpreter is not the console/tui (e.g.,
401 MI), enabling curses will certainly lose. */
403 if (strcmp (interp, INTERP_TUI) != 0)
404 error (_("Cannot enable the TUI when the interpreter is '%s'"), interp);
405
406 /* Don't try to setup curses (and print funny control
407 characters) if we're not outputting to a terminal. */
408 if (!gdb_stderr->isatty ())
409 error (_("Cannot enable the TUI when output is not a terminal"));
410
411 s = newterm (NULL, stdout, stdin);
412#ifdef __MINGW32__
413 /* The MinGW port of ncurses requires $TERM to be unset in order
414 to activate the Windows console driver. */
415 if (s == NULL)
416 s = newterm ((char *) "unknown", stdout, stdin);
417#endif
418 if (s == NULL)
419 {
420 error (_("Cannot enable the TUI: error opening terminal [TERM=%s]"),
421 gdb_getenv_term ());
422 }
423 w = stdscr;
424 if (has_colors ())
425 {
426#ifdef HAVE_USE_DEFAULT_COLORS
427 /* Ncurses extension to help with resetting to the default
428 color. */
429 use_default_colors ();
430#endif
431 start_color ();
432 }
433
434 /* Check required terminal capabilities. The MinGW port of
435 ncurses does have them, but doesn't expose them through "cup". */
436#ifndef __MINGW32__
437 cap = tigetstr ((char *) "cup");
438 if (cap == NULL || cap == (char *) -1 || *cap == '\0')
439 {
440 endwin ();
441 delscreen (s);
442 error (_("Cannot enable the TUI: "
443 "terminal doesn't support cursor addressing [TERM=%s]"),
444 gdb_getenv_term ());
445 }
446#endif
447
448 /* We must mark the tui sub-system active before trying to setup the
449 current layout as tui windows defined by an extension language
450 rely on this flag being true in order to know that the window
451 they are creating is currently valid. */
452 tui_active = true;
453
454 cbreak ();
455 noecho ();
456 /* timeout (1); */
457 nodelay(w, FALSE);
458 nl();
459 keypad (w, TRUE);
462 def_prog_mode ();
463
467 keypad (TUI_CMD_WIN->handle.get (), TRUE);
468 wrefresh (TUI_CMD_WIN->handle.get ());
469 tui_finish_init = false;
470 }
471 else
472 {
473 /* Save the current gdb setting of the terminal.
474 Curses will restore this state when endwin() is called. */
475 def_shell_mode ();
476 clearok (stdscr, TRUE);
477
478 tui_active = true;
479 }
480
481 gdb_assert (tui_active);
482
485
486 tui_setup_io (1);
487
488 /* Resize windows before anything might display/refresh a
489 window. */
490 if (tui_win_resized ())
491 {
494 }
495
498 else
500
501 /* Install the TUI specific hooks. This must be done after the call to
502 tui_display_main so that we don't detect the symtab changed event it
503 can cause. */
505 rl_startup_hook = tui_rl_startup_hook;
506
507 /* Restore TUI keymap. */
509
510 /* Refresh the screen. */
512
513 /* Update gdb's knowledge of its terminal. */
516}
517
518/* Leave the tui mode.
519 Remove the tui hooks and configure the gdb output and readline
520 back to their original state. The curses mode is left so that
521 the terminal setting is restored to the point when we entered. */
522void
524{
526
527 if (!tui_active)
528 return;
529
530 /* Restore initial readline keymap. */
531 rl_set_keymap (tui_readline_standard_keymap);
532
533 /* Remove TUI hooks. */
535 rl_startup_hook = 0;
536 rl_already_prompted = 0;
537
538#ifdef NCURSES_MOUSE_VERSION
539 mousemask (0, NULL);
540#endif
541
542 /* Leave curses and restore previous gdb terminal setting. */
543 endwin ();
544
545 /* gdb terminal has changed, update gdb internal copy of it
546 so that terminal management with the inferior works. */
547 tui_setup_io (0);
548
549 /* Update gdb's knowledge of its terminal. */
551
552 tui_active = false;
554}
555
556/* Command wrapper for enabling tui mode. */
557
558static void
559tui_enable_command (const char *args, int from_tty)
560{
561 tui_enable ();
562}
563
564/* Command wrapper for leaving tui mode. */
565
566static void
567tui_disable_command (const char *args, int from_tty)
568{
569 tui_disable ();
570}
571
572void
579
580bool
582{
583 if (!tui_active)
584 return false;
585
586 if (tui_win_list[type] == nullptr)
587 return false;
588
589 return tui_win_list[type]->is_visible ();
590}
591
592bool
594 unsigned int *height)
595{
596 if (!tui_active || (TUI_CMD_WIN == NULL))
597 return false;
598
599 *width = TUI_CMD_WIN->width;
600 *height = TUI_CMD_WIN->height;
601 return true;
602}
603
604void _initialize_tui ();
605void
607{
608 struct cmd_list_element **tuicmd;
609
610 tuicmd = tui_get_cmd_list ();
611
613 _("Enable TUI display mode.\n\
614Usage: tui enable"),
615 tuicmd);
617 _("Disable TUI display mode.\n\
618Usage: tui disable"),
619 tuicmd);
620
621 /* Debug this tui internals. */
623Set tui debugging."), _("\
624Show tui debugging."), _("\
625When true, tui specific internal debugging is enabled."),
626 NULL,
629}
const char * name() const
Definition interps.h:87
struct cmd_list_element * showdebuglist
Definition cli-cmds.c:167
struct cmd_list_element * setdebuglist
Definition cli-cmds.c:165
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition cli-decode.c:233
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
void dont_repeat()
Definition top.c:696
@ class_tui
Definition command.h:66
@ class_maintenance
Definition command.h:65
void set_force_quit_flag()
Definition event-top.c:1149
void exception_print(struct ui_file *file, const struct gdb_exception &e)
Definition exceptions.c:106
frame_info_ptr deprecated_safe_get_selected_frame(void)
Definition frame.c:1907
else inf wait suppress
Definition gnu-nat.c:1836
void gdb_save_tty_state(void)
Definition inflow.c:206
struct interp * top_level_interpreter(void)
Definition interps.c:345
#define INTERP_TUI
Definition interps.h:372
enum var_types type
Definition scm-param.c:142
const char * cmd
Definition tui.c:81
unsigned char key
Definition tui.c:80
bool is_visible() const
Definition tui-data.h:97
Definition value.h:130
int gdb_in_secondary_prompt_p(struct ui *ui)
Definition top.c:942
void tui_set_win_focus_to(struct tui_win_info *win_info)
Definition tui-data.c:65
void tui_set_term_width_to(int w)
Definition tui-data.c:102
void tui_set_win_resized_to(bool resized)
Definition tui-data.c:49
struct tui_win_info * tui_win_list[MAX_MAJOR_WINDOWS]
Definition tui-data.c:32
struct tui_win_info * tui_next_win(struct tui_win_info *cur_win)
Definition tui-data.c:111
bool tui_win_resized()
Definition tui-data.c:41
void tui_set_term_height_to(int h)
Definition tui-data.c:86
struct tui_win_info * tui_win_with_focus(void)
Definition tui-data.c:57
#define TUI_SRC_WIN
Definition tui-data.h:195
#define TUI_CMD_WIN
Definition tui-data.h:198
void tui_remove_hooks(void)
Definition tui-hooks.c:279
void tui_install_hooks(void)
Definition tui-hooks.c:264
void tui_setup_io(int mode)
Definition tui-io.c:814
void tui_set_initial_layout()
Definition tui-layout.c:223
void tui_add_win_to_layout(enum tui_win_type type)
Definition tui-layout.c:155
void tui_remove_some_windows()
Definition tui-layout.c:268
void tui_next_layout()
Definition tui-layout.c:202
bool tui_show_frame_info(frame_info_ptr fi)
Definition tui-stack.c:256
void tui_show_locator_content()
Definition tui-stack.c:304
void tui_rehighlight_all(void)
Definition tui-win.c:488
struct cmd_list_element ** tui_get_cmd_list(void)
Definition tui-win.c:300
void tui_resize_all(void)
Definition tui-win.c:497
void tui_refresh_all_win(void)
Definition tui-win.c:481
bool tui_update_variables()
Definition tui-win.c:257
void tui_update_gdb_sizes(void)
Definition tui-win.c:422
void tui_update_source_windows_with_addr(struct gdbarch *gdbarch, CORE_ADDR addr)
void tui_display_main()
bool tui_get_command_dimension(unsigned int *width, unsigned int *height)
Definition tui.c:593
void tui_disable(void)
Definition tui.c:523
void tui_ensure_readline_initialized()
Definition tui.c:294
static int tui_rl_other_window(int count, int key)
Definition tui.c:194
static int tui_rl_command_key(int count, int key)
Definition tui.c:210
static Keymap tui_keymap
Definition tui.c:101
void _initialize_tui()
Definition tui.c:606
void tui_show_assembly(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition tui.c:573
static void tui_enable_command(const char *args, int from_tty)
Definition tui.c:559
static void tui_disable_command(const char *args, int from_tty)
Definition tui.c:567
static Keymap tui_readline_standard_keymap
Definition tui.c:102
static const char * gdb_getenv_term(void)
Definition tui.c:366
static int tui_rl_next_keymap(int notused1, int notused2)
Definition tui.c:250
bool debug_tui
Definition tui.c:56
enum tui_key_mode tui_current_key_mode
Definition tui.c:76
static bool tui_finish_init
Definition tui.c:74
static int tui_rl_delete_other_windows(int notused1, int notused2)
Definition tui.c:180
bool tui_is_window_visible(enum tui_win_type type)
Definition tui.c:581
static void show_tui_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
Definition tui.c:61
static int tui_rl_switch_mode(int notused1, int notused2)
Definition tui.c:107
static const struct tui_char_command tui_commands[]
Definition tui.c:86
static int tui_rl_command_mode(int count, int key)
Definition tui.c:241
void tui_enable(void)
Definition tui.c:381
bool tui_active
Definition tui.c:73
void tui_set_key_mode(enum tui_key_mode mode)
Definition tui.c:283
static int tui_rl_startup_hook(void)
Definition tui.c:272
static int tui_rl_change_windows(int notused1, int notused2)
Definition tui.c:166
tui_win_type
Definition tui.h:53
@ DISASSEM_WIN
Definition tui.h:55
#define TUI_SCOPED_DEBUG_ENTER_EXIT
Definition tui.h:36
tui_key_mode
Definition tui.h:81
@ TUI_ONE_COMMAND_MODE
Definition tui.h:90
@ TUI_SINGLE_KEY_MODE
Definition tui.h:86
@ TUI_COMMAND_MODE
Definition tui.h:83
struct ui * current_ui
Definition ui.c:35
void reinitialize_more_filter(void)
Definition utils.c:1471
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stderr
Definition utils.h:187