1 //===--- 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"
19 #include "llvm/Support/ScopedPrinter.h"
20 #include "llvm/Support/SpecialCaseList.h"
21 
22 using namespace clang;
23 using namespace clang::driver;
24 using namespace llvm::opt;
25 
26 namespace {
27 constexpr char XRayInstrumentOption[] = "-fxray-instrument";
28 constexpr char XRayInstructionThresholdOption[] =
29     "-fxray-instruction-threshold=";
30 constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
31 } // namespace
32 
33 XRayArgs::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)) {
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.getOS() == llvm::Triple::FreeBSD ||
54                Triple.getOS() == llvm::Triple::OpenBSD ||
55                Triple.getOS() == llvm::Triple::NetBSD ||
56                Triple.getOS() == llvm::Triple::Darwin) {
57       if (Triple.getArch() != llvm::Triple::x86_64) {
58         D.Diag(diag::err_drv_clang_unsupported)
59             << (std::string(XRayInstrumentOption) + " on " + Triple.str());
60       }
61     } else {
62       D.Diag(diag::err_drv_clang_unsupported)
63           << (std::string(XRayInstrumentOption) + " on " + Triple.str());
64     }
65     XRayInstrument = true;
66     if (const Arg *A =
67             Args.getLastArg(options::OPT_fxray_instruction_threshold_,
68                             options::OPT_fxray_instruction_threshold_EQ)) {
69       StringRef S = A->getValue();
70       if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
71         D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
72     }
73 
74     // By default, the back-end will not emit the lowering for XRay customevent
75     // calls if the function is not instrumented. In the future we will change
76     // this default to be the reverse, but in the meantime we're going to
77     // introduce the new functionality behind a flag.
78     if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
79                      options::OPT_fnoxray_always_emit_customevents, false))
80       XRayAlwaysEmitCustomEvents = true;
81 
82     if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
83                      options::OPT_fnoxray_always_emit_typedevents, false))
84       XRayAlwaysEmitTypedEvents = true;
85 
86     if (!Args.hasFlag(options::OPT_fxray_link_deps,
87                       options::OPT_fnoxray_link_deps, true))
88       XRayRT = false;
89 
90     auto Bundles =
91         Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
92     if (Bundles.empty())
93       InstrumentationBundle.Mask = XRayInstrKind::All;
94     else
95       for (const auto &B : Bundles) {
96         llvm::SmallVector<StringRef, 2> BundleParts;
97         llvm::SplitString(B, BundleParts, ",");
98         for (const auto &P : BundleParts) {
99           // TODO: Automate the generation of the string case table.
100           auto Valid = llvm::StringSwitch<bool>(P)
101                            .Cases("none", "all", "function", "custom", true)
102                            .Default(false);
103 
104           if (!Valid) {
105             D.Diag(clang::diag::err_drv_invalid_value)
106                 << "-fxray-instrumentation-bundle=" << P;
107             continue;
108           }
109 
110           auto Mask = parseXRayInstrValue(P);
111           if (Mask == XRayInstrKind::None) {
112             InstrumentationBundle.clear();
113             break;
114           }
115 
116           InstrumentationBundle.Mask |= Mask;
117         }
118       }
119 
120     // Validate the always/never attribute files. We also make sure that they
121     // are treated as actual dependencies.
122     for (const auto &Filename :
123          Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
124       if (llvm::sys::fs::exists(Filename)) {
125         AlwaysInstrumentFiles.push_back(Filename);
126         ExtraDeps.push_back(Filename);
127       } else
128         D.Diag(clang::diag::err_drv_no_such_file) << Filename;
129     }
130 
131     for (const auto &Filename :
132          Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
133       if (llvm::sys::fs::exists(Filename)) {
134         NeverInstrumentFiles.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_attr_list)) {
142       if (llvm::sys::fs::exists(Filename)) {
143         AttrListFiles.push_back(Filename);
144         ExtraDeps.push_back(Filename);
145       } else
146         D.Diag(clang::diag::err_drv_no_such_file) << Filename;
147     }
148 
149     // Get the list of modes we want to support.
150     auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
151     if (SpecifiedModes.empty())
152       llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
153     else
154       for (const auto &Arg : SpecifiedModes) {
155         // Parse CSV values for -fxray-modes=...
156         llvm::SmallVector<StringRef, 2> ModeParts;
157         llvm::SplitString(Arg, ModeParts, ",");
158         for (const auto &M : ModeParts)
159           if (M == "none")
160             Modes.clear();
161           else if (M == "all")
162             llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
163           else
164             Modes.push_back(M);
165       }
166 
167     // Then we want to sort and unique the modes we've collected.
168     llvm::sort(Modes);
169     Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
170   }
171 }
172 
173 void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
174                        ArgStringList &CmdArgs, types::ID InputType) const {
175   if (!XRayInstrument)
176     return;
177 
178   CmdArgs.push_back(XRayInstrumentOption);
179 
180   if (XRayAlwaysEmitCustomEvents)
181     CmdArgs.push_back("-fxray-always-emit-customevents");
182 
183   if (XRayAlwaysEmitTypedEvents)
184     CmdArgs.push_back("-fxray-always-emit-typedevents");
185 
186   CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
187                                        Twine(InstructionThreshold)));
188 
189   for (const auto &Always : AlwaysInstrumentFiles) {
190     SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
191     AlwaysInstrumentOpt += Always;
192     CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
193   }
194 
195   for (const auto &Never : NeverInstrumentFiles) {
196     SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
197     NeverInstrumentOpt += Never;
198     CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
199   }
200 
201   for (const auto &AttrFile : AttrListFiles) {
202     SmallString<64> AttrListFileOpt("-fxray-attr-list=");
203     AttrListFileOpt += AttrFile;
204     CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
205   }
206 
207   for (const auto &Dep : ExtraDeps) {
208     SmallString<64> ExtraDepOpt("-fdepfile-entry=");
209     ExtraDepOpt += Dep;
210     CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
211   }
212 
213   for (const auto &Mode : Modes) {
214     SmallString<64> ModeOpt("-fxray-modes=");
215     ModeOpt += Mode;
216     CmdArgs.push_back(Args.MakeArgString(ModeOpt));
217   }
218 
219   SmallString<64> Bundle("-fxray-instrumentation-bundle=");
220   if (InstrumentationBundle.full()) {
221     Bundle += "all";
222   } else if (InstrumentationBundle.empty()) {
223     Bundle += "none";
224   } else {
225     if (InstrumentationBundle.has(XRayInstrKind::Function))
226       Bundle += "function";
227     if (InstrumentationBundle.has(XRayInstrKind::Custom))
228       Bundle += "custom";
229     if (InstrumentationBundle.has(XRayInstrKind::Typed))
230       Bundle += "typed";
231   }
232   CmdArgs.push_back(Args.MakeArgString(Bundle));
233 }
234