Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 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" |
Dean Michael Berris | 00ad3d5 | 2017-03-30 01:05:09 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ScopedPrinter.h" |
Dean Michael Berris | 504fc22 | 2017-03-30 22:46:45 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SpecialCaseList.h" |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 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="; |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 30 | constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"}; |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 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)) { |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 38 | if (Triple.getOS() == llvm::Triple::Linux) { |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 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 | } |
Dean Michael Berris | 9b59233 | 2018-04-04 12:47:49 +0000 | [diff] [blame] | 53 | } else if (Triple.getOS() == llvm::Triple::FreeBSD || |
Kamil Rytarowski | 448631f | 2018-05-11 00:58:55 +0000 | [diff] [blame] | 54 | Triple.getOS() == llvm::Triple::OpenBSD || |
David Carlier | 6e116a5 | 2018-08-27 05:16:09 +0000 | [diff] [blame] | 55 | Triple.getOS() == llvm::Triple::NetBSD || |
| 56 | Triple.getOS() == llvm::Triple::Darwin) { |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 57 | if (Triple.getArch() != llvm::Triple::x86_64) { |
| 58 | D.Diag(diag::err_drv_clang_unsupported) |
| 59 | << (std::string(XRayInstrumentOption) + " on " + Triple.str()); |
| 60 | } |
Petr Hosek | da91431 | 2018-11-22 02:36:47 +0000 | [diff] [blame^] | 61 | } else if (Triple.getOS() == llvm::Triple::Fuchsia) { |
| 62 | switch (Triple.getArch()) { |
| 63 | case llvm::Triple::x86_64: |
| 64 | case llvm::Triple::aarch64: |
| 65 | break; |
| 66 | default: |
| 67 | D.Diag(diag::err_drv_clang_unsupported) |
| 68 | << (std::string(XRayInstrumentOption) + " on " + Triple.str()); |
| 69 | } |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 70 | } else { |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 71 | D.Diag(diag::err_drv_clang_unsupported) |
Dean Michael Berris | 488f7c2 | 2018-04-13 02:31:58 +0000 | [diff] [blame] | 72 | << (std::string(XRayInstrumentOption) + " on " + Triple.str()); |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 73 | } |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 74 | XRayInstrument = true; |
| 75 | if (const Arg *A = |
| 76 | Args.getLastArg(options::OPT_fxray_instruction_threshold_, |
| 77 | options::OPT_fxray_instruction_threshold_EQ)) { |
| 78 | StringRef S = A->getValue(); |
| 79 | if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0) |
| 80 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
| 81 | } |
| 82 | |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 83 | // By default, the back-end will not emit the lowering for XRay customevent |
| 84 | // calls if the function is not instrumented. In the future we will change |
| 85 | // this default to be the reverse, but in the meantime we're going to |
| 86 | // introduce the new functionality behind a flag. |
| 87 | if (Args.hasFlag(options::OPT_fxray_always_emit_customevents, |
| 88 | options::OPT_fnoxray_always_emit_customevents, false)) |
| 89 | XRayAlwaysEmitCustomEvents = true; |
| 90 | |
Keith Wyss | f437e35 | 2018-04-17 21:32:43 +0000 | [diff] [blame] | 91 | if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents, |
| 92 | options::OPT_fnoxray_always_emit_typedevents, false)) |
| 93 | XRayAlwaysEmitTypedEvents = true; |
| 94 | |
Dean Michael Berris | 248148d | 2018-04-06 05:28:54 +0000 | [diff] [blame] | 95 | if (!Args.hasFlag(options::OPT_fxray_link_deps, |
| 96 | options::OPT_fnoxray_link_deps, true)) |
| 97 | XRayRT = false; |
| 98 | |
Dean Michael Berris | 488f7c2 | 2018-04-13 02:31:58 +0000 | [diff] [blame] | 99 | auto Bundles = |
| 100 | Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle); |
| 101 | if (Bundles.empty()) |
| 102 | InstrumentationBundle.Mask = XRayInstrKind::All; |
| 103 | else |
| 104 | for (const auto &B : Bundles) { |
| 105 | llvm::SmallVector<StringRef, 2> BundleParts; |
| 106 | llvm::SplitString(B, BundleParts, ","); |
| 107 | for (const auto &P : BundleParts) { |
| 108 | // TODO: Automate the generation of the string case table. |
| 109 | auto Valid = llvm::StringSwitch<bool>(P) |
| 110 | .Cases("none", "all", "function", "custom", 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 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 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 (llvm::sys::fs::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 (llvm::sys::fs::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 | } |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 148 | |
| 149 | for (const auto &Filename : |
| 150 | Args.getAllArgValues(options::OPT_fxray_attr_list)) { |
| 151 | if (llvm::sys::fs::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 | } |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 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::copy(XRaySupportedModes, std::back_inserter(Modes)); |
| 162 | else |
| 163 | for (const auto &Arg : SpecifiedModes) { |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 164 | // Parse CSV values for -fxray-modes=... |
| 165 | llvm::SmallVector<StringRef, 2> ModeParts; |
| 166 | llvm::SplitString(Arg, ModeParts, ","); |
| 167 | for (const auto &M : ModeParts) |
Dean Michael Berris | 7fc737a | 2018-04-13 05:59:57 +0000 | [diff] [blame] | 168 | if (M == "none") |
| 169 | Modes.clear(); |
| 170 | else if (M == "all") |
| 171 | llvm::copy(XRaySupportedModes, std::back_inserter(Modes)); |
| 172 | else |
| 173 | Modes.push_back(M); |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Then we want to sort and unique the modes we've collected. |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 177 | llvm::sort(Modes); |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame] | 178 | Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end()); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args, |
| 183 | ArgStringList &CmdArgs, types::ID InputType) const { |
| 184 | if (!XRayInstrument) |
| 185 | return; |
| 186 | |
| 187 | CmdArgs.push_back(XRayInstrumentOption); |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 188 | |
| 189 | if (XRayAlwaysEmitCustomEvents) |
| 190 | CmdArgs.push_back("-fxray-always-emit-customevents"); |
| 191 | |
Keith Wyss | f437e35 | 2018-04-17 21:32:43 +0000 | [diff] [blame] | 192 | if (XRayAlwaysEmitTypedEvents) |
| 193 | CmdArgs.push_back("-fxray-always-emit-typedevents"); |
| 194 | |
Dean Michael Berris | 504fc22 | 2017-03-30 22:46:45 +0000 | [diff] [blame] | 195 | CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) + |
| 196 | Twine(InstructionThreshold))); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 197 | |
| 198 | for (const auto &Always : AlwaysInstrumentFiles) { |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 199 | SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument="); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 200 | AlwaysInstrumentOpt += Always; |
| 201 | CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt)); |
| 202 | } |
| 203 | |
| 204 | for (const auto &Never : NeverInstrumentFiles) { |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 205 | SmallString<64> NeverInstrumentOpt("-fxray-never-instrument="); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 206 | NeverInstrumentOpt += Never; |
| 207 | CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt)); |
| 208 | } |
| 209 | |
Dean Michael Berris | 488f7c2 | 2018-04-13 02:31:58 +0000 | [diff] [blame] | 210 | for (const auto &AttrFile : AttrListFiles) { |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 211 | SmallString<64> AttrListFileOpt("-fxray-attr-list="); |
| 212 | AttrListFileOpt += AttrFile; |
| 213 | CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt)); |
| 214 | } |
| 215 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 216 | for (const auto &Dep : ExtraDeps) { |
| 217 | SmallString<64> ExtraDepOpt("-fdepfile-entry="); |
| 218 | ExtraDepOpt += Dep; |
| 219 | CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt)); |
| 220 | } |
Dean Michael Berris | 7fc737a | 2018-04-13 05:59:57 +0000 | [diff] [blame] | 221 | |
| 222 | for (const auto &Mode : Modes) { |
| 223 | SmallString<64> ModeOpt("-fxray-modes="); |
| 224 | ModeOpt += Mode; |
| 225 | CmdArgs.push_back(Args.MakeArgString(ModeOpt)); |
| 226 | } |
Dean Michael Berris | 5082e25 | 2018-09-21 08:32:49 +0000 | [diff] [blame] | 227 | |
| 228 | SmallString<64> Bundle("-fxray-instrumentation-bundle="); |
| 229 | if (InstrumentationBundle.full()) { |
| 230 | Bundle += "all"; |
| 231 | } else if (InstrumentationBundle.empty()) { |
| 232 | Bundle += "none"; |
| 233 | } else { |
| 234 | if (InstrumentationBundle.has(XRayInstrKind::Function)) |
| 235 | Bundle += "function"; |
| 236 | if (InstrumentationBundle.has(XRayInstrKind::Custom)) |
| 237 | Bundle += "custom"; |
| 238 | if (InstrumentationBundle.has(XRayInstrKind::Typed)) |
| 239 | Bundle += "typed"; |
| 240 | } |
| 241 | CmdArgs.push_back(Args.MakeArgString(Bundle)); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 242 | } |