Search code examples
gccgdbthread-sanitizer

How to add breakpoint when thread sanitizer reports data-race?


There is a similar question for address sanitizers, but for thread sanitizers, it doesn't work, I have tried to break on __sanitizer_print_stack_trace, which doesn't work either.


Solution

    1. Run the program under GDB, set breakpoints on exit and _exit. On Linux, also set catch syscall exit_group.
    2. set halt_on_error=1 in TSAN_OPTIONS to ask thread sanitizer to exit on first error:
    (gdb) set env TSAN_OPTIONS=halt_on_error=1
    (gdb) run
    ... error should be reported and one of the breakpoints should fire.
    
    1. Profit.

    P.S. When the breakpoint is hit, use GDB where command to see how the error is reported. Setting a breakpoint on some kind of __tsan_report_error that is likely on the stack will probably work even without setting halt_on_error.