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