ThreadSanitizer (TSan) is a tool for detecting data races. It consists of a compiler instrumentation module and a runtime library. When TSan is enabled, it can reduce performance by 5 to 15 times and increase memory usage by 5 to 10 times.
Constraints
- ASan, TSan, UBSan, and HWASan cannot be enabled simultaneously; only one of these sanitizers can be enabled at a time.
- Enabling TSan could lead to significant virtual memory allocation, which potentially impacts other memory-intensive features, such as GPU-based graphics rendering.
- TSan does not support static linking with the libc or libc++ libraries.
Enabling TSan
You can enable TSan in either of the following ways.
[h2]Method 1
- Choose Run > Edit Configurations > Diagnostics and select Thread Sanitizer.

- If a local library is referenced, set arguments to -DOHOS_ENABLE_TSAN=ON in the build-profile.json5 file of the library module. This allows the .so file to be compiled in TSan mode.

[h2]Method 2
- In the AppScope/app.json5 file of the project, add the TSan configuration.
"tsanEnabled": true

- Add build parameters to enable TSan instrumentation for the module.
In the module where TSan needs to be enabled, add build parameters to enable TSan instrumentation. Specifically, in the build-profile.json5 file of the module, add the following:
"arguments": "-DOHOS_ENABLE_TSAN=ON"

Using TSan
- Run or debug the app.
- When a thread error occurs, the TSan log information will be displayed. You can click the link in the information to go to the code that causes the thread error.

- If it is a release app and there is no source code available locally, you can use the AnalyzeStackTrace feature. Provide the .so file for which you want to analyze the stack trace; the analysis result will give you the source code addresses.
