Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an address to a specific type in python gdb

Tags:

c++

python

c

gdb

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.

like image 546
DeeYen Avatar asked Oct 12 '25 22:10

DeeYen


1 Answers

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

t = gdb.lookup_type('foo_t').pointer()
value = value.cast(t)
like image 158
Tom Tromey Avatar answered Oct 14 '25 11:10

Tom Tromey