34 using namespace dwarf;
59 void DWARFUnitVector::addUnitsImpl(
72 -> std::unique_ptr<DWARFUnit> {
81 if (!Header.
extract(Context, Data, &Offset, SectionKind, Index,
84 std::unique_ptr<DWARFUnit> U;
86 U = std::make_unique<DWARFTypeUnit>(Context, InfoSection, Header, DA,
87 RS, LocSection, SS, SOS, AOS, LS,
90 U = std::make_unique<DWARFCompileUnit>(
Context, InfoSection, Header,
91 DA, RS, LocSection,
SS, SOS,
92 AOS,
LS,
LE, IsDWO, *
this);
106 if (
I != this->
end() &&
107 (&(*I)->getInfoSection() != &Section || (*I)->getOffset() ==
Offset)) {
111 auto U = Parser(Offset, SectionKind, &Section,
nullptr);
115 Offset = U->getNextUnitOffset();
116 I = std::next(this->insert(
I, std::move(U)));
122 [](
const std::unique_ptr<DWARFUnit> &LHS,
123 const std::unique_ptr<DWARFUnit> &RHS) {
124 return LHS->getOffset() < RHS->getOffset();
126 return this->insert(
I, std::move(Unit))->get();
130 auto end =
begin() + getNumInfoUnits();
133 [](uint64_t LHS,
const std::unique_ptr<DWARFUnit> &RHS) {
134 return LHS < RHS->getNextUnitOffset();
147 auto Offset = CUOff->Offset;
148 auto end =
begin() + getNumInfoUnits();
152 [](uint64_t LHS,
const std::unique_ptr<DWARFUnit> &RHS) {
153 return LHS < RHS->getNextUnitOffset();
165 auto *NewCU = U.get();
166 this->insert(
CU, std::move(U));
177 : Context(DC), InfoSection(Section), Header(Header), Abbrev(DA),
178 RangeSection(RS), LineSection(LS), StringSection(SS),
179 StringOffsetSection(SOS), AddrOffsetSection(AOS), isLittleEndian(LE),
180 IsDWO(IsDWO), UnitVector(UnitVector) {
188 Data = Data.
substr(
C->Offset,
C->Length);
197 std::make_unique<DWARFDebugLoclists>(DWARFData, Header.
getVersion());
200 LocTable = std::make_unique<DWARFDebugLoclists>(
206 LocTable = std::make_unique<DWARFDebugLoc>(
228 if (
I != R.end() && std::next(
I) == R.end())
229 return (*I)->getAddrOffsetSectionItem(Index);
231 if (!AddrOffsetSectionBase)
239 uint64_t
Address = DA.getRelocatedAddress(&Offset, &Section);
244 if (!StringOffsetsTableContribution)
248 if (StringOffsetSection.
Data.
size() < Offset + ItemSize)
252 return DA.getRelocatedValue(ItemSize, &Offset);
257 uint64_t *offset_ptr,
264 if (!IndexEntry && Index)
269 Length = debug_info.
getU64(offset_ptr, &Err);
292 auto *UnitContrib = IndexEntry->getOffset();
293 if (!UnitContrib || UnitContrib->Length != (Length + 4))
298 AbbrOffset = AbbrEntry->Offset;
301 TypeHash = debug_info.
getU64(offset_ptr, &Err);
305 DWOId = debug_info.
getU64(offset_ptr, &Err);
311 assert(*offset_ptr -
Offset <= 255 &&
"unexpected header size");
319 : TypeOffset >=
Size &&
325 if (!LengthOK || !VersionOK || !AddrSizeOK || !TypeOffsetOK)
335 template<
typename ListTableType>
344 if (Offset < HeaderSize)
346 " list table with base = 0x%" PRIx64
"\n",
351 if (
Error E = Table.extractHeaderAndOffsets(DA, &Offset))
359 assert(!DieArray.empty());
362 uint64_t ActualRangeListOffset = RangeSectionBase + RangeListOffset;
363 return RangeList.
extract(RangesData, &ActualRangeListOffset);
369 RangeSectionBase = 0;
371 AddrOffsetSectionBase =
None;
380 void DWARFUnit::extractDIEsToVector(
381 bool AppendCUDie,
bool AppendNonCUDies,
382 std::vector<DWARFDebugInfoEntry> &Dies)
const {
383 if (!AppendCUDie && !AppendNonCUDies)
395 while (DIE.
extractFast(*
this, &DIEOffset, DebugInfoData, NextCUOffset,
400 if (!AppendNonCUDies)
405 Dies.reserve(Dies.size() + getDebugInfoSize() / 14);
414 if (AbbrDecl->hasChildren())
428 if (DIEOffset > NextCUOffset)
430 "bounds cu 0x%8.8" PRIx64
" " 431 "at 0x%8.8" PRIx64
"\n",
435 void DWARFUnit::extractDIEsIfNeeded(
bool CUDieOnly) {
441 if ((CUDieOnly && !DieArray.empty()) ||
445 bool HasCUDie = !DieArray.empty();
446 extractDIEsToVector(!HasCUDie, !CUDieOnly, DieArray);
448 if (DieArray.empty())
455 DWARFDie UnitDie(
this, &DieArray[0]);
460 assert(RangeSectionBase == 0);
461 assert(LocSectionBase == 0);
463 if (!AddrOffsetSectionBase)
464 AddrOffsetSectionBase =
480 auto StringOffsetOrError =
483 if (!StringOffsetOrError)
485 "invalid reference to or invalid content in " 486 ".debug_str_offsets[.dwo]: " +
487 toString(StringOffsetOrError.takeError()));
489 StringOffsetsTableContribution = *StringOffsetOrError;
505 auto TableOrError = parseListTableHeader<DWARFDebugRnglistTable>(
506 RangesDA, RangeSectionBase, Header.
getFormat());
509 "parsing a range list table: " +
510 toString(TableOrError.takeError()));
512 RngListTable = TableOrError.get();
516 if (IsDWO && RngListTable)
517 RangeSectionBase = RngListTable->getHeaderSize();
531 LoclistTableHeader.emplace(
".debug_loclists.dwo",
"locations");
533 LoclistTableHeader.emplace(
".debug_loclists",
"locations");
539 if (Offset < HeaderSize)
541 "did not detect a valid" 542 " list table with base = 0x%" PRIx64
"\n",
545 if (
Error E = LoclistTableHeader->extract(
Data, &Offset))
547 "parsing a loclist table: " +
557 bool DWARFUnit::parseDWO() {
587 DWO = std::shared_ptr<DWARFCompileUnit>(std::move(DWOContext), DWOCU);
589 if (AddrOffsetSectionBase)
590 DWO->setAddrOffsetSection(AddrOffsetSection, *AddrOffsetSectionBase);
595 if (
auto TableOrError = parseListTableHeader<DWARFDebugRnglistTable>(
596 RangesDA, RangeSectionBase, Header.
getFormat()))
597 DWO->RngListTable = TableOrError.get();
600 <<
toString(TableOrError.takeError())
602 if (DWO->RngListTable)
603 DWO->RangeSectionBase = DWO->RngListTable->getHeaderSize();
606 DWO->setRangesSection(RangeSection, DWORangesBase ? *DWORangesBase : 0);
612 void DWARFUnit::clearDIEs(
bool KeepCUDie) {
613 if (DieArray.size() > (
unsigned)KeepCUDie) {
614 DieArray.resize((
unsigned)KeepCUDie);
615 DieArray.shrink_to_fit();
629 isLittleEndian, RngListTable->getAddrSize());
630 auto RangeListOrError = RngListTable->findList(RangesData, Offset);
631 if (RangeListOrError)
632 return RangeListOrError.get().getAbsoluteRanges(
getBaseAddress(), *
this);
633 return RangeListOrError.takeError();
637 "missing or invalid range list table");
647 "invalid range list table index %d", Index);
650 "missing or invalid range list table");
660 if (!CUDIERangesOrError)
662 "decoding address ranges: %s",
664 return *CUDIERangesOrError;
678 Result.push_back(std::move(*L));
680 InterpretationError =
681 joinErrors(L.takeError(), std::move(InterpretationError));
682 return !InterpretationError;
685 if (ParseError || InterpretationError)
686 return joinErrors(std::move(ParseError), std::move(InterpretationError));
694 if (DIERangesOrError) {
695 for (
const auto &R : DIERangesOrError.get()) {
697 if (R.LowPC == R.HighPC)
699 auto B = AddrDieMap.upper_bound(R.LowPC);
700 if (
B != AddrDieMap.begin() && R.LowPC < (--
B)->
second.first) {
703 if (R.HighPC <
B->second.first)
704 AddrDieMap[R.HighPC] =
B->second;
705 if (R.LowPC >
B->first)
706 AddrDieMap[
B->first].first = R.LowPC;
708 AddrDieMap[R.LowPC] = std::make_pair(R.HighPC, Die);
723 extractDIEsIfNeeded(
false);
724 if (AddrDieMap.empty())
726 auto R = AddrDieMap.upper_bound(Address);
727 if (R == AddrDieMap.begin())
731 if (Address >= R->second.first)
733 return R->second.second;
751 if (SubroutineDIE.
getTag() == DW_TAG_inlined_subroutine)
753 SubroutineDIE = SubroutineDIE.
getParent();
777 const uint32_t ParentDepth = Depth - 1;
778 for (
uint32_t I = getDIEIndex(Die) - 1;
I > 0; --
I) {
779 if (DieArray[
I].
getDepth() == ParentDepth)
797 for (
size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size();
I < EndIdx;
814 for (
size_t I = getDIEIndex(Die);
I > 0;) {
829 size_t I = getDIEIndex(Die) + 1;
830 if (I >= DieArray.size())
832 return DWARFDie(
this, &DieArray[I]);
840 for (
size_t I = getDIEIndex(Die) + 1, EndIdx = DieArray.size();
I < EndIdx;
842 if (DieArray[
I].
getDepth() == Depth + 1 &&
843 DieArray[
I].getTag() == dwarf::DW_TAG_null)
845 assert(DieArray[I].
getDepth() > Depth &&
"Not processing children?");
872 uint64_t ValidationSize =
alignTo(
Size, EntrySize);
874 if (ValidationSize >=
Size)
928 return DescOrError.takeError();
937 return DescOrError.takeError();
960 return DescOrError.takeError();
979 return DescOrError.takeError();
const DWARFAbbreviationDeclarationSet * getAbbreviations() const
const DWARFUnitIndex & getTUIndex()
void getInlinedChainForAddress(uint64_t Address, SmallVectorImpl< DWARFDie > &InlinedChain)
getInlinedChainForAddress - fetches inlined chain for a given address.
const_iterator end(StringRef path)
Get end iterator over path.
uint64_t getNextUnitOffset() const
virtual const DWARFSection & getAddrSection() const
static Expected< StrOffsetsContributionDescriptor > parseDWARFStringOffsetsTableHeader(DWARFDataExtractor &DA, llvm::dwarf::DwarfFormat Format, uint64_t Offset)
virtual StringRef getStrSection() const
Error visitAbsoluteLocationList(uint64_t Offset, Optional< object::SectionedAddress > BaseAddr, std::function< Optional< object::SectionedAddress >(uint32_t)> LookupAddr, function_ref< bool(Expected< DWARFLocationExpression >)> Callback) const
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
DWARFUnit * getUnitForOffset(uint64_t Offset) const
virtual const DWARFSection & getLocDWOSection() const
Expected< Optional< StrOffsetsContributionDescriptor > > determineStringOffsetsTableContributionDWO(DWARFDataExtractor &DA)
Find the unit's contribution to the string offsets table and determine its length and form...
This class represents lattice values for constants.
Represents base address of the CU.
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
static Expected< StrOffsetsContributionDescriptor > parseDWARF32StringOffsetsTableHeader(DWARFDataExtractor &DA, uint64_t Offset)
void push_back(const T &Elt)
std::shared_ptr< DWARFContext > getDWOContext(StringRef AbsolutePath)
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
virtual StringRef getStrDWOSection() const
virtual const DWARFSection & getLocSection() const
DWARFDie getSibling(const DWARFDebugInfoEntry *Die)
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
virtual const DWARFSection & getRnglistsDWOSection() const
block Block Frequency true
bool extractFast(const DWARFUnit &U, uint64_t *OffsetPtr)
Extracts a debug info entry, which is a child of a given unit, starting at a given offset...
Expected< StrOffsetsContributionDescriptor > validateContributionSize(DWARFDataExtractor &DA)
Determine whether a contribution to the string offsets table is consistent with the relevant section ...
const DWARFDebugAbbrev * getDebugAbbrevDWO()
Get a pointer to the parsed dwo abbreviations object.
uint8_t getDwarfStringOffsetsByteSize() const
void addUnitsForSection(DWARFContext &C, const DWARFSection &Section, DWARFSectionKind SectionKind)
Read units from a .debug_info or .debug_types section.
DWARFDie getSubroutineForAddress(uint64_t Address)
Returns subprogram DIE with address range encompassing the provided address.
virtual bool isLittleEndian() const =0
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
DWARFUnit(DWARFContext &Context, const DWARFSection &Section, const DWARFUnitHeader &Header, const DWARFDebugAbbrev *DA, const DWARFSection *RS, const DWARFSection *LocSection, StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS, const DWARFSection &LS, bool LE, bool IsDWO, const DWARFUnitVector &UnitVector)
virtual const DWARFSection & getRangesSection() const
std::string toString(Error E)
Write all error messages (if any) in E to a string.
static ManagedStatic< DebugCounter > DC
DwarfFormat
Constants that define the DWARF format as 32 or 64 bit.
const char * getCompilationDir()
Optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
uint64_t getLocSectionBase() const
DWARFUnit * addUnit(std::unique_ptr< DWARFUnit > Unit)
Add an existing DWARFUnit to this UnitVector.
Tagged union holding either a T or a Error.
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
virtual const DWARFSection & getLoclistsDWOSection() const
uint8_t getAddressByteSize() const
Optional< object::SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)
DWARFAddressRangesVector getAbsoluteRanges(llvm::Optional< object::SectionedAddress > BaseAddr) const
getAbsoluteRanges - Returns absolute address ranges defined by this range list.
virtual const DWARFSection & getStrOffsetsSection() const
LLVM_NODISCARD size_t size() const
size - Get the string size.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
llvm::Optional< object::SectionedAddress > getBaseAddress()
const DWARFDebugAbbrev * getDebugAbbrev()
Get a pointer to the parsed DebugAbbrev object.
virtual const DWARFSection & getStrOffsetsDWOSection() const
uint32_t getDepth() const
DWARFDie getLastChild(const DWARFDebugInfoEntry *Die)
uint16_t getVersion() const
uint8_t getUnitLengthFieldByteSize(DwarfFormat Format)
Get the byte size of the unit length field depending on the DWARF format.
DWARFDie getSibling() const
Get the sibling of this DIE object.
Expected< DWARFAddressRangesVector > findRnglistFromIndex(uint32_t Index)
Return a vector of address ranges retrieved from an encoded range list whose offset is found via a ta...
DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die)
unit_iterator_range info_section_units()
Get units from .debug_info in this context.
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
virtual const DWARFSection & getRangesDWOSection() const
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void setLocSection(const DWARFSection *LS, uint64_t Base)
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
A structured debug information entry.
bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Optional< uint64_t > getDWOId()
Expected< DWARFAddressRangesVector > collectAddressRanges()
Error tryExtractDIEsIfNeeded(bool CUDieOnly)
const DWARFAbbreviationDeclarationSet * getAbbreviationDeclarationSet(uint64_t CUAbbrOffset) const
DWARFDie getParent(const DWARFDebugInfoEntry *Die)
virtual const DWARFSection & getLineDWOSection() const
SectionKind - This is a simple POD value that classifies the properties of a section.
Error extract(const DWARFDataExtractor &data, uint64_t *offset_ptr)
uint32_t getHeaderSize() const
Size in bytes of the parsed unit header.
Expected< DWARFLocationExpressionsVector > findLoclistFromOffset(uint64_t Offset)
Error extractRangeList(uint64_t RangeListOffset, DWARFDebugRangeList &RangeList) const
Extract the range list referenced by this compile unit from the .debug_ranges section.
void consumeError(Error Err)
Consume a Error without doing anything.
const DWARFUnitIndex & getDWARFUnitIndex(DWARFContext &Context, DWARFSectionKind Kind)
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
static Expected< ListTableType > parseListTableHeader(DWARFDataExtractor &DA, uint64_t Offset, DwarfFormat Format)
UnitType
Constants for unit types in DWARF v5.
DWARFUnit * getUnitForIndexEntry(const DWARFUnitIndex::Entry &E)
uint64_t getStringOffsetsBase() const
uint64_t getOffset() const
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
static ErrorSuccess success()
Create a success value.
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Expected< DWARFAddressRangesVector > findRnglistFromOffset(uint64_t Offset)
Return a vector of address ranges resulting from a (possibly encoded) range list starting at a given ...
void addUnitsForDWOSection(DWARFContext &C, const DWARFSection &DWOSection, DWARFSectionKind SectionKind, bool Lazy=false)
Read units from a .debug_info.dwo or .debug_types.dwo section.
static unsigned getDepth(const SmallVectorImpl< const MachineBasicBlock *> &Stack, const MachineBasicBlock *MBB)
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Expected< Optional< StrOffsetsContributionDescriptor > > determineStringOffsetsTableContribution(DWARFDataExtractor &DA)
Find the unit's contribution to the string offsets table and determine its length and form...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Describe a collection of units.
Optional< const char * > toString(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
virtual const DWARFSection & getRnglistsSection() const
DWARFDataExtractor getDebugInfoExtractor() const
static Expected< StrOffsetsContributionDescriptor > parseDWARF64StringOffsetsTableHeader(DWARFDataExtractor &DA, uint64_t Offset)
virtual const DWARFSection & getLineSection() const
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static bool isSupportedVersion(unsigned version)
const DWARFLocationTable & getLocationTable()
dwarf::Tag getTag() const
LLVM_NODISCARD bool empty() const
Error joinErrors(Error E1, Error E2)
Concatenate errors.
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die)
void updateAddressDieMap(DWARFDie Die)
Recursively update address to Die map.
Optional< uint64_t > getRnglistOffset(uint32_t Index)
Return a rangelist's offset based on an index.
void setRangesSection(const DWARFSection *RS, uint64_t Base)
bool isLittleEndian() const
const DWARFObject & getDWARFObj() const
const DWARFUnitIndex & getCUIndex()
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
DWARFDebugInfoEntry - A DIE with only the minimum required data.
DWARFDie getFirstChild() const
Get the first child of this DIE object.
uint64_t getLength() const
Lightweight error class with error context and mandatory checking.
const Entry * getFromOffset(uint32_t Offset) const
const SectionContribution * getOffset(DWARFSectionKind Sec) const
Optional< object::SectionedAddress > getAddrOffsetSectionItem(uint32_t Index) const
StringRef - Represent a constant reference to a string, i.e.
virtual const DWARFSection & getLoclistsSection() const
uint8_t getDwarfOffsetByteSize() const
void setMaxVersionIfGreater(unsigned Version)
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Optional< uint64_t > getStringOffsetSectionItem(uint32_t Index) const
DWARFDie getParent() const
Get the parent of this DIE object.
auto upper_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range))
Provide wrappers to std::upper_bound which take ranges instead of having to pass begin/end explicitly...