2018-05-02 02:43:17 +02:00
|
|
|
//===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//
|
2016-10-26 06:14:34 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-10-26 06:14:34 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of the xray-extract.h interface.
|
|
|
|
//
|
|
|
|
// FIXME: Support other XRay-instrumented binary formats other than ELF.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2017-04-19 01:23:54 +02:00
|
|
|
#include "func-id-helper.h"
|
2016-10-26 06:14:34 +02:00
|
|
|
#include "xray-registry.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-01 01:05:29 +01:00
|
|
|
#include "llvm/XRay/InstrumentationMap.h"
|
2016-10-26 06:14:34 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::xray;
|
|
|
|
using namespace llvm::yaml;
|
|
|
|
|
|
|
|
// llvm-xray extract
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
static cl::SubCommand Extract("extract", "Extract instrumentation maps");
|
|
|
|
static cl::opt<std::string> ExtractInput(cl::Positional,
|
|
|
|
cl::desc("<input file>"), cl::Required,
|
|
|
|
cl::sub(Extract));
|
|
|
|
static cl::opt<std::string>
|
|
|
|
ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),
|
|
|
|
cl::desc("output file; use '-' for stdout"),
|
|
|
|
cl::sub(Extract));
|
|
|
|
static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),
|
2020-01-10 07:02:57 +01:00
|
|
|
cl::desc("Alias for -output"));
|
2017-04-19 01:23:54 +02:00
|
|
|
static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("symbolize functions"),
|
|
|
|
cl::sub(Extract));
|
|
|
|
static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),
|
2020-01-10 07:02:57 +01:00
|
|
|
cl::desc("alias for -symbolize"));
|
2020-01-17 01:28:15 +01:00
|
|
|
static cl::opt<bool> ExtractNoDemangle("no-demangle",
|
|
|
|
cl::value_desc("no-demangle"),
|
|
|
|
cl::init(false),
|
|
|
|
cl::desc("don't demangle symbols"),
|
|
|
|
cl::sub(Extract));
|
2016-10-26 06:14:34 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2017-04-19 01:23:54 +02:00
|
|
|
void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,
|
|
|
|
FuncIdConversionHelper &FH) {
|
2016-10-26 06:14:34 +02:00
|
|
|
// First we translate the sleds into the YAMLXRaySledEntry objects in a deque.
|
|
|
|
std::vector<YAMLXRaySledEntry> YAMLSleds;
|
2017-02-01 01:05:29 +01:00
|
|
|
auto Sleds = Map.sleds();
|
|
|
|
YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));
|
2016-10-26 06:14:34 +02:00
|
|
|
for (const auto &Sled : Sleds) {
|
2017-02-01 01:05:29 +01:00
|
|
|
auto FuncId = Map.getFunctionId(Sled.Function);
|
|
|
|
if (!FuncId)
|
|
|
|
return;
|
2020-04-24 22:39:02 +02:00
|
|
|
YAMLSleds.push_back(
|
|
|
|
{*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,
|
|
|
|
ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});
|
2016-10-26 06:14:34 +02:00
|
|
|
}
|
2017-02-14 23:49:49 +01:00
|
|
|
Output Out(OS, nullptr, 0);
|
2016-10-26 06:14:34 +02:00
|
|
|
Out << YAMLSleds;
|
|
|
|
}
|
|
|
|
|
2017-02-01 01:05:29 +01:00
|
|
|
} // namespace
|
|
|
|
|
2016-11-11 05:28:40 +01:00
|
|
|
static CommandRegistration Unused(&Extract, []() -> Error {
|
2017-02-01 01:05:29 +01:00
|
|
|
auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);
|
|
|
|
if (!InstrumentationMapOrError)
|
|
|
|
return joinErrors(make_error<StringError>(
|
|
|
|
Twine("Cannot extract instrumentation map from '") +
|
|
|
|
ExtractInput + "'.",
|
|
|
|
std::make_error_code(std::errc::invalid_argument)),
|
|
|
|
InstrumentationMapOrError.takeError());
|
2016-10-26 06:14:34 +02:00
|
|
|
|
|
|
|
std::error_code EC;
|
2021-04-06 13:22:41 +02:00
|
|
|
raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);
|
2016-10-26 06:14:34 +02:00
|
|
|
if (EC)
|
|
|
|
return make_error<StringError>(
|
|
|
|
Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);
|
2017-04-19 01:23:54 +02:00
|
|
|
const auto &FunctionAddresses =
|
|
|
|
InstrumentationMapOrError->getFunctionAddresses();
|
2020-01-17 01:28:15 +01:00
|
|
|
symbolize::LLVMSymbolizer::Options opts;
|
|
|
|
if (ExtractNoDemangle)
|
|
|
|
opts.Demangle = false;
|
|
|
|
symbolize::LLVMSymbolizer Symbolizer(opts);
|
2017-04-19 01:23:54 +02:00
|
|
|
llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,
|
|
|
|
FunctionAddresses);
|
|
|
|
exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);
|
2016-10-26 06:14:34 +02:00
|
|
|
return Error::success();
|
|
|
|
});
|