DwarfTransformer.cpp 23 KB

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