xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-xray/xray-extract.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Implementation of the xray-extract.h interface.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // FIXME: Support other XRay-instrumented binary formats other than ELF.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "func-id-helper.h"
170b57cec5SDimitry Andric #include "xray-registry.h"
180b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h"
190b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
200b57cec5SDimitry Andric #include "llvm/Support/Error.h"
210b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
220b57cec5SDimitry Andric #include "llvm/Support/Format.h"
230b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
240b57cec5SDimitry Andric #include "llvm/XRay/InstrumentationMap.h"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric using namespace llvm;
270b57cec5SDimitry Andric using namespace llvm::xray;
280b57cec5SDimitry Andric using namespace llvm::yaml;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric // llvm-xray extract
310b57cec5SDimitry Andric // ----------------------------------------------------------------------------
320b57cec5SDimitry Andric static cl::SubCommand Extract("extract", "Extract instrumentation maps");
330b57cec5SDimitry Andric static cl::opt<std::string> ExtractInput(cl::Positional,
340b57cec5SDimitry Andric                                          cl::desc("<input file>"), cl::Required,
350b57cec5SDimitry Andric                                          cl::sub(Extract));
360b57cec5SDimitry Andric static cl::opt<std::string>
370b57cec5SDimitry Andric     ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
380b57cec5SDimitry Andric                   cl::desc("output file; use '-' for stdout"),
390b57cec5SDimitry Andric                   cl::sub(Extract));
400b57cec5SDimitry Andric static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
41480093f4SDimitry Andric                                 cl::desc("Alias for -output"));
420b57cec5SDimitry Andric static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
430b57cec5SDimitry Andric                                       cl::init(false),
440b57cec5SDimitry Andric                                       cl::desc("symbolize functions"),
450b57cec5SDimitry Andric                                       cl::sub(Extract));
460b57cec5SDimitry Andric static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
47480093f4SDimitry Andric                                    cl::desc("alias for -symbolize"));
48*349cc55cSDimitry Andric static cl::opt<bool> Demangle("demangle",
49*349cc55cSDimitry Andric                               cl::desc("demangle symbols (default)"),
50*349cc55cSDimitry Andric                               cl::sub(Extract));
51*349cc55cSDimitry Andric static cl::opt<bool> NoDemangle("no-demangle",
525ffd83dbSDimitry Andric                                 cl::desc("don't demangle symbols"),
535ffd83dbSDimitry Andric                                 cl::sub(Extract));
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric namespace {
560b57cec5SDimitry Andric 
exportAsYAML(const InstrumentationMap & Map,raw_ostream & OS,FuncIdConversionHelper & FH)570b57cec5SDimitry Andric void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
580b57cec5SDimitry Andric                   FuncIdConversionHelper &FH) {
590b57cec5SDimitry Andric   // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
600b57cec5SDimitry Andric   std::vector<YAMLXRaySledEntry> YAMLSleds;
610b57cec5SDimitry Andric   auto Sleds = Map.sleds();
620b57cec5SDimitry Andric   YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
630b57cec5SDimitry Andric   for (const auto &Sled : Sleds) {
640b57cec5SDimitry Andric     auto FuncId = Map.getFunctionId(Sled.Function);
650b57cec5SDimitry Andric     if (!FuncId)
660b57cec5SDimitry Andric       return;
675ffd83dbSDimitry Andric     YAMLSleds.push_back(
685ffd83dbSDimitry Andric         {*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,
695ffd83dbSDimitry Andric          ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric   Output Out(OS, nullptr, 0);
720b57cec5SDimitry Andric   Out << YAMLSleds;
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric } // namespace
760b57cec5SDimitry Andric 
__anon577b53910202() 770b57cec5SDimitry Andric static CommandRegistration Unused(&Extract, []() -> Error {
780b57cec5SDimitry Andric   auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
790b57cec5SDimitry Andric   if (!InstrumentationMapOrError)
800b57cec5SDimitry Andric     return joinErrors(make_error<StringError>(
810b57cec5SDimitry Andric                           Twine("Cannot extract instrumentation map from '") +
820b57cec5SDimitry Andric                               ExtractInput + "'.",
830b57cec5SDimitry Andric                           std::make_error_code(std::errc::invalid_argument)),
840b57cec5SDimitry Andric                       InstrumentationMapOrError.takeError());
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   std::error_code EC;
87fe6060f1SDimitry Andric   raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
880b57cec5SDimitry Andric   if (EC)
890b57cec5SDimitry Andric     return make_error<StringError>(
900b57cec5SDimitry Andric         Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
910b57cec5SDimitry Andric   const auto &FunctionAddresses =
920b57cec5SDimitry Andric       InstrumentationMapOrError->getFunctionAddresses();
935ffd83dbSDimitry Andric   symbolize::LLVMSymbolizer::Options opts;
94*349cc55cSDimitry Andric   if (Demangle.getPosition() < NoDemangle.getPosition())
955ffd83dbSDimitry Andric     opts.Demangle = false;
965ffd83dbSDimitry Andric   symbolize::LLVMSymbolizer Symbolizer(opts);
970b57cec5SDimitry Andric   llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
980b57cec5SDimitry Andric                                                   FunctionAddresses);
990b57cec5SDimitry Andric   exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
1000b57cec5SDimitry Andric   return Error::success();
1010b57cec5SDimitry Andric });
102