您好,欢迎来电子发烧友网! ,新用户?[免费注册]

您的位置:电子发烧友网>源码下载>通讯/手机编程>

如何获取APP及其动态库的UUID

大小:0.3 MB 人气: 2017-09-25 需要积分:1

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%

      发表评论

      用户评论
      评价:好评中评差评

      发表评论,获取积分! 请遵守相关规定!