Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get source code line from stack trace in obj-c / ios

I use NSSetUncaughtExceptionHandler to print the stack trace to local file in iPhone, which will be sent to our server next time the app launches. Then I can examine the exception data and fix the bug. In some crashes I have the module name and the function that threw the exception, these are easy. But mostly I have something like this:

"4   libc++abi.dylib 0x35bba3c5 _ZL19safe_handler_callerPFvvE + 76",
"5   libc++abi.dylib 0x35bba451 _ZdlPv + 0",
"6   libc++abi.dylib 0x35bbb825 __cxa_current_exception_type + 0",
"7   libobjc.A.dylib 0x37bab2a9 objc_exception_rethrow + 12",
"8   CoreFoundation  0x3575a50d CFRunLoopRunSpecific + 404"

and for example the reason:

*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

but I have dozens of arrays in my app, so I need help to find the specific line that threw the exception, using the data I get from the stack trace.

Does anyone know a good article/tutorial from Apple or other, where I can learn to decode the numbers in the stack trace to find the problematic line in the source code. Thanks in advance!

like image 376
Lena Grushevsky Avatar asked Aug 27 '12 12:08

Lena Grushevsky


2 Answers

I strongly recommend enabling Exception Breakpoint in Xcode. It will stop execution of your code on the exact line which crashes your application. So you do not need to worry about which of the array cause of the crash. *** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

Adding exception breakpoint

  1. Go to Breakpoints section on Xcode
  2. Click on plus sing at bottom of the section
  3. Select Add Exception Breakpoint

Exception BreakPoint

like image 193
bpolat Avatar answered Nov 18 '22 03:11

bpolat


I don't know how to get line numbers from stack traces (yet), but at some points in my code where I want to get line number printed I used the have the following code fragment:

NSLog(@"%s line=%d", __func__, __LINE__);

which will give the following output:

2013-04-01 00:16:46.393 MyApp[847:c07] -[AppDelegate application:didFinishLaunchingWithOptions:] line=29

If you're familiar with the Log4J framework I'ld suggest to take a look at the Lumberjack framework which proves to be very helpful for me in various projects.

https://github.com/robbiehanson/CocoaLumberjack

Although this may not directly answer your question, its just meant as a reminder.

like image 2
iOS-Coder Avatar answered Nov 18 '22 04:11

iOS-Coder