Is there a way by which I can get the address of a global symbol in my binary if I know its name with the GDB Python API ?
Is python print(gdb.parse_and_eval('symbol').address)
the correct method t o obtain this value ?
Is there a way by which I can get the address of a global symbol in my binary if I know its name with the GDB Python API ?
Is python print(gdb.parse_and_eval('symbol').address)
the correct method t o obtain this value ?
Well, you answered yourself already correctly. Easy enough to verify:
(gdb) p &t
$2 = (time_t *) 0x562076476018 <t>
(gdb) python print(gdb.parse_and_eval('t').address)
0x562076476018 <t>
(gdb)
The accepted answer is not entirely correct. It only works when there is no local symbol with the same name in the current scope.
Take this example:
int foo = 0;
int main(int argc, char **argv) {
int foo = 0;
return 0;
}
If you break at main()
, gdb.parse_and_eval("foo")
will find the local variable foo
instead of the global symbol.
You can get the global symbol with gdb.lookup_global_symbol("foo")
.
import gdb
gdb.execute("break main")
gdb.execute("run")
print("Global symbol:")
print(gdb.lookup_global_symbol("foo").value().address)
print("Local symbol:")
print(gdb.parse_and_eval("foo").address)
The result:
Breakpoint 1 at 0x1124: file symbol-tests.c, line 6.
Breakpoint 1, main (argc=1, argv=0x7fffffffdb68) at symbol-tests.c:6
6 int foo = 0;
Global symbol:
0x55555555802c <foo>
Local symbol:
0x7fffffffda6c
© 2022 - 2024 — McMap. All rights reserved.
gdb
internals. – Demob