1 //===-- sanitizer_flag_parser.h ---------------------------------*- C++ -*-===//
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 #ifndef SANITIZER_FLAG_REGISTRY_H
13 #define SANITIZER_FLAG_REGISTRY_H
15 #include "sanitizer_internal_defs.h"
16 #include "sanitizer_libc.h"
17 #include "sanitizer_common.h"
19 namespace __sanitizer
{
21 class FlagHandlerBase
{
23 virtual bool Parse(const char *value
) { return false; }
27 class FlagHandler
: public FlagHandlerBase
{
31 explicit FlagHandler(T
*t
) : t_(t
) {}
32 bool Parse(const char *value
) final
;
35 inline bool ParseBool(const char *value
, bool *b
) {
36 if (internal_strcmp(value
, "0") == 0 ||
37 internal_strcmp(value
, "no") == 0 ||
38 internal_strcmp(value
, "false") == 0) {
42 if (internal_strcmp(value
, "1") == 0 ||
43 internal_strcmp(value
, "yes") == 0 ||
44 internal_strcmp(value
, "true") == 0) {
52 inline bool FlagHandler
<bool>::Parse(const char *value
) {
53 if (ParseBool(value
, t_
)) return true;
54 Printf("ERROR: Invalid value for bool option: '%s'\n", value
);
59 inline bool FlagHandler
<HandleSignalMode
>::Parse(const char *value
) {
61 if (ParseBool(value
, &b
)) {
62 *t_
= b
? kHandleSignalYes
: kHandleSignalNo
;
65 if (internal_strcmp(value
, "2") == 0 ||
66 internal_strcmp(value
, "exclusive") == 0) {
67 *t_
= kHandleSignalExclusive
;
70 Printf("ERROR: Invalid value for signal handler option: '%s'\n", value
);
75 inline bool FlagHandler
<const char *>::Parse(const char *value
) {
81 inline bool FlagHandler
<int>::Parse(const char *value
) {
83 *t_
= internal_simple_strtoll(value
, &value_end
, 10);
84 bool ok
= *value_end
== 0;
85 if (!ok
) Printf("ERROR: Invalid value for int option: '%s'\n", value
);
90 inline bool FlagHandler
<uptr
>::Parse(const char *value
) {
92 *t_
= internal_simple_strtoll(value
, &value_end
, 10);
93 bool ok
= *value_end
== 0;
94 if (!ok
) Printf("ERROR: Invalid value for uptr option: '%s'\n", value
);
99 static const int kMaxFlags
= 200;
103 FlagHandlerBase
*handler
;
112 void RegisterHandler(const char *name
, FlagHandlerBase
*handler
,
114 void ParseString(const char *s
);
115 bool ParseFile(const char *path
, bool ignore_missing
);
116 void PrintFlagDescriptions();
118 static LowLevelAllocator Alloc
;
121 void fatal_error(const char *err
);
122 bool is_space(char c
);
123 void skip_whitespace();
126 bool run_handler(const char *name
, const char *value
);
127 char *ll_strndup(const char *s
, uptr n
);
130 template <typename T
>
131 static void RegisterFlag(FlagParser
*parser
, const char *name
, const char *desc
,
133 FlagHandler
<T
> *fh
= new (FlagParser::Alloc
) FlagHandler
<T
>(var
); // NOLINT
134 parser
->RegisterHandler(name
, fh
, desc
);
137 void ReportUnrecognizedFlags();
139 } // namespace __sanitizer
141 #endif // SANITIZER_FLAG_REGISTRY_H