2013-05-09 37 views
2

我的程序有100个线程,其中大部分都是空闲的,并且在闲置时共享一个非常好定义的回溯。大多数情况下,我只对不闲置的线程感兴趣,因此没有“普通”回溯。我认为使用gdb脚本将是一个很好的方法来做到这一点。使用gdb脚本修剪backtrace输出

define backtraces 
    thread apply all bt 
end 

该脚本将简单地打印所有的回溯。有没有办法将这个输出存储到一个变量中,然后我可以处理,修剪并仅显示相关的回溯?

我天真的尝试:

define backtraces 
    set $bts = thread apply all bt 
    // do whatever processing here 
end 

但失败与预期如下:

无符号 “线程” 在目前情况下。

有没有更好的方法来做到这一点?或者有关如何在gdb中为脚本启动的好教程?

回答

1

使用俄罗斯就业的答案中的链接我能够得到一些工作。这是后代,因为它完全不明显。

import gdb 

# This loops through all the Thread objects in the process 
for thread in gdb.selected_inferior().threads(): 

    # This is equivalent to 'thread X' 
    thread.switch()  

    print "Thread %s" % thread.num 

    # Just execute a raw gdb command 
    gdb.execute('bt') 

    framesNames = [] 
    f = gdb.newest_frame() 
    while f is not None: 
     framesNames.append(gdb.Frame.name(f)) 
     f = gdb.Frame.older(f) 

    # do something with the name of each frame 

如果这是在一个名为traces.py文件,然后从GDB你可以执行的Python:

source traces.py 

还有其他的方法来调用这个python脚本了。