Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Driver/XRayArgs.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
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
#include "clang/Driver/XRayArgs.h"
9
#include "ToolChains/CommonArgs.h"
10
#include "clang/Driver/Driver.h"
11
#include "clang/Driver/DriverDiagnostic.h"
12
#include "clang/Driver/Options.h"
13
#include "clang/Driver/ToolChain.h"
14
#include "llvm/ADT/StringExtras.h"
15
#include "llvm/ADT/StringSwitch.h"
16
#include "llvm/Support/Path.h"
17
#include "llvm/Support/ScopedPrinter.h"
18
#include "llvm/Support/SpecialCaseList.h"
19
#include "llvm/Support/VirtualFileSystem.h"
20
21
using namespace clang;
22
using namespace clang::driver;
23
using namespace llvm::opt;
24
25
constexpr const char *XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
26
27
0
XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
28
0
  const Driver &D = TC.getDriver();
29
0
  const llvm::Triple &Triple = TC.getTriple();
30
0
  if (!Args.hasFlag(options::OPT_fxray_instrument,
31
0
                    options::OPT_fno_xray_instrument, false))
32
0
    return;
33
0
  XRayInstrument = Args.getLastArg(options::OPT_fxray_instrument);
34
0
  if (Triple.isMacOSX()) {
35
0
    switch (Triple.getArch()) {
36
0
    case llvm::Triple::aarch64:
37
0
    case llvm::Triple::x86_64:
38
0
      break;
39
0
    default:
40
0
      D.Diag(diag::err_drv_unsupported_opt_for_target)
41
0
          << XRayInstrument->getSpelling() << Triple.str();
42
0
      break;
43
0
    }
44
0
  } else if (Triple.isOSBinFormatELF()) {
45
0
    switch (Triple.getArch()) {
46
0
    case llvm::Triple::x86_64:
47
0
    case llvm::Triple::arm:
48
0
    case llvm::Triple::aarch64:
49
0
    case llvm::Triple::hexagon:
50
0
    case llvm::Triple::ppc64le:
51
0
    case llvm::Triple::loongarch64:
52
0
    case llvm::Triple::mips:
53
0
    case llvm::Triple::mipsel:
54
0
    case llvm::Triple::mips64:
55
0
    case llvm::Triple::mips64el:
56
0
      break;
57
0
    default:
58
0
      D.Diag(diag::err_drv_unsupported_opt_for_target)
59
0
          << XRayInstrument->getSpelling() << Triple.str();
60
0
    }
61
0
  } else {
62
0
    D.Diag(diag::err_drv_unsupported_opt_for_target)
63
0
        << XRayInstrument->getSpelling() << Triple.str();
64
0
  }
65
66
  // Both XRay and -fpatchable-function-entry use
67
  // TargetOpcode::PATCHABLE_FUNCTION_ENTER.
68
0
  if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ))
69
0
    D.Diag(diag::err_drv_argument_not_allowed_with)
70
0
        << XRayInstrument->getSpelling() << A->getSpelling();
71
72
0
  if (!Args.hasFlag(options::OPT_fxray_link_deps,
73
0
                    options::OPT_fno_xray_link_deps, true))
74
0
    XRayRT = false;
75
76
0
  auto Bundles =
77
0
      Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
78
0
  if (Bundles.empty())
79
0
    InstrumentationBundle.Mask = XRayInstrKind::All;
80
0
  else
81
0
    for (const auto &B : Bundles) {
82
0
      llvm::SmallVector<StringRef, 2> BundleParts;
83
0
      llvm::SplitString(B, BundleParts, ",");
84
0
      for (const auto &P : BundleParts) {
85
        // TODO: Automate the generation of the string case table.
86
0
        auto Valid = llvm::StringSwitch<bool>(P)
87
0
                         .Cases("none", "all", "function", "function-entry",
88
0
                                "function-exit", "custom", true)
89
0
                         .Default(false);
90
91
0
        if (!Valid) {
92
0
          D.Diag(clang::diag::err_drv_invalid_value)
93
0
              << "-fxray-instrumentation-bundle=" << P;
94
0
          continue;
95
0
        }
96
97
0
        auto Mask = parseXRayInstrValue(P);
98
0
        if (Mask == XRayInstrKind::None) {
99
0
          InstrumentationBundle.clear();
100
0
          break;
101
0
        }
102
103
0
        InstrumentationBundle.Mask |= Mask;
104
0
      }
105
0
    }
106
107
  // Validate the always/never attribute files. We also make sure that they
108
  // are treated as actual dependencies.
109
0
  for (const auto &Filename :
110
0
       Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
111
0
    if (D.getVFS().exists(Filename)) {
112
0
      AlwaysInstrumentFiles.push_back(Filename);
113
0
      ExtraDeps.push_back(Filename);
114
0
    } else
115
0
      D.Diag(clang::diag::err_drv_no_such_file) << Filename;
116
0
  }
117
118
0
  for (const auto &Filename :
119
0
       Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
120
0
    if (D.getVFS().exists(Filename)) {
121
0
      NeverInstrumentFiles.push_back(Filename);
122
0
      ExtraDeps.push_back(Filename);
123
0
    } else
124
0
      D.Diag(clang::diag::err_drv_no_such_file) << Filename;
125
0
  }
126
127
0
  for (const auto &Filename :
128
0
       Args.getAllArgValues(options::OPT_fxray_attr_list)) {
129
0
    if (D.getVFS().exists(Filename)) {
130
0
      AttrListFiles.push_back(Filename);
131
0
      ExtraDeps.push_back(Filename);
132
0
    } else
133
0
      D.Diag(clang::diag::err_drv_no_such_file) << Filename;
134
0
  }
135
136
  // Get the list of modes we want to support.
137
0
  auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
138
0
  if (SpecifiedModes.empty())
139
0
    llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
140
0
  else
141
0
    for (const auto &Arg : SpecifiedModes) {
142
      // Parse CSV values for -fxray-modes=...
143
0
      llvm::SmallVector<StringRef, 2> ModeParts;
144
0
      llvm::SplitString(Arg, ModeParts, ",");
145
0
      for (const auto &M : ModeParts)
146
0
        if (M == "none")
147
0
          Modes.clear();
148
0
        else if (M == "all")
149
0
          llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
150
0
        else
151
0
          Modes.push_back(std::string(M));
152
0
    }
153
154
  // Then we want to sort and unique the modes we've collected.
155
0
  llvm::sort(Modes);
156
0
  Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
157
0
}
158
159
void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
160
0
                       ArgStringList &CmdArgs, types::ID InputType) const {
161
0
  if (!XRayInstrument)
162
0
    return;
163
0
  const Driver &D = TC.getDriver();
164
0
  XRayInstrument->render(Args, CmdArgs);
165
166
  // By default, the back-end will not emit the lowering for XRay customevent
167
  // calls if the function is not instrumented. In the future we will change
168
  // this default to be the reverse, but in the meantime we're going to
169
  // introduce the new functionality behind a flag.
170
0
  Args.addOptInFlag(CmdArgs, options::OPT_fxray_always_emit_customevents,
171
0
                    options::OPT_fno_xray_always_emit_customevents);
172
173
0
  Args.addOptInFlag(CmdArgs, options::OPT_fxray_always_emit_typedevents,
174
0
                    options::OPT_fno_xray_always_emit_typedevents);
175
0
  Args.addOptInFlag(CmdArgs, options::OPT_fxray_ignore_loops,
176
0
                    options::OPT_fno_xray_ignore_loops);
177
0
  Args.addOptOutFlag(CmdArgs, options::OPT_fxray_function_index,
178
0
                     options::OPT_fno_xray_function_index);
179
180
0
  if (const Arg *A =
181
0
          Args.getLastArg(options::OPT_fxray_instruction_threshold_EQ)) {
182
0
    int Value;
183
0
    StringRef S = A->getValue();
184
0
    if (S.getAsInteger(0, Value) || Value < 0)
185
0
      D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
186
0
    else
187
0
      A->render(Args, CmdArgs);
188
0
  }
189
190
0
  int XRayFunctionGroups = 1;
191
0
  int XRaySelectedFunctionGroup = 0;
192
0
  if (const Arg *A = Args.getLastArg(options::OPT_fxray_function_groups)) {
193
0
    StringRef S = A->getValue();
194
0
    if (S.getAsInteger(0, XRayFunctionGroups) || XRayFunctionGroups < 1)
195
0
      D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
196
0
    if (XRayFunctionGroups > 1)
197
0
      A->render(Args, CmdArgs);
198
0
  }
199
0
  if (const Arg *A =
200
0
          Args.getLastArg(options::OPT_fxray_selected_function_group)) {
201
0
    StringRef S = A->getValue();
202
0
    if (S.getAsInteger(0, XRaySelectedFunctionGroup) ||
203
0
        XRaySelectedFunctionGroup < 0 ||
204
0
        XRaySelectedFunctionGroup >= XRayFunctionGroups)
205
0
      D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
206
0
    if (XRaySelectedFunctionGroup != 0)
207
0
      A->render(Args, CmdArgs);
208
0
  }
209
210
0
  for (const auto &Always : AlwaysInstrumentFiles) {
211
0
    SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
212
0
    AlwaysInstrumentOpt += Always;
213
0
    CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
214
0
  }
215
216
0
  for (const auto &Never : NeverInstrumentFiles) {
217
0
    SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
218
0
    NeverInstrumentOpt += Never;
219
0
    CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
220
0
  }
221
222
0
  for (const auto &AttrFile : AttrListFiles) {
223
0
    SmallString<64> AttrListFileOpt("-fxray-attr-list=");
224
0
    AttrListFileOpt += AttrFile;
225
0
    CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
226
0
  }
227
228
0
  for (const auto &Dep : ExtraDeps) {
229
0
    SmallString<64> ExtraDepOpt("-fdepfile-entry=");
230
0
    ExtraDepOpt += Dep;
231
0
    CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
232
0
  }
233
234
0
  for (const auto &Mode : Modes) {
235
0
    SmallString<64> ModeOpt("-fxray-modes=");
236
0
    ModeOpt += Mode;
237
0
    CmdArgs.push_back(Args.MakeArgString(ModeOpt));
238
0
  }
239
240
0
  SmallString<64> Bundle("-fxray-instrumentation-bundle=");
241
0
  if (InstrumentationBundle.full()) {
242
0
    Bundle += "all";
243
0
  } else if (InstrumentationBundle.empty()) {
244
0
    Bundle += "none";
245
0
  } else {
246
0
    if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry) &&
247
0
        InstrumentationBundle.has(XRayInstrKind::FunctionExit))
248
0
      Bundle += "function";
249
0
    else if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry))
250
0
      Bundle += "function-entry";
251
0
    else if (InstrumentationBundle.has(XRayInstrKind::FunctionExit))
252
0
      Bundle += "function-exit";
253
254
0
    if (InstrumentationBundle.has(XRayInstrKind::Custom))
255
0
      Bundle += "custom";
256
0
    if (InstrumentationBundle.has(XRayInstrKind::Typed))
257
0
      Bundle += "typed";
258
0
  }
259
0
  CmdArgs.push_back(Args.MakeArgString(Bundle));
260
0
}