16#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
18#include "llvm/Object/Error.h"
36 const llvm::DWARFUnitHeader &header,
37 const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
39 :
UserID(uid), m_dwarf(
dwarf), m_header(header), m_abbrevs(&abbrevs),
40 m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo),
41 m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.getDWOId()) {}
85 std::shared_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
94 "unable to load .dwo file from \"{0}\" due to ID ({1:x16}) mismatch "
95 "for skeleton DIE at {2:x8}",
96 dwo_symbol_file->GetObjectFile()->GetFileSpec().GetPath().c_str(),
105 "multiple compile units with Dwo ID {0:x16}", *
m_dwo_id));
113 "unable to extract compile unit DIE from .dwo file for skeleton "
132 else if (dwo_symbol_file->GetDWARFContext()
133 .getOrLoadRngListsData()
135 dwo_cu->
SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
138 dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
140 dwo_cu->
SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
144 m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu);
196 m_cu->m_die_array_scoped_mutex.unlock_shared();
197 if (!m_clear_dies || m_cu->m_cancel_scopes)
200 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
201 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
202 if (m_cu->m_cancel_scopes)
204 m_cu->ClearDIEsRWLocked();
208 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
216 m_clear_dies = rhs.m_clear_dies;
228 llvm::formatv(
"{0:x16}: DWARFUnit::ExtractDIEsIfNeeded()",
GetOffset())
243 std::vector<uint32_t> die_index_stack;
244 die_index_stack.reserve(32);
245 die_index_stack.push_back(0);
246 bool prev_die_had_children =
false;
247 while (offset < next_cu_offset && die.
Extract(data, *
this, &offset)) {
248 const bool null_die = die.
IsNULL();
250 assert(
m_die_array.empty() &&
"Compile unit DIE already added");
278 if (prev_die_had_children) {
289 if (die_index_stack.back())
290 m_die_array[die_index_stack.back()].SetSiblingIndex(
300 if (!die_index_stack.empty())
301 die_index_stack.pop_back();
305 prev_die_had_children =
false;
310 if (die_has_children) {
311 die_index_stack.push_back(0);
314 prev_die_had_children = die_has_children;
338 m_dwo->ExtractDIEsIfNeeded();
350 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
351 if (
const auto *contribution =
352 entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
353 baseOffset = contribution->getOffset();
361 uint64_t length = strOffsets.
GetU32(&baseOffset);
362 if (length == 0xffffffff)
363 length = strOffsets.
GetU64(&baseOffset);
366 if (strOffsets.
GetU16(&baseOffset) < 5)
386 for (
size_t i = 0; i < attributes.
Size(); ++i) {
396 for (
size_t i = 0; i < attributes.
Size(); ++i) {
404 case DW_AT_loclists_base:
407 case DW_AT_rnglists_base:
410 case DW_AT_str_offsets_base:
421 case DW_AT_stmt_list:
424 case DW_AT_GNU_addr_base:
427 case DW_AT_GNU_ranges_base:
430 case DW_AT_GNU_dwo_id:
447const llvm::DWARFAbbreviationDeclarationSet *
465template <
typename ListTableType>
466static llvm::Expected<ListTableType>
468 DwarfFormat format) {
476 return ListTableType();
479 uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
480 if (offset < HeaderSize)
481 return llvm::createStringError(std::errc::invalid_argument,
482 "did not detect a valid"
483 " list table with base = 0x%" PRIx64
"\n",
485 offset -= HeaderSize;
487 if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
494 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
495 const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
498 "Failed to find location list contribution for CU with DWO Id "
503 offset += contribution->getOffset();
507 uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
508 if (loclists_base < header_size)
512 offset += loclists_base - header_size;
517 "Failed to extract location list table at offset {0:x16} (location "
518 "list base: {1:x16}): {2}",
519 offset, loclists_base,
toString(std::move(E)).c_str());
523std::unique_ptr<llvm::DWARFLocationTable>
525 llvm::DWARFDataExtractor llvm_data(
530 return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data,
GetVersion());
531 return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
538 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
539 if (
const auto *contribution = entry->getContribution(
540 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
542 contribution->getLength32());
551 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
552 if (
const auto *contribution =
553 entry->getContribution(llvm::DW_SECT_RNGLISTS))
555 contribution->getLength32());
557 "Failed to find range list contribution for CU with signature {0:x16}",
558 entry->getSignature());
571const std::optional<llvm::DWARFDebugRnglistTable> &
575 if (
auto table_or_error =
576 ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
581 "Failed to extract range list table at offset {0:x16}: {1}",
590 return llvm::createStringError(std::errc::invalid_argument,
591 "missing or invalid range list table");
593 return llvm::createStringError(
594 std::errc::invalid_argument,
595 llvm::formatv(
"DW_FORM_rnglistx cannot be used without "
596 "DW_AT_rnglists_base for CU at {0:x16}",
603 return llvm::createStringError(
604 std::errc::invalid_argument,
605 "invalid range list table index %u; OffsetEntryCount is %u, "
606 "DW_AT_rnglists_base is %" PRIu64,
620 if (data.ValidOffsetForDataOfSize(offset, index_size))
631 m_dwo->ClearDIEsRWLocked();
658 "GetDIE for DIE {0:x16} is outside of its CU {0:x16}", die_offset,
668 if (pos != end && die_offset == (*pos).GetOffset())
676 return llvm::StringRef();
679 if (
const char *name =
684 for (
auto attr : {DW_AT_specification, DW_AT_abstract_origin}) {
690 if (
auto name = unit->PeekDIEName(offset); !name.empty())
694 return llvm::StringRef();
719 if (candidate_skeleton_unit)
723 return llvm::dyn_cast_or_null<DWARFCompileUnit>(
m_skeleton_unit.load());
728 if (
m_skeleton_unit.compare_exchange_strong(expected_unit, &skeleton_unit))
730 if (expected_unit == &skeleton_unit) {
760 llvm::StringRef producer(
762 if (producer.empty())
766 llvm::StringRef(R
"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
768 llvm::StringRef(R
"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
770 llvm::StringRef(R
"(4\.[012]\.[01] )"
771 R"(\(Based on Apple Inc\. build [0-9]+\) )"
772 R"(\(LLVM build [\.0-9]+\)$)"));
774 llvm::SmallVector<llvm::StringRef, 3> matches;
775 if (g_swiftlang_version_regex.
Execute(producer, &matches)) {
778 }
else if (producer.contains(
"clang")) {
779 if (g_clang_version_regex.
Execute(producer, &matches))
782 }
else if (producer.contains(
"GNU")) {
784 }
else if (g_llvm_gcc_regex.
Execute(producer)) {
851static llvm::StringRef
853 if (!path_from_dwarf.contains(
':'))
854 return path_from_dwarf;
855 llvm::StringRef host, path;
856 std::tie(host, path) = path_from_dwarf.split(
':');
858 if (host.contains(
'/'))
859 return path_from_dwarf;
863 if (host.size() == 1 && llvm::isAlpha(host[0]) &&
864 (path.starts_with(
"\\") || path.starts_with(
"/")))
865 return path_from_dwarf;
878 if (!comp_dir.empty()) {
907 if (load_all_debug_info)
910 return &llvm::cast<SymbolFileDWARFDwo>(
m_dwo->GetSymbolFileDWARF());
928 const bool minimize =
false;
934llvm::Expected<DWARFUnitSP>
944 llvm::DWARFSectionKind section_kind_llvm =
946 ? llvm::DWARFSectionKind::DW_SECT_INFO
947 : llvm::DWARFSectionKind::DW_SECT_EXT_TYPES;
949 llvm::DWARFDataExtractor debug_info_llvm = debug_info.
GetAsLLVMDWARF();
950 llvm::DWARFUnitHeader header;
951 if (llvm::Error extract_err = header.extract(
952 context.
GetAsLLVM(), debug_info_llvm, offset_ptr, section_kind_llvm))
953 return std::move(extract_err);
955 if (context.
isDwo()) {
956 const llvm::DWARFUnitIndex::Entry *entry =
nullptr;
957 const llvm::DWARFUnitIndex &index = header.isTypeUnit()
961 if (header.isTypeUnit())
962 entry = index.getFromHash(header.getTypeHash());
963 else if (
auto dwo_id = header.getDWOId())
964 entry = index.getFromHash(*dwo_id);
967 entry = index.getFromOffset(header.getOffset());
969 if (llvm::Error err = header.applyIndexEntry(entry))
970 return std::move(err);
973 const llvm::DWARFDebugAbbrev *abbr =
dwarf.DebugAbbrev();
975 return llvm::make_error<llvm::object::GenericBinaryError>(
976 "No debug_abbrev data");
978 bool abbr_offset_OK =
979 dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
980 header.getAbbrOffset());
982 return llvm::make_error<llvm::object::GenericBinaryError>(
983 "Abbreviation offset for unit is not valid");
985 llvm::Expected<const llvm::DWARFAbbreviationDeclarationSet *> abbrevs_or_err =
986 abbr->getAbbreviationDeclarationSet(header.getAbbrOffset());
988 return abbrevs_or_err.takeError();
990 const llvm::DWARFAbbreviationDeclarationSet *abbrevs = *abbrevs_or_err;
992 return llvm::make_error<llvm::object::GenericBinaryError>(
993 "No abbrev exists at the specified offset.");
995 bool is_dwo =
dwarf.GetDWARFContext().isDwo();
996 if (header.isTypeUnit())
1011 case llvm::dwarf::DW_UT_compile:
1012 case llvm::dwarf::DW_UT_partial:
1014 case llvm::dwarf::DW_UT_skeleton:
1015 case llvm::dwarf::DW_UT_split_compile:
1017 case llvm::dwarf::DW_UT_type:
1018 case llvm::dwarf::DW_UT_split_type:
1021 llvm_unreachable(
"invalid UnitType.");
1024std::optional<uint64_t>
1030llvm::Expected<DWARFRangeList>
1035 return llvm::make_error<llvm::object::GenericBinaryError>(
1036 "No debug_ranges section");
1037 return debug_ranges->
FindRanges(
this, offset);
1041 return llvm::createStringError(std::errc::invalid_argument,
1042 "missing or invalid range list table");
1047 data.setAddressSize(
m_header.getAddressByteSize());
1049 if (!range_list_or_error)
1050 return range_list_or_error.takeError();
1052 llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1053 range_list_or_error->getAbsoluteRanges(
1060 return llvm::object::SectionedAddress{
1062 &offset, index_size)};
1065 return llvm_ranges.takeError();
1068 for (
const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1070 llvm_range.HighPC - llvm_range.LowPC));
1079 return maybe_offset.takeError();
1086 return m_dwo->HasAny(tags);
1089 for (
const auto tag : tags) {
1090 if (tag == die.Tag())
static bool CompareDIEOffset(const DWARFDebugInfoEntry &die, const dw_offset_t die_offset)
static llvm::Expected< ListTableType > ParseListTableHeader(const llvm::DWARFDataExtractor &data, uint64_t offset, DwarfFormat format)
static llvm::StringRef removeHostnameFromPathname(llvm::StringRef path_from_dwarf)
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
#define LLDB_SCOPED_TIMERF(...)
A class that measures elapsed time in an exception safe way.
static std::optional< Style > GuessPathStyle(llvm::StringRef absolute_path)
Attempt to guess path style for a given path string.
llvm::sys::path::Style Style
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
virtual lldb::ByteOrder GetByteOrder() const =0
Gets whether endian swapping should occur when extracting data from this object file.
void Append(const Entry &entry)
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
void Clear()
Clear the object state.
static Status createWithFormat(const char *format, Args &&...args)
ObjectFile * GetObjectFile() override
dw_attr_t AttributeAtIndex(uint32_t i) const
bool ExtractFormValueAtIndex(uint32_t i, DWARFFormValue &form_value) const
const DWARFDataExtractor & getOrLoadLocListsData()
const DWARFDataExtractor & getOrLoadStrOffsetsData()
llvm::DWARFContext & GetAsLLVM()
const DWARFDataExtractor & getOrLoadDebugTypesData()
const DWARFDataExtractor & getOrLoadDebugInfoData()
const DWARFDataExtractor & getOrLoadRngListsData()
const DWARFDataExtractor & getOrLoadLocData()
const DWARFDataExtractor & getOrLoadAddrData()
DWARFDebugInfoEntry objects assume that they are living in one big vector and do pointer arithmetic o...
void SetHasChildren(bool b)
dw_offset_t GetOffset() const
const char * GetAttributeValueAsString(const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, bool check_specification_or_abstract_origin=false) const
dw_offset_t GetAttributeValue(const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &formValue, dw_offset_t *end_attr_offset_ptr=nullptr, bool check_specification_or_abstract_origin=false) const
void SetParentIndex(uint32_t idx)
DWARFAttributes GetAttributes(DWARFUnit *cu, Recurse recurse=Recurse::yes) const
void BuildFunctionAddressRangeTable(DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const
This function is builds a table very similar to the standard .debug_aranges table,...
uint64_t GetAttributeValueAsUnsigned(const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, bool check_specification_or_abstract_origin=false) const
bool Extract(const DWARFDataExtractor &data, const DWARFUnit &cu, lldb::offset_t *offset_ptr)
collection::const_iterator const_iterator
DWARFRangeList FindRanges(const DWARFUnit *cu, dw_offset_t debug_ranges_offset) const
FileSpec::Style GetPathStyle()
std::optional< dw_addr_t > m_addr_base
Value of DW_AT_addr_base.
DWARFBaseDIE GetUnitDIEOnly()
bool m_rnglist_table_done
void ExtractDIEsIfNeeded()
std::unique_ptr< llvm::DWARFLocationTable > GetLocationTable(const DataExtractor &data) const
Return the location table for parsing the given location list data.
dw_offset_t GetAbbrevOffset() const
std::optional< FileSpec > m_file_spec
uint8_t GetAddressByteSize() const
void ExtractUnitDIEIfNeeded()
size_t GetLengthByteSize() const
Get the size in bytes of the length field in the header.
void ExtractUnitDIENoDwoIfNeeded()
std::optional< llvm::DWARFDebugRnglistTable > m_rnglist_table
const DWARFDebugInfoEntry * GetUnitDIEPtrOnly()
llvm::sys::RWMutex m_die_array_mutex
bool LinkToSkeletonUnit(DWARFUnit &skeleton_unit)
dw_offset_t m_line_table_offset
Value of DW_AT_stmt_list.
llvm::sys::RWMutex m_die_array_scoped_mutex
void SetStrOffsetsBase(dw_offset_t str_offsets_base)
dw_addr_t GetBaseAddress() const
std::atomic< bool > m_cancel_scopes
Status m_dwo_error
If we get an error when trying to load a .dwo file, save that error here.
void SetLoclistsBase(dw_addr_t loclists_base)
SymbolFileDWARF & GetSymbolFileDWARF() const
dw_offset_t GetLength() const
void SetDwoStrOffsetsBase()
const DWARFDebugAranges & GetFunctionAranges()
dw_offset_t GetLineTableOffset()
bool Supports_DW_AT_APPLE_objc_complete_type()
const DWARFDataExtractor & GetData() const
Get the data that contains the DIE information for this unit.
bool DW_AT_decl_file_attributes_are_invalid()
static llvm::Expected< DWARFUnitSP > extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid, const DWARFDataExtractor &debug_info, DIERef::Section section, lldb::offset_t *offset_ptr)
DWARFCompileUnit * GetSkeletonUnit()
Get the skeleton compile unit for a DWO file.
dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const
const DWARFDebugInfoEntry * DIEPtr()
bool ContainsDIEOffset(dw_offset_t die_offset) const
std::optional< FileSpec > m_comp_dir
dw_offset_t GetFirstDIEOffset() const
std::optional< uint64_t > m_language_type
llvm::StringRef PeekDIEName(dw_offset_t die_offset)
Returns the AT_Name of the DIE at die_offset, if it exists, without parsing the entire compile unit.
DWARFDebugInfoEntry::collection m_die_array
dw_addr_t GetStrOffsetsBase() const
void SetBaseAddress(dw_addr_t base_addr)
void ComputeCompDirAndGuessPathStyle()
std::optional< uint64_t > m_gnu_ranges_base
const llvm::DWARFAbbreviationDeclarationSet * m_abbrevs
DWARFDebugInfoEntry m_first_die
size_t GetDebugInfoSize() const
dw_offset_t GetOffset() const
const DIERef::Section m_section
dw_offset_t GetNextUnitOffset() const
DWARFDataExtractor GetLocationData() const
uint16_t GetVersion() const
const std::optional< llvm::DWARFDebugRnglistTable > & GetRnglistTable()
const FileSpec & GetAbsolutePath()
llvm::Expected< uint64_t > GetRnglistOffset(uint32_t Index)
Return a rangelist's offset based on an index.
std::unique_ptr< DWARFDebugAranges > m_func_aranges_up
dw_offset_t m_str_offsets_base
dw_addr_t m_ranges_base
Value of DW_AT_rnglists_base.
bool Supports_unnamed_objc_bitfields()
dw_addr_t m_loclists_base
Value of DW_AT_loclists_base.
void SetRangesBase(dw_addr_t ranges_base)
dw_addr_t GetAddrBase() const
uint32_t GetHeaderByteSize() const
Get the size in bytes of the unit header.
ScopedExtractDIEs ExtractDIEsScoped()
void SetAddrBase(dw_addr_t addr_base)
llvm::Expected< DWARFRangeList > FindRnglistFromIndex(uint32_t index)
Return a list of address ranges retrieved from an encoded range list whose offset is found via a tabl...
void ExtractDIEsRWLocked()
std::optional< llvm::DWARFListTableHeader > m_loclist_table_header
SymbolFileDWARF & m_dwarf
llvm::VersionTuple GetProducerVersion()
static uint8_t GetDefaultAddressSize()
uint64_t GetDWARFLanguageType()
void ComputeAbsolutePath()
std::optional< uint64_t > m_dwo_id
Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
std::shared_ptr< DWARFUnit > m_dwo
std::optional< uint64_t > m_gnu_addr_base
DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, const llvm::DWARFUnitHeader &header, const llvm::DWARFAbbreviationDeclarationSet &abbrevs, DIERef::Section section, bool is_dwo)
llvm::VersionTuple m_producer_version
std::optional< uint64_t > GetStringOffsetSectionItem(uint32_t index) const
DWARFUnit & GetNonSkeletonUnit()
DWARFDIE GetDIE(dw_offset_t die_offset)
lldb::ByteOrder GetByteOrder() const
void AddUnitDIE(const DWARFDebugInfoEntry &cu_die)
void SetDwoError(const Status &error)
Set the fission .dwo file specific error for this compile unit.
llvm::sys::RWMutex m_first_die_mutex
SymbolFileDWARFDwo * GetDwoSymbolFile(bool load_all_debug_info=true)
bool m_has_parsed_non_skeleton_unit
DWARFProducer GetProducer()
const FileSpec & GetCompilationDirectory()
std::optional< uint64_t > GetDWOId()
Get the DWO ID from the DWARFUnitHeader for DWARF5, or from the unit DIE's DW_AT_dwo_id or DW_AT_GNU_...
llvm::DWARFUnitHeader m_header
const llvm::DWARFAbbreviationDeclarationSet * GetAbbreviations() const
bool HasAny(llvm::ArrayRef< dw_tag_t > tags)
Returns true if any DIEs in the unit match any DW_TAG values in tags.
llvm::Expected< DWARFRangeList > FindRnglistFromOffset(dw_offset_t offset)
Return a list of address ranges resulting from a (possibly encoded) range list starting at a given of...
std::atomic< DWARFUnit * > m_skeleton_unit
DWARFDataExtractor GetRnglistData() const
FileSpec GetFile(size_t file_idx)
SymbolFileDWARF & GetBaseSymbolFile() const
DWARFDebugRanges * GetDebugRanges()
DWARFUnit * GetSkeletonUnit(DWARFUnit *dwo_unit)
Given a DWO DWARFUnit, find the corresponding skeleton DWARFUnit in the main symbol file.
FileSpec GetFile(DWARFUnit &unit, size_t file_idx)
std::shared_ptr< SymbolFileDWARFDwo > GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die)
StatsDuration & GetDebugInfoParseTimeRef()
DWARFContext & GetDWARFContext()
#define DW_INVALID_OFFSET
llvm::dwarf::Attribute dw_attr_t
#define LLDB_INVALID_ADDRESS
std::shared_ptr< DWARFUnit > DWARFUnitSP
A class that represents a running process on the host machine.
const char * toString(AppleArm64ExceptionClass EC)
ByteOrder
Byte ordering definitions.
A mix in class that contains a generic user ID.