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 //===----------------------------------------------------------------------===//
11 #include "BinaryHolder.h"
12 #include "ErrorReporting.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/BinaryFormat/MachO.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Chrono.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/YAMLTraits.h"
27 #include "llvm/Support/raw_ostream.h"
40 using namespace llvm::object;
42 DebugMapObject::DebugMapObject(StringRef ObjectFilename,
43 sys::TimePoint<std::chrono::seconds> Timestamp,
45 : Filename(ObjectFilename), Timestamp(Timestamp), Type(Type) {}
47 bool DebugMapObject::addSymbol(StringRef Name, Optional<uint64_t> ObjectAddress,
48 uint64_t LinkedAddress, uint32_t Size) {
49 auto InsertResult = Symbols.insert(
50 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
52 if (ObjectAddress && InsertResult.second)
53 AddressToMapping[*ObjectAddress] = &*InsertResult.first;
54 return InsertResult.second;
57 void 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).
61 using Entry = std::pair<StringRef, SymbolMapping>;
62 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()));
66 llvm::sort(Entries.begin(), Entries.end(),
67 [](const Entry &LHS, const Entry &RHS) {
68 return LHS.first < RHS.first;
70 for (const auto &Sym : Entries) {
71 if (Sym.second.ObjectAddress)
72 OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
74 OS << "\t????????????????";
75 OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
76 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
83 void DebugMapObject::dump() const { print(errs()); }
87 DebugMap::addDebugMapObject(StringRef ObjectFilePath,
88 sys::TimePoint<std::chrono::seconds> Timestamp,
90 Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp, Type));
91 return *Objects.back();
94 const DebugMapObject::DebugMapEntry *
95 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
96 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
97 if (Sym == Symbols.end())
102 const DebugMapObject::DebugMapEntry *
103 DebugMapObject::lookupObjectAddress(uint64_t Address) const {
104 auto Mapping = AddressToMapping.find(Address);
105 if (Mapping == AddressToMapping.end())
107 return Mapping->getSecond();
110 void DebugMap::print(raw_ostream &OS) const {
111 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
112 yout << const_cast<DebugMap &>(*this);
116 void DebugMap::dump() const { print(errs()); }
122 StringRef PrependPath;
126 } // end anonymous namespace
128 ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
129 DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
131 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
132 if (auto Err = ErrOrFile.getError())
137 Ctxt.PrependPath = PrependPath;
139 std::unique_ptr<DebugMap> Res;
140 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
143 if (auto EC = yin.error())
145 std::vector<std::unique_ptr<DebugMap>> Result;
146 Result.push_back(std::move(Res));
147 return std::move(Result);
150 } // end namespace dsymutil
154 // Normalize/Denormalize between YAML and a DebugMapObject.
155 struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
156 YamlDMO(IO &io) { Timestamp = 0; }
157 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
158 dsymutil::DebugMapObject denormalize(IO &IO);
160 std::string Filename;
162 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
165 void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
166 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
167 io.mapRequired("sym", s.first);
168 io.mapOptional("objAddr", s.second.ObjectAddress);
169 io.mapRequired("binAddr", s.second.BinaryAddress);
170 io.mapOptional("size", s.second.Size);
173 void MappingTraits<dsymutil::DebugMapObject>::mapping(
174 IO &io, dsymutil::DebugMapObject &DMO) {
175 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
176 io.mapRequired("filename", Norm->Filename);
177 io.mapOptional("timestamp", Norm->Timestamp);
178 io.mapRequired("symbols", Norm->Entries);
181 void ScalarTraits<Triple>::output(const Triple &val, void *, raw_ostream &out) {
185 StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
186 value = Triple(scalar);
191 SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
192 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
196 dsymutil::DebugMapObject &
197 SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
198 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
200 if (index >= seq.size()) {
201 seq.resize(index + 1);
202 seq[index].reset(new dsymutil::DebugMapObject);
207 void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
208 dsymutil::DebugMap &DM) {
209 io.mapRequired("triple", DM.BinaryTriple);
210 io.mapOptional("binary-path", DM.BinaryPath);
211 if (void *Ctxt = io.getContext())
212 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
213 io.mapOptional("objects", DM.Objects);
216 void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
217 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
219 DM.reset(new DebugMap());
220 io.mapRequired("triple", DM->BinaryTriple);
221 io.mapOptional("binary-path", DM->BinaryPath);
222 if (void *Ctxt = io.getContext())
223 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
224 io.mapOptional("objects", DM->Objects);
227 MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
228 IO &io, dsymutil::DebugMapObject &Obj) {
229 Filename = Obj.Filename;
230 Timestamp = sys::toTimeT(Obj.getTimestamp());
231 Entries.reserve(Obj.Symbols.size());
232 for (auto &Entry : Obj.Symbols)
233 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
236 dsymutil::DebugMapObject
237 MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
238 BinaryHolder BinHolder(/* Verbose =*/false);
239 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
240 SmallString<80> Path(Ctxt.PrependPath);
241 StringMap<uint64_t> SymbolAddresses;
243 sys::path::append(Path, Filename);
244 auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
245 if (auto EC = ErrOrObjectFiles.getError()) {
246 warn_ostream() << "Unable to open " << Path << " " << EC.message() << '\n';
247 } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
248 // Rewrite the object file symbol addresses in the debug map. The YAML
249 // input is mainly used to test dsymutil without requiring binaries
250 // checked-in. If we generate the object files during the test, we can't
251 // hard-code the symbols addresses, so look them up here and rewrite them.
252 for (const auto &Sym : ErrOrObjectFile->symbols()) {
253 uint64_t Address = Sym.getValue();
254 Expected<StringRef> Name = Sym.getName();
256 (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
257 // TODO: Actually report errors helpfully.
259 consumeError(Name.takeError());
262 SymbolAddresses[*Name] = Address;
266 dsymutil::DebugMapObject Res(Path, sys::toTimePoint(Timestamp), MachO::N_OSO);
267 for (auto &Entry : Entries) {
268 auto &Mapping = Entry.second;
269 Optional<uint64_t> ObjAddress;
270 if (Mapping.ObjectAddress)
271 ObjAddress = *Mapping.ObjectAddress;
272 auto AddressIt = SymbolAddresses.find(Entry.first);
273 if (AddressIt != SymbolAddresses.end())
274 ObjAddress = AddressIt->getValue();
275 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
280 } // end namespace yaml
281 } // end namespace llvm