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 /* Get the register from the frame and return a printable
46 representation of it. */
48 static gdb::unique_xmalloc_ptr
<char>
49 tui_register_format (struct frame_info
*frame
, int regnum
)
51 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
55 scoped_restore save_pagination
56 = make_scoped_restore (&pagination_enabled
, 0);
57 scoped_restore save_stdout
58 = make_scoped_restore (&gdb_stdout
, &stream
);
60 gdbarch_print_registers_info (gdbarch
, &stream
, frame
, regnum
, 1);
62 /* Remove the possible \n. */
63 std::string
&str
= stream
.string ();
64 if (!str
.empty () && str
.back () == '\n')
65 str
.resize (str
.size () - 1);
67 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
68 return tui_expand_tabs (str
.c_str ());
71 /* Get the register value from the given frame and format it for the
72 display. When changep is set, check if the new register value has
73 changed with respect to the previous call. */
75 tui_get_register (struct frame_info
*frame
,
76 struct tui_data_item_window
*data
,
77 int regnum
, bool *changedp
)
81 if (target_has_registers
)
83 gdb::unique_xmalloc_ptr
<char> new_content
84 = tui_register_format (frame
, regnum
);
87 && strcmp (data
->content
.get (), new_content
.get ()) != 0)
90 data
->content
= std::move (new_content
);
97 tui_data_window::last_regs_line_no () const
99 int num_lines
= m_regs_content
.size () / m_regs_column_count
;
100 if (m_regs_content
.size () % m_regs_column_count
)
105 /* See tui-regs.h. */
108 tui_data_window::line_from_reg_element_no (int element_no
) const
110 if (element_no
< m_regs_content
.size ())
117 if (element_no
< m_regs_column_count
* i
)
129 /* See tui-regs.h. */
132 tui_data_window::first_reg_element_no_inline (int line_no
) const
134 if (line_no
* m_regs_column_count
<= m_regs_content
.size ())
135 return ((line_no
+ 1) * m_regs_column_count
) - m_regs_column_count
;
140 /* Show the registers of the given group in the data window
141 and refresh the window. */
143 tui_data_window::show_registers (struct reggroup
*group
)
146 group
= general_reggroup
;
148 if (target_has_registers
&& target_has_stack
&& target_has_memory
)
150 show_register_group (group
, get_selected_frame (NULL
),
151 group
== m_current_group
);
153 /* Clear all notation of changed values. */
154 for (auto &&data_item_win
: m_regs_content
)
155 data_item_win
.highlight
= false;
156 m_current_group
= group
;
161 m_regs_content
.clear ();
168 /* Set the data window to display the registers of the register group
169 using the given frame. Values are refreshed only when
170 refresh_values_only is true. */
173 tui_data_window::show_register_group (struct reggroup
*group
,
174 struct frame_info
*frame
,
175 bool refresh_values_only
)
177 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
181 /* Make a new title showing which group we display. */
182 title
= string_printf ("Register group: %s", reggroup_name (group
));
184 /* See how many registers must be displayed. */
186 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
190 /* Must be in the group. */
191 if (!gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
194 /* If the register name is empty, it is undefined for this
195 processor, so don't display anything. */
196 name
= gdbarch_register_name (gdbarch
, regnum
);
197 if (name
== 0 || *name
== '\0')
203 m_regs_content
.resize (nr_regs
);
205 /* Now set the register names and values. */
207 for (regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
209 struct tui_data_item_window
*data_item_win
;
212 /* Must be in the group. */
213 if (!gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
216 /* If the register name is empty, it is undefined for this
217 processor, so don't display anything. */
218 name
= gdbarch_register_name (gdbarch
, regnum
);
219 if (name
== 0 || *name
== '\0')
222 data_item_win
= &m_regs_content
[pos
];
225 if (!refresh_values_only
)
227 data_item_win
->item_no
= regnum
;
228 data_item_win
->name
= name
;
229 data_item_win
->highlight
= false;
231 tui_get_register (frame
, data_item_win
, regnum
, 0);
237 /* See tui-regs.h. */
240 tui_data_window::display_registers_from (int start_element_no
)
242 int j
, item_win_width
, cur_y
;
245 for (auto &&data_item_win
: m_regs_content
)
251 p
= data_item_win
.content
.get ();
258 item_win_width
= max_len
+ 1;
259 int i
= start_element_no
;
261 m_regs_column_count
= (width
- 2) / item_win_width
;
262 if (m_regs_column_count
== 0)
263 m_regs_column_count
= 1;
264 item_win_width
= (width
- 2) / m_regs_column_count
;
266 /* Now create each data "sub" window, and write the display into
269 while (i
< m_regs_content
.size () && cur_y
<= height
- 2)
272 j
< m_regs_column_count
&& i
< m_regs_content
.size ();
275 /* Create the window if necessary. */
276 m_regs_content
[i
].resize (1, item_win_width
,
277 (item_win_width
* j
) + 1, cur_y
);
278 i
++; /* Next register. */
280 cur_y
++; /* Next row. */
284 /* See tui-regs.h. */
287 tui_data_window::display_reg_element_at_line (int start_element_no
,
290 int element_no
= start_element_no
;
292 if (start_element_no
!= 0 && start_line_no
!= 0)
294 int last_line_no
, first_line_on_last_page
;
296 last_line_no
= last_regs_line_no ();
297 first_line_on_last_page
= last_line_no
- (height
- 2);
298 if (first_line_on_last_page
< 0)
299 first_line_on_last_page
= 0;
301 /* If the element_no causes us to scroll past the end of the
302 registers, adjust what element to really start the
304 if (start_line_no
> first_line_on_last_page
)
305 element_no
= first_reg_element_no_inline (first_line_on_last_page
);
307 display_registers_from (element_no
);
310 /* See tui-regs.h. */
313 tui_data_window::display_registers_from_line (int line_no
)
321 /* Make sure that we don't display off the end of the
323 if (line_no
>= last_regs_line_no ())
325 line_no
= line_from_reg_element_no (m_regs_content
.size () - 1);
331 element_no
= first_reg_element_no_inline (line_no
);
332 if (element_no
< m_regs_content
.size ())
333 display_reg_element_at_line (element_no
, line_no
);
341 /* Answer the index first element displayed. If none are displayed,
344 tui_data_window::first_data_item_displayed ()
346 for (int i
= 0; i
< m_regs_content
.size (); i
++)
348 struct tui_gen_win_info
*data_item_win
;
350 data_item_win
= &m_regs_content
[i
];
351 if (data_item_win
->is_visible ())
358 /* See tui-regs.h. */
361 tui_data_window::delete_data_content_windows ()
363 for (auto &&win
: m_regs_content
)
364 win
.handle
.reset (nullptr);
369 tui_data_window::erase_data_content (const char *prompt
)
371 werase (handle
.get ());
372 check_and_display_highlight_if_needed ();
375 int half_width
= (width
- 2) / 2;
378 if (strlen (prompt
) >= half_width
)
381 x_pos
= half_width
- strlen (prompt
);
382 mvwaddstr (handle
.get (), (height
/ 2), x_pos
, (char *) prompt
);
384 wrefresh (handle
.get ());
387 /* See tui-regs.h. */
390 tui_data_window::rerender ()
392 if (m_regs_content
.empty ())
393 erase_data_content (_("[ Register Values Unavailable ]"));
396 erase_data_content (NULL
);
397 delete_data_content_windows ();
398 display_registers_from (0);
403 /* Scroll the data window vertically forward or backward. */
405 tui_data_window::do_scroll_vertical (int num_to_scroll
)
407 int first_element_no
;
408 int first_line
= (-1);
410 first_element_no
= first_data_item_displayed ();
411 if (first_element_no
< m_regs_content
.size ())
412 first_line
= line_from_reg_element_no (first_element_no
);
414 { /* Calculate the first line from the element number which is in
415 the general data content. */
420 first_line
+= num_to_scroll
;
421 erase_data_content (NULL
);
422 delete_data_content_windows ();
423 display_registers_from_line (first_line
);
427 /* See tui-regs.h. */
430 tui_data_window::refresh_window ()
432 tui_gen_win_info::refresh_window ();
433 for (auto &&win
: m_regs_content
)
434 win
.refresh_window ();
437 /* This function check all displayed registers for changes in values,
438 given a particular frame. If the values have changed, they are
439 updated with the new value and highlighted. */
441 tui_data_window::check_register_values (struct frame_info
*frame
)
443 if (m_regs_content
.empty ())
444 show_registers (m_current_group
);
447 for (auto &&data_item_win
: m_regs_content
)
451 was_hilighted
= data_item_win
.highlight
;
453 tui_get_register (frame
, &data_item_win
,
454 data_item_win
.item_no
,
455 &data_item_win
.highlight
);
457 if (data_item_win
.highlight
|| was_hilighted
)
458 data_item_win
.rerender ();
463 /* Display a register in a window. If hilite is TRUE, then the value
464 will be displayed in reverse video. */
466 tui_data_item_window::rerender ()
470 scrollok (handle
.get (), FALSE
);
472 /* We ignore the return value, casting it to void in order to avoid
473 a compiler warning. The warning itself was introduced by a patch
474 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
475 to code that causes the compiler to generate an unused-value
477 (void) wstandout (handle
.get ());
479 wmove (handle
.get (), 0, 0);
480 for (i
= 1; i
< width
; i
++)
481 waddch (handle
.get (), ' ');
482 wmove (handle
.get (), 0, 0);
484 waddstr (handle
.get (), content
.get ());
487 /* We ignore the return value, casting it to void in order to avoid
488 a compiler warning. The warning itself was introduced by a patch
489 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
490 to code that causes the compiler to generate an unused-value
492 (void) wstandend (handle
.get ());
497 tui_data_item_window::refresh_window ()
499 if (handle
!= nullptr)
501 /* This seems to be needed because the data items are nested
502 windows, which according to the ncurses man pages aren't well
504 touchwin (handle
.get ());
505 wrefresh (handle
.get ());
509 /* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
510 around behaviour. Returns the next register group, or NULL if the
511 register window is not currently being displayed. */
513 static struct reggroup
*
514 tui_reg_next (struct reggroup
*current_group
, struct gdbarch
*gdbarch
)
516 struct reggroup
*group
= NULL
;
518 if (current_group
!= NULL
)
520 group
= reggroup_next (gdbarch
, current_group
);
522 group
= reggroup_next (gdbarch
, NULL
);
527 /* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
528 around behaviour. Returns the previous register group, or NULL if the
529 register window is not currently being displayed. */
531 static struct reggroup
*
532 tui_reg_prev (struct reggroup
*current_group
, struct gdbarch
*gdbarch
)
534 struct reggroup
*group
= NULL
;
536 if (current_group
!= NULL
)
538 group
= reggroup_prev (gdbarch
, current_group
);
540 group
= reggroup_prev (gdbarch
, NULL
);
545 /* A helper function to display the register window in the appropriate
551 enum tui_layout_type cur_layout
= tui_current_layout ();
552 enum tui_layout_type new_layout
;
553 if (cur_layout
== SRC_COMMAND
|| cur_layout
== SRC_DATA_COMMAND
)
554 new_layout
= SRC_DATA_COMMAND
;
556 new_layout
= DISASSEM_DATA_COMMAND
;
557 tui_set_layout (new_layout
);
560 /* Implement the 'tui reg' command. Changes the register group displayed
561 in the tui register window. Displays the tui register window if it is
562 not already on display. */
565 tui_reg_command (const char *args
, int from_tty
)
567 struct gdbarch
*gdbarch
= get_current_arch ();
571 struct reggroup
*group
, *match
= NULL
;
572 size_t len
= strlen (args
);
574 /* Make sure the curses mode is enabled. */
577 /* Make sure the register window is visible. If not, select an
578 appropriate layout. We need to do this before trying to run the
579 'next' or 'prev' commands. */
580 if (TUI_DATA_WIN
== NULL
|| !TUI_DATA_WIN
->is_visible ())
583 struct reggroup
*current_group
= TUI_DATA_WIN
->get_current_group ();
584 if (strncmp (args
, "next", len
) == 0)
585 match
= tui_reg_next (current_group
, gdbarch
);
586 else if (strncmp (args
, "prev", len
) == 0)
587 match
= tui_reg_prev (current_group
, gdbarch
);
589 /* This loop matches on the initial part of a register group
590 name. If this initial part in ARGS matches only one register
591 group then the switch is made. */
592 for (group
= reggroup_next (gdbarch
, NULL
);
594 group
= reggroup_next (gdbarch
, group
))
596 if (strncmp (reggroup_name (group
), args
, len
) == 0)
599 error (_("ambiguous register group name '%s'"), args
);
605 error (_("unknown register group '%s'"), args
);
607 TUI_DATA_WIN
->show_registers (match
);
611 struct reggroup
*group
;
614 printf_unfiltered (_("\"tui reg\" must be followed by the name of "
615 "either a register group,\nor one of 'next' "
616 "or 'prev'. Known register groups are:\n"));
618 for (first
= 1, group
= reggroup_next (gdbarch
, NULL
);
620 first
= 0, group
= reggroup_next (gdbarch
, group
))
623 printf_unfiltered (", ");
624 printf_unfiltered ("%s", reggroup_name (group
));
627 printf_unfiltered ("\n");
631 /* Complete names of register groups, and add the special "prev" and "next"
635 tui_reggroup_completer (struct cmd_list_element
*ignore
,
636 completion_tracker
&tracker
,
637 const char *text
, const char *word
)
639 static const char *extra
[] = { "next", "prev", NULL
};
640 size_t len
= strlen (word
);
643 reggroup_completer (ignore
, tracker
, text
, word
);
645 /* XXXX use complete_on_enum instead? */
646 for (tmp
= extra
; *tmp
!= NULL
; ++tmp
)
648 if (strncmp (word
, *tmp
, len
) == 0)
649 tracker
.add_completion (make_unique_xstrdup (*tmp
));
654 _initialize_tui_regs (void)
656 struct cmd_list_element
**tuicmd
, *cmd
;
658 tuicmd
= tui_get_cmd_list ();
660 cmd
= add_cmd ("reg", class_tui
, tui_reg_command
, _("\
661 TUI command to control the register window."), tuicmd
);
662 set_cmd_completer (cmd
, tui_reggroup_completer
);