简体   繁体   English

iOS - 调用dispatch_async时没有堆栈跟踪

[英]iOS - No stack trace when calling dispatch_async

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 调试器只在main.m停止并突出显示该行

int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([FantasyUniversalAppDelegate class]));

If I remove the dispatch_async and just call handler(); 如果我删除dispatch_async并只调用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). 现在我正在使用Flurry来向我展示崩溃,但它显示的崩溃并不是很有用,因为我得到了一个非描述性的堆栈跟踪(象征性的)。 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 iOS异步堆栈跟踪记录 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: 但是,XBAsyncStackTrace将为您提供:

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! 答对了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM