DebugMap.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "DebugMap.h"
  9. #include "BinaryHolder.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringMap.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/ADT/iterator_range.h"
  15. #include "llvm/BinaryFormat/MachO.h"
  16. #include "llvm/Object/ObjectFile.h"
  17. #include "llvm/Support/Chrono.h"
  18. #include "llvm/Support/Error.h"
  19. #include "llvm/Support/Format.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/WithColor.h"
  23. #include "llvm/Support/YAMLTraits.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <algorithm>
  26. #include <cinttypes>
  27. #include <cstdint>
  28. #include <memory>
  29. #include <optional>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. namespace llvm {
  34. namespace dsymutil {
  35. using namespace llvm::object;
  36. DebugMapObject::DebugMapObject(StringRef ObjectFilename,
  37. sys::TimePoint<std::chrono::seconds> Timestamp,
  38. uint8_t Type)
  39. : Filename(std::string(ObjectFilename)), Timestamp(Timestamp), Type(Type) {}
  40. bool DebugMapObject::addSymbol(StringRef Name,
  41. std::optional<uint64_t> ObjectAddress,
  42. uint64_t LinkedAddress, uint32_t Size) {
  43. auto InsertResult = Symbols.insert(
  44. std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress, Size)));
  45. if (ObjectAddress && InsertResult.second)
  46. AddressToMapping[*ObjectAddress] = &*InsertResult.first;
  47. return InsertResult.second;
  48. }
  49. void DebugMapObject::print(raw_ostream &OS) const {
  50. OS << getObjectFilename() << ":\n";
  51. // Sort the symbols in alphabetical order, like llvm-nm (and to get
  52. // deterministic output for testing).
  53. using Entry = std::pair<StringRef, SymbolMapping>;
  54. std::vector<Entry> Entries;
  55. Entries.reserve(Symbols.getNumItems());
  56. for (const auto &Sym : Symbols)
  57. Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue()));
  58. llvm::sort(Entries, llvm::less_first());
  59. for (const auto &Sym : Entries) {
  60. if (Sym.second.ObjectAddress)
  61. OS << format("\t%016" PRIx64, uint64_t(*Sym.second.ObjectAddress));
  62. else
  63. OS << "\t????????????????";
  64. OS << format(" => %016" PRIx64 "+0x%x\t%s\n",
  65. uint64_t(Sym.second.BinaryAddress), uint32_t(Sym.second.Size),
  66. Sym.first.data());
  67. }
  68. OS << '\n';
  69. }
  70. #ifndef NDEBUG
  71. void DebugMapObject::dump() const { print(errs()); }
  72. #endif
  73. DebugMapObject &
  74. DebugMap::addDebugMapObject(StringRef ObjectFilePath,
  75. sys::TimePoint<std::chrono::seconds> Timestamp,
  76. uint8_t Type) {
  77. Objects.emplace_back(new DebugMapObject(ObjectFilePath, Timestamp, Type));
  78. return *Objects.back();
  79. }
  80. const DebugMapObject::DebugMapEntry *
  81. DebugMapObject::lookupSymbol(StringRef SymbolName) const {
  82. StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName);
  83. if (Sym == Symbols.end())
  84. return nullptr;
  85. return &*Sym;
  86. }
  87. const DebugMapObject::DebugMapEntry *
  88. DebugMapObject::lookupObjectAddress(uint64_t Address) const {
  89. auto Mapping = AddressToMapping.find(Address);
  90. if (Mapping == AddressToMapping.end())
  91. return nullptr;
  92. return Mapping->getSecond();
  93. }
  94. void DebugMap::print(raw_ostream &OS) const {
  95. yaml::Output yout(OS, /* Ctxt = */ nullptr, /* WrapColumn = */ 0);
  96. yout << const_cast<DebugMap &>(*this);
  97. }
  98. #ifndef NDEBUG
  99. void DebugMap::dump() const { print(errs()); }
  100. #endif
  101. namespace {
  102. struct YAMLContext {
  103. StringRef PrependPath;
  104. Triple BinaryTriple;
  105. };
  106. } // end anonymous namespace
  107. ErrorOr<std::vector<std::unique_ptr<DebugMap>>>
  108. DebugMap::parseYAMLDebugMap(StringRef InputFile, StringRef PrependPath,
  109. bool Verbose) {
  110. auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(InputFile);
  111. if (auto Err = ErrOrFile.getError())
  112. return Err;
  113. YAMLContext Ctxt;
  114. Ctxt.PrependPath = PrependPath;
  115. std::unique_ptr<DebugMap> Res;
  116. yaml::Input yin((*ErrOrFile)->getBuffer(), &Ctxt);
  117. yin >> Res;
  118. if (auto EC = yin.error())
  119. return EC;
  120. std::vector<std::unique_ptr<DebugMap>> Result;
  121. Result.push_back(std::move(Res));
  122. return std::move(Result);
  123. }
  124. } // end namespace dsymutil
  125. namespace yaml {
  126. // Normalize/Denormalize between YAML and a DebugMapObject.
  127. struct MappingTraits<dsymutil::DebugMapObject>::YamlDMO {
  128. YamlDMO(IO &io) { Timestamp = 0; }
  129. YamlDMO(IO &io, dsymutil::DebugMapObject &Obj);
  130. dsymutil::DebugMapObject denormalize(IO &IO);
  131. std::string Filename;
  132. int64_t Timestamp;
  133. std::vector<dsymutil::DebugMapObject::YAMLSymbolMapping> Entries;
  134. };
  135. void MappingTraits<std::pair<std::string, DebugMapObject::SymbolMapping>>::
  136. mapping(IO &io, std::pair<std::string, DebugMapObject::SymbolMapping> &s) {
  137. io.mapRequired("sym", s.first);
  138. io.mapOptional("objAddr", s.second.ObjectAddress);
  139. io.mapRequired("binAddr", s.second.BinaryAddress);
  140. io.mapOptional("size", s.second.Size);
  141. }
  142. void MappingTraits<dsymutil::DebugMapObject>::mapping(
  143. IO &io, dsymutil::DebugMapObject &DMO) {
  144. MappingNormalization<YamlDMO, dsymutil::DebugMapObject> Norm(io, DMO);
  145. io.mapRequired("filename", Norm->Filename);
  146. io.mapOptional("timestamp", Norm->Timestamp);
  147. io.mapRequired("symbols", Norm->Entries);
  148. }
  149. void ScalarTraits<Triple>::output(const Triple &val, void *, raw_ostream &out) {
  150. out << val.str();
  151. }
  152. StringRef ScalarTraits<Triple>::input(StringRef scalar, void *, Triple &value) {
  153. value = Triple(scalar);
  154. return StringRef();
  155. }
  156. size_t
  157. SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::size(
  158. IO &io, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq) {
  159. return seq.size();
  160. }
  161. dsymutil::DebugMapObject &
  162. SequenceTraits<std::vector<std::unique_ptr<dsymutil::DebugMapObject>>>::element(
  163. IO &, std::vector<std::unique_ptr<dsymutil::DebugMapObject>> &seq,
  164. size_t index) {
  165. if (index >= seq.size()) {
  166. seq.resize(index + 1);
  167. seq[index].reset(new dsymutil::DebugMapObject);
  168. }
  169. return *seq[index];
  170. }
  171. void MappingTraits<dsymutil::DebugMap>::mapping(IO &io,
  172. dsymutil::DebugMap &DM) {
  173. io.mapRequired("triple", DM.BinaryTriple);
  174. io.mapOptional("binary-path", DM.BinaryPath);
  175. if (void *Ctxt = io.getContext())
  176. reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM.BinaryTriple;
  177. io.mapOptional("objects", DM.Objects);
  178. }
  179. void MappingTraits<std::unique_ptr<dsymutil::DebugMap>>::mapping(
  180. IO &io, std::unique_ptr<dsymutil::DebugMap> &DM) {
  181. if (!DM)
  182. DM.reset(new DebugMap());
  183. io.mapRequired("triple", DM->BinaryTriple);
  184. io.mapOptional("binary-path", DM->BinaryPath);
  185. if (void *Ctxt = io.getContext())
  186. reinterpret_cast<YAMLContext *>(Ctxt)->BinaryTriple = DM->BinaryTriple;
  187. io.mapOptional("objects", DM->Objects);
  188. }
  189. MappingTraits<dsymutil::DebugMapObject>::YamlDMO::YamlDMO(
  190. IO &io, dsymutil::DebugMapObject &Obj) {
  191. Filename = Obj.Filename;
  192. Timestamp = sys::toTimeT(Obj.getTimestamp());
  193. Entries.reserve(Obj.Symbols.size());
  194. for (auto &Entry : Obj.Symbols)
  195. Entries.push_back(
  196. std::make_pair(std::string(Entry.getKey()), Entry.getValue()));
  197. }
  198. dsymutil::DebugMapObject
  199. MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
  200. BinaryHolder BinHolder(vfs::getRealFileSystem(), /* Verbose =*/false);
  201. const auto &Ctxt = *reinterpret_cast<YAMLContext *>(IO.getContext());
  202. SmallString<80> Path(Ctxt.PrependPath);
  203. StringMap<uint64_t> SymbolAddresses;
  204. sys::path::append(Path, Filename);
  205. auto ObjectEntry = BinHolder.getObjectEntry(Path);
  206. if (!ObjectEntry) {
  207. auto Err = ObjectEntry.takeError();
  208. WithColor::warning() << "Unable to open " << Path << " "
  209. << toString(std::move(Err)) << '\n';
  210. } else {
  211. auto Object = ObjectEntry->getObject(Ctxt.BinaryTriple);
  212. if (!Object) {
  213. auto Err = Object.takeError();
  214. WithColor::warning() << "Unable to open " << Path << " "
  215. << toString(std::move(Err)) << '\n';
  216. } else {
  217. for (const auto &Sym : Object->symbols()) {
  218. Expected<uint64_t> AddressOrErr = Sym.getValue();
  219. if (!AddressOrErr) {
  220. // TODO: Actually report errors helpfully.
  221. consumeError(AddressOrErr.takeError());
  222. continue;
  223. }
  224. Expected<StringRef> Name = Sym.getName();
  225. Expected<uint32_t> FlagsOrErr = Sym.getFlags();
  226. if (!Name || !FlagsOrErr ||
  227. (*FlagsOrErr & (SymbolRef::SF_Absolute | SymbolRef::SF_Common))) {
  228. // TODO: Actually report errors helpfully.
  229. if (!FlagsOrErr)
  230. consumeError(FlagsOrErr.takeError());
  231. if (!Name)
  232. consumeError(Name.takeError());
  233. continue;
  234. }
  235. SymbolAddresses[*Name] = *AddressOrErr;
  236. }
  237. }
  238. }
  239. dsymutil::DebugMapObject Res(Path, sys::toTimePoint(Timestamp), MachO::N_OSO);
  240. for (auto &Entry : Entries) {
  241. auto &Mapping = Entry.second;
  242. std::optional<uint64_t> ObjAddress;
  243. if (Mapping.ObjectAddress)
  244. ObjAddress = *Mapping.ObjectAddress;
  245. auto AddressIt = SymbolAddresses.find(Entry.first);
  246. if (AddressIt != SymbolAddresses.end())
  247. ObjAddress = AddressIt->getValue();
  248. Res.addSymbol(Entry.first, ObjAddress, Mapping.BinaryAddress, Mapping.Size);
  249. }
  250. return Res;
  251. }
  252. } // end namespace yaml
  253. } // end namespace llvm