Path: blob/main/contrib/llvm-project/llvm/tools/llvm-xray/xray-extract.cpp
35231 views
//===- xray-extract.cpp: XRay Instrumentation Map Extraction --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Implementation of the xray-extract.h interface.9//10// FIXME: Support other XRay-instrumented binary formats other than ELF.11//12//===----------------------------------------------------------------------===//131415#include "func-id-helper.h"16#include "xray-registry.h"17#include "llvm/Object/ObjectFile.h"18#include "llvm/Support/CommandLine.h"19#include "llvm/Support/Error.h"20#include "llvm/Support/FileSystem.h"21#include "llvm/Support/Format.h"22#include "llvm/Support/raw_ostream.h"23#include "llvm/XRay/InstrumentationMap.h"2425using namespace llvm;26using namespace llvm::xray;27using namespace llvm::yaml;2829// llvm-xray extract30// ----------------------------------------------------------------------------31static cl::SubCommand Extract("extract", "Extract instrumentation maps");32static cl::opt<std::string> ExtractInput(cl::Positional,33cl::desc("<input file>"), cl::Required,34cl::sub(Extract));35static cl::opt<std::string>36ExtractOutput("output", cl::value_desc("output file"), cl::init("-"),37cl::desc("output file; use '-' for stdout"),38cl::sub(Extract));39static cl::alias ExtractOutput2("o", cl::aliasopt(ExtractOutput),40cl::desc("Alias for -output"));41static cl::opt<bool> ExtractSymbolize("symbolize", cl::value_desc("symbolize"),42cl::init(false),43cl::desc("symbolize functions"),44cl::sub(Extract));45static cl::alias ExtractSymbolize2("s", cl::aliasopt(ExtractSymbolize),46cl::desc("alias for -symbolize"));47static cl::opt<bool> Demangle("demangle",48cl::desc("demangle symbols (default)"),49cl::sub(Extract));50static cl::opt<bool> NoDemangle("no-demangle",51cl::desc("don't demangle symbols"),52cl::sub(Extract));5354namespace {5556void exportAsYAML(const InstrumentationMap &Map, raw_ostream &OS,57FuncIdConversionHelper &FH) {58// First we translate the sleds into the YAMLXRaySledEntry objects in a deque.59std::vector<YAMLXRaySledEntry> YAMLSleds;60auto Sleds = Map.sleds();61YAMLSleds.reserve(std::distance(Sleds.begin(), Sleds.end()));62for (const auto &Sled : Sleds) {63auto FuncId = Map.getFunctionId(Sled.Function);64if (!FuncId)65return;66YAMLSleds.push_back(67{*FuncId, Sled.Address, Sled.Function, Sled.Kind, Sled.AlwaysInstrument,68ExtractSymbolize ? FH.SymbolOrNumber(*FuncId) : "", Sled.Version});69}70Output Out(OS, nullptr, 0);71Out << YAMLSleds;72}7374} // namespace7576static CommandRegistration Unused(&Extract, []() -> Error {77auto InstrumentationMapOrError = loadInstrumentationMap(ExtractInput);78if (!InstrumentationMapOrError)79return joinErrors(make_error<StringError>(80Twine("Cannot extract instrumentation map from '") +81ExtractInput + "'.",82std::make_error_code(std::errc::invalid_argument)),83InstrumentationMapOrError.takeError());8485std::error_code EC;86raw_fd_ostream OS(ExtractOutput, EC, sys::fs::OpenFlags::OF_TextWithCRLF);87if (EC)88return make_error<StringError>(89Twine("Cannot open file '") + ExtractOutput + "' for writing.", EC);90const auto &FunctionAddresses =91InstrumentationMapOrError->getFunctionAddresses();92symbolize::LLVMSymbolizer::Options opts;93if (Demangle.getPosition() < NoDemangle.getPosition())94opts.Demangle = false;95symbolize::LLVMSymbolizer Symbolizer(opts);96llvm::xray::FuncIdConversionHelper FuncIdHelper(ExtractInput, Symbolizer,97FunctionAddresses);98exportAsYAML(*InstrumentationMapOrError, OS, FuncIdHelper);99return Error::success();100});101102103