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

时间:2018-08-30 13:17:45

标签: 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")

但是,gdb.execute正在暂停我的线程...如果有空闲的调试进程,还有另一种方法吗?

1 个答案:

答案 0 :(得分:3)

  

但是,gdb.execute暂停了我的线程

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

这只是gdb中的一个疏忽。我已经为此提交了a bug

  

如果它处于空闲状态,是否还有另一种方法可以杀死已调试的进程?

您还可以尝试另一种技术-我不确定它是否可以工作。不幸的是,这部分gdb尚未完全充实(目前);因此也可以随时提交错误报告。

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

(gdb) while 1
> stepi
> end

然后,您的线程应该能够kill处于劣势。另一种方法是让您的线程使用gdb.post_event将gdb命令注入到主循环中。