Ant*_* C. 8 python multithreading gdb gdb-python
我用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")
Run Code Online (Sandbox Code Playgroud)
但是,gdb.execute暂停我的线程......如果它是空闲的,还有另一种方法可以杀死已调试的进程吗?
但是,gdb.execute 正在暂停我的线程
这里发生的情况是,gdb.execute调用 gdb 时不会释放 Python 的全局锁。因此,当 gdb 命令执行时,其他 Python 线程会被卡住。
这只是 gdb 的一个疏忽。我已经为此提交了一个错误。
如果调试进程空闲,还有其他方法可以杀死它吗?
您还可以尝试另一种技术——我不确定它是否有效。不幸的是,gdb 的这一部分还没有完全充实(目前);所以也请随意提交错误报告。
主要思想是在主线程上运行 gdb 命令——但不是从 Python 运行。因此,尝试使用 gdb CLI 编写步进循环,可能如下所示:
(gdb) while 1
> stepi
> end
Run Code Online (Sandbox Code Playgroud)
那么你的线程应该能够kill劣势。另一种方法可能是让您的线程使用 .gdb 命令将 gdb 命令注入到主循环中gdb.post_event。
| 归档时间: |
|
| 查看次数: |
215 次 |
| 最近记录: |