Thomas Højriis Knudsen Thomas Højriis Knudsen - 3 months ago 16
C Question

Set the depth of expanded structures with ptype in GDB

Consider the following code in C:

typedef struct {
union {
struct {
int x:16;
int y:16;
};
int combi_xy;
};
} T_MY_STRUCT;


Now with GDB I want to look at the setup of this struct. I do so with ptype:

(gdb) ptype T_MY_STRUCT
type = struct {
union {
struct {...};
int combi_xy;
};
}


But that result is not very satisfying for me, since all the content of the inner struct (x and y) has been hidden/replaced with {...}. Is it in anyway possible to see the contents of the inner struct? Is there some option to set the nesting depth for ptype? Or even an option to just expand the whole structure, no matter the the amount of nested structures?

As a note I would be good if I could just expand a nesting level equal to the one in the example.

Answer

There does not appear to be any functionality in gdb to control the nesting level that is printed. The code is in /binutils-gdb/gdb/c-typeprint.c, it is quite hard to follow however.

The best I could do was to print an actual instance.

ptype can also be used on an instance. If the nested struct is named then ptype can be used to drill down.

typedef struct {
    union {
        struct {
            int  x:16;
            int  y:16;
        }inner;
        int combi_xy;
    };
} T_MY_STRUCT;

void main()
{
  T_MY_STRUCT a;

  a.combi_xy = 5;
}

gdb has the following to say on the above

(gdb) p a
$1 = {{inner = {x = -8208, y = -1}, combi_xy = -8208}}
(gdb) ptype a
type = struct {
    union {
        struct {...} inner;
        int combi_xy;
    };
}
(gdb) ptype a.inner
type = struct {
    int x : 16;
    int y : 16;
}