Efficient Exception Debugging with Xcode 4

When dealing with Objective-C exceptions while developing Cocoa applications, you might not know where the error happened. Calling -[NSMutableDictionary setObject:forKey:] with an nil object will output an error message in the console (*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: myKey)) but the application doesn’t crash. Unlike in iOS, the program execution doesn’t stop and you don’t get a stack trace.

You can tell Xcode to stop executing the application on exceptions by setting an Exception Breakpoint in the Breakpoint Navigator (Command-6).

AddExceptionBreakpoint ExceptionBreakpointPopover

When your application hits an exception Xcode will stop the execution, but you might not know what kind of exception was thrown.

ExceptionSource

After reading Apple’s Technical Note TN2124 “Mac OS X Debugging Magic”, I found a solution. In Some Assembly Required you can find the register addresses of the function return values, which will be the NSException object. Now all you have to do is to configure an action to print the exception to the console. In case of Intel 64-bit, the command would be po $rax, for Intel 32-bit it’s po $eax.

ExceptionBreakpointWithDebuggerCommand ExceptionMessage

This entry was posted in Apple, Developer, Mac and tagged , , , . Bookmark the permalink.

Comments are closed.