2017-01-03 72 views
0

我想用LLDB使用Python和我寫我的Python腳本是這樣的:如何使用lldb.frame.variables在Python腳本

import lldb 
import commands 
import optparse 
import shlex 

def __lldb_init_module(debugger, internal_dict): 
    list1=[] 
    for i in lldb.frame.variables: 
     list1.append(str(i.name)) 
    print list1 

我想現在就打印在框架中的變量。當我將其導入在LLDB,

(LLDB)命令腳本導入〜/ str.py

結果是空的。

但是,如果我先輸入「腳本」並退出,Python腳本會根據需要輸出正確的結果。

(LLDB)腳本

Python解釋。要退出,請輸入'quit()','exit()'或Ctrl-D。

退出

(LLDB)命令腳本進口〜/ str.py

[ 'A', 'B', 'C', 'd']

斷點被設置在正確的地方,程序可以運行沒有任何錯誤。 我想知道爲什麼,以及如何得到的結果我想要的東西,而不輸入查詢「腳本」第一

回答

0

lldb.framelldb.threadlldb.processlldb.target快捷鍵在交互式腳本解釋器不獨立Python命令存在 - 存在在給定的時間可能比這些對象中的任何一個都多,我們希望腳本具體說明它正在使用哪一個。

有相同的做法是通過SB API讓我當前選擇的東西。例如

debugger.GetSelectedTarget() 
debugger.GetSelectedTarget().GetProcess() 
debugger.GetSelectedTarget().GetProcess().GetThread() 
debugger.GetSelectedTarget().GetProcess().GetThread().GetSelectedFrame() 

你在上面的例子中做的工作在你的init方法(以便你的Python只能當有一個正在運行的進程,對不對?裝),但如果你在Python中定義一個新的命令LLDB ,新的lldb's(在過去一年或兩年內)將通過一個SBExecutionContext,它會給你當前選擇的一切。例如

def disthis(debugger, command, *args): 
    """Usage: disthis 
Disables the breakpoint the currently selected thread is stopped at.""" 

    target = lldb.SBQueue()  # some random object that will be invalid 
    thread = lldb.SBQueue()  # some random object that will be invalid 

    if len(args) == 2: 
     # Old lldb invocation style 
     result = args[0] 
     if debugger and debugger.GetSelectedTarget() and debugger.GetSelectedTarget().GetProcess(): 
      target = debugger.GetSelectedTarget() 
      process = target.GetProcess() 
      thread = process.GetSelectedThread() 
    elif len(args) == 3: 
     # New (2015 & later) lldb invocation style where we're given the execution context 
     exe_ctx = args[0] 
     result = args[1] 
     target = exe_ctx.GetTarget() 
     thread = exe_ctx.GetThread() 

    if thread.IsValid() != True: 
     print >>result, "error: process is not paused." 
     result.SetStatus (lldb.eReturnStatusFailed) 
     return 

[...]

def __lldb_init_module (debugger, dict): 
    debugger.HandleCommand('command script add -f %s.disthis disthis' % __name__) 

說實話,在這一點上我還不包括爲LLDB未通過的SBExecutionContext任何更多的代碼,我可以期待每個人都要運行足夠新的lldb's。