From: Lancelot SIX via Gdb-patches <gdb-patches@sourceware.org>
To: gdb-patches@sourceware.org
Cc: Lancelot SIX <lsix@lancelotsix.com>
Subject: [PATCH 2/2] ptype: add option to use hexadecimal notation
Date: Thu, 31 Dec 2020 12:53:24 +0000 [thread overview]
Message-ID: <20201231125324.14779-3-lsix@lancelotsix.com> (raw)
In-Reply-To: <20201231125324.14779-1-lsix@lancelotsix.com>
This commit adds a flag to the ptype command in order to print the
offsets and sizes of struct members using the hexadecimal notation. The
'x' flag ensures use of the hexadecimal notation while the 'X' flag
ensures use of the decimal notation. The default is to use decimal
notation.
Before this patch, gdb only uses decimal notation, as pointed out in bug
cli/22640.
Here is an example of this new behavior with hex output turned on:
(gdb) ptype /ox struct type_print_options
/* offset | size */ type = struct type_print_options {
/* 0x00: 0 | 0x04 */ unsigned int raw : 1;
/* 0x00: 1 | 0x04 */ unsigned int print_methods : 1;
/* 0x00: 2 | 0x04 */ unsigned int print_typedefs : 1;
/* 0x00: 3 | 0x04 */ unsigned int print_offsets : 1;
/* 0x00: 4 | 0x04 */ unsigned int print_in_hex : 1;
/* XXX 3-bit hole */
/* XXX 3-byte hole */
/* 0x04 | 0x04 */ int print_nested_type_limit;
/* 0x08 | 0x08 */ typedef_hash_table *local_typedefs;
/* 0x10 | 0x08 */ typedef_hash_table *global_typedefs;
/* 0x18 | 0x08 */ ext_lang_type_printers *global_printers;
/* total size (bytes): 32 */
}
This patch also adds the 'set print type hex' and 'show print type hex'
commands in order to set and inspect the default behavior regarding the
use of decimal or hexadecimal notation when printing struct sizes and
offsets.
Tested using 'runtest -tool gdb ptype-offsets.exp'
gdb/ChangeLog:
PR cli/22640
* typeprint.h (struct type_print_options): Add print_in_hex
flag.
(struct print_offset_data): Add print_in_hex flag, add a
constructor accepting a type_print_options* argument.
* typeprint.c (print_offset_data::print_offset_data): Add
ctor.
(print_offset_data::update): Handle print_in_hex flag when
printing unions.
(whatis_exp): Handle 'x' and 'X' flags.
(set_print_offsets_and_sizes_in_hex): Create.
(show_print_offsets_and_sizes_in_hex): Create.
(_initialize_typeprint): Update help message for the ptype
command, register the 'set print type hex' and 'show print type
hex' commands.
* c-typeprint.c (c_print_type): Construct the print_offset_data
object using the type_print_optons parameter.
(c_type_print_base_struct_union): Construct the print_offset_data
object using the type_print_optons parameter.
(c_type_print_base): Construct the print_offset_data object using
the type_print_optons parameter.
* rust-lang.c (rust_language::print_type): Construct the
print_offset_data object using the type_print_optons parameter.
gdb/doc/ChangeLog:
PR cli/22640
* gdb.texinfo (Symbols): Describe the 'x' and 'X' flags of the
ptype command, describe 'set print type hex' and 'show print
type hex' commands.
gdb/testsuite/ChangeLog:
PR cli/22640
* gdb.base/ptype-offsets.exp: Add tests to verify the behavior
of 'ptype/ox' and 'ptype/oX'. Check that 'set print type hex'
changes the default behavior of 'ptype/o'.
---
gdb/c-typeprint.c | 8 +-
gdb/doc/gdb.texinfo | 27 ++++++
gdb/rust-lang.c | 2 +-
gdb/testsuite/gdb.base/ptype-offsets.exp | 114 +++++++++++++++++++++++
gdb/typeprint.c | 72 +++++++++++++-
gdb/typeprint.h | 9 ++
6 files changed, 222 insertions(+), 10 deletions(-)
diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c
index b6f1eee760..521e7d1538 100644
--- a/gdb/c-typeprint.c
+++ b/gdb/c-typeprint.c
@@ -171,7 +171,7 @@ c_print_type (struct type *type,
int show, int level,
const struct type_print_options *flags)
{
- struct print_offset_data podata;
+ struct print_offset_data podata (flags);
c_print_type_1 (type, varstring, stream, show, level,
current_language->la_language, flags, &podata);
@@ -188,7 +188,7 @@ c_print_type (struct type *type,
enum language language,
const struct type_print_options *flags)
{
- struct print_offset_data podata;
+ struct print_offset_data podata (flags);
c_print_type_1 (type, varstring, stream, show, level, language, flags,
&podata);
@@ -1149,7 +1149,7 @@ c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
int len = type->num_fields ();
vptr_fieldno = get_vptr_fieldno (type, &basetype);
- struct print_offset_data local_podata;
+ struct print_offset_data local_podata (flags);
for (int i = TYPE_N_BASECLASSES (type); i < len; i++)
{
@@ -1724,7 +1724,7 @@ c_type_print_base (struct type *type, struct ui_file *stream,
int show, int level,
const struct type_print_options *flags)
{
- struct print_offset_data podata;
+ struct print_offset_data podata (flags);
c_type_print_base_1 (type, stream, show, level,
current_language->la_language, flags, &podata);
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 93e722881a..35f7b6bf62 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -18748,6 +18748,25 @@ types.
This command shows the current setting of typedef display when
printing classes.
+@kindex set print type hex
+@item set print type hex
+@itemx set print type hex on
+@itemx set print type hex off
+
+When @value{GDBN} prints sizes and offsets of struct members, decimal or
+hexadecimal notations can be used. Selecting one or the other can be
+done either by passing the appropriate flag to @code{ptype}, or by using
+@command{set print type hex}. Specifying @code{on} causes @value{GDBN}
+to print sizes and offsets of struct members using hexadecimal notation.
+Specifying @code{off} causes @value{GDBN} to print sizes and offsets of
+struct members using decimal notation; this is the default.
+
+@kindex show print type hex
+@item show print type hex
+This command show the current setting used to configure whether decimal or
+hexadecimal notation should be used when printing sized and offsets of
+struct members.
+
@kindex info address
@cindex address of a symbol
@item info address @var{symbol}
@@ -18860,6 +18879,14 @@ exists in case you change the default with @command{set print type typedefs}.
Print the offsets and sizes of fields in a struct, similar to what the
@command{pahole} tool does. This option implies the @code{/tm} flags.
+@item x
+Use hexadecimal notation when printing offsets and sizes of fields in a
+struct.
+
+@item X
+Use decimal notation when printing offsets and sizes of fields in a
+struct.
+
For example, given the following declarations:
@smallexample
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index cda739760c..a73d41f8a6 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -1906,7 +1906,7 @@ rust_language::print_type (struct type *type, const char *varstring,
struct ui_file *stream, int show, int level,
const struct type_print_options *flags) const
{
- print_offset_data podata;
+ print_offset_data podata (flags);
rust_internal_print_type (type, varstring, stream, show, level,
flags, false, &podata);
}
diff --git a/gdb/testsuite/gdb.base/ptype-offsets.exp b/gdb/testsuite/gdb.base/ptype-offsets.exp
index 39b467bf9e..0ff24e0981 100644
--- a/gdb/testsuite/gdb.base/ptype-offsets.exp
+++ b/gdb/testsuite/gdb.base/ptype-offsets.exp
@@ -57,6 +57,31 @@ gdb_test "ptype /o struct abc" \
" /* total size (bytes): 56 */" \
" \}"]]
+# test "ptype /ox"
+gdb_test "ptype /ox struct abc" \
+ [string_to_regexp [multi_line \
+"/* offset | size */ type = struct abc {" \
+" public:" \
+"/* 0x08 | 0x08 */ void *field1;" \
+"/* 0x16: 0 | 0x04 */ unsigned int field2 : 1;" \
+"/* XXX 7-bit hole */" \
+"/* XXX 3-byte hole */" \
+"/* 0x14 | 0x04 */ int field3;" \
+"/* 0x18 | 0x01 */ signed char field4;" \
+"/* XXX 7-byte hole */" \
+"/* 0x20 | 0x08 */ uint64_t field5;" \
+"/* 0x28 | 0x08 */ union \{" \
+"/* 0x08 */ void *field6;" \
+"/* 0x04 */ int field7;" \
+"" \
+" /* total size (bytes): 8 */" \
+" \} field8;" \
+"/* 0x30 | 0x02 */ my_int_type field9;" \
+"/* XXX 6-byte padding */" \
+"" \
+" /* total size (bytes): 56 */" \
+" \}"]]
+
# Test "ptype /oTM".
gdb_test "ptype /oTM struct abc" \
[string_to_regexp [multi_line \
@@ -141,6 +166,36 @@ gdb_test "ptype /o struct pqr" \
" /* total size (bytes): 56 */" \
" \}"]]
+# Test nested struct with /x
+gdb_test "ptype /ox struct pqr" \
+ [string_to_regexp [multi_line \
+"/* offset | size */ type = struct pqr \{" \
+"/* 0x00 | 0x04 */ int ff1;" \
+"/* XXX 4-byte hole */" \
+"/* 0x08 | 0x28 */ struct xyz \{" \
+"/* 0x08 | 0x04 */ int f1;" \
+"/* 0x0c | 0x01 */ signed char f2;" \
+"/* XXX 3-byte hole */" \
+"/* 0x10 | 0x08 */ void *f3;" \
+"/* 0x18 | 0x18 */ struct tuv \{" \
+"/* 0x18 | 0x04 */ int a1;" \
+"/* XXX 4-byte hole */" \
+"/* 0x20 | 0x08 */ signed char *a2;" \
+"/* 0x28 | 0x04 */ int a3;" \
+"/* XXX 4-byte padding */" \
+"" \
+" /* total size (bytes): 24 */" \
+" \} f4;" \
+"" \
+" /* total size (bytes): 40 */" \
+" \} ff2;" \
+"/* 0x30 | 0x01 */ signed char ff3;" \
+"/* XXX 7-byte padding */" \
+"" \
+" /* total size (bytes): 56 */" \
+" \}"]]
+
+
# Test that the offset is properly reset when we are printing a union
# and go inside two inner structs.
# This also tests a struct inside a struct inside a union.
@@ -340,3 +395,62 @@ gdb_test "ptype/o static_member" \
"" \
" /* total size (bytes): 4 */" \
" \}"]]
+
+with_test_prefix "with_hex_default" {
+ # Test setting default display to hex
+ send_gdb "set print type hex on\n"
+ gdb_test "show print type hex" \
+ "Display of struct members offsets and sizes in hexadecimal is on"
+
+ # test "ptype /o" is now equivalent to "ptype /ox"
+ gdb_test "ptype /o struct abc" \
+ [string_to_regexp [multi_line \
+ "/* offset | size */ type = struct abc \{" \
+ " public:" \
+ "/* 0x08 | 0x08 */ void *field1;" \
+ "/* 0x16: 0 | 0x04 */ unsigned int field2 : 1;" \
+ "/* XXX 7-bit hole */" \
+ "/* XXX 3-byte hole */" \
+ "/* 0x14 | 0x04 */ int field3;" \
+ "/* 0x18 | 0x01 */ signed char field4;" \
+ "/* XXX 7-byte hole */" \
+ "/* 0x20 | 0x08 */ uint64_t field5;" \
+ "/* 0x28 | 0x08 */ union \{" \
+ "/* 0x08 */ void *field6;" \
+ "/* 0x04 */ int field7;" \
+ "" \
+ " /* total size (bytes): 8 */" \
+ " \} field8;" \
+ "/* 0x30 | 0x02 */ my_int_type field9;" \
+ "/* XXX 6-byte padding */" \
+ "" \
+ " /* total size (bytes): 56 */" \
+ " \}"]]
+
+ gdb_test "ptype /oX struct abc" \
+ [string_to_regexp [multi_line \
+ "/* offset | size */ type = struct abc \{" \
+ " public:" \
+ "/* 8 | 8 */ void *field1;" \
+ "/* 16: 0 | 4 */ unsigned int field2 : 1;" \
+ "/* XXX 7-bit hole */" \
+ "/* XXX 3-byte hole */" \
+ "/* 20 | 4 */ int field3;" \
+ "/* 24 | 1 */ signed char field4;" \
+ "/* XXX 7-byte hole */" \
+ "/* 32 | 8 */ uint64_t field5;" \
+ "/* 40 | 8 */ union \{" \
+ "/* 8 */ void *field6;" \
+ "/* 4 */ int field7;" \
+ "" \
+ " /* total size (bytes): 8 */" \
+ " \} field8;" \
+ "/* 48 | 2 */ my_int_type field9;" \
+ "/* XXX 6-byte padding */" \
+ "" \
+ " /* total size (bytes): 56 */" \
+ " \}"]]
+
+ # restore
+ send_gdb "set print type hex off\n"
+}
diff --git a/gdb/typeprint.c b/gdb/typeprint.c
index 49877710fa..a92d787f0a 100644
--- a/gdb/typeprint.c
+++ b/gdb/typeprint.c
@@ -44,6 +44,7 @@ const struct type_print_options type_print_raw_options =
1, /* print_methods */
1, /* print_typedefs */
0, /* print_offsets */
+ 0, /* print_in_hex */
0, /* print_nested_type_limit */
NULL, /* local_typedefs */
NULL, /* global_table */
@@ -58,6 +59,7 @@ static struct type_print_options default_ptype_flags =
1, /* print_methods */
1, /* print_typedefs */
0, /* print_offsets */
+ 0, /* print_in_hex */
0, /* print_nested_type_limit */
NULL, /* local_typedefs */
NULL, /* global_table */
@@ -70,6 +72,13 @@ static struct type_print_options default_ptype_flags =
const int print_offset_data::indentation = 23;
+/* See typeprint.h. */
+
+print_offset_data::print_offset_data(const struct type_print_options *flags)
+{
+ if (flags != nullptr)
+ print_in_hex = flags->print_in_hex;
+}
/* See typeprint.h. */
@@ -122,7 +131,9 @@ print_offset_data::update (struct type *type, unsigned int field_idx,
/* Since union fields don't have the concept of offsets, we just
print their sizes. */
fprintf_filtered (stream, "/* %4s */",
- pulongest (TYPE_LENGTH (ftype)));
+ (print_in_hex ?
+ hex_string_custom (TYPE_LENGTH (ftype), 2) :
+ pulongest (TYPE_LENGTH (ftype))));
return;
}
@@ -140,20 +151,23 @@ print_offset_data::update (struct type *type, unsigned int field_idx,
unsigned real_bitpos = bitpos + offset_bitpos;
- fprintf_filtered (stream, "/* %4u:%2u", real_bitpos / TARGET_CHAR_BIT,
+ fprintf_filtered (stream,
+ (print_in_hex ? "/* 0x%02u:%2u" : "/* %4u:%2u"),
+ real_bitpos / TARGET_CHAR_BIT,
real_bitpos % TARGET_CHAR_BIT);
}
else
{
/* The position of the field, relative to the beginning of the
struct. */
- fprintf_filtered (stream, "/* %4u",
+ fprintf_filtered (stream, (print_in_hex ? "/* 0x%02x" : "/* %4u"),
(bitpos + offset_bitpos) / TARGET_CHAR_BIT);
fprintf_filtered (stream, " ");
}
- fprintf_filtered (stream, " | %4u */", fieldsize_byte);
+ fprintf_filtered (stream, (print_in_hex ? " | 0x%02x */" : " | %4u */"),
+ fieldsize_byte);
end_bitpos = bitpos + fieldsize_bit;
}
@@ -468,6 +482,12 @@ whatis_exp (const char *exp, int show)
}
break;
}
+ case 'x':
+ flags.print_in_hex = 1;
+ break;
+ case 'X':
+ flags.print_in_hex = 0;
+ break;
default:
error (_("unrecognized flag '%c'"), *exp);
}
@@ -763,6 +783,35 @@ show_print_type_nested_types (struct ui_file *file, int from_tty,
}
}
+/* When printing structs, offsets and sizes of members can be displayed using
+ decimal notation or hexadecimal notation. By default, Decimal notation is
+ used. */
+
+static bool print_offsets_and_sizes_in_hex = false;
+
+/* Set the flags that instructs if sizes and offsets of struct members are
+ displayed in hexadecimal or decimal notation. */
+
+static void
+set_print_offsets_and_sizes_in_hex (const char *args,
+ int from_tty, struct cmd_list_element *c)
+{
+ default_ptype_flags.print_in_hex = print_offsets_and_sizes_in_hex;
+}
+
+/* Display whether struct members sizes and offsets are printed
+ using decimal or hexadecimal notation */
+
+static void
+show_print_offsets_and_sizes_in_hex (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c,
+ const char *value)
+{
+ fprintf_filtered (file, _("\
+Display of struct members offsets and sizes in hexadecimal is %s\n"),
+ value);
+}
+
void _initialize_typeprint ();
void
_initialize_typeprint ()
@@ -784,7 +833,11 @@ Available FLAGS are:\n\
/M print methods defined in a class\n\
/t do not print typedefs defined in a class\n\
/T print typedefs defined in a class\n\
- /o print offsets and sizes of fields in a struct (like pahole)"));
+ /o print offsets and sizes of fields in a struct (like pahole)\n\
+ /x use hexadecimal notation when displaying sizes and offsets\n\
+ of struct members\n\
+ /X use decimal notation when displaying sizes and offsets\n\
+ of struct members "));
set_cmd_completer (c, expression_completer);
c = add_com ("whatis", class_vars, whatis_command,
@@ -825,6 +878,15 @@ Show the number of recursive nested type definitions to print."), NULL,
set_print_type_nested_types,
show_print_type_nested_types,
&setprinttypelist, &showprinttypelist);
+
+ add_setshow_boolean_cmd ("hex", no_class, &print_offsets_and_sizes_in_hex,
+ _("Set printing of struct members sizes and offsets \
+ using hex notation"),
+ _("Show printing of struct members sizes and offsets \
+ using hex notation"), nullptr,
+ set_print_offsets_and_sizes_in_hex,
+ show_print_offsets_and_sizes_in_hex,
+ &setprinttypelist, &showprinttypelist);
}
/* Print <not allocated> status to stream STREAM. */
diff --git a/gdb/typeprint.h b/gdb/typeprint.h
index b75fa4d9f2..343d404b15 100644
--- a/gdb/typeprint.h
+++ b/gdb/typeprint.h
@@ -40,6 +40,9 @@ struct type_print_options
/* True means to print offsets, a la 'pahole'. */
unsigned int print_offsets : 1;
+ /* True means to print offsets in hex, otherwise use decimal */
+ unsigned int print_in_hex : 1;
+
/* The number of nested type definitions to print. -1 == all. */
int print_nested_type_limit;
@@ -58,6 +61,10 @@ struct type_print_options
struct print_offset_data
{
+ /* Indicate if the offset and size fields should be printed in decimal
+ (default) or hexadecimal. */
+ bool print_in_hex = false;
+
/* The offset to be applied to bitpos when PRINT_OFFSETS is true.
This is needed for when we are printing nested structs and want
to make sure that the printed offset for each field carries over
@@ -92,6 +99,8 @@ struct print_offset_data
certain field. */
static const int indentation;
+ explicit print_offset_data (const struct type_print_options *flags);
+
private:
/* Helper function for ptype/o implementation that prints
--
2.29.2
next prev parent reply other threads:[~2020-12-31 12:53 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-31 12:53 [PATCH 0/2][PR cli/22640] Have ptype support hex display of offsets and sizes Lancelot SIX via Gdb-patches
2020-12-31 12:53 ` [PATCH 1/2] typeprint.h: reorder struct declaration Lancelot SIX via Gdb-patches
2020-12-31 13:01 ` Lancelot SIX via Gdb-patches
2020-12-31 15:51 ` Andrew Burgess
2020-12-31 12:53 ` Lancelot SIX via Gdb-patches [this message]
2021-04-13 23:18 ` [PATCH 2/2] ptype: add option to use hexadecimal notation John Baldwin
2021-04-14 0:08 ` Paul Koning via Gdb-patches
2021-04-14 22:45 ` Lancelot SIX via Gdb-patches
2021-04-15 0:08 ` Paul Koning via Gdb-patches
2021-04-14 22:33 ` Lancelot SIX via Gdb-patches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201231125324.14779-3-lsix@lancelotsix.com \
--to=gdb-patches@sourceware.org \
--cc=lsix@lancelotsix.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox