xref: /llvm-project/llvm/tools/llvm-xray/xray-extract.cpp (revision 9b96b0865df158a73767c3cfdd0380f86d5dde21)
1a0e3ae4cSDean Michael Berris //===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
2a0e3ae4cSDean Michael Berris //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a0e3ae4cSDean Michael Berris //
7a0e3ae4cSDean Michael Berris //===----------------------------------------------------------------------===//
8a0e3ae4cSDean Michael Berris //
9a0e3ae4cSDean Michael Berris // Implementation of the xray-extract.h interface.
10a0e3ae4cSDean Michael Berris //
11a0e3ae4cSDean Michael Berris // FIXME: Support other XRay-instrumented binary formats other than ELF.
12a0e3ae4cSDean Michael Berris //
13a0e3ae4cSDean Michael Berris //===----------------------------------------------------------------------===//
14a0e3ae4cSDean Michael Berris 
15a0e3ae4cSDean Michael Berris 
16a0e3ae4cSDean Michael Berris #include "func-id-helper.h"
17a0e3ae4cSDean Michael Berris #include "xray-registry.h"
18a0e3ae4cSDean Michael Berris #include "llvm/Object/ObjectFile.h"
19a0e3ae4cSDean Michael Berris #include "llvm/Support/CommandLine.h"
20a0e3ae4cSDean Michael Berris #include "llvm/Support/Error.h"
21a0e3ae4cSDean Michael Berris #include "llvm/Support/FileSystem.h"
22a0e3ae4cSDean Michael Berris #include "llvm/Support/Format.h"
23a0e3ae4cSDean Michael Berris #include "llvm/Support/raw_ostream.h"
24a0e3ae4cSDean Michael Berris #include "llvm/XRay/InstrumentationMap.h"
25a0e3ae4cSDean Michael Berris 
26a0e3ae4cSDean Michael Berris using namespace llvm;
27a0e3ae4cSDean Michael Berris using namespace llvm::xray;
28a0e3ae4cSDean Michael Berris using namespace llvm::yaml;
29a0e3ae4cSDean Michael Berris 
30a0e3ae4cSDean Michael Berris // llvm-xray extract
31a0e3ae4cSDean Michael Berris // ----------------------------------------------------------------------------
32a0e3ae4cSDean Michael Berris static cl::SubCommand Extract("extract", "Extract instrumentation maps");
33a0e3ae4cSDean Michael Berris static cl::opt<std::string> ExtractInput(cl::Positional,
34a0e3ae4cSDean Michael Berris                                          cl::desc("<input file>"), cl::Required,
35a0e3ae4cSDean Michael Berris                                          cl::sub(Extract));
36a0e3ae4cSDean Michael Berris static cl::opt<std::string>
37a0e3ae4cSDean Michael Berris     ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
38a0e3ae4cSDean Michael Berris                   cl::desc("output file; use '-' for stdout"),
39a0e3ae4cSDean Michael Berris                   cl::sub(Extract));
40a0e3ae4cSDean Michael Berris static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
41995c18fcSShoaib Meenai                                 cl::desc("Alias for -output"));
42a0e3ae4cSDean Michael Berris static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
43a0e3ae4cSDean Michael Berris                                       cl::init(false),
44a0e3ae4cSDean Michael Berris                                       cl::desc("symbolize functions"),
45a0e3ae4cSDean Michael Berris                                       cl::sub(Extract));
46a0e3ae4cSDean Michael Berris static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
47995c18fcSShoaib Meenai                                    cl::desc("alias for -symbolize"));
48*9b96b086SFangrui Song static cl::opt<bool> Demangle("demangle",
49*9b96b086SFangrui Song                               cl::desc("demangle symbols (default)"),
50*9b96b086SFangrui Song                               cl::sub(Extract));
51*9b96b086SFangrui Song static cl::opt<bool> NoDemangle("no-demangle",
528ef57f3eSMax Sherman                                 cl::desc("don't demangle symbols"),
538ef57f3eSMax Sherman                                 cl::sub(Extract));
54a0e3ae4cSDean Michael Berris 
55a0e3ae4cSDean Michael Berris namespace {
56a0e3ae4cSDean Michael Berris 
exportAsYAML(const InstrumentationMap & Map,raw_ostream & OS,FuncIdConversionHelper & FH)57a0e3ae4cSDean Michael Berris void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
58a0e3ae4cSDean Michael Berris                   FuncIdConversionHelper &FH) {
59a0e3ae4cSDean Michael Berris   // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
60a0e3ae4cSDean Michael Berris   std::vector<YAMLXRaySledEntry> YAMLSleds;
61a0e3ae4cSDean Michael Berris   auto Sleds = Map.sleds();
62a0e3ae4cSDean Michael Berris   YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
63a0e3ae4cSDean Michael Berris   for (const auto &Sled : Sleds) {
64a0e3ae4cSDean Michael Berris     auto FuncId = Map.getFunctionId(Sled.Function);
65a0e3ae4cSDean Michael Berris     if (!FuncId)
66a0e3ae4cSDean Michael Berris       return;
6710bc1258SFangrui Song     YAMLSleds.push_back(
6810bc1258SFangrui Song         {*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,
6910bc1258SFangrui Song          ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});
70a0e3ae4cSDean Michael Berris   }
71a0e3ae4cSDean Michael Berris   Output Out(OS, nullptr, 0);
72a0e3ae4cSDean Michael Berris   Out << YAMLSleds;
73a0e3ae4cSDean Michael Berris }
74a0e3ae4cSDean Michael Berris 
75a0e3ae4cSDean Michael Berris } // namespace
76a0e3ae4cSDean Michael Berris 
__anond7ec54c40202() 77a0e3ae4cSDean Michael Berris static CommandRegistration Unused(&Extract, []() -> Error {
78a0e3ae4cSDean Michael Berris   auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
79a0e3ae4cSDean Michael Berris   if (!InstrumentationMapOrError)
80a0e3ae4cSDean Michael Berris     return joinErrors(make_error<StringError>(
81a0e3ae4cSDean Michael Berris                           Twine("Cannot extract instrumentation map from '") +
82a0e3ae4cSDean Michael Berris                               ExtractInput + "'.",
83a0e3ae4cSDean Michael Berris                           std::make_error_code(std::errc::invalid_argument)),
84a0e3ae4cSDean Michael Berris                       InstrumentationMapOrError.takeError());
85a0e3ae4cSDean Michael Berris 
86a0e3ae4cSDean Michael Berris   std::error_code EC;
8782b3e28eSAbhina Sreeskantharajan   raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
88a0e3ae4cSDean Michael Berris   if (EC)
89a0e3ae4cSDean Michael Berris     return make_error<StringError>(
90a0e3ae4cSDean Michael Berris         Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
91a0e3ae4cSDean Michael Berris   const auto &FunctionAddresses =
92a0e3ae4cSDean Michael Berris       InstrumentationMapOrError->getFunctionAddresses();
938ef57f3eSMax Sherman   symbolize::LLVMSymbolizer::Options opts;
94*9b96b086SFangrui Song   if (Demangle.getPosition() < NoDemangle.getPosition())
958ef57f3eSMax Sherman     opts.Demangle = false;
968ef57f3eSMax Sherman   symbolize::LLVMSymbolizer Symbolizer(opts);
97a0e3ae4cSDean Michael Berris   llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
98a0e3ae4cSDean Michael Berris                                                   FunctionAddresses);
99a0e3ae4cSDean Michael Berris   exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
100a0e3ae4cSDean Michael Berris   return Error::success();
101a0e3ae4cSDean Michael Berris });
102