1 //===-- sanitizer_flags.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_flags.h"
16 #include "sanitizer_common.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_list.h"
19 #include "sanitizer_flag_parser.h"
21 namespace __sanitizer {
23 CommonFlags common_flags_dont_use;
25 void CommonFlags::SetDefaults() {
26 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
27 #include "sanitizer_flags.inc"
31 void CommonFlags::CopyFrom(const CommonFlags &other) {
32 internal_memcpy(this, &other, sizeof(*this));
35 // Copy the string from "s" to "out", making the following substitutions:
36 // %b = binary basename
38 void SubstituteForFlagValue(const char *s, char *out, uptr out_size) {
39 char *out_end = out + out_size;
40 while (*s && out < out_end - 1) {
47 const char *base = GetProcessName();
49 while (*base && out < out_end - 1)
55 int pid = internal_getpid();
57 char *buf_pos = buf + 32;
59 *--buf_pos = (pid % 10) + '0';
62 while (buf_pos < buf + 32 && out < out_end - 1)
72 CHECK(out < out_end - 1);
76 class FlagHandlerInclude : public FlagHandlerBase {
81 explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing)
82 : parser_(parser), ignore_missing_(ignore_missing) {}
83 bool Parse(const char *value) final {
84 if (internal_strchr(value, '%')) {
85 char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude");
86 SubstituteForFlagValue(value, buf, kMaxPathLength);
87 bool res = parser_->ParseFile(buf, ignore_missing_);
88 UnmapOrDie(buf, kMaxPathLength);
91 return parser_->ParseFile(value, ignore_missing_);
95 void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) {
96 FlagHandlerInclude *fh_include = new (FlagParser::Alloc) // NOLINT
97 FlagHandlerInclude(parser, /*ignore_missing*/ false);
98 parser->RegisterHandler("include", fh_include,
99 "read more options from the given file");
100 FlagHandlerInclude *fh_include_if_exists = new (FlagParser::Alloc) // NOLINT
101 FlagHandlerInclude(parser, /*ignore_missing*/ true);
102 parser->RegisterHandler(
103 "include_if_exists", fh_include_if_exists,
104 "read more options from the given file (if it exists)");
107 void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
108 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
109 RegisterFlag(parser, #Name, Description, &cf->Name);
110 #include "sanitizer_flags.inc"
113 RegisterIncludeFlags(parser, cf);
116 void InitializeCommonFlags(CommonFlags *cf) {
117 // need to record coverage to generate coverage report.
118 cf->coverage |= cf->html_cov_report;
119 SetVerbosity(cf->verbosity);
122 } // namespace __sanitizer