2013-05-09 100 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腳本了。