LLVM 18.0.0git
DwarfTransformer.cpp
Go to the documentation of this file.
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
9#include <thread>
10#include <unordered_set>
11
15#include "llvm/Support/Error.h"
18
24#include <optional>
25
26using namespace llvm;
27using namespace gsym;
28
31 const char *CompDir;
32 std::vector<uint32_t> FileCache;
34 uint8_t AddrSize = 0;
35
38 CompDir = CU->getCompilationDir();
39 FileCache.clear();
40 if (LineTable)
41 FileCache.assign(LineTable->Prologue.FileNames.size() + 1, UINT32_MAX);
42 DWARFDie Die = CU->getUnitDIE();
43 Language = dwarf::toUnsigned(Die.find(dwarf::DW_AT_language), 0);
44 AddrSize = CU->getAddressByteSize();
45 }
46
47 /// Return true if Addr is the highest address for a given compile unit. The
48 /// highest address is encoded as -1, of all ones in the address. These high
49 /// addresses are used by some linkers to indicate that a function has been
50 /// dead stripped or didn't end up in the linked executable.
52 if (AddrSize == 4)
53 return Addr == UINT32_MAX;
54 else if (AddrSize == 8)
55 return Addr == UINT64_MAX;
56 return false;
57 }
58
59 /// Convert a DWARF compile unit file index into a GSYM global file index.
60 ///
61 /// Each compile unit in DWARF has its own file table in the line table
62 /// prologue. GSYM has a single large file table that applies to all files
63 /// from all of the info in a GSYM file. This function converts between the
64 /// two and caches and DWARF CU file index that has already been converted so
65 /// the first client that asks for a compile unit file index will end up
66 /// doing the conversion, and subsequent clients will get the cached GSYM
67 /// index.
69 if (!LineTable)
70 return 0;
71 assert(DwarfFileIdx < FileCache.size());
72 uint32_t &GsymFileIdx = FileCache[DwarfFileIdx];
73 if (GsymFileIdx != UINT32_MAX)
74 return GsymFileIdx;
75 std::string File;
76 if (LineTable->getFileNameByIndex(
77 DwarfFileIdx, CompDir,
78 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File))
79 GsymFileIdx = Gsym.insertFile(File);
80 else
81 GsymFileIdx = 0;
82 return GsymFileIdx;
83 }
84};
85
86
88 if (DWARFDie SpecDie =
89 Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_specification)) {
90 if (DWARFDie SpecParent = GetParentDeclContextDIE(SpecDie))
91 return SpecParent;
92 }
93 if (DWARFDie AbstDie =
94 Die.getAttributeValueAsReferencedDie(dwarf::DW_AT_abstract_origin)) {
95 if (DWARFDie AbstParent = GetParentDeclContextDIE(AbstDie))
96 return AbstParent;
97 }
98
99 // We never want to follow parent for inlined subroutine - that would
100 // give us information about where the function is inlined, not what
101 // function is inlined
102 if (Die.getTag() == dwarf::DW_TAG_inlined_subroutine)
103 return DWARFDie();
104
105 DWARFDie ParentDie = Die.getParent();
106 if (!ParentDie)
107 return DWARFDie();
108
109 switch (ParentDie.getTag()) {
110 case dwarf::DW_TAG_namespace:
111 case dwarf::DW_TAG_structure_type:
112 case dwarf::DW_TAG_union_type:
113 case dwarf::DW_TAG_class_type:
114 case dwarf::DW_TAG_subprogram:
115 return ParentDie; // Found parent decl context DIE
116 case dwarf::DW_TAG_lexical_block:
117 return GetParentDeclContextDIE(ParentDie);
118 default:
119 break;
120 }
121
122 return DWARFDie();
123}
124
125/// Get the GsymCreator string table offset for the qualified name for the
126/// DIE passed in. This function will avoid making copies of any strings in
127/// the GsymCreator when possible. We don't need to copy a string when the
128/// string comes from our .debug_str section or is an inlined string in the
129/// .debug_info. If we create a qualified name string in this function by
130/// combining multiple strings in the DWARF string table or info, we will make
131/// a copy of the string when we add it to the string table.
132static std::optional<uint32_t>
134 // If the dwarf has mangled name, use mangled name
135 if (auto LinkageName =
136 dwarf::toString(Die.findRecursively({dwarf::DW_AT_MIPS_linkage_name,
137 dwarf::DW_AT_linkage_name}),
138 nullptr))
139 return Gsym.insertString(LinkageName, /* Copy */ false);
140
141 StringRef ShortName(Die.getName(DINameKind::ShortName));
142 if (ShortName.empty())
143 return std::nullopt;
144
145 // For C++ and ObjC, prepend names of all parent declaration contexts
146 if (!(Language == dwarf::DW_LANG_C_plus_plus ||
147 Language == dwarf::DW_LANG_C_plus_plus_03 ||
148 Language == dwarf::DW_LANG_C_plus_plus_11 ||
149 Language == dwarf::DW_LANG_C_plus_plus_14 ||
150 Language == dwarf::DW_LANG_ObjC_plus_plus ||
151 // This should not be needed for C, but we see C++ code marked as C
152 // in some binaries. This should hurt, so let's do it for C as well
153 Language == dwarf::DW_LANG_C))
154 return Gsym.insertString(ShortName, /* Copy */ false);
155
156 // Some GCC optimizations create functions with names ending with .isra.<num>
157 // or .part.<num> and those names are just DW_AT_name, not DW_AT_linkage_name
158 // If it looks like it could be the case, don't add any prefix
159 if (ShortName.startswith("_Z") &&
160 (ShortName.contains(".isra.") || ShortName.contains(".part.")))
161 return Gsym.insertString(ShortName, /* Copy */ false);
162
163 DWARFDie ParentDeclCtxDie = GetParentDeclContextDIE(Die);
164 if (ParentDeclCtxDie) {
165 std::string Name = ShortName.str();
166 while (ParentDeclCtxDie) {
167 StringRef ParentName(ParentDeclCtxDie.getName(DINameKind::ShortName));
168 if (!ParentName.empty()) {
169 // "lambda" names are wrapped in < >. Replace with { }
170 // to be consistent with demangled names and not to confuse with
171 // templates
172 if (ParentName.front() == '<' && ParentName.back() == '>')
173 Name = "{" + ParentName.substr(1, ParentName.size() - 2).str() + "}" +
174 "::" + Name;
175 else
176 Name = ParentName.str() + "::" + Name;
177 }
178 ParentDeclCtxDie = GetParentDeclContextDIE(ParentDeclCtxDie);
179 }
180 // Copy the name since we created a new name in a std::string.
181 return Gsym.insertString(Name, /* Copy */ true);
182 }
183 // Don't copy the name since it exists in the DWARF object file.
184 return Gsym.insertString(ShortName, /* Copy */ false);
185}
186
188 bool CheckChildren = true;
189 switch (Die.getTag()) {
190 case dwarf::DW_TAG_subprogram:
191 // Don't look into functions within functions.
192 CheckChildren = Depth == 0;
193 break;
194 case dwarf::DW_TAG_inlined_subroutine:
195 return true;
196 default:
197 break;
198 }
199 if (!CheckChildren)
200 return false;
201 for (DWARFDie ChildDie : Die.children()) {
202 if (hasInlineInfo(ChildDie, Depth + 1))
203 return true;
204 }
205 return false;
206}
207
208static AddressRanges
210 AddressRanges Ranges;
211 for (const DWARFAddressRange &DwarfRange : DwarfRanges) {
212 if (DwarfRange.LowPC < DwarfRange.HighPC)
213 Ranges.insert({DwarfRange.LowPC, DwarfRange.HighPC});
214 }
215 return Ranges;
216}
217
218static void parseInlineInfo(GsymCreator &Gsym, raw_ostream *Log, CUInfo &CUI,
220 InlineInfo &Parent,
221 const AddressRanges &AllParentRanges,
222 bool &WarnIfEmpty) {
223 if (!hasInlineInfo(Die, Depth))
224 return;
225
226 dwarf::Tag Tag = Die.getTag();
227 if (Tag == dwarf::DW_TAG_inlined_subroutine) {
228 // create new InlineInfo and append to parent.children
229 InlineInfo II;
230 AddressRanges AllInlineRanges;
232 if (RangesOrError) {
233 AllInlineRanges = ConvertDWARFRanges(RangesOrError.get());
234 uint32_t EmptyCount = 0;
235 for (const AddressRange &InlineRange : AllInlineRanges) {
236 // Check for empty inline range in case inline function was outlined
237 // or has not code
238 if (InlineRange.empty()) {
239 ++EmptyCount;
240 } else {
241 if (Parent.Ranges.contains(InlineRange)) {
242 II.Ranges.insert(InlineRange);
243 } else {
244 // Only warn if the current inline range is not within any of all
245 // of the parent ranges. If we have a DW_TAG_subpgram with multiple
246 // ranges we will emit a FunctionInfo for each range of that
247 // function that only emits information within the current range,
248 // so we only want to emit an error if the DWARF has issues, not
249 // when a range currently just isn't in the range we are currently
250 // parsing for.
251 if (AllParentRanges.contains(InlineRange)) {
252 WarnIfEmpty = false;
253 } else if (Log) {
254 *Log << "error: inlined function DIE at "
255 << HEX32(Die.getOffset()) << " has a range ["
256 << HEX64(InlineRange.start()) << " - "
257 << HEX64(InlineRange.end()) << ") that isn't contained in "
258 << "any parent address ranges, this inline range will be "
259 "removed.\n";
260 }
261 }
262 }
263 }
264 // If we have all empty ranges for the inlines, then don't warn if we
265 // have an empty InlineInfo at the top level as all inline functions
266 // were elided.
267 if (EmptyCount == AllInlineRanges.size())
268 WarnIfEmpty = false;
269 }
270 if (II.Ranges.empty())
271 return;
272
273 if (auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym))
274 II.Name = *NameIndex;
276 Gsym, dwarf::toUnsigned(Die.find(dwarf::DW_AT_call_file), 0));
277 II.CallLine = dwarf::toUnsigned(Die.find(dwarf::DW_AT_call_line), 0);
278 // parse all children and append to parent
279 for (DWARFDie ChildDie : Die.children())
280 parseInlineInfo(Gsym, Log, CUI, ChildDie, Depth + 1, FI, II,
281 AllInlineRanges, WarnIfEmpty);
282 Parent.Children.emplace_back(std::move(II));
283 return;
284 }
285 if (Tag == dwarf::DW_TAG_subprogram || Tag == dwarf::DW_TAG_lexical_block) {
286 // skip this Die and just recurse down
287 for (DWARFDie ChildDie : Die.children())
288 parseInlineInfo(Gsym, Log, CUI, ChildDie, Depth + 1, FI, Parent,
289 AllParentRanges, WarnIfEmpty);
290 }
291}
292
294 DWARFDie Die, GsymCreator &Gsym,
295 FunctionInfo &FI) {
296 std::vector<uint32_t> RowVector;
297 const uint64_t StartAddress = FI.startAddress();
298 const uint64_t EndAddress = FI.endAddress();
299 const uint64_t RangeSize = EndAddress - StartAddress;
300 const object::SectionedAddress SecAddress{
302
303
304 if (!CUI.LineTable->lookupAddressRange(SecAddress, RangeSize, RowVector)) {
305 // If we have a DW_TAG_subprogram but no line entries, fall back to using
306 // the DW_AT_decl_file an d DW_AT_decl_line if we have both attributes.
307 std::string FilePath = Die.getDeclFile(
308 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath);
309 if (FilePath.empty())
310 return;
311 if (auto Line =
312 dwarf::toUnsigned(Die.findRecursively({dwarf::DW_AT_decl_line}))) {
313 LineEntry LE(StartAddress, Gsym.insertFile(FilePath), *Line);
314 FI.OptLineTable = LineTable();
315 FI.OptLineTable->push(LE);
316 }
317 return;
318 }
319
320 FI.OptLineTable = LineTable();
321 DWARFDebugLine::Row PrevRow;
322 for (uint32_t RowIndex : RowVector) {
323 // Take file number and line/column from the row.
324 const DWARFDebugLine::Row &Row = CUI.LineTable->Rows[RowIndex];
325 const uint32_t FileIdx = CUI.DWARFToGSYMFileIndex(Gsym, Row.File);
326 uint64_t RowAddress = Row.Address.Address;
327 // Watch out for a RowAddress that is in the middle of a line table entry
328 // in the DWARF. If we pass an address in between two line table entries
329 // we will get a RowIndex for the previous valid line table row which won't
330 // be contained in our function. This is usually a bug in the DWARF due to
331 // linker problems or LTO or other DWARF re-linking so it is worth emitting
332 // an error, but not worth stopping the creation of the GSYM.
333 if (!FI.Range.contains(RowAddress)) {
334 if (RowAddress < FI.Range.start()) {
335 if (Log) {
336 *Log << "error: DIE has a start address whose LowPC is between the "
337 "line table Row[" << RowIndex << "] with address "
338 << HEX64(RowAddress) << " and the next one.\n";
339 Die.dump(*Log, 0, DIDumpOptions::getForSingleDIE());
340 }
341 RowAddress = FI.Range.start();
342 } else {
343 continue;
344 }
345 }
346
347 LineEntry LE(RowAddress, FileIdx, Row.Line);
348 if (RowIndex != RowVector[0] && Row.Address < PrevRow.Address) {
349 // We have seen full duplicate line tables for functions in some
350 // DWARF files. Watch for those here by checking the last
351 // row was the function's end address (HighPC) and that the
352 // current line table entry's address is the same as the first
353 // line entry we already have in our "function_info.Lines". If
354 // so break out after printing a warning.
355 auto FirstLE = FI.OptLineTable->first();
356 if (FirstLE && *FirstLE == LE) {
357 if (Log && !Gsym.isQuiet()) {
358 *Log << "warning: duplicate line table detected for DIE:\n";
359 Die.dump(*Log, 0, DIDumpOptions::getForSingleDIE());
360 }
361 } else {
362 if (Log) {
363 *Log << "error: line table has addresses that do not "
364 << "monotonically increase:\n";
365 for (uint32_t RowIndex2 : RowVector)
366 CUI.LineTable->Rows[RowIndex2].dump(*Log);
367 Die.dump(*Log, 0, DIDumpOptions::getForSingleDIE());
368 }
369 }
370 break;
371 }
372
373 // Skip multiple line entries for the same file and line.
374 auto LastLE = FI.OptLineTable->last();
375 if (LastLE && LastLE->File == FileIdx && LastLE->Line == Row.Line)
376 continue;
377 // Only push a row if it isn't an end sequence. End sequence markers are
378 // included for the last address in a function or the last contiguous
379 // address in a sequence.
380 if (Row.EndSequence) {
381 // End sequence means that the next line entry could have a lower address
382 // that the previous entries. So we clear the previous row so we don't
383 // trigger the line table error about address that do not monotonically
384 // increase.
385 PrevRow = DWARFDebugLine::Row();
386 } else {
387 FI.OptLineTable->push(LE);
388 PrevRow = Row;
389 }
390 }
391 // If not line table rows were added, clear the line table so we don't encode
392 // on in the GSYM file.
393 if (FI.OptLineTable->empty())
394 FI.OptLineTable = std::nullopt;
395}
396
397void DwarfTransformer::handleDie(raw_ostream *OS, CUInfo &CUI, DWARFDie Die) {
398 switch (Die.getTag()) {
399 case dwarf::DW_TAG_subprogram: {
401 if (!RangesOrError) {
402 consumeError(RangesOrError.takeError());
403 break;
404 }
405 const DWARFAddressRangesVector &Ranges = RangesOrError.get();
406 if (Ranges.empty())
407 break;
408 auto NameIndex = getQualifiedNameIndex(Die, CUI.Language, Gsym);
409 if (!NameIndex) {
410 if (OS) {
411 *OS << "error: function at " << HEX64(Die.getOffset())
412 << " has no name\n ";
414 }
415 break;
416 }
417 // All ranges for the subprogram DIE in case it has multiple. We need to
418 // pass this down into parseInlineInfo so we don't warn about inline
419 // ranges that are not in the current subrange of a function when they
420 // actually are in another subgrange. We do this because when a function
421 // has discontiguos ranges, we create multiple function entries with only
422 // the info for that range contained inside of it.
423 AddressRanges AllSubprogramRanges = ConvertDWARFRanges(Ranges);
424
425 // Create a function_info for each range
426 for (const DWARFAddressRange &Range : Ranges) {
427 // The low PC must be less than the high PC. Many linkers don't remove
428 // DWARF for functions that don't get linked into the final executable.
429 // If both the high and low pc have relocations, linkers will often set
430 // the address values for both to the same value to indicate the function
431 // has been remove. Other linkers have been known to set the one or both
432 // PC values to a UINT32_MAX for 4 byte addresses and UINT64_MAX for 8
433 // byte addresses to indicate the function isn't valid. The check below
434 // tries to watch for these cases and abort if it runs into them.
435 if (Range.LowPC >= Range.HighPC || CUI.isHighestAddress(Range.LowPC))
436 break;
437
438 // Many linkers can't remove DWARF and might set the LowPC to zero. Since
439 // high PC can be an offset from the low PC in more recent DWARF versions
440 // we need to watch for a zero'ed low pc which we do using
441 // ValidTextRanges below.
442 if (!Gsym.IsValidTextAddress(Range.LowPC)) {
443 // We expect zero and -1 to be invalid addresses in DWARF depending
444 // on the linker of the DWARF. This indicates a function was stripped
445 // and the debug info wasn't able to be stripped from the DWARF. If
446 // the LowPC isn't zero or -1, then we should emit an error.
447 if (Range.LowPC != 0) {
448 if (!Gsym.isQuiet()) {
449 // Unexpected invalid address, emit a warning
450 if (OS) {
451 *OS << "warning: DIE has an address range whose start address "
452 "is not in any executable sections ("
453 << *Gsym.GetValidTextRanges()
454 << ") and will not be processed:\n";
456 }
457 }
458 }
459 break;
460 }
461
462 FunctionInfo FI;
463 FI.Range = {Range.LowPC, Range.HighPC};
464 FI.Name = *NameIndex;
465 if (CUI.LineTable)
466 convertFunctionLineTable(OS, CUI, Die, Gsym, FI);
467
468 if (hasInlineInfo(Die, 0)) {
469 FI.Inline = InlineInfo();
470 FI.Inline->Name = *NameIndex;
471 FI.Inline->Ranges.insert(FI.Range);
472 bool WarnIfEmpty = true;
473 parseInlineInfo(Gsym, OS, CUI, Die, 0, FI, *FI.Inline,
474 AllSubprogramRanges, WarnIfEmpty);
475 // Make sure we at least got some valid inline info other than just
476 // the top level function. If we didn't then remove the inline info
477 // from the function info. We have seen cases where LTO tries to modify
478 // the DWARF for functions and it messes up the address ranges for
479 // the inline functions so it is no longer valid.
480 //
481 // By checking if there are any valid children on the top level inline
482 // information object, we will know if we got anything valid from the
483 // debug info.
484 if (FI.Inline->Children.empty()) {
485 if (WarnIfEmpty && OS && !Gsym.isQuiet()) {
486 *OS << "warning: DIE contains inline function information that has "
487 "no valid ranges, removing inline information:\n";
489 }
490 FI.Inline = std::nullopt;
491 }
492 }
493 Gsym.addFunctionInfo(std::move(FI));
494 }
495 } break;
496 default:
497 break;
498 }
499 for (DWARFDie ChildDie : Die.children())
500 handleDie(OS, CUI, ChildDie);
501}
502
504 size_t NumBefore = Gsym.getNumFunctionInfos();
505 auto getDie = [&](DWARFUnit &DwarfUnit) -> DWARFDie {
506 DWARFDie ReturnDie = DwarfUnit.getUnitDIE(false);
507 if (DwarfUnit.getDWOId()) {
508 DWARFUnit *DWOCU = DwarfUnit.getNonSkeletonUnitDIE(false).getDwarfUnit();
509 if (OS && !DWOCU->isDWOUnit()) {
510 std::string DWOName = dwarf::toString(
511 DwarfUnit.getUnitDIE().find(
512 {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}),
513 "");
514 *OS << "warning: Unable to retrieve DWO .debug_info section for "
515 << DWOName << "\n";
516 } else {
517 ReturnDie = DWOCU->getUnitDIE(false);
518 }
519 }
520 return ReturnDie;
521 };
522 if (NumThreads == 1) {
523 // Parse all DWARF data from this thread, use the same string/file table
524 // for everything
525 for (const auto &CU : DICtx.compile_units()) {
526 DWARFDie Die = getDie(*CU);
527 CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
528 handleDie(OS, CUI, Die);
529 }
530 } else {
531 // LLVM Dwarf parser is not thread-safe and we need to parse all DWARF up
532 // front before we start accessing any DIEs since there might be
533 // cross compile unit references in the DWARF. If we don't do this we can
534 // end up crashing.
535
536 // We need to call getAbbreviations sequentially first so that getUnitDIE()
537 // only works with its local data.
538 for (const auto &CU : DICtx.compile_units())
539 CU->getAbbreviations();
540
541 // Now parse all DIEs in case we have cross compile unit references in a
542 // thread pool.
543 ThreadPool pool(hardware_concurrency(NumThreads));
544 for (const auto &CU : DICtx.compile_units())
545 pool.async([&CU]() { CU->getUnitDIE(false /*CUDieOnly*/); });
546 pool.wait();
547
548 // Now convert all DWARF to GSYM in a thread pool.
549 std::mutex LogMutex;
550 for (const auto &CU : DICtx.compile_units()) {
551 DWARFDie Die = getDie(*CU);
552 if (Die) {
553 CUInfo CUI(DICtx, dyn_cast<DWARFCompileUnit>(CU.get()));
554 pool.async([this, CUI, &LogMutex, OS, Die]() mutable {
555 std::string ThreadLogStorage;
556 raw_string_ostream ThreadOS(ThreadLogStorage);
557 handleDie(OS ? &ThreadOS: nullptr, CUI, Die);
558 ThreadOS.flush();
559 if (OS && !ThreadLogStorage.empty()) {
560 // Print ThreadLogStorage lines into an actual stream under a lock
561 std::lock_guard<std::mutex> guard(LogMutex);
562 *OS << ThreadLogStorage;
563 }
564 });
565 }
566 }
567 pool.wait();
568 }
569 size_t FunctionsAddedCount = Gsym.getNumFunctionInfos() - NumBefore;
570 if (OS)
571 *OS << "Loaded " << FunctionsAddedCount << " functions from DWARF.\n";
572 return Error::success();
573}
574
576 Log << "Verifying GSYM file \"" << GsymPath << "\":\n";
577
578 auto Gsym = GsymReader::openFile(GsymPath);
579 if (!Gsym)
580 return Gsym.takeError();
581
582 auto NumAddrs = Gsym->getNumAddresses();
584 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
585 DILineInfoSpecifier::FunctionNameKind::LinkageName);
586 std::string gsymFilename;
587 for (uint32_t I = 0; I < NumAddrs; ++I) {
588 auto FuncAddr = Gsym->getAddress(I);
589 if (!FuncAddr)
590 return createStringError(std::errc::invalid_argument,
591 "failed to extract address[%i]", I);
592
593 auto FI = Gsym->getFunctionInfo(*FuncAddr);
594 if (!FI)
595 return createStringError(std::errc::invalid_argument,
596 "failed to extract function info for address 0x%"
597 PRIu64, *FuncAddr);
598
599 for (auto Addr = *FuncAddr; Addr < *FuncAddr + FI->size(); ++Addr) {
600 const object::SectionedAddress SectAddr{
602 auto LR = Gsym->lookup(Addr);
603 if (!LR)
604 return LR.takeError();
605
606 auto DwarfInlineInfos =
607 DICtx.getInliningInfoForAddress(SectAddr, DLIS);
608 uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames();
609 if (NumDwarfInlineInfos == 0) {
610 DwarfInlineInfos.addFrame(
611 DICtx.getLineInfoForAddress(SectAddr, DLIS));
612 }
613
614 // Check for 1 entry that has no file and line info
615 if (NumDwarfInlineInfos == 1 &&
616 DwarfInlineInfos.getFrame(0).FileName == "<invalid>") {
617 DwarfInlineInfos = DIInliningInfo();
618 NumDwarfInlineInfos = 0;
619 }
620 if (NumDwarfInlineInfos > 0 &&
621 NumDwarfInlineInfos != LR->Locations.size()) {
622 Log << "error: address " << HEX64(Addr) << " has "
623 << NumDwarfInlineInfos << " DWARF inline frames and GSYM has "
624 << LR->Locations.size() << "\n";
625 Log << " " << NumDwarfInlineInfos << " DWARF frames:\n";
626 for (size_t Idx = 0; Idx < NumDwarfInlineInfos; ++Idx) {
627 const auto &dii = DwarfInlineInfos.getFrame(Idx);
628 Log << " [" << Idx << "]: " << dii.FunctionName << " @ "
629 << dii.FileName << ':' << dii.Line << '\n';
630 }
631 Log << " " << LR->Locations.size() << " GSYM frames:\n";
632 for (size_t Idx = 0, count = LR->Locations.size();
633 Idx < count; ++Idx) {
634 const auto &gii = LR->Locations[Idx];
635 Log << " [" << Idx << "]: " << gii.Name << " @ " << gii.Dir
636 << '/' << gii.Base << ':' << gii.Line << '\n';
637 }
638 DwarfInlineInfos = DICtx.getInliningInfoForAddress(SectAddr, DLIS);
639 Gsym->dump(Log, *FI);
640 continue;
641 }
642
643 for (size_t Idx = 0, count = LR->Locations.size(); Idx < count;
644 ++Idx) {
645 const auto &gii = LR->Locations[Idx];
646 if (Idx < NumDwarfInlineInfos) {
647 const auto &dii = DwarfInlineInfos.getFrame(Idx);
648 gsymFilename = LR->getSourceFile(Idx);
649 // Verify function name
650 if (dii.FunctionName.find(gii.Name.str()) != 0)
651 Log << "error: address " << HEX64(Addr) << " DWARF function \""
652 << dii.FunctionName.c_str()
653 << "\" doesn't match GSYM function \"" << gii.Name << "\"\n";
654 // Verify source file path
655 if (dii.FileName != gsymFilename)
656 Log << "error: address " << HEX64(Addr) << " DWARF path \""
657 << dii.FileName.c_str() << "\" doesn't match GSYM path \""
658 << gsymFilename.c_str() << "\"\n";
659 // Verify source file line
660 if (dii.Line != gii.Line)
661 Log << "error: address " << HEX64(Addr) << " DWARF line "
662 << dii.Line << " != GSYM line " << gii.Line << "\n";
663 }
664 }
665 }
666 }
667 return Error::success();
668}
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
static void convertFunctionLineTable(raw_ostream *Log, CUInfo &CUI, DWARFDie Die, GsymCreator &Gsym, FunctionInfo &FI)
static void parseInlineInfo(GsymCreator &Gsym, raw_ostream *Log, CUInfo &CUI, DWARFDie Die, uint32_t Depth, FunctionInfo &FI, InlineInfo &Parent, const AddressRanges &AllParentRanges, bool &WarnIfEmpty)
static bool hasInlineInfo(DWARFDie Die, uint32_t Depth)
static AddressRanges ConvertDWARFRanges(const DWARFAddressRangesVector &DwarfRanges)
static std::optional< uint32_t > getQualifiedNameIndex(DWARFDie &Die, uint64_t Language, GsymCreator &Gsym)
Get the GsymCreator string table offset for the qualified name for the DIE passed in.
static DWARFDie GetParentDeclContextDIE(DWARFDie &Die)
uint64_t Addr
std::string Name
#define HEX64(v)
Definition: ExtractRanges.h:21
#define HEX32(v)
Definition: ExtractRanges.h:20
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
A class that represents an address range.
Definition: AddressRanges.h:22
uint64_t start() const
Definition: AddressRanges.h:28
bool contains(uint64_t Addr) const
Definition: AddressRanges.h:32
bool contains(uint64_t Addr) const
Definition: AddressRanges.h:66
The AddressRanges class helps normalize address range collections.
Collection::const_iterator insert(AddressRange Range)
A format-neutral container for inlined code description.
Definition: DIContext.h:92
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:48
DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
compile_unit_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:187
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:42
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:66
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:375
iterator_range< iterator > children() const
Definition: DWARFDie.h:395
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:304
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:622
std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:247
std::optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition: DWARFDie.cpp:271
const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin references if necessary.
Definition: DWARFDie.cpp:442
std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Definition: DWARFDie.cpp:474
dwarf::Tag getTag() const
Definition: DWARFDie.h:71
void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition: DWARFDie.cpp:562
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:441
bool isDWOUnit() const
Definition: DWARFUnit.h:316
This dwarf writer support class manages information associated with a source file.
Definition: DwarfUnit.h:35
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:222
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:575
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
char back() const
back - Get the last character in the string.
Definition: StringRef.h:146
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
char front() const
front - Get the first character in the string.
Definition: StringRef.h:140
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:52
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
Definition: ThreadPool.cpp:202
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:66
llvm::Error verify(StringRef GsymPath, raw_ostream &OS)
llvm::Error convert(uint32_t NumThreads, raw_ostream *OS)
Extract the DWARF from the supplied object file and convert it into the Gsym format in the GsymCreato...
GsymCreator is used to emit GSYM data to a stand alone file or section within a file.
Definition: GsymCreator.h:133
void addFunctionInfo(FunctionInfo &&FI)
Add a function info to this GSYM creator.
uint32_t insertString(StringRef S, bool Copy=true)
Insert a string into the GSYM string table.
const std::optional< AddressRanges > GetValidTextRanges() const
Get the valid text ranges.
Definition: GsymCreator.h:397
bool isQuiet() const
Whether the transformation should be quiet, i.e. not output warnings.
Definition: GsymCreator.h:438
uint32_t insertFile(StringRef Path, sys::path::Style Style=sys::path::Style::native)
Insert a file into this GSYM creator.
Definition: GsymCreator.cpp:28
size_t getNumFunctionInfos() const
Get the current number of FunctionInfo objects contained in this object.
bool IsValidTextAddress(uint64_t Addr) const
Check if an address is a valid code address.
static llvm::Expected< GsymReader > openFile(StringRef Path)
Construct a GsymReader from a file on disk.
Definition: GsymReader.cpp:34
LineTable class contains deserialized versions of line tables for each function's address ranges.
Definition: LineTable.h:118
size_t size() const
Definition: LineTable.h:193
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:642
#define UINT64_MAX
Definition: DataTypes.h:77
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition: Threading.h:185
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1244
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1919
std::function< Expected< AddStreamFn >(unsigned Task, StringRef Key, const Twine &ModuleName)> FileCache
This is the type of a file cache.
Definition: Caching.h:58
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition: DIContext.h:211
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:144
bool lookupAddressRange(object::SectionedAddress Address, uint64_t Size, std::vector< uint32_t > &Result) const
Standard .debug_line state machine structure.
object::SectionedAddress Address
The program-counter value corresponding to a machine instruction generated by the compiler and sectio...
const DWARFDebugLine::LineTable * LineTable
uint32_t DWARFToGSYMFileIndex(GsymCreator &Gsym, uint32_t DwarfFileIdx)
Convert a DWARF compile unit file index into a GSYM global file index.
CUInfo(DWARFContext &DICtx, DWARFCompileUnit *CU)
bool isHighestAddress(uint64_t Addr) const
Return true if Addr is the highest address for a given compile unit.
std::vector< uint32_t > FileCache
Function information in GSYM files encodes information for one contiguous address range.
Definition: FunctionInfo.h:89
std::optional< InlineInfo > Inline
Definition: FunctionInfo.h:93
uint64_t startAddress() const
Definition: FunctionInfo.h:186
uint64_t endAddress() const
Definition: FunctionInfo.h:187
uint64_t size() const
Definition: FunctionInfo.h:188
uint32_t Name
String table offset in the string table.
Definition: FunctionInfo.h:91
std::optional< LineTable > OptLineTable
Definition: FunctionInfo.h:92
Inline information stores the name of the inline function along with an array of address ranges.
Definition: InlineInfo.h:59
std::vector< InlineInfo > Children
Definition: InlineInfo.h:65
AddressRanges Ranges
Definition: InlineInfo.h:64
uint32_t CallFile
1 based file index in the file table.
Definition: InlineInfo.h:62
uint32_t CallLine
Source line number.
Definition: InlineInfo.h:63
uint32_t Name
String table offset in the string table.
Definition: InlineInfo.h:61
Line entries are used to encode the line tables in FunctionInfo objects.
Definition: LineEntry.h:22
static const uint64_t UndefSection
Definition: ObjectFile.h:146