How do I access the registers with python in gdb

I don't believe the Python API to GDB offers direct access to the registers, but depending on what you want to do with it you can access it either by evaluating the gdb command with gdb.execute(), or evaluate the "$eax" expression with gdb.parse_and_eval():

(gdb) p $rbx
$23 = 140737488348072
(gdb) python print type(gdb.parse_and_eval("$rbx")), gdb.parse_and_eval("$rbx")
<type 'gdb.Value'> 140737488348072

(This example is at the gdb prompt, but the gdb module isn't any different in other code executed in GDB.)


Recent gdb versions (like Debian 7.12-6) have a read_register method in the gdb.Frame class.

(gdb) info register rip
rip            0x7f68656c142d       0x7f68656c142d <__lll_lock_wait+29>
(gdb) python print(gdb.selected_frame().read_register('rip'))
0x7f68656c142d <__lll_lock_wait+29>
(gdb) 

That class has no corresponding method to modify a register value. It makes sense for that method to belong to that class because register values differ across stack frames, in the sense that gdb shows saved register values in outer frames, such as the ones returned by the older method, callers of the inner frames.

Tags:

Python

Gdb