XRayArgs.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/FileSystem.h"
  17. #include "llvm/Support/Path.h"
  18. #include "llvm/Support/ScopedPrinter.h"
  19. #include "llvm/Support/SpecialCaseList.h"
  20. using namespace clang;
  21. using namespace clang::driver;
  22. using namespace llvm::opt;
  23. namespace {
  24. constexpr char XRayInstrumentOption[] = "-fxray-instrument";
  25. constexpr char XRayInstructionThresholdOption[] =
  26. "-fxray-instruction-threshold=";
  27. constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
  28. } // namespace
  29. XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
  30. const Driver &D = TC.getDriver();
  31. const llvm::Triple &Triple = TC.getTriple();
  32. if (Args.hasFlag(options::OPT_fxray_instrument,
  33. options::OPT_fnoxray_instrument, false)) {
  34. if (Triple.getOS() == llvm::Triple::Linux) {
  35. switch (Triple.getArch()) {
  36. case llvm::Triple::x86_64:
  37. case llvm::Triple::arm:
  38. case llvm::Triple::aarch64:
  39. case llvm::Triple::ppc64le:
  40. case llvm::Triple::mips:
  41. case llvm::Triple::mipsel:
  42. case llvm::Triple::mips64:
  43. case llvm::Triple::mips64el:
  44. break;
  45. default:
  46. D.Diag(diag::err_drv_clang_unsupported)
  47. << (std::string(XRayInstrumentOption) + " on " + Triple.str());
  48. }
  49. } else if (Triple.isOSFreeBSD() ||
  50. Triple.isOSOpenBSD() ||
  51. Triple.isOSNetBSD() ||
  52. Triple.isMacOSX()) {
  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 if (Triple.getOS() == llvm::Triple::Fuchsia) {
  58. switch (Triple.getArch()) {
  59. case llvm::Triple::x86_64:
  60. case llvm::Triple::aarch64:
  61. break;
  62. default:
  63. D.Diag(diag::err_drv_clang_unsupported)
  64. << (std::string(XRayInstrumentOption) + " on " + Triple.str());
  65. }
  66. } else {
  67. D.Diag(diag::err_drv_clang_unsupported)
  68. << (std::string(XRayInstrumentOption) + " on " + Triple.str());
  69. }
  70. XRayInstrument = true;
  71. if (const Arg *A =
  72. Args.getLastArg(options::OPT_fxray_instruction_threshold_,
  73. options::OPT_fxray_instruction_threshold_EQ)) {
  74. StringRef S = A->getValue();
  75. if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
  76. D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
  77. }
  78. // By default, the back-end will not emit the lowering for XRay customevent
  79. // calls if the function is not instrumented. In the future we will change
  80. // this default to be the reverse, but in the meantime we're going to
  81. // introduce the new functionality behind a flag.
  82. if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
  83. options::OPT_fnoxray_always_emit_customevents, false))
  84. XRayAlwaysEmitCustomEvents = true;
  85. if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
  86. options::OPT_fnoxray_always_emit_typedevents, false))
  87. XRayAlwaysEmitTypedEvents = true;
  88. if (!Args.hasFlag(options::OPT_fxray_link_deps,
  89. options::OPT_fnoxray_link_deps, true))
  90. XRayRT = false;
  91. auto Bundles =
  92. Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
  93. if (Bundles.empty())
  94. InstrumentationBundle.Mask = XRayInstrKind::All;
  95. else
  96. for (const auto &B : Bundles) {
  97. llvm::SmallVector<StringRef, 2> BundleParts;
  98. llvm::SplitString(B, BundleParts, ",");
  99. for (const auto &P : BundleParts) {
  100. // TODO: Automate the generation of the string case table.
  101. auto Valid = llvm::StringSwitch<bool>(P)
  102. .Cases("none", "all", "function", "custom", true)
  103. .Default(false);
  104. if (!Valid) {
  105. D.Diag(clang::diag::err_drv_invalid_value)
  106. << "-fxray-instrumentation-bundle=" << P;
  107. continue;
  108. }
  109. auto Mask = parseXRayInstrValue(P);
  110. if (Mask == XRayInstrKind::None) {
  111. InstrumentationBundle.clear();
  112. break;
  113. }
  114. InstrumentationBundle.Mask |= Mask;
  115. }
  116. }
  117. // Validate the always/never attribute files. We also make sure that they
  118. // are treated as actual dependencies.
  119. for (const auto &Filename :
  120. Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
  121. if (llvm::sys::fs::exists(Filename)) {
  122. AlwaysInstrumentFiles.push_back(Filename);
  123. ExtraDeps.push_back(Filename);
  124. } else
  125. D.Diag(clang::diag::err_drv_no_such_file) << Filename;
  126. }
  127. for (const auto &Filename :
  128. Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
  129. if (llvm::sys::fs::exists(Filename)) {
  130. NeverInstrumentFiles.push_back(Filename);
  131. ExtraDeps.push_back(Filename);
  132. } else
  133. D.Diag(clang::diag::err_drv_no_such_file) << Filename;
  134. }
  135. for (const auto &Filename :
  136. Args.getAllArgValues(options::OPT_fxray_attr_list)) {
  137. if (llvm::sys::fs::exists(Filename)) {
  138. AttrListFiles.push_back(Filename);
  139. ExtraDeps.push_back(Filename);
  140. } else
  141. D.Diag(clang::diag::err_drv_no_such_file) << Filename;
  142. }
  143. // Get the list of modes we want to support.
  144. auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
  145. if (SpecifiedModes.empty())
  146. llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
  147. else
  148. for (const auto &Arg : SpecifiedModes) {
  149. // Parse CSV values for -fxray-modes=...
  150. llvm::SmallVector<StringRef, 2> ModeParts;
  151. llvm::SplitString(Arg, ModeParts, ",");
  152. for (const auto &M : ModeParts)
  153. if (M == "none")
  154. Modes.clear();
  155. else if (M == "all")
  156. llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
  157. else
  158. Modes.push_back(M);
  159. }
  160. // Then we want to sort and unique the modes we've collected.
  161. llvm::sort(Modes);
  162. Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
  163. }
  164. }
  165. void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
  166. ArgStringList &CmdArgs, types::ID InputType) const {
  167. if (!XRayInstrument)
  168. return;
  169. CmdArgs.push_back(XRayInstrumentOption);
  170. if (XRayAlwaysEmitCustomEvents)
  171. CmdArgs.push_back("-fxray-always-emit-customevents");
  172. if (XRayAlwaysEmitTypedEvents)
  173. CmdArgs.push_back("-fxray-always-emit-typedevents");
  174. CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
  175. Twine(InstructionThreshold)));
  176. for (const auto &Always : AlwaysInstrumentFiles) {
  177. SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
  178. AlwaysInstrumentOpt += Always;
  179. CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
  180. }
  181. for (const auto &Never : NeverInstrumentFiles) {
  182. SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
  183. NeverInstrumentOpt += Never;
  184. CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
  185. }
  186. for (const auto &AttrFile : AttrListFiles) {
  187. SmallString<64> AttrListFileOpt("-fxray-attr-list=");
  188. AttrListFileOpt += AttrFile;
  189. CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
  190. }
  191. for (const auto &Dep : ExtraDeps) {
  192. SmallString<64> ExtraDepOpt("-fdepfile-entry=");
  193. ExtraDepOpt += Dep;
  194. CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
  195. }
  196. for (const auto &Mode : Modes) {
  197. SmallString<64> ModeOpt("-fxray-modes=");
  198. ModeOpt += Mode;
  199. CmdArgs.push_back(Args.MakeArgString(ModeOpt));
  200. }
  201. SmallString<64> Bundle("-fxray-instrumentation-bundle=");
  202. if (InstrumentationBundle.full()) {
  203. Bundle += "all";
  204. } else if (InstrumentationBundle.empty()) {
  205. Bundle += "none";
  206. } else {
  207. if (InstrumentationBundle.has(XRayInstrKind::Function))
  208. Bundle += "function";
  209. if (InstrumentationBundle.has(XRayInstrKind::Custom))
  210. Bundle += "custom";
  211. if (InstrumentationBundle.has(XRayInstrKind::Typed))
  212. Bundle += "typed";
  213. }
  214. CmdArgs.push_back(Args.MakeArgString(Bundle));
  215. }