我正在使用 Python 2.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
正在暂停我的线程......如果它处于空闲状态,是否有另一种方法可以杀死被调试的进程?