iOS 如何获取 Mach-O 的 UUID

LC_UUID 通常简称为 UUID,是用来标示 Mach-O 文件的,作过崩溃堆栈符号化还原的同窗应该都知道有 UUID 这个东西,你在进行符号解析的时候,就须要找到与系统库和你 APP 的 UUID 相同的 dSYM 文件来进行堆栈地址还原。bash

获取 dSYM 文件的 UUID 比较简单,随便用一个工具就能查看 UUID,那么如何获取 APP 及其动态库的 UUID 呢?app

$ xcrun dwarfdump --uuid <PATH_TO_APP_EXECUTABLE>

UUID: E73A4300-F6E5-3124-98DF-1578B8D4F96A (armv7) GYMonitorExample.app.dSYM/Contents/Resources/DWARF/GYMonitorExample
UUID: 44E27054-508E-37EF-9296-44400C5F19E1 (arm64) GYMonitorExample.app.dSYM/Contents/Resources/DWARF/GYMonitorExample复制代码

获取 APP 的 UUID

当初想只获取 APP 的 dSYM 文件的 UUID 和堆栈发生时对应设备的 APP UUID,因此直接 Google 一搜就有答案:stackoverflow.com/questions/1…工具

#import <mach-o/ldsyms.h>

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:@"%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
                    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 中没法使用。ui

能够获取主 image 文件的路径,而后根据路径去获取 image 的 index,而后根据这个 index 去获取对应 image 的header,经过 header 找到 image 的 Load Commands,遍历找到类型为 LC_UUID 的 Load Command 便可获取 UUID。下面给出一部分代码,这个其实至关于第三个例子的一部分,因此能够从第三个中提炼出这部分代码。spa

// 获取主 image 的路径
static NSString* getExecutablePath()
{
    NSBundle* mainBundle = [NSBundle mainBundle];
    NSDictionary* infoDict = [mainBundle infoDictionary];
    NSString* bundlePath = [mainBundle bundlePath];
    NSString* executableName = infoDict[@"CFBundleExecutable"];
    return [bundlePath stringByAppendingPathComponent:executableName];
}
// 获取 image 的 index
const uint32_t imageCount = _dyld_image_count();

for(uint32_t iImg = 0; iImg < imageCount; iImg++) {
    const char* name = _dyld_get_image_name(iImg);
    if (name == getExecutablePath()) return iImg;
}

// 根据 index 获取 header
const struct mach_header* header = _dyld_get_image_header(iImg);

// 获取 Load Command
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:
            // Header is corrupt
            return 0;
    }
}
// 遍历 Load Command便可复制代码

从上面代码中能够发现App image 的路径是在 mainBundle 中的,其实咱们所依赖的本身的动态库也都在这个路径下,同时因为 Swift ABI 不稳定,它所依赖的系统动态库打包的时候也会放在这个路径之下,有兴趣的能够测试下。固然,若是你想,你也能够经过这种方式获取 APP 以及本身动态库的 UUID。code

如何获取全部 Mach-O 的 UUID

当咱们写完一个 APP,打包上架后,若是遇到崩溃就须要收集堆栈信息进行符号化还原,这时候每一个动态库的 UUID 咱们都须要,系统库的 UUID 也是须要的,这样能够提供给更多的信息,有利于咱们迅速排查问题。如何获取 APP 以及全部动态库的 UUID 呢?component

其实也很简单,就是获取到 APP 中全部的 image count,而后一个个遍历获取header、Load Command,进而找到全部 Mach-O 的 UUID,这里直接上代码orm

//
//  LDAPMUUIDTool.m
//  Pods
//
//  Created by wangjiale on 2017/9/7.
//
//

#import "LDAPMUUIDTool.h"
#import <mach-o/ldsyms.h>
#include <limits.h>
#include <mach-o/dyld.h>
#include <mach-o/nlist.h>
#include <string.h>

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);
    }

    return uuidDic;
}
// 获取 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:
            return 0;
    }
}

bool JYGetBinaryImage(int index) {
    const struct mach_header* header = _dyld_get_image_header((unsigned)index);
    if(header == NULL) {
        return false;
    }

    uintptr_t cmdPtr = firstCmdAfterHeader(header);
    if(cmdPtr == 0) {
        return false;
    }

    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);
    }

    return true;
}
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复制代码
相关文章
相关标签/搜索