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