简体   繁体   English

GDB 没有停止 python 脚本中的“中断”命令

[英]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.我正在尝试使用 python 中的 gdb 模块自动测试一些代码。 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.但是当我运行内存转储时,我收到一个错误,说在目标运行时无法运行命令,错误后运行中断命令并且目标暂停,然后从 gdb 控制台窗口我可以运行内存倾倒。

I found a similar issue from awhile ago that seems to not be answered here .我不久前发现了一个类似的问题, 这里似乎没有回答。

I'm using python2.7.我正在使用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).我还发现这个链接似乎是问题,但没有迹象表明它是否在我的 gdb 构建中(这似乎不太可能)。

I just ran into this same issue while writing some automated testing scripts.我在编写一些自动化测试脚本时遇到了同样的问题。 What I've noticed is that the 'interrupt' command doesn't stop the application until after the current script has exited.我注意到的是,“中断”命令直到当前脚本退出后才会停止应用程序。

Unfortunately, this means that you would need to segment your scripts anytime you are causing an interrupt.不幸的是,这意味着您需要在导致中断的任何时候对脚本进行分段。

Script 1:脚本 1:

gdb.execute('c&')
gdb.execute('interrupt')

Script 2:脚本2:

gdb.execute("dump binary memory montiormem.bin 0x0 (&__etext + 4)")

I used multi threading to get arround this issue:我使用多线程来解决这个问题:

def post(cmd):
    def _callable():
        print("exec " + cmd , flush=True)
        gdb.execute(cmd)

    print("schedule " + cmd , flush=True)
    gdb.post_event(_callable)

class ScriptThread (threading.Thread):
    def run (self):

        while True:
            post("echo hello\n")
            time.sleep(1)

x = ScriptThread()
x.start()

Save this as "test_script.py"将其另存为“test_script.py”

Use the script as follows:使用脚本如下:

gdb
> source test_script.py

Note: that you can also pipe "source test_script.py", but you need to keep the pipe open.注意:您也可以使用管道“source test_script.py”,但您需要保持管道打开。

Once the thread is started GDB will wait for the thread to end and will process any commands you send to it via the "post_event" function.一旦线程启动,GDB 将等待线程结束并处理您通过“post_event”函数发送给它的任何命令。 Even "interrupt"!甚至“中断”!

I had the same problem, from what I can tell from googling it is a current limitation of gdb: interrupt simply doesn't work in batch mode (when specifying commands with --ex , or -x file , or on stdin, or sourcing from file), it runs the following commands before actually stopping the execution (inserting a delay doesn't help).我遇到了同样的问题,从谷歌搜索可以看出这是 gdb 的当前限制: interrupt在批处理模式下根本不起作用(使用--ex-x file指定命令时,或在标准输入上,或采购从文件),它在实际停止执行之前运行以下命令(插入延迟没有帮助)。 Building on the @dwjbosman's solution, here's a compact version suitable for feeding to gdb with --ex arguments for example:基于@dwjbosman 的解决方案,这里有一个紧凑的版本,适合使用--ex参数提供给 gdb,例如:

python import threading, gdb
python threading.Timer(1.0, lambda: gdb.post_event(lambda: gdb.execute("interrupt"))).start()
cont
thread apply all bt full # or whatever you wanted to do

It schedules an interrupt after 1 second and resumes the program, then you can do whatever you wanted to do after the pause right in the main script.它在 1 秒后安排中断并恢复程序,然后您可以在主脚本中暂停后做任何您想做的事情。

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.我遇到了同样的问题,但是发现如果您尝试从 python 编写所有内容,那么这里的其他答案都没有真正起作用。 The issue that I ran into was that when I called gdb.execute('continue') , no code in any other python thread would execute.我遇到的问题是,当我调用gdb.execute('continue')时,任何其他 python 线程中的代码都不会执行。 This appears to be because gdb does not release the python GIL while the continue command is waiting for the program to be interrupted.这似乎是因为gdb 在 continue 命令正在等待程序被中断时没有释放 python GIL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM