iOS调试技巧——使用Python 自定义LLDB

一、类介绍

在使用Python 自定义LLDB之前,先了解一下LLDB的一些类型

  • SBTarget 正在被调试的程序
  • SBProcess 和程序关联的具体的进程
  • SBThread 执行的线程
  • SBFrame 和线程关联的一个栈帧
  • SBVariable 变量,寄存器或是一个表达式

一般情况下,我们取到SBFrame就可以进行方法调用来打印关键信息

二、断点调试示例

在写Python前,先使用Xcode断点执行一下

自定义类MyClass

.h文件

复制代码
@interface MyClass : NSObject

+ (NSString *)lldbTest;

@end

.m文件

复制代码
@implementation MyClass

+ (NSString *)lldbTest {
    return @"lldb test successed";
}

@end

中断程序

打开lldb控制台

下面就开始写lldb的命令
预期目标,打印出[MyClass lldbTest]的返回值

输入script

复制代码
(lldb) script
Python Interactive Interpreter. To exit, type 'quit()', 'exit()'.
>>> 

定义变量test接收MyClass lldbTest]的返回值

复制代码
>>> test = lldb.frame.EvaluateExpression('(NSString *)[MyClass lldbTest]').GetObjectDescription()

打印变量test

复制代码
>>> print(test)

至此,直接在Xcode中使用lldb打印出[MyClass lldbTest]的返回值就完成了

三、编写Python

如果想把这个功能打包起来,使用一句命令调用,就需要使用Python来扩展我们的lldb命令

1、新建Python文件

这里将Python文件命名问lldbtest.py

1、引入lldb头文件
复制代码
import lldb
2、初始化函数
复制代码
def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand()

在HandleCommand中添加命令:

复制代码
'command script add lldb_test -f lldbtest.test'

lldb_test表示命令名称,lldbtest是Python文件名,test是自定义方法名

初始化函数最终

复制代码
def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add lldb_test -f lldbtest.test')
3、自定义Python方法

获取当前的frame栈帧

复制代码
  target = debugger.GetSelectedTarget()
  process = target.GetProcess()
  thread = process.GetSelectedThread()
  currentFrame = thread.GetSelectedFrame()

调用方法

复制代码
def test(debugger, command, result, internal_dict):
  target = debugger.GetSelectedTarget()
  process = target.GetProcess()
  thread = process.GetSelectedThread()
  currentFrame = thread.GetSelectedFrame()
  test = currentFrame.EvaluateExpression('(NSString *)[Person lldbTest]').GetObjectDescription()
  print("result:%s" % test)

整个Python文件

复制代码
#自定义lldb命令 
import lldb

def test(debugger, command, result, internal_dict):
  target = debugger.GetSelectedTarget()
  process = target.GetProcess()
  thread = process.GetSelectedThread()
  currentFrame = thread.GetSelectedFrame()
  test = currentFrame.EvaluateExpression('(NSString *)[Person lldbTest]').GetObjectDescription()
  print("result:%s" % test)

def __lldb_init_module(debugger, internal_dict):
    debugger.HandleCommand('command script add lldb_test -f lldbtest.test')

四、自动加载python脚本

原理:xcode启动的时候会读取一个默认文件:~/.lldbinit

只需要将命令command script import /Users/xx/Desktop/lldbtest.py 写入这个文件即可。

/Users/xx/Desktop/lldbtest.py是Python文件路径

测试:

相关推荐
noravinsc25 分钟前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
ajassi200036 分钟前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
沉默媛1 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
Deng9452013142 小时前
基于Python的旅游数据可视化应用
python·numpy·pandas·旅游·数据可视化技术
2401_878624792 小时前
pytorch 自动微分
人工智能·pytorch·python·机器学习
胖达不服输2 小时前
「日拱一码」021 机器学习——特征工程
人工智能·python·机器学习·特征工程
杂雾无尘3 小时前
Swift 5.9 新特性揭秘:非复制类型的安全与高效
ios·swift·apple
screenCui3 小时前
macOS运行python程序遇libiomp5.dylib库冲突错误解决方案
开发语言·python·macos
小眼睛羊羊3 小时前
pyinstaller打包paddleocr
python
java1234_小锋4 小时前
基于Python的旅游推荐协同过滤算法系统(去哪儿网数据分析及可视化(Django+echarts))
python·数据分析·旅游