16#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
17#include "llvm/Object/Error.h"
37 :
UserID(uid), m_dwarf(
dwarf), m_header(header), m_abbrevs(&abbrevs),
38 m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo),
39 m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.
GetDWOId()) {}
83 std::shared_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
92 "unable to load .dwo file from \"{0}\" due to ID ({1:x16}) mismatch "
93 "for skeleton DIE at {2:x8}",
94 dwo_symbol_file->GetObjectFile()->GetFileSpec().GetPath().c_str(),
104 "unable to extract compile unit DIE from .dwo file for skeleton "
123 else if (dwo_symbol_file->GetDWARFContext()
124 .getOrLoadRngListsData()
126 dwo_cu->
SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
129 dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
131 dwo_cu->
SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
135 m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu);
187 m_cu->m_die_array_scoped_mutex.unlock_shared();
188 if (!m_clear_dies || m_cu->m_cancel_scopes)
191 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
192 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
193 if (m_cu->m_cancel_scopes)
195 m_cu->ClearDIEsRWLocked();
199 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
207 m_clear_dies = rhs.m_clear_dies;
219 llvm::formatv(
"{0:x16}: DWARFUnit::ExtractDIEsIfNeeded()",
GetOffset())
234 std::vector<uint32_t> die_index_stack;
235 die_index_stack.reserve(32);
236 die_index_stack.push_back(0);
237 bool prev_die_had_children =
false;
238 while (offset < next_cu_offset && die.
Extract(data,
this, &offset)) {
239 const bool null_die = die.
IsNULL();
241 assert(
m_die_array.empty() &&
"Compile unit DIE already added");
269 if (prev_die_had_children) {
280 if (die_index_stack.back())
281 m_die_array[die_index_stack.back()].SetSiblingIndex(
291 if (!die_index_stack.empty())
292 die_index_stack.pop_back();
296 prev_die_had_children =
false;
301 if (die_has_children) {
302 die_index_stack.push_back(0);
305 prev_die_had_children = die_has_children;
329 m_dwo->ExtractDIEsIfNeeded();
342 if (
const auto *contribution =
343 entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
344 baseOffset = contribution->getOffset();
352 uint64_t length = strOffsets.
GetU32(&baseOffset);
353 if (length == 0xffffffff)
354 length = strOffsets.
GetU64(&baseOffset);
357 if (strOffsets.
GetU16(&baseOffset) < 5)
375 size_t num_attributes = cu_die.
GetAttributes(
this, attributes);
378 for (
size_t i = 0; i < num_attributes; ++i) {
388 for (
size_t i = 0; i < num_attributes; ++i) {
394 case DW_AT_loclists_base:
397 case DW_AT_rnglists_base:
400 case DW_AT_str_offsets_base:
411 case DW_AT_stmt_list:
414 case DW_AT_GNU_addr_base:
417 case DW_AT_GNU_ranges_base:
420 case DW_AT_GNU_dwo_id:
454template <
typename ListTableType>
455static llvm::Expected<ListTableType>
457 DwarfFormat format) {
465 return ListTableType();
468 uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
469 if (offset < HeaderSize)
470 return llvm::createStringError(std::errc::invalid_argument,
471 "did not detect a valid"
472 " list table with base = 0x%" PRIx64
"\n",
474 offset -= HeaderSize;
476 if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
484 const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
487 "Failed to find location list contribution for CU with DWO Id "
492 offset += contribution->getOffset();
496 uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
497 if (loclists_base < header_size)
501 offset += loclists_base - header_size;
506 "Failed to extract location list table at offset {0:x16} (location "
507 "list base: {1:x16}): {2}",
508 offset, loclists_base,
toString(std::move(E)).c_str());
512std::unique_ptr<llvm::DWARFLocationTable>
514 llvm::DWARFDataExtractor llvm_data(
519 return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data,
GetVersion());
520 return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
528 if (
const auto *contribution = entry->getContribution(
529 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
531 contribution->getLength32());
541 if (
const auto *contribution =
542 entry->getContribution(llvm::DW_SECT_RNGLISTS))
544 contribution->getLength32());
546 "Failed to find range list contribution for CU with signature {0:x16}",
547 entry->getSignature());
560const std::optional<llvm::DWARFDebugRnglistTable> &
564 if (
auto table_or_error =
565 ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
570 "Failed to extract range list table at offset {0:x16}: {1}",
579 return llvm::createStringError(std::errc::invalid_argument,
580 "missing or invalid range list table");
582 return llvm::createStringError(
583 std::errc::invalid_argument,
584 llvm::formatv(
"DW_FORM_rnglistx cannot be used without "
585 "DW_AT_rnglists_base for CU at {0:x16}",
592 return llvm::createStringError(
593 std::errc::invalid_argument,
594 "invalid range list table index %u; OffsetEntryCount is %u, "
595 "DW_AT_rnglists_base is %" PRIu64,
609 if (data.ValidOffsetForDataOfSize(offset, index_size))
620 m_dwo->ClearDIEsRWLocked();
647 "GetDIE for DIE {0:x16} is outside of its CU {0:x16}", die_offset,
657 if (pos != end && die_offset == (*pos).GetOffset())
704 llvm::StringRef producer(
706 if (producer.empty())
710 llvm::StringRef(R
"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
712 llvm::StringRef(R
"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
714 llvm::StringRef(R
"(4\.[012]\.[01] )"
715 R"(\(Based on Apple Inc\. build [0-9]+\) )"
716 R"(\(LLVM build [\.0-9]+\)$)"));
718 llvm::SmallVector<llvm::StringRef, 3> matches;
719 if (g_swiftlang_version_regex.
Execute(producer, &matches)) {
722 }
else if (producer.contains(
"clang")) {
723 if (g_clang_version_regex.
Execute(producer, &matches))
726 }
else if (producer.contains(
"GNU")) {
728 }
else if (g_llvm_gcc_regex.
Execute(producer)) {
795static llvm::StringRef
797 if (!path_from_dwarf.contains(
':'))
798 return path_from_dwarf;
799 llvm::StringRef host, path;
800 std::tie(host, path) = path_from_dwarf.split(
':');
802 if (host.contains(
'/'))
803 return path_from_dwarf;
807 if (host.size() == 1 && llvm::isAlpha(host[0]) &&
808 (path.startswith(
"\\") || path.startswith(
"/")))
809 return path_from_dwarf;
822 if (!comp_dir.empty()) {
853 return &llvm::cast<SymbolFileDWARFDwo>(
m_dwo->GetSymbolFileDWARF());
871 const bool minimize =
false;
877llvm::Expected<DWARFUnitHeader>
890 if (header.
m_unit_type == llvm::dwarf::DW_UT_skeleton ||
891 header.
m_unit_type == llvm::dwarf::DW_UT_split_compile)
905 if (context.
isDwo()) {
906 const llvm::DWARFUnitIndex *Index;
908 Index = &context.
GetAsLLVM().getTUIndex();
912 Index = &context.
GetAsLLVM().getCUIndex();
922 return llvm::createStringError(
923 llvm::inconvertibleErrorCode(),
924 "Package unit with a non-zero abbreviation offset");
926 auto *unit_contrib = header.
m_index_entry->getContribution();
927 if (!unit_contrib || unit_contrib->getLength32() != header.
m_length + 4) {
928 return llvm::createStringError(llvm::inconvertibleErrorCode(),
929 "Inconsistent DWARF package unit index");
934 return llvm::createStringError(
935 llvm::inconvertibleErrorCode(),
936 "DWARF package index missing abbreviation column");
944 bool type_offset_OK =
948 return llvm::make_error<llvm::object::GenericBinaryError>(
949 "Invalid unit length");
951 return llvm::make_error<llvm::object::GenericBinaryError>(
952 "Unsupported unit version");
954 return llvm::make_error<llvm::object::GenericBinaryError>(
955 "Invalid unit address size");
957 return llvm::make_error<llvm::object::GenericBinaryError>(
958 "Type offset out of range");
963llvm::Expected<DWARFUnitSP>
970 debug_info, section,
dwarf.GetDWARFContext(), offset_ptr);
971 if (!expected_header)
972 return expected_header.takeError();
976 return llvm::make_error<llvm::object::GenericBinaryError>(
977 "No debug_abbrev data");
979 bool abbr_offset_OK =
980 dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
981 expected_header->GetAbbrOffset());
983 return llvm::make_error<llvm::object::GenericBinaryError>(
984 "Abbreviation offset for unit is not valid");
989 return llvm::make_error<llvm::object::GenericBinaryError>(
990 "No abbrev exists at the specified offset.");
992 bool is_dwo =
dwarf.GetDWARFContext().isDwo();
993 if (expected_header->IsTypeUnit())
997 *abbrevs, section, is_dwo));
1008 case llvm::dwarf::DW_UT_compile:
1009 case llvm::dwarf::DW_UT_partial:
1011 case llvm::dwarf::DW_UT_skeleton:
1012 case llvm::dwarf::DW_UT_split_compile:
1014 case llvm::dwarf::DW_UT_type:
1015 case llvm::dwarf::DW_UT_split_type:
1018 llvm_unreachable(
"invalid UnitType.");
1021std::optional<uint64_t>
1027llvm::Expected<DWARFRangeList>
1032 return llvm::make_error<llvm::object::GenericBinaryError>(
1033 "No debug_ranges section");
1035 debug_ranges->
FindRanges(
this, offset, ranges);
1040 return llvm::createStringError(std::errc::invalid_argument,
1041 "missing or invalid range list table");
1048 if (!range_list_or_error)
1049 return range_list_or_error.takeError();
1051 llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1052 range_list_or_error->getAbsoluteRanges(
1059 return llvm::object::SectionedAddress{
1061 &offset, index_size)};
1064 return llvm_ranges.takeError();
1067 for (
const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1069 llvm_range.HighPC - llvm_range.LowPC));
1074llvm::Expected<DWARFRangeList>
1078 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)
std::shared_ptr< DWARFUnit > DWARFUnitSP
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(...)
dw_offset_t GetOffset() const
dw_attr_t AttributeAtIndex(uint32_t i) const
bool ExtractFormValueAtIndex(uint32_t i, DWARFFormValue &form_value) const
const DWARFAbbreviationDeclarationSet * GetAbbreviationDeclarationSet(dw_offset_t cu_abbr_offset) const
DWARFDebugInfoEntry objects assume that they are living in one big vector and do pointer arithmetic o...
dw_offset_t GetOffset() const
size_t GetAttributes(DWARFUnit *cu, DWARFAttributes &attrs, Recurse recurse=Recurse::yes) const
const char * GetAttributeValueAsString(const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, bool check_specification_or_abstract_origin=false) const
void SetHasChildren(bool b)
bool Extract(const lldb_private::DWARFDataExtractor &data, const DWARFUnit *cu, lldb::offset_t *offset_ptr)
void BuildFunctionAddressRangeTable(DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const
This function is builds a table very similar to the standard .debug_aranges table,...
void SetParentIndex(uint32_t idx)
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 FindRanges(const DWARFUnit *cu, dw_offset_t debug_ranges_offset, DWARFRangeList &range_list) const
SymbolFileDWARFDwo * GetDwoSymbolFile()
void * GetUserData() const
bool m_rnglist_table_done
const DIERef::Section m_section
lldb_private::FileSpec::Style GetPathStyle()
SymbolFileDWARF & GetSymbolFileDWARF() const
void ExtractDIEsIfNeeded()
lldb_private::Status m_dwo_error
If we get an error when trying to load a .dwo file, save that error here.
dw_offset_t GetAbbrevOffset() const
dw_addr_t m_loclists_base
Value of DW_AT_loclists_base.
void ExtractUnitDIEIfNeeded()
void SetDwoError(const lldb_private::Status &error)
Set the fission .dwo file specific error for this compile unit.
DWARFDebugInfoEntry::collection m_die_array
const DWARFDebugInfoEntry * GetUnitDIEPtrOnly()
void ExtractUnitDIENoDwoIfNeeded()
void SetStrOffsetsBase(dw_offset_t str_offsets_base)
dw_offset_t m_str_offsets_base
void SetLoclistsBase(dw_addr_t loclists_base)
void SetDwoStrOffsetsBase()
const DWARFDebugAranges & GetFunctionAranges()
std::optional< uint64_t > m_language_type
dw_offset_t GetLineTableOffset()
dw_offset_t GetFirstDIEOffset() const
std::atomic< bool > m_cancel_scopes
bool m_has_parsed_non_skeleton_unit
bool Supports_DW_AT_APPLE_objc_complete_type()
const lldb_private::DWARFDataExtractor & GetData() const
Get the data that contains the DIE information for this unit.
bool DW_AT_decl_file_attributes_are_invalid()
DWARFBaseDIE GetUnitDIEOnly()
dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const
std::optional< uint64_t > m_gnu_addr_base
uint16_t GetVersion() const
std::optional< llvm::DWARFDebugRnglistTable > m_rnglist_table
lldb_private::LazyBool m_is_optimized
llvm::VersionTuple m_producer_version
dw_addr_t GetAddrBase() const
void SetBaseAddress(dw_addr_t base_addr)
void ComputeCompDirAndGuessPathStyle()
std::optional< dw_addr_t > m_addr_base
Value of DW_AT_addr_base.
void SetUserData(void *d)
size_t GetDebugInfoSize() const
lldb_private::DWARFDataExtractor GetLocationData() const
const std::optional< llvm::DWARFDebugRnglistTable > & GetRnglistTable()
llvm::sys::RWMutex m_die_array_scoped_mutex
const lldb_private::FileSpec & GetAbsolutePath()
llvm::Expected< uint64_t > GetRnglistOffset(uint32_t Index)
Return a rangelist's offset based on an index.
std::optional< lldb_private::FileSpec > m_file_spec
dw_addr_t GetStrOffsetsBase() const
bool Supports_unnamed_objc_bitfields()
uint8_t GetAddressByteSize() const
std::unique_ptr< llvm::DWARFLocationTable > GetLocationTable(const lldb_private::DataExtractor &data) const
Return the location table for parsing the given location list data.
llvm::sys::RWMutex m_die_array_mutex
DWARFDebugInfoEntry m_first_die
void SetRangesBase(dw_addr_t ranges_base)
uint32_t GetHeaderByteSize() const
Get the size in bytes of the unit header.
ScopedExtractDIEs ExtractDIEsScoped()
void SetAddrBase(dw_addr_t addr_base)
static llvm::Expected< DWARFUnitSP > extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid, const lldb_private::DWARFDataExtractor &debug_info, DIERef::Section section, lldb::offset_t *offset_ptr)
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()
llvm::VersionTuple GetProducerVersion()
static uint8_t GetDefaultAddressSize()
std::optional< uint64_t > m_gnu_ranges_base
dw_offset_t GetNextUnitOffset() const
uint64_t GetDWARFLanguageType()
SymbolFileDWARF & m_dwarf
void ComputeAbsolutePath()
std::optional< uint64_t > m_dwo_id
Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5).
const DWARFDebugInfoEntry * DIEPtr()
size_t GetLengthByteSize() const
Get the size in bytes of the length field in the header.
std::shared_ptr< DWARFUnit > m_dwo
DWARFUnit(SymbolFileDWARF &dwarf, lldb::user_id_t uid, const DWARFUnitHeader &header, const DWARFAbbreviationDeclarationSet &abbrevs, DIERef::Section section, bool is_dwo)
std::optional< uint64_t > GetStringOffsetSectionItem(uint32_t index) const
bool ContainsDIEOffset(dw_offset_t die_offset) const
std::unique_ptr< DWARFDebugAranges > m_func_aranges_up
DWARFUnit & GetNonSkeletonUnit()
DWARFDIE GetDIE(dw_offset_t die_offset)
const DWARFAbbreviationDeclarationSet * GetAbbreviations() const
lldb::ByteOrder GetByteOrder() const
void AddUnitDIE(const DWARFDebugInfoEntry &cu_die)
dw_offset_t m_line_table_offset
Value of DW_AT_stmt_list.
const DWARFAbbreviationDeclarationSet * m_abbrevs
uint32_t GetLength() const
DWARFProducer GetProducer()
const lldb_private::FileSpec & GetCompilationDirectory()
std::optional< uint64_t > GetDWOId()
dw_addr_t GetBaseAddress() const
std::optional< lldb_private::FileSpec > m_comp_dir
dw_addr_t m_ranges_base
Value of DW_AT_rnglists_base.
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::optional< llvm::DWARFListTableHeader > m_loclist_table_header
lldb_private::DWARFDataExtractor GetRnglistData() const
lldb_private::FileSpec GetFile(size_t file_idx)
llvm::sys::RWMutex m_first_die_mutex
dw_offset_t GetOffset() const
static bool SupportedVersion(uint16_t version)
DWARFDebugRanges * GetDebugRanges()
lldb_private::StatsDuration & GetDebugInfoParseTimeRef()
lldb_private::FileSpec GetFile(DWARFUnit &unit, size_t file_idx)
std::shared_ptr< SymbolFileDWARFDwo > GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu, const DWARFDebugInfoEntry &cu_die)
lldb_private::DWARFContext & GetDWARFContext()
const DWARFDataExtractor & getOrLoadLocListsData()
const DWARFDataExtractor & getOrLoadStrOffsetsData()
llvm::DWARFContext & GetAsLLVM()
const DWARFDataExtractor & getOrLoadDebugTypesData()
const DWARFDataExtractor & getOrLoadDebugInfoData()
const DWARFDataExtractor & getOrLoadRngListsData()
const DWARFDataExtractor & getOrLoadLocData()
const DWARFDataExtractor & getOrLoadAddrData()
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
#define DW_INVALID_OFFSET
#define LLDB_INVALID_ADDRESS
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.