0

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...

How to do that with VS2017 C++ ? Or is there a way to do that?

enter image description here

armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • Debug -> Windows -> Call Stack. Should be Alt+7 as a keyboard shortcut? – UnholySheep Aug 11 '17 at 07:20
  • Afraid not. In C++ by the time you get to the code handing the exception and can place a breakpoint, the stack has unrolled. You may be able to catch the exception close to the throw and re-throw it. Then you can place a breakpoint on the rethrow and get at least some of the stack. – user4581301 Aug 11 '17 at 07:22
  • @UnholySheep Ok, thanks, it's surprised that didn't enable by default :D – armnotstrong Aug 11 '17 at 07:22
  • @user4581301 but I found it with AT UnholySheep's method, does that new feature of VS2017? – armnotstrong Aug 11 '17 at 07:24
  • Seems like I misinterpretted your question. I read it as "I have code that is called too often to place a breakpoint on the code that throws the exception, and now I want to know where it was thrown." But if you have an uncaught exception, the debugger should stop and give you access to the stack at the time of the throw. If you caught the exception, you wind up with the problem I described. – user4581301 Aug 11 '17 at 07:28
  • @user4581301 thanks anyway, my bad English to blame :D – armnotstrong Aug 11 '17 at 07:31
  • Not bad English at all. This is entirely two bad assumptions on my part: That you were A) catching the exception and B) wondering why your stack trace was useless. – user4581301 Aug 11 '17 at 07:57

1 Answers1

0

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:

Use std::nested_exception and std::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. It will, however, require that you insert try/catch statements at the functions you wish to trace.

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:

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"
GPMueller
  • 2,881
  • 2
  • 27
  • 36