Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symbolic exception breakpoint on -[NSRangeException raise]

Adding a symbolic breakpoint in Xcode gives you an example template of -[NSException raise]. I want to do the same thing but specifically on -[NSRangeException raise]. The reason being that I want to breakpoint only on specific array bounds exceptions, for example:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 31 beyond bounds [0 .. 30]'

Yes, I know that I can add a catch all exception breakpoint. However, I do not want to do that as I do not want to breakpoint on the many exceptions generated throughout using the app.

I've tried quite a few things based on other posts related to NSException. However, I have not found anything that works successfully as yet.

For example I tried:

enter image description here

This journal entry was also of interest albeit inconclusive:

Dealing with NSArray "out of bounds"

So, basically, the question is, in Xcode, how to breakpoint specifically and only on a range exception?

like image 240
Max MacLeod Avatar asked Feb 15 '23 03:02

Max MacLeod


1 Answers

NSRangeException isn't a class:

grep NSRangeException /System/Library/Frameworks/Foundation.framework/Headers/NSException.h FOUNDATION_EXPORT NSString * const NSRangeException;

Turns out, range exceptions are just NSExceptions whose name is NSRangeException, i.e.:

(lldb) b s -n objc_exception_throw
Breakpoint 2: where = libobjc.A.dylib`objc_exception_throw, address = 0x00007fff8e3c2e4a
(lldb) c
Process 58216 resuming
Process 58216 stopped
* thread #1: tid = 0x1d7f4b, function: objc_exception_throw , stop reason = breakpoint 2.1
    frame #0: 0x00007fff8e3c2e4a libobjc.A.dylib`objc_exception_throw
libobjc.A.dylib`objc_exception_throw:
-> 0x7fff8e3c2e4a:  pushq  %rbp
   0x7fff8e3c2e4b:  movq   %rsp, %rbp
   0x7fff8e3c2e4e:  pushq  %r15
   0x7fff8e3c2e50:  pushq  %r14
(lldb) bt 
* thread #1: tid = 0x1d7f4b, function: objc_exception_throw , stop reason = breakpoint 2.1
    frame #0: 0x00007fff8e3c2e4a libobjc.A.dylib`objc_exception_throw
    frame #1: 0x00007fff841ca1df CoreFoundation`-[__NSArrayI objectAtIndex:]
    frame #2: 0x0000000100000eb9 range-exception`-[Foo throwIt] at range-exception.m:14
    frame #3: 0x0000000100000f27 range-exception`main at range-exception.m:22
    frame #4: 0x00007fff8468d5fd libdyld.dylib`start
    frame #5: 0x00007fff8468d5fd libdyld.dylib`start
(lldb) expr (NSString *) [((NSException *) $arg1) name]
(NSString *) $0 = 0x00007fff74177990 @"NSRangeException"

So you could set a breakpoint on objc_exception_throw, and write a breakpoint condition comparing the name to NSRangeException. Something like:

[(NSString *) [((NSException *) $arg1) name] isEqual: (NSString *) NSRangeException]

should do the trick.

like image 146
Jim Ingham Avatar answered Mar 04 '23 03:03

Jim Ingham