93 lines · cpp
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-exception6//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 20using namespace __sanitizer;21 22namespace __xray {23 24Flags xray_flags_dont_use_directly; // use via flags().25 26void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {27#define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;28#include "xray_flags.inc"29#undef XRAY_FLAG30}31 32void 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_FLAG37}38 39// This function, as defined with the help of a macro meant to be introduced at40// build time of the XRay runtime, passes in a statically defined list of41// options that control XRay. This means users/deployments can tweak the42// defaults that override the hard-coded defaults in the xray_flags.inc at43// compile-time using the XRAY_DEFAULT_OPTIONS macro.44const char *useCompilerDefinedFlags() XRAY_NEVER_INSTRUMENT {45#ifdef XRAY_DEFAULT_OPTIONS46 // Do the double-layered string conversion to prevent badly crafted strings47 // provided through the XRAY_DEFAULT_OPTIONS from causing compilation issues48 // (or changing the semantics of the implementation through the macro). This49 // ensures that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a50 // string literal.51 return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS);52#else53 return "";54#endif55}56 57void 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 __xray89 90SANITIZER_INTERFACE_WEAK_DEF(const char *, __xray_default_options, void) {91 return "";92}93