blob: 509afaf56f4e86658dc5ad7d7e246bfdfe7f9fe6 [file] [log] [blame]
Frederic Riss31e081e2014-12-12 17:31:24 +00001//===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
2//
Jonas Devlieghere928fea22018-06-27 16:13:40 +00003// The LLVM Compiler Infrastructure
Frederic Riss31e081e2014-12-12 17:31:24 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenko66f724e2017-11-01 21:16:06 +00009
Frederic Riss31e081e2014-12-12 17:31:24 +000010#include "DebugMap.h"
Frederic Rissdfbf8972015-06-05 21:12:07 +000011#include "BinaryHolder.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000012#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/SmallString.h"
14#include "llvm/ADT/StringMap.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Triple.h"
Chandler Carruth1b279142015-01-14 11:23:27 +000017#include "llvm/ADT/iterator_range.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000018#include "llvm/BinaryFormat/MachO.h"
19#include "llvm/Object/ObjectFile.h"
20#include "llvm/Support/Chrono.h"
21#include "llvm/Support/Error.h"
Frederic Riss31e081e2014-12-12 17:31:24 +000022#include "llvm/Support/Format.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000023#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/Path.h"
Jonas Devlieghere5baab4c2018-04-14 21:36:42 +000025#include "llvm/Support/WithColor.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000026#include "llvm/Support/YAMLTraits.h"
Frederic Riss31e081e2014-12-12 17:31:24 +000027#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
Eugene Zelenko66f724e2017-11-01 21:16:06 +000029#include <cinttypes>
30#include <cstdint>
31#include <memory>
32#include <string>
33#include <utility>
34#include <vector>
Frederic Riss31e081e2014-12-12 17:31:24 +000035
36namespace llvm {
Eugene Zelenko66f724e2017-11-01 21:16:06 +000037
Frederic Riss31e081e2014-12-12 17:31:24 +000038namespace dsymutil {
39
40using namespace llvm::object;
41
Frederic Riss00d14dd2015-07-22 23:24:00 +000042DebugMapObject::DebugMapObject(StringRef ObjectFilename,
Francis Ricci6f200df2017-10-06 14:49:20 +000043 sys::TimePoint<std::chrono::seconds> Timestamp,
44 uint8_t Type)
45 : Filename(ObjectFilename), Timestamp(Timestamp), Type(Type) {}
Frederic Riss31e081e2014-12-12 17:31:24 +000046
Frederic Rissc5413fc2016-01-31 04:29:22 +000047bool DebugMapObject::addSymbol(StringRef Name, Optional<uint64_t> ObjectAddress,
Frederic Risse7a35642015-03-15 01:29:30 +000048 uint64_t LinkedAddress, uint32_t Size) {
Frederic Riss31e081e2014-12-12 17:31:24 +000049 auto InsertResult = Symbols.insert(
Frederic Risse7a35642015-03-15 01:29:30 +000050 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
Frederic Risse9483b82015-02-13 23:18:16 +000051
Frederic Rissc5413fc2016-01-31 04:29:22 +000052 if (ObjectAddress && InsertResult.second)
53 AddressToMapping[*ObjectAddress] = &*InsertResult.first;
Frederic Riss31e081e2014-12-12 17:31:24 +000054 return InsertResult.second;
55}
56
57void DebugMapObject::print(raw_ostream &OS) const {
58 OS << getObjectFilename() << ":\n";
59 // Sort the symbols in alphabetical order, like llvm-nm (and to get
60 // deterministic output for testing).
Eugene Zelenko66f724e2017-11-01 21:16:06 +000061 using Entry = std::pair<StringRef, SymbolMapping>;
Frederic Riss31e081e2014-12-12 17:31:24 +000062 std::vector<Entry> Entries;
63 Entries.reserve(Symbols.getNumItems());
64 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
65 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
Fangrui Songc4662642018-09-30 22:31:29 +000066 llvm::sort(Entries, [](const Entry &LHS, const Entry &RHS) {
67 return LHS.first < RHS.first;
68 });
Frederic Riss31e081e2014-12-12 17:31:24 +000069 for (const auto &Sym : Entries) {
Frederic Rissc5413fc2016-01-31 04:29:22 +000070 if (Sym.second.ObjectAddress)
71 OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
72 else
73 OS << "\t????????????????";
74 OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
Frederic Riss0f58c382015-06-05 20:27:07 +000075 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
76 Sym.first.data());
Frederic Riss31e081e2014-12-12 17:31:24 +000077 }
78 OS << '\n';
79}
80
81#ifndef NDEBUG
82void DebugMapObject::dump() const { print(errs()); }
83#endif
84
Pavel Labath73ce0c02016-11-09 11:43:52 +000085DebugMapObject &
86DebugMap::addDebugMapObject(StringRef ObjectFilePath,
Francis Ricci6f200df2017-10-06 14:49:20 +000087 sys::TimePoint<std::chrono::seconds> Timestamp,
88 uint8_t Type) {
89 Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp, Type));
Frederic Riss31e081e2014-12-12 17:31:24 +000090 return *Objects.back();
91}
92
Frederic Risse9483b82015-02-13 23:18:16 +000093const DebugMapObject::DebugMapEntry *
Frederic Riss31e081e2014-12-12 17:31:24 +000094DebugMapObject::lookupSymbol(StringRef SymbolName) const {
95 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
96 if (Sym == Symbols.end())
97 return nullptr;
Frederic Risse9483b82015-02-13 23:18:16 +000098 return &*Sym;
99}
100
101const DebugMapObject::DebugMapEntry *
102DebugMapObject::lookupObjectAddress(uint64_t Address) const {
103 auto Mapping = AddressToMapping.find(Address);
104 if (Mapping == AddressToMapping.end())
105 return nullptr;
106 return Mapping->getSecond();
Frederic Riss31e081e2014-12-12 17:31:24 +0000107}
108
109void DebugMap::print(raw_ostream &OS) const {
Frederic Riss786b4712015-06-01 21:12:45 +0000110 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
Frederic Riss0f58c382015-06-05 20:27:07 +0000111 yout << const_cast<DebugMap &>(*this);
Frederic Riss31e081e2014-12-12 17:31:24 +0000112}
113
114#ifndef NDEBUG
115void DebugMap::dump() const { print(errs()); }
116#endif
Frederic Riss527bb612015-06-05 20:27:04 +0000117
Frederic Rissdfbf8972015-06-05 21:12:07 +0000118namespace {
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000119
Frederic Rissdfbf8972015-06-05 21:12:07 +0000120struct YAMLContext {
121 StringRef PrependPath;
Frederic Riss7bef4c42015-06-05 21:21:57 +0000122 Triple BinaryTriple;
Frederic Rissdfbf8972015-06-05 21:12:07 +0000123};
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000124
125} // end anonymous namespace
Frederic Rissdfbf8972015-06-05 21:12:07 +0000126
Frederic Riss7a425782015-08-05 18:27:44 +0000127ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss527bb612015-06-05 20:27:04 +0000128DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
129 bool Verbose) {
130 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
131 if (auto Err = ErrOrFile.getError())
132 return Err;
133
Frederic Rissdfbf8972015-06-05 21:12:07 +0000134 YAMLContext Ctxt;
135
136 Ctxt.PrependPath = PrependPath;
137
Frederic Riss527bb612015-06-05 20:27:04 +0000138 std::unique_ptr<DebugMap> Res;
Frederic Rissdfbf8972015-06-05 21:12:07 +0000139 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
Frederic Riss527bb612015-06-05 20:27:04 +0000140 yin >> Res;
141
142 if (auto EC = yin.error())
143 return EC;
Frederic Riss7a425782015-08-05 18:27:44 +0000144 std::vector<std::unique_ptr<DebugMap>> Result;
145 Result.push_back(std::move(Res));
146 return std::move(Result);
Frederic Riss527bb612015-06-05 20:27:04 +0000147}
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000148
149} // end namespace dsymutil
Frederic Riss527bb612015-06-05 20:27:04 +0000150
151namespace yaml {
152
153// Normalize/Denormalize between YAML and a DebugMapObject.
154struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
Frederic Riss00d14dd2015-07-22 23:24:00 +0000155 YamlDMO(IO &io) { Timestamp = 0; }
Frederic Riss527bb612015-06-05 20:27:04 +0000156 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
157 dsymutil::DebugMapObject denormalize(IO &IO);
158
159 std::string Filename;
Pavel Labath73ce0c02016-11-09 11:43:52 +0000160 int64_t Timestamp;
Frederic Riss527bb612015-06-05 20:27:04 +0000161 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
162};
163
164void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
165 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
166 io.mapRequired("sym", s.first);
Frederic Rissc5413fc2016-01-31 04:29:22 +0000167 io.mapOptional("objAddr", s.second.ObjectAddress);
Frederic Riss527bb612015-06-05 20:27:04 +0000168 io.mapRequired("binAddr", s.second.BinaryAddress);
169 io.mapOptional("size", s.second.Size);
170}
171
172void MappingTraits<dsymutil::DebugMapObject>::mapping(
173 IO &io, dsymutil::DebugMapObject &DMO) {
174 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
175 io.mapRequired("filename", Norm->Filename);
Frederic Riss00d14dd2015-07-22 23:24:00 +0000176 io.mapOptional("timestamp", Norm->Timestamp);
Frederic Riss527bb612015-06-05 20:27:04 +0000177 io.mapRequired("symbols", Norm->Entries);
178}
179
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000180void ScalarTraits<Triple>::output(const Triple &val, void *, raw_ostream &out) {
Frederic Riss527bb612015-06-05 20:27:04 +0000181 out << val.str();
182}
183
184StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
185 value = Triple(scalar);
186 return StringRef();
187}
188
189size_t
190SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
191 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
192 return seq.size();
193}
194
195dsymutil::DebugMapObject &
196SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
197 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
198 size_t index) {
199 if (index >= seq.size()) {
200 seq.resize(index + 1);
201 seq[index].reset(new dsymutil::DebugMapObject);
202 }
203 return *seq[index];
204}
205
206void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
207 dsymutil::DebugMap &DM) {
208 io.mapRequired("triple", DM.BinaryTriple);
Frederic Riss65ce0b12015-08-26 05:09:59 +0000209 io.mapOptional("binary-path", DM.BinaryPath);
Frederic Rissdfbf8972015-06-05 21:12:07 +0000210 if (void *Ctxt = io.getContext())
Frederic Riss7bef4c42015-06-05 21:21:57 +0000211 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000212 io.mapOptional("objects", DM.Objects);
Frederic Riss527bb612015-06-05 20:27:04 +0000213}
214
215void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
216 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
217 if (!DM)
218 DM.reset(new DebugMap());
219 io.mapRequired("triple", DM->BinaryTriple);
Frederic Riss65ce0b12015-08-26 05:09:59 +0000220 io.mapOptional("binary-path", DM->BinaryPath);
Frederic Rissdfbf8972015-06-05 21:12:07 +0000221 if (void *Ctxt = io.getContext())
Frederic Riss7bef4c42015-06-05 21:21:57 +0000222 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
Frederic Rissfa7f7bd2015-07-24 06:41:11 +0000223 io.mapOptional("objects", DM->Objects);
Frederic Riss527bb612015-06-05 20:27:04 +0000224}
225
226MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
227 IO &io, dsymutil::DebugMapObject &Obj) {
228 Filename = Obj.Filename;
Pavel Labath73ce0c02016-11-09 11:43:52 +0000229 Timestamp = sys::toTimeT(Obj.getTimestamp());
Frederic Riss527bb612015-06-05 20:27:04 +0000230 Entries.reserve(Obj.Symbols.size());
231 for (auto &Entry : Obj.Symbols)
232 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
233}
234
235dsymutil::DebugMapObject
236MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
Frederic Rissdfbf8972015-06-05 21:12:07 +0000237 BinaryHolder BinHolder(/* Verbose =*/false);
238 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
239 SmallString<80> Path(Ctxt.PrependPath);
240 StringMap<uint64_t> SymbolAddresses;
241
Frederic Riss527bb612015-06-05 20:27:04 +0000242 sys::path::append(Path, Filename);
Jonas Devlieghere9f33bbfe2018-06-29 16:51:52 +0000243
244 auto ObjectEntry = BinHolder.getObjectEntry(Path);
245 if (!ObjectEntry) {
246 auto Err = ObjectEntry.takeError();
247 WithColor::warning() << "Unable to open " << Path << " "
248 << toString(std::move(Err)) << '\n';
249 } else {
250 auto Object = ObjectEntry->getObject(Ctxt.BinaryTriple);
251 if (!Object) {
252 auto Err = Object.takeError();
253 WithColor::warning() << "Unable to open " << Path << " "
254 << toString(std::move(Err)) << '\n';
255 } else {
256 for (const auto &Sym : Object->symbols()) {
257 uint64_t Address = Sym.getValue();
258 Expected<StringRef> Name = Sym.getName();
259 if (!Name || (Sym.getFlags() &
260 (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
261 // TODO: Actually report errors helpfully.
262 if (!Name)
263 consumeError(Name.takeError());
264 continue;
265 }
266 SymbolAddresses[*Name] = Address;
Kevin Enderby813e0cf2016-04-20 21:24:34 +0000267 }
Frederic Rissdfbf8972015-06-05 21:12:07 +0000268 }
269 }
270
Francis Ricci6f200df2017-10-06 14:49:20 +0000271 dsymutil::DebugMapObject Res(Path, sys::toTimePoint(Timestamp), MachO::N_OSO);
Frederic Riss527bb612015-06-05 20:27:04 +0000272 for (auto &Entry : Entries) {
273 auto &Mapping = Entry.second;
Frederic Rissc5413fc2016-01-31 04:29:22 +0000274 Optional<uint64_t> ObjAddress;
275 if (Mapping.ObjectAddress)
276 ObjAddress = *Mapping.ObjectAddress;
Frederic Rissdfbf8972015-06-05 21:12:07 +0000277 auto AddressIt = SymbolAddresses.find(Entry.first);
278 if (AddressIt != SymbolAddresses.end())
279 ObjAddress = AddressIt->getValue();
280 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
Frederic Riss527bb612015-06-05 20:27:04 +0000281 }
282 return Res;
283}
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000284
285} // end namespace yaml
Eugene Zelenko66f724e2017-11-01 21:16:06 +0000286} // end namespace llvm