1. LinkMap解析
導(dǎo)讀
IOS在做包大小優(yōu)化的時(shí)候拂铡,需要分析包大小組成,然后通過(guò)包大小組成來(lái)有針對(duì)的做優(yōu)化。其中最主要的工具就是linkmap文件的解析糯崎,下面文章講簡(jiǎn)單說(shuō)明如何解析linkmap文件拓哟。
1.1 如何生成linkMap文件
-
Xcode開(kāi)啟編譯選項(xiàng)Write Link Map File
XCode -> Project -> Build Settings -> 搜map -> 把Write Link Map File選項(xiàng)設(shè)為yes想许,并指定好linkMap的存儲(chǔ)位置
-
編譯后,到編譯目錄里找到該txt文件断序,文件路徑就是上面設(shè)定的路徑流纹,我的位于:
~/Library/Developer/Xcode/DerivedData/FFProject-gdxobffdqcwvyleustpwgfdxslqp/Build/Intermediates/FFProject.build/Debug-iphonesimulator/FFProject.build
1.2 linkMap文件結(jié)構(gòu)解析
1.2.1. 基礎(chǔ)信息
# Path: /Users/bolei/Library/Developer/Xcode/DerivedData/FFProject-gdxobffdqcwvyleustpwgfdxslqp/Build/Products/Debug-iphonesimulator/FFProject.app/FFProject //路徑
# Arch: x86_64 //架構(gòu)
1.2.2.類(lèi)表
# Object files: //類(lèi)文件
[ 0] linker synthesized
[ 1] dtrace
[ 2] /Users/bolei/Library/Developer/Xcode/DerivedData/FFProject-gdxobffdqcwvyleustpwgfdxslqp/Build/Intermediates/FFProject.build/Debug-iphonesimulator/FFProject.build/Objects-normal/x86_64/PAFFConfig.o
這里保存了所有用到的類(lèi)生成的.o文件,也包括用到的dylib
庫(kù)违诗。前面[num]是序號(hào)漱凝,類(lèi)是按照順序保存的,后續(xù)可以通過(guò)序號(hào)查到具體對(duì)應(yīng)的哪個(gè)類(lèi)诸迟。
1.2.3.段表
# Sections:
# Address Size Segment Section
0x100002460 0x00E382DF __TEXT __text
0x100E3A740 0x000019D4 __TEXT __stubs
0x100E3C114 0x0000273E __TEXT __stub_helper
0x100E3E860 0x0009D78B __TEXT __cstring
0x100EDBFEB 0x00089F7A __TEXT __objc_methname
0x100F65F65 0x0000CFCD __TEXT __objc_classname
0x100F72F32 0x00012A27 __TEXT __objc_methtype
0x100F8595A 0x000122E8 __TEXT __ustring
0x100F97C44 0x00067DA8 __TEXT __gcc_except_tab
0x100FFF9F0 0x000259C8 __TEXT __const
0x1010253B8 0x0000017C __TEXT __entitlements
0x101025534 0x0000037B __TEXT __dof_RACSignal
0x1010258AF 0x000002E8 __TEXT __dof_RACCompou
0x101025B98 0x00016928 __TEXT __unwind_info
0x10103C4C0 0x00013B40 __TEXT __eh_frame
0x101050000 0x00000010 __DATA __nl_symbol_ptr
0x101050010 0x00000D30 __DATA __got
0x101050D40 0x00002270 __DATA __la_symbol_ptr
0x101052FB0 0x00000030 __DATA __mod_init_func
0x101052FE0 0x00036580 __DATA __const
0x101089560 0x0005EB20 __DATA __cfstring
0x1010E8080 0x000040A8 __DATA __objc_classlist
0x1010EC128 0x00000448 __DATA __objc_nlclslist
0x1010EC570 0x00000AA8 __DATA __objc_catlist
0x1010ED018 0x00000048 __DATA __objc_nlcatlist
0x1010ED060 0x00000780 __DATA __objc_protolist
0x1010ED7E0 0x00000008 __DATA __objc_imageinfo
0x1010ED7E8 0x001A2B80 __DATA __objc_const
0x101290368 0x00020CE8 __DATA __objc_selrefs
0x1012B1050 0x00000168 __DATA __objc_protorefs
0x1012B11B8 0x00003B80 __DATA __objc_classrefs
0x1012B4D38 0x00002620 __DATA __objc_superrefs
0x1012B7358 0x00010AF0 __DATA __objc_ivar
0x1012C7E48 0x000286E0 __DATA __objc_data
0x1012F0530 0x0000BB48 __DATA __data
0x1012FC080 0x00011A40 __DATA __bss
0x10130DAC0 0x00000538 __DATA __common
接下來(lái)是段表茸炒,描述了不同功能的數(shù)據(jù)保存的地址,通過(guò)這個(gè)地址就可以查到對(duì)應(yīng)內(nèi)存里存儲(chǔ)的是什么數(shù)據(jù)阵苇。
其中第一列是起始地址壁公,第二列是段占用的大小,第三個(gè)是段類(lèi)型绅项,第四列是段名稱(chēng)贮尖,每一行初始地址 = 上一行的初始地址+占用大小
其中:
__TEXT 表示代碼段,用于執(zhí)行,可讀不可以寫(xiě)趁怔,可以被執(zhí)行
__DATA 表示數(shù)據(jù)段湿硝,用于存儲(chǔ)數(shù)據(jù)薪前,可以讀寫(xiě),不可以執(zhí)行
其中:
第一個(gè)段是__PAGEZERO 地址從0到0x100000000关斜,程序保留字段示括。
1.2.3.1 段表內(nèi)容含義
__TEXT段節(jié)名含義
1. __text: 代碼節(jié),存放機(jī)器編譯后的代碼
2. __stubs: 用于輔助做動(dòng)態(tài)鏈接代碼(dyld).
3. __stub_helper:用于輔助做動(dòng)態(tài)鏈接(dyld).
4. __objc_methname:objc的方法名稱(chēng)
5. __cstring:代碼運(yùn)行中包含的字符串常量,比如代碼中定義`#define kGeTuiPushAESKey @"DWE2#@e2!"`,那DWE2#@e2!會(huì)存在這個(gè)區(qū)里痢畜。
6. __objc_classname:objc類(lèi)名
7. __objc_methtype:objc方法類(lèi)型
8. __ustring:
9. __gcc_except_tab:
10. __const:存儲(chǔ)const修飾的常量
11. __dof_RACSignal:
12. __dof_RACCompou:
13. __unwind_info:
__DATA段節(jié)名含義
1. __got:存儲(chǔ)引用符號(hào)的實(shí)際地址垛膝,類(lèi)似于動(dòng)態(tài)符號(hào)表
2. __la_symbol_ptr:lazy symbol pointers。懶加載的函數(shù)指針地址丁稀。和__stubs和stub_helper配合使用吼拥。具體原理暫留。
3. __mod_init_func:模塊初始化的方法线衫。
4. __const:存儲(chǔ)constant常量的數(shù)據(jù)凿可。比如使用extern導(dǎo)出的const修飾的常量。
5. __cfstring:使用Core Foundation字符串
6. __objc_classlist:objc類(lèi)列表,保存類(lèi)信息授账,映射了__objc_data的地址
7. __objc_nlclslist:Objective-C 的 +load 函數(shù)列表枯跑,通常比 __mod_init_func 更早執(zhí)行。具體可以
8. __objc_catlist: categories
9. __objc_nlcatlist:Objective-C 的categories的 +load函數(shù)列表白热。
10. __objc_protolist:objc協(xié)議列表
11. __objc_imageinfo:objc鏡像信息
12. __objc_const:objc常量敛助。保存objc_classdata結(jié)構(gòu)體數(shù)據(jù)。用于映射類(lèi)相關(guān)數(shù)據(jù)的地址屋确,比如類(lèi)名纳击,方法名等。
13. __objc_selrefs:引用到的objc方法
14. __objc_protorefs:引用到的objc協(xié)議
15. __objc_classrefs:引用到的objc類(lèi)
16. __objc_superrefs:objc超類(lèi)引用
17. __objc_ivar:objc ivar指針,存儲(chǔ)屬性攻臀。
18. __objc_data:objc的數(shù)據(jù)焕数。用于保存類(lèi)需要的數(shù)據(jù)。最主要的內(nèi)容是映射__objc_const地址茵烈,用于找到類(lèi)的相關(guān)數(shù)據(jù)百匆。
19. __data:暫時(shí)沒(méi)理解砌些,從日志看存放了協(xié)議和一些固定了地址(已經(jīng)初始化)的靜態(tài)量呜投。
20. __bss:存儲(chǔ)未初始化的靜態(tài)量。比如:`static NSThread *_networkRequestThread = nil;`其中這里面的size表示應(yīng)用運(yùn)行占用的內(nèi)存存璃,不是實(shí)際的占用空間仑荐。所以計(jì)算大小的時(shí)候應(yīng)該去掉這部分?jǐn)?shù)據(jù)。
21. __common:存儲(chǔ)導(dǎo)出的全局的數(shù)據(jù)纵东。類(lèi)似于static粘招,但是沒(méi)有用static修飾。比如KSCrash里面`NSDictionary* g_registerOrders;`, g_registerOrders就存儲(chǔ)在__common里面
1.2.4 后續(xù)符號(hào)表內(nèi)容
1.2.4.1 代碼節(jié)
# Symbols:
# Address Size File Name
0x100002460 0x00000080 [ 2] +[PAFFConfig instance]
0x1000024E0 0x00000050 [ 2] ___22+[PAFFConfig instance]_block_invoke
0x100002530 0x00000090 [ 2] -[PAFFConfig init]
apiType]
這里面保存里類(lèi)里面的方法內(nèi)存情況偎球。其中
- 第一列是起始地址位置洒扎,通過(guò)這個(gè)地址我們可以查上面的段表辑甜,可以知道,對(duì)應(yīng)的節(jié)為
__text
袍冷。 - 第二列是大小磷醋,通過(guò)這個(gè)可以算出方法占用的大小。
- 第三列是歸屬的類(lèi)(.o文件)胡诗,這里序號(hào)是2邓线,通過(guò)查類(lèi)表可以知道對(duì)應(yīng)的類(lèi)是PAFFConfig。
通過(guò)這部分我們可以分析出來(lái)每個(gè)類(lèi)對(duì)應(yīng)的方法的大小是多少煌恢。
1.2.4.2 方法名節(jié)(__objc_methname
)
0x100EDBFEB 0x00000006 [ 2] literal string: alloc
0x100EDBFF1 0x00000005 [ 2] literal string: init
0x100EDBFF6 0x0000000B [ 2] literal string: mainBundle
0x100EDC001 0x0000000F [ 2] literal string: infoDictionary
0x100EDC010 0x0000000E [ 2] literal string: objectForKey:
0x100EDC01E 0x0000000C [ 2] literal string: setAppName:
0x100EDC02A 0x0000000C [ 2] literal string: setVersion:
0x100EDC036 0x0000000C [ 2] literal string: setApiType:
0x100EDC042 0x00000009 [ 2] literal string: instance
0x100EDC04B 0x00000008 [ 2] literal string: isDebug
這部分保存了類(lèi)里方法的字符串信息(所以原則上方法名起短一些骇陈,是可以減少占用的 - -!)
分析步驟:
- 查看第一列起始地址,然后在上面的段表中查看這個(gè)地址在那個(gè)節(jié)里瑰抵,可以看到在
__objc_methname
中你雌。 - 通過(guò)第二列對(duì)比大小
- 通過(guò)第三列解析對(duì)應(yīng)的類(lèi)和對(duì)應(yīng)方法名稱(chēng)
1.2.4.3類(lèi)列表節(jié)(__objc_classlist
)
0x1010E8080 0x00000008 [ 2] anon
0x1010E8088 0x00000008 [ 3] anon
0x1010E8090 0x00000008 [ 4] anon
0x1010E8098 0x00000008 [ 5] anon
0x1010E80A0 0x00000008 [ 7] anon
0x1010E80A8 0x00000008 [ 9] anon
0x1010E80B0 0x00000008 [ 10] anon
0x1010E80B8 0x00000008 [ 11] anon
0x1010E80C0 0x00000008 [ 12] anon
0x1010E80C8 0x00000008 [ 13] anon
0x1010E80D0 0x00000008 [ 14] anon
0x1010E80D8 0x00000008 [ 15] anon
0x1010E80E0 0x00000008 [ 16] anon
0x1010E80E8 0x00000008 [ 17] anon
0x1010E80F0 0x00000008 [ 18] anon
0x1010E80F8 0x00000008 [ 19] anon
0x1010E8100 0x00000038 [ 20] anon
0x1010E8138 0x00000030 [ 21] anon
__objc_classlist
存儲(chǔ)了所有類(lèi)的虛擬地址。即__objc_data
地址谍憔。這里都是二進(jìn)制數(shù)據(jù)匪蝙,具體保存了什么,看下對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu)
__objc_data
的數(shù)據(jù)結(jié)構(gòu)為:
typedef struct objc_class{
unsigned long long isa;
unsigned long long wuperclass;
unsigned long long cache;
unsigned long long vtable;
unsigned long long data;
unsigned long long reserved1;
unsigned long long reserved2;
unsigned long long reserved3;
}objc_class;
其中最主要的是data
字段习贫,保存了_objc_const
節(jié)對(duì)應(yīng)的數(shù)據(jù)地址逛球。數(shù)據(jù)結(jié)構(gòu)為:
typedef struct objc_classdata{
long long flags;
long long instanceStart;
long long instanceSize;
long long reserved;
unsigned long long ivarlayout;
unsigned long long name;
unsigned long long baseMethod;
unsigned long long baseProtocol;
unsigned long long ivars;
unsigned long long weakIvarLayout;
unsigned long long baseProperties;
}
這里面保存了類(lèi)名,方法名苫昌,協(xié)議名颤绕,ivar指針和屬性對(duì)應(yīng)的地址。最后對(duì)應(yīng)到相應(yīng)的TEXT段里就能找到祟身。比如類(lèi)名在__objc_classname
可以找到奥务,方法名可以在__objc_methname
。應(yīng)用程序就是通過(guò)這個(gè)結(jié)構(gòu)來(lái)尋找哪個(gè)類(lèi)對(duì)應(yīng)的那個(gè)方法袜硫,從而執(zhí)行相關(guān)邏輯
1.3 如何找到?jīng)]有用到的類(lèi)和方法氯葬?
我們可以利用linkmap
和otools
結(jié)合來(lái)找到,具體看下面isee
的使用
-
找到哪些類(lèi)沒(méi)有使用
通過(guò)
__objc_classrefs
和link map中解析到的所有_objc_classname
對(duì)比就可以知道哪些類(lèi)沒(méi)用婉陷。其中__objc_classrefs
的解析需要通過(guò)otool命令才能解析帚称,來(lái)找到使用到的class』喟模可以用以下命令獲取到所有解析成class
對(duì)象后的數(shù)據(jù)otool -V -o FFProject -arch arm64 | open -f
輸出的數(shù)據(jù)中找到
Contents of (__DATA,__objc_classrefs) section
部分闯睹。Contents of (__DATA,__objc_classrefs) section 000000010003cc90 0x10003d348 _OBJC_CLASS_$_AFHTTPSessionManager 000000010003cc98 0x0 _OBJC_CLASS_$_NSDictionary 000000010003cca0 0x0 _OBJC_CLASS_$_UISceneConfiguration 000000010003cca8 0x10003d230 _OBJC_CLASS_$_AppDelegate 000000010003ccb0 0x0 _OBJC_CLASS_$_NSDate 000000010003ccb8 0x0 _OBJC_CLASS_$_NSString 000000010003ccc0 0x0 _OBJC_CLASS_$_NSMutableDictionary 000000010003ccc8 0x0 _OBJC_CLASS_$_NSUUID
-
找到哪些方法沒(méi)有使用:
通過(guò)
__objc_selrefs
和_objc_methname
對(duì)比可以知道哪些方法沒(méi)有使用到。其中__objc_selrefs
需要用otool命令才能解析担神,找到使用到的所有方法楼吃。otool -v -s __DATA __objc_selrefs <path>
1.2.4 otool使用
這個(gè)用來(lái)做反匯編的,比如分析哪些類(lèi)被使用了,需要用這個(gè)工具孩锡。
比如獲取使用到的方法可以用這個(gè)命令:
otool -V -s __DATA __objc_selrefs <path> -arch arm64 | open -f
其中path是你的應(yīng)用編譯后生成的可執(zhí)行文件酷宵。通常在項(xiàng)目的DerivedData目錄下的Build/Products//.app文件,然后顯示包內(nèi)容躬窜,有個(gè)和工程同名的可執(zhí)行文件忧吟。比如我的目錄:
/Users/bolei/Library/Developer/Xcode/DerivedData/FFProject-gqpkbetfhlofkxcmyfwpmkfqubun/Build/Products/Release-iphoneos/FFProject.app/FFProject
打印使用到的類(lèi): _objc_classrefs
otool -V -o FFProject -arch arm64 | open -f
可以打印出來(lái)objc Section中的所有數(shù)據(jù)
2. iSee使用
iSee是一款分析iOS可執(zhí)行文件成分的工具,參考zyangSir的iSee工程做了修改斩披,github地址 通過(guò)加載XCode在項(xiàng)目編譯期間產(chǎn)生的linkMap文件溜族,能夠輸出項(xiàng)目中每個(gè)類(lèi)(包括第三方靜態(tài)庫(kù)),在最終可執(zhí)行文件中占用的長(zhǎng)度信息垦沉。本工具根據(jù)zyangSir的代碼做了部分修改煌抒。主要功能有:
- 各個(gè)可執(zhí)行文件占用大小
- 可執(zhí)行文件中,各個(gè)段占用的大胁薇丁(包括方法+常量字符串等)
- 未使用到的類(lèi)
- 未使用到的方法
包括:
-
使用的所有庫(kù): custom是非系統(tǒng)庫(kù)或第三方庫(kù)
image.png -
庫(kù)里面用到的所有類(lèi)
image.png -
類(lèi)里面所有節(jié)信息寡壮,Z開(kāi)頭的是輔助信息,記錄使用到的類(lèi)+方法和未使用的類(lèi)+方法
image.png -
節(jié)對(duì)應(yīng)詳情信息
image.png -
未使用的類(lèi):
image.png -
未使用的方法
2.1 使用方法
建議使用真機(jī)生成的文件測(cè)試讹弯,目前看對(duì)arm64架構(gòu)支持最好蔚出。優(yōu)先使用arm64相關(guān)文件檢測(cè)问拘。分析不了framework里面的,所以如果是cocoapods導(dǎo)入,建議去掉!framework
,使用靜態(tài)庫(kù)導(dǎo)入师骗。強(qiáng)烈建議cocoapods使用靜態(tài)庫(kù)導(dǎo)入派殷,會(huì)顯著提升啟動(dòng)速度
2.1.1 導(dǎo)出生成linkmap文件
- 在XCode編譯選項(xiàng)中打開(kāi)”WriteLinkMapFile”
image - 選擇好真機(jī)(采用arm64架構(gòu)的),選Debug录语。先編譯項(xiàng)目,進(jìn)入項(xiàng)目的Derived Data目錄
image - 依次進(jìn)入Build/Intermediates/項(xiàng)目名.build/ 目錄, 找到相應(yīng)模式下的編譯產(chǎn)物文件夾
image - 可以看到一個(gè)名為 項(xiàng)目名-LinkMap-normal-CPU架構(gòu).txt的文件魂迄,在iSee中點(diǎn)擊linkMap文件按鈕,導(dǎo)入這個(gè)文件
2.1.2 導(dǎo)出生成的可執(zhí)行文件
-
在上一步的Derived Data目錄下,
看到.app結(jié)尾的文件耸三,郵件顯示包內(nèi)容乱陡,找到和工程名稱(chēng)一樣的一個(gè)可執(zhí)行文件,把這個(gè)文件copy到相關(guān)目錄
在iSee中點(diǎn)擊可執(zhí)行文件仪壮,導(dǎo)入剛才的文件
之后程序會(huì)自動(dòng)執(zhí)行分析憨颠。
2.2 簡(jiǎn)單說(shuō)明
- 現(xiàn)在對(duì)arm64支持比較好,所以建議用這個(gè)格式測(cè)試积锅∷可以在debug模式下,鏈接Arm64的機(jī)器乏沸,然后只編譯生成此平臺(tái)下的數(shù)據(jù)淫茵。
- 對(duì)于未使用方法和未使用類(lèi)爪瓜,使用otool工具來(lái)做輔助分析
- 未使用方法和未使用類(lèi)蹬跃,有誤報(bào)可能,主要誤報(bào)的是一些實(shí)現(xiàn)協(xié)議的類(lèi),這一部分是動(dòng)態(tài)使用的蝶缀,所以靜態(tài)分析不出來(lái)丹喻。
- 目前沒(méi)有對(duì)swift語(yǔ)言的分析。
2021-04-20 16:31:57.378513+0800 otool[82701:1827934] Failed to open macho file at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool for reading: Too many levels of symbolic links
2021-04-20 16:31:57.571279+0800
2.3 原理說(shuō)明
先讀取linkmap
2.3.1 解析Object files
:
-
解析
Object files
, 按照行讀取翁都,使用正則解析@"\\[\\s*(\\d+)\\]\\s+(.+)"
每一行的數(shù)據(jù)碍论,知道遇到# Sections:
停止。其中最后一個(gè)匹配到的是具體路徑柄慰,可以切割后文件名鳍悠,比如ViewController.o
,路徑以user
開(kāi)頭的坐搔,就是用戶代碼藏研,否則認(rèn)為系統(tǒng)庫(kù)代碼。需要注意的是[數(shù)字]
概行,里面的數(shù)據(jù)是具體序號(hào)蠢挡,需要按照順序保存到數(shù)組中,供后續(xù)使用凳忙。# Object files: [ 0] linker synthesized [ 1] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/ViewController.o [ 2] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/AppDelegate.o [ 3] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/main.o [ 4] /Users/bolei/Library/Developer/Xcode/DerivedData/iSeeDemo-famzgrqugqqtpjfncdokvahyclhb/Build/Intermediates.noindex/iSeeDemo.build/Debug-iphoneos/iSeeDemo.build/Objects-normal/arm64/SceneDelegate.o [ 5] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks//Foundation.framework/Foundation.tbd [ 6] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/usr/lib/libobjc.tbd [ 7] /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/System/Library/Frameworks//UIKit.framework/UIKit.tbd
參考代碼:
/**
* 解析目標(biāo)文件log
*/
- (void)parseObjectFileLog
{
NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity: 100];
self.lastLineStr = [_linkMapfileReader readLine];
while (![self isSectionStartFlag: _lastLineStr]) {//如果沒(méi)檢測(cè)到下一段不同類(lèi)型log的起始標(biāo)識(shí)串业踏,則繼續(xù)
// NSLog(@"lastLine = %@",_lastLineStr);
if ([self.lastLineStr hasPrefix:@"#"]) {
self.lastLineStr = [_linkMapfileReader readLine];
continue;
}
NSString *regexStr = @"\\[\\s*(\\d+)\\]\\s+(.+)";
NSRegularExpression* regexExpression = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* matchs = [regexExpression matchesInString:self.lastLineStr options:0 range:NSMakeRange(0, self.lastLineStr.length)];
if (matchs == nil || [matchs count] == 0) {
return;
}
NSTextCheckingResult *checkingResult = [matchs objectAtIndex:0];
if ([checkingResult numberOfRanges] < 3) {
return;
}
NSString *indexStr = [self.lastLineStr substringWithRange:[checkingResult rangeAtIndex:1]];//索引
NSUInteger index = indexStr.integerValue;
NSString *path = [self.lastLineStr substringWithRange:[checkingResult rangeAtIndex:2]];//索引
NSRange range = [path rangeOfString:@"/"];
ObjectFileItem * objFileItem = [[ObjectFileItem alloc] init];
if (range.location == NSNotFound) {
objFileItem.fileType = OBJECT_FILE_FROM_CUSTOM_CODE;
objFileItem.name = path;
objFileItem.module = @"Custom";
} else {
NSString *pathStr = [path substringFromIndex:range.location];
NSString * objectFileName = [pathStr lastPathComponent];
// NSLog(@"path = %@, fileName= %@",pathStr,objectFileName);
if ([pathStr hasPrefix: CUSTOM_LIB_PATH_PREFIX]) {
NSRange bracketRange = [objectFileName rangeOfString: @"("];
if (bracketRange.location != NSNotFound ) {
//靜態(tài)庫(kù)中的目標(biāo)文件
objFileItem.module = [objectFileName substringToIndex:bracketRange.location];
objFileItem.fileType = OBJECT_FILE_FROM_STATIC_FILE;
NSRange objNameRange = bracketRange;
objNameRange.location ++;
objNameRange.length = objectFileName.length - (objNameRange.location + 1) - 1; //去掉兩個(gè)括號(hào)
objFileItem.name = [objectFileName substringWithRange: objNameRange];
}else
{
//用戶自行創(chuàng)建的類(lèi)
objFileItem.fileType = OBJECT_FILE_FROM_CUSTOM_CODE;
objFileItem.name = objectFileName;
objFileItem.module = @"Custom";
}
}else if ([pathStr hasPrefix: SYSTEM_LIB_PATH_PREFIX])
{ //系統(tǒng)庫(kù)目標(biāo)文件
objFileItem.fileType = OBJECT_FILE_FROM_SYSTEM_LIB;
objFileItem.name = objectFileName;
objFileItem.module = @"System";
}
double progress = [_linkMapfileReader readedFileSizeRatio];
[self updateAnalyzeProgress: progress];
}
if (tmpArray.count > index) {
[tmpArray replaceObjectAtIndex:index withObject:objFileItem];
} else {
[tmpArray addObject:objFileItem];
}
// one loop end, start parsing next line log
self.lastLineStr = [_linkMapfileReader readLine];
double progress = [_linkMapfileReader readedFileSizeRatio];
[self updateAnalyzeProgress: progress];
}
self.objectFileArray = [NSArray arrayWithArray: tmpArray];
}
2.3.2 解析section
表
這里保存了每個(gè)段的保存地址,需要保存下來(lái)涧卵,后面用于匹配在哪個(gè)段和節(jié)中勤家。
數(shù)據(jù)為:
# Sections:
# Address Size Segment Section
0x100006328 0x0001D970 __TEXT __text
0x100023C98 0x00000570 __TEXT __stubs
0x100024208 0x00000588 __TEXT __stub_helper
0x100024790 0x00005E1A __TEXT __objc_methname
0x10002A5AA 0x00002CCD __TEXT __cstring
0x10002D277 0x000004DE __TEXT __objc_classname
0x10002D755 0x00001AB0 __TEXT __objc_methtype
0x10002F208 0x00000028 __TEXT __const
0x10002F230 0x00000448 __TEXT __gcc_except_tab
0x10002F678 0x0000014A __TEXT __ustring
0x10002F7C4 0x000007C0 __TEXT __unwind_info
0x10002FF88 0x00000074 __TEXT __eh_frame
0x100030000 0x000000E8 __DATA __got
0x1000300E8 0x000003A0 __DATA __la_symbol_ptr
0x100030488 0x00000850 __DATA __const
0x100030CD8 0x00001120 __DATA __cfstring
0x100031DF8 0x00000108 __DATA __objc_classlist
0x100031F00 0x00000008 __DATA __objc_nlclslist
0x100031F08 0x00000038 __DATA __objc_catlist
0x100031F40 0x00000088 __DATA __objc_protolist
0x100031FC8 0x00000008 __DATA __objc_imageinfo
0x100031FD0 0x00009830 __DATA __objc_const
0x10003B800 0x00001480 __DATA __objc_selrefs
0x10003CC80 0x00000010 __DATA __objc_protorefs
0x10003CC90 0x00000228 __DATA __objc_classrefs
0x10003CEB8 0x000000D0 __DATA __objc_superrefs
0x10003CF88 0x0000022C __DATA __objc_ivar
0x10003D1B8 0x00000A50 __DATA __objc_data
0x10003DC08 0x00000680 __DATA __data
0x10003E288 0x000000C0 __DATA __bss
也按照順序保存到數(shù)組里。解析代碼:
/**
* 解析段表log
*/
- (void)parseSectionTableLog
{
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity: 50];
self.lastLineStr = [_linkMapfileReader readLine];
// NSLog(@"parseSectionTableLog = %@",self.lastLineStr);
while (![self isSectionStartFlag: _lastLineStr]) {
if ([self.lastLineStr hasPrefix:@"#"]) {
self.lastLineStr = [_linkMapfileReader readLine];
continue;
}
NSArray *oneLineConponents = [_lastLineStr componentsSeparatedByString:@"\t"];
NSString *address = oneLineConponents[0];
NSString *sizeStr = oneLineConponents[1];
NSString *segmentTypeStr = oneLineConponents[2];
NSString *sectionNameStr = oneLineConponents[3];
// NSLog(@"address = %@, sizeStr = %@ segmentTypeStr = %@ sectionNameStr = %@",address,sizeStr,segmentTypeStr,sectionNameStr);
ExecutableCodeItem *codeItem = [[ExecutableCodeItem alloc] init];
codeItem.size = strtoul([sizeStr UTF8String], 0, 16);
NSUInteger lastIndex = [sectionNameStr length] - 1;//2 是制表符 \t 的兩個(gè)字符位移
codeItem.name = [sectionNameStr substringToIndex: lastIndex];
codeItem.startAddress = strtoul([address UTF8String], 0, 16);
if ([segmentTypeStr isEqualToString: SEGMENT_TYPE_CODE]) {
codeItem.segmentType = CodeType_TEXT;
}else if ([segmentTypeStr isEqualToString: SEGMENT_TYPE_DATA])
{
codeItem.segmentType = CodeType_DATA;
}
[tmpArray addObject: codeItem];
//one loop end , start next circle
self.lastLineStr = [_linkMapfileReader readLine];
[self updateAnalyzeProgress: _linkMapfileReader.readedFileSizeRatio];
}
[self updateAnalyzeProgress: _linkMapfileReader.readedFileSizeRatio];
self.executableCodeArray = [NSArray arrayWithArray: tmpArray];
}
2.3.3 解析符號(hào)表
后面的數(shù)據(jù)柳恐,會(huì)按照地址順序却紧,打印相關(guān)內(nèi)容,也按行解析胎撤,匹配正則@"(.+?)\\t(.*?)\\t\\[\\s*(\\d+)\\]\\s+(.+)"
晓殊。。其中[數(shù)字]
就是匹配的上面第一步里解析Object files
里面的.o
文件伤提,通過(guò)序號(hào)匹配就能找到對(duì)應(yīng)的是哪個(gè)文件里的內(nèi)容巫俺。通過(guò)起始地址就能解析出來(lái)具體屬于哪個(gè)節(jié)section
。通過(guò)Size就能算出這個(gè)方法占用的大小肿男。
# Symbols:
# Address Size File Name
0x100006328 0x00000114 [ 1] -[ViewController viewDidLoad]
0x10000643C 0x00000028 [ 1] ___29-[ViewController viewDidLoad]_block_invoke
0x100006464 0x00000028 [ 1] ___29-[ViewController viewDidLoad]_block_invoke_2
0x10000648C 0x00000008 [ 2] -[AppDelegate application:didFinishLaunchingWithOptions:]
0x100006494 0x0000008C [ 2] -[AppDelegate application:configurationForConnectingSceneSession:options:]
0x100006520 0x00000004 [ 2] -[AppDelegate application:didDiscardSceneSessions:]
0x100006524 0x00000080 [ 3] _main
0x1000065A4 0x00000004 [ 4] -[SceneDelegate scene:willConnectToSession:options:]
0x1000065A8 0x00000004 [ 4] -[SceneDelegate sceneDidDisconnect:]
0x1000065AC 0x00000004 [ 4] -[SceneDelegate sceneDidBecomeActive:]
0x1000065B0 0x00000004 [ 4] -[SceneDelegate sceneWillResignActive:]
0x1000065B4 0x00000004 [ 4] -[SceneDelegate sceneWillEnterForeground:]
0x1000065B8 0x00000004 [ 4] -[SceneDelegate sceneDidEnterBackground:]
0x1000065BC 0x00000010 [ 4] -[SceneDelegate window]
0x1000065CC 0x00000014 [ 4] -[SceneDelegate setWindow:]
0x1000065E0 0x00000014 [ 4] -[SceneDelegate .cxx_destruct]
0x1000065F4 0x0000014C [ 5] -[AFCachedImage initWithImage:identifier:]
0x100006740 0x00000064 [ 5] -[AFCachedImage accessImage]
0x1000067A4 0x000000A4 [ 5] -[AFCachedImage description]
0x100006848 0x00000008 [ 5] -[AFCachedImage image]
0x100006850 0x0000000C [ 5] -[AFCachedImage setImage:]
0x10000685C 0x0000000C [ 5] -[AFCachedImage identifier]
解析代碼:
/**
* 解析一行符號(hào)log
*
* @param oneLineLog 一行符號(hào)log
*
* @return 解析結(jié)果
*/
- (void)parseOneLineSymbolLog:(NSString *)oneLineLog
{
// NSLog(@"parseOneLineSymbolLog = %@", oneLineLog);
//過(guò)濾非目標(biāo)串
NSString *filtreString = @"\t * \n * \x10\n * %@\n * \r\n";
NSRange range = [filtreString rangeOfString: oneLineLog];
if (range.location != NSNotFound) {
return;
}
NSString *regexStr = @"(.+?)\\t(.*?)\\t\\[\\s*(\\d+)\\]\\s+(.+)";
NSRegularExpression* regexExpression = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
NSArray* matchs = [regexExpression matchesInString:oneLineLog options:0 range:NSMakeRange(0, oneLineLog.length)];
if (matchs == nil || [matchs count] == 0) {
return;
}
NSTextCheckingResult *checkingResult = [matchs objectAtIndex:0];
if ([checkingResult numberOfRanges] < 5) {
return;
}
NSString *startAddressStr = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:1]];//起始地址
NSString *sizeStr = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:2]];//空間大小
NSString *indexStr = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:3]];//索引
NSString *name = [oneLineLog substringWithRange:[checkingResult rangeAtIndex:4]];//名稱(chēng)
long startAddress = strtoul([startAddressStr UTF8String], 0, 16);
long size = strtoul([sizeStr UTF8String], 0, 16);
NSUInteger index = indexStr.integerValue;
ExecutableCodeItem *executable = [self excutableItem:startAddress];//段名稱(chēng)
//添加到所屬的目標(biāo)文件
if (index < _objectFileArray.count) {
ObjectFileItem *targetObjectFile = _objectFileArray[ index ];
targetObjectFile.size += size;
ObjectSecionItem *section = [targetObjectFile.sectionDictionary objectForKey:executable.name];
if (section == nil) {
section = [[ObjectSecionItem alloc] init];
section.name = executable.name;
section.fileTypeName = executable.segmentTypeStr;
[targetObjectFile.sectionDictionary setObject:section forKey:executable.name];
}
section.size += size;
MethodFileItem *funcItem = [[MethodFileItem alloc] init];
funcItem.name = name;
funcItem.size = size;
funcItem.fileTypeName = executable.name;
funcItem.startAddress = startAddress;
[section.objectsList addObject:funcItem];
// NSLog(@"startAddress = %@ size = %@ index = %@ name = %@ target = %@ section = %@",startAddressStr,sizeStr,indexStr,name,targetObjectFile, executable.name);
}
}
到這里整個(gè)linkmap
就解析完成了介汹。接著使用otools
來(lái)解析具體二進(jìn)制文件。(備注:這里為啥要用otools解析舶沛,原因是objc_selrefs
和__objc_classrefs
記錄了程序中用到的方法和類(lèi)嘹承,但linkmap里面只有二進(jìn)制數(shù)據(jù),需要進(jìn)一步解析成具體數(shù)據(jù)結(jié)構(gòu)如庭,這個(gè)就需要依賴(lài)otools來(lái)做)叹卷。
2.3.4 利用otools解析使用到的所有方法和未使用的所有方法
運(yùn)行otool -v -s __DATA __objc_selrefs <path>
,輸出__objc_selrefs
內(nèi)容。
Contents of (__DATA,__objc_selrefs) section
000000010003b800 __TEXT:__objc_methname:viewDidLoad
000000010003b808 __TEXT:__objc_methname:manager
000000010003b810 __TEXT:__objc_methname:dictionaryWithObjects:forKeys:count:
000000010003b818 __TEXT:__objc_methname:GET:parameters:headers:progress:success:failure:
000000010003b820 __TEXT:__objc_methname:role
000000010003b828 __TEXT:__objc_methname:initWithName:sessionRole:
000000010003b830 __TEXT:__objc_methname:class
000000010003b838 __TEXT:__objc_methname:init
000000010003b840 __TEXT:__objc_methname:setImage:
000000010003b848 __TEXT:__objc_methname:setIdentifier:
000000010003b850 __TEXT:__objc_methname:size
000000010003b858 __TEXT:__objc_methname:scale
000000010003b860 __TEXT:__objc_methname:setTotalBytes:
000000010003b868 __TEXT:__objc_methname:date
000000010003b870 __TEXT:__objc_methname:setLastAccessDate:
...
獲取之后先通過(guò)正則解析(.+?)\\s+__TEXT:__objc_methname:(.+)
, 解析出來(lái)起始地址骤竹,方法名稱(chēng)比如viewDidLoad
帝牡。接著需要找到這個(gè)方法對(duì)應(yīng)調(diào)用的類(lèi)是什么。這個(gè)需要使用前面前面解析linkmap
用到的數(shù)據(jù)蒙揣,算法是:
- 按照順序從前面o文件列表里面搜索靶溜,取到每個(gè)類(lèi)對(duì)應(yīng)的
__objc_selrefs
數(shù)據(jù)。 - 然后從
__objc_selrefs
中懒震,比對(duì)數(shù)據(jù)里面那個(gè)起始地址一致罩息,說(shuō)明這個(gè)數(shù)據(jù)就屬于這個(gè)類(lèi)。 - 然后把這個(gè)方法記錄到已經(jīng)使用的方法列表里面个扰。
- 解析是否有
__objc_ivar
段扣汪,比如_OBJC_IVAR_$_AFSecurityPolicy._pinnedPublicKeys
,說(shuō)明是屬性,需要把_pinnedPublicKeys
和setPinnedPublicKeys
也加入到方法列表里面锨匆。 - 檢索上一步
linkmap
中的__objc_methname
里面的數(shù)據(jù)崭别,literal string: scene:willConnectToSession:options:
, 從第三步驟記錄的所有使用的方法查看是否有使用,沒(méi)有使用就寫(xiě)入未使用方法列表里面恐锣。
有部分代碼可能比較繞茅主,其實(shí)主要是為了加速搜索。因?yàn)榈刂肥且恢边f增的土榴,所以不會(huì)每次搜索都從剛開(kāi)始檢索诀姚,而是從上次的結(jié)果之后來(lái)檢索。
- (void)anylyzeUsedMethodWithData:(NSString *)string {
// 解析結(jié)果玷禽,分解數(shù)據(jù)
if (string.length) {
//數(shù)據(jù)清空
for (ObjectFileItem *file in self.resultList) {
[file.usedMethod removeAllObjects];
[file.unUsedMethod removeAllObjects];
}
//解析 0000000100dcd9c0 __TEXT:__objc_methname:alloc
NSArray *lines = [string componentsSeparatedByString:@"\n"];
NSString *regexStr = @"(.+?)\\s+__TEXT:__objc_methname:(.+)";
NSRegularExpression* regexExpression = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
int objIndex = 0; //掃描的obj的索引
int methodIndex = 0; //掃描到的方法索引
// 解析出來(lái)使用到的方法
for (NSString *line in lines) {
@autoreleasepool {
if (objIndex >= [self.resultList count]) {
break;
}
NSArray* matchs = [regexExpression matchesInString:line options:0 range:NSMakeRange(0, line.length)];
if (matchs == nil || [matchs count] == 0) {
continue;
}
NSTextCheckingResult *checkingResult = [matchs objectAtIndex:0];
if ([checkingResult numberOfRanges] < 3) {
continue;
}
NSString *startAddressStr = [line substringWithRange:[checkingResult rangeAtIndex:1]];
NSString *method = [line substringWithRange:[checkingResult rangeAtIndex:2]];
long startAddress = strtoul([startAddressStr UTF8String], 0, 16);
ObjectSecionItem *section = nil;
ObjectFileItem *obj = nil;
//需要找到對(duì)應(yīng)哪個(gè)類(lèi)的起始地址
while (objIndex < [self.resultList count]) {
obj = [self.resultList objectAtIndex:objIndex];
section = [obj.sectionDictionary objectForKey:@"__objc_selrefs"];
MethodFileItem *method = [section.objectsList lastObject];
if (method.startAddress < startAddress) {
objIndex ++;
methodIndex = 0;
}else {
break;
}
}
if (objIndex >= [self.resultList count]) {
break;
}
//數(shù)據(jù)其實(shí)是一一對(duì)應(yīng)的赫段,如果沒(méi)找到可能是異常了
// 從第0個(gè)開(kāi)始找,如果找到了矢赁,就放在usedMethod里面糯笙,并繼續(xù)循環(huán)
MethodFileItem *methodItem = [section.objectsList objectAtIndex:methodIndex];
if (methodItem.startAddress == startAddress) {
methodItem.name = method;
[obj.usedMethod setObject:methodItem forKey:method];
methodIndex ++;
continue;
}
// 如果沒(méi)找到,循環(huán)遍歷查找撩银,找到后放入给涕,,標(biāo)記下methodIndex额获,并繼續(xù)循環(huán)
for (int j = 0; j < [section.objectsList count]; j ++) {
MethodFileItem *methodItem = [section.objectsList objectAtIndex:j];
if (methodItem.startAddress == startAddress) {
//獲取到
methodIndex = j;
[obj.usedMethod setObject:methodItem forKey:method];
break;
}
}
}
}
//填充數(shù)據(jù)
for (ObjectFileItem *obj in self.resultList) {
ObjectSecionItem *allIvarSection = [obj.sectionDictionary objectForKey:@"__objc_ivar"];
for (MethodFileItem *method in allIvarSection.objectsList) {
NSRange range = [method.name rangeOfString:@"." options:NSBackwardsSearch];
if (range.location == NSNotFound) {
continue;
}
//從_開(kāi)始 0x100DF2014 0x00000004 [ 16] _OBJC_IVAR_$_AFSecurityPolicy._pinnedPublicKeys
//0x100DF44BC 0x00000004 [723] _OBJC_IVAR_$_HFDataBaseCore.dbPath
NSString *methodStr = [method.name substringFromIndex:range.location + 1];
if ([methodStr hasPrefix:@"_"]) {
methodStr = [methodStr substringFromIndex:1];
if ([methodStr length] > 1) {
NSString *_methodStr = [NSString stringWithFormat:@"_%@",methodStr];
NSString *setMethod = [NSString stringWithFormat:@"set%@%@:",[methodStr substringToIndex:1].uppercaseString,[methodStr substringFromIndex:1]];
[obj.usedMethod setObject:method forKey:_methodStr];
[obj.usedMethod setObject:method forKey:setMethod];
}
}
[obj.usedMethod setObject:method forKey:methodStr];
}
ObjectSecionItem *allClassSection = [obj.sectionDictionary objectForKey:@"__objc_methname"];
NSMutableDictionary *usedMethod = obj.usedMethod;
for (MethodFileItem *method in allClassSection.objectsList) {
NSString *methodStr = [method.name substringFromIndex:@"literal string: ".length];
if ([usedMethod objectForKey:methodStr]) {
continue;
}
[obj.unUsedMethod setObject:method forKey:methodStr];
MethodFileItem *unUsedMethod = [MethodFileItem new];
unUsedMethod.size = method.size;
unUsedMethod.fileTypeName = method.fileTypeName;
unUsedMethod.startAddress = method.startAddress;
unUsedMethod.name = [NSString stringWithFormat:@"[%@ %@]",obj.name,methodStr];
[self.unUsedSelectorList addObject:unUsedMethod];
}
if ([obj.unUsedMethod count] > 0) {
ObjectSecionItem *unusedSection = [[ObjectSecionItem alloc] init];
unusedSection.name = @"Z__unused_selector";
unusedSection.fileTypeName = @"Custom";
unusedSection.size = [obj.unUsedMethod count];
[unusedSection.objectsList addObjectsFromArray:[obj.unUsedMethod allValues]];
[obj.sectionDictionary setObject:unusedSection forKey:@"Z__unused_selector"];
}
if ([obj.usedMethod count] > 0) {
ObjectSecionItem *usedSection = [[ObjectSecionItem alloc] init];
usedSection.name = @"Z__used_selector";
usedSection.fileTypeName = @"Custom";
usedSection.size = [obj.usedMethod count];
[usedSection.objectsList addObjectsFromArray:[obj.usedMethod allValues]];
[obj.sectionDictionary setObject:usedSection forKey:@"Z__used_selector"];
}
}
}
}
2.3.5 利用otools解析使用到的所有類(lèi)和未使用的類(lèi)
接著和上面的類(lèi)似够庙,解析出來(lái)__objc_selrefs
的內(nèi)容就可以得到所有使用的class, 但是注意的是通過(guò)命令otool -V -s __DATA __objc_classrefs
輸出的是二進(jìn)制的信息。所以需要用otool -V -o <path>
抄邀,輸出OC解析后的內(nèi)容耘眨。找到Contents of (__DATA,__objc_classrefs) section
Contents of (__DATA,__objc_classrefs) section
000000010003cc90 0x10003d348 _OBJC_CLASS_$_AFHTTPSessionManager
000000010003cc98 0x0 _OBJC_CLASS_$_NSDictionary
000000010003cca0 0x0 _OBJC_CLASS_$_UISceneConfiguration
000000010003cca8 0x10003d230 _OBJC_CLASS_$_AppDelegate
000000010003ccb0 0x0 _OBJC_CLASS_$_NSDate
000000010003ccb8 0x0 _OBJC_CLASS_$_NSString
000000010003ccc0 0x0 _OBJC_CLASS_$_NSMutableDictionary
獲取之后先通過(guò)正則解析(.+?)\\s+(.+?)\\s+_OBJC_.+_\\$_(.+)
, 解析出來(lái)起始地址,使用的類(lèi)名比如_OBJC_CLASS_$_AFHTTPSessionManager
境肾。并把他關(guān)聯(lián)到具體使用的拿個(gè)類(lèi)里面剔难。然后利用linkmap
中的__objc_classname
來(lái)做對(duì)比胆屿,還要把繼承的協(xié)議(__objc_protolist
)也作為使用的類(lèi)。 思路和前面的一致钥飞,詳細(xì)請(qǐng)參考具體源碼。
2.5 代碼參考
相關(guān)代碼歡迎fork:https://github.com/dishibolei/iSee.git
主要功能包含了
- 代碼占用分析
- 未使用類(lèi)分析
- 未使用方法分析