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

测试:

相关推荐
冷雨夜中漫步6 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴7 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再7 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
2501_916008898 小时前
全面介绍Fiddler、Wireshark、HttpWatch、SmartSniff和firebug抓包工具功能与使用
android·ios·小程序·https·uni-app·iphone·webview
喵手8 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934739 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy9 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威10 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ10 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha10 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全