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] Implement pahole-like 'ptype /o' option


>>>>> "Sergio" == Sergio Durigan Junior <sergiodj@redhat.com> writes:

Sergio> This commit implements the pahole-like '/o' option for 'ptype', which
Sergio> prints the offsets and sizes of struct fields, reporting whenever
Sergio> there is a hole found.

Thanks for doing this!

Sergio> The idea is to print offsets and sizes only for structs, so unions and
Sergio> other types are mostly ignored.

I tried out the patch, since I want to add support for this feature to
the Rust language code.  I have two comments.

First, I think it would be valuable to descend into embedded structures.
This would make it simpler to review layout of the entire outer struct.
That is, like this:

    struct t {
      int x;
      int y;
    };

    struct s {
      int x;
      char c;
      struct t t;
    } s;

The old pahole.py did do this, like

    (gdb) pahole struct s
                  struct s {
     /*   0   4 */  int x
     /*   4   1 */  char c
      /* XXX 24 bit hole, try to pack */
     /*   8   8 */  struct t {
     /*   0   4 */    int x
     /*   4   4 */    int y
                    } t
                  } 

... though the old code's output is kind of confusing, restarting the
offset at 0 in the embedded struct.

Second, and similarly, descending into unions does seem to make sense
sometimes (pahole.py didn't do this, but it seems like it should have).

Continuing the above example, consider:

    union u {
      struct s s;
      int i;
    };

Here ptype shows:

    (gdb) ptype/o union u
    type = union u {
                               struct s s;
                               int i;
    }

(The formatting looks weird without the layout info here...)

I think one specific case where it is interesting to see the union's
layout is when you want to know why a union field in a struct is as
large as it is.  That is, what is the largest member of the union, in
case you want to try to shrink it?

Tom


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