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-wingeneral.h"
36 #include "tui/tui-file.h"
37 #include "tui/tui-regs.h"
38 #include "tui/tui-io.h"
39 #include "reggroups.h"
41 #include "completer.h"
43 #include "gdb_curses.h"
45 static void tui_display_register (struct tui_data_item_window *data);
47 /* Get the register from the frame and return a printable
48 representation of it. */
50 static gdb::unique_xmalloc_ptr<char>
51 tui_register_format (struct frame_info *frame, int regnum)
53 struct gdbarch *gdbarch = get_frame_arch (frame);
57 scoped_restore save_pagination
58 = make_scoped_restore (&pagination_enabled, 0);
59 scoped_restore save_stdout
60 = make_scoped_restore (&gdb_stdout, &stream);
62 gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1);
64 /* Remove the possible \n. */
65 std::string &str = stream.string ();
66 if (!str.empty () && str.back () == '\n')
67 str.resize (str.size () - 1);
69 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
70 return tui_expand_tabs (str.c_str ());
73 /* Get the register value from the given frame and format it for the
74 display. When changep is set, check if the new register value has
75 changed with respect to the previous call. */
77 tui_get_register (struct frame_info *frame,
78 struct tui_data_item_window *data,
79 int regnum, bool *changedp)
83 if (target_has_registers)
85 gdb::unique_xmalloc_ptr<char> new_content
86 = tui_register_format (frame, regnum);
89 && strcmp (data->content.get (), new_content.get ()) != 0)
92 data->content = std::move (new_content);
99 tui_data_window::last_regs_line_no () const
101 int num_lines = (-1);
103 if (!regs_content.empty ())
105 num_lines = regs_content.size () / regs_column_count;
106 if (regs_content.size () % regs_column_count)
112 /* See tui-regs.h. */
115 tui_data_window::line_from_reg_element_no (int element_no) const
117 if (element_no < regs_content.size ())
124 if (element_no < regs_column_count * i)
136 /* See tui-regs.h. */
139 tui_data_window::first_reg_element_no_inline (int line_no) const
141 if (line_no * regs_column_count <= regs_content.size ())
142 return ((line_no + 1) * regs_column_count) - regs_column_count;
147 /* Show the registers of the given group in the data window
148 and refresh the window. */
150 tui_data_window::show_registers (struct reggroup *group)
153 group = general_reggroup;
155 /* Say that registers should be displayed, even if there is a
159 if (target_has_registers && target_has_stack && target_has_memory)
161 show_register_group (group, get_selected_frame (NULL),
162 group == current_group);
164 /* Clear all notation of changed values. */
165 for (auto &&data_item_win : regs_content)
167 if (data_item_win != nullptr)
168 data_item_win->highlight = false;
170 current_group = group;
176 erase_data_content (_("[ Register Values Unavailable ]"));
181 /* Set the data window to display the registers of the register group
182 using the given frame. Values are refreshed only when
183 refresh_values_only is TRUE. */
186 tui_data_window::show_register_group (struct reggroup *group,
187 struct frame_info *frame,
188 int refresh_values_only)
190 struct gdbarch *gdbarch = get_frame_arch (frame);
194 /* Make a new title showing which group we display. */
196 title = xstrprintf ("Register group: %s", reggroup_name (group));
198 /* See how many registers must be displayed. */
200 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
204 /* Must be in the group. */
205 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
208 /* If the register name is empty, it is undefined for this
209 processor, so don't display anything. */
210 name = gdbarch_register_name (gdbarch, regnum);
211 if (name == 0 || *name == '\0')
217 if (!refresh_values_only)
218 regs_content.clear ();
220 if (nr_regs < regs_content.size ())
221 regs_content.resize (nr_regs);
224 for (int i = regs_content.size (); i < nr_regs; ++i)
225 regs_content.emplace_back (new tui_data_item_window ());
228 /* Now set the register names and values. */
230 for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
232 struct tui_data_item_window *data_item_win;
235 /* Must be in the group. */
236 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
239 /* If the register name is empty, it is undefined for this
240 processor, so don't display anything. */
241 name = gdbarch_register_name (gdbarch, regnum);
242 if (name == 0 || *name == '\0')
245 data_item_win = regs_content[pos].get ();
248 if (!refresh_values_only)
250 data_item_win->item_no = regnum;
251 data_item_win->name = name;
252 data_item_win->highlight = false;
254 tui_get_register (frame, data_item_win, regnum, 0);
260 /* See tui-regs.h. */
263 tui_data_window::display_registers_from (int start_element_no)
265 if (!regs_content.empty ())
267 int j, item_win_width, cur_y;
270 for (auto &&data_item_win : regs_content)
276 p = data_item_win->content.get ();
283 item_win_width = max_len + 1;
284 int i = start_element_no;
286 regs_column_count = (width - 2) / item_win_width;
287 if (regs_column_count == 0)
288 regs_column_count = 1;
289 item_win_width = (width - 2) / regs_column_count;
291 /* Now create each data "sub" window, and write the display into
294 while (i < regs_content.size ()
295 && cur_y <= viewport_height)
298 j < regs_column_count && i < regs_content.size ();
301 struct tui_data_item_window *data_item_win;
303 /* Create the window if necessary. */
304 data_item_win = regs_content[i].get ();
305 if (data_item_win->handle != NULL
306 && (data_item_win->height != 1
307 || data_item_win->width != item_win_width
308 || data_item_win->origin.x != (item_win_width * j) + 1
309 || data_item_win->origin.y != cur_y))
311 tui_delete_win (data_item_win->handle);
312 data_item_win->handle = 0;
315 if (data_item_win->handle == NULL)
317 data_item_win->height = 1;
318 data_item_win->width = item_win_width;
319 data_item_win->origin.x = (item_win_width * j) + 1;
320 data_item_win->origin.y = cur_y;
321 tui_make_window (data_item_win);
322 scrollok (data_item_win->handle, FALSE);
324 touchwin (data_item_win->handle);
326 /* Get the printable representation of the register
328 tui_display_register (data_item_win);
329 i++; /* Next register. */
331 cur_y++; /* Next row. */
336 /* See tui-regs.h. */
339 tui_data_window::display_reg_element_at_line (int start_element_no,
342 if (!regs_content.empty ())
344 int element_no = start_element_no;
346 if (start_element_no != 0 && start_line_no != 0)
348 int last_line_no, first_line_on_last_page;
350 last_line_no = last_regs_line_no ();
351 first_line_on_last_page = last_line_no - (height - 2);
352 if (first_line_on_last_page < 0)
353 first_line_on_last_page = 0;
355 /* If the element_no causes us to scroll past the end of the
356 registers, adjust what element to really start the
358 if (start_line_no > first_line_on_last_page)
359 element_no = first_reg_element_no_inline (first_line_on_last_page);
361 display_registers_from (element_no);
365 /* See tui-regs.h. */
368 tui_data_window::display_registers_from_line (int line_no)
370 check_and_display_highlight_if_needed ();
371 if (!regs_content.empty ())
379 /* Make sure that we don't display off the end of the
381 if (line_no >= last_regs_line_no ())
383 line_no = line_from_reg_element_no (regs_content.size () - 1);
389 element_no = first_reg_element_no_inline (line_no);
390 if (element_no < regs_content.size ())
391 display_reg_element_at_line (element_no, line_no);
398 return (-1); /* Nothing was displayed. */
402 /* Answer the index first element displayed. If none are displayed,
405 tui_data_window::first_data_item_displayed ()
407 for (int i = 0; i < regs_content.size (); i++)
409 struct tui_gen_win_info *data_item_win;
411 data_item_win = regs_content[i].get ();
412 if (data_item_win->is_visible ())
419 /* See tui-regs.h. */
422 tui_data_window::delete_data_content_windows ()
424 for (auto &&win : regs_content)
426 tui_delete_win (win->handle);
433 tui_data_window::erase_data_content (const char *prompt)
436 check_and_display_highlight_if_needed ();
439 int half_width = (width - 2) / 2;
442 if (strlen (prompt) >= half_width)
445 x_pos = half_width - strlen (prompt);
446 mvwaddstr (handle, (height / 2), x_pos, (char *) prompt);
451 /* See tui-regs.h. */
454 tui_data_window::display_all_data ()
456 if (regs_content.empty ())
457 erase_data_content (NO_DATA_STRING);
460 erase_data_content (NULL);
461 delete_data_content_windows ();
462 check_and_display_highlight_if_needed ();
463 display_registers_from (0);
468 /* Function to redisplay the contents of the data window. */
470 tui_data_window::refresh_all ()
472 erase_data_content (NULL);
473 if (!regs_content.empty ())
475 int first_element = first_data_item_displayed ();
477 if (first_element >= 0) /* Re-use existing windows. */
479 int first_line = (-1);
481 if (first_element < regs_content.size ())
482 first_line = line_from_reg_element_no (first_element);
486 erase_data_content (NULL);
487 display_registers_from_line (first_line);
494 /* Scroll the data window vertically forward or backward. */
496 tui_data_window::do_scroll_vertical (int num_to_scroll)
498 int first_element_no;
499 int first_line = (-1);
501 first_element_no = first_data_item_displayed ();
502 if (first_element_no < regs_content.size ())
503 first_line = line_from_reg_element_no (first_element_no);
505 { /* Calculate the first line from the element number which is in
506 the general data content. */
511 first_line += num_to_scroll;
512 erase_data_content (NULL);
513 delete_data_content_windows ();
514 display_registers_from_line (first_line);
518 /* See tui-regs.h. */
521 tui_data_window::rerender ()
523 /* Delete all data item windows. */
524 for (auto &&win : regs_content)
526 tui_delete_win (win->handle);
532 /* See tui-regs.h. */
535 tui_data_window::refresh_window ()
537 tui_gen_win_info::refresh_window ();
538 for (auto &&win : regs_content)
541 win->refresh_window ();
545 /* This function check all displayed registers for changes in values,
546 given a particular frame. If the values have changed, they are
547 updated with the new value and highlighted. */
549 tui_data_window::check_register_values (struct frame_info *frame)
551 if (regs_content.empty () && display_regs)
552 show_registers (current_group);
555 for (auto &&data_item_win_ptr : regs_content)
559 was_hilighted = data_item_win_ptr->highlight;
561 tui_get_register (frame, data_item_win_ptr.get (),
562 data_item_win_ptr->item_no,
563 &data_item_win_ptr->highlight);
565 if (data_item_win_ptr->highlight || was_hilighted)
566 tui_display_register (data_item_win_ptr.get ());
571 /* Display a register in a window. If hilite is TRUE, then the value
572 will be displayed in reverse video. */
574 tui_display_register (struct tui_data_item_window *data)
576 if (data->handle != NULL)
581 /* We ignore the return value, casting it to void in order to avoid
582 a compiler warning. The warning itself was introduced by a patch
583 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
584 to code that causes the compiler to generate an unused-value
586 (void) wstandout (data->handle);
588 wmove (data->handle, 0, 0);
589 for (i = 1; i < data->width; i++)
590 waddch (data->handle, ' ');
591 wmove (data->handle, 0, 0);
593 waddstr (data->handle, data->content.get ());
596 /* We ignore the return value, casting it to void in order to avoid
597 a compiler warning. The warning itself was introduced by a patch
598 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
599 to code that causes the compiler to generate an unused-value
601 (void) wstandend (data->handle);
602 data->refresh_window ();
606 /* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
607 around behaviour. Returns the next register group, or NULL if the
608 register window is not currently being displayed. */
610 static struct reggroup *
611 tui_reg_next (struct reggroup *current_group, struct gdbarch *gdbarch)
613 struct reggroup *group = NULL;
615 if (current_group != NULL)
617 group = reggroup_next (gdbarch, current_group);
619 group = reggroup_next (gdbarch, NULL);
624 /* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
625 around behaviour. Returns the previous register group, or NULL if the
626 register window is not currently being displayed. */
628 static struct reggroup *
629 tui_reg_prev (struct reggroup *current_group, struct gdbarch *gdbarch)
631 struct reggroup *group = NULL;
633 if (current_group != NULL)
635 group = reggroup_prev (gdbarch, current_group);
637 group = reggroup_prev (gdbarch, NULL);
642 /* A helper function to display the register window in the appropriate
648 enum tui_layout_type cur_layout = tui_current_layout ();
649 enum tui_layout_type new_layout;
650 if (cur_layout == SRC_COMMAND || cur_layout == SRC_DATA_COMMAND)
651 new_layout = SRC_DATA_COMMAND;
653 new_layout = DISASSEM_DATA_COMMAND;
654 tui_set_layout (new_layout);
657 /* Implement the 'tui reg' command. Changes the register group displayed
658 in the tui register window. Displays the tui register window if it is
659 not already on display. */
662 tui_reg_command (const char *args, int from_tty)
664 struct gdbarch *gdbarch = get_current_arch ();
668 struct reggroup *group, *match = NULL;
669 size_t len = strlen (args);
671 /* Make sure the curses mode is enabled. */
674 /* Make sure the register window is visible. If not, select an
675 appropriate layout. We need to do this before trying to run the
676 'next' or 'prev' commands. */
677 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->is_visible ())
680 struct reggroup *current_group = TUI_DATA_WIN->current_group;
681 if (strncmp (args, "next", len) == 0)
682 match = tui_reg_next (current_group, gdbarch);
683 else if (strncmp (args, "prev", len) == 0)
684 match = tui_reg_prev (current_group, gdbarch);
686 /* This loop matches on the initial part of a register group
687 name. If this initial part in ARGS matches only one register
688 group then the switch is made. */
689 for (group = reggroup_next (gdbarch, NULL);
691 group = reggroup_next (gdbarch, group))
693 if (strncmp (reggroup_name (group), args, len) == 0)
696 error (_("ambiguous register group name '%s'"), args);
702 error (_("unknown register group '%s'"), args);
704 TUI_DATA_WIN->show_registers (match);
708 struct reggroup *group;
711 printf_unfiltered (_("\"tui reg\" must be followed by the name of "
712 "either a register group,\nor one of 'next' "
713 "or 'prev'. Known register groups are:\n"));
715 for (first = 1, group = reggroup_next (gdbarch, NULL);
717 first = 0, group = reggroup_next (gdbarch, group))
720 printf_unfiltered (", ");
721 printf_unfiltered ("%s", reggroup_name (group));
724 printf_unfiltered ("\n");
728 /* Complete names of register groups, and add the special "prev" and "next"
732 tui_reggroup_completer (struct cmd_list_element *ignore,
733 completion_tracker &tracker,
734 const char *text, const char *word)
736 static const char *extra[] = { "next", "prev", NULL };
737 size_t len = strlen (word);
740 reggroup_completer (ignore, tracker, text, word);
742 /* XXXX use complete_on_enum instead? */
743 for (tmp = extra; *tmp != NULL; ++tmp)
745 if (strncmp (word, *tmp, len) == 0)
746 tracker.add_completion (make_unique_xstrdup (*tmp));
751 _initialize_tui_regs (void)
753 struct cmd_list_element **tuicmd, *cmd;
755 tuicmd = tui_get_cmd_list ();
757 cmd = add_cmd ("reg", class_tui, tui_reg_command, _("\
758 TUI command to control the register window."), tuicmd);
759 set_cmd_completer (cmd, tui_reggroup_completer);