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 | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 30 | } // namespace |
| 31 | |
| 32 | XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) { |
| 33 | const Driver &D = TC.getDriver(); |
| 34 | const llvm::Triple &Triple = TC.getTriple(); |
| 35 | if (Args.hasFlag(options::OPT_fxray_instrument, |
| 36 | options::OPT_fnoxray_instrument, false)) { |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 37 | if (Triple.getOS() == llvm::Triple::Linux) { |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 38 | switch (Triple.getArch()) { |
| 39 | case llvm::Triple::x86_64: |
| 40 | case llvm::Triple::arm: |
| 41 | case llvm::Triple::aarch64: |
| 42 | case llvm::Triple::ppc64le: |
| 43 | case llvm::Triple::mips: |
| 44 | case llvm::Triple::mipsel: |
| 45 | case llvm::Triple::mips64: |
| 46 | case llvm::Triple::mips64el: |
| 47 | break; |
| 48 | default: |
| 49 | D.Diag(diag::err_drv_clang_unsupported) |
| 50 | << (std::string(XRayInstrumentOption) + " on " + Triple.str()); |
| 51 | } |
Dean Michael Berris | 9b59233 | 2018-04-04 12:47:49 +0000 | [diff] [blame] | 52 | } else if (Triple.getOS() == llvm::Triple::FreeBSD || |
| 53 | Triple.getOS() == llvm::Triple::OpenBSD) { |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 54 | if (Triple.getArch() != llvm::Triple::x86_64) { |
| 55 | D.Diag(diag::err_drv_clang_unsupported) |
| 56 | << (std::string(XRayInstrumentOption) + " on " + Triple.str()); |
| 57 | } |
| 58 | } else { |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 59 | D.Diag(diag::err_drv_clang_unsupported) |
Kamil Rytarowski | 3e4e74c | 2018-02-22 06:31:40 +0000 | [diff] [blame] | 60 | << (std::string(XRayInstrumentOption) + " on non-supported target OS"); |
| 61 | } |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 62 | XRayInstrument = true; |
| 63 | if (const Arg *A = |
| 64 | Args.getLastArg(options::OPT_fxray_instruction_threshold_, |
| 65 | options::OPT_fxray_instruction_threshold_EQ)) { |
| 66 | StringRef S = A->getValue(); |
| 67 | if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0) |
| 68 | D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; |
| 69 | } |
| 70 | |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 71 | // By default, the back-end will not emit the lowering for XRay customevent |
| 72 | // calls if the function is not instrumented. In the future we will change |
| 73 | // this default to be the reverse, but in the meantime we're going to |
| 74 | // introduce the new functionality behind a flag. |
| 75 | if (Args.hasFlag(options::OPT_fxray_always_emit_customevents, |
| 76 | options::OPT_fnoxray_always_emit_customevents, false)) |
| 77 | XRayAlwaysEmitCustomEvents = true; |
| 78 | |
Dean Michael Berris | 248148d | 2018-04-06 05:28:54 +0000 | [diff] [blame] | 79 | if (!Args.hasFlag(options::OPT_fxray_link_deps, |
| 80 | options::OPT_fnoxray_link_deps, true)) |
| 81 | XRayRT = false; |
| 82 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 83 | // Validate the always/never attribute files. We also make sure that they |
| 84 | // are treated as actual dependencies. |
| 85 | for (const auto &Filename : |
| 86 | Args.getAllArgValues(options::OPT_fxray_always_instrument)) { |
| 87 | if (llvm::sys::fs::exists(Filename)) { |
| 88 | AlwaysInstrumentFiles.push_back(Filename); |
| 89 | ExtraDeps.push_back(Filename); |
| 90 | } else |
| 91 | D.Diag(clang::diag::err_drv_no_such_file) << Filename; |
| 92 | } |
| 93 | |
| 94 | for (const auto &Filename : |
| 95 | Args.getAllArgValues(options::OPT_fxray_never_instrument)) { |
| 96 | if (llvm::sys::fs::exists(Filename)) { |
| 97 | NeverInstrumentFiles.push_back(Filename); |
| 98 | ExtraDeps.push_back(Filename); |
| 99 | } else |
| 100 | D.Diag(clang::diag::err_drv_no_such_file) << Filename; |
| 101 | } |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame^] | 102 | |
| 103 | for (const auto &Filename : |
| 104 | Args.getAllArgValues(options::OPT_fxray_attr_list)) { |
| 105 | if (llvm::sys::fs::exists(Filename)) { |
| 106 | AttrListFiles.push_back(Filename); |
| 107 | ExtraDeps.push_back(Filename); |
| 108 | } else |
| 109 | D.Diag(clang::diag::err_drv_no_such_file) << Filename; |
| 110 | } |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args, |
| 115 | ArgStringList &CmdArgs, types::ID InputType) const { |
| 116 | if (!XRayInstrument) |
| 117 | return; |
| 118 | |
| 119 | CmdArgs.push_back(XRayInstrumentOption); |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 120 | |
| 121 | if (XRayAlwaysEmitCustomEvents) |
| 122 | CmdArgs.push_back("-fxray-always-emit-customevents"); |
| 123 | |
Dean Michael Berris | 504fc22 | 2017-03-30 22:46:45 +0000 | [diff] [blame] | 124 | CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) + |
| 125 | Twine(InstructionThreshold))); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 126 | |
| 127 | for (const auto &Always : AlwaysInstrumentFiles) { |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 128 | SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument="); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 129 | AlwaysInstrumentOpt += Always; |
| 130 | CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt)); |
| 131 | } |
| 132 | |
| 133 | for (const auto &Never : NeverInstrumentFiles) { |
Dean Michael Berris | 1a5b10d | 2017-11-30 00:04:54 +0000 | [diff] [blame] | 134 | SmallString<64> NeverInstrumentOpt("-fxray-never-instrument="); |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 135 | NeverInstrumentOpt += Never; |
| 136 | CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt)); |
| 137 | } |
| 138 | |
Dean Michael Berris | 20dc6ef | 2018-04-09 04:02:09 +0000 | [diff] [blame^] | 139 | for (const auto&AttrFile : AttrListFiles) { |
| 140 | SmallString<64> AttrListFileOpt("-fxray-attr-list="); |
| 141 | AttrListFileOpt += AttrFile; |
| 142 | CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt)); |
| 143 | } |
| 144 | |
Dean Michael Berris | 835832d | 2017-03-30 00:29:36 +0000 | [diff] [blame] | 145 | for (const auto &Dep : ExtraDeps) { |
| 146 | SmallString<64> ExtraDepOpt("-fdepfile-entry="); |
| 147 | ExtraDepOpt += Dep; |
| 148 | CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt)); |
| 149 | } |
| 150 | } |