]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_flags.cc
Merge clang, llvm, lld, lldb, compiler-rt and libc++ 5.0.0 release.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_flags.cc
1 //===-- xray_flags.cc -------------------------------------------*- C++ -*-===//
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 XRay, a dynamic runtime instrumentation system.
11 //
12 // XRay flag parsing logic.
13 //===----------------------------------------------------------------------===//
14
15 #include "xray_flags.h"
16 #include "sanitizer_common/sanitizer_common.h"
17 #include "sanitizer_common/sanitizer_flag_parser.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19 #include "xray_defs.h"
20
21 using namespace __sanitizer;
22
23 namespace __xray {
24
25 Flags xray_flags_dont_use_directly; // use via flags().
26
27 void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {
28 #define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
29 #include "xray_flags.inc"
30 #undef XRAY_FLAG
31 }
32
33 static void registerXRayFlags(FlagParser *P, Flags *F) XRAY_NEVER_INSTRUMENT {
34 #define XRAY_FLAG(Type, Name, DefaultValue, Description)                       \
35   RegisterFlag(P, #Name, Description, &F->Name);
36 #include "xray_flags.inc"
37 #undef XRAY_FLAG
38 }
39
40 // This function, as defined with the help of a macro meant to be introduced at
41 // build time of the XRay runtime, passes in a statically defined list of
42 // options that control XRay. This means users/deployments can tweak the
43 // defaults that override the hard-coded defaults in the xray_flags.inc at
44 // compile-time using the XRAY_DEFAULT_OPTIONS macro.
45 static const char *useCompilerDefinedFlags() XRAY_NEVER_INSTRUMENT {
46 #ifdef XRAY_DEFAULT_OPTIONS
47 // Do the double-layered string conversion to prevent badly crafted strings
48 // provided through the XRAY_DEFAULT_OPTIONS from causing compilation issues (or
49 // changing the semantics of the implementation through the macro). This ensures
50 // that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a string literal.
51 #define XRAY_STRINGIZE(x) #x
52 #define XRAY_STRINGIZE_OPTIONS(options) XRAY_STRINGIZE(options)
53   return XRAY_STRINGIZE_OPTIONS(XRAY_DEFAULT_OPTIONS);
54 #else
55   return "";
56 #endif
57 }
58
59 void initializeFlags() XRAY_NEVER_INSTRUMENT {
60   SetCommonFlagsDefaults();
61   auto *F = flags();
62   F->setDefaults();
63
64   FlagParser XRayParser;
65   registerXRayFlags(&XRayParser, F);
66   RegisterCommonFlags(&XRayParser);
67
68   // Use options defaulted at compile-time for the runtime.
69   const char *XRayCompileFlags = useCompilerDefinedFlags();
70   XRayParser.ParseString(XRayCompileFlags);
71
72   // Override from environment variables.
73   XRayParser.ParseString(GetEnv("XRAY_OPTIONS"));
74
75   // Override from command line.
76   InitializeCommonFlags();
77
78   if (Verbosity())
79     ReportUnrecognizedFlags();
80
81   if (common_flags()->help) {
82     XRayParser.PrintFlagDescriptions();
83   }
84 }
85
86 } // namespace __xray