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 ******************************************/
50 static void tui_display_register (struct tui_data_item_window *data);
52 static void tui_show_register_group (struct reggroup *group,
53 struct frame_info *frame,
54 int refresh_values_only);
56 static enum tui_status tui_get_register (struct frame_info *frame,
57 struct tui_data_item_window *data,
58 int regnum, bool *changedp);
62 /*****************************************
63 ** PUBLIC FUNCTIONS **
64 ******************************************/
69 tui_data_window::last_regs_line_no () const
73 if (!regs_content.empty ())
75 num_lines = regs_content.size () / regs_column_count;
76 if (regs_content.size () % regs_column_count)
85 tui_data_window::line_from_reg_element_no (int element_no) const
87 if (element_no < regs_content.size ())
94 if (element_no < regs_column_count * i)
106 /* See tui-data.h. */
109 tui_data_window::first_reg_element_no_inline (int line_no) const
111 if (line_no * regs_column_count <= regs_content.size ())
112 return ((line_no + 1) * regs_column_count) - regs_column_count;
118 /* Show the registers of the given group in the data window
119 and refresh the window. */
121 tui_show_registers (struct reggroup *group)
123 enum tui_status ret = TUI_FAILURE;
125 /* Make sure the curses mode is enabled. */
128 /* Make sure the register window is visible. If not, select an
129 appropriate layout. */
130 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->is_visible)
131 tui_set_layout_by_name (DATA_NAME);
134 group = general_reggroup;
136 /* Say that registers should be displayed, even if there is a
138 TUI_DATA_WIN->display_regs = true;
140 if (target_has_registers && target_has_stack && target_has_memory)
142 tui_show_register_group (group, get_selected_frame (NULL),
143 group == TUI_DATA_WIN->current_group);
146 if (ret == TUI_FAILURE)
148 TUI_DATA_WIN->current_group = 0;
149 tui_erase_data_content (NO_REGS_STRING);
153 /* Clear all notation of changed values. */
154 for (auto &&data_item_win : TUI_DATA_WIN->regs_content)
156 if (data_item_win != nullptr)
157 data_item_win->highlight = false;
159 TUI_DATA_WIN->current_group = group;
160 tui_display_all_data ();
165 /* Set the data window to display the registers of the register group
166 using the given frame. Values are refreshed only when
167 refresh_values_only is TRUE. */
170 tui_show_register_group (struct reggroup *group,
171 struct frame_info *frame,
172 int refresh_values_only)
174 struct gdbarch *gdbarch = get_frame_arch (frame);
179 /* Make a new title showing which group we display. */
180 snprintf (title, sizeof (title) - 1, "Register group: %s",
181 reggroup_name (group));
182 xfree (TUI_DATA_WIN->title);
183 TUI_DATA_WIN->title = xstrdup (title);
185 /* See how many registers must be displayed. */
187 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
191 /* Must be in the group. */
192 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
195 /* If the register name is empty, it is undefined for this
196 processor, so don't display anything. */
197 name = gdbarch_register_name (gdbarch, regnum);
198 if (name == 0 || *name == '\0')
204 if (!refresh_values_only)
205 TUI_DATA_WIN->regs_content.clear ();
207 if (nr_regs < TUI_DATA_WIN->regs_content.size ())
208 TUI_DATA_WIN->regs_content.resize (nr_regs);
211 for (int i = TUI_DATA_WIN->regs_content.size (); i < nr_regs; ++i)
212 TUI_DATA_WIN->regs_content.emplace_back (new tui_data_item_window ());
215 /* Now set the register names and values. */
217 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
219 struct tui_data_item_window *data_item_win;
222 /* Must be in the group. */
223 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
226 /* If the register name is empty, it is undefined for this
227 processor, so don't display anything. */
228 name = gdbarch_register_name (gdbarch, regnum);
229 if (name == 0 || *name == '\0')
232 data_item_win = TUI_DATA_WIN->regs_content[pos].get ();
235 if (!refresh_values_only)
237 data_item_win->item_no = regnum;
238 data_item_win->name = name;
239 data_item_win->highlight = false;
241 tui_get_register (frame, data_item_win, regnum, 0);
247 /* Function to display the registers in the content from
248 'start_element_no' until the end of the register content or the end
249 of the display height. No checking for displaying past the end of
250 the registers is done here. */
252 tui_display_registers_from (int start_element_no)
254 if (!TUI_DATA_WIN->regs_content.empty ())
256 int j, item_win_width, cur_y;
259 for (auto &&data_item_win : TUI_DATA_WIN->regs_content)
265 p = data_item_win->content;
270 len = 8 * ((len / 8) + 1);
278 item_win_width = max_len + 1;
279 int i = start_element_no;
281 TUI_DATA_WIN->regs_column_count =
282 (TUI_DATA_WIN->width - 2) / item_win_width;
283 if (TUI_DATA_WIN->regs_column_count == 0)
284 TUI_DATA_WIN->regs_column_count = 1;
286 (TUI_DATA_WIN->width - 2) / TUI_DATA_WIN->regs_column_count;
288 /* Now create each data "sub" window, and write the display into
291 while (i < TUI_DATA_WIN->regs_content.size ()
292 && cur_y <= TUI_DATA_WIN->viewport_height)
295 j < TUI_DATA_WIN->regs_column_count
296 && i < TUI_DATA_WIN->regs_content.size ();
299 struct tui_data_item_window *data_item_win;
301 /* Create the window if necessary. */
302 data_item_win = TUI_DATA_WIN->regs_content[i].get ();
303 if (data_item_win->handle != NULL
304 && (data_item_win->height != 1
305 || data_item_win->width != item_win_width
306 || data_item_win->origin.x != (item_win_width * j) + 1
307 || data_item_win->origin.y != cur_y))
309 tui_delete_win (data_item_win->handle);
310 data_item_win->handle = 0;
313 if (data_item_win->handle == NULL)
315 data_item_win->height = 1;
316 data_item_win->width = item_win_width;
317 data_item_win->origin.x = (item_win_width * j) + 1;
318 data_item_win->origin.y = cur_y;
319 tui_make_window (data_item_win, DONT_BOX_WINDOW);
320 scrollok (data_item_win->handle, FALSE);
322 touchwin (data_item_win->handle);
324 /* Get the printable representation of the register
326 tui_display_register (data_item_win);
327 i++; /* Next register. */
329 cur_y++; /* Next row. */
335 /* Function to display the registers in the content from
336 'start_element_no' on 'start_line_no' until the end of the register
337 content or the end of the display height. This function checks
338 that we won't display off the end of the register display. */
340 tui_display_reg_element_at_line (int start_element_no,
343 if (!TUI_DATA_WIN->regs_content.empty ())
345 int element_no = start_element_no;
347 if (start_element_no != 0 && start_line_no != 0)
349 int last_line_no, first_line_on_last_page;
351 last_line_no = TUI_DATA_WIN->last_regs_line_no ();
352 first_line_on_last_page
353 = last_line_no - (TUI_DATA_WIN->height - 2);
354 if (first_line_on_last_page < 0)
355 first_line_on_last_page = 0;
357 /* If the element_no causes us to scroll past the end of the
358 registers, adjust what element to really start the
360 if (start_line_no > first_line_on_last_page)
362 = (TUI_DATA_WIN->first_reg_element_no_inline
363 (first_line_on_last_page));
365 tui_display_registers_from (element_no);
371 /* Function to display the registers starting at line line_no in the
372 data window. Answers the line number that the display actually
373 started from. If nothing is displayed (-1) is returned. */
375 tui_display_registers_from_line (int line_no)
377 tui_check_and_display_highlight_if_needed (TUI_DATA_WIN);
378 if (!TUI_DATA_WIN->regs_content.empty ())
386 /* Make sure that we don't display off the end of the
388 if (line_no >= TUI_DATA_WIN->last_regs_line_no ())
390 if ((line_no = TUI_DATA_WIN->line_from_reg_element_no (
391 TUI_DATA_WIN->regs_content.size () - 1)) < 0)
396 element_no = TUI_DATA_WIN->first_reg_element_no_inline (line_no);
397 if (element_no < TUI_DATA_WIN->regs_content.size ())
398 tui_display_reg_element_at_line (element_no, line_no);
405 return (-1); /* Nothing was displayed. */
409 /* This function check all displayed registers for changes in values,
410 given a particular frame. If the values have changed, they are
411 updated with the new value and highlighted. */
413 tui_check_register_values (struct frame_info *frame)
415 if (TUI_DATA_WIN != NULL
416 && TUI_DATA_WIN->is_visible)
418 if (TUI_DATA_WIN->regs_content.empty ()
419 && TUI_DATA_WIN->display_regs)
420 tui_show_registers (TUI_DATA_WIN->current_group);
423 for (auto &&data_item_win_ptr : TUI_DATA_WIN->regs_content)
427 was_hilighted = data_item_win_ptr->highlight;
429 tui_get_register (frame, data_item_win_ptr.get (),
430 data_item_win_ptr->item_no,
431 &data_item_win_ptr->highlight);
433 if (data_item_win_ptr->highlight || was_hilighted)
434 tui_display_register (data_item_win_ptr.get ());
440 /* Display a register in a window. If hilite is TRUE, then the value
441 will be displayed in reverse video. */
443 tui_display_register (struct tui_data_item_window *data)
445 if (data->handle != NULL)
450 /* We ignore the return value, casting it to void in order to avoid
451 a compiler warning. The warning itself was introduced by a patch
452 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
453 to code that causes the compiler to generate an unused-value
455 (void) wstandout (data->handle);
457 wmove (data->handle, 0, 0);
458 for (i = 1; i < data->width; i++)
459 waddch (data->handle, ' ');
460 wmove (data->handle, 0, 0);
462 waddstr (data->handle, data->content);
465 /* We ignore the return value, casting it to void in order to avoid
466 a compiler warning. The warning itself was introduced by a patch
467 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
468 to code that causes the compiler to generate an unused-value
470 (void) wstandend (data->handle);
471 data->refresh_window ();
475 /* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
476 around behaviour. Returns the next register group, or NULL if the
477 register window is not currently being displayed. */
479 static struct reggroup *
480 tui_reg_next (struct gdbarch *gdbarch)
482 struct reggroup *group = NULL;
484 if (TUI_DATA_WIN != NULL)
486 group = TUI_DATA_WIN->current_group;
487 group = reggroup_next (gdbarch, group);
489 group = reggroup_next (gdbarch, NULL);
494 /* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
495 around behaviour. Returns the previous register group, or NULL if the
496 register window is not currently being displayed. */
498 static struct reggroup *
499 tui_reg_prev (struct gdbarch *gdbarch)
501 struct reggroup *group = NULL;
503 if (TUI_DATA_WIN != NULL)
505 group = TUI_DATA_WIN->current_group;
506 group = reggroup_prev (gdbarch, group);
508 group = reggroup_prev (gdbarch, NULL);
513 /* Implement the 'tui reg' command. Changes the register group displayed
514 in the tui register window. Displays the tui register window if it is
515 not already on display. */
518 tui_reg_command (const char *args, int from_tty)
520 struct gdbarch *gdbarch = get_current_arch ();
524 struct reggroup *group, *match = NULL;
525 size_t len = strlen (args);
527 /* Make sure the curses mode is enabled. */
530 /* Make sure the register window is visible. If not, select an
531 appropriate layout. We need to do this before trying to run the
532 'next' or 'prev' commands. */
533 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->is_visible)
534 tui_set_layout_by_name (DATA_NAME);
536 if (strncmp (args, "next", len) == 0)
537 match = tui_reg_next (gdbarch);
538 else if (strncmp (args, "prev", len) == 0)
539 match = tui_reg_prev (gdbarch);
541 /* This loop matches on the initial part of a register group
542 name. If this initial part in ARGS matches only one register
543 group then the switch is made. */
544 for (group = reggroup_next (gdbarch, NULL);
546 group = reggroup_next (gdbarch, group))
548 if (strncmp (reggroup_name (group), args, len) == 0)
551 error (_("ambiguous register group name '%s'"), args);
557 error (_("unknown register group '%s'"), args);
559 tui_show_registers (match);
563 struct reggroup *group;
566 printf_unfiltered (_("\"tui reg\" must be followed by the name of "
567 "either a register group,\nor one of 'next' "
568 "or 'prev'. Known register groups are:\n"));
570 for (first = 1, group = reggroup_next (gdbarch, NULL);
572 first = 0, group = reggroup_next (gdbarch, group))
575 printf_unfiltered (", ");
576 printf_unfiltered ("%s", reggroup_name (group));
579 printf_unfiltered ("\n");
583 /* Complete names of register groups, and add the special "prev" and "next"
587 tui_reggroup_completer (struct cmd_list_element *ignore,
588 completion_tracker &tracker,
589 const char *text, const char *word)
591 static const char *extra[] = { "next", "prev", NULL };
592 size_t len = strlen (word);
595 reggroup_completer (ignore, tracker, text, word);
597 /* XXXX use complete_on_enum instead? */
598 for (tmp = extra; *tmp != NULL; ++tmp)
600 if (strncmp (word, *tmp, len) == 0)
601 tracker.add_completion (make_unique_xstrdup (*tmp));
606 _initialize_tui_regs (void)
608 struct cmd_list_element **tuicmd, *cmd;
610 tuicmd = tui_get_cmd_list ();
612 cmd = add_cmd ("reg", class_tui, tui_reg_command, _("\
613 TUI command to control the register window."), tuicmd);
614 set_cmd_completer (cmd, tui_reggroup_completer);
618 /*****************************************
619 ** STATIC LOCAL FUNCTIONS **
620 ******************************************/
622 /* Get the register from the frame and return a printable
623 representation of it. */
626 tui_register_format (struct frame_info *frame, int regnum)
628 struct gdbarch *gdbarch = get_frame_arch (frame);
632 scoped_restore save_pagination
633 = make_scoped_restore (&pagination_enabled, 0);
634 scoped_restore save_stdout
635 = make_scoped_restore (&gdb_stdout, &stream);
637 gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1);
639 /* Remove the possible \n. */
640 std::string &str = stream.string ();
641 if (!str.empty () && str.back () == '\n')
642 str.resize (str.size () - 1);
644 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
645 return tui_expand_tabs (str.c_str (), 0);
648 /* Get the register value from the given frame and format it for the
649 display. When changep is set, check if the new register value has
650 changed with respect to the previous call. */
651 static enum tui_status
652 tui_get_register (struct frame_info *frame,
653 struct tui_data_item_window *data,
654 int regnum, bool *changedp)
656 enum tui_status ret = TUI_FAILURE;
660 if (target_has_registers)
662 char *prev_content = data->content;
664 data->content = tui_register_format (frame, regnum);
667 && strcmp (prev_content, data->content) != 0)
670 xfree (prev_content);