Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can gdb be used to backtrace when exceptions are caught?

Tags:

I have just started using c++ exceptions and want to get it right. What I have in mind is to generate some sort of backtrace information when exceptions are caught. Initially I had ideas similar to Call-stack for exceptions in C++ but eventually figured out that's not quite good.

I have also read How to generate a stacktrace when my gcc C++ app crashes but do not want to add more complexity to my current project. Since, I only need the backtracing when in debug mode, I was hoping I could be using gdb for that purpose.

My strategy has been to insert breakpoint in the catch block and then go up through the call stack to exactly pinpoint why the exception was thrown in the first place (or what caused it)? Unfortunatly, I cannot seem to be able to do this since when gdb reaches the breakpoint, it clears the call stack and I can only see main (that's where I catch). Is this supposed to happen or am I doing something wrong here?

Edit: I just like to summarize the methods here for other folks:

1st Method (by paulsm4). Set a catchpoint via catch throw for catching on throw or catch catch for catching on catch! Then call backtrace

2nd Method (by aschepler) Set a breakpoint on __cxa_throw and then backtrace

3rd Method (in Qt Creator -- if you happen to use) You can easily set a breakpoint on throw or catch!

Edit_2: Using Qt Creator debugger, it seems that setting a breakpoint on __cxa_begin_catch is also an equivalent to catch catch :)

like image 375
mmirzadeh Avatar asked May 15 '12 00:05

mmirzadeh


People also ask

How do I debug an exception in GDB?

To make the debugger catch all exceptions before any stack unwinding takes place, set a breakpoint on __raise_exception (see section Breakpoints, watchpoints, and catchpoints).

How does GDB backtrace work?

A backtrace is a summary of how your program got where it is. It shows one line per frame, for many frames, starting with the currently executing frame (frame zero), followed by its caller (frame one), and on up the stack. Print a backtrace of the entire stack: one line per frame for all frames in the stack.

How do I use backtrace code?

To print a backtrace of the entire stack, use the backtrace command, or its alias bt . This command will print one line per frame for frames in the stack. By default, all stack frames are printed. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c .


1 Answers

This this:

http://sourceware.org/gdb/onlinedocs/gdb/Set-Catchpoints.html

You can use catchpoints to cause the debugger to stop for certain kinds of program events, such as C++ exceptions or the loading of a shared library. Use the catch command to set a catchpoint.

So the answer should be "yes", and it should avoid the problems with the two links you cited.

Please post back if it helped! Personally, I've never tried this GDB feature myself :)

like image 67
paulsm4 Avatar answered Sep 28 '22 16:09

paulsm4