目录

TSan: Detecting Thread Errors

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

Enabling TSan

You can enable TSan in either of the following ways.

[h2]Method 1

  1. Choose Run > Edit Configurations > Diagnostics and select Thread Sanitizer.

  2. 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

  1. In the AppScope/app.json5 file of the project, add the TSan configuration.

     "tsanEnabled": true

  2. 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

  1. Run or debug the app.
  2. 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.

  3. 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.