blob: e25259ca6d64c4220cddb61bd8d3321ec23d744b [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
Dean Michael Berris248148d2018-04-06 05:28:54 +000080 if (!Args.hasFlag(options::OPT_fxray_link_deps,
81 options::OPT_fnoxray_link_deps, true))
82 XRayRT = false;
83
Dean Michael Berris488f7c22018-04-13 02:31:58 +000084 auto Bundles =
85 Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
86 if (Bundles.empty())
87 InstrumentationBundle.Mask = XRayInstrKind::All;
88 else
89 for (const auto &B : Bundles) {
90 llvm::SmallVector<StringRef, 2> BundleParts;
91 llvm::SplitString(B, BundleParts, ",");
92 for (const auto &P : BundleParts) {
93 // TODO: Automate the generation of the string case table.
94 auto Valid = llvm::StringSwitch<bool>(P)
95 .Cases("none", "all", "function", "custom", true)
96 .Default(false);
97
98 if (!Valid) {
99 D.Diag(clang::diag::err_drv_invalid_value)
100 << "-fxray-instrumentation-bundle=" << P;
101 continue;
102 }
103
104 auto Mask = parseXRayInstrValue(P);
105 if (Mask == XRayInstrKind::None) {
106 InstrumentationBundle.clear();
107 break;
108 }
109
110 InstrumentationBundle.Mask |= Mask;
111 }
112 }
113
Dean Michael Berris835832d2017-03-30 00:29:36 +0000114 // Validate the always/never attribute files. We also make sure that they
115 // are treated as actual dependencies.
116 for (const auto &Filename :
117 Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
118 if (llvm::sys::fs::exists(Filename)) {
119 AlwaysInstrumentFiles.push_back(Filename);
120 ExtraDeps.push_back(Filename);
121 } else
122 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
123 }
124
125 for (const auto &Filename :
126 Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
127 if (llvm::sys::fs::exists(Filename)) {
128 NeverInstrumentFiles.push_back(Filename);
129 ExtraDeps.push_back(Filename);
130 } else
131 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
132 }
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +0000133
134 for (const auto &Filename :
135 Args.getAllArgValues(options::OPT_fxray_attr_list)) {
136 if (llvm::sys::fs::exists(Filename)) {
137 AttrListFiles.push_back(Filename);
138 ExtraDeps.push_back(Filename);
139 } else
140 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
141 }
Dean Michael Berris826e6662018-04-11 01:28:25 +0000142
143 // Get the list of modes we want to support.
144 auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
145 if (SpecifiedModes.empty())
146 llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
147 else
148 for (const auto &Arg : SpecifiedModes) {
149 if (Arg == "none") {
150 Modes.clear();
151 break;
152 }
153 if (Arg == "all") {
154 Modes.clear();
155 llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
156 break;
157 }
158
159 // Parse CSV values for -fxray-modes=...
160 llvm::SmallVector<StringRef, 2> ModeParts;
161 llvm::SplitString(Arg, ModeParts, ",");
162 for (const auto &M : ModeParts)
163 Modes.push_back(M);
164 }
165
166 // Then we want to sort and unique the modes we've collected.
167 std::sort(Modes.begin(), Modes.end());
168 Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
Dean Michael Berris835832d2017-03-30 00:29:36 +0000169 }
170}
171
172void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
173 ArgStringList &CmdArgs, types::ID InputType) const {
174 if (!XRayInstrument)
175 return;
176
177 CmdArgs.push_back(XRayInstrumentOption);
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000178
179 if (XRayAlwaysEmitCustomEvents)
180 CmdArgs.push_back("-fxray-always-emit-customevents");
181
Dean Michael Berris504fc222017-03-30 22:46:45 +0000182 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
183 Twine(InstructionThreshold)));
Dean Michael Berris835832d2017-03-30 00:29:36 +0000184
185 for (const auto &Always : AlwaysInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000186 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000187 AlwaysInstrumentOpt += Always;
188 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
189 }
190
191 for (const auto &Never : NeverInstrumentFiles) {
Dean Michael Berris1a5b10d2017-11-30 00:04:54 +0000192 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
Dean Michael Berris835832d2017-03-30 00:29:36 +0000193 NeverInstrumentOpt += Never;
194 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
195 }
196
Dean Michael Berris488f7c22018-04-13 02:31:58 +0000197 for (const auto &AttrFile : AttrListFiles) {
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +0000198 SmallString<64> AttrListFileOpt("-fxray-attr-list=");
199 AttrListFileOpt += AttrFile;
200 CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
201 }
202
Dean Michael Berris835832d2017-03-30 00:29:36 +0000203 for (const auto &Dep : ExtraDeps) {
204 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
205 ExtraDepOpt += Dep;
206 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
207 }
208}