1 //===-- sanitizer_flags.cc ------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_flags.h"
14 #include "sanitizer_common.h"
15 #include "sanitizer_libc.h"
16 #include "sanitizer_list.h"
17 #include "sanitizer_flag_parser.h"
19 namespace __sanitizer
{
21 CommonFlags common_flags_dont_use
;
23 struct FlagDescription
{
25 const char *description
;
26 FlagDescription
*next
;
29 IntrusiveList
<FlagDescription
> flag_descriptions
;
31 void CommonFlags::SetDefaults() {
32 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
33 #include "sanitizer_flags.inc"
37 void CommonFlags::CopyFrom(const CommonFlags
&other
) {
38 internal_memcpy(this, &other
, sizeof(*this));
41 // Copy the string from "s" to "out", making the following substitutions:
42 // %b = binary basename
44 void SubstituteForFlagValue(const char *s
, char *out
, uptr out_size
) {
45 char *out_end
= out
+ out_size
;
46 while (*s
&& out
< out_end
- 1) {
53 const char *base
= GetProcessName();
55 while (*base
&& out
< out_end
- 1)
61 int pid
= internal_getpid();
63 char *buf_pos
= buf
+ 32;
65 *--buf_pos
= (pid
% 10) + '0';
68 while (buf_pos
< buf
+ 32 && out
< out_end
- 1)
78 CHECK(out
< out_end
- 1);
82 class FlagHandlerInclude
: public FlagHandlerBase
{
87 explicit FlagHandlerInclude(FlagParser
*parser
, bool ignore_missing
)
88 : parser_(parser
), ignore_missing_(ignore_missing
) {}
89 bool Parse(const char *value
) final
{
90 if (internal_strchr(value
, '%')) {
91 char *buf
= (char *)MmapOrDie(kMaxPathLength
, "FlagHandlerInclude");
92 SubstituteForFlagValue(value
, buf
, kMaxPathLength
);
93 bool res
= parser_
->ParseFile(buf
, ignore_missing_
);
94 UnmapOrDie(buf
, kMaxPathLength
);
97 return parser_
->ParseFile(value
, ignore_missing_
);
101 void RegisterIncludeFlags(FlagParser
*parser
, CommonFlags
*cf
) {
102 FlagHandlerInclude
*fh_include
= new (FlagParser::Alloc
) // NOLINT
103 FlagHandlerInclude(parser
, /*ignore_missing*/ false);
104 parser
->RegisterHandler("include", fh_include
,
105 "read more options from the given file");
106 FlagHandlerInclude
*fh_include_if_exists
= new (FlagParser::Alloc
) // NOLINT
107 FlagHandlerInclude(parser
, /*ignore_missing*/ true);
108 parser
->RegisterHandler(
109 "include_if_exists", fh_include_if_exists
,
110 "read more options from the given file (if it exists)");
113 void RegisterCommonFlags(FlagParser
*parser
, CommonFlags
*cf
) {
114 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
115 RegisterFlag(parser, #Name, Description, &cf->Name);
116 #include "sanitizer_flags.inc"
119 RegisterIncludeFlags(parser
, cf
);
122 void InitializeCommonFlags(CommonFlags
*cf
) {
123 // need to record coverage to generate coverage report.
124 cf
->coverage
|= cf
->html_cov_report
;
125 SetVerbosity(cf
->verbosity
);
128 } // namespace __sanitizer