blob: 1d5850b55983b76f4a39d9b1ab7c66f72d6ef3d3 [file] [log] [blame]
Dean Michael Berris835832d2017-03-30 00:29:36 +00001//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "clang/Driver/XRayArgs.h"
10#include "ToolChains/CommonArgs.h"
11#include "clang/Driver/Driver.h"
12#include "clang/Driver/DriverDiagnostic.h"
13#include "clang/Driver/Options.h"
14#include "clang/Driver/ToolChain.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/ADT/StringSwitch.h"
17#include "llvm/Support/FileSystem.h"
18#include "llvm/Support/Path.h"
Dean Michael Berris00ad3d52017-03-30 01:05:09 +000019#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berris504fc222017-03-30 22:46:45 +000020#include "llvm/Support/SpecialCaseList.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000021
22using namespace clang;
23using namespace clang::driver;
24using namespace llvm::opt;
25
26namespace {
27constexpr char XRayInstrumentOption[] = "-fxray-instrument";
28constexpr char XRayInstructionThresholdOption[] =
29 "-fxray-instruction-threshold=";
Dean Michael Berris826e6662018-04-11 01:28:25 +000030constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
Dean Michael Berris835832d2017-03-30 00:29:36 +000031} // namespace
32
33XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
34 const Driver &D = TC.getDriver();
35 const llvm::Triple &Triple = TC.getTriple();
36 if (Args.hasFlag(options::OPT_fxray_instrument,
37 options::OPT_fnoxray_instrument, false)) {
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000038 if (Triple.getOS() == llvm::Triple::Linux) {
Dean Michael Berris835832d2017-03-30 00:29:36 +000039 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 }
Dean Michael Berris9b592332018-04-04 12:47:49 +000053 } else if (Triple.getOS() == llvm::Triple::FreeBSD ||
54 Triple.getOS() == llvm::Triple::OpenBSD) {
Dean Michael Berris826e6662018-04-11 01:28:25 +000055 if (Triple.getArch() != llvm::Triple::x86_64) {
56 D.Diag(diag::err_drv_clang_unsupported)
57 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
58 }
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000059 } else {
Dean Michael Berris835832d2017-03-30 00:29:36 +000060 D.Diag(diag::err_drv_clang_unsupported)
Dean Michael Berris488f7c22018-04-13 02:31:58 +000061 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
Kamil Rytarowski3e4e74c2018-02-22 06:31:40 +000062 }
Dean Michael Berris835832d2017-03-30 00:29:36 +000063 XRayInstrument = true;
64 if (const Arg *A =
65 Args.getLastArg(options::OPT_fxray_instruction_threshold_,
66 options::OPT_fxray_instruction_threshold_EQ)) {
67 StringRef S = A->getValue();
68 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
69 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
70 }
71
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +000072 // By default, the back-end will not emit the lowering for XRay customevent
73 // calls if the function is not instrumented. In the future we will change
74 // this default to be the reverse, but in the meantime we're going to
75 // introduce the new functionality behind a flag.
76 if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
77 options::OPT_fnoxray_always_emit_customevents, false))
78 XRayAlwaysEmitCustomEvents = true;
79
Keith Wyssf437e352018-04-17 21:32:43 +000080 if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
81 options::OPT_fnoxray_always_emit_typedevents, false))
82 XRayAlwaysEmitTypedEvents = true;
83
Dean Michael Berris248148d2018-04-06 05:28:54 +000084 if (!Args.hasFlag(options::OPT_fxray_link_deps,
85 options::OPT_fnoxray_link_deps, true))
86 XRayRT = false;
87
Dean Michael Berris488f7c22018-04-13 02:31:58 +000088 auto Bundles =
89 Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
90 if (Bundles.empty())
91 InstrumentationBundle.Mask = XRayInstrKind::All;
92 else
93 for (const auto &B : Bundles) {
94 llvm::SmallVector<StringRef, 2> BundleParts;
95 llvm::SplitString(B, BundleParts, ",");
96 for (const auto &P : BundleParts) {
97 // TODO: Automate the generation of the string case table.
98 auto Valid = llvm::StringSwitch<bool>(P)
99 .Cases("none", "all", "function", "custom", true)
100 .Default(false);
101
102 if (!Valid) {
103 D.Diag(clang::diag::err_drv_invalid_value)
104 << "-fxray-instrumentation-bundle=" << P;
105 continue;
106 }
107
108 auto Mask = parseXRayInstrValue(P);
109 if (Mask == XRayInstrKind::None) {
110 InstrumentationBundle.clear();
111 break;
112 }
113
114 InstrumentationBundle.Mask |= Mask;
115 }
116 }
117
Dean Michael Berris835832d2017-03-30 00:29:36 +0000118 // Validate the always/never attribute files. We also make sure that they
119 // are treated as actual dependencies.
120 for (const auto &Filename :
121 Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
122 if (llvm::sys::fs::exists(Filename)) {
123 AlwaysInstrumentFiles.push_back(Filename);
124 ExtraDeps.push_back(Filename);
125 } else
126 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
127 }
128
129 for (const auto &Filename :
130 Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
131 if (llvm::sys::fs::exists(Filename)) {
132 NeverInstrumentFiles.push_back(Filename);
133 ExtraDeps.push_back(Filename);
134 } else
135 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
136 }
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +0000137
138 for (const auto &Filename :
139 Args.getAllArgValues(options::OPT_fxray_attr_list)) {
140 if (llvm::sys::fs::exists(Filename)) {
141 AttrListFiles.push_back(Filename);
142 ExtraDeps.push_back(Filename);
143 } else
144 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
145 }
Dean Michael Berris826e6662018-04-11 01:28:25 +0000146
147 // Get the list of modes we want to support.
148 auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
149 if (SpecifiedModes.empty())
150 llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
151 else
152 for (const auto &Arg : SpecifiedModes) {
Dean Michael Berris826e6662018-04-11 01:28:25 +0000153 // Parse CSV values for -fxray-modes=...
154 llvm::SmallVector<StringRef, 2> ModeParts;
155 llvm::SplitString(Arg, ModeParts, ",");
156 for (const auto &M : ModeParts)
Dean Michael Berris7fc737a2018-04-13 05:59:57 +0000157 if (M == "none")
158 Modes.clear();
159 else if (M == "all")
160 llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
161 else
162 Modes.push_back(M);
Dean Michael Berris826e6662018-04-11 01:28:25 +0000163 }
164
165 // Then we want to sort and unique the modes we've collected.
Mandeep Singh Grangfce35652018-04-23 00:49:25 +0000166 llvm::sort(Modes.begin(), Modes.end());
Dean Michael Berris826e6662018-04-11 01:28:25 +0000167 Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
Dean Michael Berris835832d2017-03-30 00:29:36 +0000168 }
169}
170
171void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
172 ArgStringList &CmdArgs, types::ID InputType) const {
173 if (!XRayInstrument)
174 return;
175
176 CmdArgs.push_back(XRayInstrumentOption);
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000177
178 if (XRayAlwaysEmitCustomEvents)
179 CmdArgs.push_back("-fxray-always-emit-customevents");
180
Keith Wyssf437e352018-04-17 21:32:43 +0000181 if (XRayAlwaysEmitTypedEvents)
182 CmdArgs.push_back("-fxray-always-emit-typedevents");
183
Dean Michael Berris504fc222017-03-30 22:46:45 +0000184 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
185 Twine(InstructionThreshold)));
Dean Michael Berris835832d2017-03-30 00:29:36 +0000186
187 for (const auto &Always : AlwaysInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000188 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000189 AlwaysInstrumentOpt += Always;
190 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
191 }
192
193 for (const auto &Never : NeverInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000194 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000195 NeverInstrumentOpt += Never;
196 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
197 }
198
Dean Michael Berris488f7c22018-04-13 02:31:58 +0000199 for (const auto &AttrFile : AttrListFiles) {
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +0000200 SmallString<64> AttrListFileOpt("-fxray-attr-list=");
201 AttrListFileOpt += AttrFile;
202 CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
203 }
204
Dean Michael Berris835832d2017-03-30 00:29:36 +0000205 for (const auto &Dep : ExtraDeps) {
206 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
207 ExtraDepOpt += Dep;
208 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
209 }
Dean Michael Berris7fc737a2018-04-13 05:59:57 +0000210
211 for (const auto &Mode : Modes) {
212 SmallString<64> ModeOpt("-fxray-modes=");
213 ModeOpt += Mode;
214 CmdArgs.push_back(Args.MakeArgString(ModeOpt));
215 }
Dean Michael Berris835832d2017-03-30 00:29:36 +0000216}