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