Debugging Legacy Code

Debugging Legacy Code #

Flexible Breakpoints #

Occasionally, we’d like to enter the debugger before it hits an exception or segfault. There’s regular breakpoints, but sometimes they’re inconvenient.

We can use signals to create a flexible, persistent breakpoint:

#include <signal.h>

void foo() {
    signal(SIGTRAP, SIG_IGN);
    raise(SIGTRAP);
}

the compiler will stop when SIGTRAP is raised, but otherwise nothing happens.

Catch a Throw #

It can happen that legacy codes handle the exception and helpfully convert it into an printf-statement; or they might rethrow and exception. Either way, assume that gdb wont automatically stop at the right place where we want to start debugging, i.e. type bt.

We can catch throwing and exception:

(gdb) catch throw

Similarly, catch catch will catch catching an exception.