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)
377 for (
size_t i = 0; i < attributes.
Size(); ++i) {
387 for (
size_t i = 0; i < attributes.
Size(); ++i) {
395 case DW_AT_loclists_base:
398 case DW_AT_rnglists_base:
401 case DW_AT_str_offsets_base:
412 case DW_AT_stmt_list:
415 case DW_AT_GNU_addr_base:
418 case DW_AT_GNU_ranges_base:
421 case DW_AT_GNU_dwo_id:
455template <
typename ListTableType>
456static llvm::Expected<ListTableType>
458 DwarfFormat format) {
466 return ListTableType();
469 uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
470 if (offset < HeaderSize)
471 return llvm::createStringError(std::errc::invalid_argument,
472 "did not detect a valid"
473 " list table with base = 0x%" PRIx64
"\n",
475 offset -= HeaderSize;
477 if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
485 const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
488 "Failed to find location list contribution for CU with DWO Id "
493 offset += contribution->getOffset();
497 uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
498 if (loclists_base < header_size)
502 offset += loclists_base - header_size;
507 "Failed to extract location list table at offset {0:x16} (location "
508 "list base: {1:x16}): {2}",
509 offset, loclists_base,
toString(std::move(E)).c_str());
513std::unique_ptr<llvm::DWARFLocationTable>
515 llvm::DWARFDataExtractor llvm_data(
520 return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data,
GetVersion());
521 return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
529 if (
const auto *contribution = entry->getContribution(
530 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
532 contribution->getLength32());
542 if (
const auto *contribution =
543 entry->getContribution(llvm::DW_SECT_RNGLISTS))
545 contribution->getLength32());
547 "Failed to find range list contribution for CU with signature {0:x16}",
548 entry->getSignature());
561const std::optional<llvm::DWARFDebugRnglistTable> &
565 if (
auto table_or_error =
566 ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
571 "Failed to extract range list table at offset {0:x16}: {1}",
580 return llvm::createStringError(std::errc::invalid_argument,
581 "missing or invalid range list table");
583 return llvm::createStringError(
584 std::errc::invalid_argument,
585 llvm::formatv(
"DW_FORM_rnglistx cannot be used without "
586 "DW_AT_rnglists_base for CU at {0:x16}",
593 return llvm::createStringError(
594 std::errc::invalid_argument,
595 "invalid range list table index %u; OffsetEntryCount is %u, "
596 "DW_AT_rnglists_base is %" PRIu64,
610 if (data.ValidOffsetForDataOfSize(offset, index_size))
621 m_dwo->ClearDIEsRWLocked();
648 "GetDIE for DIE {0:x16} is outside of its CU {0:x16}", die_offset,
658 if (pos != end && die_offset == (*pos).GetOffset())
705 llvm::StringRef producer(
707 if (producer.empty())
711 llvm::StringRef(R
"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
713 llvm::StringRef(R
"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
715 llvm::StringRef(R
"(4\.[012]\.[01] )"
716 R"(\(Based on Apple Inc\. build [0-9]+\) )"
717 R"(\(LLVM build [\.0-9]+\)$)"));
719 llvm::SmallVector<llvm::StringRef, 3> matches;
720 if (g_swiftlang_version_regex.
Execute(producer, &matches)) {
723 }
else if (producer.contains(
"clang")) {
724 if (g_clang_version_regex.
Execute(producer, &matches))
727 }
else if (producer.contains(
"GNU")) {
729 }
else if (g_llvm_gcc_regex.
Execute(producer)) {
796static llvm::StringRef
798 if (!path_from_dwarf.contains(
':'))
799 return path_from_dwarf;
800 llvm::StringRef host, path;
801 std::tie(host, path) = path_from_dwarf.split(
':');
803 if (host.contains(
'/'))
804 return path_from_dwarf;
808 if (host.size() == 1 && llvm::isAlpha(host[0]) &&
809 (path.startswith(
"\\") || path.startswith(
"/")))
810 return path_from_dwarf;
823 if (!comp_dir.empty()) {
854 return &llvm::cast<SymbolFileDWARFDwo>(
m_dwo->GetSymbolFileDWARF());
872 const bool minimize =
false;
878llvm::Expected<DWARFUnitHeader>
891 if (header.
m_unit_type == llvm::dwarf::DW_UT_skeleton ||
892 header.
m_unit_type == llvm::dwarf::DW_UT_split_compile)
906 if (context.
isDwo()) {
907 const llvm::DWARFUnitIndex *Index;
909 Index = &context.
GetAsLLVM().getTUIndex();
913 Index = &context.
GetAsLLVM().getCUIndex();
923 return llvm::createStringError(
924 llvm::inconvertibleErrorCode(),
925 "Package unit with a non-zero abbreviation offset");
927 auto *unit_contrib = header.
m_index_entry->getContribution();
928 if (!unit_contrib || unit_contrib->getLength32() != header.
m_length + 4) {
929 return llvm::createStringError(llvm::inconvertibleErrorCode(),
930 "Inconsistent DWARF package unit index");
935 return llvm::createStringError(
936 llvm::inconvertibleErrorCode(),
937 "DWARF package index missing abbreviation column");
946 bool type_offset_OK =
950 return llvm::make_error<llvm::object::GenericBinaryError>(
951 "Invalid unit length");
953 return llvm::make_error<llvm::object::GenericBinaryError>(
954 "Unsupported unit version");
956 return llvm::make_error<llvm::object::GenericBinaryError>(
957 "Invalid unit address size");
959 return llvm::make_error<llvm::object::GenericBinaryError>(
960 "Type offset out of range");
965llvm::Expected<DWARFUnitSP>
972 debug_info, section,
dwarf.GetDWARFContext(), offset_ptr);
973 if (!expected_header)
974 return expected_header.takeError();
978 return llvm::make_error<llvm::object::GenericBinaryError>(
979 "No debug_abbrev data");
981 bool abbr_offset_OK =
982 dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
983 expected_header->GetAbbrOffset());
985 return llvm::make_error<llvm::object::GenericBinaryError>(
986 "Abbreviation offset for unit is not valid");
991 return llvm::make_error<llvm::object::GenericBinaryError>(
992 "No abbrev exists at the specified offset.");
994 bool is_dwo =
dwarf.GetDWARFContext().isDwo();
995 if (expected_header->IsTypeUnit())
999 *abbrevs, section, is_dwo));
1010 case llvm::dwarf::DW_UT_compile:
1011 case llvm::dwarf::DW_UT_partial:
1013 case llvm::dwarf::DW_UT_skeleton:
1014 case llvm::dwarf::DW_UT_split_compile:
1016 case llvm::dwarf::DW_UT_type:
1017 case llvm::dwarf::DW_UT_split_type:
1020 llvm_unreachable(
"invalid UnitType.");
1023std::optional<uint64_t>
1029llvm::Expected<DWARFRangeList>
1034 return llvm::make_error<llvm::object::GenericBinaryError>(
1035 "No debug_ranges section");
1036 return debug_ranges->
FindRanges(
this, offset);
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())
llvm::DWARFAbbreviationDeclarationSet DWARFAbbreviationDeclarationSet
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_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
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
DWARFAttributes GetAttributes(DWARFUnit *cu, Recurse recurse=Recurse::yes) const
collection::const_iterator const_iterator
DWARFRangeList FindRanges(const DWARFUnit *cu, dw_offset_t debug_ranges_offset) 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
llvm::dwarf::Attribute dw_attr_t
#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.