[PATCH 2/2] ptype: add option to use hexadecimal notation

Lancelot SIX lsix@lancelotsix.com
Wed Apr 14 22:33:41 GMT 2021


On Tue, Apr 13, 2021 at 04:18:24PM -0700, John Baldwin wrote:
> On 12/31/20 4:53 AM, Lancelot SIX via Gdb-patches wrote:
> > 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'
> 
> Thanks for working on this.  In general I think this looks fine in
> terms of new print flags, etc.  My only concern might be that a
> single byte worth of space might be a bit short.  I frequently
> use ptype with some larger data structures whose sizes in hex
> run to 3 digits and would distort the formatting as a result.
> 
> What would you think of using 6 character fields for hex values
> such as 0x%04x?  It would mean the entire display would shift left
> by 4 characters and the header would have to be adjusted.  Alternatively
> if you are really concerned about horizontal space you could perhaps
> only expand the offset portion to 4 hex digits?
> 
> The other possible consideration is to not print leading zeroes
> but instead use '%6s' with hex_string() for the hex fields.
> 
> -- 
> John Baldwin

Hi,

Thanks for the feedback.

For my initial take at this I kept the overall horizontal layout
unchanged, but it is true that the 0x prefix consumes quite some
space. I’ll increase the horizontal space and prepare a new version
(with the 0x%04x approach I think).

Lancelot.


More information about the Gdb-patches mailing list