12

I'm looking for an answer in MS VC++.

When debugging a large C++ application, which unfortunately has a very extensive usage of C++ exceptions. Sometimes I catch an exception a little later than I actually want.

Example in pseudo code:

FunctionB()
{
    ...
    throw e;
    ...
}

FunctionA()
{
    ...
    FunctionB()
    ...
}

try
{
    Function A()
}
catch(e)
{
    (<--- breakpoint)
    ...
}

I can catch the exception with a breakpoint when debugging. But I can't trace back if the exception occurred in FunctionA() or FunctionB(), or some other function. (Assuming extensive exception use and a huge version of the above example).

One solution to my problem is to determine and save the call stack in the exception constructor (i.e. before it is caught). But this would require me to derive all exceptions from this base exception class. It would also require a lot of code, and perhaps slow down my program.

Is there an easier way that requires less work? Without having to change my large code base?

Are there better solutions to this problem in other languages?

Ajay
  • 18,086
  • 12
  • 59
  • 105
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636

14 Answers14

13

You pointed to a breakpoint in the code. Since you are in the debugger, you could set a breakpoint on the constructor of the exception class, or set Visual Studio debugger to break on all thrown exceptions (Debug->Exceptions Click on C++ exceptions, select thrown and uncaught options)

Jeremy
  • 2,321
  • 3
  • 21
  • 24
12

If you are just interested in where the exception came from, you could just write a simple macro like

#define throwException(message) \
    {                           \
        std::ostringstream oss; \
        oss << __FILE __ << " " << __LINE__ << " "  \
           << __FUNC__ << " " << message; \
        throw std::exception(oss.str().c_str()); \
    }

which will add the file name, line number and function name to the exception text (if the compiler provides the respective macros).

Then throw exceptions using

throwException("An unknown enum value has been passed!");
Ajay
  • 18,086
  • 12
  • 59
  • 105
MP24
  • 3,110
  • 21
  • 23
7

There's an excellent book written by John Robbins which tackles many difficult debugging questions. The book is called Debugging Applications for Microsoft .NET and Microsoft Windows. Despite the title, the book contains a host of information about debugging native C++ applications.

In this book, there is a lengthy section all about how to get the call stack for exceptions that are thrown. If I remember correctly, some of his advice involves using structured exception handling (SEH) instead of (or in addition to) C++ exceptions. I really cannot recommend the book highly enough.

Matt Dillard
  • 14,677
  • 7
  • 51
  • 61
  • Even beyond the extra up-vote I just gave, I want to emphasize again that the John Robbins book is an EXCELLENT BOOK. I, too, highly recommend it. – pestophagous Oct 24 '08 at 21:06
5

Put a breakpoint in the exception object constructor. You'll get your breakpoint before the exception is thrown.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
4

There is no way to find out the source of an exception after it's caught, unless you include that information when it is thrown. By the time you catch the exception, the stack is already unwound, and there's no way to reconstruct the stack's previous state.

Your suggestion to include the stack trace in the constructor is your best bet. Yes, it costs time during construction, but you probably shouldn't be throwing exceptions often enough that this is a concern. Making all of your exceptions inherit from a new base may also be more than you need. You could simply have the relevant exceptions inherit (thank you, multiple inheritance), and have a separate catch for those.

You can use the StackTrace64 function to build the trace (I believe there are other ways as well). Check out this article for example code.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Derek Park
  • 45,824
  • 15
  • 58
  • 76
  • actually stack is not unwound until all catch handlers have been invoked. (c++ exception model, implemented atop window SEH, makes a few passes through the SEH exception chain). and it's perfectly possible to get at the source of exception (in debugger for instance) being inside this catch. – deemok Jan 21 '09 at 08:35
2

Here's how I do it in C++ using GCC libraries:

#include <execinfo.h> // Backtrace
#include <cxxabi.h> // Demangling

vector<Str> backtrace(size_t numskip) {
    vector<Str> result;
    std::vector<void*> bt(100);
    bt.resize(backtrace(&(*bt.begin()), bt.size()));
    char **btsyms = backtrace_symbols(&(*bt.begin()), bt.size());
    if (btsyms) {
        for (size_t i = numskip; i < bt.size(); i++) {
            Aiss in(btsyms[i]);
            int idx = 0; Astr nt, addr, mangled;
            in >> idx >> nt >> addr >> mangled;
            if (mangled == "start") break;
            int status = 0;
            char *demangled = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);

            Str frame = (status==0) ? Str(demangled, demangled+strlen(demangled)) : 
                                      Str(mangled.begin(), mangled.end());
            result.push_back(frame);

            free(demangled);
        }
        free(btsyms);
    }
    return result;
}

Your exception's constructor can simply call this function and store away the stack trace. It takes the param numskip because I like to slice off the exception's constructor from my stack traces.

Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
1

I believe MSDev allows you to set break points when an exception is thrown.

Alternatively put the break point on the constructor of your exception object.

Martin York
  • 257,169
  • 86
  • 333
  • 562
1

There's no standard way to do this.

Further, the call stack must typically be recorded at the time of the exception being thrown; once it has been caught the stack has unrolled, so you no longer know what was going on at the point of being thrown.

In VC++ on Win32/Win64, you might get usable-enough results by recording the value from the compiler intrinsic _ReturnAddress() and ensuring that your exception class constructor is __declspec(noinline). In conjunction with the debug symbol library, I think you could probably get the function name (and line number, if your .pdb contains it) that corresponds to the return address using SymGetLineFromAddr64.

DrPizza
  • 17,882
  • 7
  • 41
  • 53
1

In native code you can get a shot at walking the callstack by installing a Vectored Exception handler. VC++ implements C++ exceptions on top of SEH exceptions and a vectored exception handler is given first shot before any frame based handlers. However be really careful, problems introduced by vectored exception handling can be difficult to diagnose.

Also Mike Stall has some warnings about using it in an app that has managed code. Finally, read Matt Pietrek's article and make sure you understand SEH and vectored exception handling before you try this. (Nothing feels quite so bad as tracking down a critical problem to code you added help track down critical problems.)

Steve Steiner
  • 5,299
  • 4
  • 32
  • 43
1

If you're debugging from the IDE, go to Debug->Exceptions, click Thrown for C++ exceptions.

Mat Noguchi
  • 1,072
  • 6
  • 7
0

Other languages? Well, in Java you call e.printStackTrace(); It doesn't get much simpler than that.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
0

In case anyone is interested, a co-worker replied to this question to me via email:

Artem wrote:

There is a flag to MiniDumpWriteDump() that can do better crash dumps that will allow seeing full program state, with all global variables, etc. As for call stacks, I doubt they can be better because of optimizations... unless you turn (maybe some) optimizations off.

Also, I think disabling inline functions and whole program optimization will help quite a lot.

In fact, there are many dump types, maybe you could choose one small enough but still having more info http://msdn.microsoft.com/en-us/library/ms680519(VS.85).aspx

Those types won't help with call stack though, they only affect the amount of variables you'll be able to see.

I noticed some of those dump types aren't supported in dbghelp.dll version 5.1 that we use. We could update it to the newest, 6.9 version though, I've just checked the EULA for MS Debugging Tools -- the newest dbghelp.dll is still ok to redistribute.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
0

I use my own exceptions. You can handle them quite simple - also they contain text. I use the format:

throw Exception( "comms::serial::serial( )", "Something failed!" );

Also I have a second exception format:

throw Exception( "comms::serial::serial( )", ::GetLastError( ) );

Which is then converted from a DWORD value to the actual message using FormatMessage. Using the where/what format will show you what happened and in what function.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
0

By now, it has been 11 years since this question was asked and today, we can solve this problem using only standard C++11, i.e. cross-platform and without the need for a debugger or cumbersome logging. You can trace the call stack that led to an exception

Use std::nested_exception and std::throw_with_nested

This won't give you a stack unwind, but in my opinion the next best thing. It is described on StackOverflow here and here, how you can get a backtrace on your exceptions inside your code without need for a debugger or cumbersome logging, 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 (i.e. functions without this will not appear in your trace). You could automate this with macros, reducing the amount of code you have to write/change.

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