如何獲取APP及其動態庫的UUID
LC_UUID 一般簡稱為 UUID,是用來標示 Mach-O 文件的,做過崩潰堆棧符號化還原的同學應該都知道有 UUID 這個東西,你在進行符號解析的時候,就需要找到與系統庫和你 APP 的 UUID 相同的 dSYM 文件來進行堆棧地址還原。
獲取 dSYM 文件的 UUID 比較簡單,隨便用一個工具就能查看 UUID,那么如何獲取 APP 及其動態庫的 UUID 呢?
$ xcrun dwarfdump --uuid UUID: E73A4300-F6E5-3124-98DF-1578B8D4F96A (armv7) GYMonitorExample.app.dSYM/Contents/Resources/DWARF/GYMonitorExampleUUID: 44E27054-508E-37EF-9296-44400C5F19E1 (arm64) GYMonitorExample.app.dSYM/Contents/Resources/DWARF/GYMonitorExample
獲取 APP 的 UUID
當初想只獲取 APP 的 dSYM 文件的 UUID 和堆棧發生時對應設備的 APP UUID,所以直接 Google 一搜就有答案:https://stackoverflow.com/questions/10119700/how-to-get-mach-o-uuid-of-a-running-process
#import NSString *executableUUID(){ const uint8_t *command = (const uint8_t *)(&_mh_execute_header + 1); for (uint32_t idx = 0; idx 《 _mh_execute_header.ncmds; ++idx) { if (((const struct load_command *)command)-》cmd == LC_UUID) { command += sizeof(struct load_command);return [NSString stringWithFormat:@“XXXX-XX-XX-XX-XXXXXX”, command[0],command[1], command[2], command[3], command[4], command[5],command[6], command[7], command[8], command[9], command[10],command[11], command[12], command[13], command[14], command[15]]; }else { command += ((const struct load_command *)command)-》cmdsize; } }return nil;}
把上述方法放在 AppDelegate 中進行測試,測試結果完全正確,喜出望外。上述代碼的大概意思是獲取 MH_EXECUTE (可執行的主 image )文件的 Load Command,并且利用 For 循環遍歷所有的 Load Command,找到類型為 LC_UUID 的 Load Command,進而獲取 UUID。
在 Pod 中獲取 APP 的 UUID
因為崩潰采集是在一個獨立的庫中進行的,在崩潰時想要采集 UUID 的話也應該在當前庫中獲取 UUID,因為 Pod 使用了 use_frameworks ,所以問題就變成了如何在一個動態庫中獲取 APP 的 UUID,靜態庫會把代碼復制到主 APP 中,而動態庫是一個獨立的 Mach-O 文件。把上面代碼直接丟在 Pod 中使用是行不通的,因為 _mh_execute_header 在 MH_DYLIB 中無法使用。
可以獲取主 image 文件的路徑,然后根據路徑去獲取 image 的 index,然后根據這個 index 去獲取對應 image 的header,通過 header 找到 image 的 Load Commands,遍歷找到類型為 LC_UUID 的 Load Command 即可獲取 UUID。下面給出一部分代碼,這個其實相當于第三個例子的一部分,所以可以從第三個中提煉出這部分代碼。
// 獲取主 image 的路徑staticNSString* getExecutablePath(){ NSBundle* mainBundle = [NSBundle mainBundle]; NSDictionary* infoDict = [mainBundle infoDictionary]; NSString* bundlePath = [mainBundle bundlePath]; NSString* executableName = infoDict[@“CFBundleExecutable”]; return [bundlePath stringByAppendingPathComponent:executableName];}// 獲取 image 的 indexconst uint32_t imageCount = _dyld_image_count();for(uint32_t iImg = 0; iImg 《 imageCount; iImg++) { constchar* name = _dyld_get_image_name(iImg); if (name == getExecutablePath()) returniImg;}// 根據 index 獲取 headerconst struct mach_header* header = _dyld_get_image_header(iImg);// 獲取 LoadCommandstatic uintptr_t firstCmdAfterHeader(const struct mach_header* const header) { switch(header-》magic) { caseMH_MAGIC: caseMH_CIGAM: return(uintptr_t)(header + 1); caseMH_MAGIC_64: caseMH_CIGAM_64: return(uintptr_t)(((struct mach_header_64*)header) + 1); default: // Headeris corrupt return0; }}// 遍歷 LoadCommand即可
從上面代碼中可以發現App image 的路徑是在 mainBundle 中的,其實我們所依賴的自己的動態庫也都在這個路徑下,同時由于 Swift ABI 不穩定,它所依賴的系統動態庫打包的時候也會放在這個路徑之下,有興趣的可以測試下。當然,如果你想,你也可以通過這種方式獲取 APP 以及自己動態庫的 UUID。
如何獲取所有 Mach-O 的 UUID
當我們寫完一個 APP,打包上架后,如果遇到崩潰就需要收集堆棧信息進行符號化還原,這時候每個動態庫的 UUID 我們都需要,系統庫的 UUID 也是需要的,這樣可以提供給更多的信息,有利于我們迅速排查問題。如何獲取 APP 以及所有動態庫的 UUID 呢?
其實也很簡單,就是獲取到 APP 中所有的 image count,然后一個個遍歷獲取header、Load Command,進而找到所有 Mach-O 的 UUID,這里直接上代碼
//// LDAPMUUIDTool.m// Pods//// Created by wangjiale on2017/9/7.////#import “LDAPMUUIDTool.h”#import #include #include #include #include static NSMutableArray *_UUIDRecordArray;@implementation LDAPMUUIDTool+ (NSDictionary *)getUUIDDictionary { NSDictionary *uuidDic = [[NSDictionary alloc] init]; int imageCount = (int)_dyld_image_count(); for(int iImg = 0; iImg 《 imageCount; iImg++) { JYGetBinaryImage(iImg); } returnuuidDic;}// 獲取 Load Command, 會根據 header 的 magic 來判斷是 64 位 還是 32 位static uintptr_t firstCmdAfterHeader(const struct mach_header* const header) { switch(header-》magic) { case MH_MAGIC: case MH_CIGAM:return (uintptr_t)(header + 1); case MH_MAGIC_64: case MH_CIGAM_64:return (uintptr_t)(((struct mach_header_64*)header) + 1); default:return0; }}bool JYGetBinaryImage(int index) { const struct mach_header* header = _dyld_get_image_header((unsigned)index);if(header == NULL) { returnfalse; } uintptr_t cmdPtr = firstCmdAfterHeader(header); if(cmdPtr == 0) { returnfalse; } uint8_t* uuid = NULL; for(uint32_t iCmd = 0; iCmd 《 header-》ncmds; iCmd++) { struct load_command* loadCmd = (struct load_command*)cmdPtr;if (loadCmd-》cmd == LC_UUID) { struct uuid_command* uuidCmd = (struct uuid_command*)cmdPtr; uuid = uuidCmd-》uuid; break; } cmdPtr += loadCmd-》cmdsize; } const char* path = _dyld_get_image_name((unsigned)index); NSString *imagePath = [NSString stringWithUTF8String:path]; NSArray *array = [imagePath componentsSeparatedByString:@“/”]; NSString *imageName =array[array.count - 1]; NSLog(@“buffer-》name:%@”,imageName); const char* result = nil; if(uuid != NULL) { result = uuidBytesToString(uuid); NSString *lduuid = [NSString stringWithUTF8String:result]; NSLog(@“buffer-》uuid:%@”,lduuid); }returntrue;}static const char* uuidBytesToString(const uint8_t* uuidBytes) { CFUUIDRef uuidRef = CFUUIDCreateFromUUIDBytes(NULL, *((CFUUIDBytes*)uuidBytes)); NSString* str = (__bridge_transfer NSString*)CFUUIDCreateString(NULL, uuidRef); CFRelease(uuidRef);return cString(str);}const char* cString(NSString* str) { return str == NULL ? NULL : strdup(str.UTF8String);}@end
非常好我支持^.^
(0) 0%
不好我反對
(0) 0%
下載地址
如何獲取APP及其動態庫的UUID下載
相關電子資料下載
- iOS17.1可能明天發布,iOS17.1主要修復哪些問題? 376
- 華為全新鴻蒙蓄勢待發 僅支持鴻蒙內核和鴻蒙系統應用 719
- 蘋果手機系統iOS 17遭用戶質疑 731
- iPhone12輻射超標?蘋果推送iOS 17.1解決此事 750
- 傳華為囤積零部件 目標明年智能手機出貨7000萬部;消息稱 MiOS 僅限國內,小米 28208
- 蘋果推送iOS17.0.3,解決iPhone15Pro系列存在機身過熱 216
- Testin云測兼容和真機服務平臺中上線iPhone 15系列手機 208
- 利爾達推出搭載HooRiiOS的Matter模組 145
- 運放參數解析:輸入偏置電流(Ibias)和失調電流(Ios) 128
- 昆侖太科發布支持國產飛騰騰銳D2000芯片的開源BIOS固件版本 448