Speed up GDB's TUI output
[deliverable/binutils-gdb.git] / gdb / tui / tui-io.c
1 /* TUI support I/O functions.
2
3 Copyright (C) 1998-2015 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 "target.h"
24 #include "event-loop.h"
25 #include "event-top.h"
26 #include "command.h"
27 #include "top.h"
28 #include "tui/tui.h"
29 #include "tui/tui-data.h"
30 #include "tui/tui-io.h"
31 #include "tui/tui-command.h"
32 #include "tui/tui-win.h"
33 #include "tui/tui-wingeneral.h"
34 #include "tui/tui-file.h"
35 #include "ui-out.h"
36 #include "cli-out.h"
37 #include <fcntl.h>
38 #include <signal.h>
39 #include "filestuff.h"
40 #include "completer.h"
41 #include "gdb_curses.h"
42
43 /* This redefines CTRL if it is not already defined, so it must come
44 after terminal state releated include files like <term.h> and
45 "gdb_curses.h". */
46 #include "readline/readline.h"
47
48 int
49 key_is_start_sequence (int ch)
50 {
51 return (ch == 27);
52 }
53
54 int
55 key_is_end_sequence (int ch)
56 {
57 return (ch == 126);
58 }
59
60 int
61 key_is_backspace (int ch)
62 {
63 return (ch == 8);
64 }
65
66 int
67 key_is_command_char (int ch)
68 {
69 return ((ch == KEY_NPAGE) || (ch == KEY_PPAGE)
70 || (ch == KEY_LEFT) || (ch == KEY_RIGHT)
71 || (ch == KEY_UP) || (ch == KEY_DOWN)
72 || (ch == KEY_SF) || (ch == KEY_SR)
73 || (ch == (int)'\f')
74 || key_is_start_sequence (ch));
75 }
76
77 /* Use definition from readline 4.3. */
78 #undef CTRL_CHAR
79 #define CTRL_CHAR(c) \
80 ((c) < control_character_threshold && (((c) & 0x80) == 0))
81
82 /* This file controls the IO interactions between gdb and curses.
83 When the TUI is enabled, gdb has two modes a curses and a standard
84 mode.
85
86 In curses mode, the gdb outputs are made in a curses command
87 window. For this, the gdb_stdout and gdb_stderr are redirected to
88 the specific ui_file implemented by TUI. The output is handled by
89 tui_puts(). The input is also controlled by curses with
90 tui_getc(). The readline library uses this function to get its
91 input. Several readline hooks are installed to redirect readline
92 output to the TUI (see also the note below).
93
94 In normal mode, the gdb outputs are restored to their origin, that
95 is as if TUI is not used. Readline also uses its original getc()
96 function with stdin.
97
98 Note SCz/2001-07-21: the current readline is not clean in its
99 management of the output. Even if we install a redisplay handler,
100 it sometimes writes on a stdout file. It is important to redirect
101 every output produced by readline, otherwise the curses window will
102 be garbled. This is implemented with a pipe that TUI reads and
103 readline writes to. A gdb input handler is created so that reading
104 the pipe is handled automatically. This will probably not work on
105 non-Unix platforms. The best fix is to make readline clean enougth
106 so that is never write on stdout.
107
108 Note SCz/2002-09-01: we now use more readline hooks and it seems
109 that with them we don't need the pipe anymore (verified by creating
110 the pipe and closing its end so that write causes a SIGPIPE). The
111 old pipe code is still there and can be conditionally removed by
112 #undef TUI_USE_PIPE_FOR_READLINE. */
113
114 /* For gdb 5.3, prefer to continue the pipe hack as a backup wheel. */
115 #ifdef HAVE_PIPE
116 #define TUI_USE_PIPE_FOR_READLINE
117 #endif
118 /* #undef TUI_USE_PIPE_FOR_READLINE */
119
120 /* TUI output files. */
121 static struct ui_file *tui_stdout;
122 static struct ui_file *tui_stderr;
123 struct ui_out *tui_out;
124
125 /* GDB output files in non-curses mode. */
126 static struct ui_file *tui_old_stdout;
127 static struct ui_file *tui_old_stderr;
128 struct ui_out *tui_old_uiout;
129
130 /* Readline previous hooks. */
131 static rl_getc_func_t *tui_old_rl_getc_function;
132 static rl_voidfunc_t *tui_old_rl_redisplay_function;
133 static rl_vintfunc_t *tui_old_rl_prep_terminal;
134 static rl_voidfunc_t *tui_old_rl_deprep_terminal;
135 static rl_compdisp_func_t *tui_old_rl_display_matches_hook;
136 static int tui_old_rl_echoing_p;
137
138 /* Readline output stream.
139 Should be removed when readline is clean. */
140 static FILE *tui_rl_outstream;
141 static FILE *tui_old_rl_outstream;
142 #ifdef TUI_USE_PIPE_FOR_READLINE
143 static int tui_readline_pipe[2];
144 #endif
145
146 /* The last gdb prompt that was registered in readline.
147 This may be the main gdb prompt or a secondary prompt. */
148 static char *tui_rl_saved_prompt;
149
150 static int tui_handle_resize_during_io (int, int);
151
152 static void
153 tui_putc (char c)
154 {
155 char buf[2];
156
157 buf[0] = c;
158 buf[1] = 0;
159 tui_puts (buf);
160 }
161
162 /* Print the string in the curses command window.
163 The output is buffered. It is up to the caller to refresh the screen
164 if necessary. */
165
166 void
167 tui_puts (const char *string)
168 {
169 static int tui_skip_line = -1;
170 char c;
171 WINDOW *w;
172
173 w = TUI_CMD_WIN->generic.handle;
174 while ((c = *string++) != 0)
175 {
176 /* Catch annotation and discard them. We need two \032 and
177 discard until a \n is seen. */
178 if (c == '\032')
179 {
180 tui_skip_line++;
181 }
182 else if (tui_skip_line != 1)
183 {
184 tui_skip_line = -1;
185 /* Expand TABs, since ncurses on MS-Windows doesn't. */
186 if (c == '\t')
187 {
188 int line, col;
189
190 getyx (w, line, col);
191 do
192 {
193 waddch (w, ' ');
194 col++;
195 } while ((col % 8) != 0);
196 }
197 else
198 waddch (w, c);
199 }
200 else if (c == '\n')
201 tui_skip_line = -1;
202 }
203 getyx (w, TUI_CMD_WIN->detail.command_info.cur_line,
204 TUI_CMD_WIN->detail.command_info.curch);
205 TUI_CMD_WIN->detail.command_info.start_line
206 = TUI_CMD_WIN->detail.command_info.cur_line;
207 }
208
209 /* Readline callback.
210 Redisplay the command line with its prompt after readline has
211 changed the edited text. */
212 void
213 tui_redisplay_readline (void)
214 {
215 int prev_col;
216 int height;
217 int col, line;
218 int c_pos;
219 int c_line;
220 int in;
221 WINDOW *w;
222 char *prompt;
223 int start_line;
224
225 /* Detect when we temporarily left SingleKey and now the readline
226 edit buffer is empty, automatically restore the SingleKey
227 mode. The restore must only be done if the command has finished.
228 The command could call prompt_for_continue and we must not
229 restore SingleKey so that the prompt and normal keymap are used. */
230 if (tui_current_key_mode == TUI_ONE_COMMAND_MODE && rl_end == 0
231 && immediate_quit == 0)
232 tui_set_key_mode (TUI_SINGLE_KEY_MODE);
233
234 if (tui_current_key_mode == TUI_SINGLE_KEY_MODE)
235 prompt = "";
236 else
237 prompt = tui_rl_saved_prompt;
238
239 c_pos = -1;
240 c_line = -1;
241 w = TUI_CMD_WIN->generic.handle;
242 start_line = TUI_CMD_WIN->detail.command_info.start_line;
243 wmove (w, start_line, 0);
244 prev_col = 0;
245 height = 1;
246 for (in = 0; prompt && prompt[in]; in++)
247 {
248 waddch (w, prompt[in]);
249 getyx (w, line, col);
250 if (col <= prev_col)
251 height++;
252 prev_col = col;
253 }
254 for (in = 0; in <= rl_end; in++)
255 {
256 unsigned char c;
257
258 if (in == rl_point)
259 {
260 getyx (w, c_line, c_pos);
261 }
262
263 if (in == rl_end)
264 break;
265
266 c = (unsigned char) rl_line_buffer[in];
267 if (CTRL_CHAR (c) || c == RUBOUT)
268 {
269 waddch (w, '^');
270 waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?');
271 }
272 else if (c == '\t')
273 {
274 /* Expand TABs, since ncurses on MS-Windows doesn't. */
275 getyx (w, line, col);
276 do
277 {
278 waddch (w, ' ');
279 col++;
280 } while ((col % 8) != 0);
281 }
282 else
283 {
284 waddch (w, c);
285 }
286 if (c == '\n')
287 {
288 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
289 TUI_CMD_WIN->detail.command_info.curch);
290 }
291 getyx (w, line, col);
292 if (col < prev_col)
293 height++;
294 prev_col = col;
295 }
296 wclrtobot (w);
297 getyx (w, TUI_CMD_WIN->detail.command_info.start_line,
298 TUI_CMD_WIN->detail.command_info.curch);
299 if (c_line >= 0)
300 {
301 wmove (w, c_line, c_pos);
302 TUI_CMD_WIN->detail.command_info.cur_line = c_line;
303 TUI_CMD_WIN->detail.command_info.curch = c_pos;
304 }
305 TUI_CMD_WIN->detail.command_info.start_line -= height - 1;
306
307 wrefresh (w);
308 fflush(stdout);
309 }
310
311 /* Readline callback to prepare the terminal. It is called once each
312 time we enter readline. Terminal is already setup in curses
313 mode. */
314 static void
315 tui_prep_terminal (int notused1)
316 {
317 /* Save the prompt registered in readline to correctly display it.
318 (we can't use gdb_prompt() due to secondary prompts and can't use
319 rl_prompt because it points to an alloca buffer). */
320 xfree (tui_rl_saved_prompt);
321 tui_rl_saved_prompt = rl_prompt != NULL ? xstrdup (rl_prompt) : NULL;
322 }
323
324 /* Readline callback to restore the terminal. It is called once each
325 time we leave readline. There is nothing to do in curses mode. */
326 static void
327 tui_deprep_terminal (void)
328 {
329 }
330
331 #ifdef TUI_USE_PIPE_FOR_READLINE
332 /* Read readline output pipe and feed the command window with it.
333 Should be removed when readline is clean. */
334 static void
335 tui_readline_output (int error, gdb_client_data data)
336 {
337 int size;
338 char buf[256];
339
340 size = read (tui_readline_pipe[0], buf, sizeof (buf) - 1);
341 if (size > 0 && tui_active)
342 {
343 buf[size] = 0;
344 tui_puts (buf);
345 }
346 }
347 #endif
348
349 /* TUI version of displayer.crlf. */
350
351 static void
352 tui_mld_crlf (const struct match_list_displayer *displayer)
353 {
354 tui_putc ('\n');
355 }
356
357 /* TUI version of displayer.putch. */
358
359 static void
360 tui_mld_putch (const struct match_list_displayer *displayer, int ch)
361 {
362 tui_putc (ch);
363 }
364
365 /* TUI version of displayer.puts. */
366
367 static void
368 tui_mld_puts (const struct match_list_displayer *displayer, const char *s)
369 {
370 tui_puts (s);
371 }
372
373 /* TUI version of displayer.flush. */
374
375 static void
376 tui_mld_flush (const struct match_list_displayer *displayer)
377 {
378 wrefresh (TUI_CMD_WIN->generic.handle);
379 }
380
381 /* TUI version of displayer.erase_entire_line. */
382
383 static void
384 tui_mld_erase_entire_line (const struct match_list_displayer *displayer)
385 {
386 WINDOW *w = TUI_CMD_WIN->generic.handle;
387
388 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
389 wclrtoeol (w);
390 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
391 }
392
393 /* TUI version of displayer.beep. */
394
395 static void
396 tui_mld_beep (const struct match_list_displayer *displayer)
397 {
398 beep ();
399 }
400
401 /* Helper function for tui_mld_read_key.
402 This temporarily replaces tui_getc for use during tab-completion
403 match list display. */
404
405 static int
406 tui_mld_getc (FILE *fp)
407 {
408 WINDOW *w = TUI_CMD_WIN->generic.handle;
409 int c = wgetch (w);
410
411 c = tui_handle_resize_during_io (c, 1);
412
413 return c;
414 }
415
416 /* TUI version of displayer.read_key. */
417
418 static int
419 tui_mld_read_key (const struct match_list_displayer *displayer)
420 {
421 rl_getc_func_t *prev = rl_getc_function;
422 int c;
423
424 /* We can't use tui_getc as we need NEWLINE to not get emitted. */
425 rl_getc_function = tui_mld_getc;
426 c = rl_read_key ();
427 rl_getc_function = prev;
428 return c;
429 }
430
431 /* TUI version of rl_completion_display_matches_hook.
432 See gdb_display_match_list for a description of the arguments. */
433
434 static void
435 tui_rl_display_match_list (char **matches, int len, int max)
436 {
437 struct match_list_displayer displayer;
438
439 rl_get_screen_size (&displayer.height, &displayer.width);
440 displayer.crlf = tui_mld_crlf;
441 displayer.putch = tui_mld_putch;
442 displayer.puts = tui_mld_puts;
443 displayer.flush = tui_mld_flush;
444 displayer.erase_entire_line = tui_mld_erase_entire_line;
445 displayer.beep = tui_mld_beep;
446 displayer.read_key = tui_mld_read_key;
447
448 gdb_display_match_list (matches, len, max, &displayer);
449 }
450
451 /* Setup the IO for curses or non-curses mode.
452 - In non-curses mode, readline and gdb use the standard input and
453 standard output/error directly.
454 - In curses mode, the standard output/error is controlled by TUI
455 with the tui_stdout and tui_stderr. The output is redirected in
456 the curses command window. Several readline callbacks are installed
457 so that readline asks for its input to the curses command window
458 with wgetch(). */
459 void
460 tui_setup_io (int mode)
461 {
462 extern int _rl_echoing_p;
463
464 if (mode)
465 {
466 /* Redirect readline to TUI. */
467 tui_old_rl_redisplay_function = rl_redisplay_function;
468 tui_old_rl_deprep_terminal = rl_deprep_term_function;
469 tui_old_rl_prep_terminal = rl_prep_term_function;
470 tui_old_rl_getc_function = rl_getc_function;
471 tui_old_rl_display_matches_hook = rl_completion_display_matches_hook;
472 tui_old_rl_outstream = rl_outstream;
473 tui_old_rl_echoing_p = _rl_echoing_p;
474 rl_redisplay_function = tui_redisplay_readline;
475 rl_deprep_term_function = tui_deprep_terminal;
476 rl_prep_term_function = tui_prep_terminal;
477 rl_getc_function = tui_getc;
478 _rl_echoing_p = 0;
479 rl_outstream = tui_rl_outstream;
480 rl_prompt = 0;
481 rl_completion_display_matches_hook = tui_rl_display_match_list;
482 rl_already_prompted = 0;
483
484 /* Keep track of previous gdb output. */
485 tui_old_stdout = gdb_stdout;
486 tui_old_stderr = gdb_stderr;
487 tui_old_uiout = current_uiout;
488
489 /* Reconfigure gdb output. */
490 gdb_stdout = tui_stdout;
491 gdb_stderr = tui_stderr;
492 gdb_stdlog = gdb_stdout; /* for moment */
493 gdb_stdtarg = gdb_stderr; /* for moment */
494 gdb_stdtargerr = gdb_stderr; /* for moment */
495 current_uiout = tui_out;
496
497 /* Save tty for SIGCONT. */
498 savetty ();
499 }
500 else
501 {
502 /* Restore gdb output. */
503 gdb_stdout = tui_old_stdout;
504 gdb_stderr = tui_old_stderr;
505 gdb_stdlog = gdb_stdout; /* for moment */
506 gdb_stdtarg = gdb_stderr; /* for moment */
507 gdb_stdtargerr = gdb_stderr; /* for moment */
508 current_uiout = tui_old_uiout;
509
510 /* Restore readline. */
511 rl_redisplay_function = tui_old_rl_redisplay_function;
512 rl_deprep_term_function = tui_old_rl_deprep_terminal;
513 rl_prep_term_function = tui_old_rl_prep_terminal;
514 rl_getc_function = tui_old_rl_getc_function;
515 rl_completion_display_matches_hook = tui_old_rl_display_matches_hook;
516 rl_outstream = tui_old_rl_outstream;
517 _rl_echoing_p = tui_old_rl_echoing_p;
518 rl_already_prompted = 0;
519
520 /* Save tty for SIGCONT. */
521 savetty ();
522 }
523 }
524
525 #ifdef SIGCONT
526 /* Catch SIGCONT to restore the terminal and refresh the screen. */
527 static void
528 tui_cont_sig (int sig)
529 {
530 if (tui_active)
531 {
532 /* Restore the terminal setting because another process (shell)
533 might have changed it. */
534 resetty ();
535
536 /* Force a refresh of the screen. */
537 tui_refresh_all_win ();
538
539 /* Update cursor position on the screen. */
540 wmove (TUI_CMD_WIN->generic.handle,
541 TUI_CMD_WIN->detail.command_info.start_line,
542 TUI_CMD_WIN->detail.command_info.curch);
543 wrefresh (TUI_CMD_WIN->generic.handle);
544 }
545 signal (sig, tui_cont_sig);
546 }
547 #endif
548
549 /* Initialize the IO for gdb in curses mode. */
550 void
551 tui_initialize_io (void)
552 {
553 #ifdef SIGCONT
554 signal (SIGCONT, tui_cont_sig);
555 #endif
556
557 /* Create tui output streams. */
558 tui_stdout = tui_fileopen (stdout);
559 tui_stderr = tui_fileopen (stderr);
560 tui_out = tui_out_new (tui_stdout);
561
562 /* Create the default UI. */
563 tui_old_uiout = cli_out_new (gdb_stdout);
564
565 #ifdef TUI_USE_PIPE_FOR_READLINE
566 /* Temporary solution for readline writing to stdout: redirect
567 readline output in a pipe, read that pipe and output the content
568 in the curses command window. */
569 if (gdb_pipe_cloexec (tui_readline_pipe) != 0)
570 error (_("Cannot create pipe for readline"));
571
572 tui_rl_outstream = fdopen (tui_readline_pipe[1], "w");
573 if (tui_rl_outstream == 0)
574 error (_("Cannot redirect readline output"));
575
576 setvbuf (tui_rl_outstream, (char*) NULL, _IOLBF, 0);
577
578 #ifdef O_NONBLOCK
579 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NONBLOCK);
580 #else
581 #ifdef O_NDELAY
582 (void) fcntl (tui_readline_pipe[0], F_SETFL, O_NDELAY);
583 #endif
584 #endif
585 add_file_handler (tui_readline_pipe[0], tui_readline_output, 0);
586 #else
587 tui_rl_outstream = stdout;
588 #endif
589 }
590
591 /* Get a character from the command window. This is called from the
592 readline package. */
593 int
594 tui_getc (FILE *fp)
595 {
596 int ch;
597 WINDOW *w;
598
599 w = TUI_CMD_WIN->generic.handle;
600
601 #ifdef TUI_USE_PIPE_FOR_READLINE
602 /* Flush readline output. */
603 tui_readline_output (0, 0);
604 #endif
605
606 ch = wgetch (w);
607 ch = tui_handle_resize_during_io (ch, 0);
608
609 /* The \n must be echoed because it will not be printed by
610 readline. */
611 if (ch == '\n')
612 {
613 /* When hitting return with an empty input, gdb executes the last
614 command. If we emit a newline, this fills up the command window
615 with empty lines with gdb prompt at beginning. Instead of that,
616 stay on the same line but provide a visual effect to show the
617 user we recognized the command. */
618 if (rl_end == 0)
619 {
620 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line, 0);
621
622 /* Clear the line. This will blink the gdb prompt since
623 it will be redrawn at the same line. */
624 wclrtoeol (w);
625 wrefresh (w);
626 napms (20);
627 }
628 else
629 {
630 wmove (w, TUI_CMD_WIN->detail.command_info.cur_line,
631 TUI_CMD_WIN->detail.command_info.curch);
632 waddch (w, ch);
633 }
634 }
635
636 if (key_is_command_char (ch))
637 { /* Handle prev/next/up/down here. */
638 ch = tui_dispatch_ctrl_char (ch);
639 }
640
641 if (ch == '\n' || ch == '\r' || ch == '\f')
642 TUI_CMD_WIN->detail.command_info.curch = 0;
643 if (ch == KEY_BACKSPACE)
644 return '\b';
645
646 if (async_command_editing_p && key_is_start_sequence (ch))
647 {
648 int ch_pending;
649
650 nodelay (w, TRUE);
651 ch_pending = wgetch (w);
652 nodelay (w, FALSE);
653
654 /* If we have pending input following a start sequence, call the stdin
655 event handler again because ncurses may have already read and stored
656 the input into its internal buffer, meaning that we won't get an stdin
657 event for it. If we don't compensate for this missed stdin event, key
658 sequences as Alt_F (^[f) will not behave promptly.
659
660 (We only compensates for the missed 2nd byte of a key sequence because
661 2-byte sequences are by far the most commonly used. ncurses may have
662 buffered a larger, 3+-byte key sequence though it remains to be seen
663 whether it is useful to compensate for all the bytes of such
664 sequences.) */
665 if (ch_pending != ERR)
666 {
667 ungetch (ch_pending);
668 call_stdin_event_handler_again_p = 1;
669 }
670 }
671
672 return ch;
673 }
674
675 /* Utility function to expand TABs in a STRING into spaces. STRING
676 will be displayed starting at column COL, and is assumed to include
677 no newlines. The returned expanded string is malloc'ed. */
678
679 char *
680 tui_expand_tabs (const char *string, int col)
681 {
682 int n_adjust;
683 const char *s;
684 char *ret, *q;
685
686 /* 1. How many additional characters do we need? */
687 for (n_adjust = 0, s = string; s; )
688 {
689 s = strpbrk (s, "\t");
690 if (s)
691 {
692 col += (s - string) + n_adjust;
693 /* Adjustment for the next tab stop, minus one for the TAB
694 we replace with spaces. */
695 n_adjust += 8 - (col % 8) - 1;
696 s++;
697 }
698 }
699
700 /* Allocate the copy. */
701 ret = q = xmalloc (strlen (string) + n_adjust + 1);
702
703 /* 2. Copy the original string while replacing TABs with spaces. */
704 for (s = string; s; )
705 {
706 char *s1 = strpbrk (s, "\t");
707 if (s1)
708 {
709 if (s1 > s)
710 {
711 strncpy (q, s, s1 - s);
712 q += s1 - s;
713 col += s1 - s;
714 }
715 do {
716 *q++ = ' ';
717 col++;
718 } while ((col % 8) != 0);
719 s1++;
720 }
721 else
722 strcpy (q, s);
723 s = s1;
724 }
725
726 return ret;
727 }
728
729 /* Cleanup when a resize has occured.
730 Returns the character that must be processed. */
731
732 static int
733 tui_handle_resize_during_io (int original_ch, int for_completion)
734 {
735 if (tui_win_resized ())
736 {
737 tui_resize_all ();
738 tui_refresh_all_win ();
739 tui_set_win_resized_to (FALSE);
740 if (!for_completion)
741 {
742 dont_repeat ();
743 return '\n';
744 }
745 }
746
747 return original_ch;
748 }
This page took 0.045065 seconds and 5 git commands to generate.