1 //===-- tsan_flags.cc -----------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "sanitizer_common/sanitizer_flags.h"
15 #include "sanitizer_common/sanitizer_flag_parser.h"
16 #include "sanitizer_common/sanitizer_libc.h"
17 #include "tsan_flags.h"
18 #include "tsan_rtl.h"
19 #include "tsan_mman.h"
20 #include "ubsan/ubsan_flags.h"
21
22 namespace __tsan {
23
flags()24 Flags *flags() {
25 return &ctx->flags;
26 }
27
28 // Can be overriden in frontend.
29 #ifdef TSAN_EXTERNAL_HOOKS
30 extern "C" const char* __tsan_default_options();
31 #else
32 SANITIZER_WEAK_DEFAULT_IMPL
__tsan_default_options()33 const char *__tsan_default_options() {
34 return "";
35 }
36 #endif
37
SetDefaults()38 void Flags::SetDefaults() {
39 #define TSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
40 #include "tsan_flags.inc"
41 #undef TSAN_FLAG
42 // DDFlags
43 second_deadlock_stack = false;
44 }
45
RegisterTsanFlags(FlagParser * parser,Flags * f)46 void RegisterTsanFlags(FlagParser *parser, Flags *f) {
47 #define TSAN_FLAG(Type, Name, DefaultValue, Description) \
48 RegisterFlag(parser, #Name, Description, &f->Name);
49 #include "tsan_flags.inc"
50 #undef TSAN_FLAG
51 // DDFlags
52 RegisterFlag(parser, "second_deadlock_stack",
53 "Report where each mutex is locked in deadlock reports",
54 &f->second_deadlock_stack);
55 }
56
InitializeFlags(Flags * f,const char * env)57 void InitializeFlags(Flags *f, const char *env) {
58 SetCommonFlagsDefaults();
59 {
60 // Override some common flags defaults.
61 CommonFlags cf;
62 cf.CopyFrom(*common_flags());
63 cf.allow_addr2line = true;
64 if (kGoMode) {
65 // Does not work as expected for Go: runtime handles SIGABRT and crashes.
66 cf.abort_on_error = false;
67 // Go does not have mutexes.
68 } else {
69 cf.detect_deadlocks = true;
70 }
71 cf.print_suppressions = false;
72 cf.stack_trace_format = " #%n %f %S %M";
73 cf.exitcode = 66;
74 OverrideCommonFlags(cf);
75 }
76
77 f->SetDefaults();
78
79 FlagParser parser;
80 RegisterTsanFlags(&parser, f);
81 RegisterCommonFlags(&parser);
82
83 #if TSAN_CONTAINS_UBSAN
84 __ubsan::Flags *uf = __ubsan::flags();
85 uf->SetDefaults();
86
87 FlagParser ubsan_parser;
88 __ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
89 RegisterCommonFlags(&ubsan_parser);
90 #endif
91
92 // Let a frontend override.
93 parser.ParseString(__tsan_default_options());
94 #if TSAN_CONTAINS_UBSAN
95 const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions();
96 ubsan_parser.ParseString(ubsan_default_options);
97 #endif
98 // Override from command line.
99 parser.ParseString(env);
100 #if TSAN_CONTAINS_UBSAN
101 ubsan_parser.ParseString(GetEnv("UBSAN_OPTIONS"));
102 #endif
103
104 // Sanity check.
105 if (!f->report_bugs) {
106 f->report_thread_leaks = false;
107 f->report_destroy_locked = false;
108 f->report_signal_unsafe = false;
109 }
110
111 SetVerbosity(common_flags()->verbosity);
112
113 if (Verbosity()) ReportUnrecognizedFlags();
114
115 if (common_flags()->help) parser.PrintFlagDescriptions();
116
117 if (f->history_size < 0 || f->history_size > 7) {
118 Printf("ThreadSanitizer: incorrect value for history_size"
119 " (must be [0..7])\n");
120 Die();
121 }
122
123 if (f->io_sync < 0 || f->io_sync > 2) {
124 Printf("ThreadSanitizer: incorrect value for io_sync"
125 " (must be [0..2])\n");
126 Die();
127 }
128 }
129
130 } // namespace __tsan
131