clang  14.0.0git
XRayArgs.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Driver.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 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 XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
33  const Driver &D = TC.getDriver();
34  const llvm::Triple &Triple = TC.getTriple();
35  if (!Args.hasFlag(options::OPT_fxray_instrument,
36  options::OPT_fno_xray_instrument, false))
37  return;
38  if (Triple.getOS() == llvm::Triple::Linux) {
39  switch (Triple.getArch()) {
40  case llvm::Triple::x86_64:
41  case llvm::Triple::arm:
42  case llvm::Triple::aarch64:
43  case llvm::Triple::ppc64le:
44  case llvm::Triple::mips:
45  case llvm::Triple::mipsel:
46  case llvm::Triple::mips64:
47  case llvm::Triple::mips64el:
48  break;
49  default:
50  D.Diag(diag::err_drv_clang_unsupported)
51  << (std::string(XRayInstrumentOption) + " on " + Triple.str());
52  }
53  } else if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() ||
54  Triple.isOSNetBSD() || Triple.isMacOSX()) {
55  if (Triple.getArch() != llvm::Triple::x86_64) {
56  D.Diag(diag::err_drv_clang_unsupported)
57  << (std::string(XRayInstrumentOption) + " on " + Triple.str());
58  }
59  } else if (Triple.getOS() == llvm::Triple::Fuchsia) {
60  switch (Triple.getArch()) {
61  case llvm::Triple::x86_64:
62  case llvm::Triple::aarch64:
63  break;
64  default:
65  D.Diag(diag::err_drv_clang_unsupported)
66  << (std::string(XRayInstrumentOption) + " on " + Triple.str());
67  }
68  } else {
69  D.Diag(diag::err_drv_clang_unsupported)
70  << (std::string(XRayInstrumentOption) + " on " + Triple.str());
71  }
72 
73  // Both XRay and -fpatchable-function-entry use
74  // TargetOpcode::PATCHABLE_FUNCTION_ENTER.
75  if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ))
76  D.Diag(diag::err_drv_argument_not_allowed_with)
77  << "-fxray-instrument" << A->getSpelling();
78 
79  XRayInstrument = true;
80  if (const Arg *A =
81  Args.getLastArg(options::OPT_fxray_instruction_threshold_,
82  options::OPT_fxray_instruction_threshold_EQ)) {
83  StringRef S = A->getValue();
84  if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
85  D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
86  }
87 
88  // By default, the back-end will not emit the lowering for XRay customevent
89  // calls if the function is not instrumented. In the future we will change
90  // this default to be the reverse, but in the meantime we're going to
91  // introduce the new functionality behind a flag.
92  if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
93  options::OPT_fno_xray_always_emit_customevents, false))
94  XRayAlwaysEmitCustomEvents = true;
95 
96  if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
97  options::OPT_fno_xray_always_emit_typedevents, false))
98  XRayAlwaysEmitTypedEvents = true;
99 
100  if (!Args.hasFlag(options::OPT_fxray_link_deps,
101  options::OPT_fnoxray_link_deps, true))
102  XRayRT = false;
103 
104  if (Args.hasFlag(options::OPT_fxray_ignore_loops,
105  options::OPT_fno_xray_ignore_loops, false))
106  XRayIgnoreLoops = true;
107 
108  XRayFunctionIndex = Args.hasFlag(options::OPT_fxray_function_index,
109  options::OPT_fno_xray_function_index, true);
110 
111  auto Bundles =
112  Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
113  if (Bundles.empty())
114  InstrumentationBundle.Mask = XRayInstrKind::All;
115  else
116  for (const auto &B : Bundles) {
118  llvm::SplitString(B, BundleParts, ",");
119  for (const auto &P : BundleParts) {
120  // TODO: Automate the generation of the string case table.
121  auto Valid = llvm::StringSwitch<bool>(P)
122  .Cases("none", "all", "function", "function-entry",
123  "function-exit", "custom", true)
124  .Default(false);
125 
126  if (!Valid) {
127  D.Diag(clang::diag::err_drv_invalid_value)
128  << "-fxray-instrumentation-bundle=" << P;
129  continue;
130  }
131 
132  auto Mask = parseXRayInstrValue(P);
133  if (Mask == XRayInstrKind::None) {
134  InstrumentationBundle.clear();
135  break;
136  }
137 
138  InstrumentationBundle.Mask |= Mask;
139  }
140  }
141 
142  // Validate the always/never attribute files. We also make sure that they
143  // are treated as actual dependencies.
144  for (const auto &Filename :
145  Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
146  if (D.getVFS().exists(Filename)) {
147  AlwaysInstrumentFiles.push_back(Filename);
148  ExtraDeps.push_back(Filename);
149  } else
150  D.Diag(clang::diag::err_drv_no_such_file) << Filename;
151  }
152 
153  for (const auto &Filename :
154  Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
155  if (D.getVFS().exists(Filename)) {
156  NeverInstrumentFiles.push_back(Filename);
157  ExtraDeps.push_back(Filename);
158  } else
159  D.Diag(clang::diag::err_drv_no_such_file) << Filename;
160  }
161 
162  for (const auto &Filename :
163  Args.getAllArgValues(options::OPT_fxray_attr_list)) {
164  if (D.getVFS().exists(Filename)) {
165  AttrListFiles.push_back(Filename);
166  ExtraDeps.push_back(Filename);
167  } else
168  D.Diag(clang::diag::err_drv_no_such_file) << Filename;
169  }
170 
171  // Get the list of modes we want to support.
172  auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
173  if (SpecifiedModes.empty())
174  llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
175  else
176  for (const auto &Arg : SpecifiedModes) {
177  // Parse CSV values for -fxray-modes=...
179  llvm::SplitString(Arg, ModeParts, ",");
180  for (const auto &M : ModeParts)
181  if (M == "none")
182  Modes.clear();
183  else if (M == "all")
184  llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
185  else
186  Modes.push_back(std::string(M));
187  }
188 
189  if (const Arg *A = Args.getLastArg(options::OPT_fxray_function_groups)) {
190  StringRef S = A->getValue();
191  if (S.getAsInteger(0, XRayFunctionGroups) || XRayFunctionGroups < 1)
192  D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
193  }
194 
195  if (const Arg *A =
196  Args.getLastArg(options::OPT_fxray_selected_function_group)) {
197  StringRef S = A->getValue();
198  if (S.getAsInteger(0, XRaySelectedFunctionGroup) ||
199  XRaySelectedFunctionGroup < 0 ||
200  XRaySelectedFunctionGroup >= XRayFunctionGroups)
201  D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
202  }
203 
204  // Then we want to sort and unique the modes we've collected.
205  llvm::sort(Modes);
206  Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
207 }
208 
209 void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
210  ArgStringList &CmdArgs, types::ID InputType) const {
211  if (!XRayInstrument)
212  return;
213 
214  CmdArgs.push_back(XRayInstrumentOption);
215 
216  if (XRayAlwaysEmitCustomEvents)
217  CmdArgs.push_back("-fxray-always-emit-customevents");
218 
219  if (XRayAlwaysEmitTypedEvents)
220  CmdArgs.push_back("-fxray-always-emit-typedevents");
221 
222  if (XRayIgnoreLoops)
223  CmdArgs.push_back("-fxray-ignore-loops");
224 
225  if (!XRayFunctionIndex)
226  CmdArgs.push_back("-fno-xray-function-index");
227 
228  if (XRayFunctionGroups > 1) {
229  CmdArgs.push_back(Args.MakeArgString(Twine("-fxray-function-groups=") +
230  Twine(XRayFunctionGroups)));
231  }
232 
233  if (XRaySelectedFunctionGroup != 0) {
234  CmdArgs.push_back(
235  Args.MakeArgString(Twine("-fxray-selected-function-group=") +
236  Twine(XRaySelectedFunctionGroup)));
237  }
238 
239  CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
240  Twine(InstructionThreshold)));
241 
242  for (const auto &Always : AlwaysInstrumentFiles) {
243  SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
244  AlwaysInstrumentOpt += Always;
245  CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
246  }
247 
248  for (const auto &Never : NeverInstrumentFiles) {
249  SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
250  NeverInstrumentOpt += Never;
251  CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
252  }
253 
254  for (const auto &AttrFile : AttrListFiles) {
255  SmallString<64> AttrListFileOpt("-fxray-attr-list=");
256  AttrListFileOpt += AttrFile;
257  CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
258  }
259 
260  for (const auto &Dep : ExtraDeps) {
261  SmallString<64> ExtraDepOpt("-fdepfile-entry=");
262  ExtraDepOpt += Dep;
263  CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
264  }
265 
266  for (const auto &Mode : Modes) {
267  SmallString<64> ModeOpt("-fxray-modes=");
268  ModeOpt += Mode;
269  CmdArgs.push_back(Args.MakeArgString(ModeOpt));
270  }
271 
272  SmallString<64> Bundle("-fxray-instrumentation-bundle=");
273  if (InstrumentationBundle.full()) {
274  Bundle += "all";
275  } else if (InstrumentationBundle.empty()) {
276  Bundle += "none";
277  } else {
278  if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry) &&
279  InstrumentationBundle.has(XRayInstrKind::FunctionExit))
280  Bundle += "function";
281  else if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry))
282  Bundle += "function-entry";
283  else if (InstrumentationBundle.has(XRayInstrKind::FunctionExit))
284  Bundle += "function-exit";
285 
286  if (InstrumentationBundle.has(XRayInstrKind::Custom))
287  Bundle += "custom";
288  if (InstrumentationBundle.has(XRayInstrKind::Typed))
289  Bundle += "typed";
290  }
291  CmdArgs.push_back(Args.MakeArgString(Bundle));
292 }
XRayArgs.h
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::XRayInstrKind::FunctionEntry
constexpr XRayInstrMask FunctionEntry
Definition: XRayInstr.h:39
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::ToolChain::getDriver
const Driver & getDriver() const
Definition: ToolChain.h:221
Filename
StringRef Filename
Definition: Format.cpp:2379
clang::XRayInstrKind::All
constexpr XRayInstrMask All
Definition: XRayInstr.h:43
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:112
Options.h
clang::XRayInstrKind::None
constexpr XRayInstrMask None
Definition: XRayInstr.h:38
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
llvm::SmallString
Definition: LLVM.h:37
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:223
clang::for
for(auto typeArg :T->getTypeArgsAsWritten())
Definition: RecursiveASTVisitor.h:1063
clang::parseXRayInstrValue
XRayInstrMask parseXRayInstrValue(StringRef Value)
Parses a command line argument into a mask.
Definition: XRayInstr.cpp:19
clang::XRayInstrKind::FunctionExit
constexpr XRayInstrMask FunctionExit
Definition: XRayInstr.h:40
clang::MSVtorDispMode::Never
@ Never
ToolChain.h
P
StringRef P
Definition: ASTMatchersInternal.cpp:563
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
clang::driver::types::ID
ID
Definition: Types.h:23
clang::driver::XRayArgs::XRayArgs
XRayArgs(const ToolChain &TC, const llvm::opt::ArgList &Args)
Parses the XRay arguments from an argument list.
Definition: XRayArgs.cpp:32
CommonArgs.h
clang
Definition: CalledOnceCheck.h:17
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
clang::driver::Driver::getVFS
llvm::vfs::FileSystem & getVFS() const
Definition: Driver.h:329
clang::XRayInstrKind::Custom
constexpr XRayInstrMask Custom
Definition: XRayInstr.h:41
clang::driver
Definition: Action.h:31
clang::driver::XRayArgs::addArgs
void addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const
Definition: XRayArgs.cpp:209
clang::XRayInstrKind::Typed
constexpr XRayInstrMask Typed
Definition: XRayInstr.h:42