Leak debugging with LeakSanitizer

Leak debugging with LeakSanitizer

May 22, 2023

LeakSanitizer internally uses ptrace, probably to suspend all threads such that it can scan for leaks without false positives (see issue 9). Only one application can use ptrace, so if we run application under gdb or strace, then LeakSanitizer won’t be able to attach via ptrace. if we are using clion, it probably end up with a error message like “LeakSanitizer does not work under ptrace (strace, gdb, etc)”

Disable LeakSanitizer #

We can disable the sanitizer

export ASAN_OPTIONS=detect_leaks=0

Enable leak debugging #

If you do want to enable leak debugging, you must detach the debugger before LeakSanitizer starts scanning. To be able to attach a debugger shortly afterwards, sleep a bit (for example, 10 seconds):

export ASAN_OPTIONS=sleep_before_dying=10
./program

Then in another shell, attach to the application again:

gdb -q -p $(pidof program)