The best way to print information when debugging race conditions

I am debugging an application to fix a segmentation error, which I suspect is caused by a race condition.

I would like to add some instructions for printing to the code, but I know that adding calls to is printfnot recommended, as this can change the behavior of threads and in some cases hide the error.

If you look at other options, I saw that with gdb you can use breakpoints to print, and then automatically continue execution:

break foo
commands
silent
printf "Called foo: x is %d\n",x
cont
end

Is this better than putting printfin my code?

I know that gdb also has Tracepoints , but they only work with gdbserver, and this is an additional level of complexity that I would prefer to avoid at the moment.

Additional Information: The application is written in C and runs on Linux.

+4
source share
2 answers

Is this better than putting printf in my code?

No, it's a lot worse. Each breakpoint that falls into GDB fires the following chain of events:

  • context switch from current stream to gdb
  • GDB stops all other threads (assuming the default stop mode)
  • GDB evaluates breakpoint commands
  • GDB resumes all threads (this in itself is a complex multi-step process that I would not be here).

, , , printf, , , , .

, GDB .

ThreadSanitizer .

​​, , SW.

-, ThreadSanitizer . , . , .

-, ThreadSanitizer , . , : ( , ?) .

+2

Linux, ThreadSanitizer. gcc clang -fsanitize = thread . printf, - . , , . , , , Valgrind http://valgrind.org Data Race Detector, ThreadSanitizer.

+2

All Articles