简体   繁体   English

如何在 VS2017 中使用 C++ 获取异常跟踪堆栈

[英]How to get exception trace stack with C++ in VS2017

When running into an exception, there is only a break point on the line that throws the exception, since it's a very wildly used method in the library, I want to know the exact call stack of the breaking exception, could do that very easily with other languages like Java/Python etc...当遇到异常时,在抛出异常的行上只有一个断点,因为它是库中使用非常广泛的方法,我想知道中断异常的确切调用堆栈,可以很容易地做到这一点其他语言,如 Java/Python 等...

How to do that with VS2017 C++ ?如何使用 VS2017 C++ 做到这一点? Or is there a way to do that?或者有没有办法做到这一点?

在此处输入图片说明

While not a direct answer to your question, I want to note that you can get nice backtraces in a cross-platform way, using only standard C++11 and without the need for a debugger or cumbersome logging:虽然不是您问题的直接答案,但我想指出,您可以以跨平台的方式获得很好的回溯,仅使用标准 C++11 ,无需调试器或繁琐的日志记录:

Use std::nested_exception and std::throw_with_nested使用std::nested_exceptionstd::throw_with_nested

It is described on StackOverflow here and here , how you can get a backtrace on your exceptions inside your code by simply writing a proper exception handler which will rethrow nested exceptions.此处此处的StackOverflow 上进行描述,您可以通过简单地编写将重新抛出嵌套异常的适当异常处理程序来获取代码中异常的回溯 It will, however, require that you insert try/catch statements at the functions you wish to trace.但是,它要求您在要跟踪的函数中插入try/catch语句。

Since you can do this with any derived exception class, you can add a lot of information to such a backtrace!由于您可以使用任何派生的异常类来执行此操作,因此您可以向此类回溯添加大量信息! You may also take a look at my MWE on GitHub or my "trace" library , where a backtrace would look something like this:您还可以查看我在 GitHub 上的 MWE或我的“跟踪”库,其中的回溯如下所示:

Library API: Exception caught in function 'api_function'
Backtrace:
~/Git/mwe-cpp-exception/src/detail/Library.cpp:17 : library_function failed
~/Git/mwe-cpp-exception/src/detail/Library.cpp:13 : could not open file "nonexistent.txt"

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

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