1 /* TUI display registers in window.
3 Copyright (C) 1998-2020 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 /* A subclass of string_file that expands tab characters. */
46 class tab_expansion_file
: public string_file
50 tab_expansion_file () = default;
52 void write (const char *buf
, long length_buf
) override
;
60 tab_expansion_file::write (const char *buf
, long length_buf
)
62 for (long i
= 0; i
< length_buf
; ++i
)
68 string_file::write (" ", 1);
71 while ((m_column
% 8) != 0);
75 string_file::write (&buf
[i
], 1);
84 /* Get the register from the frame and return a printable
85 representation of it. */
88 tui_register_format (struct frame_info
*frame
, int regnum
)
90 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
92 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
93 tab_expansion_file stream
;
95 scoped_restore save_pagination
96 = make_scoped_restore (&pagination_enabled
, 0);
97 scoped_restore save_stdout
98 = make_scoped_restore (&gdb_stdout
, &stream
);
100 gdbarch_print_registers_info (gdbarch
, &stream
, frame
, regnum
, 1);
102 /* Remove the possible \n. */
103 std::string
&str
= stream
.string ();
104 if (!str
.empty () && str
.back () == '\n')
105 str
.resize (str
.size () - 1);
110 /* Get the register value from the given frame and format it for the
111 display. When changep is set, check if the new register value has
112 changed with respect to the previous call. */
114 tui_get_register (struct frame_info
*frame
,
115 struct tui_data_item_window
*data
,
116 int regnum
, bool *changedp
)
120 if (target_has_registers
)
122 std::string new_content
= tui_register_format (frame
, regnum
);
124 if (changedp
!= NULL
&& data
->content
!= new_content
)
127 data
->content
= std::move (new_content
);
131 /* See tui-regs.h. */
134 tui_data_window::last_regs_line_no () const
136 int num_lines
= m_regs_content
.size () / m_regs_column_count
;
137 if (m_regs_content
.size () % m_regs_column_count
)
142 /* See tui-regs.h. */
145 tui_data_window::line_from_reg_element_no (int element_no
) const
147 if (element_no
< m_regs_content
.size ())
154 if (element_no
< m_regs_column_count
* i
)
166 /* See tui-regs.h. */
169 tui_data_window::first_reg_element_no_inline (int line_no
) const
171 if (line_no
* m_regs_column_count
<= m_regs_content
.size ())
172 return ((line_no
+ 1) * m_regs_column_count
) - m_regs_column_count
;
177 /* Show the registers of the given group in the data window
178 and refresh the window. */
180 tui_data_window::show_registers (struct reggroup
*group
)
183 group
= general_reggroup
;
185 if (target_has_registers
&& target_has_stack
&& target_has_memory
)
187 show_register_group (group
, get_selected_frame (NULL
),
188 group
== m_current_group
);
190 /* Clear all notation of changed values. */
191 for (auto &&data_item_win
: m_regs_content
)
192 data_item_win
.highlight
= false;
193 m_current_group
= group
;
198 m_regs_content
.clear ();
205 /* Set the data window to display the registers of the register group
206 using the given frame. Values are refreshed only when
207 refresh_values_only is true. */
210 tui_data_window::show_register_group (struct reggroup
*group
,
211 struct frame_info
*frame
,
212 bool refresh_values_only
)
214 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
218 /* Make a new title showing which group we display. */
219 title
= string_printf ("Register group: %s", reggroup_name (group
));
221 /* See how many registers must be displayed. */
223 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
227 /* Must be in the group. */
228 if (!gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
231 /* If the register name is empty, it is undefined for this
232 processor, so don't display anything. */
233 name
= gdbarch_register_name (gdbarch
, regnum
);
234 if (name
== 0 || *name
== '\0')
240 m_regs_content
.resize (nr_regs
);
242 /* Now set the register names and values. */
244 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
246 struct tui_data_item_window
*data_item_win
;
249 /* Must be in the group. */
250 if (!gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
253 /* If the register name is empty, it is undefined for this
254 processor, so don't display anything. */
255 name
= gdbarch_register_name (gdbarch
, regnum
);
256 if (name
== 0 || *name
== '\0')
259 data_item_win
= &m_regs_content
[pos
];
260 if (!refresh_values_only
)
262 data_item_win
->item_no
= regnum
;
263 data_item_win
->highlight
= false;
265 tui_get_register (frame
, data_item_win
, regnum
, 0);
270 /* See tui-regs.h. */
273 tui_data_window::display_registers_from (int start_element_no
)
275 int j
, item_win_width
, cur_y
;
278 for (auto &&data_item_win
: m_regs_content
)
280 int len
= data_item_win
.content
.size ();
285 item_win_width
= max_len
+ 1;
286 int i
= start_element_no
;
288 m_regs_column_count
= (width
- 2) / item_win_width
;
289 if (m_regs_column_count
== 0)
290 m_regs_column_count
= 1;
291 item_win_width
= (width
- 2) / m_regs_column_count
;
293 /* Now create each data "sub" window, and write the display into
296 while (i
< m_regs_content
.size () && cur_y
<= height
- 2)
299 j
< m_regs_column_count
&& i
< m_regs_content
.size ();
302 /* Create the window if necessary. */
303 m_regs_content
[i
].resize (1, item_win_width
,
304 x
+ (item_win_width
* j
) + 1, y
+ cur_y
);
305 i
++; /* Next register. */
307 cur_y
++; /* Next row. */
311 /* See tui-regs.h. */
314 tui_data_window::display_reg_element_at_line (int start_element_no
,
317 int element_no
= start_element_no
;
319 if (start_element_no
!= 0 && start_line_no
!= 0)
321 int last_line_no
, first_line_on_last_page
;
323 last_line_no
= last_regs_line_no ();
324 first_line_on_last_page
= last_line_no
- (height
- 2);
325 if (first_line_on_last_page
< 0)
326 first_line_on_last_page
= 0;
328 /* If the element_no causes us to scroll past the end of the
329 registers, adjust what element to really start the
331 if (start_line_no
> first_line_on_last_page
)
332 element_no
= first_reg_element_no_inline (first_line_on_last_page
);
334 display_registers_from (element_no
);
337 /* See tui-regs.h. */
340 tui_data_window::display_registers_from_line (int line_no
)
348 /* Make sure that we don't display off the end of the
350 if (line_no
>= last_regs_line_no ())
352 line_no
= line_from_reg_element_no (m_regs_content
.size () - 1);
358 element_no
= first_reg_element_no_inline (line_no
);
359 if (element_no
< m_regs_content
.size ())
360 display_reg_element_at_line (element_no
, line_no
);
368 /* Answer the index first element displayed. If none are displayed,
371 tui_data_window::first_data_item_displayed ()
373 for (int i
= 0; i
< m_regs_content
.size (); i
++)
375 struct tui_gen_win_info
*data_item_win
;
377 data_item_win
= &m_regs_content
[i
];
378 if (data_item_win
->is_visible ())
385 /* See tui-regs.h. */
388 tui_data_window::delete_data_content_windows ()
390 for (auto &&win
: m_regs_content
)
391 win
.handle
.reset (nullptr);
396 tui_data_window::erase_data_content (const char *prompt
)
398 werase (handle
.get ());
399 check_and_display_highlight_if_needed ();
402 int half_width
= (width
- 2) / 2;
405 if (strlen (prompt
) >= half_width
)
408 x_pos
= half_width
- strlen (prompt
);
409 mvwaddstr (handle
.get (), (height
/ 2), x_pos
, (char *) prompt
);
411 tui_wrefresh (handle
.get ());
414 /* See tui-regs.h. */
417 tui_data_window::rerender ()
419 if (m_regs_content
.empty ())
420 erase_data_content (_("[ Register Values Unavailable ]"));
423 erase_data_content (NULL
);
424 delete_data_content_windows ();
425 display_registers_from (0);
430 /* Scroll the data window vertically forward or backward. */
432 tui_data_window::do_scroll_vertical (int num_to_scroll
)
434 int first_element_no
;
435 int first_line
= (-1);
437 first_element_no
= first_data_item_displayed ();
438 if (first_element_no
< m_regs_content
.size ())
439 first_line
= line_from_reg_element_no (first_element_no
);
441 { /* Calculate the first line from the element number which is in
442 the general data content. */
447 first_line
+= num_to_scroll
;
448 erase_data_content (NULL
);
449 delete_data_content_windows ();
450 display_registers_from_line (first_line
);
454 /* See tui-regs.h. */
457 tui_data_window::refresh_window ()
459 tui_gen_win_info::refresh_window ();
460 for (auto &&win
: m_regs_content
)
461 win
.refresh_window ();
465 tui_data_window::no_refresh ()
467 tui_gen_win_info::no_refresh ();
468 for (auto &&win
: m_regs_content
)
472 /* This function check all displayed registers for changes in values,
473 given a particular frame. If the values have changed, they are
474 updated with the new value and highlighted. */
476 tui_data_window::check_register_values (struct frame_info
*frame
)
478 if (m_regs_content
.empty ())
479 show_registers (m_current_group
);
482 for (auto &&data_item_win
: m_regs_content
)
486 was_hilighted
= data_item_win
.highlight
;
488 tui_get_register (frame
, &data_item_win
,
489 data_item_win
.item_no
,
490 &data_item_win
.highlight
);
492 if (data_item_win
.highlight
|| was_hilighted
)
493 data_item_win
.rerender ();
498 /* Display a register in a window. If hilite is TRUE, then the value
499 will be displayed in reverse video. */
501 tui_data_item_window::rerender ()
505 scrollok (handle
.get (), FALSE
);
507 /* We ignore the return value, casting it to void in order to avoid
508 a compiler warning. The warning itself was introduced by a patch
509 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
510 to code that causes the compiler to generate an unused-value
512 (void) wstandout (handle
.get ());
514 wmove (handle
.get (), 0, 0);
515 for (i
= 1; i
< width
; i
++)
516 waddch (handle
.get (), ' ');
517 wmove (handle
.get (), 0, 0);
518 waddstr (handle
.get (), content
.c_str ());
521 /* We ignore the return value, casting it to void in order to avoid
522 a compiler warning. The warning itself was introduced by a patch
523 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
524 to code that causes the compiler to generate an unused-value
526 (void) wstandend (handle
.get ());
531 tui_data_item_window::refresh_window ()
533 if (handle
!= nullptr)
535 /* This seems to be needed because the data items are nested
536 windows, which according to the ncurses man pages aren't well
538 touchwin (handle
.get ());
539 tui_wrefresh (handle
.get ());
543 /* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
544 around behaviour. Returns the next register group, or NULL if the
545 register window is not currently being displayed. */
547 static struct reggroup
*
548 tui_reg_next (struct reggroup
*current_group
, struct gdbarch
*gdbarch
)
550 struct reggroup
*group
= NULL
;
552 if (current_group
!= NULL
)
554 group
= reggroup_next (gdbarch
, current_group
);
556 group
= reggroup_next (gdbarch
, NULL
);
561 /* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
562 around behaviour. Returns the previous register group, or NULL if the
563 register window is not currently being displayed. */
565 static struct reggroup
*
566 tui_reg_prev (struct reggroup
*current_group
, struct gdbarch
*gdbarch
)
568 struct reggroup
*group
= NULL
;
570 if (current_group
!= NULL
)
572 group
= reggroup_prev (gdbarch
, current_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 tui_suppress_output suppress
;
598 /* Make sure the register window is visible. If not, select an
599 appropriate layout. We need to do this before trying to run the
600 'next' or 'prev' commands. */
601 if (TUI_DATA_WIN
== NULL
|| !TUI_DATA_WIN
->is_visible ())
604 struct reggroup
*current_group
= TUI_DATA_WIN
->get_current_group ();
605 if (strncmp (args
, "next", len
) == 0)
606 match
= tui_reg_next (current_group
, gdbarch
);
607 else if (strncmp (args
, "prev", len
) == 0)
608 match
= tui_reg_prev (current_group
, gdbarch
);
610 /* This loop matches on the initial part of a register group
611 name. If this initial part in ARGS matches only one register
612 group then the switch is made. */
613 for (group
= reggroup_next (gdbarch
, NULL
);
615 group
= reggroup_next (gdbarch
, group
))
617 if (strncmp (reggroup_name (group
), args
, len
) == 0)
620 error (_("ambiguous register group name '%s'"), args
);
626 error (_("unknown register group '%s'"), args
);
628 TUI_DATA_WIN
->show_registers (match
);
632 struct reggroup
*group
;
635 printf_unfiltered (_("\"tui reg\" must be followed by the name of "
636 "either a register group,\nor one of 'next' "
637 "or 'prev'. Known register groups are:\n"));
639 for (first
= 1, group
= reggroup_next (gdbarch
, NULL
);
641 first
= 0, group
= reggroup_next (gdbarch
, group
))
644 printf_unfiltered (", ");
645 printf_unfiltered ("%s", reggroup_name (group
));
648 printf_unfiltered ("\n");
652 /* Complete names of register groups, and add the special "prev" and "next"
656 tui_reggroup_completer (struct cmd_list_element
*ignore
,
657 completion_tracker
&tracker
,
658 const char *text
, const char *word
)
660 static const char * const extra
[] = { "next", "prev", NULL
};
662 reggroup_completer (ignore
, tracker
, text
, word
);
664 complete_on_enum (tracker
, extra
, text
, word
);
667 void _initialize_tui_regs ();
669 _initialize_tui_regs ()
671 struct cmd_list_element
**tuicmd
, *cmd
;
673 tuicmd
= tui_get_cmd_list ();
675 cmd
= add_cmd ("reg", class_tui
, tui_reg_command
, _("\
676 TUI command to control the register window.\n\
677 Usage: tui reg NAME\n\
678 NAME is the name of the register group to display"), tuicmd
);
679 set_cmd_completer (cmd
, tui_reggroup_completer
);