1 //===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implementation of the xray-extract.h interface.
11 // FIXME: Support other XRay-instrumented binary formats other than ELF.
13 //===----------------------------------------------------------------------===//
16 #include "func-id-helper.h"
17 #include "xray-registry.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include "llvm/XRay/InstrumentationMap.h"
27 using namespace llvm::xray
;
28 using namespace llvm::yaml
;
31 // ----------------------------------------------------------------------------
32 static cl::SubCommand
Extract("extract", "Extract instrumentation maps");
33 static cl::opt
<std::string
> ExtractInput(cl::Positional
,
34 cl::desc("<input file>"), cl::Required
,
36 static cl::opt
<std::string
>
37 ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
38 cl::desc("output file; use '-' for stdout"),
40 static cl::alias
ExtractOutput2("o", cl::aliasopt(ExtractOutput
),
41 cl::desc("Alias for -output"),
43 static cl::opt
<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
45 cl::desc("symbolize functions"),
47 static cl::alias
ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize
),
48 cl::desc("alias for -symbolize"),
53 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(std::distance(Sleds
.begin(), Sleds
.end()));
59 for (const auto &Sled
: Sleds
) {
60 auto FuncId
= Map
.getFunctionId(Sled
.Function
);
63 YAMLSleds
.push_back({*FuncId
, Sled
.Address
, Sled
.Function
, Sled
.Kind
,
64 Sled
.AlwaysInstrument
,
65 ExtractSymbolize
? FH
.SymbolOrNumber(*FuncId
) : ""});
67 Output
Out(OS
, nullptr, 0);
73 static CommandRegistration
Unused(&Extract
, []() -> Error
{
74 auto InstrumentationMapOrError
= loadInstrumentationMap(ExtractInput
);
75 if (!InstrumentationMapOrError
)
76 return joinErrors(make_error
<StringError
>(
77 Twine("Cannot extract instrumentation map from '") +
79 std::make_error_code(std::errc::invalid_argument
)),
80 InstrumentationMapOrError
.takeError());
83 raw_fd_ostream
OS(ExtractOutput
, EC
, sys::fs::OpenFlags::OF_Text
);
85 return make_error
<StringError
>(
86 Twine("Cannot open file '") + ExtractOutput
+ "' for writing.", EC
);
87 const auto &FunctionAddresses
=
88 InstrumentationMapOrError
->getFunctionAddresses();
89 symbolize::LLVMSymbolizer Symbolizer
;
90 llvm::xray::FuncIdConversionHelper
FuncIdHelper(ExtractInput
, Symbolizer
,
92 exportAsYAML(*InstrumentationMapOrError
, OS
, FuncIdHelper
);
93 return Error::success();