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 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 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
49 // (or changing the semantics of the implementation through the macro). This
50 // ensures that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a
52 return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS);
58 void initializeFlags() XRAY_NEVER_INSTRUMENT {
59 SetCommonFlagsDefaults();
63 FlagParser XRayParser;
64 registerXRayFlags(&XRayParser, F);
65 RegisterCommonFlags(&XRayParser);
67 // Use options defaulted at compile-time for the runtime.
68 const char *XRayCompileFlags = useCompilerDefinedFlags();
69 XRayParser.ParseString(XRayCompileFlags);
71 // Override from environment variables.
72 XRayParser.ParseString(GetEnv("XRAY_OPTIONS"));
74 // Override from command line.
75 InitializeCommonFlags();
78 ReportUnrecognizedFlags();
80 if (common_flags()->help) {
81 XRayParser.PrintFlagDescriptions();