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 void CommonFlags::SetDefaults() {
24 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
25 #include "sanitizer_flags.inc"
29 void CommonFlags::CopyFrom(const CommonFlags
&other
) {
30 internal_memcpy(this, &other
, sizeof(*this));
33 // Copy the string from "s" to "out", making the following substitutions:
34 // %b = binary basename
36 void SubstituteForFlagValue(const char *s
, char *out
, uptr out_size
) {
37 char *out_end
= out
+ out_size
;
38 while (*s
&& out
< out_end
- 1) {
45 const char *base
= GetProcessName();
47 while (*base
&& out
< out_end
- 1)
53 int pid
= internal_getpid();
55 char *buf_pos
= buf
+ 32;
57 *--buf_pos
= (pid
% 10) + '0';
60 while (buf_pos
< buf
+ 32 && out
< out_end
- 1)
70 CHECK(out
< out_end
- 1);
74 class FlagHandlerInclude
: public FlagHandlerBase
{
79 explicit FlagHandlerInclude(FlagParser
*parser
, bool ignore_missing
)
80 : parser_(parser
), ignore_missing_(ignore_missing
) {}
81 bool Parse(const char *value
) final
{
82 if (internal_strchr(value
, '%')) {
83 char *buf
= (char *)MmapOrDie(kMaxPathLength
, "FlagHandlerInclude");
84 SubstituteForFlagValue(value
, buf
, kMaxPathLength
);
85 bool res
= parser_
->ParseFile(buf
, ignore_missing_
);
86 UnmapOrDie(buf
, kMaxPathLength
);
89 return parser_
->ParseFile(value
, ignore_missing_
);
93 void RegisterIncludeFlags(FlagParser
*parser
, CommonFlags
*cf
) {
94 FlagHandlerInclude
*fh_include
= new (FlagParser::Alloc
) // NOLINT
95 FlagHandlerInclude(parser
, /*ignore_missing*/ false);
96 parser
->RegisterHandler("include", fh_include
,
97 "read more options from the given file");
98 FlagHandlerInclude
*fh_include_if_exists
= new (FlagParser::Alloc
) // NOLINT
99 FlagHandlerInclude(parser
, /*ignore_missing*/ true);
100 parser
->RegisterHandler(
101 "include_if_exists", fh_include_if_exists
,
102 "read more options from the given file (if it exists)");
105 void RegisterCommonFlags(FlagParser
*parser
, CommonFlags
*cf
) {
106 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
107 RegisterFlag(parser, #Name, Description, &cf->Name);
108 #include "sanitizer_flags.inc"
111 RegisterIncludeFlags(parser
, cf
);
114 void InitializeCommonFlags(CommonFlags
*cf
) {
115 // need to record coverage to generate coverage report.
116 cf
->coverage
|= cf
->html_cov_report
;
117 SetVerbosity(cf
->verbosity
);
120 } // namespace __sanitizer