我正在使用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
会暂停我的线程... 如果被调试的进程处于空闲状态,是否有其他方法可以终止它?
gdb.post_event
,因为我需要按顺序将gdb.execute
命令的输出检索到一个字符串中。 - Antoine C.