Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the registers with python in gdb

Tags:

python

gdb

How can I access the cpu registers of in the current debugged instance? From gdb you can call for example printf "0x%x", $eax and set $eax_b = $eax is there also a way to do this via the python support gdb gives? Or should I create a python function which can be call like save-reg "eax" $eax which on his hand stores the registers in an array where I want them to be stored?

On the other hand, with gdb script you can also set $eax = 1000 for example, this I would also like to do from within a python script, instead of a gdb script.

like image 367
DipSwitch Avatar asked May 23 '11 22:05

DipSwitch


2 Answers

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.)

like image 193
Thomas Wouters Avatar answered Nov 19 '22 18:11

Thomas Wouters


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.

like image 29
Eirik Fuller Avatar answered Nov 19 '22 17:11

Eirik Fuller