123

I'm working on a multithreaded application, and I want to debug it using GDB.

Problem is, one of my threads keeps dying with the message:

pure virtual method called
terminate called without an active exception
Abort

I know the cause of that message, but I have no idea where in my thread it occurs. A backtrace would really be helpful.

When I run my app in GDB, it pauses every time a thread is suspended or resumed. I want my app to continue running normally until one of the threads dies with that exception, at which point everything should halt so that I can get a backtrace.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
Ankur Sethi
  • 3,508
  • 5
  • 23
  • 17
  • What signal is GDB reporting when it pauses? you should be able to run a command like `handle SIGUSR1 pass noprint nostop` – Hasturkun Jul 12 '09 at 14:13

4 Answers4

171

You can try using a "catchpoint" (catch throw) to stop the debugger at the point where the exception is generated.

The following excerpt From the gdb manual describes the catchpoint feature.


5.1.3 Setting catchpoints

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.

  • catch event

    Stop when event occurs. event can be any of the following:

    • throw

      The throwing of a C++ exception.

    • catch

      The catching of a C++ exception.

    • exec

      A call to exec. This is currently only available for HP-UX.

    • fork

      A call to fork. This is currently only available for HP-UX.

    • vfork

      A call to vfork. This is currently only available for HP-UX.

    • load or load libname

      The dynamic loading of any shared library, or the loading of the library libname. This is currently only available for HP-UX.

    • unload or unload libname

      The unloading of any dynamically loaded shared library, or the unloading of the library libname. This is currently only available for HP-UX.

  • tcatch event

    Set a catchpoint that is enabled only for one stop. The catchpoint is automatically deleted after the first time the event is caught.

Use the info break command to list the current catchpoints.

There are currently some limitations to C++ exception handling (catch throw and catch catch) in GDB:

  • If you call a function interactively, GDB normally returns control to you when the function has finished executing. If the call raises an exception, however, the call may bypass the mechanism that returns control to you and cause your program either to abort or to simply continue running until it hits a breakpoint, catches a signal that GDB is listening for, or exits. This is the case even if you set a catchpoint for the exception; catchpoints on exceptions are disabled within interactive calls.

  • You cannot raise an exception interactively.

  • You cannot install an exception handler interactively.

Sometimes catch is not the best way to debug exception handling: if you need to know exactly where an exception is raised, it is better to stop before the exception handler is called, since that way you can see the stack before any unwinding takes place. If you set a breakpoint in an exception handler instead, it may not be easy to find out where the exception was raised.

To stop just before an exception handler is called, you need some knowledge of the implementation. In the case of GNU C++, exceptions are raised by calling a library function named __raise_exception which has the following ANSI C interface:

/* addr is where the exception identifier is stored.
   id is the exception identifier.  */
void __raise_exception (void **addr, void *id);

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

With a conditional breakpoint (see section Break conditions) that depends on the value of id, you can stop your program when a specific exception is raised. You can use multiple conditional breakpoints to stop your program when any of a number of exceptions are raised.

Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
Dan
  • 3,485
  • 2
  • 21
  • 25
  • 2
    You can also specify the type of exception to catch, e.g. `catch throw std::runtime_exception`. – scai Feb 25 '19 at 09:44
7

FWIW, apparently, in gcc 4.1, the appropriate function name has changed and one must set a breakpoint in this function.

__cxa_pure_virtual

Jeffrey Hill
  • 101
  • 1
  • 2
7

Only below one worked for me with gdb 8.3:

break _Unwind_RaiseException

"catch throw" or "break __cxx_throw" didn't work for me.

soumeng78
  • 600
  • 7
  • 12
5

Set a breakpoint on __pure_virtual

Steve Folly
  • 8,327
  • 9
  • 52
  • 63
  • In @JeffreyHill answer, it is called __cxa_pure_virtual now. I do not know how to check that myself, so I don't want to edit the answer. I don't intend to down-vote, but the answer could be wrong now and should be edited by someone who knows what is correct. – Philipp Claßen Aug 31 '16 at 22:42