]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-xray/xray-extract.cc
Merge llvm, clang, lld and lldb trunk r300890, and update build glue.
[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 "func-id-helper.h"
20 #include "xray-registry.h"
21 #include "llvm/Object/ELF.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/DataExtractor.h"
25 #include "llvm/Support/ELF.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"
31
32 using namespace llvm;
33 using namespace llvm::xray;
34 using namespace llvm::yaml;
35
36 // llvm-xray extract
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,
41                                          cl::sub(Extract));
42 static cl::opt<std::string>
43     ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
44                   cl::desc("output file; use '-' for stdout"),
45                   cl::sub(Extract));
46 static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
47                                 cl::desc("Alias for -output"),
48                                 cl::sub(Extract));
49 static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
50                                       cl::init(false),
51                                       cl::desc("symbolize functions"),
52                                       cl::sub(Extract));
53 static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
54                                    cl::desc("alias for -symbolize"),
55                                    cl::sub(Extract));
56
57 namespace {
58
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);
67     if (!FuncId)
68       return;
69     YAMLSleds.push_back({*FuncId, Sled.Address, Sled.Function, Sled.Kind,
70                          Sled.AlwaysInstrument,
71                          ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : ""});
72   }
73   Output Out(OS, nullptr, 0);
74   Out << YAMLSleds;
75 }
76
77 } // namespace
78
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 '") +
84                               ExtractInput + "'.",
85                           std::make_error_code(std::errc::invalid_argument)),
86                       InstrumentationMapOrError.takeError());
87
88   std::error_code EC;
89   raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::F_Text);
90   if (EC)
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,
99                                                   FunctionAddresses);
100   exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
101   return Error::success();
102 });