简体   繁体   中英

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!

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

异常BreakPoint

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.

在catch块中设置断点,一旦代码流停止,就可以使用像'bt'这样的gdb命令。

Print the stack trace stored in the exception, ie [exception callStackSymbols] or [exception callStackReturnAddresses] . In Apple's crash logs since iOS 5, this shows up at the top as "Last Exception Backtrace".

What you're seeing is the call stack when the exception is re-thrown, which will happen for things as simple as try...finally . I'm not sure exactly why Apple made this change (possibly to make run loops exception-safe?), but there you go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM