Debugging iPhone Exceptions

Hunting down crashes in an iPhone app often involves uncaught exceptions. Even if we’re running from the debugger at the time, the crash happens after the exception is thrown, so the only entries shown in the debugger’s stack are from the exception catching code.

We do get a printout of the stack at the time of the exception but it’s a numeric stack trace so we’ll need to use the atos command line utility to make sense of the numbers. Here’s an example of a crash in a development build of Tomatoes:

2008-08-01 21:10:20.138 Tomatoes[151:20b] *** Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘*** -[NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds’
2008-08-01 21:10:20.213 Tomatoes[151:20b] Stack: (
808163835,
806099672,
807978623,
807978527,
812533155,
812164939,
812716685,
22067,
35325,
812317455,
812317325,
824032771,
824024301,
824022997,
807779567,
807777675,
829004012,
816177936,
816214500,
8381,
8244
)
terminate called after throwing an instance of ‘NSException’
kill
quit

We’ll need to call atos for each stack entry and we’ll need to know the path to the app on disk: In Xcode right click on the app under Products and select ‘Reveal in Finder’, then right click on the app in the Finder and select ‘Show Package Contents’, find the executable and drag it to the Terminal window to get the path.

The entries in the stack that belong to our app will be have smaller values. In this case 22067 and 35325. So let’s see what we have:

atos -o /Users/pboctor/iphone/Tomatoes/build/Debug-iphonesimulator/Tomatoes.app/Tomatoes 22067
-[NSObject(MovieUtils) stripHTML:] (in Tomatoes) (MovieUtils.m:55)

atos -o /Users/pboctor/iphone/Tomatoes/build/Debug-iphonesimulator/Tomatoes.app/Tomatoes 35325
-[RottenCurrentMoviesTableController connectionDidFinishLoading:] (in Tomatoes) (RottenCurrentMoviesTableController.m:482)

It looks like after we’re done downloading some data, we call stripHTML and then crash while trying to replace some values in a string. Off to fix some code!

One final thing: If the numeric stack trace came from the app while running on the iPhone and not in the simulator, then we need to tell atos the proper architecture of the executable:

atos -o /Users/pboctor/iphone/Tomatoes/build/Debug-iphoneos/Tomatoes.app/Tomatoes -arch armv6 22067

Happy bug fixing.

One Response to “Debugging iPhone Exceptions”

  1. Rama says:

    I just published “ratos”, an open source shell that you can start in a terminal and past an entire Xcode stack trace and it will decode it. Speeds up using atos.

    Announcement/Doc is here: http://myutil.com/2009/2/6/announcing-ratos-a-shell-to-decode-iphone-stack-traces

    And you can go straight to the code here: http://github.com/face/ratos/tree/master

    -Rama

Leave a Reply