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
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_FLAG
30}
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_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.
44const 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
57void initializeFlags() XRAY_NEVER_INSTRUMENT {
58 SetCommonFlagsDefaults();
59 auto *F = flags();
60 F->setDefaults();
61
62 FlagParser XRayParser;
63 registerXRayFlags(P: &XRayParser, F);
64 RegisterCommonFlags(parser: &XRayParser);
65
66 // Use options defaulted at compile-time for the runtime.
67 const char *XRayCompileFlags = useCompilerDefinedFlags();
68 XRayParser.ParseString(s: XRayCompileFlags);
69
70 // Override from environment variables.
71 XRayParser.ParseStringFromEnv(env_name: "XRAY_OPTIONS");
72
73 // Override from command line.
74 InitializeCommonFlags();
75
76 if (Verbosity())
77 ReportUnrecognizedFlags();
78
79 if (common_flags()->help) {
80 XRayParser.PrintFlagDescriptions();
81 }
82}
83
84} // namespace __xray
85