Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing commands at breakpoint in gdb via python interface

Tags:

python

gdb

Not a duplicate of this question, as I'm working through the python interface to gdb.
This one is similar but does not have an answer.

I'm extending a gdb.breakpoint in python so that it writes certain registers to file, and then jumps to an address: at 0x4021ee, I want to write stuff to file, then jump to 0x4021f3

However, nothing in command is ever getting executed.

import gdb
class DebugPrintingBreakpoint(gdb.Breakpoint):
    def __init__(self, spec, command):
        super(DebugPrintingBreakpoint, self).__init__(spec, gdb.BP_BREAKPOINT, internal = False)
        self.command = command

    def stop(self):
        with open('tracer', 'a') as f:
            f.write(chr(gdb.parse_and_eval("$rbx") ^ 0x71))
            f.close()
        return False



gdb.execute("start")
DebugPrintingBreakpoint("*0x4021ee", "jump *0x4021f3")
gdb.execute("continue")

If I explicitly add gdb.execute(self.command) to the end of stop(), I get Python Exception <class 'gdb.error'> Cannot execute this command while the selected thread is running.:

Anyone have a working example of command lists with breakpoints in python gdb?

like image 297
robertkin Avatar asked Apr 25 '26 14:04

robertkin


1 Answers

A couple options to try:

  1. Use gdb.post_event from stop() to run the desired command later. I believe you'll need to return True from your function then call continue from your event.
  2. Create a normal breakpoint and listen to events.stop to check if your breakpoint was hit.
like image 112
Matt Avatar answered Apr 28 '26 04:04

Matt