In gdb while debugging a core file, how do I read a structure given the address using a python gdb extension?

26 views Asked by At

I am writing a python gdb extension. Given the address of a structure, I would like to read the core file and obtain a gdb.Value object out of it. How do I go about doing that?

1

There are 1 answers

0
Andrew On

I started with this test program:

struct foo
{
  int a;
  int b;
  int *c;
};

struct foo global_foo = { 0, 0, 0 };

int
main ()
{
  global_foo.a = *global_foo.c;
  global_foo.b = 1;

  return 0;
}

Compile (with debug info) the program, run it and it crashes, dumping core. Load the program and corefile into GDB, and then:

(gdb) p &global_foo
$1 = (struct foo *) 0x404030 <global_foo>
(gdb) python-interactive
>>> t = gdb.lookup_type("struct foo")
>>> t = t.pointer()
>>> print(t)
struct foo *
>>> v = gdb.Value(0x404030)
>>> v = v.cast(t)
>>> print(v)
0x404030 <global_foo>
>>> print(v.dereference())
{
  a = 0,
  b = 0,
  c = 0x0
}
>>> v = v.dereference()
>>> print ( v['a'] )
0
>>> print ( v['b'] )
0
>>> print ( v['c'] )
0x0
>>> quit

(gdb)