runtime: add "success" field to sudog
[gcc.git] / libsanitizer / sanitizer_common / sanitizer_flags.cpp
1 //===-- sanitizer_flags.cpp -----------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "sanitizer_flags.h"
14
15 #include "sanitizer_common.h"
16 #include "sanitizer_flag_parser.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_linux.h"
19 #include "sanitizer_list.h"
20
21 namespace __sanitizer {
22
23 CommonFlags common_flags_dont_use;
24
25 void CommonFlags::SetDefaults() {
26 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
27 #include "sanitizer_flags.inc"
28 #undef COMMON_FLAG
29 }
30
31 void CommonFlags::CopyFrom(const CommonFlags &other) {
32 internal_memcpy(this, &other, sizeof(*this));
33 }
34
35 // Copy the string from "s" to "out", making the following substitutions:
36 // %b = binary basename
37 // %p = pid
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) {
41 if (s[0] != '%') {
42 *out++ = *s++;
43 continue;
44 }
45 switch (s[1]) {
46 case 'b': {
47 const char *base = GetProcessName();
48 CHECK(base);
49 while (*base && out < out_end - 1)
50 *out++ = *base++;
51 s += 2; // skip "%b"
52 break;
53 }
54 case 'p': {
55 int pid = internal_getpid();
56 char buf[32];
57 char *buf_pos = buf + 32;
58 do {
59 *--buf_pos = (pid % 10) + '0';
60 pid /= 10;
61 } while (pid);
62 while (buf_pos < buf + 32 && out < out_end - 1)
63 *out++ = *buf_pos++;
64 s += 2; // skip "%p"
65 break;
66 }
67 default:
68 *out++ = *s++;
69 break;
70 }
71 }
72 CHECK(out < out_end - 1);
73 *out = '\0';
74 }
75
76 class FlagHandlerInclude final : public FlagHandlerBase {
77 FlagParser *parser_;
78 bool ignore_missing_;
79 const char *original_path_;
80
81 public:
82 explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing)
83 : parser_(parser), ignore_missing_(ignore_missing), original_path_("") {}
84 bool Parse(const char *value) final {
85 original_path_ = value;
86 if (internal_strchr(value, '%')) {
87 char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude");
88 SubstituteForFlagValue(value, buf, kMaxPathLength);
89 bool res = parser_->ParseFile(buf, ignore_missing_);
90 UnmapOrDie(buf, kMaxPathLength);
91 return res;
92 }
93 return parser_->ParseFile(value, ignore_missing_);
94 }
95 bool Format(char *buffer, uptr size) override {
96 // Note `original_path_` isn't actually what's parsed due to `%`
97 // substitutions. Printing the substituted path would require holding onto
98 // mmap'ed memory.
99 return FormatString(buffer, size, original_path_);
100 }
101 };
102
103 void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) {
104 FlagHandlerInclude *fh_include = new (FlagParser::Alloc)
105 FlagHandlerInclude(parser, /*ignore_missing*/ false);
106 parser->RegisterHandler("include", fh_include,
107 "read more options from the given file");
108 FlagHandlerInclude *fh_include_if_exists = new (FlagParser::Alloc)
109 FlagHandlerInclude(parser, /*ignore_missing*/ true);
110 parser->RegisterHandler(
111 "include_if_exists", fh_include_if_exists,
112 "read more options from the given file (if it exists)");
113 }
114
115 void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
116 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
117 RegisterFlag(parser, #Name, Description, &cf->Name);
118 #include "sanitizer_flags.inc"
119 #undef COMMON_FLAG
120
121 RegisterIncludeFlags(parser, cf);
122 }
123
124 void InitializeCommonFlags(CommonFlags *cf) {
125 // need to record coverage to generate coverage report.
126 cf->coverage |= cf->html_cov_report;
127 SetVerbosity(cf->verbosity);
128
129 InitializePlatformCommonFlags(cf);
130 }
131
132 } // namespace __sanitizer