iOS崩溃地址和dSYM查找

dSYM

  • 编译时添加选项:DWARF with dSYM File,在编译打包完成之后就会生成调试符号文件(Mach-O文件
  • 文件查找:找到.xcarchive文件→show package contents→...一直到DWARF→工程二进制文件

atos命令

有了dSYM文件,就可以使用atos命令查找到具体代码行出现奔溃信息的地方

atos [-o executable] [-l loadAddress] [-arch architecture] [address ...]
#-arch 选择框架arm64/arm32/x86_64

举例:

atos -arch arm64 -o CallWatch -l 0x0000000100000000 0x1000e51f8

加载地址

从堆栈信息中,无法获取到加载地址(加载地址每次启动APP,都有所不同),可以添加函数

uintptr_t get_load_address(void) {
    const struct mach_header *exe_header = NULL;
    for (uint32_t i = 0; i < _dyld_image_count(); i++) {
        const struct mach_header *header = _dyld_get_image_header(i);
        if (header->filetype == MH_EXECUTE) {
            exe_header = header;
            break;
        }
    }
    
    //返回值即为加载地址
    return (uintptr_t)exe_header;
}

堆栈地址(stack addr) - 加载地址(load addr) = 符号地址(symbol addr)

所以使用查询命令(lookup)时,需要先找到基地址,然后减去该值,才是符号地址;

所以在使用atos时,从堆栈中获取的地址,需要减去基地址,才是符号地址。比如,有一处堆栈信息

stack = "BaseAddress:0x10533a000
    0   CoreFoundation                      0x0000000108f0c113 __exceptionPreprocess + 147
    1   libobjc.A.dylib                     0x000000010f0bcf41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000108f112f2 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x0000000109f21d69 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
    4   UIKit                               0x000000010c179209 -[UITableView _endCellAnimationsWithContext:] + 19416
    5   UIKit                               0x000000010c194075 -[UITableView endUpdates] + 75
    6   XXX                                 0x0000000105dfbca2* -[SMSCollectionViewController openStateTableView:commitEditingStyle:forRowAtIndexPath:] + 866
    7   XXX                                 0x0000000105dfa8bc -[SMSCollectionViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 220
    8   UIKit                               0x000000010c1b8a5f -[UITableView _animateDeletionOfRowAtIndexPath:] + 177
    9   UIKit                               0x000000010c1c1a59 __82-[UITableView _contextualActionForDeletingRowAtIndexPath:usingPresentationValues:]_block_invoke + 59
    10  UIKit                               0x000000010c6fbd67 -[UIContextualAction executeHandlerWithView:completionHandler:] + 174
    11  UIKit                               0x000000010c9e9374 -[UISwipeOccurrence _performSwipeAction:inPullview:swipeInfo:] + 702
    12  UIKit                               0x000000010c9eabd1 -[UISwipeOccurrence swipeActionPullView:tappedAction:] + 112
    13  UIKit                               0x000000010cacded2 -[UISwipeActionPullView _tappedButton:] + 138
    14  UIKit                               0x000000010c056972 -[UIApplication sendAction:to:from:forEvent:] + 83
    15  UIKit                               0x000000010c1d5c3c -[UIControl sendAction:to:forEvent:] + 67
    16  CoreFoundation                      0x0000000108e9036c __invoking___ + 140
    17  CoreFoundation                      0x0000000108e90240 -[NSInvocation invoke] + 320
    18  XXX                                 0x0000000105f232c4 __ASPECTS_ARE_BEING_CALLED__ + 4452
    19  CoreFoundation                      0x0000000108e8ecd8 ___forwarding___ + 760
    20  CoreFoundation                      0x0000000108e8e958 _CF_forwarding_prep_0 + 120
    21  UIKit                               0x000000010c1d5f59 -[UIControl _sendActionsForEvents:withEvent:] + 450
    22  UIKit                               0x000000010c1d4e86 -[UIControl touchesEnded:withEvent:] + 618
    23  UIKit                               0x000000010c646bad _UIGestureEnvironmentSortAndSendDelayedTouches + 5560
    24  UIKit                               0x000000010c640a4d _UIGestureEnvironmentUpdate + 1506
    25  UIKit                               0x000000010c64041f -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
    26  UIKit                               0x000000010c63f4cb -[UIGestureEnvironment _updateGesturesForEvent:window:] + 288
    27  UIKit                               0x000000010c0cdf14 -[UIWindow sendEvent:] + 4102
    28  UIKit                               0x000000010c071365 -[UIApplication sendEvent:] + 352
    29  XXX                                 0x00000001

与XXX有关的奔溃地址:0x0000000105dfbca2,需要减去基地址(BaseAddress:0x10533a000)+偏移地址(Slide Address:)0x100000000=0x100ac1d42, 从而查询命令为:

atos arch arm64 -o CallWatch 0x100ac1d42

或者(UMeng平台里面可直接复制,但是需要在编译版本的电脑里面才能找得到)

export dSYMPath="$(find ~/Library/Developer/Xcode -iname '*.dSYM' -print0 | xargs -0 dwarfdump -u  | grep B76812D2-4F7A-3A7D-89E2-BBA3B20ADA03 | sed -E 's/^[^/]+//' | head -n 1)";
dwarfdump --arch=arm64 --lookup 0x100ac1d42 "$dSYMPath"

你可能感兴趣的:(iOS崩溃地址和dSYM查找)