1 /* TUI display registers in window.
3 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 Contributed by Hewlett-Packard Company.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "tui/tui-data.h"
33 #include "gdb_string.h"
34 #include "tui/tui-layout.h"
35 #include "tui/tui-win.h"
36 #include "tui/tui-windata.h"
37 #include "tui/tui-wingeneral.h"
38 #include "tui/tui-file.h"
39 #include "tui/tui-regs.h"
40 #include "reggroups.h"
43 #include "gdb_curses.h"
46 /*****************************************
47 ** STATIC LOCAL FUNCTIONS FORWARD DECLS **
48 ******************************************/
50 tui_display_register (struct tui_data_element *data,
51 struct tui_gen_win_info *win_info);
53 static enum tui_status
54 tui_show_register_group (struct gdbarch *gdbarch,
55 struct reggroup *group,
56 struct frame_info *frame,
57 int refresh_values_only);
59 static enum tui_status
60 tui_get_register (struct gdbarch *gdbarch,
61 struct frame_info *frame,
62 struct tui_data_element *data,
63 int regnum, int *changedp);
64 static void tui_register_format (struct gdbarch *,
66 struct tui_data_element*,
68 static void tui_scroll_regs_forward_command (char *, int);
69 static void tui_scroll_regs_backward_command (char *, int);
73 /*****************************************
74 ** PUBLIC FUNCTIONS **
75 ******************************************/
77 /* Answer the number of the last line in the regs display. If there
78 are no registers (-1) is returned. */
80 tui_last_regs_line_no (void)
84 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
86 num_lines = (TUI_DATA_WIN->detail.data_display_info.regs_content_count /
87 TUI_DATA_WIN->detail.data_display_info.regs_column_count);
88 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count %
89 TUI_DATA_WIN->detail.data_display_info.regs_column_count)
96 /* Answer the line number that the register element at element_no is
97 on. If element_no is greater than the number of register elements
98 there are, -1 is returned. */
100 tui_line_from_reg_element_no (int element_no)
102 if (element_no < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
110 (TUI_DATA_WIN->detail.data_display_info.regs_column_count * i))
123 /* Answer the index of the first element in line_no. If line_no is
124 past the register area (-1) is returned. */
126 tui_first_reg_element_no_inline (int line_no)
128 if ((line_no * TUI_DATA_WIN->detail.data_display_info.regs_column_count)
129 <= TUI_DATA_WIN->detail.data_display_info.regs_content_count)
130 return ((line_no + 1) *
131 TUI_DATA_WIN->detail.data_display_info.regs_column_count) -
132 TUI_DATA_WIN->detail.data_display_info.regs_column_count;
138 /* Answer the index of the last element in line_no. If line_no is
139 past the register area (-1) is returned. */
141 tui_last_reg_element_no_in_line (int line_no)
143 if ((line_no * TUI_DATA_WIN->detail.data_display_info.regs_column_count) <=
144 TUI_DATA_WIN->detail.data_display_info.regs_content_count)
145 return ((line_no + 1) *
146 TUI_DATA_WIN->detail.data_display_info.regs_column_count) - 1;
151 /* Show the registers of the given group in the data window
152 and refresh the window. */
154 tui_show_registers (struct reggroup *group)
156 enum tui_status ret = TUI_FAILURE;
157 struct tui_data_info *display_info;
159 /* Make sure the curses mode is enabled. */
162 /* Make sure the register window is visible. If not, select an
163 appropriate layout. */
164 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
165 tui_set_layout_for_display_command (DATA_NAME);
167 display_info = &TUI_DATA_WIN->detail.data_display_info;
169 group = general_reggroup;
171 /* Say that registers should be displayed, even if there is a
173 display_info->display_regs = TRUE;
175 if (target_has_registers && target_has_stack && target_has_memory)
177 ret = tui_show_register_group (current_gdbarch, group,
178 get_current_frame (),
179 group == display_info->current_group);
181 if (ret == TUI_FAILURE)
183 display_info->current_group = 0;
184 tui_erase_data_content (NO_REGS_STRING);
190 /* Clear all notation of changed values. */
191 for (i = 0; i < display_info->regs_content_count; i++)
193 struct tui_gen_win_info *data_item_win;
194 struct tui_win_element *win;
196 data_item_win = &display_info->regs_content[i]
197 ->which_element.data_window;
198 win = (struct tui_win_element *) data_item_win->content[0];
199 win->which_element.data.highlight = FALSE;
201 display_info->current_group = group;
202 tui_display_all_data ();
207 /* Set the data window to display the registers of the register group
208 using the given frame. Values are refreshed only when
209 refresh_values_only is TRUE. */
211 static enum tui_status
212 tui_show_register_group (struct gdbarch *gdbarch,
213 struct reggroup *group,
214 struct frame_info *frame,
215 int refresh_values_only)
217 enum tui_status ret = TUI_FAILURE;
219 int allocated_here = FALSE;
222 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
224 /* Make a new title showing which group we display. */
225 snprintf (title, sizeof (title) - 1, "Register group: %s",
226 reggroup_name (group));
227 xfree (TUI_DATA_WIN->generic.title);
228 TUI_DATA_WIN->generic.title = xstrdup (title);
230 /* See how many registers must be displayed. */
233 regnum < gdbarch_num_regs (current_gdbarch)
234 + gdbarch_num_pseudo_regs (current_gdbarch);
237 /* Must be in the group and have a name. */
238 if (gdbarch_register_reggroup_p (gdbarch, regnum, group)
239 && gdbarch_register_name (gdbarch, regnum) != 0)
243 if (display_info->regs_content_count > 0 && !refresh_values_only)
245 tui_free_data_content (display_info->regs_content,
246 display_info->regs_content_count);
247 display_info->regs_content_count = 0;
250 if (display_info->regs_content_count <= 0)
252 display_info->regs_content = tui_alloc_content (nr_regs, DATA_WIN);
253 allocated_here = TRUE;
254 refresh_values_only = FALSE;
257 if (display_info->regs_content != (tui_win_content) NULL)
259 if (!refresh_values_only || allocated_here)
261 TUI_DATA_WIN->generic.content = (void*) NULL;
262 TUI_DATA_WIN->generic.content_size = 0;
263 tui_add_content_elements (&TUI_DATA_WIN->generic, nr_regs);
264 display_info->regs_content
265 = (tui_win_content) TUI_DATA_WIN->generic.content;
266 display_info->regs_content_count = nr_regs;
269 /* Now set the register names and values. */
272 regnum < gdbarch_num_regs (current_gdbarch)
273 + gdbarch_num_pseudo_regs (current_gdbarch);
276 struct tui_gen_win_info *data_item_win;
277 struct tui_data_element *data;
280 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
283 name = gdbarch_register_name (gdbarch, regnum);
288 &display_info->regs_content[pos]->which_element.data_window;
290 &((struct tui_win_element *) data_item_win->content[0])->which_element.data;
293 if (!refresh_values_only)
295 data->item_no = regnum;
297 data->highlight = FALSE;
299 if (data->value == (void*) NULL)
300 data->value = (void*) xmalloc (MAX_REGISTER_SIZE);
302 tui_get_register (gdbarch, frame, data, regnum, 0);
307 TUI_DATA_WIN->generic.content_size =
308 display_info->regs_content_count + display_info->data_content_count;
315 /* Function to display the registers in the content from
316 'start_element_no' until the end of the register content or the end
317 of the display height. No checking for displaying past the end of
318 the registers is done here. */
320 tui_display_registers_from (int start_element_no)
322 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
324 if (display_info->regs_content != (tui_win_content) NULL
325 && display_info->regs_content_count > 0)
327 int i = start_element_no;
328 int j, value_chars_wide, item_win_width, cur_y;
331 for (i = 0; i < display_info->regs_content_count; i++)
333 struct tui_data_element *data;
334 struct tui_gen_win_info *data_item_win;
338 data_item_win = &display_info->regs_content[i]->which_element.data_window;
339 data = &((struct tui_win_element *)
340 data_item_win->content[0])->which_element.data;
347 len = 8 * ((len / 8) + 1);
355 item_win_width = max_len + 1;
356 i = start_element_no;
358 display_info->regs_column_count =
359 (TUI_DATA_WIN->generic.width - 2) / item_win_width;
360 if (display_info->regs_column_count == 0)
361 display_info->regs_column_count = 1;
363 (TUI_DATA_WIN->generic.width - 2) / display_info->regs_column_count;
365 /* Now create each data "sub" window, and write the display into
368 while (i < display_info->regs_content_count
369 && cur_y <= TUI_DATA_WIN->generic.viewport_height)
372 j < display_info->regs_column_count
373 && i < display_info->regs_content_count;
376 struct tui_gen_win_info *data_item_win;
377 struct tui_data_element *data_element_ptr;
379 /* Create the window if necessary. */
380 data_item_win = &display_info->regs_content[i]
381 ->which_element.data_window;
382 data_element_ptr = &((struct tui_win_element *)
383 data_item_win->content[0])->which_element.data;
384 if (data_item_win->handle != (WINDOW*) NULL
385 && (data_item_win->height != 1
386 || data_item_win->width != item_win_width
387 || data_item_win->origin.x != (item_win_width * j) + 1
388 || data_item_win->origin.y != cur_y))
390 tui_delete_win (data_item_win->handle);
391 data_item_win->handle = 0;
394 if (data_item_win->handle == (WINDOW *) NULL)
396 data_item_win->height = 1;
397 data_item_win->width = item_win_width;
398 data_item_win->origin.x = (item_win_width * j) + 1;
399 data_item_win->origin.y = cur_y;
400 tui_make_window (data_item_win, DONT_BOX_WINDOW);
401 scrollok (data_item_win->handle, FALSE);
403 touchwin (data_item_win->handle);
405 /* Get the printable representation of the register
407 tui_display_register (data_element_ptr, data_item_win);
408 i++; /* Next register. */
410 cur_y++; /* Next row. */
416 /* Function to display the registers in the content from
417 'start_element_no' on 'start_line_no' until the end of the register
418 content or the end of the display height. This function checks
419 that we won't display off the end of the register display. */
421 tui_display_reg_element_at_line (int start_element_no,
424 if (TUI_DATA_WIN->detail.data_display_info.regs_content != (tui_win_content) NULL
425 && TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
427 int element_no = start_element_no;
429 if (start_element_no != 0 && start_line_no != 0)
431 int last_line_no, first_line_on_last_page;
433 last_line_no = tui_last_regs_line_no ();
434 first_line_on_last_page = last_line_no - (TUI_DATA_WIN->generic.height - 2);
435 if (first_line_on_last_page < 0)
436 first_line_on_last_page = 0;
438 /* If there is no other data displayed except registers, and
439 the element_no causes us to scroll past the end of the
440 registers, adjust what element to really start the
442 if (TUI_DATA_WIN->detail.data_display_info.data_content_count <= 0
443 && start_line_no > first_line_on_last_page)
444 element_no = tui_first_reg_element_no_inline (first_line_on_last_page);
446 tui_display_registers_from (element_no);
452 /* Function to display the registers starting at line line_no in the
453 data window. Answers the line number that the display actually
454 started from. If nothing is displayed (-1) is returned. */
456 tui_display_registers_from_line (int line_no,
459 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
461 int line, element_no;
465 else if (force_display)
466 { /* If we must display regs (force_display is true), then
467 make sure that we don't display off the end of the
469 if (line_no >= tui_last_regs_line_no ())
471 if ((line = tui_line_from_reg_element_no (
472 TUI_DATA_WIN->detail.data_display_info.regs_content_count - 1)) < 0)
481 element_no = tui_first_reg_element_no_inline (line);
482 if (element_no < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
483 tui_display_reg_element_at_line (element_no, line);
490 return (-1); /* Nothing was displayed. */
494 /* This function check all displayed registers for changes in values,
495 given a particular frame. If the values have changed, they are
496 updated with the new value and highlighted. */
498 tui_check_register_values (struct frame_info *frame)
500 if (TUI_DATA_WIN != NULL
501 && TUI_DATA_WIN->generic.is_visible)
503 struct tui_data_info *display_info
504 = &TUI_DATA_WIN->detail.data_display_info;
506 if (display_info->regs_content_count <= 0
507 && display_info->display_regs)
508 tui_show_registers (display_info->current_group);
513 for (i = 0; (i < display_info->regs_content_count); i++)
515 struct tui_data_element *data;
516 struct tui_gen_win_info *data_item_win_ptr;
519 data_item_win_ptr = &display_info->regs_content[i]->
520 which_element.data_window;
521 data = &((struct tui_win_element *)
522 data_item_win_ptr->content[0])->which_element.data;
523 was_hilighted = data->highlight;
525 tui_get_register (current_gdbarch, frame, data,
526 data->item_no, &data->highlight);
528 if (data->highlight || was_hilighted)
530 tui_display_register (data, data_item_win_ptr);
537 /* Display a register in a window. If hilite is TRUE, then the value
538 will be displayed in reverse video. */
540 tui_display_register (struct tui_data_element *data,
541 struct tui_gen_win_info *win_info)
543 if (win_info->handle != (WINDOW *) NULL)
548 wstandout (win_info->handle);
550 wmove (win_info->handle, 0, 0);
551 for (i = 1; i < win_info->width; i++)
552 waddch (win_info->handle, ' ');
553 wmove (win_info->handle, 0, 0);
555 waddstr (win_info->handle, data->content);
558 wstandend (win_info->handle);
559 tui_refresh_win (win_info);
564 tui_reg_next_command (char *arg, int from_tty)
566 if (TUI_DATA_WIN != 0)
568 struct reggroup *group
569 = TUI_DATA_WIN->detail.data_display_info.current_group;
571 group = reggroup_next (current_gdbarch, group);
573 group = reggroup_next (current_gdbarch, 0);
576 tui_show_registers (group);
581 tui_reg_float_command (char *arg, int from_tty)
583 tui_show_registers (float_reggroup);
587 tui_reg_general_command (char *arg, int from_tty)
589 tui_show_registers (general_reggroup);
593 tui_reg_system_command (char *arg, int from_tty)
595 tui_show_registers (system_reggroup);
598 static struct cmd_list_element *tuireglist;
601 tui_reg_command (char *args, int from_tty)
603 printf_unfiltered (_("\"tui reg\" must be followed by the name of a "
604 "tui reg command.\n"));
605 help_list (tuireglist, "tui reg ", -1, gdb_stdout);
608 /* Provide a prototype to silence -Wmissing-prototypes. */
609 extern initialize_file_ftype _initialize_tui_regs;
612 _initialize_tui_regs (void)
614 struct cmd_list_element **tuicmd;
616 tuicmd = tui_get_cmd_list ();
618 add_prefix_cmd ("reg", class_tui, tui_reg_command,
619 _("TUI commands to control the register window."),
620 &tuireglist, "tui reg ", 0,
623 add_cmd ("float", class_tui, tui_reg_float_command,
624 _("Display only floating point registers."),
626 add_cmd ("general", class_tui, tui_reg_general_command,
627 _("Display only general registers."),
629 add_cmd ("system", class_tui, tui_reg_system_command,
630 _("Display only system registers."),
632 add_cmd ("next", class_tui, tui_reg_next_command,
633 _("Display next register group."),
638 add_com ("fr", class_tui, tui_reg_float_command,
639 _("Display only floating point registers\n"));
640 add_com ("gr", class_tui, tui_reg_general_command,
641 _("Display only general registers\n"));
642 add_com ("sr", class_tui, tui_reg_system_command,
643 _("Display only special registers\n"));
644 add_com ("+r", class_tui, tui_scroll_regs_forward_command,
645 _("Scroll the registers window forward\n"));
646 add_com ("-r", class_tui, tui_scroll_regs_backward_command,
647 _("Scroll the register window backward\n"));
652 /*****************************************
653 ** STATIC LOCAL FUNCTIONS **
654 ******************************************/
656 extern int pagination_enabled;
659 tui_restore_gdbout (void *ui)
661 ui_file_delete (gdb_stdout);
662 gdb_stdout = (struct ui_file*) ui;
663 pagination_enabled = 1;
666 /* Get the register from the frame and make a printable representation
667 of it in the data element. */
669 tui_register_format (struct gdbarch *gdbarch,
670 struct frame_info *frame,
671 struct tui_data_element *data_element,
674 struct ui_file *stream;
675 struct ui_file *old_stdout;
677 struct cleanup *cleanups;
680 struct type *type = register_type (gdbarch, regnum);
682 name = gdbarch_register_name (gdbarch, regnum);
688 pagination_enabled = 0;
689 old_stdout = gdb_stdout;
690 stream = tui_sfileopen (256);
692 cleanups = make_cleanup (tui_restore_gdbout, (void*) old_stdout);
693 if (TYPE_VECTOR (type) != 0 && 0)
695 gdb_byte buf[MAX_REGISTER_SIZE];
697 struct value_print_options opts;
699 len = register_size (current_gdbarch, regnum);
700 fprintf_filtered (stream, "%-14s ", name);
701 get_frame_register (frame, regnum, buf);
702 get_formatted_print_options (&opts, 'f');
703 print_scalar_formatted (buf, type, &opts, len, stream);
707 gdbarch_print_registers_info (current_gdbarch, stream,
711 /* Save formatted output in the buffer. */
712 p = tui_file_get_strbuf (stream);
714 /* Remove the possible \n. */
715 s = strrchr (p, '\n');
719 xfree (data_element->content);
720 data_element->content = xstrdup (p);
721 do_cleanups (cleanups);
724 /* Get the register value from the given frame and format it for the
725 display. When changep is set, check if the new register value has
726 changed with respect to the previous call. */
727 static enum tui_status
728 tui_get_register (struct gdbarch *gdbarch,
729 struct frame_info *frame,
730 struct tui_data_element *data,
731 int regnum, int *changedp)
733 enum tui_status ret = TUI_FAILURE;
737 if (target_has_registers)
739 gdb_byte buf[MAX_REGISTER_SIZE];
740 get_frame_register (frame, regnum, buf);
744 int size = register_size (gdbarch, regnum);
745 char *old = (char*) data->value;
748 for (i = 0; i < size; i++)
749 if (buf[i] != old[i])
756 /* Reformat the data content if the value changed. */
757 if (changedp == 0 || *changedp == TRUE)
758 tui_register_format (gdbarch, frame, data, regnum);
766 tui_scroll_regs_forward_command (char *arg, int from_tty)
768 tui_scroll (FORWARD_SCROLL, TUI_DATA_WIN, 1);
773 tui_scroll_regs_backward_command (char *arg, int from_tty)
775 tui_scroll (BACKWARD_SCROLL, TUI_DATA_WIN, 1);