Coverage Report

Created: 2020-02-15 09:57

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