pythonpython-2.7gdb

GDB not stopping with "interrupt" command from python script


I've been ripping my hair out over this. I've searched the internet and can't seem to find a solution to my problem. I'm trying to auto test some code using the gdb module from python. I can do basic command and things are working except for stopping a process that's running in the background. Currently I continue my program in the background after a break point with this:

gdb.execute("c&")

I then interact with the running program reading different constant values and getting responses from the program.
Next I need to get a chunk of memory so I run these commands:

gdb.execute("interrupt") #Pause execution gdb.execute("dump binary memory montiormem.bin 0x0 (&__etext + 4)") #dump memory to file

But when I run the memory dump I get an error saying the command can't be run when the target is running, after the error the interrupt command is run and the target is paused, then from the gdb console window I can run the memory dump.

I found a similar issue from awhile ago that seems to not be answered here.

I'm using python2.7.

I also found this link which seems to be the issue but no indication if it's in my build of gdb (which seems unlikely).


Solution

  • I had the same problem, but found that none of the other answers here really work if you are trying to script everything from python. The issue that I ran into was that when I called gdb.execute('continue'), no code in any other python thread would execute. This appears to be because gdb does not release the python GIL while the continue command is waiting for the program to be interrupted.

    What I found that actually worked for me was this:

    def delayed_interrupt():
        time.sleep(1)
        gdb.execute('interrupt')
    gdb.post_event(delayed_interrupt)
    gdb.execute('continue')