2017-02-01 08:05:29 +08:00
|
|
|
//===- InstrumentationMap.cpp - XRay Instrumentation Map ------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2017-02-01 08:05:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Implementation of the InstrumentationMap type for XRay sleds.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/XRay/InstrumentationMap.h"
|
2018-12-14 09:37:56 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/Object/Binary.h"
|
2018-12-14 09:37:56 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2017-02-01 08:05:29 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2019-11-08 13:20:34 +08:00
|
|
|
#include "llvm/Object/RelocationResolver.h"
|
2017-02-01 08:05:29 +08:00
|
|
|
#include "llvm/Support/DataExtractor.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
2017-02-01 08:05:29 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-02-09 09:09:54 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2017-02-01 08:05:29 +08:00
|
|
|
#include <system_error>
|
2017-02-09 09:09:54 +08:00
|
|
|
#include <vector>
|
2017-02-01 08:05:29 +08:00
|
|
|
|
2017-02-09 09:09:54 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace xray;
|
2017-02-01 08:05:29 +08:00
|
|
|
|
|
|
|
Optional<int32_t> InstrumentationMap::getFunctionId(uint64_t Addr) const {
|
|
|
|
auto I = FunctionIds.find(Addr);
|
|
|
|
if (I != FunctionIds.end())
|
|
|
|
return I->second;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<uint64_t> InstrumentationMap::getFunctionAddr(int32_t FuncId) const {
|
|
|
|
auto I = FunctionAddresses.find(FuncId);
|
|
|
|
if (I != FunctionAddresses.end())
|
|
|
|
return I->second;
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-12-14 09:37:56 +08:00
|
|
|
using RelocMap = DenseMap<uint64_t, uint64_t>;
|
|
|
|
|
2017-02-09 09:09:54 +08:00
|
|
|
static Error
|
2018-09-10 13:00:43 +08:00
|
|
|
loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
|
2017-02-09 09:09:54 +08:00
|
|
|
InstrumentationMap::SledContainer &Sleds,
|
|
|
|
InstrumentationMap::FunctionAddressMap &FunctionAddresses,
|
|
|
|
InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
|
2017-02-01 08:05:29 +08:00
|
|
|
InstrumentationMap Map;
|
|
|
|
|
|
|
|
// Find the section named "xray_instr_map".
|
2018-09-10 13:00:43 +08:00
|
|
|
if ((!ObjFile.getBinary()->isELF() && !ObjFile.getBinary()->isMachO()) ||
|
[XRay] Implement powerpc64le xray.
Summary:
powerpc64 big-endian is not supported, but I believe that most logic can
be shared, except for xray_powerpc64.cc.
Also add a function InvalidateInstructionCache to xray_util.h, which is
copied from llvm/Support/Memory.cpp. I'm not sure if I need to add a unittest,
and I don't know how.
Reviewers: dberris, echristo, iteratee, kbarton, hfinkel
Subscribers: mehdi_amini, nemanjai, mgorny, llvm-commits
Differential Revision: https://reviews.llvm.org/D29742
llvm-svn: 294781
2017-02-11 05:03:24 +08:00
|
|
|
!(ObjFile.getBinary()->getArch() == Triple::x86_64 ||
|
2019-11-08 13:20:34 +08:00
|
|
|
ObjFile.getBinary()->getArch() == Triple::ppc64le ||
|
|
|
|
ObjFile.getBinary()->getArch() == Triple::aarch64))
|
2017-02-01 08:05:29 +08:00
|
|
|
return make_error<StringError>(
|
2018-09-10 13:00:43 +08:00
|
|
|
"File format not supported (only does ELF and Mach-O little endian 64-bit).",
|
2017-02-01 08:05:29 +08:00
|
|
|
std::make_error_code(std::errc::not_supported));
|
|
|
|
|
|
|
|
StringRef Contents = "";
|
|
|
|
const auto &Sections = ObjFile.getBinary()->sections();
|
2017-02-09 09:09:54 +08:00
|
|
|
auto I = llvm::find_if(Sections, [&](object::SectionRef Section) {
|
2019-08-14 19:10:11 +08:00
|
|
|
Expected<StringRef> NameOrErr = Section.getName();
|
|
|
|
if (NameOrErr)
|
|
|
|
return *NameOrErr == "xray_instr_map";
|
|
|
|
consumeError(NameOrErr.takeError());
|
|
|
|
return false;
|
2017-02-01 08:05:29 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (I == Sections.end())
|
|
|
|
return make_error<StringError>(
|
|
|
|
"Failed to find XRay instrumentation map.",
|
|
|
|
std::make_error_code(std::errc::executable_format_error));
|
|
|
|
|
2019-05-16 21:24:04 +08:00
|
|
|
if (Expected<StringRef> E = I->getContents())
|
|
|
|
Contents = *E;
|
|
|
|
else
|
|
|
|
return E.takeError();
|
2017-02-01 08:05:29 +08:00
|
|
|
|
2018-12-14 09:37:56 +08:00
|
|
|
RelocMap Relocs;
|
|
|
|
if (ObjFile.getBinary()->isELF()) {
|
2018-12-14 15:46:58 +08:00
|
|
|
uint32_t RelativeRelocation = [](object::ObjectFile *ObjFile) {
|
2018-12-14 09:37:56 +08:00
|
|
|
if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(ObjFile))
|
2018-12-14 15:46:58 +08:00
|
|
|
return ELFObj->getELFFile()->getRelativeRelocationType();
|
2018-12-14 09:37:56 +08:00
|
|
|
else if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(ObjFile))
|
2018-12-14 15:46:58 +08:00
|
|
|
return ELFObj->getELFFile()->getRelativeRelocationType();
|
2018-12-14 09:37:56 +08:00
|
|
|
else if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(ObjFile))
|
2018-12-14 15:46:58 +08:00
|
|
|
return ELFObj->getELFFile()->getRelativeRelocationType();
|
2018-12-14 09:37:56 +08:00
|
|
|
else if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(ObjFile))
|
2018-12-14 15:46:58 +08:00
|
|
|
return ELFObj->getELFFile()->getRelativeRelocationType();
|
2018-12-14 09:37:56 +08:00
|
|
|
else
|
|
|
|
return static_cast<uint32_t>(0);
|
|
|
|
}(ObjFile.getBinary());
|
|
|
|
|
2019-11-08 13:20:34 +08:00
|
|
|
bool (*SupportsRelocation)(uint64_t);
|
|
|
|
object::RelocationResolver Resolver;
|
|
|
|
std::tie(SupportsRelocation, Resolver) =
|
|
|
|
object::getRelocationResolver(*ObjFile.getBinary());
|
|
|
|
|
2018-12-14 09:37:56 +08:00
|
|
|
for (const object::SectionRef &Section : Sections) {
|
|
|
|
for (const object::RelocationRef &Reloc : Section.relocations()) {
|
2019-11-08 13:20:34 +08:00
|
|
|
if (SupportsRelocation && SupportsRelocation(Reloc.getType())) {
|
|
|
|
auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend();
|
|
|
|
auto A = AddendOrErr ? *AddendOrErr : 0;
|
|
|
|
uint64_t resolved = Resolver(Reloc, Reloc.getSymbol()->getValue(), A);
|
|
|
|
Relocs.insert({Reloc.getOffset(), resolved});
|
|
|
|
} else if (Reloc.getType() == RelativeRelocation) {
|
|
|
|
if (auto AddendOrErr = object::ELFRelocationRef(Reloc).getAddend())
|
|
|
|
Relocs.insert({Reloc.getOffset(), *AddendOrErr});
|
|
|
|
}
|
2018-12-14 09:37:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-01 08:05:29 +08:00
|
|
|
// Copy the instrumentation map data into the Sleds data structure.
|
|
|
|
auto C = Contents.bytes_begin();
|
|
|
|
static constexpr size_t ELF64SledEntrySize = 32;
|
|
|
|
|
|
|
|
if ((C - Contents.bytes_end()) % ELF64SledEntrySize != 0)
|
|
|
|
return make_error<StringError>(
|
|
|
|
Twine("Instrumentation map entries not evenly divisible by size of "
|
|
|
|
"an XRay sled entry in ELF64."),
|
|
|
|
std::make_error_code(std::errc::executable_format_error));
|
|
|
|
|
2019-08-06 18:49:40 +08:00
|
|
|
auto RelocateOrElse = [&](uint64_t Offset, uint64_t Address) {
|
2018-12-14 09:37:56 +08:00
|
|
|
if (!Address) {
|
|
|
|
uint64_t A = I->getAddress() + C - Contents.bytes_begin() + Offset;
|
|
|
|
RelocMap::const_iterator R = Relocs.find(A);
|
|
|
|
if (R != Relocs.end())
|
|
|
|
return R->second;
|
|
|
|
}
|
|
|
|
return Address;
|
|
|
|
};
|
|
|
|
|
2017-02-01 08:05:29 +08:00
|
|
|
int32_t FuncId = 1;
|
|
|
|
uint64_t CurFn = 0;
|
|
|
|
for (; C != Contents.bytes_end(); C += ELF64SledEntrySize) {
|
|
|
|
DataExtractor Extractor(
|
|
|
|
StringRef(reinterpret_cast<const char *>(C), ELF64SledEntrySize), true,
|
|
|
|
8);
|
|
|
|
Sleds.push_back({});
|
|
|
|
auto &Entry = Sleds.back();
|
2019-08-06 18:49:40 +08:00
|
|
|
uint64_t OffsetPtr = 0;
|
|
|
|
uint64_t AddrOff = OffsetPtr;
|
2018-12-14 13:56:20 +08:00
|
|
|
Entry.Address = RelocateOrElse(AddrOff, Extractor.getU64(&OffsetPtr));
|
2019-08-06 18:49:40 +08:00
|
|
|
uint64_t FuncOff = OffsetPtr;
|
2018-12-14 13:56:20 +08:00
|
|
|
Entry.Function = RelocateOrElse(FuncOff, Extractor.getU64(&OffsetPtr));
|
2017-02-01 08:05:29 +08:00
|
|
|
auto Kind = Extractor.getU8(&OffsetPtr);
|
|
|
|
static constexpr SledEntry::FunctionKinds Kinds[] = {
|
|
|
|
SledEntry::FunctionKinds::ENTRY, SledEntry::FunctionKinds::EXIT,
|
|
|
|
SledEntry::FunctionKinds::TAIL,
|
2017-08-21 08:14:06 +08:00
|
|
|
SledEntry::FunctionKinds::LOG_ARGS_ENTER,
|
|
|
|
SledEntry::FunctionKinds::CUSTOM_EVENT};
|
2017-02-01 08:05:29 +08:00
|
|
|
if (Kind >= sizeof(Kinds))
|
|
|
|
return errorCodeToError(
|
|
|
|
std::make_error_code(std::errc::executable_format_error));
|
|
|
|
Entry.Kind = Kinds[Kind];
|
|
|
|
Entry.AlwaysInstrument = Extractor.getU8(&OffsetPtr) != 0;
|
|
|
|
|
|
|
|
// We do replicate the function id generation scheme implemented in the
|
|
|
|
// XRay runtime.
|
|
|
|
// FIXME: Figure out how to keep this consistent with the XRay runtime.
|
|
|
|
if (CurFn == 0) {
|
|
|
|
CurFn = Entry.Function;
|
|
|
|
FunctionAddresses[FuncId] = Entry.Function;
|
|
|
|
FunctionIds[Entry.Function] = FuncId;
|
|
|
|
}
|
|
|
|
if (Entry.Function != CurFn) {
|
|
|
|
++FuncId;
|
|
|
|
CurFn = Entry.Function;
|
|
|
|
FunctionAddresses[FuncId] = Entry.Function;
|
|
|
|
FunctionIds[Entry.Function] = FuncId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-09 09:09:54 +08:00
|
|
|
static Error
|
2019-07-12 04:29:32 +08:00
|
|
|
loadYAML(sys::fs::file_t Fd, size_t FileSize, StringRef Filename,
|
2017-02-09 09:09:54 +08:00
|
|
|
InstrumentationMap::SledContainer &Sleds,
|
|
|
|
InstrumentationMap::FunctionAddressMap &FunctionAddresses,
|
|
|
|
InstrumentationMap::FunctionAddressReverseMap &FunctionIds) {
|
2017-02-01 08:05:29 +08:00
|
|
|
std::error_code EC;
|
|
|
|
sys::fs::mapped_file_region MappedFile(
|
2019-07-12 04:29:32 +08:00
|
|
|
Fd, sys::fs::mapped_file_region::mapmode::readonly, FileSize, 0, EC);
|
|
|
|
sys::fs::closeFile(Fd);
|
2017-02-01 08:05:29 +08:00
|
|
|
if (EC)
|
|
|
|
return make_error<StringError>(
|
|
|
|
Twine("Failed memory-mapping file '") + Filename + "'.", EC);
|
|
|
|
|
|
|
|
std::vector<YAMLXRaySledEntry> YAMLSleds;
|
|
|
|
yaml::Input In(StringRef(MappedFile.data(), MappedFile.size()));
|
|
|
|
In >> YAMLSleds;
|
|
|
|
if (In.error())
|
|
|
|
return make_error<StringError>(
|
|
|
|
Twine("Failed loading YAML document from '") + Filename + "'.",
|
|
|
|
In.error());
|
|
|
|
|
|
|
|
Sleds.reserve(YAMLSleds.size());
|
|
|
|
for (const auto &Y : YAMLSleds) {
|
|
|
|
FunctionAddresses[Y.FuncId] = Y.Function;
|
|
|
|
FunctionIds[Y.Function] = Y.FuncId;
|
|
|
|
Sleds.push_back(
|
|
|
|
SledEntry{Y.Address, Y.Function, Y.Kind, Y.AlwaysInstrument});
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Create error types that encapsulate a bit more information than what
|
|
|
|
// StringError instances contain.
|
2017-02-09 09:09:54 +08:00
|
|
|
Expected<InstrumentationMap>
|
|
|
|
llvm::xray::loadInstrumentationMap(StringRef Filename) {
|
2017-02-01 08:05:29 +08:00
|
|
|
// At this point we assume the file is an object file -- and if that doesn't
|
|
|
|
// work, we treat it as YAML.
|
|
|
|
// FIXME: Extend to support non-ELF and non-x86_64 binaries.
|
|
|
|
|
|
|
|
InstrumentationMap Map;
|
|
|
|
auto ObjectFileOrError = object::ObjectFile::createObjectFile(Filename);
|
|
|
|
if (!ObjectFileOrError) {
|
|
|
|
auto E = ObjectFileOrError.takeError();
|
|
|
|
// We try to load it as YAML if the ELF load didn't work.
|
2019-07-12 04:29:32 +08:00
|
|
|
Expected<sys::fs::file_t> FdOrErr = sys::fs::openNativeFileForRead(Filename);
|
|
|
|
if (!FdOrErr) {
|
|
|
|
// Report the ELF load error if YAML failed.
|
|
|
|
consumeError(FdOrErr.takeError());
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2019-07-12 04:29:32 +08:00
|
|
|
}
|
2017-02-01 08:05:29 +08:00
|
|
|
|
|
|
|
uint64_t FileSize;
|
|
|
|
if (sys::fs::file_size(Filename, FileSize))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2017-02-01 08:05:29 +08:00
|
|
|
|
|
|
|
// If the file is empty, we return the original error.
|
|
|
|
if (FileSize == 0)
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2017-02-01 08:05:29 +08:00
|
|
|
|
|
|
|
// From this point on the errors will be only for the YAML parts, so we
|
|
|
|
// consume the errors at this point.
|
|
|
|
consumeError(std::move(E));
|
2019-07-12 04:29:32 +08:00
|
|
|
if (auto E = loadYAML(*FdOrErr, FileSize, Filename, Map.Sleds,
|
2017-02-01 08:05:29 +08:00
|
|
|
Map.FunctionAddresses, Map.FunctionIds))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2018-09-10 13:00:43 +08:00
|
|
|
} else if (auto E = loadObj(Filename, *ObjectFileOrError, Map.Sleds,
|
2017-02-01 08:05:29 +08:00
|
|
|
Map.FunctionAddresses, Map.FunctionIds)) {
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2017-02-01 08:05:29 +08:00
|
|
|
}
|
|
|
|
return Map;
|
|
|
|
}
|