1 /* General functions for the WDB TUI.
2 
3    Copyright (C) 1998-2021 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 
45 #include <ctype.h>
46 #include <signal.h>
47 #include <fcntl.h>
48 #include <setjmp.h>
49 
50 #include "gdb_curses.h"
51 #include "interps.h"
52 
53 /* This redefines CTRL if it is not already defined, so it must come
54    after terminal state releated include files like <term.h> and
55    "gdb_curses.h".  */
56 #include "readline/readline.h"
57 
58 /* Tells whether the TUI is active or not.  */
59 bool tui_active = false;
60 static bool tui_finish_init = true;
61 
62 enum tui_key_mode tui_current_key_mode = TUI_COMMAND_MODE;
63 
64 struct tui_char_command
65 {
66   unsigned char key;
67   const char *cmd;
68 };
69 
70 /* Key mapping to gdb commands when the TUI is using the single key
71    mode.  */
72 static const struct tui_char_command tui_commands[] = {
73   { 'c', "continue" },
74   { 'd', "down" },
75   { 'f', "finish" },
76   { 'n', "next" },
77   { 'o', "nexti" },
78   { 'r', "run" },
79   { 's', "step" },
80   { 'i', "stepi" },
81   { 'u', "up" },
82   { 'v', "info locals" },
83   { 'w', "where" },
84   { 0, 0 },
85 };
86 
87 static Keymap tui_keymap;
88 static Keymap tui_readline_standard_keymap;
89 
90 /* TUI readline command.
91    Switch the output mode between TUI/standard gdb.  */
92 static int
tui_rl_switch_mode(int notused1,int notused2)93 tui_rl_switch_mode (int notused1, int notused2)
94 {
95 
96   /* Don't let exceptions escape.  We're in the middle of a readline
97      callback that isn't prepared for that.  */
98   try
99     {
100       if (tui_active)
101 	{
102 	  tui_disable ();
103 	  rl_prep_terminal (0);
104 	}
105       else
106 	{
107 	  /* If tui_enable throws, we'll re-prep below.  */
108 	  rl_deprep_terminal ();
109 	  tui_enable ();
110 	}
111     }
112   catch (const gdb_exception &ex)
113     {
114       exception_print (gdb_stderr, ex);
115 
116       if (!tui_active)
117 	rl_prep_terminal (0);
118     }
119 
120   /* Clear the readline in case switching occurred in middle of
121      something.  */
122   if (rl_end)
123     rl_kill_text (0, rl_end);
124 
125   /* Since we left the curses mode, the terminal mode is restored to
126      some previous state.  That state may not be suitable for readline
127      to work correctly (it may be restored in line mode).  We force an
128      exit of the current readline so that readline is re-entered and
129      it will be able to setup the terminal for its needs.  By
130      re-entering in readline, we also redisplay its prompt in the
131      non-curses mode.  */
132   rl_newline (1, '\n');
133 
134   /* Make sure the \n we are returning does not repeat the last
135      command.  */
136   dont_repeat ();
137   return 0;
138 }
139 
140 /* TUI readline command.
141    Change the TUI layout to show a next layout.
142    This function is bound to CTRL-X 2.  It is intended to provide
143    a functionality close to the Emacs split-window command.  */
144 static int
tui_rl_change_windows(int notused1,int notused2)145 tui_rl_change_windows (int notused1, int notused2)
146 {
147   if (!tui_active)
148     tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
149 
150   if (tui_active)
151     tui_next_layout ();
152 
153   return 0;
154 }
155 
156 /* TUI readline command.
157    Delete the second TUI window to only show one.  */
158 static int
tui_rl_delete_other_windows(int notused1,int notused2)159 tui_rl_delete_other_windows (int notused1, int notused2)
160 {
161   if (!tui_active)
162     tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
163 
164   if (tui_active)
165     tui_remove_some_windows ();
166 
167   return 0;
168 }
169 
170 /* TUI readline command.
171    Switch the active window to give the focus to a next window.  */
172 static int
tui_rl_other_window(int count,int key)173 tui_rl_other_window (int count, int key)
174 {
175   struct tui_win_info *win_info;
176 
177   if (!tui_active)
178     tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
179 
180   win_info = tui_next_win (tui_win_with_focus ());
181   if (win_info)
182     tui_set_win_focus_to (win_info);
183   return 0;
184 }
185 
186 /* TUI readline command.
187    Execute the gdb command bound to the specified key.  */
188 static int
tui_rl_command_key(int count,int key)189 tui_rl_command_key (int count, int key)
190 {
191   int i;
192 
193   reinitialize_more_filter ();
194   for (i = 0; tui_commands[i].cmd; i++)
195     {
196       if (tui_commands[i].key == key)
197 	{
198 	  /* Insert the command in the readline buffer.
199 	     Avoid calling the gdb command here since it creates
200 	     a possible recursion on readline if prompt_for_continue
201 	     is called (See PR 9584).  The command will also appear
202 	     in the readline history which turns out to be better.  */
203 	  rl_insert_text (tui_commands[i].cmd);
204 	  rl_newline (1, '\n');
205 
206 	  /* Switch to gdb command mode while executing the command.
207 	     This way the gdb's continue prompty will be displayed.  */
208 	  tui_set_key_mode (TUI_ONE_COMMAND_MODE);
209 	  return 0;
210 	}
211     }
212   return 0;
213 }
214 
215 /* TUI readline command.
216    Temporarily leave the TUI SingleKey mode to allow editing
217    a gdb command with the normal readline.  Once the command
218    is executed, the TUI SingleKey mode is installed back.  */
219 static int
tui_rl_command_mode(int count,int key)220 tui_rl_command_mode (int count, int key)
221 {
222   tui_set_key_mode (TUI_ONE_COMMAND_MODE);
223   return rl_insert (count, key);
224 }
225 
226 /* TUI readline command.
227    Switch between TUI SingleKey mode and gdb readline editing.  */
228 static int
tui_rl_next_keymap(int notused1,int notused2)229 tui_rl_next_keymap (int notused1, int notused2)
230 {
231   if (!tui_active)
232     tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
233 
234   tui_set_key_mode (tui_current_key_mode == TUI_COMMAND_MODE
235 		    ? TUI_SINGLE_KEY_MODE : TUI_COMMAND_MODE);
236   return 0;
237 }
238 
239 /* Readline hook to redisplay ourself the gdb prompt.
240    In the SingleKey mode, the prompt is not printed so that
241    the command window is cleaner.  It will be displayed if
242    we temporarily leave the SingleKey mode.  */
243 static int
tui_rl_startup_hook(void)244 tui_rl_startup_hook (void)
245 {
246   rl_already_prompted = 1;
247   if (tui_current_key_mode != TUI_COMMAND_MODE
248       && !gdb_in_secondary_prompt_p (current_ui))
249     tui_set_key_mode (TUI_SINGLE_KEY_MODE);
250   tui_redisplay_readline ();
251   return 0;
252 }
253 
254 /* Change the TUI key mode by installing the appropriate readline
255    keymap.  */
256 void
tui_set_key_mode(enum tui_key_mode mode)257 tui_set_key_mode (enum tui_key_mode mode)
258 {
259   tui_current_key_mode = mode;
260   rl_set_keymap (mode == TUI_SINGLE_KEY_MODE
261 		 ? tui_keymap : tui_readline_standard_keymap);
262   tui_show_locator_content ();
263 }
264 
265 /* Initialize readline and configure the keymap for the switching
266    key shortcut.  */
267 void
tui_ensure_readline_initialized()268 tui_ensure_readline_initialized ()
269 {
270   static bool initialized;
271 
272   if (initialized)
273     return;
274   initialized = true;
275 
276   int i;
277   Keymap tui_ctlx_keymap;
278 
279   rl_add_defun ("tui-switch-mode", tui_rl_switch_mode, -1);
280   rl_add_defun ("next-keymap", tui_rl_next_keymap, -1);
281   rl_add_defun ("tui-delete-other-windows", tui_rl_delete_other_windows, -1);
282   rl_add_defun ("tui-change-windows", tui_rl_change_windows, -1);
283   rl_add_defun ("tui-other-window", tui_rl_other_window, -1);
284 
285   tui_keymap = rl_make_bare_keymap ();
286 
287   /* The named keymap feature was added in Readline 8.0.  */
288 #if RL_READLINE_VERSION >= 0x800
289   rl_set_keymap_name ("SingleKey", tui_keymap);
290 #endif
291 
292   tui_ctlx_keymap = rl_make_bare_keymap ();
293   tui_readline_standard_keymap = rl_get_keymap ();
294 
295   for (i = 0; tui_commands[i].cmd; i++)
296     rl_bind_key_in_map (tui_commands[i].key, tui_rl_command_key, tui_keymap);
297 
298   rl_generic_bind (ISKMAP, "\\C-x", (char*) tui_ctlx_keymap, tui_keymap);
299 
300   /* Bind all other keys to tui_rl_command_mode so that we switch
301      temporarily from SingleKey mode and can enter a gdb command.  */
302   for (i = ' '; i < 0x7f; i++)
303     {
304       int j;
305 
306       for (j = 0; tui_commands[j].cmd; j++)
307 	if (tui_commands[j].key == i)
308 	  break;
309 
310       if (tui_commands[j].cmd)
311 	continue;
312 
313       rl_bind_key_in_map (i, tui_rl_command_mode, tui_keymap);
314     }
315 
316   rl_bind_key_in_map ('a', tui_rl_switch_mode, emacs_ctlx_keymap);
317   rl_bind_key_in_map ('a', tui_rl_switch_mode, tui_ctlx_keymap);
318   rl_bind_key_in_map ('A', tui_rl_switch_mode, emacs_ctlx_keymap);
319   rl_bind_key_in_map ('A', tui_rl_switch_mode, tui_ctlx_keymap);
320   rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, emacs_ctlx_keymap);
321   rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, tui_ctlx_keymap);
322   rl_bind_key_in_map ('1', tui_rl_delete_other_windows, emacs_ctlx_keymap);
323   rl_bind_key_in_map ('1', tui_rl_delete_other_windows, tui_ctlx_keymap);
324   rl_bind_key_in_map ('2', tui_rl_change_windows, emacs_ctlx_keymap);
325   rl_bind_key_in_map ('2', tui_rl_change_windows, tui_ctlx_keymap);
326   rl_bind_key_in_map ('o', tui_rl_other_window, emacs_ctlx_keymap);
327   rl_bind_key_in_map ('o', tui_rl_other_window, tui_ctlx_keymap);
328   rl_bind_key_in_map ('q', tui_rl_next_keymap, tui_keymap);
329   rl_bind_key_in_map ('s', tui_rl_next_keymap, emacs_ctlx_keymap);
330   rl_bind_key_in_map ('s', tui_rl_next_keymap, tui_ctlx_keymap);
331 
332   /* Initialize readline after the above.  */
333   rl_initialize ();
334 }
335 
336 /* Return the TERM variable from the environment, or "<unset>"
337    if not set.  */
338 
339 static const char *
gdb_getenv_term(void)340 gdb_getenv_term (void)
341 {
342   const char *term;
343 
344   term = getenv ("TERM");
345   if (term != NULL)
346     return term;
347   return "<unset>";
348 }
349 
350 /* Enter in the tui mode (curses).
351    When in normal mode, it installs the tui hooks in gdb, redirects
352    the gdb output, configures the readline to work in tui mode.
353    When in curses mode, it does nothing.  */
354 void
tui_enable(void)355 tui_enable (void)
356 {
357   if (tui_active)
358     return;
359 
360   /* To avoid to initialize curses when gdb starts, there is a deferred
361      curses initialization.  This initialization is made only once
362      and the first time the curses mode is entered.  */
363   if (tui_finish_init)
364     {
365       WINDOW *w;
366       SCREEN *s;
367 #ifndef __MINGW32__
368        const char *cap;
369 #endif
370       const char *interp;
371 
372       /* If the top level interpreter is not the console/tui (e.g.,
373 	 MI), enabling curses will certainly lose.  */
374       interp = top_level_interpreter ()->name ();
375       if (strcmp (interp, INTERP_TUI) != 0)
376 	error (_("Cannot enable the TUI when the interpreter is '%s'"), interp);
377 
378       /* Don't try to setup curses (and print funny control
379 	 characters) if we're not outputting to a terminal.  */
380       if (!gdb_stderr->isatty ())
381 	error (_("Cannot enable the TUI when output is not a terminal"));
382 
383       s = newterm (NULL, stdout, stdin);
384 #ifdef __MINGW32__
385       /* The MinGW port of ncurses requires $TERM to be unset in order
386 	 to activate the Windows console driver.  */
387       if (s == NULL)
388 	s = newterm ((char *) "unknown", stdout, stdin);
389 #endif
390       if (s == NULL)
391 	{
392 	  error (_("Cannot enable the TUI: error opening terminal [TERM=%s]"),
393 		 gdb_getenv_term ());
394 	}
395       w = stdscr;
396       if (has_colors ())
397 	{
398 #ifdef HAVE_USE_DEFAULT_COLORS
399 	  /* Ncurses extension to help with resetting to the default
400 	     color.  */
401 	  use_default_colors ();
402 #endif
403 	  start_color ();
404 	}
405 
406       /* Check required terminal capabilities.  The MinGW port of
407 	 ncurses does have them, but doesn't expose them through "cup".  */
408 #ifndef __MINGW32__
409       cap = tigetstr ((char *) "cup");
410       if (cap == NULL || cap == (char *) -1 || *cap == '\0')
411 	{
412 	  endwin ();
413 	  delscreen (s);
414 	  error (_("Cannot enable the TUI: "
415 		   "terminal doesn't support cursor addressing [TERM=%s]"),
416 		 gdb_getenv_term ());
417 	}
418 #endif
419 
420       /* We must mark the tui sub-system active before trying to setup the
421 	 current layout as tui windows defined by an extension language
422 	 rely on this flag being true in order to know that the window
423 	 they are creating is currently valid.  */
424       tui_active = true;
425 
426       cbreak ();
427       noecho ();
428       /* timeout (1); */
429       nodelay(w, FALSE);
430       nl();
431       keypad (w, TRUE);
432       tui_set_term_height_to (LINES);
433       tui_set_term_width_to (COLS);
434       def_prog_mode ();
435 
436       tui_show_frame_info (0);
437       tui_set_initial_layout ();
438       tui_set_win_focus_to (TUI_SRC_WIN);
439       keypad (TUI_CMD_WIN->handle.get (), TRUE);
440       wrefresh (TUI_CMD_WIN->handle.get ());
441       tui_finish_init = false;
442     }
443   else
444     {
445       /* Save the current gdb setting of the terminal.
446 	 Curses will restore this state when endwin() is called.  */
447       def_shell_mode ();
448       clearok (stdscr, TRUE);
449 
450       tui_active = true;
451     }
452 
453   gdb_assert (tui_active);
454 
455   if (tui_update_variables ())
456     tui_rehighlight_all ();
457 
458   tui_setup_io (1);
459 
460   /* Resize windows before anything might display/refresh a
461      window.  */
462   if (tui_win_resized ())
463     {
464       tui_set_win_resized_to (false);
465       tui_resize_all ();
466     }
467 
468   if (deprecated_safe_get_selected_frame ())
469     tui_show_frame_info (deprecated_safe_get_selected_frame ());
470   else
471     tui_display_main ();
472 
473   /* Install the TUI specific hooks.  This must be done after the call to
474      tui_display_main so that we don't detect the symtab changed event it
475      can cause.  */
476   tui_install_hooks ();
477   rl_startup_hook = tui_rl_startup_hook;
478 
479   /* Restore TUI keymap.  */
480   tui_set_key_mode (tui_current_key_mode);
481 
482   /* Refresh the screen.  */
483   tui_refresh_all_win ();
484 
485   /* Update gdb's knowledge of its terminal.  */
486   gdb_save_tty_state ();
487   tui_update_gdb_sizes ();
488 }
489 
490 /* Leave the tui mode.
491    Remove the tui hooks and configure the gdb output and readline
492    back to their original state.  The curses mode is left so that
493    the terminal setting is restored to the point when we entered.  */
494 void
tui_disable(void)495 tui_disable (void)
496 {
497   if (!tui_active)
498     return;
499 
500   /* Restore initial readline keymap.  */
501   rl_set_keymap (tui_readline_standard_keymap);
502 
503   /* Remove TUI hooks.  */
504   tui_remove_hooks ();
505   rl_startup_hook = 0;
506   rl_already_prompted = 0;
507 
508 #ifdef NCURSES_MOUSE_VERSION
509   mousemask (0, NULL);
510 #endif
511 
512   /* Leave curses and restore previous gdb terminal setting.  */
513   endwin ();
514 
515   /* gdb terminal has changed, update gdb internal copy of it
516      so that terminal management with the inferior works.  */
517   tui_setup_io (0);
518 
519   /* Update gdb's knowledge of its terminal.  */
520   gdb_save_tty_state ();
521 
522   tui_active = false;
523   tui_update_gdb_sizes ();
524 }
525 
526 /* Command wrapper for enabling tui mode.  */
527 
528 static void
tui_enable_command(const char * args,int from_tty)529 tui_enable_command (const char *args, int from_tty)
530 {
531   tui_enable ();
532 }
533 
534 /* Command wrapper for leaving tui mode.  */
535 
536 static void
tui_disable_command(const char * args,int from_tty)537 tui_disable_command (const char *args, int from_tty)
538 {
539   tui_disable ();
540 }
541 
542 void
tui_show_assembly(struct gdbarch * gdbarch,CORE_ADDR addr)543 tui_show_assembly (struct gdbarch *gdbarch, CORE_ADDR addr)
544 {
545   tui_suppress_output suppress;
546   tui_add_win_to_layout (DISASSEM_WIN);
547   tui_update_source_windows_with_addr (gdbarch, addr);
548 }
549 
550 bool
tui_is_window_visible(enum tui_win_type type)551 tui_is_window_visible (enum tui_win_type type)
552 {
553   if (!tui_active)
554     return false;
555 
556   if (tui_win_list[type] == nullptr)
557     return false;
558 
559   return tui_win_list[type]->is_visible ();
560 }
561 
562 bool
tui_get_command_dimension(unsigned int * width,unsigned int * height)563 tui_get_command_dimension (unsigned int *width,
564 			   unsigned int *height)
565 {
566   if (!tui_active || (TUI_CMD_WIN == NULL))
567     return false;
568 
569   *width = TUI_CMD_WIN->width;
570   *height = TUI_CMD_WIN->height;
571   return true;
572 }
573 
574 void _initialize_tui ();
575 void
_initialize_tui()576 _initialize_tui ()
577 {
578   struct cmd_list_element **tuicmd;
579 
580   tuicmd = tui_get_cmd_list ();
581 
582   add_cmd ("enable", class_tui, tui_enable_command,
583 	   _("Enable TUI display mode.\n\
584 Usage: tui enable"),
585 	   tuicmd);
586   add_cmd ("disable", class_tui, tui_disable_command,
587 	   _("Disable TUI display mode.\n\
588 Usage: tui disable"),
589 	   tuicmd);
590 }
591