1 /* TUI display registers in window.
3 Copyright (C) 1998-2014 Free Software Foundation, Inc.
5 Contributed by Hewlett-Packard Company.
7 This file is part of GDB.
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.
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.
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/>. */
23 #include "arch-utils.h"
25 #include "tui/tui-data.h"
34 #include "tui/tui-layout.h"
35 #include "tui/tui-win.h"
36 #include "tui/tui-windata.h"
37 #include "tui/tui-wingeneral.h"
38 #include "tui/tui-file.h"
39 #include "tui/tui-regs.h"
40 #include "reggroups.h"
43 #include "gdb_curses.h"
46 /*****************************************
47 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
48 ******************************************/
50 tui_display_register (struct tui_data_element *data,
51 struct tui_gen_win_info *win_info);
53 static enum tui_status tui_show_register_group (struct reggroup *group,
54 struct frame_info *frame,
55 int refresh_values_only);
57 static enum tui_status tui_get_register (struct frame_info *frame,
58 struct tui_data_element *data,
59 int regnum, int *changedp);
61 static void tui_scroll_regs_forward_command (char *, int);
62 static void tui_scroll_regs_backward_command (char *, int);
66 /*****************************************
67 ** PUBLIC FUNCTIONS **
68 ******************************************/
70 /* Answer the number of the last line in the regs display. If there
71 are no registers (-1) is returned. */
73 tui_last_regs_line_no (void)
77 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
79 num_lines = (TUI_DATA_WIN->detail.data_display_info.regs_content_count /
80 TUI_DATA_WIN->detail.data_display_info.regs_column_count);
81 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count %
82 TUI_DATA_WIN->detail.data_display_info.regs_column_count)
89 /* Answer the line number that the register element at element_no is
90 on. If element_no is greater than the number of register elements
91 there are, -1 is returned. */
93 tui_line_from_reg_element_no (int element_no)
95 if (element_no < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
103 (TUI_DATA_WIN->detail.data_display_info.regs_column_count * i))
116 /* Answer the index of the first element in line_no. If line_no is
117 past the register area (-1) is returned. */
119 tui_first_reg_element_no_inline (int line_no)
121 if ((line_no * TUI_DATA_WIN->detail.data_display_info.regs_column_count)
122 <= TUI_DATA_WIN->detail.data_display_info.regs_content_count)
123 return ((line_no + 1) *
124 TUI_DATA_WIN->detail.data_display_info.regs_column_count) -
125 TUI_DATA_WIN->detail.data_display_info.regs_column_count;
131 /* Show the registers of the given group in the data window
132 and refresh the window. */
134 tui_show_registers (struct reggroup *group)
136 enum tui_status ret = TUI_FAILURE;
137 struct tui_data_info *display_info;
139 /* Make sure the curses mode is enabled. */
142 /* Make sure the register window is visible. If not, select an
143 appropriate layout. */
144 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
145 tui_set_layout_for_display_command (DATA_NAME);
147 display_info = &TUI_DATA_WIN->detail.data_display_info;
149 group = general_reggroup;
151 /* Say that registers should be displayed, even if there is a
153 display_info->display_regs = TRUE;
155 if (target_has_registers && target_has_stack && target_has_memory)
157 ret = tui_show_register_group (group, get_selected_frame (NULL),
158 group == display_info->current_group);
160 if (ret == TUI_FAILURE)
162 display_info->current_group = 0;
163 tui_erase_data_content (NO_REGS_STRING);
169 /* Clear all notation of changed values. */
170 for (i = 0; i < display_info->regs_content_count; i++)
172 struct tui_gen_win_info *data_item_win;
173 struct tui_win_element *win;
175 data_item_win = &display_info->regs_content[i]
176 ->which_element.data_window;
177 win = (struct tui_win_element *) data_item_win->content[0];
178 win->which_element.data.highlight = FALSE;
180 display_info->current_group = group;
181 tui_display_all_data ();
186 /* Set the data window to display the registers of the register group
187 using the given frame. Values are refreshed only when
188 refresh_values_only is TRUE. */
190 static enum tui_status
191 tui_show_register_group (struct reggroup *group,
192 struct frame_info *frame,
193 int refresh_values_only)
195 struct gdbarch *gdbarch = get_frame_arch (frame);
196 enum tui_status ret = TUI_FAILURE;
198 int allocated_here = FALSE;
201 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
203 /* Make a new title showing which group we display. */
204 snprintf (title, sizeof (title) - 1, "Register group: %s",
205 reggroup_name (group));
206 xfree (TUI_DATA_WIN->generic.title);
207 TUI_DATA_WIN->generic.title = xstrdup (title);
209 /* See how many registers must be displayed. */
212 regnum < gdbarch_num_regs (gdbarch)
213 + gdbarch_num_pseudo_regs (gdbarch);
218 /* Must be in the group. */
219 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
222 /* If the register name is empty, it is undefined for this
223 processor, so don't display anything. */
224 name = gdbarch_register_name (gdbarch, regnum);
225 if (name == 0 || *name == '\0')
231 if (display_info->regs_content_count > 0 && !refresh_values_only)
233 tui_free_data_content (display_info->regs_content,
234 display_info->regs_content_count);
235 display_info->regs_content_count = 0;
238 if (display_info->regs_content_count <= 0)
240 display_info->regs_content = tui_alloc_content (nr_regs, DATA_WIN);
241 allocated_here = TRUE;
242 refresh_values_only = FALSE;
245 if (display_info->regs_content != (tui_win_content) NULL)
247 if (!refresh_values_only || allocated_here)
249 TUI_DATA_WIN->generic.content = (void*) NULL;
250 TUI_DATA_WIN->generic.content_size = 0;
251 tui_add_content_elements (&TUI_DATA_WIN->generic, nr_regs);
252 display_info->regs_content
253 = (tui_win_content) TUI_DATA_WIN->generic.content;
254 display_info->regs_content_count = nr_regs;
257 /* Now set the register names and values. */
260 regnum < gdbarch_num_regs (gdbarch)
261 + gdbarch_num_pseudo_regs (gdbarch);
264 struct tui_gen_win_info *data_item_win;
265 struct tui_data_element *data;
268 /* Must be in the group. */
269 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
272 /* If the register name is empty, it is undefined for this
273 processor, so don't display anything. */
274 name = gdbarch_register_name (gdbarch, regnum);
275 if (name == 0 || *name == '\0')
279 &display_info->regs_content[pos]->which_element.data_window;
280 data = &((struct tui_win_element *)
281 data_item_win->content[0])->which_element.data;
284 if (!refresh_values_only)
286 data->item_no = regnum;
288 data->highlight = FALSE;
290 tui_get_register (frame, data, regnum, 0);
295 TUI_DATA_WIN->generic.content_size =
296 display_info->regs_content_count + display_info->data_content_count;
303 /* Function to display the registers in the content from
304 'start_element_no' until the end of the register content or the end
305 of the display height. No checking for displaying past the end of
306 the registers is done here. */
308 tui_display_registers_from (int start_element_no)
310 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
312 if (display_info->regs_content != (tui_win_content) NULL
313 && display_info->regs_content_count > 0)
315 int i = start_element_no;
316 int j, item_win_width, cur_y;
319 for (i = 0; i < display_info->regs_content_count; i++)
321 struct tui_data_element *data;
322 struct tui_gen_win_info *data_item_win;
327 = &display_info->regs_content[i]->which_element.data_window;
328 data = &((struct tui_win_element *)
329 data_item_win->content[0])->which_element.data;
336 len = 8 * ((len / 8) + 1);
344 item_win_width = max_len + 1;
345 i = start_element_no;
347 display_info->regs_column_count =
348 (TUI_DATA_WIN->generic.width - 2) / item_win_width;
349 if (display_info->regs_column_count == 0)
350 display_info->regs_column_count = 1;
352 (TUI_DATA_WIN->generic.width - 2) / display_info->regs_column_count;
354 /* Now create each data "sub" window, and write the display into
357 while (i < display_info->regs_content_count
358 && cur_y <= TUI_DATA_WIN->generic.viewport_height)
361 j < display_info->regs_column_count
362 && i < display_info->regs_content_count;
365 struct tui_gen_win_info *data_item_win;
366 struct tui_data_element *data_element_ptr;
368 /* Create the window if necessary. */
369 data_item_win = &display_info->regs_content[i]
370 ->which_element.data_window;
371 data_element_ptr = &((struct tui_win_element *)
372 data_item_win->content[0])->which_element.data;
373 if (data_item_win->handle != (WINDOW*) NULL
374 && (data_item_win->height != 1
375 || data_item_win->width != item_win_width
376 || data_item_win->origin.x != (item_win_width * j) + 1
377 || data_item_win->origin.y != cur_y))
379 tui_delete_win (data_item_win->handle);
380 data_item_win->handle = 0;
383 if (data_item_win->handle == (WINDOW *) NULL)
385 data_item_win->height = 1;
386 data_item_win->width = item_win_width;
387 data_item_win->origin.x = (item_win_width * j) + 1;
388 data_item_win->origin.y = cur_y;
389 tui_make_window (data_item_win, DONT_BOX_WINDOW);
390 scrollok (data_item_win->handle, FALSE);
392 touchwin (data_item_win->handle);
394 /* Get the printable representation of the register
396 tui_display_register (data_element_ptr, data_item_win);
397 i++; /* Next register. */
399 cur_y++; /* Next row. */
405 /* Function to display the registers in the content from
406 'start_element_no' on 'start_line_no' until the end of the register
407 content or the end of the display height. This function checks
408 that we won't display off the end of the register display. */
410 tui_display_reg_element_at_line (int start_element_no,
413 if (TUI_DATA_WIN->detail.data_display_info.regs_content
414 != (tui_win_content) NULL
415 && TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
417 int element_no = start_element_no;
419 if (start_element_no != 0 && start_line_no != 0)
421 int last_line_no, first_line_on_last_page;
423 last_line_no = tui_last_regs_line_no ();
424 first_line_on_last_page
425 = last_line_no - (TUI_DATA_WIN->generic.height - 2);
426 if (first_line_on_last_page < 0)
427 first_line_on_last_page = 0;
429 /* If there is no other data displayed except registers, and
430 the element_no causes us to scroll past the end of the
431 registers, adjust what element to really start the
433 if (TUI_DATA_WIN->detail.data_display_info.data_content_count <= 0
434 && start_line_no > first_line_on_last_page)
436 = tui_first_reg_element_no_inline (first_line_on_last_page);
438 tui_display_registers_from (element_no);
444 /* Function to display the registers starting at line line_no in the
445 data window. Answers the line number that the display actually
446 started from. If nothing is displayed (-1) is returned. */
448 tui_display_registers_from_line (int line_no,
451 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
453 int line, element_no;
457 else if (force_display)
458 { /* If we must display regs (force_display is true), then
459 make sure that we don't display off the end of the
461 if (line_no >= tui_last_regs_line_no ())
463 if ((line = tui_line_from_reg_element_no (
464 TUI_DATA_WIN->detail.data_display_info.regs_content_count - 1)) < 0)
473 element_no = tui_first_reg_element_no_inline (line);
475 < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
476 tui_display_reg_element_at_line (element_no, line);
483 return (-1); /* Nothing was displayed. */
487 /* This function check all displayed registers for changes in values,
488 given a particular frame. If the values have changed, they are
489 updated with the new value and highlighted. */
491 tui_check_register_values (struct frame_info *frame)
493 if (TUI_DATA_WIN != NULL
494 && TUI_DATA_WIN->generic.is_visible)
496 struct tui_data_info *display_info
497 = &TUI_DATA_WIN->detail.data_display_info;
499 if (display_info->regs_content_count <= 0
500 && display_info->display_regs)
501 tui_show_registers (display_info->current_group);
506 for (i = 0; (i < display_info->regs_content_count); i++)
508 struct tui_data_element *data;
509 struct tui_gen_win_info *data_item_win_ptr;
512 data_item_win_ptr = &display_info->regs_content[i]->
513 which_element.data_window;
514 data = &((struct tui_win_element *)
515 data_item_win_ptr->content[0])->which_element.data;
516 was_hilighted = data->highlight;
518 tui_get_register (frame, data,
519 data->item_no, &data->highlight);
521 if (data->highlight || was_hilighted)
523 tui_display_register (data, data_item_win_ptr);
530 /* Display a register in a window. If hilite is TRUE, then the value
531 will be displayed in reverse video. */
533 tui_display_register (struct tui_data_element *data,
534 struct tui_gen_win_info *win_info)
536 if (win_info->handle != (WINDOW *) NULL)
541 /* We ignore the return value, casting it to void in order to avoid
542 a compiler warning. The warning itself was introduced by a patch
543 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
544 to code that causes the compiler to generate an unused-value
546 (void) wstandout (win_info->handle);
548 wmove (win_info->handle, 0, 0);
549 for (i = 1; i < win_info->width; i++)
550 waddch (win_info->handle, ' ');
551 wmove (win_info->handle, 0, 0);
553 waddstr (win_info->handle, data->content);
556 /* We ignore the return value, casting it to void in order to avoid
557 a compiler warning. The warning itself was introduced by a patch
558 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
559 to code that causes the compiler to generate an unused-value
561 (void) wstandend (win_info->handle);
562 tui_refresh_win (win_info);
567 tui_reg_next_command (char *arg, int from_tty)
569 struct gdbarch *gdbarch = get_current_arch ();
571 if (TUI_DATA_WIN != 0)
573 struct reggroup *group
574 = TUI_DATA_WIN->detail.data_display_info.current_group;
576 group = reggroup_next (gdbarch, group);
578 group = reggroup_next (gdbarch, 0);
581 tui_show_registers (group);
586 tui_reg_float_command (char *arg, int from_tty)
588 tui_show_registers (float_reggroup);
592 tui_reg_general_command (char *arg, int from_tty)
594 tui_show_registers (general_reggroup);
598 tui_reg_system_command (char *arg, int from_tty)
600 tui_show_registers (system_reggroup);
603 static struct cmd_list_element *tuireglist;
606 tui_reg_command (char *args, int from_tty)
608 printf_unfiltered (_("\"tui reg\" must be followed by the name of a "
609 "tui reg command.\n"));
610 help_list (tuireglist, "tui reg ", -1, gdb_stdout);
613 /* Provide a prototype to silence -Wmissing-prototypes. */
614 extern initialize_file_ftype _initialize_tui_regs;
617 _initialize_tui_regs (void)
619 struct cmd_list_element **tuicmd;
621 tuicmd = tui_get_cmd_list ();
623 add_prefix_cmd ("reg", class_tui, tui_reg_command,
624 _("TUI commands to control the register window."),
625 &tuireglist, "tui reg ", 0,
628 add_cmd ("float", class_tui, tui_reg_float_command,
629 _("Display only floating point registers."),
631 add_cmd ("general", class_tui, tui_reg_general_command,
632 _("Display only general registers."),
634 add_cmd ("system", class_tui, tui_reg_system_command,
635 _("Display only system registers."),
637 add_cmd ("next", class_tui, tui_reg_next_command,
638 _("Display next register group."),
643 add_com ("fr", class_tui, tui_reg_float_command,
644 _("Display only floating point registers\n"));
645 add_com ("gr", class_tui, tui_reg_general_command,
646 _("Display only general registers\n"));
647 add_com ("sr", class_tui, tui_reg_system_command,
648 _("Display only special registers\n"));
649 add_com ("+r", class_tui, tui_scroll_regs_forward_command,
650 _("Scroll the registers window forward\n"));
651 add_com ("-r", class_tui, tui_scroll_regs_backward_command,
652 _("Scroll the register window backward\n"));
657 /*****************************************
658 ** STATIC LOCAL FUNCTIONS **
659 ******************************************/
662 tui_restore_gdbout (void *ui)
664 ui_file_delete (gdb_stdout);
665 gdb_stdout = (struct ui_file*) ui;
666 pagination_enabled = 1;
669 /* Get the register from the frame and return a printable
670 representation of it. */
673 tui_register_format (struct frame_info *frame, int regnum)
675 struct gdbarch *gdbarch = get_frame_arch (frame);
676 struct ui_file *stream;
677 struct ui_file *old_stdout;
678 struct cleanup *cleanups;
682 pagination_enabled = 0;
683 old_stdout = gdb_stdout;
684 stream = tui_sfileopen (256);
686 cleanups = make_cleanup (tui_restore_gdbout, (void*) old_stdout);
687 gdbarch_print_registers_info (gdbarch, stream, frame, regnum, 1);
689 /* Save formatted output in the buffer. */
690 p = tui_file_get_strbuf (stream);
692 /* Remove the possible \n. */
693 s = strrchr (p, '\n');
698 do_cleanups (cleanups);
703 /* Get the register value from the given frame and format it for the
704 display. When changep is set, check if the new register value has
705 changed with respect to the previous call. */
706 static enum tui_status
707 tui_get_register (struct frame_info *frame,
708 struct tui_data_element *data,
709 int regnum, int *changedp)
711 enum tui_status ret = TUI_FAILURE;
715 if (target_has_registers)
717 char *prev_content = data->content;
719 data->content = tui_register_format (frame, regnum);
722 && strcmp (prev_content, data->content) != 0)
725 xfree (prev_content);
733 tui_scroll_regs_forward_command (char *arg, int from_tty)
735 tui_scroll (FORWARD_SCROLL, TUI_DATA_WIN, 1);
740 tui_scroll_regs_backward_command (char *arg, int from_tty)
742 tui_scroll (BACKWARD_SCROLL, TUI_DATA_WIN, 1);