1

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the answer, but I have built the whole infrastructure using the GDB Python API, so I would like something in Python.
The plugin is written in Python, here's a direct link to the source: raw.githubusercontent.com/hotsphink/sfink-tools/master/conf/….
@NefHal If this answers your question, please upvote and accept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.