summaryrefslogtreecommitdiff
path: root/lib/xray/xray_flags.cc
blob: 1ee4d10d753cd85f09247d5f7708590ee9bc1036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//===-- xray_flags.cc -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a dynamic runtime instrumentation system.
//
// XRay flag parsing logic.
//===----------------------------------------------------------------------===//

#include "xray_flags.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_flag_parser.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "xray_defs.h"

using namespace __sanitizer;

namespace __xray {

Flags xray_flags_dont_use_directly; // use via flags().

void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {
#define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
#include "xray_flags.inc"
#undef XRAY_FLAG
}

static void registerXRayFlags(FlagParser *P, Flags *F) XRAY_NEVER_INSTRUMENT {
#define XRAY_FLAG(Type, Name, DefaultValue, Description)                       \
  RegisterFlag(P, #Name, Description, &F->Name);
#include "xray_flags.inc"
#undef XRAY_FLAG
}

// This function, as defined with the help of a macro meant to be introduced at
// build time of the XRay runtime, passes in a statically defined list of
// options that control XRay. This means users/deployments can tweak the
// defaults that override the hard-coded defaults in the xray_flags.inc at
// compile-time using the XRAY_DEFAULT_OPTIONS macro.
static const char *useCompilerDefinedFlags() XRAY_NEVER_INSTRUMENT {
#ifdef XRAY_DEFAULT_OPTIONS
// Do the double-layered string conversion to prevent badly crafted strings
// provided through the XRAY_DEFAULT_OPTIONS from causing compilation issues (or
// changing the semantics of the implementation through the macro). This ensures
// that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a string literal.
#define XRAY_STRINGIZE(x) #x
#define XRAY_STRINGIZE_OPTIONS(options) XRAY_STRINGIZE(options)
  return XRAY_STRINGIZE_OPTIONS(XRAY_DEFAULT_OPTIONS);
#else
  return "";
#endif
}

void initializeFlags() XRAY_NEVER_INSTRUMENT {
  SetCommonFlagsDefaults();
  auto *F = flags();
  F->setDefaults();

  FlagParser XRayParser;
  registerXRayFlags(&XRayParser, F);
  RegisterCommonFlags(&XRayParser);

  // Use options defaulted at compile-time for the runtime.
  const char *XRayCompileFlags = useCompilerDefinedFlags();
  XRayParser.ParseString(XRayCompileFlags);

  // Override from environment variables.
  XRayParser.ParseString(GetEnv("XRAY_OPTIONS"));

  // Override from command line.
  InitializeCommonFlags();

  if (Verbosity())
    ReportUnrecognizedFlags();

  if (common_flags()->help) {
    XRayParser.PrintFlagDescriptions();
  }
}

} // namespace __xray