2013年6月8日土曜日

uncaught exceptionのstackのアドレスをシンボル表示する方法



たまにstackのアドレスだけがありシンボル表示がなく、どこで発生しているエラーかわからないメッセージで終了することがあります。

エラーメッセージの例:
2013-06-08 14:27:49.160 Test[29884:11303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM removeObjectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:

(0x1c90012 0x10cde7e 0x1c321c4 0x20bb 0xacb5b3 0x1c4f376 0x1c4ee06 0x1c36a82 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x11ffc 0x1b4d 0x1a75)

こんな場合にuncaught exceptionの内容を表示する方法が紹介されていました。
Xcode 4.2 debug doesn't symbolicate stack call

次のコードはこのページからの引用です。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
    .....
    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
}


void uncaughtExceptionHandler(NSException *exception) {
    NSLog(@"CRASH: %@", exception);
    NSLog(@"Stack Trace: %@", [exception callStackSymbols]);

}

出力例:
2013-06-08 14:27:49.153 Test[29884:11303] Stack Trace: (
0   CoreFoundation                      0x01c9002e __exceptionPreprocess + 206
1   libobjc.A.dylib                     0x010cde7e objc_exception_throw + 44
2   CoreFoundation                      0x01c321c4 -[__NSArrayM removeObjectAtIndex:] + 212
3   Test                                0x000020bb -[MyViewController remove] + 91
4   Foundation                          0x00acb5b3 __NSFireDelayedPerform + 380
.....
)

エラーによっては手がかりになる情報も含まれてきます。仕込んでおいて損はないでしょう。

追記
swiftの場合
        NSSetUncaughtExceptionHandler{ exception in
            print(exception)
            print(exception.callStackSymbols)

        }
参照リンク:

How should I use NSSetUncaughtExceptionHandler in Swift


0 件のコメント: