[PR gdb/22640] ptype: add option to use hexadecimal notation
authorLancelot SIX <lsix@lancelotsix.com>
Sat, 17 Apr 2021 11:10:23 +0000 (12:10 +0100)
committerLancelot SIX <lsix@lancelotsix.com>
Sun, 25 Apr 2021 17:00:54 +0000 (18:00 +0100)
commitfbb46296d7e0d09e792a7c9d7841cb26753cb3ce
tree57a8906aac5ac27c70664f749980f2d3b6c6f517
parent18bbba46a974d8b667f9b60c5a7d41bb3cb07369
[PR gdb/22640] ptype: add option to use hexadecimal notation

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 'd' 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 PR
gdb/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 {
    /* 0x0000: 0x0 |  0x0004 */    unsigned int raw : 1;
    /* 0x0000: 0x1 |  0x0004 */    unsigned int print_methods : 1;
    /* 0x0000: 0x2 |  0x0004 */    unsigned int print_typedefs : 1;
    /* 0x0000: 0x3 |  0x0004 */    unsigned int print_offsets : 1;
    /* 0x0000: 0x4 |  0x0004 */    unsigned int print_in_hex : 1;
    /* XXX  3-bit hole       */
    /* XXX  3-byte hole      */
    /* 0x0004      |  0x0004 */    int print_nested_type_limit;
    /* 0x0008      |  0x0008 */    typedef_hash_table *local_typedefs;
    /* 0x0010      |  0x0008 */    typedef_hash_table *global_typedefs;
    /* 0x0018      |  0x0008 */    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 on x86_64.

gdb/ChangeLog:

PR gdb/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 (type_print_raw_options, default_ptype_flags): Set
default value for print_in_hex.
(print_offset_data::indentation): Allow more horizontal space.
(print_offset_data::print_offset_data): Add ctor.
(print_offset_data::maybe_print_hole, print_offset_data::update):
Handle the print_in_hex flag.
(whatis_exp): Handle 'x' and 'd' flags.
(print_offsets_and_sizes_in_hex): Declare.
(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, c_type_print_base_struct_union)
(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.
* NEWS: Mention the new flags of the ptype command.

gdb/doc/ChangeLog:

PR gdb/22640
* gdb.texinfo (Symbols): Describe the 'x' and 'd' flags of the
ptype command, describe 'set print type hex' and 'show print
type hex' commands.  Update 'ptype/o' examples.

gdb/testsuite/ChangeLog:

PR gdb/22640
* gdb.base/ptype-offsets.exp: Add tests to verify the behavior
of 'ptype/ox' and 'ptype/od'. Check that 'set print type hex'
changes the default behavior of 'ptype/o'.  Update to take into
account new horizontal layout.
* gdb.rust/simple.exp: Update ptype test to check new horizontal
layout.
* gdb.rust/union.exp: Same.
12 files changed:
gdb/ChangeLog
gdb/NEWS
gdb/c-typeprint.c
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/rust-lang.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/ptype-offsets.exp
gdb/testsuite/gdb.rust/simple.exp
gdb/testsuite/gdb.rust/union.exp
gdb/typeprint.c
gdb/typeprint.h
This page took 0.037039 seconds and 4 git commands to generate.