1 //===-- xray_flags.cc -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
12 // XRay flag parsing logic.
13 //===----------------------------------------------------------------------===//
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"
21 using namespace __sanitizer;
25 Flags xray_flags_dont_use_directly; // use via flags().
27 void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {
28 #define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
29 #include "xray_flags.inc"
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"
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);
59 void initializeFlags() XRAY_NEVER_INSTRUMENT {
60 SetCommonFlagsDefaults();
64 FlagParser XRayParser;
65 registerXRayFlags(&XRayParser, F);
66 RegisterCommonFlags(&XRayParser);
68 // Use options defaulted at compile-time for the runtime.
69 const char *XRayCompileFlags = useCompilerDefinedFlags();
70 XRayParser.ParseString(XRayCompileFlags);
72 // Override from environment variables.
73 XRayParser.ParseString(GetEnv("XRAY_OPTIONS"));
75 // Override from command line.
76 InitializeCommonFlags();
79 ReportUnrecognizedFlags();
81 if (common_flags()->help) {
82 XRayParser.PrintFlagDescriptions();