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