1 /* TUI display registers in window.
3 Copyright (C) 1998-2019 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"
42 #include "completer.h"
44 #include "gdb_curses.h"
47 /*****************************************
48 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
49 ******************************************/
51 tui_display_register (struct tui_data_element *data,
52 struct tui_gen_win_info *win_info);
54 static enum tui_status tui_show_register_group (struct reggroup *group,
55 struct frame_info *frame,
56 int refresh_values_only);
58 static enum tui_status tui_get_register (struct frame_info *frame,
59 struct tui_data_element *data,
60 int regnum, int *changedp);
64 /*****************************************
65 ** PUBLIC FUNCTIONS **
66 ******************************************/
68 /* Answer the number of the last line in the regs display. If there
69 are no registers (-1) is returned. */
71 tui_last_regs_line_no (void)
75 if (TUI_DATA_WIN->regs_content_count > 0)
77 num_lines = (TUI_DATA_WIN->regs_content_count
78 / TUI_DATA_WIN->regs_column_count);
79 if (TUI_DATA_WIN->regs_content_count % TUI_DATA_WIN->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->regs_content_count)
99 if (element_no < TUI_DATA_WIN->regs_column_count * i)
112 /* Answer the index of the first element in line_no. If line_no is
113 past the register area (-1) is returned. */
115 tui_first_reg_element_no_inline (int line_no)
117 if ((line_no * TUI_DATA_WIN->regs_column_count)
118 <= TUI_DATA_WIN->regs_content_count)
119 return (((line_no + 1) * TUI_DATA_WIN->regs_column_count)
120 - TUI_DATA_WIN->regs_column_count);
126 /* Show the registers of the given group in the data window
127 and refresh the window. */
129 tui_show_registers (struct reggroup *group)
131 enum tui_status ret = TUI_FAILURE;
133 /* Make sure the curses mode is enabled. */
136 /* Make sure the register window is visible. If not, select an
137 appropriate layout. */
138 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
139 tui_set_layout_by_name (DATA_NAME);
142 group = general_reggroup;
144 /* Say that registers should be displayed, even if there is a
146 TUI_DATA_WIN->display_regs = TRUE;
148 if (target_has_registers && target_has_stack && target_has_memory)
150 ret = tui_show_register_group (group, get_selected_frame (NULL),
151 group == TUI_DATA_WIN->current_group);
153 if (ret == TUI_FAILURE)
155 TUI_DATA_WIN->current_group = 0;
156 tui_erase_data_content (NO_REGS_STRING);
162 /* Clear all notation of changed values. */
163 for (i = 0; i < TUI_DATA_WIN->regs_content_count; i++)
165 struct tui_gen_win_info *data_item_win;
166 struct tui_win_element *win;
168 data_item_win = &TUI_DATA_WIN->regs_content[i]
169 ->which_element.data_window;
170 win = data_item_win->content[0];
171 win->which_element.data.highlight = FALSE;
173 TUI_DATA_WIN->current_group = group;
174 tui_display_all_data ();
179 /* Set the data window to display the registers of the register group
180 using the given frame. Values are refreshed only when
181 refresh_values_only is TRUE. */
183 static enum tui_status
184 tui_show_register_group (struct reggroup *group,
185 struct frame_info *frame,
186 int refresh_values_only)
188 struct gdbarch *gdbarch = get_frame_arch (frame);
189 enum tui_status ret = TUI_FAILURE;
191 int allocated_here = FALSE;
195 /* Make a new title showing which group we display. */
196 snprintf (title, sizeof (title) - 1, "Register group: %s",
197 reggroup_name (group));
198 xfree (TUI_DATA_WIN->generic.title);
199 TUI_DATA_WIN->generic.title = xstrdup (title);
201 /* See how many registers must be displayed. */
203 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
207 /* Must be in the group. */
208 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
211 /* If the register name is empty, it is undefined for this
212 processor, so don't display anything. */
213 name = gdbarch_register_name (gdbarch, regnum);
214 if (name == 0 || *name == '\0')
220 if (TUI_DATA_WIN->regs_content_count > 0 && !refresh_values_only)
222 tui_free_data_content (TUI_DATA_WIN->regs_content,
223 TUI_DATA_WIN->regs_content_count);
224 TUI_DATA_WIN->regs_content_count = 0;
227 if (TUI_DATA_WIN->regs_content_count <= 0)
229 TUI_DATA_WIN->regs_content = tui_alloc_content (nr_regs, DATA_WIN);
230 allocated_here = TRUE;
231 refresh_values_only = FALSE;
234 if (TUI_DATA_WIN->regs_content != NULL)
236 if (!refresh_values_only || allocated_here)
238 TUI_DATA_WIN->generic.content = NULL;
239 TUI_DATA_WIN->generic.content_size = 0;
240 tui_add_content_elements (&TUI_DATA_WIN->generic, nr_regs);
241 TUI_DATA_WIN->regs_content = TUI_DATA_WIN->generic.content;
242 TUI_DATA_WIN->regs_content_count = nr_regs;
245 /* Now set the register names and values. */
247 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
249 struct tui_gen_win_info *data_item_win;
250 struct tui_data_element *data;
253 /* Must be in the group. */
254 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
257 /* If the register name is empty, it is undefined for this
258 processor, so don't display anything. */
259 name = gdbarch_register_name (gdbarch, regnum);
260 if (name == 0 || *name == '\0')
264 &TUI_DATA_WIN->regs_content[pos]->which_element.data_window;
265 data = &data_item_win->content[0]->which_element.data;
268 if (!refresh_values_only)
270 data->item_no = regnum;
272 data->highlight = FALSE;
274 tui_get_register (frame, data, regnum, 0);
279 TUI_DATA_WIN->generic.content_size =
280 TUI_DATA_WIN->regs_content_count + TUI_DATA_WIN->data_content_count;
287 /* Function to display the registers in the content from
288 'start_element_no' until the end of the register content or the end
289 of the display height. No checking for displaying past the end of
290 the registers is done here. */
292 tui_display_registers_from (int start_element_no)
294 if (TUI_DATA_WIN->regs_content != NULL
295 && TUI_DATA_WIN->regs_content_count > 0)
297 int i = start_element_no;
298 int j, item_win_width, cur_y;
301 for (i = 0; i < TUI_DATA_WIN->regs_content_count; i++)
303 struct tui_data_element *data;
304 struct tui_gen_win_info *data_item_win;
309 = &TUI_DATA_WIN->regs_content[i]->which_element.data_window;
310 data = &data_item_win->content[0]->which_element.data;
317 len = 8 * ((len / 8) + 1);
325 item_win_width = max_len + 1;
326 i = start_element_no;
328 TUI_DATA_WIN->regs_column_count =
329 (TUI_DATA_WIN->generic.width - 2) / item_win_width;
330 if (TUI_DATA_WIN->regs_column_count == 0)
331 TUI_DATA_WIN->regs_column_count = 1;
333 (TUI_DATA_WIN->generic.width - 2) / TUI_DATA_WIN->regs_column_count;
335 /* Now create each data "sub" window, and write the display into
338 while (i < TUI_DATA_WIN->regs_content_count
339 && cur_y <= TUI_DATA_WIN->generic.viewport_height)
342 j < TUI_DATA_WIN->regs_column_count
343 && i < TUI_DATA_WIN->regs_content_count;
346 struct tui_gen_win_info *data_item_win;
347 struct tui_data_element *data_element_ptr;
349 /* Create the window if necessary. */
350 data_item_win = &TUI_DATA_WIN->regs_content[i]
351 ->which_element.data_window;
352 data_element_ptr = &data_item_win->content[0]->which_element.data;
353 if (data_item_win->handle != NULL
354 && (data_item_win->height != 1
355 || data_item_win->width != item_win_width
356 || data_item_win->origin.x != (item_win_width * j) + 1
357 || data_item_win->origin.y != cur_y))
359 tui_delete_win (data_item_win->handle);
360 data_item_win->handle = 0;
363 if (data_item_win->handle == NULL)
365 data_item_win->height = 1;
366 data_item_win->width = item_win_width;
367 data_item_win->origin.x = (item_win_width * j) + 1;
368 data_item_win->origin.y = cur_y;
369 tui_make_window (data_item_win, DONT_BOX_WINDOW);
370 scrollok (data_item_win->handle, FALSE);
372 touchwin (data_item_win->handle);
374 /* Get the printable representation of the register
376 tui_display_register (data_element_ptr, data_item_win);
377 i++; /* Next register. */
379 cur_y++; /* Next row. */
385 /* Function to display the registers in the content from
386 'start_element_no' on 'start_line_no' until the end of the register
387 content or the end of the display height. This function checks
388 that we won't display off the end of the register display. */
390 tui_display_reg_element_at_line (int start_element_no,
393 if (TUI_DATA_WIN->regs_content != NULL
394 && TUI_DATA_WIN->regs_content_count > 0)
396 int element_no = start_element_no;
398 if (start_element_no != 0 && start_line_no != 0)
400 int last_line_no, first_line_on_last_page;
402 last_line_no = tui_last_regs_line_no ();
403 first_line_on_last_page
404 = last_line_no - (TUI_DATA_WIN->generic.height - 2);
405 if (first_line_on_last_page < 0)
406 first_line_on_last_page = 0;
408 /* If there is no other data displayed except registers, and
409 the element_no causes us to scroll past the end of the
410 registers, adjust what element to really start the
412 if (TUI_DATA_WIN->data_content_count <= 0
413 && start_line_no > first_line_on_last_page)
415 = tui_first_reg_element_no_inline (first_line_on_last_page);
417 tui_display_registers_from (element_no);
423 /* Function to display the registers starting at line line_no in the
424 data window. Answers the line number that the display actually
425 started from. If nothing is displayed (-1) is returned. */
427 tui_display_registers_from_line (int line_no,
430 if (TUI_DATA_WIN->regs_content_count > 0)
432 int line, element_no;
436 else if (force_display)
437 { /* If we must display regs (force_display is true), then
438 make sure that we don't display off the end of the
440 if (line_no >= tui_last_regs_line_no ())
442 if ((line = tui_line_from_reg_element_no (
443 TUI_DATA_WIN->regs_content_count - 1)) < 0)
452 element_no = tui_first_reg_element_no_inline (line);
454 < TUI_DATA_WIN->regs_content_count)
455 tui_display_reg_element_at_line (element_no, line);
462 return (-1); /* Nothing was displayed. */
466 /* This function check all displayed registers for changes in values,
467 given a particular frame. If the values have changed, they are
468 updated with the new value and highlighted. */
470 tui_check_register_values (struct frame_info *frame)
472 if (TUI_DATA_WIN != NULL
473 && TUI_DATA_WIN->generic.is_visible)
475 if (TUI_DATA_WIN->regs_content_count <= 0
476 && TUI_DATA_WIN->display_regs)
477 tui_show_registers (TUI_DATA_WIN->current_group);
482 for (i = 0; (i < TUI_DATA_WIN->regs_content_count); i++)
484 struct tui_data_element *data;
485 struct tui_gen_win_info *data_item_win_ptr;
488 data_item_win_ptr = &TUI_DATA_WIN->regs_content[i]->
489 which_element.data_window;
490 data = &data_item_win_ptr->content[0]->which_element.data;
491 was_hilighted = data->highlight;
493 tui_get_register (frame, data,
494 data->item_no, &data->highlight);
496 if (data->highlight || was_hilighted)
498 tui_display_register (data, data_item_win_ptr);
505 /* Display a register in a window. If hilite is TRUE, then the value
506 will be displayed in reverse video. */
508 tui_display_register (struct tui_data_element *data,
509 struct tui_gen_win_info *win_info)
511 if (win_info->handle != NULL)
516 /* We ignore the return value, casting it to void in order to avoid
517 a compiler warning. The warning itself was introduced by a patch
518 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
519 to code that causes the compiler to generate an unused-value
521 (void) wstandout (win_info->handle);
523 wmove (win_info->handle, 0, 0);
524 for (i = 1; i < win_info->width; i++)
525 waddch (win_info->handle, ' ');
526 wmove (win_info->handle, 0, 0);
528 waddstr (win_info->handle, data->content);
531 /* We ignore the return value, casting it to void in order to avoid
532 a compiler warning. The warning itself was introduced by a patch
533 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
534 to code that causes the compiler to generate an unused-value
536 (void) wstandend (win_info->handle);
537 tui_refresh_win (win_info);
541 /* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
542 around behaviour. Returns the next register group, or NULL if the
543 register window is not currently being displayed. */
545 static struct reggroup *
546 tui_reg_next (struct gdbarch *gdbarch)
548 struct reggroup *group = NULL;
550 if (TUI_DATA_WIN != NULL)
552 group = TUI_DATA_WIN->current_group;
553 group = reggroup_next (gdbarch, group);
555 group = reggroup_next (gdbarch, NULL);
560 /* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
561 around behaviour. Returns the previous register group, or NULL if the
562 register window is not currently being displayed. */
564 static struct reggroup *
565 tui_reg_prev (struct gdbarch *gdbarch)
567 struct reggroup *group = NULL;
569 if (TUI_DATA_WIN != NULL)
571 group = TUI_DATA_WIN->current_group;
572 group = reggroup_prev (gdbarch, group);
574 group = reggroup_prev (gdbarch, NULL);
579 /* Implement the 'tui reg' command. Changes the register group displayed
580 in the tui register window. Displays the tui register window if it is
581 not already on display. */
584 tui_reg_command (const char *args, int from_tty)
586 struct gdbarch *gdbarch = get_current_arch ();
590 struct reggroup *group, *match = NULL;
591 size_t len = strlen (args);
593 /* Make sure the curses mode is enabled. */
596 /* Make sure the register window is visible. If not, select an
597 appropriate layout. We need to do this before trying to run the
598 'next' or 'prev' commands. */
599 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
600 tui_set_layout_by_name (DATA_NAME);
602 if (strncmp (args, "next", len) == 0)
603 match = tui_reg_next (gdbarch);
604 else if (strncmp (args, "prev", len) == 0)
605 match = tui_reg_prev (gdbarch);
607 /* This loop matches on the initial part of a register group
608 name. If this initial part in ARGS matches only one register
609 group then the switch is made. */
610 for (group = reggroup_next (gdbarch, NULL);
612 group = reggroup_next (gdbarch, group))
614 if (strncmp (reggroup_name (group), args, len) == 0)
617 error (_("ambiguous register group name '%s'"), args);
623 error (_("unknown register group '%s'"), args);
625 tui_show_registers (match);
629 struct reggroup *group;
632 printf_unfiltered (_("\"tui reg\" must be followed by the name of "
633 "either a register group,\nor one of 'next' "
634 "or 'prev'. Known register groups are:\n"));
636 for (first = 1, group = reggroup_next (gdbarch, NULL);
638 first = 0, group = reggroup_next (gdbarch, group))
641 printf_unfiltered (", ");
642 printf_unfiltered ("%s", reggroup_name (group));
645 printf_unfiltered ("\n");
649 /* Complete names of register groups, and add the special "prev" and "next"
653 tui_reggroup_completer (struct cmd_list_element *ignore,
654 completion_tracker &tracker,
655 const char *text, const char *word)
657 static const char *extra[] = { "next", "prev", NULL };
658 size_t len = strlen (word);
661 reggroup_completer (ignore, tracker, text, word);
663 /* XXXX use complete_on_enum instead? */
664 for (tmp = extra; *tmp != NULL; ++tmp)
666 if (strncmp (word, *tmp, len) == 0)
667 tracker.add_completion (make_unique_xstrdup (*tmp));
672 _initialize_tui_regs (void)
674 struct cmd_list_element **tuicmd, *cmd;
676 tuicmd = tui_get_cmd_list ();
678 cmd = add_cmd ("reg", class_tui, tui_reg_command, _("\
679 TUI command to control the register window."), tuicmd);
680 set_cmd_completer (cmd, tui_reggroup_completer);
684 /*****************************************
685 ** STATIC LOCAL FUNCTIONS **
686 ******************************************/
688 /* Get the register from the frame and return a printable
689 representation of it. */
692 tui_register_format (struct frame_info *frame, int regnum)
694 struct gdbarch *gdbarch = get_frame_arch (frame);
698 scoped_restore save_pagination
699 = make_scoped_restore (&pagination_enabled, 0);
700 scoped_restore save_stdout
701 = make_scoped_restore (&gdb_stdout, &stream);
703 gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1);
705 /* Remove the possible \n. */
706 std::string &str = stream.string ();
707 if (!str.empty () && str.back () == '\n')
708 str.resize (str.size () - 1);
710 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
711 return tui_expand_tabs (str.c_str (), 0);
714 /* Get the register value from the given frame and format it for the
715 display. When changep is set, check if the new register value has
716 changed with respect to the previous call. */
717 static enum tui_status
718 tui_get_register (struct frame_info *frame,
719 struct tui_data_element *data,
720 int regnum, int *changedp)
722 enum tui_status ret = TUI_FAILURE;
726 if (target_has_registers)
728 char *prev_content = data->content;
730 data->content = tui_register_format (frame, regnum);
733 && strcmp (prev_content, data->content) != 0)
736 xfree (prev_content);