]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/tui/tui-regs.c
Allocate buffer with proper size in amd64_pseudo_register_{read_value,write}
[thirdparty/binutils-gdb.git] / gdb / tui / tui-regs.c
CommitLineData
f377b406 1/* TUI display registers in window.
f33c6cbf 2
e2882c85 3 Copyright (C) 1998-2018 Free Software Foundation, Inc.
f33c6cbf 4
f377b406 5 Contributed by Hewlett-Packard Company.
c906108c 6
f377b406 7 This file is part of GDB.
c906108c 8
f377b406
SC
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
f377b406
SC
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
21
22#include "defs.h"
e17c207e 23#include "arch-utils.h"
d7b2e967
AC
24#include "tui/tui.h"
25#include "tui/tui-data.h"
c906108c
SS
26#include "symtab.h"
27#include "gdbtypes.h"
28#include "gdbcmd.h"
29#include "frame.h"
bc77de56 30#include "regcache.h"
c906108c
SS
31#include "inferior.h"
32#include "target.h"
d7b2e967
AC
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"
2c0b251b 38#include "tui/tui-regs.h"
312809f8 39#include "tui/tui-io.h"
10f59415 40#include "reggroups.h"
79a45b7d 41#include "valprint.h"
51f0e40d 42#include "completer.h"
c906108c 43
6a83354a 44#include "gdb_curses.h"
96ec9981 45
c906108c
SS
46
47/*****************************************
10f59415 48** STATIC LOCAL FUNCTIONS FORWARD DECLS **
c906108c 49******************************************/
10f59415
SC
50static void
51tui_display_register (struct tui_data_element *data,
52 struct tui_gen_win_info *win_info);
c906108c 53
5eccfcc2
UW
54static enum tui_status tui_show_register_group (struct reggroup *group,
55 struct frame_info *frame,
56 int refresh_values_only);
57
58static enum tui_status tui_get_register (struct frame_info *frame,
59 struct tui_data_element *data,
60 int regnum, int *changedp);
61
c906108c
SS
62
63
64/*****************************************
65** PUBLIC FUNCTIONS **
66******************************************/
67
55fb0713
AC
68/* Answer the number of the last line in the regs display. If there
69 are no registers (-1) is returned. */
c906108c 70int
55fb0713 71tui_last_regs_line_no (void)
c906108c 72{
d02c80cd 73 int num_lines = (-1);
c906108c 74
6d012f14 75 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
c906108c 76 {
6ba8e26f 77 num_lines = (TUI_DATA_WIN->detail.data_display_info.regs_content_count /
6d012f14
AC
78 TUI_DATA_WIN->detail.data_display_info.regs_column_count);
79 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count %
80 TUI_DATA_WIN->detail.data_display_info.regs_column_count)
6ba8e26f 81 num_lines++;
c906108c 82 }
6ba8e26f 83 return num_lines;
55fb0713 84}
c906108c
SS
85
86
6ba8e26f
AC
87/* Answer the line number that the register element at element_no is
88 on. If element_no is greater than the number of register elements
55fb0713 89 there are, -1 is returned. */
c906108c 90int
6ba8e26f 91tui_line_from_reg_element_no (int element_no)
c906108c 92{
6ba8e26f 93 if (element_no < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
c906108c
SS
94 {
95 int i, line = (-1);
96
97 i = 1;
98 while (line == (-1))
99 {
6ba8e26f 100 if (element_no <
6d012f14 101 (TUI_DATA_WIN->detail.data_display_info.regs_column_count * i))
c906108c
SS
102 line = i - 1;
103 else
104 i++;
105 }
106
107 return line;
108 }
109 else
110 return (-1);
55fb0713 111}
c906108c
SS
112
113
1cc6d956
MS
114/* Answer the index of the first element in line_no. If line_no is
115 past the register area (-1) is returned. */
c906108c 116int
6ba8e26f 117tui_first_reg_element_no_inline (int line_no)
c906108c 118{
6ba8e26f 119 if ((line_no * TUI_DATA_WIN->detail.data_display_info.regs_column_count)
6d012f14 120 <= TUI_DATA_WIN->detail.data_display_info.regs_content_count)
6ba8e26f 121 return ((line_no + 1) *
6d012f14
AC
122 TUI_DATA_WIN->detail.data_display_info.regs_column_count) -
123 TUI_DATA_WIN->detail.data_display_info.regs_column_count;
c906108c
SS
124 else
125 return (-1);
55fb0713 126}
c906108c
SS
127
128
10f59415
SC
129/* Show the registers of the given group in the data window
130 and refresh the window. */
c906108c 131void
10f59415 132tui_show_registers (struct reggroup *group)
c906108c 133{
22940a24 134 enum tui_status ret = TUI_FAILURE;
0bfbda3b 135 struct tui_data_info *display_info;
c906108c 136
0bfbda3b
SC
137 /* Make sure the curses mode is enabled. */
138 tui_enable ();
139
140 /* Make sure the register window is visible. If not, select an
141 appropriate layout. */
142 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
7bd0be3a 143 tui_set_layout_by_name (DATA_NAME);
0bfbda3b
SC
144
145 display_info = &TUI_DATA_WIN->detail.data_display_info;
10f59415
SC
146 if (group == 0)
147 group = general_reggroup;
c906108c 148
1cc6d956
MS
149 /* Say that registers should be displayed, even if there is a
150 problem. */
10f59415
SC
151 display_info->display_regs = TRUE;
152
153 if (target_has_registers && target_has_stack && target_has_memory)
c906108c 154 {
8eb6bda2 155 ret = tui_show_register_group (group, get_selected_frame (NULL),
10f59415 156 group == display_info->current_group);
c906108c
SS
157 }
158 if (ret == TUI_FAILURE)
159 {
10f59415 160 display_info->current_group = 0;
edae1ccf 161 tui_erase_data_content (NO_REGS_STRING);
c906108c
SS
162 }
163 else
164 {
165 int i;
166
1cc6d956 167 /* Clear all notation of changed values. */
10f59415 168 for (i = 0; i < display_info->regs_content_count; i++)
c906108c 169 {
10f59415
SC
170 struct tui_gen_win_info *data_item_win;
171 struct tui_win_element *win;
c906108c 172
10f59415
SC
173 data_item_win = &display_info->regs_content[i]
174 ->which_element.data_window;
63ed8182 175 win = data_item_win->content[0];
10f59415 176 win->which_element.data.highlight = FALSE;
c906108c 177 }
10f59415 178 display_info->current_group = group;
edae1ccf 179 tui_display_all_data ();
c906108c 180 }
55fb0713 181}
c906108c
SS
182
183
10f59415 184/* Set the data window to display the registers of the register group
1cc6d956
MS
185 using the given frame. Values are refreshed only when
186 refresh_values_only is TRUE. */
10f59415
SC
187
188static enum tui_status
5eccfcc2 189tui_show_register_group (struct reggroup *group,
08ef48c5
MS
190 struct frame_info *frame,
191 int refresh_values_only)
10f59415 192{
5eccfcc2 193 struct gdbarch *gdbarch = get_frame_arch (frame);
10f59415
SC
194 enum tui_status ret = TUI_FAILURE;
195 int nr_regs;
196 int allocated_here = FALSE;
197 int regnum, pos;
198 char title[80];
199 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
200
201 /* Make a new title showing which group we display. */
202 snprintf (title, sizeof (title) - 1, "Register group: %s",
203 reggroup_name (group));
204 xfree (TUI_DATA_WIN->generic.title);
205 TUI_DATA_WIN->generic.title = xstrdup (title);
206
207 /* See how many registers must be displayed. */
208 nr_regs = 0;
f57d151a 209 for (regnum = 0;
5eccfcc2
UW
210 regnum < gdbarch_num_regs (gdbarch)
211 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 212 regnum++)
10f59415 213 {
d20c1c3f
PA
214 const char *name;
215
216 /* Must be in the group. */
217 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
218 continue;
219
220 /* If the register name is empty, it is undefined for this
221 processor, so don't display anything. */
222 name = gdbarch_register_name (gdbarch, regnum);
223 if (name == 0 || *name == '\0')
224 continue;
225
226 nr_regs++;
10f59415
SC
227 }
228
229 if (display_info->regs_content_count > 0 && !refresh_values_only)
230 {
231 tui_free_data_content (display_info->regs_content,
232 display_info->regs_content_count);
233 display_info->regs_content_count = 0;
234 }
235
236 if (display_info->regs_content_count <= 0)
237 {
238 display_info->regs_content = tui_alloc_content (nr_regs, DATA_WIN);
239 allocated_here = TRUE;
240 refresh_values_only = FALSE;
241 }
242
243 if (display_info->regs_content != (tui_win_content) NULL)
244 {
245 if (!refresh_values_only || allocated_here)
246 {
e65b5245 247 TUI_DATA_WIN->generic.content = NULL;
10f59415
SC
248 TUI_DATA_WIN->generic.content_size = 0;
249 tui_add_content_elements (&TUI_DATA_WIN->generic, nr_regs);
63a33118 250 display_info->regs_content = TUI_DATA_WIN->generic.content;
10f59415
SC
251 display_info->regs_content_count = nr_regs;
252 }
253
1cc6d956 254 /* Now set the register names and values. */
10f59415 255 pos = 0;
f57d151a 256 for (regnum = 0;
5eccfcc2
UW
257 regnum < gdbarch_num_regs (gdbarch)
258 + gdbarch_num_pseudo_regs (gdbarch);
f57d151a 259 regnum++)
10f59415
SC
260 {
261 struct tui_gen_win_info *data_item_win;
262 struct tui_data_element *data;
263 const char *name;
264
d20c1c3f 265 /* Must be in the group. */
10f59415
SC
266 if (!gdbarch_register_reggroup_p (gdbarch, regnum, group))
267 continue;
268
d20c1c3f
PA
269 /* If the register name is empty, it is undefined for this
270 processor, so don't display anything. */
271 name = gdbarch_register_name (gdbarch, regnum);
272 if (name == 0 || *name == '\0')
273 continue;
10f59415
SC
274
275 data_item_win =
276 &display_info->regs_content[pos]->which_element.data_window;
63ed8182 277 data = &data_item_win->content[0]->which_element.data;
10f59415
SC
278 if (data)
279 {
280 if (!refresh_values_only)
281 {
282 data->item_no = regnum;
283 data->name = name;
284 data->highlight = FALSE;
285 }
5eccfcc2 286 tui_get_register (frame, data, regnum, 0);
10f59415
SC
287 }
288 pos++;
289 }
290
291 TUI_DATA_WIN->generic.content_size =
292 display_info->regs_content_count + display_info->data_content_count;
293 ret = TUI_SUCCESS;
294 }
295
296 return ret;
297}
298
55fb0713 299/* Function to display the registers in the content from
6ba8e26f 300 'start_element_no' until the end of the register content or the end
55fb0713
AC
301 of the display height. No checking for displaying past the end of
302 the registers is done here. */
c906108c 303void
6ba8e26f 304tui_display_registers_from (int start_element_no)
c906108c 305{
10f59415
SC
306 struct tui_data_info *display_info = &TUI_DATA_WIN->detail.data_display_info;
307
e5908723
MS
308 if (display_info->regs_content != (tui_win_content) NULL
309 && display_info->regs_content_count > 0)
c906108c 310 {
d02c80cd 311 int i = start_element_no;
0043e6a5 312 int j, item_win_width, cur_y;
10f59415
SC
313
314 int max_len = 0;
315 for (i = 0; i < display_info->regs_content_count; i++)
316 {
317 struct tui_data_element *data;
318 struct tui_gen_win_info *data_item_win;
319 char *p;
320 int len;
321
9a2b4c1b
MS
322 data_item_win
323 = &display_info->regs_content[i]->which_element.data_window;
63ed8182 324 data = &data_item_win->content[0]->which_element.data;
10f59415
SC
325 len = 0;
326 p = data->content;
327 if (p != 0)
328 while (*p)
329 {
330 if (*p++ == '\t')
331 len = 8 * ((len / 8) + 1);
332 else
333 len++;
334 }
335
336 if (len > max_len)
337 max_len = len;
338 }
339 item_win_width = max_len + 1;
340 i = start_element_no;
341
342 display_info->regs_column_count =
343 (TUI_DATA_WIN->generic.width - 2) / item_win_width;
344 if (display_info->regs_column_count == 0)
345 display_info->regs_column_count = 1;
346 item_win_width =
347 (TUI_DATA_WIN->generic.width - 2) / display_info->regs_column_count;
348
ef5eab5a
MS
349 /* Now create each data "sub" window, and write the display into
350 it. */
6ba8e26f 351 cur_y = 1;
e5908723
MS
352 while (i < display_info->regs_content_count
353 && cur_y <= TUI_DATA_WIN->generic.viewport_height)
c906108c
SS
354 {
355 for (j = 0;
e5908723
MS
356 j < display_info->regs_column_count
357 && i < display_info->regs_content_count;
358 j++)
c906108c 359 {
5b6fe301
MS
360 struct tui_gen_win_info *data_item_win;
361 struct tui_data_element *data_element_ptr;
c906108c 362
1cc6d956 363 /* Create the window if necessary. */
10f59415
SC
364 data_item_win = &display_info->regs_content[i]
365 ->which_element.data_window;
63ed8182 366 data_element_ptr = &data_item_win->content[0]->which_element.data;
10f59415
SC
367 if (data_item_win->handle != (WINDOW*) NULL
368 && (data_item_win->height != 1
369 || data_item_win->width != item_win_width
370 || data_item_win->origin.x != (item_win_width * j) + 1
371 || data_item_win->origin.y != cur_y))
372 {
373 tui_delete_win (data_item_win->handle);
374 data_item_win->handle = 0;
375 }
376
6ba8e26f 377 if (data_item_win->handle == (WINDOW *) NULL)
c906108c 378 {
6ba8e26f 379 data_item_win->height = 1;
10f59415 380 data_item_win->width = item_win_width;
6ba8e26f
AC
381 data_item_win->origin.x = (item_win_width * j) + 1;
382 data_item_win->origin.y = cur_y;
383 tui_make_window (data_item_win, DONT_BOX_WINDOW);
384 scrollok (data_item_win->handle, FALSE);
c906108c 385 }
6ba8e26f 386 touchwin (data_item_win->handle);
fea14702 387
10f59415
SC
388 /* Get the printable representation of the register
389 and display it. */
390 tui_display_register (data_element_ptr, data_item_win);
1cc6d956 391 i++; /* Next register. */
c906108c 392 }
1cc6d956 393 cur_y++; /* Next row. */
c906108c
SS
394 }
395 }
55fb0713 396}
c906108c
SS
397
398
6ba8e26f
AC
399/* Function to display the registers in the content from
400 'start_element_no' on 'start_line_no' until the end of the register
401 content or the end of the display height. This function checks
402 that we won't display off the end of the register display. */
2c0b251b 403static void
08ef48c5
MS
404tui_display_reg_element_at_line (int start_element_no,
405 int start_line_no)
c906108c 406{
9a2b4c1b
MS
407 if (TUI_DATA_WIN->detail.data_display_info.regs_content
408 != (tui_win_content) NULL
e5908723 409 && TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
c906108c 410 {
d02c80cd 411 int element_no = start_element_no;
c906108c 412
6ba8e26f 413 if (start_element_no != 0 && start_line_no != 0)
c906108c 414 {
d02c80cd 415 int last_line_no, first_line_on_last_page;
c906108c 416
6ba8e26f 417 last_line_no = tui_last_regs_line_no ();
9a2b4c1b
MS
418 first_line_on_last_page
419 = last_line_no - (TUI_DATA_WIN->generic.height - 2);
6ba8e26f
AC
420 if (first_line_on_last_page < 0)
421 first_line_on_last_page = 0;
ef5eab5a
MS
422
423 /* If there is no other data displayed except registers, and
424 the element_no causes us to scroll past the end of the
425 registers, adjust what element to really start the
426 display at. */
e5908723
MS
427 if (TUI_DATA_WIN->detail.data_display_info.data_content_count <= 0
428 && start_line_no > first_line_on_last_page)
9a2b4c1b
MS
429 element_no
430 = tui_first_reg_element_no_inline (first_line_on_last_page);
c906108c 431 }
6ba8e26f 432 tui_display_registers_from (element_no);
c906108c 433 }
6ba8e26f 434}
c906108c
SS
435
436
437
6ba8e26f 438/* Function to display the registers starting at line line_no in the
55fb0713
AC
439 data window. Answers the line number that the display actually
440 started from. If nothing is displayed (-1) is returned. */
c906108c 441int
08ef48c5
MS
442tui_display_registers_from_line (int line_no,
443 int force_display)
c906108c 444{
6d012f14 445 if (TUI_DATA_WIN->detail.data_display_info.regs_content_count > 0)
c906108c 446 {
6ba8e26f 447 int line, element_no;
c906108c 448
6ba8e26f 449 if (line_no < 0)
c906108c 450 line = 0;
6ba8e26f 451 else if (force_display)
ef5eab5a
MS
452 { /* If we must display regs (force_display is true), then
453 make sure that we don't display off the end of the
454 registers. */
6ba8e26f 455 if (line_no >= tui_last_regs_line_no ())
c906108c 456 {
55fb0713 457 if ((line = tui_line_from_reg_element_no (
6d012f14 458 TUI_DATA_WIN->detail.data_display_info.regs_content_count - 1)) < 0)
c906108c
SS
459 line = 0;
460 }
461 else
6ba8e26f 462 line = line_no;
c906108c
SS
463 }
464 else
6ba8e26f 465 line = line_no;
c906108c 466
6ba8e26f 467 element_no = tui_first_reg_element_no_inline (line);
9a2b4c1b
MS
468 if (element_no
469 < TUI_DATA_WIN->detail.data_display_info.regs_content_count)
6ba8e26f 470 tui_display_reg_element_at_line (element_no, line);
c906108c
SS
471 else
472 line = (-1);
473
474 return line;
475 }
476
1cc6d956 477 return (-1); /* Nothing was displayed. */
55fb0713 478}
c906108c
SS
479
480
55fb0713
AC
481/* This function check all displayed registers for changes in values,
482 given a particular frame. If the values have changed, they are
483 updated with the new value and highlighted. */
c906108c 484void
55fb0713 485tui_check_register_values (struct frame_info *frame)
c906108c 486{
e5908723
MS
487 if (TUI_DATA_WIN != NULL
488 && TUI_DATA_WIN->generic.is_visible)
c906108c 489 {
10f59415
SC
490 struct tui_data_info *display_info
491 = &TUI_DATA_WIN->detail.data_display_info;
492
e5908723
MS
493 if (display_info->regs_content_count <= 0
494 && display_info->display_regs)
10f59415 495 tui_show_registers (display_info->current_group);
c906108c
SS
496 else
497 {
0043e6a5 498 int i;
c906108c 499
10f59415 500 for (i = 0; (i < display_info->regs_content_count); i++)
c906108c 501 {
10f59415
SC
502 struct tui_data_element *data;
503 struct tui_gen_win_info *data_item_win_ptr;
6ba8e26f 504 int was_hilighted;
c906108c 505
10f59415
SC
506 data_item_win_ptr = &display_info->regs_content[i]->
507 which_element.data_window;
63ed8182 508 data = &data_item_win_ptr->content[0]->which_element.data;
10f59415
SC
509 was_hilighted = data->highlight;
510
5eccfcc2 511 tui_get_register (frame, data,
10f59415
SC
512 data->item_no, &data->highlight);
513
514 if (data->highlight || was_hilighted)
c906108c 515 {
10f59415 516 tui_display_register (data, data_item_win_ptr);
c906108c
SS
517 }
518 }
519 }
520 }
55fb0713 521}
c906108c 522
1cc6d956
MS
523/* Display a register in a window. If hilite is TRUE, then the value
524 will be displayed in reverse video. */
10f59415
SC
525static void
526tui_display_register (struct tui_data_element *data,
527 struct tui_gen_win_info *win_info)
528{
529 if (win_info->handle != (WINDOW *) NULL)
530 {
531 int i;
c906108c 532
10f59415 533 if (data->highlight)
cae3f17b
JB
534 /* We ignore the return value, casting it to void in order to avoid
535 a compiler warning. The warning itself was introduced by a patch
536 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
537 to code that causes the compiler to generate an unused-value
538 warning. */
ae3bccd4 539 (void) wstandout (win_info->handle);
10f59415
SC
540
541 wmove (win_info->handle, 0, 0);
542 for (i = 1; i < win_info->width; i++)
543 waddch (win_info->handle, ' ');
544 wmove (win_info->handle, 0, 0);
545 if (data->content)
546 waddstr (win_info->handle, data->content);
547
548 if (data->highlight)
cae3f17b
JB
549 /* We ignore the return value, casting it to void in order to avoid
550 a compiler warning. The warning itself was introduced by a patch
551 to ncurses 5.7 dated 2009-08-29, changing this macro to expand
552 to code that causes the compiler to generate an unused-value
553 warning. */
ae3bccd4 554 (void) wstandend (win_info->handle);
10f59415
SC
555 tui_refresh_win (win_info);
556 }
557}
558
51f0e40d
AB
559/* Helper for "tui reg next", wraps a call to REGGROUP_NEXT, but adds wrap
560 around behaviour. Returns the next register group, or NULL if the
561 register window is not currently being displayed. */
562
563static struct reggroup *
564tui_reg_next (struct gdbarch *gdbarch)
c906108c 565{
51f0e40d 566 struct reggroup *group = NULL;
e17c207e 567
b75c69bb 568 if (TUI_DATA_WIN != NULL)
10f59415 569 {
51f0e40d 570 group = TUI_DATA_WIN->detail.data_display_info.current_group;
e17c207e 571 group = reggroup_next (gdbarch, group);
b75c69bb
AB
572 if (group == NULL)
573 group = reggroup_next (gdbarch, NULL);
10f59415 574 }
51f0e40d 575 return group;
10f59415
SC
576}
577
51f0e40d
AB
578/* Helper for "tui reg prev", wraps a call to REGGROUP_PREV, but adds wrap
579 around behaviour. Returns the previous register group, or NULL if the
580 register window is not currently being displayed. */
55b40027 581
51f0e40d
AB
582static struct reggroup *
583tui_reg_prev (struct gdbarch *gdbarch)
55b40027 584{
51f0e40d 585 struct reggroup *group = NULL;
55b40027
AB
586
587 if (TUI_DATA_WIN != NULL)
588 {
51f0e40d 589 group = TUI_DATA_WIN->detail.data_display_info.current_group;
55b40027
AB
590 group = reggroup_prev (gdbarch, group);
591 if (group == NULL)
592 group = reggroup_prev (gdbarch, NULL);
55b40027 593 }
51f0e40d 594 return group;
55b40027
AB
595}
596
51f0e40d
AB
597/* Implement the 'tui reg' command. Changes the register group displayed
598 in the tui register window. Displays the tui register window if it is
599 not already on display. */
c906108c 600
10f59415 601static void
e2d8ae16 602tui_reg_command (const char *args, int from_tty)
10f59415 603{
51f0e40d 604 struct gdbarch *gdbarch = get_current_arch ();
c906108c 605
51f0e40d
AB
606 if (args != NULL)
607 {
608 struct reggroup *group, *match = NULL;
609 size_t len = strlen (args);
610
611 /* Make sure the curses mode is enabled. */
612 tui_enable ();
613
614 /* Make sure the register window is visible. If not, select an
615 appropriate layout. We need to do this before trying to run the
616 'next' or 'prev' commands. */
617 if (TUI_DATA_WIN == NULL || !TUI_DATA_WIN->generic.is_visible)
618 tui_set_layout_by_name (DATA_NAME);
619
620 if (strncmp (args, "next", len) == 0)
621 match = tui_reg_next (gdbarch);
622 else if (strncmp (args, "prev", len) == 0)
623 match = tui_reg_prev (gdbarch);
624
625 /* This loop matches on the initial part of a register group
626 name. If this initial part in ARGS matches only one register
627 group then the switch is made. */
628 for (group = reggroup_next (gdbarch, NULL);
629 group != NULL;
630 group = reggroup_next (gdbarch, group))
631 {
632 if (strncmp (reggroup_name (group), args, len) == 0)
633 {
634 if (match != NULL)
635 error (_("ambiguous register group name '%s'"), args);
636 match = group;
637 }
638 }
639
640 if (match == NULL)
641 error (_("unknown register group '%s'"), args);
642
643 tui_show_registers (match);
644 }
645 else
646 {
647 struct reggroup *group;
648 int first;
649
650 printf_unfiltered (_("\"tui reg\" must be followed by the name of "
651 "either a register group,\nor one of 'next' "
652 "or 'prev'. Known register groups are:\n"));
653
654 for (first = 1, group = reggroup_next (gdbarch, NULL);
655 group != NULL;
656 first = 0, group = reggroup_next (gdbarch, group))
657 {
658 if (!first)
659 printf_unfiltered (", ");
660 printf_unfiltered ("%s", reggroup_name (group));
661 }
662
663 printf_unfiltered ("\n");
664 }
10f59415
SC
665}
666
51f0e40d
AB
667/* Complete names of register groups, and add the special "prev" and "next"
668 names. */
c906108c 669
eb3ff9a5 670static void
51f0e40d 671tui_reggroup_completer (struct cmd_list_element *ignore,
eb3ff9a5 672 completion_tracker &tracker,
51f0e40d 673 const char *text, const char *word)
10f59415 674{
51f0e40d
AB
675 static const char *extra[] = { "next", "prev", NULL };
676 size_t len = strlen (word);
677 const char **tmp;
678
eb3ff9a5 679 reggroup_completer (ignore, tracker, text, word);
51f0e40d 680
eb3ff9a5 681 /* XXXX use complete_on_enum instead? */
51f0e40d
AB
682 for (tmp = extra; *tmp != NULL; ++tmp)
683 {
684 if (strncmp (word, *tmp, len) == 0)
eb3ff9a5 685 tracker.add_completion (gdb::unique_xmalloc_ptr<char> (xstrdup (*tmp)));
51f0e40d 686 }
10f59415 687}
c906108c
SS
688
689void
6ba8e26f 690_initialize_tui_regs (void)
c906108c 691{
51f0e40d 692 struct cmd_list_element **tuicmd, *cmd;
10f59415
SC
693
694 tuicmd = tui_get_cmd_list ();
695
51f0e40d
AB
696 cmd = add_cmd ("reg", class_tui, tui_reg_command, _("\
697TUI command to control the register window."), tuicmd);
698 set_cmd_completer (cmd, tui_reggroup_completer);
41783295 699}
c906108c
SS
700
701
702/*****************************************
703** STATIC LOCAL FUNCTIONS **
704******************************************/
705
6eed1678
PA
706/* Get the register from the frame and return a printable
707 representation of it. */
708
709static char *
710tui_register_format (struct frame_info *frame, int regnum)
c906108c 711{
5eccfcc2 712 struct gdbarch *gdbarch = get_frame_arch (frame);
d20c1c3f 713
d7e74731
PA
714 string_file stream;
715
31b68d4a
TT
716 scoped_restore save_pagination
717 = make_scoped_restore (&pagination_enabled, 0);
718 scoped_restore save_stdout
719 = make_scoped_restore (&gdb_stdout, &stream);
720
d7e74731 721 gdbarch_print_registers_info (gdbarch, &stream, frame, regnum, 1);
c46cc7df
SC
722
723 /* Remove the possible \n. */
d7e74731
PA
724 std::string &str = stream.string ();
725 if (!str.empty () && str.back () == '\n')
726 str.resize (str.size () - 1);
c46cc7df 727
312809f8 728 /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */
31b68d4a 729 return tui_expand_tabs (str.c_str (), 0);
c46cc7df 730}
c906108c 731
1cc6d956
MS
732/* Get the register value from the given frame and format it for the
733 display. When changep is set, check if the new register value has
734 changed with respect to the previous call. */
22940a24 735static enum tui_status
5eccfcc2 736tui_get_register (struct frame_info *frame,
08ef48c5
MS
737 struct tui_data_element *data,
738 int regnum, int *changedp)
c906108c 739{
22940a24 740 enum tui_status ret = TUI_FAILURE;
c906108c 741
10f59415
SC
742 if (changedp)
743 *changedp = FALSE;
c906108c
SS
744 if (target_has_registers)
745 {
6eed1678 746 char *prev_content = data->content;
10f59415 747
6eed1678 748 data->content = tui_register_format (frame, regnum);
9c5ea4d9 749
6eed1678
PA
750 if (changedp != NULL
751 && strcmp (prev_content, data->content) != 0)
752 *changedp = 1;
d20c1c3f 753
6eed1678 754 xfree (prev_content);
9c5ea4d9
UW
755
756 ret = TUI_SUCCESS;
c906108c 757 }
c906108c 758 return ret;
6ba8e26f 759}