1 //===-- sanitizer_flags.cc ------------------------------------------------===//
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/AddressSanitizer runtime.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_flags.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_libc.h"
17 #include "sanitizer_list.h"
18 #include "sanitizer_flag_parser.h"
20 namespace __sanitizer {
22 CommonFlags common_flags_dont_use;
24 void CommonFlags::SetDefaults() {
25 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
26 #include "sanitizer_flags.inc"
30 void CommonFlags::CopyFrom(const CommonFlags &other) {
31 internal_memcpy(this, &other, sizeof(*this));
34 // Copy the string from "s" to "out", making the following substitutions:
35 // %b = binary basename
37 void SubstituteForFlagValue(const char *s, char *out, uptr out_size) {
38 char *out_end = out + out_size;
39 while (*s && out < out_end - 1) {
46 const char *base = GetProcessName();
48 while (*base && out < out_end - 1)
54 int pid = internal_getpid();
56 char *buf_pos = buf + 32;
58 *--buf_pos = (pid % 10) + '0';
61 while (buf_pos < buf + 32 && out < out_end - 1)
71 CHECK(out < out_end - 1);
75 class FlagHandlerInclude : public FlagHandlerBase {
80 explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing)
81 : parser_(parser), ignore_missing_(ignore_missing) {}
82 bool Parse(const char *value) final {
83 if (internal_strchr(value, '%')) {
84 char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude");
85 SubstituteForFlagValue(value, buf, kMaxPathLength);
86 bool res = parser_->ParseFile(buf, ignore_missing_);
87 UnmapOrDie(buf, kMaxPathLength);
90 return parser_->ParseFile(value, ignore_missing_);
94 void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) {
95 FlagHandlerInclude *fh_include = new (FlagParser::Alloc) // NOLINT
96 FlagHandlerInclude(parser, /*ignore_missing*/ false);
97 parser->RegisterHandler("include", fh_include,
98 "read more options from the given file");
99 FlagHandlerInclude *fh_include_if_exists = new (FlagParser::Alloc) // NOLINT
100 FlagHandlerInclude(parser, /*ignore_missing*/ true);
101 parser->RegisterHandler(
102 "include_if_exists", fh_include_if_exists,
103 "read more options from the given file (if it exists)");
106 void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
107 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
108 RegisterFlag(parser, #Name, Description, &cf->Name);
109 #include "sanitizer_flags.inc"
112 RegisterIncludeFlags(parser, cf);
115 void InitializeCommonFlags(CommonFlags *cf) {
116 // need to record coverage to generate coverage report.
117 cf->coverage |= cf->html_cov_report;
118 SetVerbosity(cf->verbosity);
121 } // namespace __sanitizer