1 /* TUI display registers in window.
3 Copyright (C) 1998-2015 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"
33 #include "tui/tui-layout.h"
34 #include "tui/tui-win.h"
35 #include "tui/tui-windata.h"
36 #include "tui/tui-wingeneral.h"
37 #include "tui/tui-file.h"
38 #include "tui/tui-regs.h"
39 #include "tui/tui-io.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);
63 /*****************************************
64 ** PUBLIC FUNCTIONS **
65 ******************************************/
67 /* Answer the number of the last line in the regs display. If there
68 are no registers (-1) is returned. */
70 tui_last_regs_line_no (void)
74 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
76 num_lines = (TUI_DATA_WIN->detail.data_display_info.regs_content_count /
77 TUI_DATA_WIN->detail.data_display_info.regs_column_count);
78 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count %
79 TUI_DATA_WIN->detail.data_display_info.regs_column_count)
86 /* Answer the line number that the register element at element_no is
87 on. If element_no is greater than the number of register elements
88 there are, -1 is returned. */
90 tui_line_from_reg_element_no (int element_no)
92 if (element_no < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
100 (TUI_DATA_WIN->detail.data_display_info.regs_column_count * i))
113 /* Answer the index of the first element in line_no. If line_no is
114 past the register area (-1) is returned. */
116 tui_first_reg_element_no_inline (int line_no)
118 if ((line_no * TUI_DATA_WIN->detail.data_display_info.regs_column_count)
119 <= TUI_DATA_WIN->detail.data_display_info.regs_content_count)
120 return ((line_no + 1) *
121 TUI_DATA_WIN->detail.data_display_info.regs_column_count) -
122 TUI_DATA_WIN->detail.data_display_info.regs_column_count;
128 /* Show the registers of the given group in the data window
129 and refresh the window. */
131 tui_show_registers (struct reggroup *group)
133 enum tui_status ret = TUI_FAILURE;
134 struct tui_data_info *display_info;
136 /* Make sure the curses mode is enabled. */
139 /* Make sure the register window is visible. If not, select an
140 appropriate layout. */
141 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
142 tui_set_layout_by_name (DATA_NAME);
144 display_info = &TUI_DATA_WIN->detail.data_display_info;
146 group = general_reggroup;
148 /* Say that registers should be displayed, even if there is a
150 display_info->display_regs = TRUE;
152 if (target_has_registers && target_has_stack && target_has_memory)
154 ret = tui_show_register_group (group, get_selected_frame (NULL),
155 group == display_info->current_group);
157 if (ret == TUI_FAILURE)
159 display_info->current_group = 0;
160 tui_erase_data_content (NO_REGS_STRING);
166 /* Clear all notation of changed values. */
167 for (i = 0; i < display_info->regs_content_count; i++)
169 struct tui_gen_win_info *data_item_win;
170 struct tui_win_element *win;
172 data_item_win = &display_info->regs_content[i]
173 ->which_element.data_window;
174 win = data_item_win->content[0];
175 win->which_element.data.highlight = FALSE;
177 display_info->current_group = group;
178 tui_display_all_data ();
183 /* Set the data window to display the registers of the register group
184 using the given frame. Values are refreshed only when
185 refresh_values_only is TRUE. */
187 static enum tui_status
188 tui_show_register_group (struct reggroup *group,
189 struct frame_info *frame,
190 int refresh_values_only)
192 struct gdbarch *gdbarch = get_frame_arch (frame);
193 enum tui_status ret = TUI_FAILURE;
195 int allocated_here = FALSE;
198 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
200 /* Make a new title showing which group we display. */
201 snprintf (title, sizeof (title) - 1, "Register group: %s",
202 reggroup_name (group));
203 xfree (TUI_DATA_WIN->generic.title);
204 TUI_DATA_WIN->generic.title = xstrdup (title);
206 /* See how many registers must be displayed. */
209 regnum < gdbarch_num_regs (gdbarch)
210 + gdbarch_num_pseudo_regs (gdbarch);
215 /* Must be in the group. */
216 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
219 /* If the register name is empty, it is undefined for this
220 processor, so don't display anything. */
221 name = gdbarch_register_name (gdbarch, regnum);
222 if (name == 0 || *name == '\0')
228 if (display_info->regs_content_count > 0 && !refresh_values_only)
230 tui_free_data_content (display_info->regs_content,
231 display_info->regs_content_count);
232 display_info->regs_content_count = 0;
235 if (display_info->regs_content_count <= 0)
237 display_info->regs_content = tui_alloc_content (nr_regs, DATA_WIN);
238 allocated_here = TRUE;
239 refresh_values_only = FALSE;
242 if (display_info->regs_content != (tui_win_content) NULL)
244 if (!refresh_values_only || allocated_here)
246 TUI_DATA_WIN->generic.content = (void*) NULL;
247 TUI_DATA_WIN->generic.content_size = 0;
248 tui_add_content_elements (&TUI_DATA_WIN->generic, nr_regs);
249 display_info->regs_content
250 = (tui_win_content) TUI_DATA_WIN->generic.content;
251 display_info->regs_content_count = nr_regs;
254 /* Now set the register names and values. */
257 regnum < gdbarch_num_regs (gdbarch)
258 + gdbarch_num_pseudo_regs (gdbarch);
261 struct tui_gen_win_info *data_item_win;
262 struct tui_data_element *data;
265 /* Must be in the group. */
266 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
269 /* If the register name is empty, it is undefined for this
270 processor, so don't display anything. */
271 name = gdbarch_register_name (gdbarch, regnum);
272 if (name == 0 || *name == '\0')
276 &display_info->regs_content[pos]->which_element.data_window;
277 data = &data_item_win->content[0]->which_element.data;
280 if (!refresh_values_only)
282 data->item_no = regnum;
284 data->highlight = FALSE;
286 tui_get_register (frame, data, regnum, 0);
291 TUI_DATA_WIN->generic.content_size =
292 display_info->regs_content_count + display_info->data_content_count;
299 /* Function to display the registers in the content from
300 'start_element_no' until the end of the register content or the end
301 of the display height. No checking for displaying past the end of
302 the registers is done here. */
304 tui_display_registers_from (int start_element_no)
306 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
308 if (display_info->regs_content != (tui_win_content) NULL
309 && display_info->regs_content_count > 0)
311 int i = start_element_no;
312 int j, item_win_width, cur_y;
315 for (i = 0; i < display_info->regs_content_count; i++)
317 struct tui_data_element *data;
318 struct tui_gen_win_info *data_item_win;
323 = &display_info->regs_content[i]->which_element.data_window;
324 data = &data_item_win->content[0]->which_element.data;
331 len = 8 * ((len / 8) + 1);
339 item_win_width = max_len + 1;
340 i = start_element_no;
342 display_info->regs_column_count =
343 (TUI_DATA_WIN->generic.width - 2) / item_win_width;
344 if (display_info->regs_column_count == 0)
345 display_info->regs_column_count = 1;
347 (TUI_DATA_WIN->generic.width - 2) / display_info->regs_column_count;
349 /* Now create each data "sub" window, and write the display into
352 while (i < display_info->regs_content_count
353 && cur_y <= TUI_DATA_WIN->generic.viewport_height)
356 j < display_info->regs_column_count
357 && i < display_info->regs_content_count;
360 struct tui_gen_win_info *data_item_win;
361 struct tui_data_element *data_element_ptr;
363 /* Create the window if necessary. */
364 data_item_win = &display_info->regs_content[i]
365 ->which_element.data_window;
366 data_element_ptr = &data_item_win->content[0]->which_element.data;
367 if (data_item_win->handle != (WINDOW*) NULL
368 && (data_item_win->height != 1
369 || data_item_win->width != item_win_width
370 || data_item_win->origin.x != (item_win_width * j) + 1
371 || data_item_win->origin.y != cur_y))
373 tui_delete_win (data_item_win->handle);
374 data_item_win->handle = 0;
377 if (data_item_win->handle == (WINDOW *) NULL)
379 data_item_win->height = 1;
380 data_item_win->width = item_win_width;
381 data_item_win->origin.x = (item_win_width * j) + 1;
382 data_item_win->origin.y = cur_y;
383 tui_make_window (data_item_win, DONT_BOX_WINDOW);
384 scrollok (data_item_win->handle, FALSE);
386 touchwin (data_item_win->handle);
388 /* Get the printable representation of the register
390 tui_display_register (data_element_ptr, data_item_win);
391 i++; /* Next register. */
393 cur_y++; /* Next row. */
399 /* Function to display the registers in the content from
400 'start_element_no' on 'start_line_no' until the end of the register
401 content or the end of the display height. This function checks
402 that we won't display off the end of the register display. */
404 tui_display_reg_element_at_line (int start_element_no,
407 if (TUI_DATA_WIN->detail.data_display_info.regs_content
408 != (tui_win_content) NULL
409 && TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
411 int element_no = start_element_no;
413 if (start_element_no != 0 && start_line_no != 0)
415 int last_line_no, first_line_on_last_page;
417 last_line_no = tui_last_regs_line_no ();
418 first_line_on_last_page
419 = last_line_no - (TUI_DATA_WIN->generic.height - 2);
420 if (first_line_on_last_page < 0)
421 first_line_on_last_page = 0;
423 /* If there is no other data displayed except registers, and
424 the element_no causes us to scroll past the end of the
425 registers, adjust what element to really start the
427 if (TUI_DATA_WIN->detail.data_display_info.data_content_count <= 0
428 && start_line_no > first_line_on_last_page)
430 = tui_first_reg_element_no_inline (first_line_on_last_page);
432 tui_display_registers_from (element_no);
438 /* Function to display the registers starting at line line_no in the
439 data window. Answers the line number that the display actually
440 started from. If nothing is displayed (-1) is returned. */
442 tui_display_registers_from_line (int line_no,
445 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
447 int line, element_no;
451 else if (force_display)
452 { /* If we must display regs (force_display is true), then
453 make sure that we don't display off the end of the
455 if (line_no >= tui_last_regs_line_no ())
457 if ((line = tui_line_from_reg_element_no (
458 TUI_DATA_WIN->detail.data_display_info.regs_content_count - 1)) < 0)
467 element_no = tui_first_reg_element_no_inline (line);
469 < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
470 tui_display_reg_element_at_line (element_no, line);
477 return (-1); /* Nothing was displayed. */
481 /* This function check all displayed registers for changes in values,
482 given a particular frame. If the values have changed, they are
483 updated with the new value and highlighted. */
485 tui_check_register_values (struct frame_info *frame)
487 if (TUI_DATA_WIN != NULL
488 && TUI_DATA_WIN->generic.is_visible)
490 struct tui_data_info *display_info
491 = &TUI_DATA_WIN->detail.data_display_info;
493 if (display_info->regs_content_count <= 0
494 && display_info->display_regs)
495 tui_show_registers (display_info->current_group);
500 for (i = 0; (i < display_info->regs_content_count); i++)
502 struct tui_data_element *data;
503 struct tui_gen_win_info *data_item_win_ptr;
506 data_item_win_ptr = &display_info->regs_content[i]->
507 which_element.data_window;
508 data = &data_item_win_ptr->content[0]->which_element.data;
509 was_hilighted = data->highlight;
511 tui_get_register (frame, data,
512 data->item_no, &data->highlight);
514 if (data->highlight || was_hilighted)
516 tui_display_register (data, data_item_win_ptr);
523 /* Display a register in a window. If hilite is TRUE, then the value
524 will be displayed in reverse video. */
526 tui_display_register (struct tui_data_element *data,
527 struct tui_gen_win_info *win_info)
529 if (win_info->handle != (WINDOW *) NULL)
534 /* We ignore the return value, casting it to void in order to avoid
535 a compiler warning. The warning itself was introduced by a patch
536 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
537 to code that causes the compiler to generate an unused-value
539 (void) wstandout (win_info->handle);
541 wmove (win_info->handle, 0, 0);
542 for (i = 1; i < win_info->width; i++)
543 waddch (win_info->handle, ' ');
544 wmove (win_info->handle, 0, 0);
546 waddstr (win_info->handle, data->content);
549 /* We ignore the return value, casting it to void in order to avoid
550 a compiler warning. The warning itself was introduced by a patch
551 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
552 to code that causes the compiler to generate an unused-value
554 (void) wstandend (win_info->handle);
555 tui_refresh_win (win_info);
560 tui_reg_next_command (char *arg, int from_tty)
562 struct gdbarch *gdbarch = get_current_arch ();
564 if (TUI_DATA_WIN != NULL)
566 struct reggroup *group
567 = TUI_DATA_WIN->detail.data_display_info.current_group;
569 group = reggroup_next (gdbarch, group);
571 group = reggroup_next (gdbarch, NULL);
574 tui_show_registers (group);
579 tui_reg_float_command (char *arg, int from_tty)
581 tui_show_registers (float_reggroup);
585 tui_reg_general_command (char *arg, int from_tty)
587 tui_show_registers (general_reggroup);
591 tui_reg_system_command (char *arg, int from_tty)
593 tui_show_registers (system_reggroup);
596 static struct cmd_list_element *tuireglist;
599 tui_reg_command (char *args, int from_tty)
601 printf_unfiltered (_("\"tui reg\" must be followed by the name of a "
602 "tui reg command.\n"));
603 help_list (tuireglist, "tui reg ", all_commands, gdb_stdout);
606 /* Provide a prototype to silence -Wmissing-prototypes. */
607 extern initialize_file_ftype _initialize_tui_regs;
610 _initialize_tui_regs (void)
612 struct cmd_list_element **tuicmd;
614 tuicmd = tui_get_cmd_list ();
616 add_prefix_cmd ("reg", class_tui, tui_reg_command,
617 _("TUI commands to control the register window."),
618 &tuireglist, "tui reg ", 0,
621 add_cmd ("float", class_tui, tui_reg_float_command,
622 _("Display only floating point registers."),
624 add_cmd ("general", class_tui, tui_reg_general_command,
625 _("Display only general registers."),
627 add_cmd ("system", class_tui, tui_reg_system_command,
628 _("Display only system registers."),
630 add_cmd ("next", class_tui, tui_reg_next_command,
631 _("Display next register group."),
636 /*****************************************
637 ** STATIC LOCAL FUNCTIONS **
638 ******************************************/
641 tui_restore_gdbout (void *ui)
643 ui_file_delete (gdb_stdout);
644 gdb_stdout = (struct ui_file*) ui;
645 pagination_enabled = 1;
648 /* Get the register from the frame and return a printable
649 representation of it. */
652 tui_register_format (struct frame_info *frame, int regnum)
654 struct gdbarch *gdbarch = get_frame_arch (frame);
655 struct ui_file *stream;
656 struct ui_file *old_stdout;
657 struct cleanup *cleanups;
661 pagination_enabled = 0;
662 old_stdout = gdb_stdout;
663 stream = tui_sfileopen (256);
665 cleanups = make_cleanup (tui_restore_gdbout, (void*) old_stdout);
666 gdbarch_print_registers_info (gdbarch, stream, frame, regnum, 1);
668 /* Save formatted output in the buffer. */
669 p = tui_file_get_strbuf (stream);
671 /* Remove the possible \n. */
672 s = strrchr (p, '\n');
676 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
677 ret = tui_expand_tabs (p, 0);
679 do_cleanups (cleanups);
684 /* Get the register value from the given frame and format it for the
685 display. When changep is set, check if the new register value has
686 changed with respect to the previous call. */
687 static enum tui_status
688 tui_get_register (struct frame_info *frame,
689 struct tui_data_element *data,
690 int regnum, int *changedp)
692 enum tui_status ret = TUI_FAILURE;
696 if (target_has_registers)
698 char *prev_content = data->content;
700 data->content = tui_register_format (frame, regnum);
703 && strcmp (prev_content, data->content) != 0)
706 xfree (prev_content);