I've created some example code to demonstrate my problem.
- (void) test {
void (^handler)(void) = ^ {
NSArray *test = [NSArray array];
[test objectAtIndex: 5];
};
handler = [handler copy];
dispatch_async(dispatch_get_main_queue(), handler);
}
When I call the test method I don't get a stack trace. The debugger just stops at main.m and has this line highlighted
int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([FantasyUniversalAppDelegate class]));
If I remove the dispatch_async and just call handler(); then I do get a stack trace. Can some one explain why this is and is there a way to get a more descriptive stack trace?
Right now I am using Flurry to show me crashes but the crashes it is showing me isn't very useful because I get a very non-descriptive stack trace (which is symbolicated). It looks like this:
Thread: Unknown Name
0 libsystem_kernel.dylib 0x3a224eb4 mach_msg_trap + 20
1 CoreFoundation 0x32067045 __CFRunLoopServiceMachPort + 129
2 CoreFoundation 0x32065d5f __CFRunLoopRun + 815
3 CoreFoundation 0x31fd8ebd CFRunLoopRunSpecific + 357
4 CoreFoundation 0x31fd8d49 CFRunLoopRunInMode + 105
5 GraphicsServices 0x35b9c2eb GSEventRunModal + 75
6 UIKit 0x33eee301 UIApplicationMain + 1121
7 IOSTestApp 0x00025d7b main (main.m:14)
You could wrap your code in a
@try {
NSArray *test = [NSArray array];
[test objectAtIndex: 5];
}
@catch {
// Get the stack track for the current thread
NSArray *stacktrace = [NSThread callStackSymbols];
// Do something with the symbols
}
all within the block that you are dispatching. Though I'm sure there is a much nicer way!
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.