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

测试:

相关推荐
LuckyLay34 分钟前
1.1.2 运算符与表达式——AI教你学Django
python·django
学不会就看35 分钟前
Django--01基本请求与响应流程
后端·python·django
nbsaas-boot6 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯6 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
2501_915918418 小时前
Fiddler中文版全面评测:功能亮点、使用场景与中文网资源整合指南
android·ios·小程序·https·uni-app·iphone·webview
不知名It水手10 小时前
uniapp运行项目到ios基座
ios·uni-app·cocoa
chao_78910 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴10 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
Digitally11 小时前
[5种方法] 如何将iPhone短信保存到电脑
ios·iphone