Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_flags.cc -----------------------------------------------------===// |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 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 | |
Alexey Samsonov | f3be706 | 2012-07-09 13:21:39 +0000 | [diff] [blame] | 14 | #include "sanitizer_common/sanitizer_flags.h" |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_flag_parser.h" |
Alexey Samsonov | 230c3be | 2012-06-06 09:26:25 +0000 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_libc.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 17 | #include "tsan_flags.h" |
| 18 | #include "tsan_rtl.h" |
| 19 | #include "tsan_mman.h" |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 20 | #include "ubsan/ubsan_flags.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 21 | |
| 22 | namespace __tsan { |
| 23 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 24 | Flags *flags() { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 25 | return &ctx->flags; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // Can be overriden in frontend. |
Dmitry Vyukov | 87dbdf5 | 2012-07-25 14:30:51 +0000 | [diff] [blame] | 29 | #ifdef TSAN_EXTERNAL_HOOKS |
Dmitry Vyukov | f719b92 | 2013-10-15 15:58:11 +0000 | [diff] [blame] | 30 | extern "C" const char* __tsan_default_options(); |
Dmitry Vyukov | 87dbdf5 | 2012-07-25 14:30:51 +0000 | [diff] [blame] | 31 | #else |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 32 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE |
| 33 | const char *WEAK __tsan_default_options() { |
Dmitry Vyukov | f719b92 | 2013-10-15 15:58:11 +0000 | [diff] [blame] | 34 | return ""; |
| 35 | } |
Dmitry Vyukov | 87dbdf5 | 2012-07-25 14:30:51 +0000 | [diff] [blame] | 36 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 37 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 38 | void Flags::SetDefaults() { |
| 39 | #define TSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; |
| 40 | #include "tsan_flags.inc" |
| 41 | #undef TSAN_FLAG |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 42 | // DDFlags |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 43 | second_deadlock_stack = false; |
| 44 | } |
| 45 | |
| 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 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 51 | // DDFlags |
| 52 | RegisterFlag(parser, "second_deadlock_stack", |
| 53 | "Report where each mutex is locked in deadlock reports", |
| 54 | &f->second_deadlock_stack); |
Dmitry Vyukov | f719b92 | 2013-10-15 15:58:11 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 57 | void InitializeFlags(Flags *f, const char *env) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 58 | SetCommonFlagsDefaults(); |
| 59 | { |
| 60 | // Override some common flags defaults. |
| 61 | CommonFlags cf; |
| 62 | cf.CopyFrom(*common_flags()); |
| 63 | cf.allow_addr2line = true; |
| 64 | #ifndef SANITIZER_GO |
| 65 | cf.detect_deadlocks = true; |
| 66 | #endif |
| 67 | cf.print_suppressions = false; |
| 68 | cf.stack_trace_format = " #%n %f %S %M"; |
| 69 | OverrideCommonFlags(cf); |
| 70 | } |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 71 | |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 72 | f->SetDefaults(); |
| 73 | |
| 74 | FlagParser parser; |
| 75 | RegisterTsanFlags(&parser, f); |
| 76 | RegisterCommonFlags(&parser); |
| 77 | |
| 78 | #if TSAN_CONTAINS_UBSAN |
| 79 | __ubsan::Flags *uf = __ubsan::flags(); |
| 80 | uf->SetDefaults(); |
| 81 | |
| 82 | FlagParser ubsan_parser; |
| 83 | __ubsan::RegisterUbsanFlags(&ubsan_parser, uf); |
| 84 | RegisterCommonFlags(&ubsan_parser); |
| 85 | #endif |
| 86 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 87 | // Let a frontend override. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 88 | parser.ParseString(__tsan_default_options()); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 89 | #if TSAN_CONTAINS_UBSAN |
| 90 | const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions(); |
| 91 | ubsan_parser.ParseString(ubsan_default_options); |
| 92 | #endif |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 93 | // Override from command line. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 94 | parser.ParseString(env); |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 95 | #if TSAN_CONTAINS_UBSAN |
| 96 | ubsan_parser.ParseString(GetEnv("UBSAN_OPTIONS")); |
| 97 | #endif |
Dmitry Vyukov | 8a32677 | 2012-11-07 16:14:12 +0000 | [diff] [blame] | 98 | |
Alexey Samsonov | 55c7947 | 2013-11-12 13:59:08 +0000 | [diff] [blame] | 99 | // Sanity check. |
Dmitry Vyukov | 8a32677 | 2012-11-07 16:14:12 +0000 | [diff] [blame] | 100 | if (!f->report_bugs) { |
| 101 | f->report_thread_leaks = false; |
| 102 | f->report_destroy_locked = false; |
| 103 | f->report_signal_unsafe = false; |
| 104 | } |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 105 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 106 | SetVerbosity(common_flags()->verbosity); |
| 107 | |
| 108 | if (Verbosity()) ReportUnrecognizedFlags(); |
| 109 | |
| 110 | if (common_flags()->help) parser.PrintFlagDescriptions(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 111 | |
Dmitry Vyukov | d698edc | 2012-11-28 12:19:50 +0000 | [diff] [blame] | 112 | if (f->history_size < 0 || f->history_size > 7) { |
| 113 | Printf("ThreadSanitizer: incorrect value for history_size" |
| 114 | " (must be [0..7])\n"); |
| 115 | Die(); |
| 116 | } |
Dmitry Vyukov | e3178e8 | 2012-12-18 12:20:55 +0000 | [diff] [blame] | 117 | |
| 118 | if (f->io_sync < 0 || f->io_sync > 2) { |
| 119 | Printf("ThreadSanitizer: incorrect value for io_sync" |
| 120 | " (must be [0..2])\n"); |
| 121 | Die(); |
| 122 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | } // namespace __tsan |