Visual C++:断言失败时的调用堆栈
在Release模式下,当assert
失败时,是否可以输出调用堆栈?
当 assert
在调试模式下失败时,我可以在 Visual Studio 中看到调用堆栈。我已经编译了 Release 模式,并删除了 NDEBUG
,以便编译断言。当此断言失败时,断言参数将打印到控制台上。如果我也能以某种方式获得调用堆栈的输出,那将非常有帮助。
Is it possible to output the call stack when an assert
fails in Release mode?
I can see the call stack inside Visual Studio when an assert
fails in Debug mode. I have compiled Release mode with NDEBUG
removed so that assertions are compiled. When this assert
fails, the assert parameters are printed onto the console. It would be very helpful if I can somehow get an output of the call stack too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两个函数应该有所帮助: CaptureStackBackTrace 和 StackWalk64 (需要 Dbghelp.dll ,这是示例)。
无论如何,它们都只返回一个返回地址列表,因此必须手动将它们解析为函数名称。您可以设置 VS 生成一个 MAP 文件,其中包含可执行文件中所有函数的地址。这是操作方法。
另外,不要忘记某些函数可能(并且将会)被内联。因此,当使用
A ->; 代替时,请不要感到惊讶。 B-> C
你会看到A -> C
调用大头针。There are two functions that should help: CaptureStackBackTrace and StackWalk64 (requires Dbghelp.dll, here's an example).
Anyhow, both of them return you only a list of return addresses, so have to resolve them into function names manually. You can set VS to generate a MAP file with addresses of all functions in you executable. Here's how.
Also, don't forget that some functions may (and will) be inlined. So don't be surprised when instead of
A -> B -> C
you will seeA -> C
calls tack.