blob: a7f6eb782b5fade03c2a5b9ebeda2691809585f7 [file] [log] [blame]
Sean Silva3872b462012-12-12 23:44:55 +00001ThreadSanitizer
2===============
3
4Introduction
5------------
6
7ThreadSanitizer is a tool that detects data races. It consists of a compiler
8instrumentation module and a run-time library. Typical slowdown introduced by
Dmitry Vyukovc092cba2012-12-17 07:16:54 +00009ThreadSanitizer is about **5x-15x**. Typical memory overhead introduced by
10ThreadSanitizer is about **5x-10x**.
Sean Silva3872b462012-12-12 23:44:55 +000011
12How to build
13------------
14
15Follow the `Clang build instructions <../get_started.html>`_. CMake build is
16supported.
17
18Supported Platforms
19-------------------
20
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000021ThreadSanitizer is supported on Linux x86_64 (tested on Ubuntu 10.04 and 12.04).
22Support for MacOS 10.7 (64-bit only) is planned for 2013. Support for 32-bit
Sean Silva3872b462012-12-12 23:44:55 +000023platforms is problematic and not yet planned.
24
25Usage
26-----
27
28Simply compile your program with ``-fsanitize=thread -fPIE`` and link it with
29``-fsanitize=thread -pie``. To get a reasonable performance add ``-O1`` or
30higher. Use ``-g`` to get file names and line numbers in the warning messages.
31
32Example:
33
34.. code-block:: c++
35
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000036 % cat projects/compiler-rt/lib/tsan/lit_tests/tiny_race.c
Sean Silva3872b462012-12-12 23:44:55 +000037 #include <pthread.h>
38 int Global;
39 void *Thread1(void *x) {
40 Global = 42;
41 return x;
42 }
43 int main() {
44 pthread_t t;
45 pthread_create(&t, NULL, Thread1, NULL);
46 Global = 43;
47 pthread_join(t, NULL);
48 return Global;
49 }
50
51 $ clang -fsanitize=thread -g -O1 tiny_race.c -fPIE -pie
52
53If a bug is detected, the program will print an error message to stderr.
54Currently, ThreadSanitizer symbolizes its output using an external
55``addr2line`` process (this will be fixed in future).
56
57.. code-block:: bash
58
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000059 % ./a.out
Sean Silva3872b462012-12-12 23:44:55 +000060 WARNING: ThreadSanitizer: data race (pid=19219)
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000061 Write of size 4 at 0x7fcf47b21bc0 by thread T1:
Sean Silva3872b462012-12-12 23:44:55 +000062 #0 Thread1 tiny_race.c:4 (exe+0x00000000a360)
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000063
Sean Silva3872b462012-12-12 23:44:55 +000064 Previous write of size 4 at 0x7fcf47b21bc0 by main thread:
65 #0 main tiny_race.c:10 (exe+0x00000000a3b4)
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000066
67 Thread T1 (running) created at:
68 #0 pthread_create tsan_interceptors.cc:705 (exe+0x00000000c790)
Sean Silva3872b462012-12-12 23:44:55 +000069 #1 main tiny_race.c:9 (exe+0x00000000a3a4)
70
Dmitry Vyukov728e2122012-12-17 08:52:05 +000071``__has_feature(thread_sanitizer)``
72------------------------------------
73
74In some cases one may need to execute different code depending on whether
75ThreadSanitizer is enabled.
76:ref:`\_\_has\_feature <langext-__has_feature-__has_extension>` can be used for
77this purpose.
78
79.. code-block:: c
80
81 #if defined(__has_feature) && __has_feature(thread_sanitizer)
82 // code that builds only under ThreadSanitizer
83 #endif
84
Sean Silva3872b462012-12-12 23:44:55 +000085Limitations
86-----------
87
88* ThreadSanitizer uses more real memory than a native run. At the default
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000089 settings the memory overhead is 5x plus 1Mb per each thread. Settings with 3x
90 (less accurate analysis) and 9x (more accurate analysis) overhead are also
91 available.
Sean Silva3872b462012-12-12 23:44:55 +000092* ThreadSanitizer maps (but does not reserve) a lot of virtual address space.
93 This means that tools like ``ulimit`` may not work as usually expected.
Dmitry Vyukovc092cba2012-12-17 07:16:54 +000094* Libc/libstdc++ static linking is not supported.
95* ThreadSanitizer requires ``-fPIE -pie`` compiler flags.
Sean Silva3872b462012-12-12 23:44:55 +000096
97Current Status
98--------------
99
Dmitry Vyukovc092cba2012-12-17 07:16:54 +0000100ThreadSanitizer is in beta stage. It is known to work on large C++ programs
101using pthreads, but we do not promise anything (yet). C++11 threading is
Dmitry Vyukov61dde192012-12-17 13:07:35 +0000102supported with llvm libc++. The test suite is integrated into CMake build
Dmitry Vyukovc092cba2012-12-17 07:16:54 +0000103and can be run with ``make check-tsan`` command.
Sean Silva3872b462012-12-12 23:44:55 +0000104
105We are actively working on enhancing the tool --- stay tuned. Any help,
106especially in the form of minimized standalone tests is more than welcome.
107
108More Information
109----------------
110`http://code.google.com/p/thread-sanitizer <http://code.google.com/p/thread-sanitizer/>`_.
111