gdb.执行阻止python脚本中的所有线程

2024-04-26 14:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我用python2.7编写GDB脚本。在

我只是用gdb.execute("stepi")逐级执行指令。如果调试的程序正在空闲并等待用户交互,gdb.execute("stepi")不会返回。如果出现这种情况,我希望在不终止gdb的情况下停止调试会话。在

为此,我创建一个线程,如果当前指令运行超过x秒,它将终止已调试的进程:

from ctypes import c_ulonglong, c_bool
from os import kill
from threading import Thread
from time import sleep
import signal

# We need mutable primitives in order to update them in the thread
it = c_ulonglong(0) # Instructions counter
program_exited = c_bool(False)
t = Thread(target=check_for_idle, args=(pid,it,program_exited))
t.start()

while not program_exited.value:
    gdb.execute("si") # Step instruction
    it.value += 1

# Threaded function that will kill the loaded program if it's idling
def check_for_idle(pid, it, program_exited):
    delta_max = 0.1 # Max delay between 2 instructions, seconds
    while not program_exited.value:
        it_prev = c_ulonglong(it.value) # Previous value of instructions counter
        sleep(delta_max)
        # If previous instruction lasted for more than 'delta_max', kill debugged process
        if (it_prev.value == it.value):
            # Process pid has been retrieved before
            kill(pid, signal.SIGTERM)       
            program_exited.value = True
    print("idle_process_end")

但是,gdb.execute正在暂停我的线程。。。如果已调试的进程处于空闲状态,是否还有其他方法可以终止它?在


Tags: fromimportforexecutevalueitprogrampid
1条回答
网友
1楼 · 发布于 2024-04-26 14:28:20

However, gdb.execute is pausing my thread

这里发生的是,gdb.execute在调用gdb时没有释放Python的全局锁。因此,当gdb命令执行时,其他Python线程会被卡住。在

这只是gdb的一个疏忽。我已经申请了a bug。在

Is there another way to kill the debugged process if it is idling?

还有一种方法你可以试试,我不确定它是否有效。不幸的是,gdb的这一部分还没有完全充实(目前);所以也可以随意提交bug报告。在

主要思想是在主线程上运行gdb命令,而不是从Python运行gdb命令。因此,请尝试使用gdb CLI编写步进循环,例如:

(gdb) while 1
> stepi
> end

那么你的线程应该能够kill较低的。另一种方法是让线程使用gdb.post_event将gdb命令注入主循环。在

相关问题 更多 >