blob: d2d5b615a32d77c1c07433ed5b95c4d8d4419b26 [file] [log] [blame]
Frederic Riss231f7142014-12-12 17:31:24 +00001//===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "DebugMap.h"
Frederic Riss4f5874a2015-06-05 21:12:07 +000010#include "BinaryHolder.h"
Frederic Riss231f7142014-12-12 17:31:24 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000012#include "llvm/ADT/iterator_range.h"
Frederic Riss231f7142014-12-12 17:31:24 +000013#include "llvm/Support/DataTypes.h"
14#include "llvm/Support/Format.h"
15#include "llvm/Support/raw_ostream.h"
16#include <algorithm>
17
18namespace llvm {
19namespace dsymutil {
20
21using namespace llvm::object;
22
Frederic Riss9ccfddc2015-07-22 23:24:00 +000023DebugMapObject::DebugMapObject(StringRef ObjectFilename,
24 sys::TimeValue Timestamp)
25 : Filename(ObjectFilename), Timestamp(Timestamp) {}
Frederic Riss231f7142014-12-12 17:31:24 +000026
Frederic Rissd8c33dc2016-01-31 04:29:22 +000027bool DebugMapObject::addSymbol(StringRef Name, Optional<uint64_t> ObjectAddress,
Frederic Riss912d0f12015-03-15 01:29:30 +000028 uint64_t LinkedAddress, uint32_t Size) {
Frederic Riss231f7142014-12-12 17:31:24 +000029 auto InsertResult = Symbols.insert(
Frederic Riss912d0f12015-03-15 01:29:30 +000030 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
Frederic Riss1595c5d2015-02-13 23:18:16 +000031
Frederic Rissd8c33dc2016-01-31 04:29:22 +000032 if (ObjectAddress && InsertResult.second)
33 AddressToMapping[*ObjectAddress] = &*InsertResult.first;
Frederic Riss231f7142014-12-12 17:31:24 +000034 return InsertResult.second;
35}
36
37void DebugMapObject::print(raw_ostream &OS) const {
38 OS << getObjectFilename() << ":\n";
39 // Sort the symbols in alphabetical order, like llvm-nm (and to get
40 // deterministic output for testing).
41 typedef std::pair<StringRef, SymbolMapping> Entry;
42 std::vector<Entry> Entries;
43 Entries.reserve(Symbols.getNumItems());
44 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
45 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
46 std::sort(
47 Entries.begin(), Entries.end(),
48 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
49 for (const auto &Sym : Entries) {
Frederic Rissd8c33dc2016-01-31 04:29:22 +000050 if (Sym.second.ObjectAddress)
51 OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
52 else
53 OS << "\t????????????????";
54 OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
Frederic Rissf37964c2015-06-05 20:27:07 +000055 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
56 Sym.first.data());
Frederic Riss231f7142014-12-12 17:31:24 +000057 }
58 OS << '\n';
59}
60
61#ifndef NDEBUG
62void DebugMapObject::dump() const { print(errs()); }
63#endif
64
Frederic Riss9ccfddc2015-07-22 23:24:00 +000065DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath,
66 sys::TimeValue Timestamp) {
67 Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp));
Frederic Riss231f7142014-12-12 17:31:24 +000068 return *Objects.back();
69}
70
Frederic Riss1595c5d2015-02-13 23:18:16 +000071const DebugMapObject::DebugMapEntry *
Frederic Riss231f7142014-12-12 17:31:24 +000072DebugMapObject::lookupSymbol(StringRef SymbolName) const {
73 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
74 if (Sym == Symbols.end())
75 return nullptr;
Frederic Riss1595c5d2015-02-13 23:18:16 +000076 return &*Sym;
77}
78
79const DebugMapObject::DebugMapEntry *
80DebugMapObject::lookupObjectAddress(uint64_t Address) const {
81 auto Mapping = AddressToMapping.find(Address);
82 if (Mapping == AddressToMapping.end())
83 return nullptr;
84 return Mapping->getSecond();
Frederic Riss231f7142014-12-12 17:31:24 +000085}
86
87void DebugMap::print(raw_ostream &OS) const {
Frederic Riss08462f72015-06-01 21:12:45 +000088 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
Frederic Rissf37964c2015-06-05 20:27:07 +000089 yout << const_cast<DebugMap &>(*this);
Frederic Riss231f7142014-12-12 17:31:24 +000090}
91
92#ifndef NDEBUG
93void DebugMap::dump() const { print(errs()); }
94#endif
Frederic Riss4d0ba662015-06-05 20:27:04 +000095
Frederic Riss4f5874a2015-06-05 21:12:07 +000096namespace {
97struct YAMLContext {
98 StringRef PrependPath;
Frederic Rissa81e8812015-06-05 21:21:57 +000099 Triple BinaryTriple;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000100};
101}
102
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000103ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
Frederic Riss4d0ba662015-06-05 20:27:04 +0000104DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
105 bool Verbose) {
106 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
107 if (auto Err = ErrOrFile.getError())
108 return Err;
109
Frederic Riss4f5874a2015-06-05 21:12:07 +0000110 YAMLContext Ctxt;
111
112 Ctxt.PrependPath = PrependPath;
113
Frederic Riss4d0ba662015-06-05 20:27:04 +0000114 std::unique_ptr<DebugMap> Res;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000115 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000116 yin >> Res;
117
118 if (auto EC = yin.error())
119 return EC;
Frederic Riss4dd3e0c2015-08-05 18:27:44 +0000120 std::vector<std::unique_ptr<DebugMap>> Result;
121 Result.push_back(std::move(Res));
122 return std::move(Result);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000123}
124}
125
126namespace yaml {
127
128// Normalize/Denormalize between YAML and a DebugMapObject.
129struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000130 YamlDMO(IO &io) { Timestamp = 0; }
Frederic Riss4d0ba662015-06-05 20:27:04 +0000131 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
132 dsymutil::DebugMapObject denormalize(IO &IO);
133
134 std::string Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000135 sys::TimeValue::SecondsType Timestamp;
Frederic Riss4d0ba662015-06-05 20:27:04 +0000136 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
137};
138
139void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
140 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
141 io.mapRequired("sym", s.first);
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000142 io.mapOptional("objAddr", s.second.ObjectAddress);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000143 io.mapRequired("binAddr", s.second.BinaryAddress);
144 io.mapOptional("size", s.second.Size);
145}
146
147void MappingTraits<dsymutil::DebugMapObject>::mapping(
148 IO &io, dsymutil::DebugMapObject &DMO) {
149 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
150 io.mapRequired("filename", Norm->Filename);
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000151 io.mapOptional("timestamp", Norm->Timestamp);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000152 io.mapRequired("symbols", Norm->Entries);
153}
154
155void ScalarTraits<Triple>::output(const Triple &val, void *,
156 llvm::raw_ostream &out) {
157 out << val.str();
158}
159
160StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
161 value = Triple(scalar);
162 return StringRef();
163}
164
165size_t
166SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
167 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
168 return seq.size();
169}
170
171dsymutil::DebugMapObject &
172SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
173 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
174 size_t index) {
175 if (index >= seq.size()) {
176 seq.resize(index + 1);
177 seq[index].reset(new dsymutil::DebugMapObject);
178 }
179 return *seq[index];
180}
181
182void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
183 dsymutil::DebugMap &DM) {
184 io.mapRequired("triple", DM.BinaryTriple);
Frederic Riss2c69d362015-08-26 05:09:59 +0000185 io.mapOptional("binary-path", DM.BinaryPath);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000186 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000187 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
Frederic Risseb85c8f2015-07-24 06:41:11 +0000188 io.mapOptional("objects", DM.Objects);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000189}
190
191void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
192 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
193 if (!DM)
194 DM.reset(new DebugMap());
195 io.mapRequired("triple", DM->BinaryTriple);
Frederic Riss2c69d362015-08-26 05:09:59 +0000196 io.mapOptional("binary-path", DM->BinaryPath);
Frederic Riss4f5874a2015-06-05 21:12:07 +0000197 if (void *Ctxt = io.getContext())
Frederic Rissa81e8812015-06-05 21:21:57 +0000198 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
Frederic Risseb85c8f2015-07-24 06:41:11 +0000199 io.mapOptional("objects", DM->Objects);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000200}
201
202MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
203 IO &io, dsymutil::DebugMapObject &Obj) {
204 Filename = Obj.Filename;
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000205 Timestamp = Obj.getTimestamp().toEpochTime();
Frederic Riss4d0ba662015-06-05 20:27:04 +0000206 Entries.reserve(Obj.Symbols.size());
207 for (auto &Entry : Obj.Symbols)
208 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
209}
210
211dsymutil::DebugMapObject
212MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000213 BinaryHolder BinHolder(/* Verbose =*/false);
214 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
215 SmallString<80> Path(Ctxt.PrependPath);
216 StringMap<uint64_t> SymbolAddresses;
217
Frederic Riss4d0ba662015-06-05 20:27:04 +0000218 sys::path::append(Path, Filename);
Frederic Risseb85c8f2015-07-24 06:41:11 +0000219 auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
220 if (auto EC = ErrOrObjectFiles.getError()) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000221 llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
222 << '\n';
Frederic Risseb85c8f2015-07-24 06:41:11 +0000223 } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
Frederic Riss4f5874a2015-06-05 21:12:07 +0000224 // Rewrite the object file symbol addresses in the debug map. The
225 // YAML input is mainly used to test llvm-dsymutil without
226 // requiring binaries checked-in. If we generate the object files
227 // during the test, we can't hardcode the symbols addresses, so
228 // look them up here and rewrite them.
229 for (const auto &Sym : ErrOrObjectFile->symbols()) {
Rafael Espindolabe8b0ea2015-07-07 17:12:59 +0000230 uint64_t Address = Sym.getValue();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000231 ErrorOr<StringRef> Name = Sym.getName();
232 if (!Name)
233 continue;
234 SymbolAddresses[*Name] = Address;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000235 }
236 }
237
Frederic Riss9ccfddc2015-07-22 23:24:00 +0000238 sys::TimeValue TV;
239 TV.fromEpochTime(Timestamp);
240 dsymutil::DebugMapObject Res(Path, TV);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000241 for (auto &Entry : Entries) {
242 auto &Mapping = Entry.second;
Frederic Rissd8c33dc2016-01-31 04:29:22 +0000243 Optional<uint64_t> ObjAddress;
244 if (Mapping.ObjectAddress)
245 ObjAddress = *Mapping.ObjectAddress;
Frederic Riss4f5874a2015-06-05 21:12:07 +0000246 auto AddressIt = SymbolAddresses.find(Entry.first);
247 if (AddressIt != SymbolAddresses.end())
248 ObjAddress = AddressIt->getValue();
249 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
Frederic Riss4d0ba662015-06-05 20:27:04 +0000250 }
251 return Res;
252}
Frederic Riss231f7142014-12-12 17:31:24 +0000253}
254}