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文件路径

测试:

相关推荐
站大爷IP26 分钟前
Pandas时间数据处理:从基础到进阶的实战指南
python
智能砖头1 小时前
本地文档AI助手:基于LangChain和Qwen2.5的智能问答系统
人工智能·python
郭枫寅2 小时前
第二课 Python 注释与规范格式
python
郭枫寅2 小时前
第三课 Python中的简单数据类型
python
郭枫寅2 小时前
第四课 Python基础语法(一)
python
JavaEdge在掘金3 小时前
解决 Maven os.detected.classifier 报错:快速修复指南
python
Hierifer3 小时前
跨端实现 DSBridge 源码解析
前端·ios
CocoaKier5 小时前
你的开发者账号已经被盯上了,如何区分钓鱼邮件
ios·google·apple
HEY_FLYINGPIG5 小时前
【FLASK】FLASK应用中的多用户并行登录与加密
后端·python·flask
fydw_7155 小时前
Flask-login 处理授权逻辑
后端·python·flask