blob: 16e7c7ecf36b479d0c22992824f431fee7bb0037 [file] [log] [blame]
Dean Michael Berris835832d2017-03-30 00:29:36 +00001//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berris835832d2017-03-30 00:29:36 +00006//
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"
Dean Michael Berris00ad3d52017-03-30 01:05:09 +000018#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berris504fc222017-03-30 22:46:45 +000019#include "llvm/Support/SpecialCaseList.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000020
21using namespace clang;
22using namespace clang::driver;
23using namespace llvm::opt;
24
25namespace {
26constexpr char XRayInstrumentOption[] = "-fxray-instrument";
27constexpr char XRayInstructionThresholdOption[] =
28 "-fxray-instruction-threshold=";
Dean Michael Berris826e6662018-04-11 01:28:25 +000029constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
Dean Michael Berris835832d2017-03-30 00:29:36 +000030} // namespace
31
32XRayArgs::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_fnoxray_instrument, false)) {
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000037 if (Triple.getOS() == llvm::Triple::Linux) {
Dean Michael Berris835832d2017-03-30 00:29:36 +000038 switch (Triple.getArch()) {
39 case llvm::Triple::x86_64:
40 case llvm::Triple::arm:
41 case llvm::Triple::aarch64:
42 case llvm::Triple::ppc64le:
43 case llvm::Triple::mips:
44 case llvm::Triple::mipsel:
45 case llvm::Triple::mips64:
46 case llvm::Triple::mips64el:
47 break;
48 default:
49 D.Diag(diag::err_drv_clang_unsupported)
50 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
51 }
Michal Gorny5a409d02018-12-20 13:09:30 +000052 } else if (Triple.isOSFreeBSD() ||
53 Triple.isOSOpenBSD() ||
54 Triple.isOSNetBSD() ||
Alex Lorenz3737c022019-08-27 18:26:36 +000055 Triple.isMacOSX()) {
Dean Michael Berris826e6662018-04-11 01:28:25 +000056 if (Triple.getArch() != llvm::Triple::x86_64) {
57 D.Diag(diag::err_drv_clang_unsupported)
58 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
59 }
Petr Hosekda914312018-11-22 02:36:47 +000060 } else if (Triple.getOS() == llvm::Triple::Fuchsia) {
61 switch (Triple.getArch()) {
62 case llvm::Triple::x86_64:
63 case llvm::Triple::aarch64:
64 break;
65 default:
66 D.Diag(diag::err_drv_clang_unsupported)
67 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
68 }
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000069 } else {
Dean Michael Berris835832d2017-03-30 00:29:36 +000070 D.Diag(diag::err_drv_clang_unsupported)
Dean Michael Berris488f7c22018-04-13 02:31:58 +000071 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000072 }
Dean Michael Berris835832d2017-03-30 00:29:36 +000073 XRayInstrument = true;
74 if (const Arg *A =
75 Args.getLastArg(options::OPT_fxray_instruction_threshold_,
76 options::OPT_fxray_instruction_threshold_EQ)) {
77 StringRef S = A->getValue();
78 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
79 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
80 }
81
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +000082 // By default, the back-end will not emit the lowering for XRay customevent
83 // calls if the function is not instrumented. In the future we will change
84 // this default to be the reverse, but in the meantime we're going to
85 // introduce the new functionality behind a flag.
86 if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
87 options::OPT_fnoxray_always_emit_customevents, false))
88 XRayAlwaysEmitCustomEvents = true;
89
Keith Wyssf437e352018-04-17 21:32:43 +000090 if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
91 options::OPT_fnoxray_always_emit_typedevents, false))
92 XRayAlwaysEmitTypedEvents = true;
93
Dean Michael Berris248148d2018-04-06 05:28:54 +000094 if (!Args.hasFlag(options::OPT_fxray_link_deps,
95 options::OPT_fnoxray_link_deps, true))
96 XRayRT = false;
97
Dean Michael Berris488f7c22018-04-13 02:31:58 +000098 auto Bundles =
99 Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
100 if (Bundles.empty())
101 InstrumentationBundle.Mask = XRayInstrKind::All;
102 else
103 for (const auto &B : Bundles) {
104 llvm::SmallVector<StringRef, 2> BundleParts;
105 llvm::SplitString(B, BundleParts, ",");
106 for (const auto &P : BundleParts) {
107 // TODO: Automate the generation of the string case table.
108 auto Valid = llvm::StringSwitch<bool>(P)
109 .Cases("none", "all", "function", "custom", true)
110 .Default(false);
111
112 if (!Valid) {
113 D.Diag(clang::diag::err_drv_invalid_value)
114 << "-fxray-instrumentation-bundle=" << P;
115 continue;
116 }
117
118 auto Mask = parseXRayInstrValue(P);
119 if (Mask == XRayInstrKind::None) {
120 InstrumentationBundle.clear();
121 break;
122 }
123
124 InstrumentationBundle.Mask |= Mask;
125 }
126 }
127
Dean Michael Berris835832d2017-03-30 00:29:36 +0000128 // Validate the always/never attribute files. We also make sure that they
129 // are treated as actual dependencies.
130 for (const auto &Filename :
131 Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
132 if (llvm::sys::fs::exists(Filename)) {
133 AlwaysInstrumentFiles.push_back(Filename);
134 ExtraDeps.push_back(Filename);
135 } else
136 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
137 }
138
139 for (const auto &Filename :
140 Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
141 if (llvm::sys::fs::exists(Filename)) {
142 NeverInstrumentFiles.push_back(Filename);
143 ExtraDeps.push_back(Filename);
144 } else
145 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
146 }
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +0000147
148 for (const auto &Filename :
149 Args.getAllArgValues(options::OPT_fxray_attr_list)) {
150 if (llvm::sys::fs::exists(Filename)) {
151 AttrListFiles.push_back(Filename);
152 ExtraDeps.push_back(Filename);
153 } else
154 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
155 }
Dean Michael Berris826e6662018-04-11 01:28:25 +0000156
157 // Get the list of modes we want to support.
158 auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
159 if (SpecifiedModes.empty())
160 llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
161 else
162 for (const auto &Arg : SpecifiedModes) {
Dean Michael Berris826e6662018-04-11 01:28:25 +0000163 // Parse CSV values for -fxray-modes=...
164 llvm::SmallVector<StringRef, 2> ModeParts;
165 llvm::SplitString(Arg, ModeParts, ",");
166 for (const auto &M : ModeParts)
Dean Michael Berris7fc737a2018-04-13 05:59:57 +0000167 if (M == "none")
168 Modes.clear();
169 else if (M == "all")
170 llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
171 else
172 Modes.push_back(M);
Dean Michael Berris826e6662018-04-11 01:28:25 +0000173 }
174
175 // Then we want to sort and unique the modes we've collected.
Fangrui Song55fab262018-09-26 22:16:28 +0000176 llvm::sort(Modes);
Dean Michael Berris826e6662018-04-11 01:28:25 +0000177 Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
Dean Michael Berris835832d2017-03-30 00:29:36 +0000178 }
179}
180
181void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
182 ArgStringList &CmdArgs, types::ID InputType) const {
183 if (!XRayInstrument)
184 return;
185
186 CmdArgs.push_back(XRayInstrumentOption);
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000187
188 if (XRayAlwaysEmitCustomEvents)
189 CmdArgs.push_back("-fxray-always-emit-customevents");
190
Keith Wyssf437e352018-04-17 21:32:43 +0000191 if (XRayAlwaysEmitTypedEvents)
192 CmdArgs.push_back("-fxray-always-emit-typedevents");
193
Dean Michael Berris504fc222017-03-30 22:46:45 +0000194 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
195 Twine(InstructionThreshold)));
Dean Michael Berris835832d2017-03-30 00:29:36 +0000196
197 for (const auto &Always : AlwaysInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000198 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000199 AlwaysInstrumentOpt += Always;
200 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
201 }
202
203 for (const auto &Never : NeverInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000204 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000205 NeverInstrumentOpt += Never;
206 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
207 }
208
Dean Michael Berris488f7c22018-04-13 02:31:58 +0000209 for (const auto &AttrFile : AttrListFiles) {
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +0000210 SmallString<64> AttrListFileOpt("-fxray-attr-list=");
211 AttrListFileOpt += AttrFile;
212 CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
213 }
214
Dean Michael Berris835832d2017-03-30 00:29:36 +0000215 for (const auto &Dep : ExtraDeps) {
216 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
217 ExtraDepOpt += Dep;
218 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
219 }
Dean Michael Berris7fc737a2018-04-13 05:59:57 +0000220
221 for (const auto &Mode : Modes) {
222 SmallString<64> ModeOpt("-fxray-modes=");
223 ModeOpt += Mode;
224 CmdArgs.push_back(Args.MakeArgString(ModeOpt));
225 }
Dean Michael Berris5082e252018-09-21 08:32:49 +0000226
227 SmallString<64> Bundle("-fxray-instrumentation-bundle=");
228 if (InstrumentationBundle.full()) {
229 Bundle += "all";
230 } else if (InstrumentationBundle.empty()) {
231 Bundle += "none";
232 } else {
233 if (InstrumentationBundle.has(XRayInstrKind::Function))
234 Bundle += "function";
235 if (InstrumentationBundle.has(XRayInstrKind::Custom))
236 Bundle += "custom";
237 if (InstrumentationBundle.has(XRayInstrKind::Typed))
238 Bundle += "typed";
239 }
240 CmdArgs.push_back(Args.MakeArgString(Bundle));
Dean Michael Berris835832d2017-03-30 00:29:36 +0000241}