简体   繁体   中英

Can a thrown exception be accessed in a C++ catch(…) block

I have GDB attached to a process that is currently inside a catch(...) block.

Is there a known technique to access that thrown exception?

The program in question is a gcc/x86-64 binary, but I'm also curious about other builds.

As you say, you can re-throw it, so you can re-throw it inside another try/catch block with more specific clauses to extract the exception (and another ... if you want as well). You can even do this inside another function so you can centralize your exception handling.

Edit: I misunderstood the importance of gdb in your question, but you can apply the idea I described. Make a function that re-throws the exception you can set a breakpoint in:

void
helper()
{
    try {
        throw;
    } catch (int i) {
        // anything that won't get optimized away
        volatile int j = i;  // breakpoint here
    }
}

Then in gdb just do call helper() . I just tested this to be sure it worked.

Further edit: If you literally mean I'm running a program under gdb right now and you are not exiting gdb until you are sure you can't get the exception, then it's time to look at eh_throw.cc and friends in the gcc source. __cxa_rethrow starts with:

  __cxa_eh_globals *globals = __cxa_get_globals ();
  __cxa_exception *header = globals->caughtExceptions;

You will have to examine all of those structures to figure out what's buried inside.

I have not tested this - but the exception (or at least a pointer to it) should probably be on the stack somewhere close to the head. I guess the exact position and format is implementation dependent, but you should be able casting different addresses in this area of the stack to your exception type (or at least to std::exception) and see if you get meaningful results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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