Nef Hal
Nef Hal

Reputation: 13

Find absolute offsets of nested structs using GDB-Python Types API

I am trying to explore datatypes' memory layout using GDB-Python Type API (gdb.types). Specifically, I would like to find a way to get the absolute Offsets of all members of a nested struct which is defined inside a parent struct.

In C, I have defined:

typedef struct
{
    int  a;
    short bf1:2;
    char b:4;

    struct   //nested struct
    {
        long x;
        long y;
    };
} a_struct;

Using ptype command in gdb, I get:

(gdb) ptype /o a_struct
/* offset    |  size */  type = struct a_struct {
/*    0      |     4 */    int a;
/*    4:14   |     2 */    short bf1 : 2;
/*    4: 2   |     1 */    char b : 4;
/* XXX  2-bit hole  */
/* XXX  3-byte hole */
/*    8      |    16 */    struct {
/*    8      |     8 */        long x;
/*   16      |     8 */        long y;

                               /* total size (bytes):   16 */
                           };

                           /* total size (bytes):   24 */
                         }

The above output shows the offsets of the fields of the nested anonymous struct as absolute values from the beginning of the parent struct, that is x is at byte 8 and y at byte 16.

I am trying to get the same results using the GDB Python Type API but without success. In particular, I am using gdb.types.deep_items(lookup_type) method which returns the relative offsets of the fields of the nested struct, that is 0 for the first field (x) and 8 for the second field (y).

Is there any way to get 8 for x and 16 for y (as ptype output shows) using the GDB Python API?

Thank you

Upvotes: 1

Views: 569

Answers (1)

AVH
AVH

Reputation: 11516

I can't check right now, but if I remember correctly, this plugins for gdb provides absolute offsets: https://blog.mozilla.org/sfink/2018/08/17/type-examination-in-gdb/. It produces output such as:

(gdb) pahole js::jit::ABIArg
  offset size
       0   16 : struct js::jit::ABIArg {
       0    4 :   kind_ : js::jit::ABIArg::Kind
       4    4 : --> 32 bit hole in js::jit::ABIArg <--
       8    8 :   u : struct union {...} {
   8  +0    1 :     gpr_ : js::jit::Register::Code
   8  +0    8 :     fpu_ : js::jit::FloatRegister::Code
   8  +0    4 :     offset_ : uint32_t
                  } union {...}
                } js::jit::ABIArg

Upvotes: 1

Related Questions