2014-12-13 01:31:24 +08:00
|
|
|
//===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
|
|
|
|
//
|
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
|
2014-12-13 01:31:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-11-02 05:16:06 +08:00
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "DebugMap.h"
|
2015-06-06 05:12:07 +08:00
|
|
|
#include "BinaryHolder.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Support/Chrono.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2018-04-15 05:36:42 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2017-11-02 05:16:06 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2014-12-13 01:31:24 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <algorithm>
|
2017-11-02 05:16:06 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2014-12-13 01:31:24 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
2017-11-02 05:16:06 +08:00
|
|
|
|
2014-12-13 01:31:24 +08:00
|
|
|
namespace dsymutil {
|
|
|
|
|
|
|
|
using namespace llvm::object;
|
|
|
|
|
2015-07-23 07:24:00 +08:00
|
|
|
DebugMapObject::DebugMapObject(StringRef ObjectFilename,
|
2017-10-06 22:49:20 +08:00
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp,
|
|
|
|
uint8_t Type)
|
|
|
|
: Filename(ObjectFilename), Timestamp(Timestamp), Type(Type) {}
|
2014-12-13 01:31:24 +08:00
|
|
|
|
2016-01-31 12:29:22 +08:00
|
|
|
bool DebugMapObject::addSymbol(StringRef Name, Optional<uint64_t> ObjectAddress,
|
2015-03-15 09:29:30 +08:00
|
|
|
uint64_t LinkedAddress, uint32_t Size) {
|
2014-12-13 01:31:24 +08:00
|
|
|
auto InsertResult = Symbols.insert(
|
2015-03-15 09:29:30 +08:00
|
|
|
std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
|
2015-02-14 07:18:16 +08:00
|
|
|
|
2016-01-31 12:29:22 +08:00
|
|
|
if (ObjectAddress && InsertResult.second)
|
|
|
|
AddressToMapping[*ObjectAddress] = &*InsertResult.first;
|
2014-12-13 01:31:24 +08:00
|
|
|
return InsertResult.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DebugMapObject::print(raw_ostream &OS) const {
|
|
|
|
OS << getObjectFilename() << ":\n";
|
|
|
|
// Sort the symbols in alphabetical order, like llvm-nm (and to get
|
|
|
|
// deterministic output for testing).
|
2017-11-02 05:16:06 +08:00
|
|
|
using Entry = std::pair<StringRef, SymbolMapping>;
|
2014-12-13 01:31:24 +08:00
|
|
|
std::vector<Entry> Entries;
|
|
|
|
Entries.reserve(Symbols.getNumItems());
|
|
|
|
for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
|
|
|
|
Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
|
2018-10-01 06:31:29 +08:00
|
|
|
llvm::sort(Entries, [](const Entry &LHS, const Entry &RHS) {
|
|
|
|
return LHS.first < RHS.first;
|
|
|
|
});
|
2014-12-13 01:31:24 +08:00
|
|
|
for (const auto &Sym : Entries) {
|
2016-01-31 12:29:22 +08:00
|
|
|
if (Sym.second.ObjectAddress)
|
|
|
|
OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
|
|
|
|
else
|
|
|
|
OS << "\t????????????????";
|
|
|
|
OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
|
2015-06-06 04:27:07 +08:00
|
|
|
uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
|
|
|
|
Sym.first.data());
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void DebugMapObject::dump() const { print(errs()); }
|
|
|
|
#endif
|
|
|
|
|
2016-11-09 19:43:52 +08:00
|
|
|
DebugMapObject &
|
|
|
|
DebugMap::addDebugMapObject(StringRef ObjectFilePath,
|
2017-10-06 22:49:20 +08:00
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp,
|
|
|
|
uint8_t Type) {
|
|
|
|
Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp, Type));
|
2014-12-13 01:31:24 +08:00
|
|
|
return *Objects.back();
|
|
|
|
}
|
|
|
|
|
2015-02-14 07:18:16 +08:00
|
|
|
const DebugMapObject::DebugMapEntry *
|
2014-12-13 01:31:24 +08:00
|
|
|
DebugMapObject::lookupSymbol(StringRef SymbolName) const {
|
|
|
|
StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
|
|
|
|
if (Sym == Symbols.end())
|
|
|
|
return nullptr;
|
2015-02-14 07:18:16 +08:00
|
|
|
return &*Sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DebugMapObject::DebugMapEntry *
|
|
|
|
DebugMapObject::lookupObjectAddress(uint64_t Address) const {
|
|
|
|
auto Mapping = AddressToMapping.find(Address);
|
|
|
|
if (Mapping == AddressToMapping.end())
|
|
|
|
return nullptr;
|
|
|
|
return Mapping->getSecond();
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DebugMap::print(raw_ostream &OS) const {
|
2015-06-02 05:12:45 +08:00
|
|
|
yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
|
2015-06-06 04:27:07 +08:00
|
|
|
yout << const_cast<DebugMap &>(*this);
|
2014-12-13 01:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void DebugMap::dump() const { print(errs()); }
|
|
|
|
#endif
|
2015-06-06 04:27:04 +08:00
|
|
|
|
2015-06-06 05:12:07 +08:00
|
|
|
namespace {
|
2017-11-02 05:16:06 +08:00
|
|
|
|
2015-06-06 05:12:07 +08:00
|
|
|
struct YAMLContext {
|
|
|
|
StringRef PrependPath;
|
2015-06-06 05:21:57 +08:00
|
|
|
Triple BinaryTriple;
|
2015-06-06 05:12:07 +08:00
|
|
|
};
|
2017-11-02 05:16:06 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2015-06-06 05:12:07 +08:00
|
|
|
|
2015-08-06 02:27:44 +08:00
|
|
|
ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
|
2015-06-06 04:27:04 +08:00
|
|
|
DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
|
|
|
|
bool Verbose) {
|
|
|
|
auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
|
|
|
|
if (auto Err = ErrOrFile.getError())
|
|
|
|
return Err;
|
|
|
|
|
2015-06-06 05:12:07 +08:00
|
|
|
YAMLContext Ctxt;
|
|
|
|
|
|
|
|
Ctxt.PrependPath = PrependPath;
|
|
|
|
|
2015-06-06 04:27:04 +08:00
|
|
|
std::unique_ptr<DebugMap> Res;
|
2015-06-06 05:12:07 +08:00
|
|
|
yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
|
2015-06-06 04:27:04 +08:00
|
|
|
yin >> Res;
|
|
|
|
|
|
|
|
if (auto EC = yin.error())
|
|
|
|
return EC;
|
2015-08-06 02:27:44 +08:00
|
|
|
std::vector<std::unique_ptr<DebugMap>> Result;
|
|
|
|
Result.push_back(std::move(Res));
|
|
|
|
return std::move(Result);
|
2015-06-06 04:27:04 +08:00
|
|
|
}
|
2017-11-02 05:16:06 +08:00
|
|
|
|
|
|
|
} // end namespace dsymutil
|
2015-06-06 04:27:04 +08:00
|
|
|
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
// Normalize/Denormalize between YAML and a DebugMapObject.
|
|
|
|
struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
|
2015-07-23 07:24:00 +08:00
|
|
|
YamlDMO(IO &io) { Timestamp = 0; }
|
2015-06-06 04:27:04 +08:00
|
|
|
YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
|
|
|
|
dsymutil::DebugMapObject denormalize(IO &IO);
|
|
|
|
|
|
|
|
std::string Filename;
|
2016-11-09 19:43:52 +08:00
|
|
|
int64_t Timestamp;
|
2015-06-06 04:27:04 +08:00
|
|
|
std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
|
|
|
|
mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
|
|
|
|
io.mapRequired("sym", s.first);
|
2016-01-31 12:29:22 +08:00
|
|
|
io.mapOptional("objAddr", s.second.ObjectAddress);
|
2015-06-06 04:27:04 +08:00
|
|
|
io.mapRequired("binAddr", s.second.BinaryAddress);
|
|
|
|
io.mapOptional("size", s.second.Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingTraits<dsymutil::DebugMapObject>::mapping(
|
|
|
|
IO &io, dsymutil::DebugMapObject &DMO) {
|
|
|
|
MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
|
|
|
|
io.mapRequired("filename", Norm->Filename);
|
2015-07-23 07:24:00 +08:00
|
|
|
io.mapOptional("timestamp", Norm->Timestamp);
|
2015-06-06 04:27:04 +08:00
|
|
|
io.mapRequired("symbols", Norm->Entries);
|
|
|
|
}
|
|
|
|
|
2017-11-02 05:16:06 +08:00
|
|
|
void ScalarTraits<Triple>::output(const Triple &val, void *, raw_ostream &out) {
|
2015-06-06 04:27:04 +08:00
|
|
|
out << val.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
|
|
|
|
value = Triple(scalar);
|
|
|
|
return StringRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
|
|
|
|
IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
|
|
|
|
return seq.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
dsymutil::DebugMapObject &
|
|
|
|
SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
|
|
|
|
IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
|
|
|
|
size_t index) {
|
|
|
|
if (index >= seq.size()) {
|
|
|
|
seq.resize(index + 1);
|
|
|
|
seq[index].reset(new dsymutil::DebugMapObject);
|
|
|
|
}
|
|
|
|
return *seq[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
|
|
|
|
dsymutil::DebugMap &DM) {
|
|
|
|
io.mapRequired("triple", DM.BinaryTriple);
|
2015-08-26 13:09:59 +08:00
|
|
|
io.mapOptional("binary-path", DM.BinaryPath);
|
2015-06-06 05:12:07 +08:00
|
|
|
if (void *Ctxt = io.getContext())
|
2015-06-06 05:21:57 +08:00
|
|
|
reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
|
2015-07-24 14:41:11 +08:00
|
|
|
io.mapOptional("objects", DM.Objects);
|
2015-06-06 04:27:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
|
|
|
|
IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
|
|
|
|
if (!DM)
|
|
|
|
DM.reset(new DebugMap());
|
|
|
|
io.mapRequired("triple", DM->BinaryTriple);
|
2015-08-26 13:09:59 +08:00
|
|
|
io.mapOptional("binary-path", DM->BinaryPath);
|
2015-06-06 05:12:07 +08:00
|
|
|
if (void *Ctxt = io.getContext())
|
2015-06-06 05:21:57 +08:00
|
|
|
reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
|
2015-07-24 14:41:11 +08:00
|
|
|
io.mapOptional("objects", DM->Objects);
|
2015-06-06 04:27:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
|
|
|
|
IO &io, dsymutil::DebugMapObject &Obj) {
|
|
|
|
Filename = Obj.Filename;
|
2016-11-09 19:43:52 +08:00
|
|
|
Timestamp = sys::toTimeT(Obj.getTimestamp());
|
2015-06-06 04:27:04 +08:00
|
|
|
Entries.reserve(Obj.Symbols.size());
|
|
|
|
for (auto &Entry : Obj.Symbols)
|
|
|
|
Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
dsymutil::DebugMapObject
|
|
|
|
MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
|
2015-06-06 05:12:07 +08:00
|
|
|
BinaryHolder BinHolder(/* Verbose =*/false);
|
|
|
|
const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
|
|
|
|
SmallString<80> Path(Ctxt.PrependPath);
|
|
|
|
StringMap<uint64_t> SymbolAddresses;
|
|
|
|
|
2015-06-06 04:27:04 +08:00
|
|
|
sys::path::append(Path, Filename);
|
2018-06-30 00:51:52 +08:00
|
|
|
|
|
|
|
auto ObjectEntry = BinHolder.getObjectEntry(Path);
|
|
|
|
if (!ObjectEntry) {
|
|
|
|
auto Err = ObjectEntry.takeError();
|
|
|
|
WithColor::warning() << "Unable to open " << Path << " "
|
|
|
|
<< toString(std::move(Err)) << '\n';
|
|
|
|
} else {
|
|
|
|
auto Object = ObjectEntry->getObject(Ctxt.BinaryTriple);
|
|
|
|
if (!Object) {
|
|
|
|
auto Err = Object.takeError();
|
|
|
|
WithColor::warning() << "Unable to open " << Path << " "
|
|
|
|
<< toString(std::move(Err)) << '\n';
|
|
|
|
} else {
|
|
|
|
for (const auto &Sym : Object->symbols()) {
|
|
|
|
uint64_t Address = Sym.getValue();
|
|
|
|
Expected<StringRef> Name = Sym.getName();
|
|
|
|
if (!Name || (Sym.getFlags() &
|
|
|
|
(SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
|
|
|
|
// TODO: Actually report errors helpfully.
|
|
|
|
if (!Name)
|
|
|
|
consumeError(Name.takeError());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SymbolAddresses[*Name] = Address;
|
2016-04-21 05:24:34 +08:00
|
|
|
}
|
2015-06-06 05:12:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 22:49:20 +08:00
|
|
|
dsymutil::DebugMapObject Res(Path, sys::toTimePoint(Timestamp), MachO::N_OSO);
|
2015-06-06 04:27:04 +08:00
|
|
|
for (auto &Entry : Entries) {
|
|
|
|
auto &Mapping = Entry.second;
|
2016-01-31 12:29:22 +08:00
|
|
|
Optional<uint64_t> ObjAddress;
|
|
|
|
if (Mapping.ObjectAddress)
|
|
|
|
ObjAddress = *Mapping.ObjectAddress;
|
2015-06-06 05:12:07 +08:00
|
|
|
auto AddressIt = SymbolAddresses.find(Entry.first);
|
|
|
|
if (AddressIt != SymbolAddresses.end())
|
|
|
|
ObjAddress = AddressIt->getValue();
|
|
|
|
Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
|
2015-06-06 04:27:04 +08:00
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
2017-11-02 05:16:06 +08:00
|
|
|
|
|
|
|
} // end namespace yaml
|
|
|
|
} // end namespace llvm
|