Move stdlib.h to common-defs.h
[deliverable/binutils-gdb.git] / gdb / tui / tui.c
1 /* General functions for the WDB TUI.
2
3 Copyright (C) 1998-2014 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-data.h"
27 #include "tui/tui-layout.h"
28 #include "tui/tui-io.h"
29 #include "tui/tui-regs.h"
30 #include "tui/tui-stack.h"
31 #include "tui/tui-win.h"
32 #include "tui/tui-winsource.h"
33 #include "tui/tui-windata.h"
34 #include "target.h"
35 #include "frame.h"
36 #include "breakpoint.h"
37 #include "inferior.h"
38 #include "symtab.h"
39 #include "source.h"
40
41 #include <ctype.h>
42 #include <signal.h>
43 #include <fcntl.h>
44 #if 0
45 #include <termio.h>
46 #endif
47 #include <setjmp.h>
48
49 #include "gdb_curses.h"
50
51 /* This redefines CTRL if it is not already defined, so it must come
52 after terminal state releated include files like <term.h> and
53 "gdb_curses.h". */
54 #include "readline/readline.h"
55
56 /* Tells whether the TUI is active or not. */
57 int tui_active = 0;
58 static int tui_finish_init = 1;
59
60 enum tui_key_mode tui_current_key_mode = TUI_COMMAND_MODE;
61
62 struct tui_char_command
63 {
64 unsigned char key;
65 const char *cmd;
66 };
67
68 /* Key mapping to gdb commands when the TUI is using the single key
69 mode. */
70 static const struct tui_char_command tui_commands[] = {
71 { 'c', "continue" },
72 { 'd', "down" },
73 { 'f', "finish" },
74 { 'n', "next" },
75 { 'r', "run" },
76 { 's', "step" },
77 { 'u', "up" },
78 { 'v', "info locals" },
79 { 'w', "where" },
80 { 0, 0 },
81 };
82
83 static Keymap tui_keymap;
84 static Keymap tui_readline_standard_keymap;
85
86 /* TUI readline command.
87 Switch the output mode between TUI/standard gdb. */
88 static int
89 tui_rl_switch_mode (int notused1, int notused2)
90 {
91 if (tui_active)
92 {
93 tui_disable ();
94 rl_prep_terminal (0);
95 }
96 else
97 {
98 rl_deprep_terminal ();
99 tui_enable ();
100 }
101
102 /* Clear the readline in case switching occurred in middle of
103 something. */
104 if (rl_end)
105 rl_kill_text (0, rl_end);
106
107 /* Since we left the curses mode, the terminal mode is restored to
108 some previous state. That state may not be suitable for readline
109 to work correctly (it may be restored in line mode). We force an
110 exit of the current readline so that readline is re-entered and
111 it will be able to setup the terminal for its needs. By
112 re-entering in readline, we also redisplay its prompt in the
113 non-curses mode. */
114 rl_newline (1, '\n');
115
116 /* Make sure the \n we are returning does not repeat the last
117 command. */
118 dont_repeat ();
119 return 0;
120 }
121
122 /* TUI readline command.
123 Change the TUI layout to show a next layout.
124 This function is bound to CTRL-X 2. It is intended to provide
125 a functionality close to the Emacs split-window command. We
126 always show two windows (src+asm), (src+regs) or (asm+regs). */
127 static int
128 tui_rl_change_windows (int notused1, int notused2)
129 {
130 if (!tui_active)
131 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
132
133 if (tui_active)
134 {
135 enum tui_layout_type new_layout;
136 enum tui_register_display_type regs_type = TUI_UNDEFINED_REGS;
137
138 new_layout = tui_current_layout ();
139
140 /* Select a new layout to have a rolling layout behavior with
141 always two windows (except when undefined). */
142 switch (new_layout)
143 {
144 case SRC_COMMAND:
145 new_layout = SRC_DISASSEM_COMMAND;
146 break;
147
148 case DISASSEM_COMMAND:
149 new_layout = SRC_DISASSEM_COMMAND;
150 break;
151
152 case SRC_DATA_COMMAND:
153 new_layout = SRC_DISASSEM_COMMAND;
154 break;
155
156 case SRC_DISASSEM_COMMAND:
157 new_layout = DISASSEM_DATA_COMMAND;
158 break;
159
160 case DISASSEM_DATA_COMMAND:
161 new_layout = SRC_DATA_COMMAND;
162 break;
163
164 default:
165 new_layout = SRC_COMMAND;
166 break;
167 }
168 tui_set_layout (new_layout, regs_type);
169 }
170 return 0;
171 }
172
173 /* TUI readline command.
174 Delete the second TUI window to only show one. */
175 static int
176 tui_rl_delete_other_windows (int notused1, int notused2)
177 {
178 if (!tui_active)
179 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
180
181 if (tui_active)
182 {
183 enum tui_layout_type new_layout;
184 enum tui_register_display_type regs_type = TUI_UNDEFINED_REGS;
185
186 new_layout = tui_current_layout ();
187
188 /* Kill one window. */
189 switch (new_layout)
190 {
191 case SRC_COMMAND:
192 case SRC_DATA_COMMAND:
193 case SRC_DISASSEM_COMMAND:
194 default:
195 new_layout = SRC_COMMAND;
196 break;
197
198 case DISASSEM_COMMAND:
199 case DISASSEM_DATA_COMMAND:
200 new_layout = DISASSEM_COMMAND;
201 break;
202 }
203 tui_set_layout (new_layout, regs_type);
204 }
205 return 0;
206 }
207
208 /* TUI readline command.
209 Switch the active window to give the focus to a next window. */
210 static int
211 tui_rl_other_window (int count, int key)
212 {
213 struct tui_win_info *win_info;
214
215 if (!tui_active)
216 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
217
218 win_info = tui_next_win (tui_win_with_focus ());
219 if (win_info)
220 {
221 tui_set_win_focus_to (win_info);
222 if (TUI_DATA_WIN && TUI_DATA_WIN->generic.is_visible)
223 tui_refresh_data_win ();
224 keypad (TUI_CMD_WIN->generic.handle, (win_info != TUI_CMD_WIN));
225 }
226 return 0;
227 }
228
229 /* TUI readline command.
230 Execute the gdb command bound to the specified key. */
231 static int
232 tui_rl_command_key (int count, int key)
233 {
234 int i;
235
236 reinitialize_more_filter ();
237 for (i = 0; tui_commands[i].cmd; i++)
238 {
239 if (tui_commands[i].key == key)
240 {
241 /* Insert the command in the readline buffer.
242 Avoid calling the gdb command here since it creates
243 a possible recursion on readline if prompt_for_continue
244 is called (See PR 9584). The command will also appear
245 in the readline history which turns out to be better. */
246 rl_insert_text (tui_commands[i].cmd);
247 rl_newline (1, '\n');
248
249 /* Switch to gdb command mode while executing the command.
250 This way the gdb's continue prompty will be displayed. */
251 tui_set_key_mode (TUI_ONE_COMMAND_MODE);
252 return 0;
253 }
254 }
255 return 0;
256 }
257
258 /* TUI readline command.
259 Temporarily leave the TUI SingleKey mode to allow editing
260 a gdb command with the normal readline. Once the command
261 is executed, the TUI SingleKey mode is installed back. */
262 static int
263 tui_rl_command_mode (int count, int key)
264 {
265 tui_set_key_mode (TUI_ONE_COMMAND_MODE);
266 return rl_insert (count, key);
267 }
268
269 /* TUI readline command.
270 Switch between TUI SingleKey mode and gdb readline editing. */
271 static int
272 tui_rl_next_keymap (int notused1, int notused2)
273 {
274 if (!tui_active)
275 tui_rl_switch_mode (0 /* notused */, 0 /* notused */);
276
277 tui_set_key_mode (tui_current_key_mode == TUI_COMMAND_MODE
278 ? TUI_SINGLE_KEY_MODE : TUI_COMMAND_MODE);
279 return 0;
280 }
281
282 /* Readline hook to redisplay ourself the gdb prompt.
283 In the SingleKey mode, the prompt is not printed so that
284 the command window is cleaner. It will be displayed if
285 we temporarily leave the SingleKey mode. */
286 static int
287 tui_rl_startup_hook (void)
288 {
289 rl_already_prompted = 1;
290 if (tui_current_key_mode != TUI_COMMAND_MODE && immediate_quit == 0)
291 tui_set_key_mode (TUI_SINGLE_KEY_MODE);
292 tui_redisplay_readline ();
293 return 0;
294 }
295
296 /* Change the TUI key mode by installing the appropriate readline
297 keymap. */
298 void
299 tui_set_key_mode (enum tui_key_mode mode)
300 {
301 tui_current_key_mode = mode;
302 rl_set_keymap (mode == TUI_SINGLE_KEY_MODE
303 ? tui_keymap : tui_readline_standard_keymap);
304 tui_show_locator_content ();
305 }
306
307 /* Initialize readline and configure the keymap for the switching
308 key shortcut. */
309 void
310 tui_initialize_readline (void)
311 {
312 int i;
313 Keymap tui_ctlx_keymap;
314
315 rl_initialize ();
316
317 rl_add_defun ("tui-switch-mode", tui_rl_switch_mode, -1);
318 rl_add_defun ("gdb-command", tui_rl_command_key, -1);
319 rl_add_defun ("next-keymap", tui_rl_next_keymap, -1);
320
321 tui_keymap = rl_make_bare_keymap ();
322 tui_ctlx_keymap = rl_make_bare_keymap ();
323 tui_readline_standard_keymap = rl_get_keymap ();
324
325 for (i = 0; tui_commands[i].cmd; i++)
326 rl_bind_key_in_map (tui_commands[i].key, tui_rl_command_key, tui_keymap);
327
328 rl_generic_bind (ISKMAP, "\\C-x", (char*) tui_ctlx_keymap, tui_keymap);
329
330 /* Bind all other keys to tui_rl_command_mode so that we switch
331 temporarily from SingleKey mode and can enter a gdb command. */
332 for (i = ' '; i < 0x7f; i++)
333 {
334 int j;
335
336 for (j = 0; tui_commands[j].cmd; j++)
337 if (tui_commands[j].key == i)
338 break;
339
340 if (tui_commands[j].cmd)
341 continue;
342
343 rl_bind_key_in_map (i, tui_rl_command_mode, tui_keymap);
344 }
345
346 rl_bind_key_in_map ('a', tui_rl_switch_mode, emacs_ctlx_keymap);
347 rl_bind_key_in_map ('a', tui_rl_switch_mode, tui_ctlx_keymap);
348 rl_bind_key_in_map ('A', tui_rl_switch_mode, emacs_ctlx_keymap);
349 rl_bind_key_in_map ('A', tui_rl_switch_mode, tui_ctlx_keymap);
350 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, emacs_ctlx_keymap);
351 rl_bind_key_in_map (CTRL ('A'), tui_rl_switch_mode, tui_ctlx_keymap);
352 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, emacs_ctlx_keymap);
353 rl_bind_key_in_map ('1', tui_rl_delete_other_windows, tui_ctlx_keymap);
354 rl_bind_key_in_map ('2', tui_rl_change_windows, emacs_ctlx_keymap);
355 rl_bind_key_in_map ('2', tui_rl_change_windows, tui_ctlx_keymap);
356 rl_bind_key_in_map ('o', tui_rl_other_window, emacs_ctlx_keymap);
357 rl_bind_key_in_map ('o', tui_rl_other_window, tui_ctlx_keymap);
358 rl_bind_key_in_map ('q', tui_rl_next_keymap, tui_keymap);
359 rl_bind_key_in_map ('s', tui_rl_next_keymap, emacs_ctlx_keymap);
360 rl_bind_key_in_map ('s', tui_rl_next_keymap, tui_ctlx_keymap);
361 }
362
363 /* Enter in the tui mode (curses).
364 When in normal mode, it installs the tui hooks in gdb, redirects
365 the gdb output, configures the readline to work in tui mode.
366 When in curses mode, it does nothing. */
367 void
368 tui_enable (void)
369 {
370 if (!tui_allowed_p ())
371 error (_("TUI mode not allowed"));
372
373 if (tui_active)
374 return;
375
376 /* To avoid to initialize curses when gdb starts, there is a defered
377 curses initialization. This initialization is made only once
378 and the first time the curses mode is entered. */
379 if (tui_finish_init)
380 {
381 WINDOW *w;
382
383 w = initscr ();
384
385 cbreak ();
386 noecho ();
387 /* timeout (1); */
388 nodelay(w, FALSE);
389 nl();
390 keypad (w, TRUE);
391 rl_initialize ();
392 tui_set_term_height_to (LINES);
393 tui_set_term_width_to (COLS);
394 def_prog_mode ();
395
396 tui_show_frame_info (0);
397 tui_set_layout (SRC_COMMAND, TUI_UNDEFINED_REGS);
398 tui_set_win_focus_to (TUI_SRC_WIN);
399 keypad (TUI_CMD_WIN->generic.handle, TRUE);
400 wrefresh (TUI_CMD_WIN->generic.handle);
401 tui_finish_init = 0;
402 }
403 else
404 {
405 /* Save the current gdb setting of the terminal.
406 Curses will restore this state when endwin() is called. */
407 def_shell_mode ();
408 clearok (stdscr, TRUE);
409 }
410
411 /* Install the TUI specific hooks. */
412 tui_install_hooks ();
413 rl_startup_hook = tui_rl_startup_hook;
414
415 tui_update_variables ();
416
417 tui_setup_io (1);
418
419 tui_active = 1;
420 if (deprecated_safe_get_selected_frame ())
421 tui_show_frame_info (deprecated_safe_get_selected_frame ());
422
423 /* Restore TUI keymap. */
424 tui_set_key_mode (tui_current_key_mode);
425 tui_refresh_all_win ();
426
427 /* Update gdb's knowledge of its terminal. */
428 target_terminal_save_ours ();
429 tui_update_gdb_sizes ();
430 }
431
432 /* Leave the tui mode.
433 Remove the tui hooks and configure the gdb output and readline
434 back to their original state. The curses mode is left so that
435 the terminal setting is restored to the point when we entered. */
436 void
437 tui_disable (void)
438 {
439 if (!tui_active)
440 return;
441
442 /* Restore initial readline keymap. */
443 rl_set_keymap (tui_readline_standard_keymap);
444
445 /* Remove TUI hooks. */
446 tui_remove_hooks ();
447 rl_startup_hook = 0;
448 rl_already_prompted = 0;
449
450 /* Leave curses and restore previous gdb terminal setting. */
451 endwin ();
452
453 /* gdb terminal has changed, update gdb internal copy of it
454 so that terminal management with the inferior works. */
455 tui_setup_io (0);
456
457 /* Update gdb's knowledge of its terminal. */
458 target_terminal_save_ours ();
459
460 tui_active = 0;
461 tui_update_gdb_sizes ();
462 }
463
464 void
465 strcat_to_buf (char *buf, int buflen,
466 const char *item_to_add)
467 {
468 if (item_to_add != (char *) NULL && buf != (char *) NULL)
469 {
470 if ((strlen (buf) + strlen (item_to_add)) <= buflen)
471 strcat (buf, item_to_add);
472 else
473 strncat (buf, item_to_add, (buflen - strlen (buf)));
474 }
475 }
476
477 #if 0
478 /* Solaris <sys/termios.h> defines CTRL. */
479 #ifndef CTRL
480 #define CTRL(x) (x & ~0140)
481 #endif
482
483 #define FILEDES 2
484 #define CHK(val, dft) (val<=0 ? dft : val)
485
486 static void
487 tui_reset (void)
488 {
489 struct termio mode;
490
491 /* Reset the teletype mode bits to a sensible state.
492 Copied tset.c. */
493 #if defined (TIOCGETC)
494 struct tchars tbuf;
495 #endif /* TIOCGETC */
496 #ifdef UCB_NTTY
497 struct ltchars ltc;
498
499 if (ldisc == NTTYDISC)
500 {
501 ioctl (FILEDES, TIOCGLTC, &ltc);
502 ltc.t_suspc = CHK (ltc.t_suspc, CTRL ('Z'));
503 ltc.t_dsuspc = CHK (ltc.t_dsuspc, CTRL ('Y'));
504 ltc.t_rprntc = CHK (ltc.t_rprntc, CTRL ('R'));
505 ltc.t_flushc = CHK (ltc.t_flushc, CTRL ('O'));
506 ltc.t_werasc = CHK (ltc.t_werasc, CTRL ('W'));
507 ltc.t_lnextc = CHK (ltc.t_lnextc, CTRL ('V'));
508 ioctl (FILEDES, TIOCSLTC, &ltc);
509 }
510 #endif /* UCB_NTTY */
511 #ifdef TIOCGETC
512 ioctl (FILEDES, TIOCGETC, &tbuf);
513 tbuf.t_intrc = CHK (tbuf.t_intrc, CTRL ('?'));
514 tbuf.t_quitc = CHK (tbuf.t_quitc, CTRL ('\\'));
515 tbuf.t_startc = CHK (tbuf.t_startc, CTRL ('Q'));
516 tbuf.t_stopc = CHK (tbuf.t_stopc, CTRL ('S'));
517 tbuf.t_eofc = CHK (tbuf.t_eofc, CTRL ('D'));
518 /* brkc is left alone. */
519 ioctl (FILEDES, TIOCSETC, &tbuf);
520 #endif /* TIOCGETC */
521 mode.sg_flags &= ~(RAW
522 #ifdef CBREAK
523 | CBREAK
524 #endif /* CBREAK */
525 | VTDELAY | ALLDELAY);
526 mode.sg_flags |= XTABS | ECHO | CRMOD | ANYP;
527
528 return;
529 }
530 #endif
531
532 void
533 tui_show_source (const char *fullname, int line)
534 {
535 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
536
537 /* Make sure that the source window is displayed. */
538 tui_add_win_to_layout (SRC_WIN);
539
540 tui_update_source_windows_with_line (cursal.symtab, line);
541 tui_update_locator_fullname (fullname);
542 }
543
544 void
545 tui_show_assembly (struct gdbarch *gdbarch, CORE_ADDR addr)
546 {
547 tui_add_win_to_layout (DISASSEM_WIN);
548 tui_update_source_windows_with_addr (gdbarch, addr);
549 }
550
551 int
552 tui_is_window_visible (enum tui_win_type type)
553 {
554 if (tui_active == 0)
555 return 0;
556
557 if (tui_win_list[type] == 0)
558 return 0;
559
560 return tui_win_list[type]->generic.is_visible;
561 }
562
563 int
564 tui_get_command_dimension (unsigned int *width,
565 unsigned int *height)
566 {
567 if (!tui_active || (TUI_CMD_WIN == NULL))
568 {
569 return 0;
570 }
571
572 *width = TUI_CMD_WIN->generic.width;
573 *height = TUI_CMD_WIN->generic.height;
574 return 1;
575 }
This page took 0.056261 seconds and 4 git commands to generate.