16#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
18#include "llvm/Object/Error.h"
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(),
111 "unable to extract compile unit DIE from .dwo file for skeleton "
130 else if (dwo_symbol_file->GetDWARFContext()
131 .getOrLoadRngListsData()
133 dwo_cu->
SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
136 dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
138 dwo_cu->
SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
142 m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu);
194 m_cu->m_die_array_scoped_mutex.unlock_shared();
195 if (!m_clear_dies || m_cu->m_cancel_scopes)
198 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
199 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
200 if (m_cu->m_cancel_scopes)
202 m_cu->ClearDIEsRWLocked();
206 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
214 m_clear_dies = rhs.m_clear_dies;
226 llvm::formatv(
"{0:x16}: DWARFUnit::ExtractDIEsIfNeeded()",
GetOffset())
241 std::vector<uint32_t> die_index_stack;
242 die_index_stack.reserve(32);
243 die_index_stack.push_back(0);
244 bool prev_die_had_children =
false;
245 while (offset < next_cu_offset && die.
Extract(data,
this, &offset)) {
246 const bool null_die = die.
IsNULL();
248 assert(
m_die_array.empty() &&
"Compile unit DIE already added");
276 if (prev_die_had_children) {
287 if (die_index_stack.back())
288 m_die_array[die_index_stack.back()].SetSiblingIndex(
298 if (!die_index_stack.empty())
299 die_index_stack.pop_back();
303 prev_die_had_children =
false;
308 if (die_has_children) {
309 die_index_stack.push_back(0);
312 prev_die_had_children = die_has_children;
336 m_dwo->ExtractDIEsIfNeeded();
349 if (
const auto *contribution =
350 entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
351 baseOffset = contribution->getOffset();
359 uint64_t length = strOffsets.
GetU32(&baseOffset);
360 if (length == 0xffffffff)
361 length = strOffsets.
GetU64(&baseOffset);
364 if (strOffsets.
GetU16(&baseOffset) < 5)
384 for (
size_t i = 0; i < attributes.
Size(); ++i) {
394 for (
size_t i = 0; i < attributes.
Size(); ++i) {
402 case DW_AT_loclists_base:
405 case DW_AT_rnglists_base:
408 case DW_AT_str_offsets_base:
419 case DW_AT_stmt_list:
422 case DW_AT_GNU_addr_base:
425 case DW_AT_GNU_ranges_base:
428 case DW_AT_GNU_dwo_id:
445const llvm::DWARFAbbreviationDeclarationSet *
463template <
typename ListTableType>
464static llvm::Expected<ListTableType>
466 DwarfFormat format) {
474 return ListTableType();
477 uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
478 if (offset < HeaderSize)
479 return llvm::createStringError(std::errc::invalid_argument,
480 "did not detect a valid"
481 " list table with base = 0x%" PRIx64
"\n",
483 offset -= HeaderSize;
485 if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
493 const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
496 "Failed to find location list contribution for CU with DWO Id "
501 offset += contribution->getOffset();
505 uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
506 if (loclists_base < header_size)
510 offset += loclists_base - header_size;
515 "Failed to extract location list table at offset {0:x16} (location "
516 "list base: {1:x16}): {2}",
517 offset, loclists_base,
toString(std::move(E)).c_str());
521std::unique_ptr<llvm::DWARFLocationTable>
523 llvm::DWARFDataExtractor llvm_data(
528 return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data,
GetVersion());
529 return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
537 if (
const auto *contribution = entry->getContribution(
538 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
540 contribution->getLength32());
550 if (
const auto *contribution =
551 entry->getContribution(llvm::DW_SECT_RNGLISTS))
553 contribution->getLength32());
555 "Failed to find range list contribution for CU with signature {0:x16}",
556 entry->getSignature());
569const std::optional<llvm::DWARFDebugRnglistTable> &
573 if (
auto table_or_error =
574 ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
579 "Failed to extract range list table at offset {0:x16}: {1}",
588 return llvm::createStringError(std::errc::invalid_argument,
589 "missing or invalid range list table");
591 return llvm::createStringError(
592 std::errc::invalid_argument,
593 llvm::formatv(
"DW_FORM_rnglistx cannot be used without "
594 "DW_AT_rnglists_base for CU at {0:x16}",
601 return llvm::createStringError(
602 std::errc::invalid_argument,
603 "invalid range list table index %u; OffsetEntryCount is %u, "
604 "DW_AT_rnglists_base is %" PRIu64,
618 if (data.ValidOffsetForDataOfSize(offset, index_size))
629 m_dwo->ClearDIEsRWLocked();
656 "GetDIE for DIE {0:x16} is outside of its CU {0:x16}", die_offset,
666 if (pos != end && die_offset == (*pos).GetOffset())
674 return llvm::StringRef();
677 if (
const char *name =
682 for (
auto attr : {DW_AT_specification, DW_AT_abstract_origin}) {
688 if (
auto name = unit->PeekDIEName(offset); !name.empty())
692 return llvm::StringRef();
725 assert(skeleton_unit);
753 llvm::StringRef producer(
755 if (producer.empty())
759 llvm::StringRef(R
"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
761 llvm::StringRef(R
"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
763 llvm::StringRef(R
"(4\.[012]\.[01] )"
764 R"(\(Based on Apple Inc\. build [0-9]+\) )"
765 R"(\(LLVM build [\.0-9]+\)$)"));
767 llvm::SmallVector<llvm::StringRef, 3> matches;
768 if (g_swiftlang_version_regex.
Execute(producer, &matches)) {
771 }
else if (producer.contains(
"clang")) {
772 if (g_clang_version_regex.
Execute(producer, &matches))
775 }
else if (producer.contains(
"GNU")) {
777 }
else if (g_llvm_gcc_regex.
Execute(producer)) {
844static llvm::StringRef
846 if (!path_from_dwarf.contains(
':'))
847 return path_from_dwarf;
848 llvm::StringRef host, path;
849 std::tie(host, path) = path_from_dwarf.split(
':');
851 if (host.contains(
'/'))
852 return path_from_dwarf;
856 if (host.size() == 1 && llvm::isAlpha(host[0]) &&
857 (path.starts_with(
"\\") || path.starts_with(
"/")))
858 return path_from_dwarf;
871 if (!comp_dir.empty()) {
900 if (load_all_debug_info)
903 return &llvm::cast<SymbolFileDWARFDwo>(
m_dwo->GetSymbolFileDWARF());
921 const bool minimize =
false;
928 const llvm::DWARFUnitIndex::Entry *index_entry) {
932 assert(!m_index_entry);
935 return llvm::createStringError(
936 llvm::inconvertibleErrorCode(),
937 "Package unit with a non-zero abbreviation offset");
939 auto *unit_contrib = index_entry->getContribution();
940 if (!unit_contrib || unit_contrib->getLength32() != m_length + 4)
941 return llvm::createStringError(llvm::inconvertibleErrorCode(),
942 "Inconsistent DWARF package unit index");
944 auto *abbr_entry = index_entry->getContribution(llvm::DW_SECT_ABBREV);
946 return llvm::createStringError(
947 llvm::inconvertibleErrorCode(),
948 "DWARF package index missing abbreviation column");
950 m_abbr_offset = abbr_entry->getOffset();
951 m_index_entry = index_entry;
952 return llvm::Error::success();
955llvm::Expected<DWARFUnitHeader>
967 if (header.
m_unit_type == llvm::dwarf::DW_UT_skeleton ||
968 header.
m_unit_type == llvm::dwarf::DW_UT_split_compile)
986 bool type_offset_OK =
990 return llvm::make_error<llvm::object::GenericBinaryError>(
991 "Invalid unit length");
993 return llvm::make_error<llvm::object::GenericBinaryError>(
994 "Unsupported unit version");
996 return llvm::make_error<llvm::object::GenericBinaryError>(
997 "Invalid unit address size");
999 return llvm::make_error<llvm::object::GenericBinaryError>(
1000 "Type offset out of range");
1005llvm::Expected<DWARFUnitSP>
1012 auto expected_header =
1014 if (!expected_header)
1015 return expected_header.takeError();
1017 if (context.
isDwo()) {
1018 const llvm::DWARFUnitIndex::Entry *entry =
nullptr;
1019 const llvm::DWARFUnitIndex &index = expected_header->IsTypeUnit()
1023 if (expected_header->IsTypeUnit())
1024 entry = index.getFromHash(expected_header->GetTypeHash());
1025 else if (
auto dwo_id = expected_header->GetDWOId())
1026 entry = index.getFromHash(*dwo_id);
1029 entry = index.getFromOffset(expected_header->GetOffset());
1031 if (llvm::Error err = expected_header->ApplyIndexEntry(entry))
1032 return std::move(err);
1035 const llvm::DWARFDebugAbbrev *abbr =
dwarf.DebugAbbrev();
1037 return llvm::make_error<llvm::object::GenericBinaryError>(
1038 "No debug_abbrev data");
1040 bool abbr_offset_OK =
1041 dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
1042 expected_header->GetAbbrOffset());
1043 if (!abbr_offset_OK)
1044 return llvm::make_error<llvm::object::GenericBinaryError>(
1045 "Abbreviation offset for unit is not valid");
1047 llvm::Expected<const llvm::DWARFAbbreviationDeclarationSet *> abbrevs_or_err =
1048 abbr->getAbbreviationDeclarationSet(expected_header->GetAbbrOffset());
1049 if (!abbrevs_or_err)
1050 return abbrevs_or_err.takeError();
1052 const llvm::DWARFAbbreviationDeclarationSet *abbrevs = *abbrevs_or_err;
1054 return llvm::make_error<llvm::object::GenericBinaryError>(
1055 "No abbrev exists at the specified offset.");
1057 bool is_dwo =
dwarf.GetDWARFContext().isDwo();
1058 if (expected_header->IsTypeUnit())
1062 *abbrevs, section, is_dwo));
1073 case llvm::dwarf::DW_UT_compile:
1074 case llvm::dwarf::DW_UT_partial:
1076 case llvm::dwarf::DW_UT_skeleton:
1077 case llvm::dwarf::DW_UT_split_compile:
1079 case llvm::dwarf::DW_UT_type:
1080 case llvm::dwarf::DW_UT_split_type:
1083 llvm_unreachable(
"invalid UnitType.");
1086std::optional<uint64_t>
1092llvm::Expected<DWARFRangeList>
1097 return llvm::make_error<llvm::object::GenericBinaryError>(
1098 "No debug_ranges section");
1099 return debug_ranges->
FindRanges(
this, offset);
1103 return llvm::createStringError(std::errc::invalid_argument,
1104 "missing or invalid range list table");
1111 if (!range_list_or_error)
1112 return range_list_or_error.takeError();
1114 llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1115 range_list_or_error->getAbsoluteRanges(
1122 return llvm::object::SectionedAddress{
1124 &offset, index_size)};
1127 return llvm_ranges.takeError();
1130 for (
const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1132 llvm_range.HighPC - llvm_range.LowPC));
1140 return maybe_offset.takeError();
1147 return m_dwo->HasAny(tags);
1150 for (
const auto tag : tags) {
1151 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)
static std::optional< uint64_t > GetDWOId(DWARFCompileUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die)
Return the DW_AT_(GNU_)dwo_id.
#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
collection::const_iterator const_iterator
bool Extract(const DWARFDataExtractor &data, const DWARFUnit *cu, lldb::offset_t *offset_ptr)
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
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
DWARFUnit * m_skeleton_unit
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
void SetSkeletonUnit(DWARFUnit *skeleton_unit)
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
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_...
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...
DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, const DWARFUnitHeader &header, const llvm::DWARFAbbreviationDeclarationSet &abbrevs, DIERef::Section section, bool is_dwo)
DWARFDataExtractor GetRnglistData() const
FileSpec GetFile(size_t file_idx)
SymbolFileDWARF & GetBaseSymbolFile() const
static bool SupportedVersion(uint16_t version)
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.