xray-extract.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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> ExtractNoDemangle("no-demangle",
  45. cl::value_desc("no-demangle"),
  46. cl::init(false),
  47. cl::desc("don't demangle symbols"),
  48. cl::sub(Extract));
  49. namespace {
  50. void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
  51. FuncIdConversionHelper &FH) {
  52. // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
  53. std::vector<YAMLXRaySledEntry> YAMLSleds;
  54. auto Sleds = Map.sleds();
  55. YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
  56. for (const auto &Sled : Sleds) {
  57. auto FuncId = Map.getFunctionId(Sled.Function);
  58. if (!FuncId)
  59. return;
  60. YAMLSleds.push_back(
  61. {*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,
  62. ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});
  63. }
  64. Output Out(OS, nullptr, 0);
  65. Out << YAMLSleds;
  66. }
  67. } // namespace
  68. static CommandRegistration Unused(&Extract, []() -> Error {
  69. auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
  70. if (!InstrumentationMapOrError)
  71. return joinErrors(make_error<StringError>(
  72. Twine("Cannot extract instrumentation map from '") +
  73. ExtractInput + "'.",
  74. std::make_error_code(std::errc::invalid_argument)),
  75. InstrumentationMapOrError.takeError());
  76. std::error_code EC;
  77. raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_Text);
  78. if (EC)
  79. return make_error<StringError>(
  80. Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
  81. const auto &FunctionAddresses =
  82. InstrumentationMapOrError->getFunctionAddresses();
  83. symbolize::LLVMSymbolizer::Options opts;
  84. if (ExtractNoDemangle)
  85. opts.Demangle = false;
  86. symbolize::LLVMSymbolizer Symbolizer(opts);
  87. llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
  88. FunctionAddresses);
  89. exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
  90. return Error::success();
  91. });