xref: /freebsd/contrib/llvm-project/compiler-rt/lib/xray/xray_flags.cpp (revision 5b27928474e6a4103d65b347544705c40c9618fd)
1*68d75effSDimitry Andric //===-- xray_flags.cpp ------------------------------------------*- C++ -*-===//
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*68d75effSDimitry Andric //
7*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
8*68d75effSDimitry Andric //
9*68d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
10*68d75effSDimitry Andric //
11*68d75effSDimitry Andric // XRay flag parsing logic.
12*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
13*68d75effSDimitry Andric 
14*68d75effSDimitry Andric #include "xray_flags.h"
15*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
16*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_flag_parser.h"
17*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_libc.h"
18*68d75effSDimitry Andric #include "xray_defs.h"
19*68d75effSDimitry Andric 
20*68d75effSDimitry Andric using namespace __sanitizer;
21*68d75effSDimitry Andric 
22*68d75effSDimitry Andric namespace __xray {
23*68d75effSDimitry Andric 
24*68d75effSDimitry Andric Flags xray_flags_dont_use_directly; // use via flags().
25*68d75effSDimitry Andric 
setDefaults()26*68d75effSDimitry Andric void Flags::setDefaults() XRAY_NEVER_INSTRUMENT {
27*68d75effSDimitry Andric #define XRAY_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
28*68d75effSDimitry Andric #include "xray_flags.inc"
29*68d75effSDimitry Andric #undef XRAY_FLAG
30*68d75effSDimitry Andric }
31*68d75effSDimitry Andric 
registerXRayFlags(FlagParser * P,Flags * F)32*68d75effSDimitry Andric void registerXRayFlags(FlagParser *P, Flags *F) XRAY_NEVER_INSTRUMENT {
33*68d75effSDimitry Andric #define XRAY_FLAG(Type, Name, DefaultValue, Description)                       \
34*68d75effSDimitry Andric   RegisterFlag(P, #Name, Description, &F->Name);
35*68d75effSDimitry Andric #include "xray_flags.inc"
36*68d75effSDimitry Andric #undef XRAY_FLAG
37*68d75effSDimitry Andric }
38*68d75effSDimitry Andric 
39*68d75effSDimitry Andric // This function, as defined with the help of a macro meant to be introduced at
40*68d75effSDimitry Andric // build time of the XRay runtime, passes in a statically defined list of
41*68d75effSDimitry Andric // options that control XRay. This means users/deployments can tweak the
42*68d75effSDimitry Andric // defaults that override the hard-coded defaults in the xray_flags.inc at
43*68d75effSDimitry Andric // compile-time using the XRAY_DEFAULT_OPTIONS macro.
useCompilerDefinedFlags()44*68d75effSDimitry Andric const char *useCompilerDefinedFlags() XRAY_NEVER_INSTRUMENT {
45*68d75effSDimitry Andric #ifdef XRAY_DEFAULT_OPTIONS
46*68d75effSDimitry Andric   // Do the double-layered string conversion to prevent badly crafted strings
47*68d75effSDimitry Andric   // provided through the XRAY_DEFAULT_OPTIONS from causing compilation issues
48*68d75effSDimitry Andric   // (or changing the semantics of the implementation through the macro). This
49*68d75effSDimitry Andric   // ensures that we convert whatever XRAY_DEFAULT_OPTIONS is defined as a
50*68d75effSDimitry Andric   // string literal.
51*68d75effSDimitry Andric   return SANITIZER_STRINGIFY(XRAY_DEFAULT_OPTIONS);
52*68d75effSDimitry Andric #else
53*68d75effSDimitry Andric   return "";
54*68d75effSDimitry Andric #endif
55*68d75effSDimitry Andric }
56*68d75effSDimitry Andric 
initializeFlags()57*68d75effSDimitry Andric void initializeFlags() XRAY_NEVER_INSTRUMENT {
58*68d75effSDimitry Andric   SetCommonFlagsDefaults();
59*68d75effSDimitry Andric   auto *F = flags();
60*68d75effSDimitry Andric   F->setDefaults();
61*68d75effSDimitry Andric 
62*68d75effSDimitry Andric   FlagParser XRayParser;
63*68d75effSDimitry Andric   registerXRayFlags(&XRayParser, F);
64*68d75effSDimitry Andric   RegisterCommonFlags(&XRayParser);
65*68d75effSDimitry Andric 
66*68d75effSDimitry Andric   // Use options defaulted at compile-time for the runtime.
67*68d75effSDimitry Andric   const char *XRayCompileFlags = useCompilerDefinedFlags();
68*68d75effSDimitry Andric   XRayParser.ParseString(XRayCompileFlags);
69*68d75effSDimitry Andric 
70*68d75effSDimitry Andric   // Override from environment variables.
71*68d75effSDimitry Andric   XRayParser.ParseStringFromEnv("XRAY_OPTIONS");
72*68d75effSDimitry Andric 
73*68d75effSDimitry Andric   // Override from command line.
74*68d75effSDimitry Andric   InitializeCommonFlags();
75*68d75effSDimitry Andric 
76*68d75effSDimitry Andric   if (Verbosity())
77*68d75effSDimitry Andric     ReportUnrecognizedFlags();
78*68d75effSDimitry Andric 
79*68d75effSDimitry Andric   if (common_flags()->help) {
80*68d75effSDimitry Andric     XRayParser.PrintFlagDescriptions();
81*68d75effSDimitry Andric   }
82*68d75effSDimitry Andric }
83*68d75effSDimitry Andric 
84*68d75effSDimitry Andric } // namespace __xray
85