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 } // 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)) { 37 if (Triple.getOS() == llvm::Triple::Linux) { 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 } 52 } else if (Triple.getOS() == llvm::Triple::FreeBSD) { 53 if (Triple.getArch() != llvm::Triple::x86_64) { 54 D.Diag(diag::err_drv_clang_unsupported) 55 << (std::string(XRayInstrumentOption) + " on " + Triple.str()); 56 } 57 } else { 58 D.Diag(diag::err_drv_clang_unsupported) 59 << (std::string(XRayInstrumentOption) + " on non-supported target OS"); 60 } 61 XRayInstrument = true; 62 if (const Arg *A = 63 Args.getLastArg(options::OPT_fxray_instruction_threshold_, 64 options::OPT_fxray_instruction_threshold_EQ)) { 65 StringRef S = A->getValue(); 66 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0) 67 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; 68 } 69 70 // By default, the back-end will not emit the lowering for XRay customevent 71 // calls if the function is not instrumented. In the future we will change 72 // this default to be the reverse, but in the meantime we're going to 73 // introduce the new functionality behind a flag. 74 if (Args.hasFlag(options::OPT_fxray_always_emit_customevents, 75 options::OPT_fnoxray_always_emit_customevents, false)) 76 XRayAlwaysEmitCustomEvents = true; 77 78 // Validate the always/never attribute files. We also make sure that they 79 // are treated as actual dependencies. 80 for (const auto &Filename : 81 Args.getAllArgValues(options::OPT_fxray_always_instrument)) { 82 if (llvm::sys::fs::exists(Filename)) { 83 AlwaysInstrumentFiles.push_back(Filename); 84 ExtraDeps.push_back(Filename); 85 } else 86 D.Diag(clang::diag::err_drv_no_such_file) << Filename; 87 } 88 89 for (const auto &Filename : 90 Args.getAllArgValues(options::OPT_fxray_never_instrument)) { 91 if (llvm::sys::fs::exists(Filename)) { 92 NeverInstrumentFiles.push_back(Filename); 93 ExtraDeps.push_back(Filename); 94 } else 95 D.Diag(clang::diag::err_drv_no_such_file) << Filename; 96 } 97 } 98 } 99 100 void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args, 101 ArgStringList &CmdArgs, types::ID InputType) const { 102 if (!XRayInstrument) 103 return; 104 105 CmdArgs.push_back(XRayInstrumentOption); 106 107 if (XRayAlwaysEmitCustomEvents) 108 CmdArgs.push_back("-fxray-always-emit-customevents"); 109 110 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) + 111 Twine(InstructionThreshold))); 112 113 for (const auto &Always : AlwaysInstrumentFiles) { 114 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument="); 115 AlwaysInstrumentOpt += Always; 116 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt)); 117 } 118 119 for (const auto &Never : NeverInstrumentFiles) { 120 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument="); 121 NeverInstrumentOpt += Never; 122 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt)); 123 } 124 125 for (const auto &Dep : ExtraDeps) { 126 SmallString<64> ExtraDepOpt("-fdepfile-entry="); 127 ExtraDepOpt += Dep; 128 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt)); 129 } 130 } 131