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 || |
| 54 | Triple.getOS() == llvm::Triple::OpenBSD) { |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame^] | 55 | if (Triple.getArch() != llvm::Triple::x86_64) { |
| 56 | D.Diag(diag::err_drv_clang_unsupported) |
| 57 | << (std::string(XRayInstrumentOption) + " on " + Triple.str()); |
| 58 | } |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 59 | } else { |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 60 | D.Diag(diag::err_drv_clang_unsupported) |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame^] | 61 | << (std::string(XRayInstrumentOption) + |
| 62 | " on non-supported target OS"); |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 63 | } |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 64 | XRayInstrument = true; |
| 65 | if (const Arg *A = |
| 66 | Args.getLastArg(options::OPT_fxray_instruction_threshold_, |
| 67 | options::OPT_fxray_instruction_threshold_EQ)) { |
| 68 | StringRef S = A->getValue(); |
| 69 | if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0) |
| 70 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
| 71 | } |
| 72 | |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 73 | // By default, the back-end will not emit the lowering for XRay customevent |
| 74 | // calls if the function is not instrumented. In the future we will change |
| 75 | // this default to be the reverse, but in the meantime we're going to |
| 76 | // introduce the new functionality behind a flag. |
| 77 | if (Args.hasFlag(options::OPT_fxray_always_emit_customevents, |
| 78 | options::OPT_fnoxray_always_emit_customevents, false)) |
| 79 | XRayAlwaysEmitCustomEvents = true; |
| 80 | |
Dean Michael Berris | 248148d | 2018-04-06 05:28:54 +0000 | [diff] [blame] | 81 | if (!Args.hasFlag(options::OPT_fxray_link_deps, |
| 82 | options::OPT_fnoxray_link_deps, true)) |
| 83 | XRayRT = false; |
| 84 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 85 | // Validate the always/never attribute files. We also make sure that they |
| 86 | // are treated as actual dependencies. |
| 87 | for (const auto &Filename : |
| 88 | Args.getAllArgValues(options::OPT_fxray_always_instrument)) { |
| 89 | if (llvm::sys::fs::exists(Filename)) { |
| 90 | AlwaysInstrumentFiles.push_back(Filename); |
| 91 | ExtraDeps.push_back(Filename); |
| 92 | } else |
| 93 | D.Diag(clang::diag::err_drv_no_such_file) << Filename; |
| 94 | } |
| 95 | |
| 96 | for (const auto &Filename : |
| 97 | Args.getAllArgValues(options::OPT_fxray_never_instrument)) { |
| 98 | if (llvm::sys::fs::exists(Filename)) { |
| 99 | NeverInstrumentFiles.push_back(Filename); |
| 100 | ExtraDeps.push_back(Filename); |
| 101 | } else |
| 102 | D.Diag(clang::diag::err_drv_no_such_file) << Filename; |
| 103 | } |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 104 | |
| 105 | for (const auto &Filename : |
| 106 | Args.getAllArgValues(options::OPT_fxray_attr_list)) { |
| 107 | if (llvm::sys::fs::exists(Filename)) { |
| 108 | AttrListFiles.push_back(Filename); |
| 109 | ExtraDeps.push_back(Filename); |
| 110 | } else |
| 111 | D.Diag(clang::diag::err_drv_no_such_file) << Filename; |
| 112 | } |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame^] | 113 | |
| 114 | // Get the list of modes we want to support. |
| 115 | auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes); |
| 116 | if (SpecifiedModes.empty()) |
| 117 | llvm::copy(XRaySupportedModes, std::back_inserter(Modes)); |
| 118 | else |
| 119 | for (const auto &Arg : SpecifiedModes) { |
| 120 | if (Arg == "none") { |
| 121 | Modes.clear(); |
| 122 | break; |
| 123 | } |
| 124 | if (Arg == "all") { |
| 125 | Modes.clear(); |
| 126 | llvm::copy(XRaySupportedModes, std::back_inserter(Modes)); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | // Parse CSV values for -fxray-modes=... |
| 131 | llvm::SmallVector<StringRef, 2> ModeParts; |
| 132 | llvm::SplitString(Arg, ModeParts, ","); |
| 133 | for (const auto &M : ModeParts) |
| 134 | Modes.push_back(M); |
| 135 | } |
| 136 | |
| 137 | // Then we want to sort and unique the modes we've collected. |
| 138 | std::sort(Modes.begin(), Modes.end()); |
| 139 | Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end()); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args, |
| 144 | ArgStringList &CmdArgs, types::ID InputType) const { |
| 145 | if (!XRayInstrument) |
| 146 | return; |
| 147 | |
| 148 | CmdArgs.push_back(XRayInstrumentOption); |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 149 | |
| 150 | if (XRayAlwaysEmitCustomEvents) |
| 151 | CmdArgs.push_back("-fxray-always-emit-customevents"); |
| 152 | |
Dean Michael Berris | 504fc22 | 2017-03-30 22:46:45 +0000 | [diff] [blame] | 153 | CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) + |
| 154 | Twine(InstructionThreshold))); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 155 | |
| 156 | for (const auto &Always : AlwaysInstrumentFiles) { |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 157 | SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument="); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 158 | AlwaysInstrumentOpt += Always; |
| 159 | CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt)); |
| 160 | } |
| 161 | |
| 162 | for (const auto &Never : NeverInstrumentFiles) { |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 163 | SmallString<64> NeverInstrumentOpt("-fxray-never-instrument="); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 164 | NeverInstrumentOpt += Never; |
| 165 | CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt)); |
| 166 | } |
| 167 | |
Dean Michael Berris | 826e666 | 2018-04-11 01:28:25 +0000 | [diff] [blame^] | 168 | for (const auto& AttrFile : AttrListFiles) { |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame] | 169 | SmallString<64> AttrListFileOpt("-fxray-attr-list="); |
| 170 | AttrListFileOpt += AttrFile; |
| 171 | CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt)); |
| 172 | } |
| 173 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 174 | for (const auto &Dep : ExtraDeps) { |
| 175 | SmallString<64> ExtraDepOpt("-fdepfile-entry="); |
| 176 | ExtraDepOpt += Dep; |
| 177 | CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt)); |
| 178 | } |
| 179 | } |