iOS Tip—Break into the debugger

Sometimes, when debugging edge cases, it’s useful to have the debugger break at an arbitrary point without having to set a breakpoint and step through the code. It could be that you have a large array of items and only one is causing an issue you need to resolve. Rather than stepping through each item in the array, it’s more efficient for your workflow if the debugger were to break only when a certain condition is met. For example:

if ([myArray objectForKey:@"identifier"] isEqualToString:@"myID"]) {
    DebugBreak(); // Step through the code from here

The macro below will allow you to write code like the example above. It will work on both real iOS hardware and in the iOS simulator:

    #define DebugBreak() __asm__ __volatile__ ("mov r0, %0\nmov r1, %1\nmov r12, #37\nswi 128\n" \
                                            : : "r" (getpid ()), "r" (signal) : "r12", "r0", "r1", "cc")
#elif TARGET_CPU_X86
    #define DebugBreak() __asm__("int $3\n" : : )

Anything that makes debugging easier and more efficient is a good thing in my book.