xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-xray/xray-extract.cpp (revision e25152834cdf3b353892835a4f3b157e066a8ed4)
1 //===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of the xray-extract.h interface.
10 //
11 // FIXME: Support other XRay-instrumented binary formats other than ELF.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 
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"
25 
26 using namespace llvm;
27 using namespace llvm::xray;
28 using namespace llvm::yaml;
29 
30 // llvm-xray extract
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,
35                                          cl::sub(Extract));
36 static cl::opt<std::string>
37     ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
38                   cl::desc("output file; use '-' for stdout"),
39                   cl::sub(Extract));
40 static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
41                                 cl::desc("Alias for -output"));
42 static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
43                                       cl::init(false),
44                                       cl::desc("symbolize functions"),
45                                       cl::sub(Extract));
46 static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
47                                    cl::desc("alias for -symbolize"));
48 static cl::opt<bool> ExtractNoDemangle("no-demangle",
49                                        cl::value_desc("no-demangle"),
50                                        cl::init(false),
51                                        cl::desc("don't demangle symbols"),
52                                        cl::sub(Extract));
53 
54 namespace {
55 
56 void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
57                   FuncIdConversionHelper &FH) {
58   // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
59   std::vector<YAMLXRaySledEntry> YAMLSleds;
60   auto Sleds = Map.sleds();
61   YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
62   for (const auto &Sled : Sleds) {
63     auto FuncId = Map.getFunctionId(Sled.Function);
64     if (!FuncId)
65       return;
66     YAMLSleds.push_back(
67         {*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,
68          ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});
69   }
70   Output Out(OS, nullptr, 0);
71   Out << YAMLSleds;
72 }
73 
74 } // namespace
75 
76 static CommandRegistration Unused(&Extract, []() -> Error {
77   auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
78   if (!InstrumentationMapOrError)
79     return joinErrors(make_error<StringError>(
80                           Twine("Cannot extract instrumentation map from '") +
81                               ExtractInput + "'.",
82                           std::make_error_code(std::errc::invalid_argument)),
83                       InstrumentationMapOrError.takeError());
84 
85   std::error_code EC;
86   raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_Text);
87   if (EC)
88     return make_error<StringError>(
89         Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
90   const auto &FunctionAddresses =
91       InstrumentationMapOrError->getFunctionAddresses();
92   symbolize::LLVMSymbolizer::Options opts;
93   if (ExtractNoDemangle)
94     opts.Demangle = false;
95   symbolize::LLVMSymbolizer Symbolizer(opts);
96   llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
97                                                   FunctionAddresses);
98   exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
99   return Error::success();
100 });
101