简体   繁体   English

在C ++异常中获取堆栈跟踪

[英]Getting a stack trace in C++ exceptions

The fact a stack/function trace is supplied in Java/C# exceptions is really useful. 在Java / C#异常中提供堆栈/函数跟踪的事实非常有用。 Is there a handy way to do this in C++ or would I have to bake extra data into every method/function? 有没有方便的方法在C ++中执行此操作,还是我必须将额外的数据烘焙到每个方法/函数中?

Most debuggers can be set up to pause your program each time an exception is thrown (either any exception or an exception for which there's no handler) so that you can obserevr a call stack. 大多数调试器都可以设置为每次抛出异常时暂停程序(任何异常或没有处理程序的异常),以便您可以监视调用堆栈。

Also if your code only throws exceptions of classes you control you can put code for dumping the call stack in those classes constructors . 此外,如果您的代码仅抛出您控制的类的异常,则可以将代码转储到这些类构造函数中的调用堆栈

If you are considering adding metdata to your exceptions, You may consider using boost exception handling . 如果您正在考虑将metdata添加到异常中,可以考虑使用boost异常处理 boost::exception allows adding information to an exception after it has been thrown. boost :: exception允许在抛出异常后向其添加信息。

Some OS APIs provide for stack traces, I know that the Windows API has StackWalk64 or something like that that can do stack tracing. 一些OS API提供了堆栈跟踪,我知道Windows API具有StackWalk64或类似的东西可以执行堆栈跟踪。

However, if you can't depend on such a thing, then pretty much all you can do is either ship a debug build, or do it manually. 但是,如果你不能依赖这样的东西,那么你可以做的就是运送调试版本,或者手动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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