blob: 01098a3d6b87e76036ce479024fdcfdfba75144f [file] [log] [blame]
Alexey Samsonovf3be7062012-07-09 13:21:39 +00001//===-- sanitizer_flags.cc ------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer/AddressSanitizer runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "sanitizer_flags.h"
15
16#include "sanitizer_common.h"
17#include "sanitizer_libc.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -070018#include "sanitizer_list.h"
Stephen Hines86277eb2015-03-23 12:06:32 -070019#include "sanitizer_flag_parser.h"
Alexey Samsonovf3be7062012-07-09 13:21:39 +000020
21namespace __sanitizer {
22
Stephen Hines2d1fdb22014-05-28 23:58:16 -070023CommonFlags common_flags_dont_use;
24
25struct FlagDescription {
26 const char *name;
27 const char *description;
28 FlagDescription *next;
29};
30
31IntrusiveList<FlagDescription> flag_descriptions;
32
Stephen Hines6a211c52014-07-21 00:49:56 -070033// If set, the tool will install its own SEGV signal handler by default.
34#ifndef SANITIZER_NEEDS_SEGV
35# define SANITIZER_NEEDS_SEGV 1
36#endif
37
Stephen Hines86277eb2015-03-23 12:06:32 -070038void CommonFlags::SetDefaults() {
39#define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
40#include "sanitizer_flags.inc"
41#undef COMMON_FLAG
Alexey Samsonov55c79472013-11-12 13:59:08 +000042}
43
Stephen Hines86277eb2015-03-23 12:06:32 -070044void CommonFlags::CopyFrom(const CommonFlags &other) {
45 internal_memcpy(this, &other, sizeof(*this));
Sergey Matveeved20ebe2013-05-06 11:27:58 +000046}
47
Stephen Hines86277eb2015-03-23 12:06:32 -070048class FlagHandlerInclude : public FlagHandlerBase {
49 static const uptr kMaxIncludeSize = 1 << 15;
50 FlagParser *parser_;
51
52 public:
53 explicit FlagHandlerInclude(FlagParser *parser) : parser_(parser) {}
54 bool Parse(const char *value) final {
55 char *data;
56 uptr data_mapped_size;
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070057 error_t err;
Stephen Hines86277eb2015-03-23 12:06:32 -070058 uptr len =
59 ReadFileToBuffer(value, &data, &data_mapped_size,
60 Max(kMaxIncludeSize, GetPageSizeCached()), &err);
61 if (!len) {
62 Printf("Failed to read options from '%s': error %d\n", value, err);
Dmitry Vyukov5d834a82013-03-27 17:59:13 +000063 return false;
Dmitry Vyukov5d834a82013-03-27 17:59:13 +000064 }
Stephen Hines86277eb2015-03-23 12:06:32 -070065 parser_->ParseString(data);
66 UnmapOrDie(data, data_mapped_size);
67 return true;
Dmitry Vyukov5d834a82013-03-27 17:59:13 +000068 }
Stephen Hines86277eb2015-03-23 12:06:32 -070069};
70
71void RegisterIncludeFlag(FlagParser *parser, CommonFlags *cf) {
72 FlagHandlerInclude *fh_include =
73 new (FlagParser::Alloc) FlagHandlerInclude(parser); // NOLINT
74 parser->RegisterHandler("include", fh_include,
75 "read more options from the given file");
Alexey Samsonov947fbd12012-08-27 14:04:54 +000076}
77
Stephen Hines86277eb2015-03-23 12:06:32 -070078void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
79#define COMMON_FLAG(Type, Name, DefaultValue, Description) \
80 RegisterFlag(parser, #Name, Description, &cf->Name);
81#include "sanitizer_flags.inc"
82#undef COMMON_FLAG
Alexey Samsonovf3be7062012-07-09 13:21:39 +000083
Stephen Hines86277eb2015-03-23 12:06:32 -070084 RegisterIncludeFlag(parser, cf);
Alexey Samsonovf3be7062012-07-09 13:21:39 +000085}
86
87} // namespace __sanitizer