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 
21 namespace __tsan {
22 
flags()23 Flags *flags() {
24   return &ctx->flags;
25 }
26 
27 // Can be overriden in frontend.
28 #ifdef TSAN_EXTERNAL_HOOKS
29 extern "C" const char* __tsan_default_options();
30 #else
31 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__tsan_default_options()32 const char *WEAK __tsan_default_options() {
33   return "";
34 }
35 #endif
36 
SetDefaults()37 void Flags::SetDefaults() {
38 #define TSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
39 #include "tsan_flags.inc"
40 #undef TSAN_FLAG
41   // DDFlags
42   second_deadlock_stack = false;
43 }
44 
RegisterTsanFlags(FlagParser * parser,Flags * f)45 void RegisterTsanFlags(FlagParser *parser, Flags *f) {
46 #define TSAN_FLAG(Type, Name, DefaultValue, Description) \
47   RegisterFlag(parser, #Name, Description, &f->Name);
48 #include "tsan_flags.inc"
49 #undef TSAN_FLAG
50   // DDFlags
51   RegisterFlag(parser, "second_deadlock_stack",
52       "Report where each mutex is locked in deadlock reports",
53       &f->second_deadlock_stack);
54 }
55 
InitializeFlags(Flags * f,const char * env)56 void InitializeFlags(Flags *f, const char *env) {
57   FlagParser parser;
58   RegisterTsanFlags(&parser, f);
59   RegisterCommonFlags(&parser);
60 
61   f->SetDefaults();
62 
63   SetCommonFlagsDefaults();
64   {
65     // Override some common flags defaults.
66     CommonFlags cf;
67     cf.CopyFrom(*common_flags());
68     cf.allow_addr2line = true;
69 #ifndef SANITIZER_GO
70     cf.detect_deadlocks = true;
71 #endif
72     cf.print_suppressions = false;
73     cf.stack_trace_format = "    #%n %f %S %M";
74     OverrideCommonFlags(cf);
75   }
76 
77   // Let a frontend override.
78   parser.ParseString(__tsan_default_options());
79   // Override from command line.
80   parser.ParseString(env);
81 
82   // Sanity check.
83   if (!f->report_bugs) {
84     f->report_thread_leaks = false;
85     f->report_destroy_locked = false;
86     f->report_signal_unsafe = false;
87   }
88 
89   SetVerbosity(common_flags()->verbosity);
90 
91   if (Verbosity()) ReportUnrecognizedFlags();
92 
93   if (common_flags()->help) parser.PrintFlagDescriptions();
94 
95   if (f->history_size < 0 || f->history_size > 7) {
96     Printf("ThreadSanitizer: incorrect value for history_size"
97            " (must be [0..7])\n");
98     Die();
99   }
100 
101   if (f->io_sync < 0 || f->io_sync > 2) {
102     Printf("ThreadSanitizer: incorrect value for io_sync"
103            " (must be [0..2])\n");
104     Die();
105   }
106 }
107 
108 }  // namespace __tsan
109