Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_flags.cpp
35265 views
//===-- xray_flags.cpp ------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of XRay, a dynamic runtime instrumentation system.9//10// XRay flag parsing logic.11//===----------------------------------------------------------------------===//1213#include "xray_flags.h"14#include "sanitizer_common/sanitizer_common.h"15#include "sanitizer_common/sanitizer_flag_parser.h"16#include "sanitizer_common/sanitizer_libc.h"17#include "xray_defs.h"1819using namespace __sanitizer;2021namespace __xray {2223Flags xray_flags_dont_use_directly; // use via flags().2425void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {26#define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;27#include "xray_flags.inc"28#undef XRAY_FLAG29}3031void registerXRayFlags(FlagParser *P, Flags *F) XRAY_NEVER_INSTRUMENT {32#define XRAY_FLAG(Type, Name, DefaultValue, Description) \33RegisterFlag(P, #Name, Description, &F->Name);34#include "xray_flags.inc"35#undef XRAY_FLAG36}3738// This function, as defined with the help of a macro meant to be introduced at39// build time of the XRay runtime, passes in a statically defined list of40// options that control XRay. This means users/deployments can tweak the41// defaults that override the hard-coded defaults in the xray_flags.inc at42// compile-time using the XRAY_DEFAULT_OPTIONS macro.43const char *useCompilerDefinedFlags() XRAY_NEVER_INSTRUMENT {44#ifdef XRAY_DEFAULT_OPTIONS45// Do the double-layered string conversion to prevent badly crafted strings46// provided through the XRAY_DEFAULT_OPTIONS from causing compilation issues47// (or changing the semantics of the implementation through the macro). This48// ensures that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a49// string literal.50return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS);51#else52return "";53#endif54}5556void initializeFlags() XRAY_NEVER_INSTRUMENT {57SetCommonFlagsDefaults();58auto *F = flags();59F->setDefaults();6061FlagParser XRayParser;62registerXRayFlags(&XRayParser, F);63RegisterCommonFlags(&XRayParser);6465// Use options defaulted at compile-time for the runtime.66const char *XRayCompileFlags = useCompilerDefinedFlags();67XRayParser.ParseString(XRayCompileFlags);6869// Override from environment variables.70XRayParser.ParseStringFromEnv("XRAY_OPTIONS");7172// Override from command line.73InitializeCommonFlags();7475if (Verbosity())76ReportUnrecognizedFlags();7778if (common_flags()->help) {79XRayParser.PrintFlagDescriptions();80}81}8283} // namespace __xray848586