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 "xray-registry.h"
20 #include "llvm/Object/ELF.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/DataExtractor.h"
24 #include "llvm/Support/ELF.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/XRay/InstrumentationMap.h"
32 using namespace llvm::xray;
33 using namespace llvm::yaml;
36 // ----------------------------------------------------------------------------
37 static cl::SubCommand Extract("extract", "Extract instrumentation maps");
38 static cl::opt<std::string> ExtractInput(cl::Positional,
39 cl::desc("<input file>"), cl::Required,
41 static cl::opt<std::string>
42 ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
43 cl::desc("output file; use '-' for stdout"),
45 static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
46 cl::desc("Alias for -output"),
51 void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS) {
52 // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
53 std::vector<YAMLXRaySledEntry> YAMLSleds;
54 auto Sleds = Map.sleds();
55 YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
56 for (const auto &Sled : Sleds) {
57 auto FuncId = Map.getFunctionId(Sled.Function);
60 YAMLSleds.push_back({*FuncId, Sled.Address, Sled.Function, Sled.Kind,
61 Sled.AlwaysInstrument});
63 Output Out(OS, nullptr, 0);
69 static CommandRegistration Unused(&Extract, []() -> Error {
70 auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
71 if (!InstrumentationMapOrError)
72 return joinErrors(make_error<StringError>(
73 Twine("Cannot extract instrumentation map from '") +
75 std::make_error_code(std::errc::invalid_argument)),
76 InstrumentationMapOrError.takeError());
79 raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::F_Text);
81 return make_error<StringError>(
82 Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
83 exportAsYAML(*InstrumentationMapOrError, OS);
84 return Error::success();