2
votes

I have an user defined struct.

struct foo {
    int bar1;
    char *name;
    user_type_t *tba;
}foo_t;

While in gdb, I have an address that points to type foo_t. e.g 0xfe83ba56 points to a structure in the memory that is of type foo_t.

I can convert that address to foo_t by type casting, and then I can dereference it if needed. (gdb) p (foo_t *)0xfe83ba56

How do to the same with python inside gdb? If I have a gdb.Value object which already points to an object of type foo_t, then I can its address. but here, I'm trying to convert a raw address into a gdb.Value object.

I looked into https://sourceware.org/gdb/onlinedocs/gdb/Values-From-Inferior.html#Values-From-Inferior and lot of posts from the 'similar questions' section of SO, but couldn't find the exact answer.

1
I don't know the answer to this myself, but it occurs to me that a custom type printer will probably need to do exactly this, so maybe you can figure it out by looking at the custom type printers shipped with e.g. libstdc++? gcc.gnu.org/git/?p=gcc.git;a=tree;f=libstdc%2B%2B-v3/python - zwol

1 Answers

4
votes

To cast, use the Value.cast method. Something like:

t = gdb.lookup_type('foo_t').pointer()
value = value.cast(t)