This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v4 2/2] Implement pahole-like 'ptype /o' option


On Monday, December 11 2017, I wrote:

> On Monday, December 11 2017, I wrote:
>
>> This commit implements the pahole-like '/o' option for 'ptype', which
>> prints the offsets and sizes of struct fields, reporting whenever
>> there is a hole found.
>>
>> The output is heavily based on pahole(1), with a few modifications
>> here and there to adjust it to our reality.  Here's an example:
>>
>>   (gdb) ptype /o stap_probe
>>   /* offset    |  size */
>>   struct stap_probe {
>>   /*    0      |    40 */    struct probe {
>>   /*    0      |     8 */        const probe_ops *pops;
>>   /*    8      |     8 */        gdbarch *arch;
>>   /*   16      |     8 */        const char *name;
>>   /*   24      |     8 */        const char *provider;
>>   /*   32      |     8 */        CORE_ADDR address;
>> 			     } /* total size:   40 bytes */ p;
>>   /*   40      |     8 */    CORE_ADDR sem_addr;
>>   /*   48:31   |     4 */    unsigned int args_parsed : 1;
>>   /* XXX  7-bit hole   */
>>   /* XXX  7-byte hole  */
>>   /*   56      |     8 */    union {
>>   /*                 8 */        const char *text;
>>   /*                 8 */        VEC_stap_probe_arg_s *vec;
>> 			     } /* total size:    8 bytes */ args_u;
>>   } /* total size:   64 bytes */
>>
>> A big part of this patch handles the formatting logic of 'ptype',
>> which is a bit messy.  I tried to be not very invasive, but I had to
>> do some cleanups here and there to make life easier.
>>
>> This patch is the start of a long-term work I'll do to flush the local
>> patches we carry for Fedora GDB.  In this specific case, I'm aiming at
>> upstreaming the feature implemented by the 'pahole.py' script that is
>> shipped with Fedora GDB:
>>
>>   <https://src.fedoraproject.org/rpms/gdb/blob/master/f/gdb-archer.patch#_311>
>>
>> This has been regression-tested on the BuildBot.  There's a new
>> testcase for it, along with an update to the documentation.  I also
>> thought it was worth mentioning this feature in the NEWS file.
>
> The patch below applies on top of this one and extends the output to
> include offsets of union fields.
>
> -- 
> Sergio
> GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
> Please send encrypted e-mail if possible
> http://sergiodj.net/
>
> diff --git a/gdb/c-typeprint.c b/gdb/c-typeprint.c
> index 23138d8a40..e66129f643 100644
> --- a/gdb/c-typeprint.c
> +++ b/gdb/c-typeprint.c
> @@ -906,11 +906,15 @@ output_access_specifier (struct ui_file *stream,
>  
>  static void
>  c_print_type_union_field_offset (struct type *type, unsigned int field_idx,
> -				 struct ui_file *stream)
> +				 struct ui_file *stream,
> +				 unsigned int offset_bitpos)
>  {
>    struct type *ftype = check_typedef (TYPE_FIELD_TYPE (type, field_idx));
> +  unsigned int bitpos = TYPE_FIELD_BITPOS (type, field_idx);
>  
> -  fprintf_filtered (stream, "/*              %4u */", TYPE_LENGTH (ftype));
> +  fprintf_filtered (stream, "/* %4u      |  %4u */",
> +		    (bitpos + offset_bitpos) / TARGET_CHAR_BIT,
> +		    TYPE_LENGTH (ftype));
>  }

The comment of the function above needs an update:

/* Print information about field at index FIELD_IDX of the union type
   TYPE.  Since union fields don't have the concept of offsets, we
   just print their sizes.  OFFSET_BITPOS is the offset value we carry
   over when we are printing a union that is inside a struct; this is
   useful so that the offset is constantly incremented (if we didn't
   carry it over, the offset would be reset to zero when printing the
   inner union).

   The output is strongly based on pahole(1).  */

-- 
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]