xray-extract.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
  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. //
  9. // Implementation of the xray-extract.h interface.
  10. //
  11. // FIXME: Support other XRay-instrumented binary formats other than ELF.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "func-id-helper.h"
  15. #include "xray-registry.h"
  16. #include "llvm/Object/ObjectFile.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/Error.h"
  19. #include "llvm/Support/FileSystem.h"
  20. #include "llvm/Support/Format.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #include "llvm/XRay/InstrumentationMap.h"
  23. using namespace llvm;
  24. using namespace llvm::xray;
  25. using namespace llvm::yaml;
  26. // llvm-xray extract
  27. // ----------------------------------------------------------------------------
  28. static cl::SubCommand Extract("extract", "Extract instrumentation maps");
  29. static cl::opt<std::string> ExtractInput(cl::Positional,
  30. cl::desc("<input file>"), cl::Required,
  31. cl::sub(Extract));
  32. static cl::opt<std::string>
  33. ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
  34. cl::desc("output file; use '-' for stdout"),
  35. cl::sub(Extract));
  36. static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
  37. cl::desc("Alias for -output"));
  38. static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
  39. cl::init(false),
  40. cl::desc("symbolize functions"),
  41. cl::sub(Extract));
  42. static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
  43. cl::desc("alias for -symbolize"));
  44. static cl::opt<bool> Demangle("demangle",
  45. cl::desc("demangle symbols (default)"),
  46. cl::sub(Extract));
  47. static cl::opt<bool> NoDemangle("no-demangle",
  48. cl::desc("don't demangle symbols"),
  49. cl::sub(Extract));
  50. namespace {
  51. void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
  52. FuncIdConversionHelper &FH) {
  53. // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
  54. std::vector<YAMLXRaySledEntry> YAMLSleds;
  55. auto Sleds = Map.sleds();
  56. YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
  57. for (const auto &Sled : Sleds) {
  58. auto FuncId = Map.getFunctionId(Sled.Function);
  59. if (!FuncId)
  60. return;
  61. YAMLSleds.push_back(
  62. {*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,
  63. ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});
  64. }
  65. Output Out(OS, nullptr, 0);
  66. Out << YAMLSleds;
  67. }
  68. } // namespace
  69. static CommandRegistration Unused(&Extract, []() -> Error {
  70. auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
  71. if (!InstrumentationMapOrError)
  72. return joinErrors(make_error<StringError>(
  73. Twine("Cannot extract instrumentation map from '") +
  74. ExtractInput + "'.",
  75. std::make_error_code(std::errc::invalid_argument)),
  76. InstrumentationMapOrError.takeError());
  77. std::error_code EC;
  78. raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
  79. if (EC)
  80. return make_error<StringError>(
  81. Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
  82. const auto &FunctionAddresses =
  83. InstrumentationMapOrError->getFunctionAddresses();
  84. symbolize::LLVMSymbolizer::Options opts;
  85. if (Demangle.getPosition() < NoDemangle.getPosition())
  86. opts.Demangle = false;
  87. symbolize::LLVMSymbolizer Symbolizer(opts);
  88. llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
  89. FunctionAddresses);
  90. exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
  91. return Error::success();
  92. });