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 "BinaryHolder.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/iterator_range.h"
13 #include "llvm/Support/DataTypes.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm::object;
23 DebugMapObject::DebugMapObject(StringRef ObjectFilename,
24 sys::TimePoint<std::chrono::seconds> Timestamp,
26 : Filename(ObjectFilename), Timestamp(Timestamp), Type(Type) {}
28 bool DebugMapObject::addSymbol(StringRef Name, Optional<uint64_t> ObjectAddress,
29 uint64_t LinkedAddress, uint32_t Size) {
30 auto InsertResult = Symbols.insert(
31 std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
33 if (ObjectAddress && InsertResult.second)
34 AddressToMapping[*ObjectAddress] = &*InsertResult.first;
35 return InsertResult.second;
38 void DebugMapObject::print(raw_ostream &OS) const {
39 OS << getObjectFilename() << ":\n";
40 // Sort the symbols in alphabetical order, like llvm-nm (and to get
41 // deterministic output for testing).
42 typedef std::pair<StringRef, SymbolMapping> Entry;
43 std::vector<Entry> Entries;
44 Entries.reserve(Symbols.getNumItems());
45 for (const auto &Sym : make_range(Symbols.begin(), Symbols.end()))
46 Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
48 Entries.begin(), Entries.end(),
49 [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; });
50 for (const auto &Sym : Entries) {
51 if (Sym.second.ObjectAddress)
52 OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
54 OS << "\t????????????????";
55 OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
56 uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
63 void DebugMapObject::dump() const { print(errs()); }
67 DebugMap::addDebugMapObject(StringRef ObjectFilePath,
68 sys::TimePoint<std::chrono::seconds> Timestamp,
70 Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp, Type));
71 return *Objects.back();
74 const DebugMapObject::DebugMapEntry *
75 DebugMapObject::lookupSymbol(StringRef SymbolName) const {
76 StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
77 if (Sym == Symbols.end())
82 const DebugMapObject::DebugMapEntry *
83 DebugMapObject::lookupObjectAddress(uint64_t Address) const {
84 auto Mapping = AddressToMapping.find(Address);
85 if (Mapping == AddressToMapping.end())
87 return Mapping->getSecond();
90 void DebugMap::print(raw_ostream &OS) const {
91 yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
92 yout << const_cast<DebugMap &>(*this);
96 void DebugMap::dump() const { print(errs()); }
101 StringRef PrependPath;
106 ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
107 DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
109 auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
110 if (auto Err = ErrOrFile.getError())
115 Ctxt.PrependPath = PrependPath;
117 std::unique_ptr<DebugMap> Res;
118 yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
121 if (auto EC = yin.error())
123 std::vector<std::unique_ptr<DebugMap>> Result;
124 Result.push_back(std::move(Res));
125 return std::move(Result);
131 // Normalize/Denormalize between YAML and a DebugMapObject.
132 struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
133 YamlDMO(IO &io) { Timestamp = 0; }
134 YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
135 dsymutil::DebugMapObject denormalize(IO &IO);
137 std::string Filename;
139 std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
142 void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
143 mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
144 io.mapRequired("sym", s.first);
145 io.mapOptional("objAddr", s.second.ObjectAddress);
146 io.mapRequired("binAddr", s.second.BinaryAddress);
147 io.mapOptional("size", s.second.Size);
150 void MappingTraits<dsymutil::DebugMapObject>::mapping(
151 IO &io, dsymutil::DebugMapObject &DMO) {
152 MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
153 io.mapRequired("filename", Norm->Filename);
154 io.mapOptional("timestamp", Norm->Timestamp);
155 io.mapRequired("symbols", Norm->Entries);
158 void ScalarTraits<Triple>::output(const Triple &val, void *,
159 llvm::raw_ostream &out) {
163 StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
164 value = Triple(scalar);
169 SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
170 IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
174 dsymutil::DebugMapObject &
175 SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
176 IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
178 if (index >= seq.size()) {
179 seq.resize(index + 1);
180 seq[index].reset(new dsymutil::DebugMapObject);
185 void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
186 dsymutil::DebugMap &DM) {
187 io.mapRequired("triple", DM.BinaryTriple);
188 io.mapOptional("binary-path", DM.BinaryPath);
189 if (void *Ctxt = io.getContext())
190 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
191 io.mapOptional("objects", DM.Objects);
194 void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
195 IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
197 DM.reset(new DebugMap());
198 io.mapRequired("triple", DM->BinaryTriple);
199 io.mapOptional("binary-path", DM->BinaryPath);
200 if (void *Ctxt = io.getContext())
201 reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
202 io.mapOptional("objects", DM->Objects);
205 MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
206 IO &io, dsymutil::DebugMapObject &Obj) {
207 Filename = Obj.Filename;
208 Timestamp = sys::toTimeT(Obj.getTimestamp());
209 Entries.reserve(Obj.Symbols.size());
210 for (auto &Entry : Obj.Symbols)
211 Entries.push_back(std::make_pair(Entry.getKey(), Entry.getValue()));
214 dsymutil::DebugMapObject
215 MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
216 BinaryHolder BinHolder(/* Verbose =*/false);
217 const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
218 SmallString<80> Path(Ctxt.PrependPath);
219 StringMap<uint64_t> SymbolAddresses;
221 sys::path::append(Path, Filename);
222 auto ErrOrObjectFiles = BinHolder.GetObjectFiles(Path);
223 if (auto EC = ErrOrObjectFiles.getError()) {
224 llvm::errs() << "warning: Unable to open " << Path << " " << EC.message()
226 } else if (auto ErrOrObjectFile = BinHolder.Get(Ctxt.BinaryTriple)) {
227 // Rewrite the object file symbol addresses in the debug map. The
228 // YAML input is mainly used to test llvm-dsymutil without
229 // requiring binaries checked-in. If we generate the object files
230 // during the test, we can't hardcode the symbols addresses, so
231 // look them up here and rewrite them.
232 for (const auto &Sym : ErrOrObjectFile->symbols()) {
233 uint64_t Address = Sym.getValue();
234 Expected<StringRef> Name = Sym.getName();
236 (Sym.getFlags() & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
237 // TODO: Actually report errors helpfully.
239 consumeError(Name.takeError());
242 SymbolAddresses[*Name] = Address;
246 dsymutil::DebugMapObject Res(Path, sys::toTimePoint(Timestamp), MachO::N_OSO);
247 for (auto &Entry : Entries) {
248 auto &Mapping = Entry.second;
249 Optional<uint64_t> ObjAddress;
250 if (Mapping.ObjectAddress)
251 ObjAddress = *Mapping.ObjectAddress;
252 auto AddressIt = SymbolAddresses.find(Entry.first);
253 if (AddressIt != SymbolAddresses.end())
254 ObjAddress = AddressIt->getValue();
255 Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);