问题
I wonder if anyone knows how can I trace back beyond dispatch_async when debugging my iOS program. My program constant crash at two spots: in buffer_is_unused, as shown in




回答1:
iOS async stack trace record https://github.com/tiantianbobo/XBAsyncStackTrace
Suppose codes like this when compiled in release version:
- (void)callCrashFunc {
id object = (__bridge id)(void*)1;
[object class];
// NSLog(@"remove this line will cause tail call optimization");
}
- (void)testDispatchAsyncCrash {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self callCrashFunc];
});
}
normal stack trace methods only get you this:
frame #0: 0x000000010186fd85 libobjc.A.dylib`objc_msgSend + 5
frame #1: 0x0000000104166595 libdispatch.dylib`_dispatch_call_block_and_release + 12
frame #2: 0x0000000104167602 libdispatch.dylib`_dispatch_client_callout + 8
frame #3: 0x000000010416a064 libdispatch.dylib`_dispatch_queue_override_invoke + 1028
frame #4: 0x000000010417800a libdispatch.dylib`_dispatch_root_queue_drain + 351
frame #5: 0x00000001041789af libdispatch.dylib`_dispatch_worker_thread2 + 130
frame #6: 0x0000000104553169 libsystem_pthread.dylib`_pthread_wqthread + 1387
frame #7: 0x0000000104552be9 libsystem_pthread.dylib`start_wqthread + 13
But you still don't know where actual code is.
However, XBAsyncStackTrace will give you:
0 XBAsyncStackTraceExample 0x000000010c89d75c blockRecordAsyncTrace + 76
1 XBAsyncStackTraceExample 0x000000010c89d302 wrap_dispatch_async + 98
2 XBAsyncStackTraceExample 0x000000010c89c02c -[ViewController testDispatchAsyncCrash] + 92
3 XBAsyncStackTraceExample 0x000000010c89be3d -[ViewController viewDidLoad] + 269
4 UIKitCore 0x0000000110ae44e1 -[UIViewController loadViewIfRequired] + 1186
5 UIKitCore 0x0000000110ae4940 -[UIViewController view] + 27
Bingo!
来源:https://stackoverflow.com/questions/29811207/how-can-i-trace-back-beyond-dispatch-async-when-debugging-an-ios-program