aboutsummaryrefslogtreecommitdiff
path: root/gdb/valprint.c
diff options
context:
space:
mode:
authorEnze Li <enze.li@hotmail.com>2022-06-12 13:22:25 +0800
committerEnze Li <enze.li@hotmail.com>2022-06-18 11:23:06 +0800
commit21a527dfc85d62c9e90c65ac4076f517a6b76a48 (patch)
treeead0bfa6f75933bf50a82b62d692d08504ab5e2a /gdb/valprint.c
parent40d4cb8bccb0f46f34826564ea3502717ca8f0ce (diff)
downloadgdb-21a527dfc85d62c9e90c65ac4076f517a6b76a48.zip
gdb-21a527dfc85d62c9e90c65ac4076f517a6b76a48.tar.gz
gdb-21a527dfc85d62c9e90c65ac4076f517a6b76a48.tar.bz2
gdb: Add new 'print nibbles' feature
Make an introduction of a new print setting that can be set by 'set print nibbles [on|off]'. The default value if OFF, which can be changed by user manually. Of course, 'show print nibbles' is also included in the patch. The new feature displays binary values by group, with four bits per group. The motivation for this work is to enhance the readability of binary values. Here's a GDB session before this patch is applied. (gdb) print var_a $1 = 1230 (gdb) print/t var_a $2 = 10011001110 With this patch applied, we can use the new print setting to display the new form of the binary values. (gdb) print var_a $1 = 1230 (gdb) print/t var_a $2 = 10011001110 (gdb) set print nibbles on (gdb) print/t var_a $3 = 0100 1100 1110 Tested on x86_64 openSUSE Tumbleweed.
Diffstat (limited to 'gdb/valprint.c')
-rw-r--r--gdb/valprint.c48
1 files changed, 47 insertions, 1 deletions
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 4711467..e63ec30 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -105,6 +105,7 @@ struct value_print_options user_print_options =
0, /* vtblprint */
1, /* unionprint */
1, /* addressprint */
+ false, /* nibblesprint */
0, /* objectprint */
PRINT_MAX_DEFAULT, /* print_max */
10, /* repeat_count_threshold */
@@ -257,6 +258,17 @@ show_unionprint (struct ui_file *file, int from_tty,
value);
}
+/* Controls the format of printing binary values. */
+
+static void
+show_nibbles (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ gdb_printf (file,
+ _("Printing binary values in groups is %s.\n"),
+ value);
+}
+
/* If nonzero, causes machine addresses to be printed in certain contexts. */
static void
@@ -1357,18 +1369,23 @@ print_floating (const gdb_byte *valaddr, struct type *type,
void
print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
- unsigned len, enum bfd_endian byte_order, bool zero_pad)
+ unsigned len, enum bfd_endian byte_order, bool zero_pad,
+ const struct value_print_options *options)
{
const gdb_byte *p;
unsigned int i;
int b;
bool seen_a_one = false;
+ const char *digit_separator = nullptr;
/* Declared "int" so it will be signed.
This ensures that right shift will shift in zeros. */
const int mask = 0x080;
+ if (options->nibblesprint)
+ digit_separator = current_language->get_digit_separator();
+
if (byte_order == BFD_ENDIAN_BIG)
{
for (p = valaddr;
@@ -1380,6 +1397,9 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
{
+ if (options->nibblesprint && seen_a_one && i % 4 == 0)
+ gdb_putc (*digit_separator, stream);
+
if (*p & (mask >> i))
b = '1';
else
@@ -1387,6 +1407,13 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
if (zero_pad || seen_a_one || b == '1')
gdb_putc (b, stream);
+ else if (options->nibblesprint)
+ {
+ if ((0xf0 & (mask >> i) && (*p & 0xf0))
+ || (0x0f & (mask >> i) && (*p & 0x0f)))
+ gdb_putc (b, stream);
+ }
+
if (b == '1')
seen_a_one = true;
}
@@ -1400,6 +1427,9 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
{
for (i = 0; i < (HOST_CHAR_BIT * sizeof (*p)); i++)
{
+ if (options->nibblesprint && seen_a_one && i % 4 == 0)
+ gdb_putc (*digit_separator, stream);
+
if (*p & (mask >> i))
b = '1';
else
@@ -1407,6 +1437,13 @@ print_binary_chars (struct ui_file *stream, const gdb_byte *valaddr,
if (zero_pad || seen_a_one || b == '1')
gdb_putc (b, stream);
+ else if (options->nibblesprint)
+ {
+ if ((0xf0 & (mask >> i) && (*p & 0xf0))
+ || (0x0f & (mask >> i) && (*p & 0x0f)))
+ gdb_putc (b, stream);
+ }
+
if (b == '1')
seen_a_one = true;
}
@@ -2837,6 +2874,15 @@ static const gdb::option::option_def value_print_option_defs[] = {
NULL, /* help_doc */
},
+ boolean_option_def {
+ "nibbles",
+ [] (value_print_options *opt) { return &opt->nibblesprint; },
+ show_nibbles, /* show_cmd_cb */
+ N_("Set whether to print binary values in groups of four bits."),
+ N_("Show whether to print binary values in groups of four bits."),
+ NULL, /* help_doc */
+ },
+
uinteger_option_def {
"elements",
[] (value_print_options *opt) { return &opt->print_max; },