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 // If set, the tool will install its own SEGV signal handler by default.
32 #ifndef SANITIZER_NEEDS_SEGV
33 # define SANITIZER_NEEDS_SEGV 1
36 void CommonFlags::SetDefaults() {
37 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
38 #include "sanitizer_flags.inc"
42 void CommonFlags::CopyFrom(const CommonFlags
&other
) {
43 internal_memcpy(this, &other
, sizeof(*this));
46 // Copy the string from "s" to "out", replacing "%b" with the binary basename.
47 static void SubstituteBinaryName(const char *s
, char *out
, uptr out_size
) {
48 char *out_end
= out
+ out_size
;
49 while (*s
&& out
< out_end
- 1) {
50 if (s
[0] != '%' || s
[1] != 'b') { *out
++ = *s
++; continue; }
51 const char *base
= GetProcessName();
53 while (*base
&& out
< out_end
- 1)
60 class FlagHandlerInclude
: public FlagHandlerBase
{
65 explicit FlagHandlerInclude(FlagParser
*parser
, bool ignore_missing
)
66 : parser_(parser
), ignore_missing_(ignore_missing
) {}
67 bool Parse(const char *value
) final
{
68 if (internal_strchr(value
, '%')) {
69 char *buf
= (char *)MmapOrDie(kMaxPathLength
, "FlagHandlerInclude");
70 SubstituteBinaryName(value
, buf
, kMaxPathLength
);
71 bool res
= parser_
->ParseFile(buf
, ignore_missing_
);
72 UnmapOrDie(buf
, kMaxPathLength
);
75 return parser_
->ParseFile(value
, ignore_missing_
);
79 void RegisterIncludeFlags(FlagParser
*parser
, CommonFlags
*cf
) {
80 FlagHandlerInclude
*fh_include
= new (FlagParser::Alloc
) // NOLINT
81 FlagHandlerInclude(parser
, /*ignore_missing*/ false);
82 parser
->RegisterHandler("include", fh_include
,
83 "read more options from the given file");
84 FlagHandlerInclude
*fh_include_if_exists
= new (FlagParser::Alloc
) // NOLINT
85 FlagHandlerInclude(parser
, /*ignore_missing*/ true);
86 parser
->RegisterHandler(
87 "include_if_exists", fh_include_if_exists
,
88 "read more options from the given file (if it exists)");
91 void RegisterCommonFlags(FlagParser
*parser
, CommonFlags
*cf
) {
92 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
93 RegisterFlag(parser, #Name, Description, &cf->Name);
94 #include "sanitizer_flags.inc"
97 RegisterIncludeFlags(parser
, cf
);
100 } // namespace __sanitizer