Home | History | Annotate | Line # | Download | only in xray
      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 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 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
     51   // string literal.
     52   return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS);
     53 #else
     54   return "";
     55 #endif
     56 }
     57 
     58 void initializeFlags() XRAY_NEVER_INSTRUMENT {
     59   SetCommonFlagsDefaults();
     60   auto *F = flags();
     61   F->setDefaults();
     62 
     63   FlagParser XRayParser;
     64   registerXRayFlags(&XRayParser, F);
     65   RegisterCommonFlags(&XRayParser);
     66 
     67   // Use options defaulted at compile-time for the runtime.
     68   const char *XRayCompileFlags = useCompilerDefinedFlags();
     69   XRayParser.ParseString(XRayCompileFlags);
     70 
     71   // Override from environment variables.
     72   XRayParser.ParseString(GetEnv("XRAY_OPTIONS"));
     73 
     74   // Override from command line.
     75   InitializeCommonFlags();
     76 
     77   if (Verbosity())
     78     ReportUnrecognizedFlags();
     79 
     80   if (common_flags()->help) {
     81     XRayParser.PrintFlagDescriptions();
     82   }
     83 }
     84 
     85 } // namespace __xray
     86