Advertisement

Exceptions and stack traces

Started by May 13, 2006 01:20 PM
2 comments, last by Bregma 18 years, 4 months ago
I like using C++ exceptions, but one major problem I have with them is that I can't get a stack trace with gdb when one is thrown. gdb just says "Program exited with code 0377." Is there any way to get a stack trace?
I like the DARK layout!
In C++ the stack as was atm of throwing an exception isn't stored with the exception. After throwing an exception the stack is reduced until a matching catch clause is found. So the stack trace will be lost.

Hence your chance is to look at the stack just at the moment of throwing the exception. (1) don't throw exceptions directly but by a routine, and set a breakpoint into that routine. (2) Perhaps there is a way to instruct gdb to watch at exception throwing, so that it stops as soon as an exception occurs; but I'm absolutely not sure if such a way exists.
Advertisement
Quote: Original post by haegarr
(2) Perhaps there is a way to instruct gdb to watch at exception throwing, so that it stops as soon as an exception occurs; but I'm absolutely not sure if such a way exists.


I'm not sure if this is The Right Way to do it but break __cxa_throw has always worked for me.
Quote: Original post by BradDaBug
I like using C++ exceptions, but one major problem I have with them is that I can't get a stack trace with gdb when one is thrown. gdb just says "Program exited with code 0377." Is there any way to get a stack trace?


GDB will break when an exception is thrown if you tell it to.

Try catch throw.

If that's not good enough, set a breakpoint on __cxa_throw for GCC 3.0 and later, or on __raise_exception for GCC 2.95 and earlier.

Stephen M. Webb
Professional Free Software Developer

This topic is closed to new replies.

Advertisement