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

测试:

相关推荐
宇寒风暖16 分钟前
Flask 框架全面详解
笔记·后端·python·学习·flask·知识
哪 吒25 分钟前
【2025C卷】华为OD机试九日集训第3期 - 按算法分类,由易到难,提升编程能力和解题技巧
python·算法·华为od·华为od机试·2025c卷
0wioiw042 分钟前
Apple基础(Xcode②-Flutter结构解析)
flutter·macos·xcode
0wioiw044 分钟前
Apple基础(Xcode④-Flutter-Platform Channels)
flutter·macos·xcode
rockmelodies2 小时前
RSA 解密逻辑
开发语言·python
Tobiichiorigami.2 小时前
Python训练Day30
python
Dxy12393102163 小时前
python创建一个excel文件
开发语言·python·excel
CocoaKier5 小时前
推荐一个历代iPhone设备型号网站,比维基百科好用
ios·apple
涡能增压发动积5 小时前
Browser-Use Agent使用初体验
人工智能·后端·python
笑尘pyrotechnic6 小时前
自动布局视图来实现聊天室的界面
ui·ios·objective-c