DwarfTransformer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. //===- DwarfTransformer.cpp -----------------------------------------------===//
  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 <thread>
  9. #include <unordered_set>
  10. #include "llvm/DebugInfo/DIContext.h"
  11. #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
  12. #include "llvm/DebugInfo/DWARF/DWARFContext.h"
  13. #include "llvm/Support/Error.h"
  14. #include "llvm/Support/ThreadPool.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include "llvm/DebugInfo/GSYM/DwarfTransformer.h"
  17. #include "llvm/DebugInfo/GSYM/FunctionInfo.h"
  18. #include "llvm/DebugInfo/GSYM/GsymCreator.h"
  19. #include "llvm/DebugInfo/GSYM/GsymReader.h"
  20. #include "llvm/DebugInfo/GSYM/InlineInfo.h"
  21. #include <optional>
  22. using namespace llvm;
  23. using namespace gsym;
  24. struct llvm::gsym::CUInfo {
  25. const DWARFDebugLine::LineTable *LineTable;
  26. const char *CompDir;
  27. std::vector<uint32_t> FileCache;
  28. uint64_t Language = 0;
  29. uint8_t AddrSize = 0;
  30. CUInfo(DWARFContext &DICtx, DWARFCompileUnit *CU) {
  31. LineTable = DICtx.getLineTableForUnit(CU);
  32. CompDir = CU->getCompilationDir();
  33. FileCache.clear();
  34. if (LineTable)
  35. FileCache.assign(LineTable->Prologue.FileNames.size() + 1, UINT32_MAX);
  36. DWARFDie Die = CU->getUnitDIE();
  37. Language = dwarf::toUnsigned(Die.find(dwarf::DW_AT_language), 0);
  38. AddrSize = CU->getAddressByteSize();
  39. }
  40. /// Return true if Addr is the highest address for a given compile unit. The
  41. /// highest address is encoded as -1, of all ones in the address. These high
  42. /// addresses are used by some linkers to indicate that a function has been
  43. /// dead stripped or didn't end up in the linked executable.
  44. bool isHighestAddress(uint64_t Addr) const {
  45. if (AddrSize == 4)
  46. return Addr == UINT32_MAX;
  47. else if (AddrSize == 8)
  48. return Addr == UINT64_MAX;
  49. return false;
  50. }
  51. /// Convert a DWARF compile unit file index into a GSYM global file index.
  52. ///
  53. /// Each compile unit in DWARF has its own file table in the line table
  54. /// prologue. GSYM has a single large file table that applies to all files
  55. /// from all of the info in a GSYM file. This function converts between the
  56. /// two and caches and DWARF CU file index that has already been converted so
  57. /// the first client that asks for a compile unit file index will end up
  58. /// doing the conversion, and subsequent clients will get the cached GSYM
  59. /// index.
  60. uint32_t DWARFToGSYMFileIndex(GsymCreator &Gsym, uint32_t DwarfFileIdx) {
  61. if (!LineTable)
  62. return 0;
  63. assert(DwarfFileIdx < FileCache.size());
  64. uint32_t &GsymFileIdx = FileCache[DwarfFileIdx];
  65. if (GsymFileIdx != UINT32_MAX)
  66. return GsymFileIdx;
  67. std::string File;
  68. if (LineTable->getFileNameByIndex(
  69. DwarfFileIdx, CompDir,
  70. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File))
  71. GsymFileIdx = Gsym.insertFile(File);
  72. else
  73. GsymFileIdx = 0;
  74. return GsymFileIdx;
  75. }
  76. };
  77. static DWARFDie GetParentDeclContextDIE(DWARFDie &Die) {
  78. if (DWARFDie SpecDie =
  79. Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_specification)) {
  80. if (DWARFDie SpecParent = GetParentDeclContextDIE(SpecDie))
  81. return SpecParent;
  82. }
  83. if (DWARFDie AbstDie =
  84. Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_abstract_origin)) {
  85. if (DWARFDie AbstParent = GetParentDeclContextDIE(AbstDie))
  86. return AbstParent;
  87. }
  88. // We never want to follow parent for inlined subroutine - that would
  89. // give us information about where the function is inlined, not what
  90. // function is inlined
  91. if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine)
  92. return DWARFDie();
  93. DWARFDie ParentDie = Die.getParent();
  94. if (!ParentDie)
  95. return DWARFDie();
  96. switch (ParentDie.getTag()) {
  97. case dwarf::DW_TAG_namespace:
  98. case dwarf::DW_TAG_structure_type:
  99. case dwarf::DW_TAG_union_type:
  100. case dwarf::DW_TAG_class_type:
  101. case dwarf::DW_TAG_subprogram:
  102. return ParentDie; // Found parent decl context DIE
  103. case dwarf::DW_TAG_lexical_block:
  104. return GetParentDeclContextDIE(ParentDie);
  105. default:
  106. break;
  107. }
  108. return DWARFDie();
  109. }
  110. /// Get the GsymCreator string table offset for the qualified name for the
  111. /// DIE passed in. This function will avoid making copies of any strings in
  112. /// the GsymCreator when possible. We don't need to copy a string when the
  113. /// string comes from our .debug_str section or is an inlined string in the
  114. /// .debug_info. If we create a qualified name string in this function by
  115. /// combining multiple strings in the DWARF string table or info, we will make
  116. /// a copy of the string when we add it to the string table.
  117. static std::optional<uint32_t>
  118. getQualifiedNameIndex(DWARFDie &Die, uint64_t Language, GsymCreator &Gsym) {
  119. // If the dwarf has mangled name, use mangled name
  120. if (auto LinkageName =
  121. dwarf::toString(Die.findRecursively({dwarf::DW_AT_MIPS_linkage_name,
  122. dwarf::DW_AT_linkage_name}),
  123. nullptr))
  124. return Gsym.insertString(LinkageName, /* Copy */ false);
  125. StringRef ShortName(Die.getName(DINameKind::ShortName));
  126. if (ShortName.empty())
  127. return std::nullopt;
  128. // For C++ and ObjC, prepend names of all parent declaration contexts
  129. if (!(Language == dwarf::DW_LANG_C_plus_plus ||
  130. Language == dwarf::DW_LANG_C_plus_plus_03 ||
  131. Language == dwarf::DW_LANG_C_plus_plus_11 ||
  132. Language == dwarf::DW_LANG_C_plus_plus_14 ||
  133. Language == dwarf::DW_LANG_ObjC_plus_plus ||
  134. // This should not be needed for C, but we see C++ code marked as C
  135. // in some binaries. This should hurt, so let's do it for C as well
  136. Language == dwarf::DW_LANG_C))
  137. return Gsym.insertString(ShortName, /* Copy */ false);
  138. // Some GCC optimizations create functions with names ending with .isra.<num>
  139. // or .part.<num> and those names are just DW_AT_name, not DW_AT_linkage_name
  140. // If it looks like it could be the case, don't add any prefix
  141. if (ShortName.startswith("_Z") &&
  142. (ShortName.contains(".isra.") || ShortName.contains(".part.")))
  143. return Gsym.insertString(ShortName, /* Copy */ false);
  144. DWARFDie ParentDeclCtxDie = GetParentDeclContextDIE(Die);
  145. if (ParentDeclCtxDie) {
  146. std::string Name = ShortName.str();
  147. while (ParentDeclCtxDie) {
  148. StringRef ParentName(ParentDeclCtxDie.getName(DINameKind::ShortName));
  149. if (!ParentName.empty()) {
  150. // "lambda" names are wrapped in < >. Replace with { }
  151. // to be consistent with demangled names and not to confuse with
  152. // templates
  153. if (ParentName.front() == '<' && ParentName.back() == '>')
  154. Name = "{" + ParentName.substr(1, ParentName.size() - 2).str() + "}" +
  155. "::" + Name;
  156. else
  157. Name = ParentName.str() + "::" + Name;
  158. }
  159. ParentDeclCtxDie = GetParentDeclContextDIE(ParentDeclCtxDie);
  160. }
  161. // Copy the name since we created a new name in a std::string.
  162. return Gsym.insertString(Name, /* Copy */ true);
  163. }
  164. // Don't copy the name since it exists in the DWARF object file.
  165. return Gsym.insertString(ShortName, /* Copy */ false);
  166. }
  167. static bool hasInlineInfo(DWARFDie Die, uint32_t Depth) {
  168. bool CheckChildren = true;
  169. switch (Die.getTag()) {
  170. case dwarf::DW_TAG_subprogram:
  171. // Don't look into functions within functions.
  172. CheckChildren = Depth == 0;
  173. break;
  174. case dwarf::DW_TAG_inlined_subroutine:
  175. return true;
  176. default:
  177. break;
  178. }
  179. if (!CheckChildren)
  180. return false;
  181. for (DWARFDie ChildDie : Die.children()) {
  182. if (hasInlineInfo(ChildDie, Depth + 1))
  183. return true;
  184. }
  185. return false;
  186. }
  187. static void parseInlineInfo(GsymCreator &Gsym, CUInfo &CUI, DWARFDie Die,
  188. uint32_t Depth, FunctionInfo &FI,
  189. InlineInfo &parent) {
  190. if (!hasInlineInfo(Die, Depth))
  191. return;
  192. dwarf::Tag Tag = Die.getTag();
  193. if (Tag == dwarf::DW_TAG_inlined_subroutine) {
  194. // create new InlineInfo and append to parent.children
  195. InlineInfo II;
  196. DWARFAddressRange FuncRange =
  197. DWARFAddressRange(FI.startAddress(), FI.endAddress());
  198. Expected<DWARFAddressRangesVector> RangesOrError = Die.getAddressRanges();
  199. if (RangesOrError) {
  200. for (const DWARFAddressRange &Range : RangesOrError.get()) {
  201. // Check that the inlined function is within the range of the function
  202. // info, it might not be in case of split functions
  203. if (FuncRange.LowPC <= Range.LowPC && Range.HighPC <= FuncRange.HighPC)
  204. II.Ranges.insert(AddressRange(Range.LowPC, Range.HighPC));
  205. }
  206. }
  207. if (II.Ranges.empty())
  208. return;
  209. if (auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym))
  210. II.Name = *NameIndex;
  211. II.CallFile = CUI.DWARFToGSYMFileIndex(
  212. Gsym, dwarf::toUnsigned(Die.find(dwarf::DW_AT_call_file), 0));
  213. II.CallLine = dwarf::toUnsigned(Die.find(dwarf::DW_AT_call_line), 0);
  214. // parse all children and append to parent
  215. for (DWARFDie ChildDie : Die.children())
  216. parseInlineInfo(Gsym, CUI, ChildDie, Depth + 1, FI, II);
  217. parent.Children.emplace_back(std::move(II));
  218. return;
  219. }
  220. if (Tag == dwarf::DW_TAG_subprogram || Tag == dwarf::DW_TAG_lexical_block) {
  221. // skip this Die and just recurse down
  222. for (DWARFDie ChildDie : Die.children())
  223. parseInlineInfo(Gsym, CUI, ChildDie, Depth + 1, FI, parent);
  224. }
  225. }
  226. static void convertFunctionLineTable(raw_ostream &Log, CUInfo &CUI,
  227. DWARFDie Die, GsymCreator &Gsym,
  228. FunctionInfo &FI) {
  229. std::vector<uint32_t> RowVector;
  230. const uint64_t StartAddress = FI.startAddress();
  231. const uint64_t EndAddress = FI.endAddress();
  232. const uint64_t RangeSize = EndAddress - StartAddress;
  233. const object::SectionedAddress SecAddress{
  234. StartAddress, object::SectionedAddress::UndefSection};
  235. if (!CUI.LineTable->lookupAddressRange(SecAddress, RangeSize, RowVector)) {
  236. // If we have a DW_TAG_subprogram but no line entries, fall back to using
  237. // the DW_AT_decl_file an d DW_AT_decl_line if we have both attributes.
  238. std::string FilePath = Die.getDeclFile(
  239. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath);
  240. if (FilePath.empty())
  241. return;
  242. if (auto Line =
  243. dwarf::toUnsigned(Die.findRecursively({dwarf::DW_AT_decl_line}))) {
  244. LineEntry LE(StartAddress, Gsym.insertFile(FilePath), *Line);
  245. FI.OptLineTable = LineTable();
  246. FI.OptLineTable->push(LE);
  247. }
  248. return;
  249. }
  250. FI.OptLineTable = LineTable();
  251. DWARFDebugLine::Row PrevRow;
  252. for (uint32_t RowIndex : RowVector) {
  253. // Take file number and line/column from the row.
  254. const DWARFDebugLine::Row &Row = CUI.LineTable->Rows[RowIndex];
  255. const uint32_t FileIdx = CUI.DWARFToGSYMFileIndex(Gsym, Row.File);
  256. uint64_t RowAddress = Row.Address.Address;
  257. // Watch out for a RowAddress that is in the middle of a line table entry
  258. // in the DWARF. If we pass an address in between two line table entries
  259. // we will get a RowIndex for the previous valid line table row which won't
  260. // be contained in our function. This is usually a bug in the DWARF due to
  261. // linker problems or LTO or other DWARF re-linking so it is worth emitting
  262. // an error, but not worth stopping the creation of the GSYM.
  263. if (!FI.Range.contains(RowAddress)) {
  264. if (RowAddress < FI.Range.start()) {
  265. Log << "error: DIE has a start address whose LowPC is between the "
  266. "line table Row[" << RowIndex << "] with address "
  267. << HEX64(RowAddress) << " and the next one.\n";
  268. Die.dump(Log, 0, DIDumpOptions::getForSingleDIE());
  269. RowAddress = FI.Range.start();
  270. } else {
  271. continue;
  272. }
  273. }
  274. LineEntry LE(RowAddress, FileIdx, Row.Line);
  275. if (RowIndex != RowVector[0] && Row.Address < PrevRow.Address) {
  276. // We have seen full duplicate line tables for functions in some
  277. // DWARF files. Watch for those here by checking the the last
  278. // row was the function's end address (HighPC) and that the
  279. // current line table entry's address is the same as the first
  280. // line entry we already have in our "function_info.Lines". If
  281. // so break out after printing a warning.
  282. auto FirstLE = FI.OptLineTable->first();
  283. if (FirstLE && *FirstLE == LE) {
  284. if (!Gsym.isQuiet()) {
  285. Log << "warning: duplicate line table detected for DIE:\n";
  286. Die.dump(Log, 0, DIDumpOptions::getForSingleDIE());
  287. }
  288. } else {
  289. // Print out (ignore if os == nulls as this is expensive)
  290. Log << "error: line table has addresses that do not "
  291. << "monotonically increase:\n";
  292. for (uint32_t RowIndex2 : RowVector) {
  293. CUI.LineTable->Rows[RowIndex2].dump(Log);
  294. }
  295. Die.dump(Log, 0, DIDumpOptions::getForSingleDIE());
  296. }
  297. break;
  298. }
  299. // Skip multiple line entries for the same file and line.
  300. auto LastLE = FI.OptLineTable->last();
  301. if (LastLE && LastLE->File == FileIdx && LastLE->Line == Row.Line)
  302. continue;
  303. // Only push a row if it isn't an end sequence. End sequence markers are
  304. // included for the last address in a function or the last contiguous
  305. // address in a sequence.
  306. if (Row.EndSequence) {
  307. // End sequence means that the next line entry could have a lower address
  308. // that the previous entries. So we clear the previous row so we don't
  309. // trigger the line table error about address that do not monotonically
  310. // increase.
  311. PrevRow = DWARFDebugLine::Row();
  312. } else {
  313. FI.OptLineTable->push(LE);
  314. PrevRow = Row;
  315. }
  316. }
  317. // If not line table rows were added, clear the line table so we don't encode
  318. // on in the GSYM file.
  319. if (FI.OptLineTable->empty())
  320. FI.OptLineTable = std::nullopt;
  321. }
  322. void DwarfTransformer::handleDie(raw_ostream &OS, CUInfo &CUI, DWARFDie Die) {
  323. switch (Die.getTag()) {
  324. case dwarf::DW_TAG_subprogram: {
  325. Expected<DWARFAddressRangesVector> RangesOrError = Die.getAddressRanges();
  326. if (!RangesOrError) {
  327. consumeError(RangesOrError.takeError());
  328. break;
  329. }
  330. const DWARFAddressRangesVector &Ranges = RangesOrError.get();
  331. if (Ranges.empty())
  332. break;
  333. auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym);
  334. if (!NameIndex) {
  335. OS << "error: function at " << HEX64(Die.getOffset())
  336. << " has no name\n ";
  337. Die.dump(OS, 0, DIDumpOptions::getForSingleDIE());
  338. break;
  339. }
  340. // Create a function_info for each range
  341. for (const DWARFAddressRange &Range : Ranges) {
  342. // The low PC must be less than the high PC. Many linkers don't remove
  343. // DWARF for functions that don't get linked into the final executable.
  344. // If both the high and low pc have relocations, linkers will often set
  345. // the address values for both to the same value to indicate the function
  346. // has been remove. Other linkers have been known to set the one or both
  347. // PC values to a UINT32_MAX for 4 byte addresses and UINT64_MAX for 8
  348. // byte addresses to indicate the function isn't valid. The check below
  349. // tries to watch for these cases and abort if it runs into them.
  350. if (Range.LowPC >= Range.HighPC || CUI.isHighestAddress(Range.LowPC))
  351. break;
  352. // Many linkers can't remove DWARF and might set the LowPC to zero. Since
  353. // high PC can be an offset from the low PC in more recent DWARF versions
  354. // we need to watch for a zero'ed low pc which we do using
  355. // ValidTextRanges below.
  356. if (!Gsym.IsValidTextAddress(Range.LowPC)) {
  357. // We expect zero and -1 to be invalid addresses in DWARF depending
  358. // on the linker of the DWARF. This indicates a function was stripped
  359. // and the debug info wasn't able to be stripped from the DWARF. If
  360. // the LowPC isn't zero or -1, then we should emit an error.
  361. if (Range.LowPC != 0) {
  362. if (!Gsym.isQuiet()) {
  363. // Unexpected invalid address, emit a warning
  364. OS << "warning: DIE has an address range whose start address is "
  365. "not in any executable sections ("
  366. << *Gsym.GetValidTextRanges()
  367. << ") and will not be processed:\n";
  368. Die.dump(OS, 0, DIDumpOptions::getForSingleDIE());
  369. }
  370. }
  371. break;
  372. }
  373. FunctionInfo FI;
  374. FI.Range = {Range.LowPC, Range.HighPC};
  375. FI.Name = *NameIndex;
  376. if (CUI.LineTable) {
  377. convertFunctionLineTable(OS, CUI, Die, Gsym, FI);
  378. }
  379. if (hasInlineInfo(Die, 0)) {
  380. FI.Inline = InlineInfo();
  381. FI.Inline->Name = *NameIndex;
  382. FI.Inline->Ranges.insert(FI.Range);
  383. parseInlineInfo(Gsym, CUI, Die, 0, FI, *FI.Inline);
  384. }
  385. Gsym.addFunctionInfo(std::move(FI));
  386. }
  387. } break;
  388. default:
  389. break;
  390. }
  391. for (DWARFDie ChildDie : Die.children())
  392. handleDie(OS, CUI, ChildDie);
  393. }
  394. Error DwarfTransformer::convert(uint32_t NumThreads) {
  395. size_t NumBefore = Gsym.getNumFunctionInfos();
  396. auto getDie = [&](DWARFUnit &DwarfUnit) -> DWARFDie {
  397. DWARFDie ReturnDie = DwarfUnit.getUnitDIE(false);
  398. if (std::optional<uint64_t> DWOId = DwarfUnit.getDWOId()) {
  399. DWARFUnit *DWOCU = DwarfUnit.getNonSkeletonUnitDIE(false).getDwarfUnit();
  400. if (!DWOCU->isDWOUnit()) {
  401. std::string DWOName = dwarf::toString(
  402. DwarfUnit.getUnitDIE().find(
  403. {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
  404. "");
  405. Log << "warning: Unable to retrieve DWO .debug_info section for "
  406. << DWOName << "\n";
  407. } else {
  408. ReturnDie = DWOCU->getUnitDIE(false);
  409. }
  410. }
  411. return ReturnDie;
  412. };
  413. if (NumThreads == 1) {
  414. // Parse all DWARF data from this thread, use the same string/file table
  415. // for everything
  416. for (const auto &CU : DICtx.compile_units()) {
  417. DWARFDie Die = getDie(*CU);
  418. CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
  419. handleDie(Log, CUI, Die);
  420. }
  421. } else {
  422. // LLVM Dwarf parser is not thread-safe and we need to parse all DWARF up
  423. // front before we start accessing any DIEs since there might be
  424. // cross compile unit references in the DWARF. If we don't do this we can
  425. // end up crashing.
  426. // We need to call getAbbreviations sequentially first so that getUnitDIE()
  427. // only works with its local data.
  428. for (const auto &CU : DICtx.compile_units())
  429. CU->getAbbreviations();
  430. // Now parse all DIEs in case we have cross compile unit references in a
  431. // thread pool.
  432. ThreadPool pool(hardware_concurrency(NumThreads));
  433. for (const auto &CU : DICtx.compile_units())
  434. pool.async([&CU]() { CU->getUnitDIE(false /*CUDieOnly*/); });
  435. pool.wait();
  436. // Now convert all DWARF to GSYM in a thread pool.
  437. std::mutex LogMutex;
  438. for (const auto &CU : DICtx.compile_units()) {
  439. DWARFDie Die = getDie(*CU);
  440. if (Die) {
  441. CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
  442. pool.async([this, CUI, &LogMutex, Die]() mutable {
  443. std::string ThreadLogStorage;
  444. raw_string_ostream ThreadOS(ThreadLogStorage);
  445. handleDie(ThreadOS, CUI, Die);
  446. ThreadOS.flush();
  447. if (!ThreadLogStorage.empty()) {
  448. // Print ThreadLogStorage lines into an actual stream under a lock
  449. std::lock_guard<std::mutex> guard(LogMutex);
  450. Log << ThreadLogStorage;
  451. }
  452. });
  453. }
  454. }
  455. pool.wait();
  456. }
  457. size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
  458. Log << "Loaded " << FunctionsAddedCount << " functions from DWARF.\n";
  459. return Error::success();
  460. }
  461. llvm::Error DwarfTransformer::verify(StringRef GsymPath) {
  462. Log << "Verifying GSYM file \"" << GsymPath << "\":\n";
  463. auto Gsym = GsymReader::openFile(GsymPath);
  464. if (!Gsym)
  465. return Gsym.takeError();
  466. auto NumAddrs = Gsym->getNumAddresses();
  467. DILineInfoSpecifier DLIS(
  468. DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
  469. DILineInfoSpecifier::FunctionNameKind::LinkageName);
  470. std::string gsymFilename;
  471. for (uint32_t I = 0; I < NumAddrs; ++I) {
  472. auto FuncAddr = Gsym->getAddress(I);
  473. if (!FuncAddr)
  474. return createStringError(std::errc::invalid_argument,
  475. "failed to extract address[%i]", I);
  476. auto FI = Gsym->getFunctionInfo(*FuncAddr);
  477. if (!FI)
  478. return createStringError(std::errc::invalid_argument,
  479. "failed to extract function info for address 0x%"
  480. PRIu64, *FuncAddr);
  481. for (auto Addr = *FuncAddr; Addr < *FuncAddr + FI->size(); ++Addr) {
  482. const object::SectionedAddress SectAddr{
  483. Addr, object::SectionedAddress::UndefSection};
  484. auto LR = Gsym->lookup(Addr);
  485. if (!LR)
  486. return LR.takeError();
  487. auto DwarfInlineInfos =
  488. DICtx.getInliningInfoForAddress(SectAddr, DLIS);
  489. uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames();
  490. if (NumDwarfInlineInfos == 0) {
  491. DwarfInlineInfos.addFrame(
  492. DICtx.getLineInfoForAddress(SectAddr, DLIS));
  493. }
  494. // Check for 1 entry that has no file and line info
  495. if (NumDwarfInlineInfos == 1 &&
  496. DwarfInlineInfos.getFrame(0).FileName == "<invalid>") {
  497. DwarfInlineInfos = DIInliningInfo();
  498. NumDwarfInlineInfos = 0;
  499. }
  500. if (NumDwarfInlineInfos > 0 &&
  501. NumDwarfInlineInfos != LR->Locations.size()) {
  502. Log << "error: address " << HEX64(Addr) << " has "
  503. << NumDwarfInlineInfos << " DWARF inline frames and GSYM has "
  504. << LR->Locations.size() << "\n";
  505. Log << " " << NumDwarfInlineInfos << " DWARF frames:\n";
  506. for (size_t Idx = 0; Idx < NumDwarfInlineInfos; ++Idx) {
  507. const auto &dii = DwarfInlineInfos.getFrame(Idx);
  508. Log << " [" << Idx << "]: " << dii.FunctionName << " @ "
  509. << dii.FileName << ':' << dii.Line << '\n';
  510. }
  511. Log << " " << LR->Locations.size() << " GSYM frames:\n";
  512. for (size_t Idx = 0, count = LR->Locations.size();
  513. Idx < count; ++Idx) {
  514. const auto &gii = LR->Locations[Idx];
  515. Log << " [" << Idx << "]: " << gii.Name << " @ " << gii.Dir
  516. << '/' << gii.Base << ':' << gii.Line << '\n';
  517. }
  518. DwarfInlineInfos = DICtx.getInliningInfoForAddress(SectAddr, DLIS);
  519. Gsym->dump(Log, *FI);
  520. continue;
  521. }
  522. for (size_t Idx = 0, count = LR->Locations.size(); Idx < count;
  523. ++Idx) {
  524. const auto &gii = LR->Locations[Idx];
  525. if (Idx < NumDwarfInlineInfos) {
  526. const auto &dii = DwarfInlineInfos.getFrame(Idx);
  527. gsymFilename = LR->getSourceFile(Idx);
  528. // Verify function name
  529. if (dii.FunctionName.find(gii.Name.str()) != 0)
  530. Log << "error: address " << HEX64(Addr) << " DWARF function \""
  531. << dii.FunctionName.c_str()
  532. << "\" doesn't match GSYM function \"" << gii.Name << "\"\n";
  533. // Verify source file path
  534. if (dii.FileName != gsymFilename)
  535. Log << "error: address " << HEX64(Addr) << " DWARF path \""
  536. << dii.FileName.c_str() << "\" doesn't match GSYM path \""
  537. << gsymFilename.c_str() << "\"\n";
  538. // Verify source file line
  539. if (dii.Line != gii.Line)
  540. Log << "error: address " << HEX64(Addr) << " DWARF line "
  541. << dii.Line << " != GSYM line " << gii.Line << "\n";
  542. }
  543. }
  544. }
  545. }
  546. return Error::success();
  547. }