Recent versions of GCC and Clang feature Undefined Behavior Sanitizer (UBSan) which is a compile flag (-fsanitize=undefined
) that adds runtime instrumentation code. On errors, a warning such as this one is shown:
packet-ber.c:1917:23: runtime error: left shift of 54645397829836991 by 8 places cannot be represented in type 'long int'
Now I would like to debug this and get a debug break on said line. For Address Sanitizer (ASAN) there is ASAN_OPTIONS=abort_on_error=1
which results in a fatal error that is catchable. The only UBSan option that seems usable is UBSAN_OPTIONS=print_stacktrace=1
which results in a call trace dump for reports. This however does not allow me to inspect the local variables and then continue the program. Use of -fsanitize-undefined-trap-on-error
therefore not possible.
How should I break in gdb on UBSan reports? While break __sanitizer::SharedPrintfCode
seems to work, the name looks quite internal.
While breaking on the detection functions (as described by @Mark Plotnick and @Iwillnotexist Idonotexist) is one option, a better approach is breaking on the functions that report these issues after detection. This approach is also used for ASAN where one would break on
__asan_report_error
.Summary: You can stop on an ubsan report via a breakpoint on
__ubsan::ScopedReport::~ScopedReport
or__ubsan::Diag::~Diag
. These are private implementation details which might change in the future though. Tested with GCC 4.9, 5.1.0, 5.2.0 and Clang 3.3, 3.4, 3.6.2.For GCC 4.9.2 from ppa:ubuntu-toolchain-r/test, you need
libubsan0-dbg
to make the above breakpoints available. Ubuntu 14.04 with Clang 3.3 and 3.4 do not support the__ubsan::ScopedReport::~ScopedReport
breakpoints, so you can only break before printing the message using__ubsan::Diag::~Diag
.Example buggy source code and a gdb session:
Detailled analysis follows. Note that both ASAN and ubsan both originate from a LLVM project, compiler-rt. This is used by Clang and ends up in GCC as well. Links in the following sections point to the compiler-rt project code, release 3.6.
ASAN has made its internal
__asan_report_error
part of the documented public interface. This function gets called whenever a violation is detected, its flow continues in lib/asan/asan_report.c:938:ubsan on the other hand has no public interface, but its current implementation is also much simpler and limited (less options). On errors, a stacktrace can be printed when the
UBSAN_OPTIONS=print_stacktrace=1
environment variable is set. Thus, by searching the source code forprint_stacktrace
, one finds function MaybePrintStackTrace which is called though the ScopedReport destructor:As you can see, there is a method to kill the program on errors, but unfortunately there is no builtin mechanism to trigger a debugger trap. Let's find a suitable breakpoint then.
The GDB command
info functions <function name>
made it possible to identifyMaybePrintStackTrace
as function on which a breakpoint can be set. Execution ofinfo functions ScopedReport::~ScopedReport
gave another function:__ubsan::ScopedReport::~ScopedReport
. If none of these functions seem available (even with debugging symbols installed), you can tryinfo functions ubsan
orinfo functions sanitizer
to get all (UndefinedBehavior)Sanitizer-related functions.