Selectively ignoring Objective-C exceptions in Xcode

Edit – Octorber 9, 2013: Updated the script to support iOS Simulator (i386) and iPhone 5S (ARM 64bit).

Xcode has the ability to break on all Objective-C exceptions (Debug > Breakpoints > Create Exception Breakpoint...), which is extremely useful as you can see the exception before the stack unwinds.

However, enabling this option will cause Xcode to break whenever an exception occurs (even when inside a @[email protected] block), making it hard to track down the exception you’re actually after when you’re working with exception-heavy frameworks like CoreData or Accessibility. Also, Cocoa frameworks sometimes throws exceptions internally, which can be confusing as you wouldn’t see an exception at your code otherwise.

This was annoying me today while I was working on Shortcat as Accessibility really likes throwing exceptions, so I wrote a lldb script that lets you easily specify what exceptions to ignore based on any selector on NSException. I based it off Rob Mayoff’s sniff_objc_exception_throw script as there wasn’t really much in way of lldb documentation.

Setup

  • Grab the script from here
  • Put this script somewhere (eg. ~/Library/lldb/ignore_specified_objc_exceptions.py)
  • Add this to your ~/.lldbinit: command script import <path to ignore_specified_objc_exceptions.py>

But I’m lazy!

for-the-lazy.sh
1
2
3
mkdir -p ~/Library/lldb
curl -L https://gist.github.com/chendo/6759305/raw/ignore_specified_objc_exceptions.py > ~/Library/lldb/ignore_specified_objc_exceptions.py
echo "command script import ~/Library/lldb/ignore_specified_objc_exceptions.py" >> ~/.lldbinit

Usage

  • In Xcode, add a breakpoint to catch all Objective-C exceptions
  • Edit the breakpoint and add a Debugger Command with the following command: ignore_specified_objc_exceptions name:NSAccessibilityException className:NSSomeException
  • This will ignore exceptions where NSException -name matches NSAccessibilityException OR -className matches NSSomeException

It should look something like this:

Screenshot

Let me know if you have any issues!

Comments