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

谁都会走 提交于 2019-12-03 02:29:50

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

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.

Set a breakpoint in the catch block, once the flow of code stops you can use gdb commands like 'bt'.

Print the stack trace stored in the exception, i.e. [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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!