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 "sanitizer_common/sanitizer_flags.h"
14 #include "sanitizer_common/sanitizer_flag_parser.h"
15 #include "sanitizer_common/sanitizer_libc.h"
16 #include "tsan_flags.h"
18 #include "tsan_mman.h"
19 #include "ubsan/ubsan_flags.h"
23 // Can be overriden in frontend.
24 #ifdef TSAN_EXTERNAL_HOOKS
25 extern "C" const char* __tsan_default_options();
27 SANITIZER_WEAK_DEFAULT_IMPL
28 const char *__tsan_default_options() {
33 void Flags::SetDefaults() {
34 #define TSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
35 #include "tsan_flags.inc"
38 second_deadlock_stack
= false;
41 void RegisterTsanFlags(FlagParser
*parser
, Flags
*f
) {
42 #define TSAN_FLAG(Type, Name, DefaultValue, Description) \
43 RegisterFlag(parser, #Name, Description, &f->Name);
44 #include "tsan_flags.inc"
47 RegisterFlag(parser
, "second_deadlock_stack",
48 "Report where each mutex is locked in deadlock reports",
49 &f
->second_deadlock_stack
);
52 void InitializeFlags(Flags
*f
, const char *env
, const char *env_option_name
) {
53 SetCommonFlagsDefaults();
55 // Override some common flags defaults.
57 cf
.CopyFrom(*common_flags());
58 cf
.allow_addr2line
= true;
60 // Does not work as expected for Go: runtime handles SIGABRT and crashes.
61 cf
.abort_on_error
= false;
62 // Go does not have mutexes.
63 cf
.detect_deadlocks
= false;
65 cf
.print_suppressions
= false;
66 cf
.stack_trace_format
= " #%n %f %S %M";
68 cf
.intercept_tls_get_addr
= true;
69 OverrideCommonFlags(cf
);
75 RegisterTsanFlags(&parser
, f
);
76 RegisterCommonFlags(&parser
);
78 #if TSAN_CONTAINS_UBSAN
79 __ubsan::Flags
*uf
= __ubsan::flags();
82 FlagParser ubsan_parser
;
83 __ubsan::RegisterUbsanFlags(&ubsan_parser
, uf
);
84 RegisterCommonFlags(&ubsan_parser
);
87 // Let a frontend override.
88 parser
.ParseString(__tsan_default_options());
89 #if TSAN_CONTAINS_UBSAN
90 const char *ubsan_default_options
= __ubsan::MaybeCallUbsanDefaultOptions();
91 ubsan_parser
.ParseString(ubsan_default_options
);
93 // Override from command line.
94 parser
.ParseString(env
, env_option_name
);
95 #if TSAN_CONTAINS_UBSAN
96 ubsan_parser
.ParseStringFromEnv("UBSAN_OPTIONS");
100 if (!f
->report_bugs
) {
101 f
->report_thread_leaks
= false;
102 f
->report_destroy_locked
= false;
103 f
->report_signal_unsafe
= false;
106 InitializeCommonFlags();
108 if (Verbosity()) ReportUnrecognizedFlags();
110 if (common_flags()->help
) parser
.PrintFlagDescriptions();
112 if (f
->history_size
< 0 || f
->history_size
> 7) {
113 Printf("ThreadSanitizer: incorrect value for history_size"
114 " (must be [0..7])\n");
118 if (f
->io_sync
< 0 || f
->io_sync
> 2) {
119 Printf("ThreadSanitizer: incorrect value for io_sync"
120 " (must be [0..2])\n");
125 } // namespace __tsan