簡體   English   中英

如何將“圖像查找 -v 地址”結果存儲在變量中?

[英]How can I store “image lookup -v address” result inside a variable?

我可以通過以下 lldb 命令來表示符號地址:

image lookup --address $SYMBOL_ADDRRESS

但是在編寫 shell 腳本進行解析時,我無法找到將上述命令的 output 存儲到變量或文件中的方法。

First off, if your script's job is mostly about driving lldb and you happen to know Python, you will be much happier using the lldb module in Python, where you can drive the debugger directly, than getting lldb to produce text output which you parse in shell 腳本。

The lldb Python module provides API's like SBTarget.ResolveSymbolContextForAddress , which runs the same lookup as image lookup --address but returns the result as a Python lldb.SBSymbolContext object, which you can either query for module/file/line etc using API's on the object。 因此,使用 lldd API 可以更輕松地從此結果中獲取一些信息。

但是,如果您必須使用 shell 腳本,那么最簡單的方法可能是將命令 output 寫入文件並將其讀回 shell 腳本。 lldb 還沒有對 tee 命令 output 到日志文件的通用支持,但是 lldb Python 模塊允許您運行命令行命令並以編程方式捕獲 Z78E6221F6393D13566668。

因此,您可以從 lldb 的 Python 腳本解釋器輕松完成:

(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
>>> result = lldb.SBCommandReturnObject()
>>> lldb.debugger.GetCommandInterpreter().HandleCommand("image lookup -va $pc", result)
2
>>> fh = open("/tmp/out.txt", "w")
>>> fh.write(result.GetOutput())
>>> fh.close()
>>> quit
(lldb) plat shell cat /tmp/out.txt
      Address: foo[0x0000000100003f6f] (foo.__TEXT.__text + 15)
      Summary: foo`main + 15 at foo.c:6:3
       Module: file = "/tmp/foo", arch = "x86_64"
  CompileUnit: id = {0x00000000}, file = "/tmp/foo.c", language = "c99"
     Function: id = {0x7fffffff00000032}, name = "main", range = [0x0000000100003f60-0x0000000100003f8a)
     FuncType: id = {0x7fffffff00000032}, byte-size = 0, decl = foo.c:4, compiler_type = "int (void)"
       Blocks: id = {0x7fffffff00000032}, range = [0x100003f60-0x100003f8a)
    LineEntry: [0x0000000100003f6f-0x0000000100003f82): /tmp/foo.c:6:3
       Symbol: id = {0x00000005}, range = [0x0000000100003f60-0x0000000100003f8a), name="main"

你也可以在 Python 中寫一個 lldb 命令來包裝這部分業務,這樣會更容易使用。 詳細信息在這里:

https://lldb.llvm.org/use/python-reference.html#create-a-new-lldb-command-using-a-python-function

您甚至可以采用混合方法,並使您想要執行自定義 Python 命令的所有 lldb 工作。 這將允許您使用 lldb Python API 來獲取您需要的信息並以您方便的任何格式將其寫出,並將簡化 shell 腳本中的 lldb 調用,並有助於恢復 lldb 提供的信息......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM