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