gdb allows one to catch exceptions when they're thrown, and when they're caught. But sometimes the line an exception is thrown has no symbols, or a breakpoint is triggered during exception handling. How do I inspect the value of the current exception?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Updated
Here's some info from the GDB Manual
That said
It depends on the code and where you are in the stack. If you actually caught the exception as in :
You could probably try printing
e.what()
, or look at the members of the exception. If you just caught it as (...) then I'm not sure what you'd be able to gather.Another handling thing you could do is to catch 'throw' in gdb and catch 'catch' as well if you really want to follow the whole flow.
This way you will get a breakpoints right before exceptions are thrown and right as they are caught, you could then walk the stack to gain more information about what was going on. Even if you are in another break point you should be able to walk up the stack (using up or down) to get the frame in which the exception is visible.
Earlier answers were correct when written (in 2013), but since then gdb and libstdc++ have changed.
libstdc++ now has some hooks that let gdb interact more nicely with the exception system. In particular, there is now enough information exposed for gdb to provide a
$_exception
convenience variable to the user. This variable holds the exception being thrown. It is only valid at exactly the spot where the exception is being caught; which you can stop at usingcatch catch
.See the page from the manual for details.
Short answer: you can't because most work of exception handling is done outside of your program, and is therefore outside of gdb's scope.
Explained answer:
If the binary you're debugging has no debug symbols then the binary is probably stripped and you won't be able to find much at all about the types/values of anything at all.
I think you're assuming here that an exception is a language feature that gdb can inspect; in fact an exception in C++ is a combination of features from C++ as a language, of libc++ and the ABI. And there might even be more than a single active current exception.
Like UpAndAdam points out you can set a breakpoint in a catch block with a type specifier, and then inspect that element, but I suspect your problem is in cases where you find a "catch (...)". In those cases you won't be able to learn much about the current exception unless you go digging into the implementation of exception handling.
With a very short and incomplete description we could say that to throw an exception:
Now, it's difficult to talk about details because a lot of exception handling depends on your toolchain (compiler, platform, architecture, libc++, etc) but in most cases a "catch (...)" won't even receive the original exception as an argument. In any case, to somehow answer your question: in gcc with gnu's libc++ you could try something like this:
In any case, you'll probably need to spend quite a bit of time trying to understand how exception handling is implemented in your platform. If you want to read a bit more about exception handling I spent some time in the past writing about the topic @ http://monoinfinito.wordpress.com/series/exception-handling-in-c/. It's not an official source but it does have links to the specs of each part involved in handling an exception.