1 //===-- xray_flags.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of XRay, a dynamic runtime instrumentation system.
11 // XRay flag parsing logic.
12 //===----------------------------------------------------------------------===//
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"
20 using namespace __sanitizer;
24 Flags xray_flags_dont_use_directly; // use via flags().
26 void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {
27 #define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
28 #include "xray_flags.inc"
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"
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
51 return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS);
57 void initializeFlags() XRAY_NEVER_INSTRUMENT {
58 SetCommonFlagsDefaults();
62 FlagParser XRayParser;
63 registerXRayFlags(&XRayParser, F);
64 RegisterCommonFlags(&XRayParser);
66 // Use options defaulted at compile-time for the runtime.
67 const char *XRayCompileFlags = useCompilerDefinedFlags();
68 XRayParser.ParseString(XRayCompileFlags);
70 // Override from environment variables.
71 XRayParser.ParseStringFromEnv("XRAY_OPTIONS");
73 // Override from command line.
74 InitializeCommonFlags();
77 ReportUnrecognizedFlags();
79 if (common_flags()->help) {
80 XRayParser.PrintFlagDescriptions();