xcode Objective C - 从调试器错误中获取行号或完整堆栈跟踪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10501358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C - getting line number or full stack trace from debugger error?
提问by Kevin
Is it possible to get a line number for the source code (or anything that helps debug where the problem is) from the debugger, that shows where the problem is originating?
是否可以从调试器中获取源代码(或任何有助于调试问题所在的任何内容)的行号,以显示问题的来源?
I am getting an error:
我收到一个错误:
-[NSCFArray objectAtIndex:]: index (-1 (or possibly larger)) beyond bounds (9)
which obviously means that I am going out of bounds at some point, however if it possible I would like to get some more information to help me solve the problem.
这显然意味着我在某个时候会越界,但是如果可能的话,我想获得更多信息来帮助我解决问题。
I am placing a breakpoint and trying to go through the program line by line but it is a tedious process. Thanks!
我正在放置一个断点并尝试逐行执行程序,但这是一个乏味的过程。谢谢!
回答by Mike Weller
When the debugger stops, go to the "Debug Navigator" and make sure the slider on the bottom is all the way to the right.
当调试器停止时,转到“调试导航器”并确保底部的滑块一直向右。
Scan your eye down from the point at which the exception is thrown and you should eventually come to your own code. Click on the appropriate method/function name and the code will be opened in the editor.
从抛出异常的点向下扫描你的眼睛,你最终应该会看到你自己的代码。单击适当的方法/函数名称,代码将在编辑器中打开。
If you don't see any of your own methods in the stack trace, the exception may have been passed through a performSelector
-style call in which case the stack trace is gone. If this is the case, you may get better information by adding an "On Throw" exception break point. First switch to the "Breakpoint navigator":
如果在堆栈跟踪中没有看到您自己的任何方法,则异常可能已通过performSelector
- 样式调用传递,在这种情况下堆栈跟踪消失了。如果是这种情况,您可以通过添加“On Throw”异常断点来获得更好的信息。首先切换到“断点导航器”:
Then click on the plus and choose "Add Exception breakpoint..."
然后单击加号并选择“添加异常断点...”
Create an "On Throw" break point:
创建一个“On Throw”断点:
This will stop the debugger at the exact point the exception is thrown, and you get a better stack trace. It's a good idea to have an exception break point like this enabled all the time, although you will occasionally get internal exceptions from Apple code (e.g. when using QLPreviewController, MPMoviePlayerController).
这将在抛出异常的确切位置停止调试器,并且您将获得更好的堆栈跟踪。始终启用这样的异常断点是个好主意,尽管您偶尔会从 Apple 代码中获得内部异常(例如,在使用 QLPreviewController、MPMoviePlayerController 时)。
回答by Dávid Kaszás
You should also consider using the NSSetUncaughtExceptionHandler. (You can save the crash log to the disk, check next startup if a new crash log was saved, attach it to an email, etc.)
您还应该考虑使用 NSSetUncaughtExceptionHandler。(您可以将崩溃日志保存到磁盘,检查下次启动是否保存了新的崩溃日志,将其附加到电子邮件等)
put this into your didFinishLaunchingWithOptions method:
把它放到你的 didFinishLaunchingWithOptions 方法中:
NSSetUncaughtExceptionHandler(&exceptionHandler);
and implement your exception handler:
并实现您的异常处理程序:
void exceptionHandler(NSException *exception)
{
NSLog(@"%@",[exception name]);
NSLog(@"%@",[exception reason]);
NSLog(@"%@",[exception userInfo]);
NSLog(@"%@",[exception callStackSymbols]);
NSLog(@"%@",[exception callStackReturnAddresses]);
}