【问题标题】:how to use the lldb.frame.variables in Python script如何在 Python 脚本中使用 lldb.frame.variables
【发布时间】:2017-01-03 05:27:02
【问题描述】:

我想在 Python 中使用 LLDB,我编写了这样的 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']

断点设置在正确的位置,程序可以正常运行。 我想知道为什么以及如何在不先输入“脚本”的情况下得到我想要的结果

【问题讨论】:

    标签: python python-2.7 debugging lldb


    【解决方案1】:

    交互式脚本解释器中的 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 的 (在过去一两年内)将传递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__)
    

    老实说,在这一点上,我什至不会包含不再通过 SBExecutionContext 的 lldb 的代码,我可以期望每个人都在运行足够新的 lldb。

    【讨论】:

      猜你喜欢
      • 2014-07-09
      • 2020-03-29
      • 2017-06-16
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 2022-10-18
      相关资源
      最近更新 更多