1 //===-- tsan_flags.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
13 #include "tsan_flags.h"
15 #include "sanitizer_common/sanitizer_flag_parser.h"
16 #include "sanitizer_common/sanitizer_flags.h"
17 #include "sanitizer_common/sanitizer_libc.h"
18 #include "tsan_interface.h"
19 #include "tsan_mman.h"
21 #include "ubsan/ubsan_flags.h"
25 // Can be overriden in frontend.
26 #ifdef TSAN_EXTERNAL_HOOKS
27 extern "C" const char *__tsan_default_options();
29 SANITIZER_WEAK_DEFAULT_IMPL
30 const char *__tsan_default_options() {
35 void Flags::SetDefaults() {
36 #define TSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
37 #include "tsan_flags.inc"
40 second_deadlock_stack
= false;
43 void RegisterTsanFlags(FlagParser
*parser
, Flags
*f
) {
44 #define TSAN_FLAG(Type, Name, DefaultValue, Description) \
45 RegisterFlag(parser, #Name, Description, &f->Name);
46 #include "tsan_flags.inc"
49 RegisterFlag(parser
, "second_deadlock_stack",
50 "Report where each mutex is locked in deadlock reports",
51 &f
->second_deadlock_stack
);
54 void InitializeFlags(Flags
*f
, const char *env
, const char *env_option_name
) {
55 SetCommonFlagsDefaults();
57 // Override some common flags defaults.
59 cf
.CopyFrom(*common_flags());
60 cf
.external_symbolizer_path
= GetEnv("TSAN_SYMBOLIZER_PATH");
61 cf
.allow_addr2line
= true;
63 // Does not work as expected for Go: runtime handles SIGABRT and crashes.
64 cf
.abort_on_error
= false;
65 // Go does not have mutexes.
66 cf
.detect_deadlocks
= false;
68 cf
.print_suppressions
= false;
69 cf
.stack_trace_format
= " #%n %f %S %M";
71 cf
.intercept_tls_get_addr
= true;
72 OverrideCommonFlags(cf
);
78 RegisterTsanFlags(&parser
, f
);
79 RegisterCommonFlags(&parser
);
81 #if TSAN_CONTAINS_UBSAN
82 __ubsan::Flags
*uf
= __ubsan::flags();
85 FlagParser ubsan_parser
;
86 __ubsan::RegisterUbsanFlags(&ubsan_parser
, uf
);
87 RegisterCommonFlags(&ubsan_parser
);
90 // Let a frontend override.
91 parser
.ParseString(__tsan_default_options());
92 #if TSAN_CONTAINS_UBSAN
93 const char *ubsan_default_options
= __ubsan_default_options();
94 ubsan_parser
.ParseString(ubsan_default_options
);
96 // Override from command line.
97 parser
.ParseString(env
, env_option_name
);
98 #if TSAN_CONTAINS_UBSAN
99 ubsan_parser
.ParseStringFromEnv("UBSAN_OPTIONS");
103 if (!f
->report_bugs
) {
104 f
->report_thread_leaks
= false;
105 f
->report_destroy_locked
= false;
106 f
->report_signal_unsafe
= false;
109 InitializeCommonFlags();
111 if (Verbosity()) ReportUnrecognizedFlags();
113 if (common_flags()->help
) parser
.PrintFlagDescriptions();
115 if (f
->io_sync
< 0 || f
->io_sync
> 2) {
116 Printf("ThreadSanitizer: incorrect value for io_sync"
117 " (must be [0..2])\n");
122 } // namespace __tsan