1 //===--- XRayArgs.cpp - Arguments for XRay --------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 #include "clang/Driver/XRayArgs.h" 9 #include "ToolChains/CommonArgs.h" 10 #include "clang/Driver/Driver.h" 11 #include "clang/Driver/DriverDiagnostic.h" 12 #include "clang/Driver/Options.h" 13 #include "clang/Driver/ToolChain.h" 14 #include "llvm/ADT/StringExtras.h" 15 #include "llvm/ADT/StringSwitch.h" 16 #include "llvm/Support/Path.h" 17 #include "llvm/Support/ScopedPrinter.h" 18 #include "llvm/Support/SpecialCaseList.h" 19 #include "llvm/Support/VirtualFileSystem.h" 20 21 using namespace clang; 22 using namespace clang::driver; 23 using namespace llvm::opt; 24 25 namespace { 26 constexpr char XRayInstrumentOption[] = "-fxray-instrument"; 27 constexpr char XRayInstructionThresholdOption[] = 28 "-fxray-instruction-threshold="; 29 constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"}; 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_fno_xray_instrument, false)) 37 return; 38 if (Triple.getOS() == llvm::Triple::Linux) { 39 switch (Triple.getArch()) { 40 case llvm::Triple::x86_64: 41 case llvm::Triple::arm: 42 case llvm::Triple::aarch64: 43 case llvm::Triple::hexagon: 44 case llvm::Triple::ppc64le: 45 case llvm::Triple::mips: 46 case llvm::Triple::mipsel: 47 case llvm::Triple::mips64: 48 case llvm::Triple::mips64el: 49 break; 50 default: 51 D.Diag(diag::err_drv_clang_unsupported) 52 << (std::string(XRayInstrumentOption) + " on " + Triple.str()); 53 } 54 } else if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() || 55 Triple.isOSNetBSD() || Triple.isMacOSX()) { 56 if (Triple.getArch() != llvm::Triple::x86_64) { 57 D.Diag(diag::err_drv_clang_unsupported) 58 << (std::string(XRayInstrumentOption) + " on " + Triple.str()); 59 } 60 } else if (Triple.getOS() == llvm::Triple::Fuchsia) { 61 switch (Triple.getArch()) { 62 case llvm::Triple::x86_64: 63 case llvm::Triple::aarch64: 64 break; 65 default: 66 D.Diag(diag::err_drv_clang_unsupported) 67 << (std::string(XRayInstrumentOption) + " on " + Triple.str()); 68 } 69 } else { 70 D.Diag(diag::err_drv_clang_unsupported) 71 << (std::string(XRayInstrumentOption) + " on " + Triple.str()); 72 } 73 74 // Both XRay and -fpatchable-function-entry use 75 // TargetOpcode::PATCHABLE_FUNCTION_ENTER. 76 if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ)) 77 D.Diag(diag::err_drv_argument_not_allowed_with) 78 << "-fxray-instrument" << A->getSpelling(); 79 80 XRayInstrument = true; 81 if (const Arg *A = 82 Args.getLastArg(options::OPT_fxray_instruction_threshold_, 83 options::OPT_fxray_instruction_threshold_EQ)) { 84 StringRef S = A->getValue(); 85 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0) 86 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; 87 } 88 89 // By default, the back-end will not emit the lowering for XRay customevent 90 // calls if the function is not instrumented. In the future we will change 91 // this default to be the reverse, but in the meantime we're going to 92 // introduce the new functionality behind a flag. 93 if (Args.hasFlag(options::OPT_fxray_always_emit_customevents, 94 options::OPT_fno_xray_always_emit_customevents, false)) 95 XRayAlwaysEmitCustomEvents = true; 96 97 if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents, 98 options::OPT_fno_xray_always_emit_typedevents, false)) 99 XRayAlwaysEmitTypedEvents = true; 100 101 if (!Args.hasFlag(options::OPT_fxray_link_deps, 102 options::OPT_fnoxray_link_deps, true)) 103 XRayRT = false; 104 105 if (Args.hasFlag(options::OPT_fxray_ignore_loops, 106 options::OPT_fno_xray_ignore_loops, false)) 107 XRayIgnoreLoops = true; 108 109 XRayFunctionIndex = Args.hasFlag(options::OPT_fxray_function_index, 110 options::OPT_fno_xray_function_index, true); 111 112 auto Bundles = 113 Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle); 114 if (Bundles.empty()) 115 InstrumentationBundle.Mask = XRayInstrKind::All; 116 else 117 for (const auto &B : Bundles) { 118 llvm::SmallVector<StringRef, 2> BundleParts; 119 llvm::SplitString(B, BundleParts, ","); 120 for (const auto &P : BundleParts) { 121 // TODO: Automate the generation of the string case table. 122 auto Valid = llvm::StringSwitch<bool>(P) 123 .Cases("none", "all", "function", "function-entry", 124 "function-exit", "custom", true) 125 .Default(false); 126 127 if (!Valid) { 128 D.Diag(clang::diag::err_drv_invalid_value) 129 << "-fxray-instrumentation-bundle=" << P; 130 continue; 131 } 132 133 auto Mask = parseXRayInstrValue(P); 134 if (Mask == XRayInstrKind::None) { 135 InstrumentationBundle.clear(); 136 break; 137 } 138 139 InstrumentationBundle.Mask |= Mask; 140 } 141 } 142 143 // Validate the always/never attribute files. We also make sure that they 144 // are treated as actual dependencies. 145 for (const auto &Filename : 146 Args.getAllArgValues(options::OPT_fxray_always_instrument)) { 147 if (D.getVFS().exists(Filename)) { 148 AlwaysInstrumentFiles.push_back(Filename); 149 ExtraDeps.push_back(Filename); 150 } else 151 D.Diag(clang::diag::err_drv_no_such_file) << Filename; 152 } 153 154 for (const auto &Filename : 155 Args.getAllArgValues(options::OPT_fxray_never_instrument)) { 156 if (D.getVFS().exists(Filename)) { 157 NeverInstrumentFiles.push_back(Filename); 158 ExtraDeps.push_back(Filename); 159 } else 160 D.Diag(clang::diag::err_drv_no_such_file) << Filename; 161 } 162 163 for (const auto &Filename : 164 Args.getAllArgValues(options::OPT_fxray_attr_list)) { 165 if (D.getVFS().exists(Filename)) { 166 AttrListFiles.push_back(Filename); 167 ExtraDeps.push_back(Filename); 168 } else 169 D.Diag(clang::diag::err_drv_no_such_file) << Filename; 170 } 171 172 // Get the list of modes we want to support. 173 auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes); 174 if (SpecifiedModes.empty()) 175 llvm::copy(XRaySupportedModes, std::back_inserter(Modes)); 176 else 177 for (const auto &Arg : SpecifiedModes) { 178 // Parse CSV values for -fxray-modes=... 179 llvm::SmallVector<StringRef, 2> ModeParts; 180 llvm::SplitString(Arg, ModeParts, ","); 181 for (const auto &M : ModeParts) 182 if (M == "none") 183 Modes.clear(); 184 else if (M == "all") 185 llvm::copy(XRaySupportedModes, std::back_inserter(Modes)); 186 else 187 Modes.push_back(std::string(M)); 188 } 189 190 if (const Arg *A = Args.getLastArg(options::OPT_fxray_function_groups)) { 191 StringRef S = A->getValue(); 192 if (S.getAsInteger(0, XRayFunctionGroups) || XRayFunctionGroups < 1) 193 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; 194 } 195 196 if (const Arg *A = 197 Args.getLastArg(options::OPT_fxray_selected_function_group)) { 198 StringRef S = A->getValue(); 199 if (S.getAsInteger(0, XRaySelectedFunctionGroup) || 200 XRaySelectedFunctionGroup < 0 || 201 XRaySelectedFunctionGroup >= XRayFunctionGroups) 202 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S; 203 } 204 205 // Then we want to sort and unique the modes we've collected. 206 llvm::sort(Modes); 207 Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end()); 208 } 209 210 void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args, 211 ArgStringList &CmdArgs, types::ID InputType) const { 212 if (!XRayInstrument) 213 return; 214 215 CmdArgs.push_back(XRayInstrumentOption); 216 217 if (XRayAlwaysEmitCustomEvents) 218 CmdArgs.push_back("-fxray-always-emit-customevents"); 219 220 if (XRayAlwaysEmitTypedEvents) 221 CmdArgs.push_back("-fxray-always-emit-typedevents"); 222 223 if (XRayIgnoreLoops) 224 CmdArgs.push_back("-fxray-ignore-loops"); 225 226 if (!XRayFunctionIndex) 227 CmdArgs.push_back("-fno-xray-function-index"); 228 229 if (XRayFunctionGroups > 1) { 230 CmdArgs.push_back(Args.MakeArgString(Twine("-fxray-function-groups=") + 231 Twine(XRayFunctionGroups))); 232 } 233 234 if (XRaySelectedFunctionGroup != 0) { 235 CmdArgs.push_back( 236 Args.MakeArgString(Twine("-fxray-selected-function-group=") + 237 Twine(XRaySelectedFunctionGroup))); 238 } 239 240 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) + 241 Twine(InstructionThreshold))); 242 243 for (const auto &Always : AlwaysInstrumentFiles) { 244 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument="); 245 AlwaysInstrumentOpt += Always; 246 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt)); 247 } 248 249 for (const auto &Never : NeverInstrumentFiles) { 250 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument="); 251 NeverInstrumentOpt += Never; 252 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt)); 253 } 254 255 for (const auto &AttrFile : AttrListFiles) { 256 SmallString<64> AttrListFileOpt("-fxray-attr-list="); 257 AttrListFileOpt += AttrFile; 258 CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt)); 259 } 260 261 for (const auto &Dep : ExtraDeps) { 262 SmallString<64> ExtraDepOpt("-fdepfile-entry="); 263 ExtraDepOpt += Dep; 264 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt)); 265 } 266 267 for (const auto &Mode : Modes) { 268 SmallString<64> ModeOpt("-fxray-modes="); 269 ModeOpt += Mode; 270 CmdArgs.push_back(Args.MakeArgString(ModeOpt)); 271 } 272 273 SmallString<64> Bundle("-fxray-instrumentation-bundle="); 274 if (InstrumentationBundle.full()) { 275 Bundle += "all"; 276 } else if (InstrumentationBundle.empty()) { 277 Bundle += "none"; 278 } else { 279 if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry) && 280 InstrumentationBundle.has(XRayInstrKind::FunctionExit)) 281 Bundle += "function"; 282 else if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry)) 283 Bundle += "function-entry"; 284 else if (InstrumentationBundle.has(XRayInstrKind::FunctionExit)) 285 Bundle += "function-exit"; 286 287 if (InstrumentationBundle.has(XRayInstrKind::Custom)) 288 Bundle += "custom"; 289 if (InstrumentationBundle.has(XRayInstrKind::Typed)) 290 Bundle += "typed"; 291 } 292 CmdArgs.push_back(Args.MakeArgString(Bundle)); 293 } 294