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 struct FlagDescription
{
27 const char *description
;
28 FlagDescription
*next
;
31 IntrusiveList
<FlagDescription
> flag_descriptions
;
33 void CommonFlags::SetDefaults() {
34 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
35 #include "sanitizer_flags.inc"
39 void CommonFlags::CopyFrom(const CommonFlags
&other
) {
40 internal_memcpy(this, &other
, sizeof(*this));
43 // Copy the string from "s" to "out", making the following substitutions:
44 // %b = binary basename
46 void SubstituteForFlagValue(const char *s
, char *out
, uptr out_size
) {
47 char *out_end
= out
+ out_size
;
48 while (*s
&& out
< out_end
- 1) {
55 const char *base
= GetProcessName();
57 while (*base
&& out
< out_end
- 1)
63 int pid
= internal_getpid();
65 char *buf_pos
= buf
+ 32;
67 *--buf_pos
= (pid
% 10) + '0';
70 while (buf_pos
< buf
+ 32 && out
< out_end
- 1)
80 CHECK(out
< out_end
- 1);
84 class FlagHandlerInclude
: public FlagHandlerBase
{
89 explicit FlagHandlerInclude(FlagParser
*parser
, bool ignore_missing
)
90 : parser_(parser
), ignore_missing_(ignore_missing
) {}
91 bool Parse(const char *value
) final
{
92 if (internal_strchr(value
, '%')) {
93 char *buf
= (char *)MmapOrDie(kMaxPathLength
, "FlagHandlerInclude");
94 SubstituteForFlagValue(value
, buf
, kMaxPathLength
);
95 bool res
= parser_
->ParseFile(buf
, ignore_missing_
);
96 UnmapOrDie(buf
, kMaxPathLength
);
99 return parser_
->ParseFile(value
, ignore_missing_
);
103 void RegisterIncludeFlags(FlagParser
*parser
, CommonFlags
*cf
) {
104 FlagHandlerInclude
*fh_include
= new (FlagParser::Alloc
) // NOLINT
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
) // NOLINT
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)");
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"
121 RegisterIncludeFlags(parser
, cf
);
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
);
130 } // namespace __sanitizer