├── Images └── banner.jpeg ├── LICENSE └── README.md /Images/banner.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iOS-APM/iOS-APM-Secrets/a79f9abf7070deab2ed58f6264018ea3811842ee/Images/banner.jpeg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Reveal 4 | 5 |

6 | 7 | # 揭秘 APM iOS SDK 的核心技术 8 | 9 | ## 前言 10 | 11 | 有关 APM 的技术文章非常多,但大部分文章都只是浅尝辄止,并未对实现细节进行深挖。本文旨在通过剖析 SDK 具体实现细节,揭露知名 APM 厂商的 iOS SDK 背后的秘密。分析的 APM SDK 有**听云**, **OneAPM** 和 **Firebase Performance Monitoring** 等。 12 | 13 | ## 页面渲染时间 14 | 15 | 页面渲染的监控,比较容易想到的是通过 hook 页面的几个关键的生命周期方法,例如 `viewDidLoad`、`viewDidAppear:` 等,从而计算出页面渲染时间,最终发现慢加载的页面。然而如果真正通过上述思路去着手实现的时候,便会遇到难题。在 APM SDK 中如何才能 hook 应用所有页面的生命周期的方法呢?如果尝试 hook `UIViewController` 的方法又会如何呢?hook `UIViewController` 的方法明显不可行,原因是他只会作用 `UIViewController` 的方法,而应用中大部分的视图控制器都是继承自 `UIViewController` 的,所以这种方法不可行。但是听云 SDK 却能够实现。页面 Hook 的逻辑主要是 `_priv_NBSUIAgent` 类中实现的,下面是 `_priv_NBSUIAgent` 类的定义,其中 `hook_viewDidLoad` 等几个方法便是线索。 16 | 17 | ``` 18 | ; @class _priv_NBSUIAgent : NSObject { 19 | ; +hookUIImage 20 | ; +hookNSManagedObjectContext 21 | ; +hookNSJSONSerialization 22 | ; +hookNSData 23 | ; +hookNSArray 24 | ; +hookNSDictionary 25 | ; +hook_viewDidLoad: 26 | ; +hook_viewWillAppear: 27 | ; +hook_viewDidAppear: 28 | ; +hook_viewWillLayoutSubviews: 29 | ; +hook_viewDidLayoutSubviews: 30 | ; +nbs_jump_initialize: 31 | ; +hookSubOfController 32 | ; +hookFMDB 33 | ; +start 34 | ; } 35 | ``` 36 | 37 | 我们先将目光转到另外一个更可疑的方法:`hookSubOfController`,具体实现如下: 38 | 39 | ``` 40 | void +[_priv_NBSUIAgent hookSubOfController](void * self, void * _cmd) { 41 | r14 = self; 42 | r12 = [_subMetaClassNamesInMainBundle_c("UIViewController") retain]; 43 | var_C0 = r12; 44 | if ((r12 != 0x0) && ([r12 count] != 0x0)) { 45 | var_C8 = object_getClass(r14); 46 | if ([r12 count] != 0x0) { 47 | r15 = @selector(nbs_jump_initialize:); 48 | rdx = 0x0; 49 | do { 50 | var_98 = rdx; 51 | r12 = [[r12 objectAtIndexedSubscript:rdx, rcx, r8] retain]; 52 | [r12 release]; 53 | if ([r12 respondsToSelector:r15, rcx, r8] == 0x0) { 54 | _hookClass_CopyAMetaMethod(); 55 | } 56 | r13 = class_getName(r12); 57 | rax = [NSString stringWithFormat:@"nbs_%s_initialize", r13]; 58 | rax = [rax retain]; 59 | var_A0 = rax; 60 | rax = NSSelectorFromString(rax); 61 | var_B0 = rax; 62 | rax = objc_retainBlock(__NSConcreteStackBlock); 63 | var_A8 = rax; 64 | r15 = objc_retainBlock(rax); 65 | var_B8 = imp_implementationWithBlock(r15); 66 | [r15 release]; 67 | rax = class_getSuperclass(r12); 68 | r15 = objc_retainBlock(__NSConcreteStackBlock); 69 | rbx = objc_retainBlock(r15); 70 | r13 = imp_implementationWithBlock(rbx); 71 | [rbx release]; 72 | rcx = r13; 73 | r8 = var_B8; 74 | _nbs_Swizzle_orReplaceWithIMPs(r12, @selector(initialize), var_B0, rcx, r8); 75 | rdi = r15; 76 | r15 = @selector(nbs_jump_initialize:); 77 | [rdi release]; 78 | [var_A8 release]; 79 | [var_A0 release]; 80 | rax = [var_C0 count]; 81 | r12 = var_C0; 82 | rdx = var_98 + 0x1; 83 | } while (var_98 + 0x1 < rax); 84 | } 85 | } 86 | [r12 release]; 87 | return; 88 | } 89 | ``` 90 | 91 | 从 `_subMetaClassNamesInMainBundle_c` 的命名和传入的 "UIViewController" 参数,基本可以推断这个 C 函数是获取 MainBundle 中所有 `UIViewController` 的子类。而事实上,如果通过 LLDB 在这个函数 Call 完之后的那行汇编代码下断点,会发现返回的确实是 `UIViewController` 子类的数组。下面的 `if` 语句判断 `r12` 寄存器不为 `nil` 并且 `r12` 寄存器的 `count` 不等于0才执行 `if` 里面的逻辑,而 `r12` 寄存器存放的正是 `_subMetaClassNamesInMainBundle_c` 函数的返回值,也就是 `UIViewController` 子类的数组。 92 | 93 | 下面再来重点看看里面的 `do-while` 循环语句,循环判断的语句为 `var_98 + 0x1 < rax`,`var_98` 在循环开始的位置赋值 `rdx` 寄存器,`rdx` 寄存器在循环外初始化为0,所以 `var_98` 就是计数器,而 `rax` 寄存器则是赋值为 `r12` 寄存器的 `count` 方法,依此得出这个 `do-while` 循环实际就是遍历 `UIViewController` 子类的数组。遍历的行为则是通过 `_nbs_Swizzle_orReplaceWithIMPs` 实现 `initialize` 和 `nbs_jump_initialize:` 的方法交换。 94 | 95 | `nbs_jump_initialize` 的代码如下: 96 | 97 | ``` 98 | void +[_priv_NBSUIAgent nbs_jump_initialize:](void * self, void * _cmd, void * arg2) { 99 | rbx = arg2; 100 | r15 = self; 101 | r14 = [NSStringFromSelector(rbx) retain]; 102 | if ((r14 != 0x0) && ([r14 isEqualToString:@""] == 0x0)) { 103 | [r15 class]; 104 | rax = _nbs_getClassImpOf(); 105 | (rax)(r15, @selector(initialize)); 106 | } 107 | rax = class_getName(r15); 108 | r13 = [[NSString stringWithUTF8String:rax] retain]; 109 | rdx = @"_Aspects_"; 110 | if ([r13 hasSuffix:rdx] == 0x0) goto loc_100050137; 111 | 112 | loc_10005011e: 113 | if (*(int8_t *)_is_tiaoshi_kai == 0x0) goto loc_100050218; 114 | 115 | loc_10005012e: 116 | rsi = cfstring__V__A; 117 | goto loc_100050195; 118 | 119 | loc_100050195: 120 | __NBSDebugLog(0x3, rsi, rdx, rcx, r8, r9, stack[2048]); 121 | goto loc_100050218; 122 | 123 | loc_100050218: 124 | [r13 release]; 125 | rdi = r14; 126 | [rdi release]; 127 | return; 128 | 129 | loc_100050137: 130 | rdx = @"RACSelectorSignal"; 131 | if ([r13 hasSuffix:rdx] == 0x0) goto loc_10005016b; 132 | 133 | loc_100050152: 134 | if (*(int8_t *)_is_tiaoshi_kai == 0x0) goto loc_100050218; 135 | 136 | loc_100050162: 137 | rsi = cfstring__V__R; 138 | goto loc_100050195; 139 | 140 | loc_10005016b: 141 | if (_classSelf_isImpOf(r15, "nbs_vc_flag") == 0x0) goto loc_1000501a3; 142 | 143 | loc_10005017e: 144 | if (*(int8_t *)_is_tiaoshi_kai == 0x0) goto loc_100050218; 145 | 146 | loc_10005018e: 147 | rsi = cfstring____Yh; 148 | goto loc_100050195; 149 | 150 | loc_1000501a3: 151 | rbx = objc_retainBlock(void ^(void * _block, void * arg1) { 152 | return; 153 | }); 154 | rax = imp_implementationWithBlock(rbx); 155 | class_addMethod(r15, @selector(nbs_vc_flag), rax, "v@:"); 156 | [rbx release]; 157 | [_priv_NBSUIAgent hook_viewDidLoad:r15]; 158 | [_priv_NBSUIAgent hook_viewWillAppear:r15]; 159 | [_priv_NBSUIAgent hook_viewDidAppear:r15]; 160 | goto loc_100050218; 161 | } 162 | ``` 163 | `nbs_jump_initialize` 的代码有点长,但是从 `loc_1000501a3` 的例程可以观察到主要逻辑会执行 `hook_viewDidLoad`、`hook_viewWillAppear` 和 `hook_viewDidAppear` 三个方法,从而 hook 住 `UIViewController` 子类的这三个方法。 164 | 165 | 先以 `hook_viewDidLoad:` 方法为例讲解,下面这段代码可能有点晦涩难懂,需要认真分析 166 | 167 | ``` 168 | void +[_priv_NBSUIAgent hook_viewDidLoad:](void * self, void * _cmd, void * arg2) { 169 | rax = [_priv_NBSUIHookMatrix class]; 170 | var_D8 = _nbs_getInstanceImpOf(); 171 | var_D0 = _nbs_getInstanceImpOf(); 172 | rbx = class_getName(arg2); 173 | r14 = class_getSuperclass(arg2); 174 | rax = [NSString stringWithFormat:@"nbs_%s_viewDidLoad", rbx]; 175 | rax = [rax retain]; 176 | var_B8 = rax; 177 | var_C0 = NSSelectorFromString(rax); 178 | r12 = objc_retainBlock(__NSConcreteStackBlock); 179 | var_D0 = imp_implementationWithBlock(r12); 180 | [r12 release]; 181 | rbx = objc_retainBlock(__NSConcreteStackBlock); 182 | r14 = imp_implementationWithBlock(rbx); 183 | [rbx release]; 184 | _nbs_Swizzle_orReplaceWithIMPs(arg2, @selector(viewDidLoad), var_C0, r14, var_D0); 185 | [var_B8 release]; 186 | return; 187 | } 188 | 189 | ``` 190 | 191 | `hook_viewDidLoad:` 方法中的参数 `arg2` 即是要 hook 的 `ViewController` 的类,获取 `arg2` 的类名并赋给 `rbx` 寄存器,然后利用 `rbx` 构造字符串 `nbs_%s_viewDidLoad`,如 `nbs_XXViewController_viewDidLoad`,获得该字符串的 selector 后赋给 `var_C0`,下面几句中的 `__NSConcreteStackBlock` 是创建的存储栈的 block 对象,这个 block 之后会通过 `imp_implementationWithBlock` 方法获取到 IMP 函数指针,`_nbs_Swizzle_orReplaceWithIMPs` 是实现方法交换的函数,参数依次为:`arg2` 是 `ViewController` 的类;`@selector(viewDidLoad)` 是 `viewDidLoad` 的 selector;`var_C0` 是 `nbs_%s_viewDidLoad` 的 selector,`r14` 是第二个 `__NSConcreteStackBlock` 的 IMP;`var_D0` 是第一个 `__NSConcreteStackBlock` 的 IMP。 192 | 193 | `hook_viewDidLoad:` 的整个逻辑大致清楚了,不过这里有个问题为什么不直接交换两个 IMP,而是要先构造两个 block,然后交换两个 block 的 IMP呢?原因是需要将 `ViewController` 的父类也就是 `class_getSuperclass` 的结果作为参数传递给交换后的方法,这样交换的两个 selector 签名的参数个数不一致,需要通过构造 block 去巧妙的解决这个问题,而事实上第一个 `__NSConcreteStackBlock` 的执行的就是 `_priv_NBSUIHookMatrix` 的 `nbs_jump_viewDidLoad:superClass:` 方法,正如之前所说的,这个方法的参数中有 `superClass`,至于为什么需要这个参数,稍后再做介绍。 194 | 195 | 为什么第二个 `__NSConcreteStackBlock` 的执行的是 `nbs_jump_viewDidLoad:superClass:` 方法呢?取消勾选 Hopper 的 `Remove potentially dead code` 选项,代码如下: 196 | 197 | ``` 198 | void +[_priv_NBSUIAgent hook_viewDidLoad:](void * self, void * _cmd, void * arg2) { 199 | rsi = _cmd; 200 | rdi = self; 201 | r12 = _objc_msgSend; 202 | rax = [_priv_NBSUIHookMatrix class]; 203 | rsi = @selector(nbs_jump_viewDidLoad:superClass:); 204 | rdi = rax; 205 | var_D8 = _nbs_getInstanceImpOf(); 206 | rdi = arg2; 207 | rsi = @selector(viewDidLoad); 208 | var_D0 = _nbs_getInstanceImpOf(); 209 | rbx = class_getName(arg2); 210 | r14 = class_getSuperclass(arg2); 211 | LODWORD(rax) = 0x0; 212 | rax = [NSString stringWithFormat:@"nbs_%s_viewDidLoad", rbx]; 213 | rax = [rax retain]; 214 | var_B8 = rax; 215 | var_C0 = NSSelectorFromString(rax); 216 | var_60 = 0xc0000000; 217 | var_5C = 0x0; 218 | var_58 = ___37+[_priv_NBSUIAgent hook_viewDidLoad:]_block_invoke; 219 | var_50 = ___block_descriptor_tmp; 220 | var_48 = var_D8; 221 | var_40 = @selector(viewDidLoad); 222 | var_38 = var_D0; 223 | var_30 = r14; 224 | r12 = objc_retainBlock(__NSConcreteStackBlock); 225 | var_D0 = imp_implementationWithBlock(r12); 226 | r13 = _objc_release; 227 | rax = [r12 release]; 228 | var_A8 = 0xc0000000; 229 | var_A4 = 0x0; 230 | var_A0 = ___37+[_priv_NBSUIAgent hook_viewDidLoad:]_block_invoke_2; 231 | var_98 = ___block_descriptor_tmp47; 232 | var_90 = rbx; 233 | var_88 = var_D8; 234 | var_80 = @selector(viewDidLoad); 235 | var_78 = r14; 236 | var_70 = arg2; 237 | rbx = objc_retainBlock(__NSConcreteStackBlock); 238 | r14 = imp_implementationWithBlock(rbx); 239 | rax = [rbx release]; 240 | rax = _nbs_Swizzle_orReplaceWithIMPs(arg2, @selector(viewDidLoad), var_C0, r14, var_D0); 241 | rax = [var_B8 release]; 242 | rsp = rsp + 0xb8; 243 | rbx = stack[2047]; 244 | r12 = stack[2046]; 245 | r13 = stack[2045]; 246 | r14 = stack[2044]; 247 | r15 = stack[2043]; 248 | rbp = stack[2042]; 249 | return; 250 | } 251 | ``` 252 | 253 | 再来看 `_nbs_getInstanceImpOf` 的代码: 254 | 255 | ``` 256 | void _nbs_getInstanceImpOf() { 257 | rax = class_getInstanceMethod(rdi, rsi); 258 | method_getImplementation(rax); 259 | return; 260 | } 261 | ``` 262 | `_nbs_getInstanceImpOf` 函数的作用很明显,获取 `rdi` 类中 `rsi` selector 的 IMP,读者会发现在 `hook_viewDidLoad:` 方法中共调用了两次 `_nbs_getInstanceImpOf`,第一次 `rdi` 是 `_priv_NBSUIHookMatrix` 类,`rdx` 是 `@selector(nbs_jump_viewDidLoad:superClass:)`,第二次 `rdi` 是 `ViewController` 类,`rdx` 是 `@selector(viewDidLoad)`。 263 | 264 | 接下来看第一个 `__NSConcreteStackBlock`,也就是会调用 `nbs_jump_viewDidLoad:superClass:` 的 block,代码如下: 265 | 266 | ``` 267 | int ___37+[_priv_NBSUIAgent hook_viewDidLoad:]_block_invoke(int arg0, int arg1) { 268 | r8 = *(arg0 + 0x20); 269 | rax = *(arg0 + 0x28); 270 | rdx = *(arg0 + 0x30); 271 | rcx = *(arg0 + 0x38); 272 | rax = (r8)(arg1, rax, rdx, rcx, r8); 273 | return rax; 274 | } 275 | ``` 276 | 277 | `r8` 寄存器是 `nbs_jump_viewDidLoad:superClass:` 的 IMP,这段代码只是调用这个 IMP。IMP 函数的参数与 `nbs_jump_viewDidLoad:superClass:` 相同。 278 | --------------------------------------------------------------------------------