本文主要介绍下源码探索的三种方法
- 1、
符号断点
直接跟流程 - 2、通过按住
control
+step into
- 3、
汇编
跟流程
下面详细讲下这三种方法是如何查找到函数所在的源码库,以alloc
为例
一、符号断点
直接跟流程
- 通过下
alloc
的符号断点
1.选择断点Symbolic Breakpoint

2.符号断点中输入 alloc

3.ViewController
中的LGPerson
处 加一个断点
在走到这部分断点之前,需要关闭上面新增的符号断点,原因是因为alloc的调用有很多,如果开启了就不能准确的定位到LGPerson
的alloc
方法

以下为符号断点的关闭状态

- 运行程序, 断在
LGPerson
部分,打开alloc
符号断点 ,断点状态为下图
,然后继续执行


以下为alloc符号断点断住的堆栈调用情况,从下图可以看出 alloc 的源码位于libobjc.A.dylib
库(需要去Apple
相应的开源网址下载 objc
源码进行更深入的探索)

二、通过按住control
+ step into
1.ViewController
中的LGPerson
处 加一个断点

2.按住 control
键,选择 step into
⬇️键

进去后,显示为以下内容

3.再下一个objc_alloc
符号断点,符号断点后显示了 objc_alloc
所在的源码库
(需要去Apple
相应的开源网址下载 objc
源码进行更深入的探索)

三、汇编
跟流程
1.ViewController
中的LGPerson
处 加一个断点

2.xcode 工具栏 选择 Debug
-->Debug Workflow
--> Always Show Disassembly

,这个 选项表示 始终显示反汇编 ,即 通过汇编 跟流程
3.按住control
,点击 step into
⬇️键,执行到下图的对应 objc_alloc

4.按住control
,点击 step into
⬇️键进入,看到断点断在objc_alloc
部分

5.同样通过objc_alloc
的符号断点,得知源码所在库
(需要去Apple 相应的开源网址下载 objc源码进行更深入的探索)

注意(以下是Apple 提供的源码下载地址):
1、Apple 所有开源源码汇总地址,根据相应的版本查找对应的源码,以mac 10.15为例:macOS --> 10.15 --> 选择10.15 --> 搜索 objc
2、Apple 比较直接的源码下载地址,直接搜索想要下载的源码名称即可,例如objc
:直接搜索 objc --> objc4/ --> 选择相应的objc的版本
网友评论