1 //===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/Support/DataTypes.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm::object;
22 DebugMapObject::DebugMapObject(StringRef ObjectFilename)
23 : Filename(ObjectFilename) {}
25 bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress,
26 uint64_t LinkedAddress, uint32_t Size) {
27 auto InsertResult = Symbols.insert(
28 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
30 if (InsertResult.second)
31 AddressToMapping[ObjectAddress] = &*InsertResult.first;
32 return InsertResult.second;
35 void DebugMapObject::print(raw_ostream &OS) const {
36 OS << getObjectFilename() << ":\n";
37 // Sort the symbols in alphabetical order, like llvm-nm (and to get
38 // deterministic output for testing).
39 typedef std::pair<StringRef, SymbolMapping> Entry;
40 std::vector<Entry> Entries;
41 Entries.reserve(Symbols.getNumItems());
42 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
43 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
45 Entries.begin(), Entries.end(),
46 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
47 for (const auto &Sym : Entries) {
48 OS << format("\t%016" PRIx64 " => %016" PRIx64 "+0x%x\t%s\n",
49 uint64_t(Sym.second.ObjectAddress),
50 uint64_t(Sym.second.BinaryAddress),
51 uint32_t(Sym.second.Size), Sym.first.data());
57 void DebugMapObject::dump() const { print(errs()); }
60 DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) {
61 Objects.emplace_back(new DebugMapObject(ObjectFilePath));
62 return *Objects.back();
65 const DebugMapObject::DebugMapEntry *
66 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
67 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
68 if (Sym == Symbols.end())
73 const DebugMapObject::DebugMapEntry *
74 DebugMapObject::lookupObjectAddress(uint64_t Address) const {
75 auto Mapping = AddressToMapping.find(Address);
76 if (Mapping == AddressToMapping.end())
78 return Mapping->getSecond();
81 void DebugMap::print(raw_ostream &OS) const {
82 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
83 yout << const_cast<DebugMap&>(*this);
87 void DebugMap::dump() const { print(errs()); }
90 ErrorOr<std::unique_ptr<DebugMap>>
91 DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
93 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
94 if (auto Err = ErrOrFile.getError())
97 std::unique_ptr<DebugMap> Res;
98 yaml::Input yin((*ErrOrFile)->getBuffer(), &PrependPath);
101 if (auto EC = yin.error())
104 return std::move(Res);
110 // Normalize/Denormalize between YAML and a DebugMapObject.
111 struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
113 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
114 dsymutil::DebugMapObject denormalize(IO &IO);
116 std::string Filename;
117 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
120 void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
121 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
122 io.mapRequired("sym", s.first);
123 io.mapRequired("objAddr", s.second.ObjectAddress);
124 io.mapRequired("binAddr", s.second.BinaryAddress);
125 io.mapOptional("size", s.second.Size);
128 void MappingTraits<dsymutil::DebugMapObject>::mapping(
129 IO &io, dsymutil::DebugMapObject &DMO) {
130 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
131 io.mapRequired("filename", Norm->Filename);
132 io.mapRequired("symbols", Norm->Entries);
135 void ScalarTraits<Triple>::output(const Triple &val, void *,
136 llvm::raw_ostream &out) {
140 StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
141 value = Triple(scalar);
146 SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
147 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
151 dsymutil::DebugMapObject &
152 SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
153 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
155 if (index >= seq.size()) {
156 seq.resize(index + 1);
157 seq[index].reset(new dsymutil::DebugMapObject);
162 void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
163 dsymutil::DebugMap &DM) {
164 io.mapRequired("triple", DM.BinaryTriple);
165 io.mapOptional("objects", DM.Objects);
168 void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
169 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
171 DM.reset(new DebugMap());
172 io.mapRequired("triple", DM->BinaryTriple);
173 io.mapOptional("objects", DM->Objects);
176 MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
177 IO &io, dsymutil::DebugMapObject &Obj) {
178 Filename = Obj.Filename;
179 Entries.reserve(Obj.Symbols.size());
180 for (auto &Entry : Obj.Symbols)
181 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
184 dsymutil::DebugMapObject
185 MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
186 void *Ctxt = IO.getContext();
187 StringRef PrependPath = *reinterpret_cast<StringRef *>(Ctxt);
188 SmallString<80> Path(PrependPath);
189 sys::path::append(Path, Filename);
190 dsymutil::DebugMapObject Res(Path);
191 for (auto &Entry : Entries) {
192 auto &Mapping = Entry.second;
193 Res.addSymbol(Entry.first, Mapping.ObjectAddress, Mapping.BinaryAddress,