XRayArgs.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. 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_fno_xray_instrument, false))
  34. return;
  35. if (Triple.getOS() == llvm::Triple::Linux) {
  36. switch (Triple.getArch()) {
  37. case llvm::Triple::x86_64:
  38. case llvm::Triple::arm:
  39. case llvm::Triple::aarch64:
  40. case llvm::Triple::hexagon:
  41. case llvm::Triple::ppc64le:
  42. case llvm::Triple::mips:
  43. case llvm::Triple::mipsel:
  44. case llvm::Triple::mips64:
  45. case llvm::Triple::mips64el:
  46. break;
  47. default:
  48. D.Diag(diag::err_drv_clang_unsupported)
  49. << (std::string(XRayInstrumentOption) + " on " + Triple.str());
  50. }
  51. } else if (Triple.isOSFreeBSD() || Triple.isOSOpenBSD() ||
  52. Triple.isOSNetBSD() || 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. // Both XRay and -fpatchable-function-entry use
  71. // TargetOpcode::PATCHABLE_FUNCTION_ENTER.
  72. if (Arg *A = Args.getLastArg(options::OPT_fpatchable_function_entry_EQ))
  73. D.Diag(diag::err_drv_argument_not_allowed_with)
  74. << "-fxray-instrument" << A->getSpelling();
  75. XRayInstrument = true;
  76. if (const Arg *A =
  77. Args.getLastArg(options::OPT_fxray_instruction_threshold_,
  78. options::OPT_fxray_instruction_threshold_EQ)) {
  79. StringRef S = A->getValue();
  80. if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
  81. D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
  82. }
  83. // By default, the back-end will not emit the lowering for XRay customevent
  84. // calls if the function is not instrumented. In the future we will change
  85. // this default to be the reverse, but in the meantime we're going to
  86. // introduce the new functionality behind a flag.
  87. if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
  88. options::OPT_fno_xray_always_emit_customevents, false))
  89. XRayAlwaysEmitCustomEvents = true;
  90. if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
  91. options::OPT_fno_xray_always_emit_typedevents, false))
  92. XRayAlwaysEmitTypedEvents = true;
  93. if (!Args.hasFlag(options::OPT_fxray_link_deps,
  94. options::OPT_fnoxray_link_deps, true))
  95. XRayRT = false;
  96. if (Args.hasFlag(options::OPT_fxray_ignore_loops,
  97. options::OPT_fno_xray_ignore_loops, false))
  98. XRayIgnoreLoops = true;
  99. XRayFunctionIndex = Args.hasFlag(options::OPT_fxray_function_index,
  100. options::OPT_fno_xray_function_index, true);
  101. auto Bundles =
  102. Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
  103. if (Bundles.empty())
  104. InstrumentationBundle.Mask = XRayInstrKind::All;
  105. else
  106. for (const auto &B : Bundles) {
  107. llvm::SmallVector<StringRef, 2> BundleParts;
  108. llvm::SplitString(B, BundleParts, ",");
  109. for (const auto &P : BundleParts) {
  110. // TODO: Automate the generation of the string case table.
  111. auto Valid = llvm::StringSwitch<bool>(P)
  112. .Cases("none", "all", "function", "function-entry",
  113. "function-exit", "custom", true)
  114. .Default(false);
  115. if (!Valid) {
  116. D.Diag(clang::diag::err_drv_invalid_value)
  117. << "-fxray-instrumentation-bundle=" << P;
  118. continue;
  119. }
  120. auto Mask = parseXRayInstrValue(P);
  121. if (Mask == XRayInstrKind::None) {
  122. InstrumentationBundle.clear();
  123. break;
  124. }
  125. InstrumentationBundle.Mask |= Mask;
  126. }
  127. }
  128. // Validate the always/never attribute files. We also make sure that they
  129. // are treated as actual dependencies.
  130. for (const auto &Filename :
  131. Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
  132. if (D.getVFS().exists(Filename)) {
  133. AlwaysInstrumentFiles.push_back(Filename);
  134. ExtraDeps.push_back(Filename);
  135. } else
  136. D.Diag(clang::diag::err_drv_no_such_file) << Filename;
  137. }
  138. for (const auto &Filename :
  139. Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
  140. if (D.getVFS().exists(Filename)) {
  141. NeverInstrumentFiles.push_back(Filename);
  142. ExtraDeps.push_back(Filename);
  143. } else
  144. D.Diag(clang::diag::err_drv_no_such_file) << Filename;
  145. }
  146. for (const auto &Filename :
  147. Args.getAllArgValues(options::OPT_fxray_attr_list)) {
  148. if (D.getVFS().exists(Filename)) {
  149. AttrListFiles.push_back(Filename);
  150. ExtraDeps.push_back(Filename);
  151. } else
  152. D.Diag(clang::diag::err_drv_no_such_file) << Filename;
  153. }
  154. // Get the list of modes we want to support.
  155. auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
  156. if (SpecifiedModes.empty())
  157. llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
  158. else
  159. for (const auto &Arg : SpecifiedModes) {
  160. // Parse CSV values for -fxray-modes=...
  161. llvm::SmallVector<StringRef, 2> ModeParts;
  162. llvm::SplitString(Arg, ModeParts, ",");
  163. for (const auto &M : ModeParts)
  164. if (M == "none")
  165. Modes.clear();
  166. else if (M == "all")
  167. llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
  168. else
  169. Modes.push_back(std::string(M));
  170. }
  171. if (const Arg *A = Args.getLastArg(options::OPT_fxray_function_groups)) {
  172. StringRef S = A->getValue();
  173. if (S.getAsInteger(0, XRayFunctionGroups) || XRayFunctionGroups < 1)
  174. D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
  175. }
  176. if (const Arg *A =
  177. Args.getLastArg(options::OPT_fxray_selected_function_group)) {
  178. StringRef S = A->getValue();
  179. if (S.getAsInteger(0, XRaySelectedFunctionGroup) ||
  180. XRaySelectedFunctionGroup < 0 ||
  181. XRaySelectedFunctionGroup >= XRayFunctionGroups)
  182. D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
  183. }
  184. // Then we want to sort and unique the modes we've collected.
  185. llvm::sort(Modes);
  186. Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
  187. }
  188. void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
  189. ArgStringList &CmdArgs, types::ID InputType) const {
  190. if (!XRayInstrument)
  191. return;
  192. CmdArgs.push_back(XRayInstrumentOption);
  193. if (XRayAlwaysEmitCustomEvents)
  194. CmdArgs.push_back("-fxray-always-emit-customevents");
  195. if (XRayAlwaysEmitTypedEvents)
  196. CmdArgs.push_back("-fxray-always-emit-typedevents");
  197. if (XRayIgnoreLoops)
  198. CmdArgs.push_back("-fxray-ignore-loops");
  199. if (!XRayFunctionIndex)
  200. CmdArgs.push_back("-fno-xray-function-index");
  201. if (XRayFunctionGroups > 1) {
  202. CmdArgs.push_back(Args.MakeArgString(Twine("-fxray-function-groups=") +
  203. Twine(XRayFunctionGroups)));
  204. }
  205. if (XRaySelectedFunctionGroup != 0) {
  206. CmdArgs.push_back(
  207. Args.MakeArgString(Twine("-fxray-selected-function-group=") +
  208. Twine(XRaySelectedFunctionGroup)));
  209. }
  210. CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
  211. Twine(InstructionThreshold)));
  212. for (const auto &Always : AlwaysInstrumentFiles) {
  213. SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
  214. AlwaysInstrumentOpt += Always;
  215. CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
  216. }
  217. for (const auto &Never : NeverInstrumentFiles) {
  218. SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
  219. NeverInstrumentOpt += Never;
  220. CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
  221. }
  222. for (const auto &AttrFile : AttrListFiles) {
  223. SmallString<64> AttrListFileOpt("-fxray-attr-list=");
  224. AttrListFileOpt += AttrFile;
  225. CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
  226. }
  227. for (const auto &Dep : ExtraDeps) {
  228. SmallString<64> ExtraDepOpt("-fdepfile-entry=");
  229. ExtraDepOpt += Dep;
  230. CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
  231. }
  232. for (const auto &Mode : Modes) {
  233. SmallString<64> ModeOpt("-fxray-modes=");
  234. ModeOpt += Mode;
  235. CmdArgs.push_back(Args.MakeArgString(ModeOpt));
  236. }
  237. SmallString<64> Bundle("-fxray-instrumentation-bundle=");
  238. if (InstrumentationBundle.full()) {
  239. Bundle += "all";
  240. } else if (InstrumentationBundle.empty()) {
  241. Bundle += "none";
  242. } else {
  243. if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry) &&
  244. InstrumentationBundle.has(XRayInstrKind::FunctionExit))
  245. Bundle += "function";
  246. else if (InstrumentationBundle.has(XRayInstrKind::FunctionEntry))
  247. Bundle += "function-entry";
  248. else if (InstrumentationBundle.has(XRayInstrKind::FunctionExit))
  249. Bundle += "function-exit";
  250. if (InstrumentationBundle.has(XRayInstrKind::Custom))
  251. Bundle += "custom";
  252. if (InstrumentationBundle.has(XRayInstrKind::Typed))
  253. Bundle += "typed";
  254. }
  255. CmdArgs.push_back(Args.MakeArgString(Bundle));
  256. }