blob: 7800b88d9eeb6c1956cce0d4bc63054380429ca2 [file] [log] [blame]
Dean Michael Berrisa0e3ae42018-05-02 00:43:17 +00001//===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +00006//
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
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000015
Dean Michael Berris918802b2017-04-18 23:23:54 +000016#include "func-id-helper.h"
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000017#include "xray-registry.h"
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000018#include "llvm/Object/ObjectFile.h"
19#include "llvm/Support/CommandLine.h"
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000020#include "llvm/Support/Error.h"
21#include "llvm/Support/FileSystem.h"
22#include "llvm/Support/Format.h"
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000023#include "llvm/Support/raw_ostream.h"
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000024#include "llvm/XRay/InstrumentationMap.h"
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000025
26using namespace llvm;
27using namespace llvm::xray;
28using namespace llvm::yaml;
29
30// llvm-xray extract
31// ----------------------------------------------------------------------------
32static cl::SubCommand Extract("extract", "Extract instrumentation maps");
33static cl::opt<std::string> ExtractInput(cl::Positional,
34 cl::desc("<input file>"), cl::Required,
35 cl::sub(Extract));
36static 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));
40static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
41 cl::desc("Alias for -output"),
42 cl::sub(Extract));
Dean Michael Berris918802b2017-04-18 23:23:54 +000043static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
44 cl::init(false),
45 cl::desc("symbolize functions"),
46 cl::sub(Extract));
47static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
48 cl::desc("alias for -symbolize"),
49 cl::sub(Extract));
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000050
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000051namespace {
52
Dean Michael Berris918802b2017-04-18 23:23:54 +000053void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
54 FuncIdConversionHelper &FH) {
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000055 // First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
56 std::vector<YAMLXRaySledEntry> YAMLSleds;
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000057 auto Sleds = Map.sleds();
58 YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000059 for (const auto &Sled : Sleds) {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000060 auto FuncId = Map.getFunctionId(Sled.Function);
61 if (!FuncId)
62 return;
63 YAMLSleds.push_back({*FuncId, Sled.Address, Sled.Function, Sled.Kind,
Dean Michael Berris918802b2017-04-18 23:23:54 +000064 Sled.AlwaysInstrument,
65 ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : ""});
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000066 }
Dimitry Andric9afed032017-02-14 22:49:49 +000067 Output Out(OS, nullptr, 0);
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000068 Out << YAMLSleds;
69}
70
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000071} // namespace
72
Mehdi Amini41af4302016-11-11 04:28:40 +000073static CommandRegistration Unused(&Extract, []() -> Error {
Dean Michael Berris0e8abab2017-02-01 00:05:29 +000074 auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
75 if (!InstrumentationMapOrError)
76 return joinErrors(make_error<StringError>(
77 Twine("Cannot extract instrumentation map from '") +
78 ExtractInput + "'.",
79 std::make_error_code(std::errc::invalid_argument)),
80 InstrumentationMapOrError.takeError());
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000081
82 std::error_code EC;
Fangrui Songd9b948b2019-08-05 05:43:48 +000083 raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_Text);
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000084 if (EC)
85 return make_error<StringError>(
86 Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
Dean Michael Berris918802b2017-04-18 23:23:54 +000087 const auto &FunctionAddresses =
88 InstrumentationMapOrError->getFunctionAddresses();
Peter Collingbournea2048f82019-06-11 02:31:54 +000089 symbolize::LLVMSymbolizer Symbolizer;
Dean Michael Berris918802b2017-04-18 23:23:54 +000090 llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
91 FunctionAddresses);
92 exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
Dean Michael Berrisc92bfb52016-10-26 04:14:34 +000093 return Error::success();
94});