]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-xray/xray-extract.cc
Merge ^/head r316992 through r317215.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-xray / xray-extract.cc
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 #include <type_traits>
17 #include <utility>
18
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"
30
31 using namespace llvm;
32 using namespace llvm::xray;
33 using namespace llvm::yaml;
34
35 // llvm-xray extract
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,
40                                          cl::sub(Extract));
41 static cl::opt<std::string>
42     ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
43                   cl::desc("output file; use '-' for stdout"),
44                   cl::sub(Extract));
45 static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
46                                 cl::desc("Alias for -output"),
47                                 cl::sub(Extract));
48
49 namespace {
50
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);
58     if (!FuncId)
59       return;
60     YAMLSleds.push_back({*FuncId, Sled.Address, Sled.Function, Sled.Kind,
61                          Sled.AlwaysInstrument});
62   }
63   Output Out(OS, nullptr, 0);
64   Out << YAMLSleds;
65 }
66
67 } // namespace
68
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 '") +
74                               ExtractInput + "'.",
75                           std::make_error_code(std::errc::invalid_argument)),
76                       InstrumentationMapOrError.takeError());
77
78   std::error_code EC;
79   raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::F_Text);
80   if (EC)
81     return make_error<StringError>(
82         Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
83   exportAsYAML(*InstrumentationMapOrError, OS);
84   return Error::success();
85 });