1 //===-- xray_flags.cpp ------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is a part of XRay, a dynamic runtime instrumentation system. 10 // 11 // XRay flag parsing logic. 12 //===----------------------------------------------------------------------===// 13 14 #include "xray_flags.h" 15 #include "sanitizer_common/sanitizer_common.h" 16 #include "sanitizer_common/sanitizer_flag_parser.h" 17 #include "sanitizer_common/sanitizer_libc.h" 18 #include "xray_defs.h" 19 20 using namespace __sanitizer; 21 22 namespace __xray { 23 24 Flags xray_flags_dont_use_directly; // use via flags(). 25 26 void Flags::setDefaults() XRAY_NEVER_INSTRUMENT { 27 #define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue; 28 #include "xray_flags.inc" 29 #undef XRAY_FLAG 30 } 31 32 void registerXRayFlags(FlagParser *P, Flags *F) XRAY_NEVER_INSTRUMENT { 33 #define XRAY_FLAG(Type, Name, DefaultValue, Description) \ 34 RegisterFlag(P, #Name, Description, &F->Name); 35 #include "xray_flags.inc" 36 #undef XRAY_FLAG 37 } 38 39 // This function, as defined with the help of a macro meant to be introduced at 40 // build time of the XRay runtime, passes in a statically defined list of 41 // options that control XRay. This means users/deployments can tweak the 42 // defaults that override the hard-coded defaults in the xray_flags.inc at 43 // compile-time using the XRAY_DEFAULT_OPTIONS macro. 44 const char *useCompilerDefinedFlags() XRAY_NEVER_INSTRUMENT { 45 #ifdef XRAY_DEFAULT_OPTIONS 46 // Do the double-layered string conversion to prevent badly crafted strings 47 // provided through the XRAY_DEFAULT_OPTIONS from causing compilation issues 48 // (or changing the semantics of the implementation through the macro). This 49 // ensures that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a 50 // string literal. 51 return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS); 52 #else 53 return ""; 54 #endif 55 } 56 57 void initializeFlags() XRAY_NEVER_INSTRUMENT { 58 SetCommonFlagsDefaults(); 59 auto *F = flags(); 60 F->setDefaults(); 61 62 FlagParser XRayParser; 63 registerXRayFlags(&XRayParser, F); 64 RegisterCommonFlags(&XRayParser); 65 66 // Use options defaulted at compile-time for the runtime. 67 const char *XRayCompileFlags = useCompilerDefinedFlags(); 68 XRayParser.ParseString(XRayCompileFlags); 69 70 // Use options provided at build time of the instrumented program. 71 const char *XRayDefaultOptions = __xray_default_options(); 72 XRayParser.ParseString(XRayDefaultOptions); 73 74 // Override from environment variables. 75 XRayParser.ParseStringFromEnv("XRAY_OPTIONS"); 76 77 // Override from command line. 78 InitializeCommonFlags(); 79 80 if (Verbosity()) 81 ReportUnrecognizedFlags(); 82 83 if (common_flags()->help) { 84 XRayParser.PrintFlagDescriptions(); 85 } 86 } 87 88 } // namespace __xray 89 90 SANITIZER_INTERFACE_WEAK_DEF(const char *, __xray_default_options, void) { 91 return ""; 92 } 93