16#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
18#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
19#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
20#include "llvm/Object/Error.h"
38 const llvm::DWARFUnitHeader &header,
39 const llvm::DWARFAbbreviationDeclarationSet &abbrevs,
41 :
UserID(uid), m_dwarf(
dwarf), m_header(header), m_abbrevs(&abbrevs),
42 m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo),
43 m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.getDWOId()) {}
87 std::shared_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
96 "unable to load .dwo file from \"{0}\" due to ID ({1:x16}) mismatch "
97 "for skeleton DIE at {2:x8}",
98 dwo_symbol_file->GetObjectFile()->GetFileSpec().GetPath(), *
m_dwo_id,
107 "multiple compile units with Dwo ID {0:x16}", *
m_dwo_id));
115 "unable to extract compile unit DIE from .dwo file for skeleton "
134 else if (dwo_symbol_file->GetDWARFContext()
135 .getOrLoadRngListsData()
137 dwo_cu->
SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
140 dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() >
142 dwo_cu->
SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32));
146 m_dwo = std::shared_ptr<DWARFUnit>(std::move(dwo_symbol_file), dwo_cu);
198 m_cu->m_die_array_scoped_mutex.unlock_shared();
199 if (!m_clear_dies || m_cu->m_cancel_scopes)
202 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
203 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
204 if (m_cu->m_cancel_scopes)
206 m_cu->ClearDIEsRWLocked();
210 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
218 m_clear_dies = rhs.m_clear_dies;
230 llvm::formatv(
"{0:x16}: DWARFUnit::ExtractDIEsIfNeeded()",
GetOffset())
245 std::vector<uint32_t> die_index_stack;
246 die_index_stack.reserve(32);
247 die_index_stack.push_back(0);
248 bool prev_die_had_children =
false;
249 while (offset < next_cu_offset && die.
Extract(data, *
this, &offset)) {
250 const bool null_die = die.
IsNULL();
252 assert(
m_die_array.empty() &&
"Compile unit DIE already added");
280 if (prev_die_had_children) {
291 if (die_index_stack.back())
292 m_die_array[die_index_stack.back()].SetSiblingIndex(
302 if (!die_index_stack.empty())
303 die_index_stack.pop_back();
307 prev_die_had_children =
false;
312 if (die_has_children) {
313 die_index_stack.push_back(0);
316 prev_die_had_children = die_has_children;
340 m_dwo->ExtractDIEsIfNeeded();
352 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
353 if (
const auto *contribution =
354 entry->getContribution(llvm::DW_SECT_STR_OFFSETS))
355 baseOffset = contribution->getOffset();
363 uint64_t length = strOffsets.
GetU32(&baseOffset);
364 if (length == 0xffffffff)
365 length = strOffsets.
GetU64(&baseOffset);
368 if (strOffsets.
GetU16(&baseOffset) < 5)
388 for (
size_t i = 0; i < attributes.
Size(); ++i) {
398 for (
size_t i = 0; i < attributes.
Size(); ++i) {
406 case DW_AT_loclists_base:
409 case DW_AT_rnglists_base:
412 case DW_AT_str_offsets_base:
423 case DW_AT_stmt_list:
426 case DW_AT_GNU_addr_base:
429 case DW_AT_GNU_ranges_base:
432 case DW_AT_GNU_dwo_id:
449const llvm::DWARFAbbreviationDeclarationSet *
467template <
typename ListTableType>
468static llvm::Expected<ListTableType>
470 DwarfFormat format) {
478 return ListTableType();
481 uint64_t HeaderSize = llvm::DWARFListTableHeader::getHeaderSize(format);
482 if (offset < HeaderSize)
483 return llvm::createStringError(std::errc::invalid_argument,
484 "did not detect a valid"
485 " list table with base = 0x%" PRIx64
"\n",
487 offset -= HeaderSize;
489 if (llvm::Error E = Table.extractHeaderAndOffsets(data, &offset))
496 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
497 const auto *contribution = entry->getContribution(llvm::DW_SECT_LOCLISTS);
500 "Failed to find location list contribution for CU with DWO Id "
505 offset += contribution->getOffset();
509 uint64_t header_size = llvm::DWARFListTableHeader::getHeaderSize(DWARF32);
510 if (loclists_base < header_size)
514 offset += loclists_base - header_size;
519 "Failed to extract location list table at offset {0:x16} (location "
520 "list base: {1:x16}): {2}",
521 offset, loclists_base,
toString(std::move(E)).c_str());
525std::unique_ptr<llvm::DWARFLocationTable>
527 llvm::DWARFDataExtractor llvm_data(
532 return std::make_unique<llvm::DWARFDebugLoclists>(llvm_data,
GetVersion());
533 return std::make_unique<llvm::DWARFDebugLoc>(llvm_data);
540 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
541 if (
const auto *contribution = entry->getContribution(
542 GetVersion() >= 5 ? llvm::DW_SECT_LOCLISTS : llvm::DW_SECT_EXT_LOC))
544 contribution->getLength32());
553 if (
const llvm::DWARFUnitIndex::Entry *entry =
m_header.getIndexEntry()) {
554 if (
const auto *contribution =
555 entry->getContribution(llvm::DW_SECT_RNGLISTS))
557 contribution->getLength32());
559 "Failed to find range list contribution for CU with signature {0:x16}",
560 entry->getSignature());
573const std::optional<llvm::DWARFDebugRnglistTable> &
577 if (
auto table_or_error =
578 ParseListTableHeader<llvm::DWARFDebugRnglistTable>(
583 "Failed to extract range list table at offset {0:x16}: {1}",
592 return llvm::createStringError(std::errc::invalid_argument,
593 "missing or invalid range list table");
595 return llvm::createStringError(
596 std::errc::invalid_argument,
597 llvm::formatv(
"DW_FORM_rnglistx cannot be used without "
598 "DW_AT_rnglists_base for CU at {0:x16}",
605 return llvm::createStringError(
606 std::errc::invalid_argument,
607 "invalid range list table index %u; OffsetEntryCount is %u, "
608 "DW_AT_rnglists_base is %" PRIu64,
622 if (data.ValidOffsetForDataOfSize(offset, index_size))
633 m_dwo->ClearDIEsRWLocked();
660 "GetDIE for DIE {0:x16} is outside of its CU {1:x16}", die_offset,
670 if (pos != end && die_offset == (*pos).GetOffset())
678 return llvm::StringRef();
681 if (
const char *name =
686 for (
auto attr : {DW_AT_specification, DW_AT_abstract_origin}) {
692 if (
auto name = unit->PeekDIEName(offset); !name.empty())
696 return llvm::StringRef();
721 if (candidate_skeleton_unit)
725 return llvm::dyn_cast_or_null<DWARFCompileUnit>(
m_skeleton_unit.load());
730 if (
m_skeleton_unit.compare_exchange_strong(expected_unit, &skeleton_unit))
732 if (expected_unit == &skeleton_unit) {
752 llvm::StringRef producer(
754 if (producer.empty())
758 llvm::StringRef(R
"(swiftlang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
760 llvm::StringRef(R
"(clang-([0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?))"));
762 llvm::SmallVector<llvm::StringRef, 3> matches;
763 if (g_swiftlang_version_regex.
Execute(producer, &matches)) {
766 }
else if (producer.contains(
"clang")) {
767 if (g_clang_version_regex.
Execute(producer, &matches))
770 }
else if (producer.contains(
"GNU")) {
837static llvm::StringRef
839 if (!path_from_dwarf.contains(
':'))
840 return path_from_dwarf;
841 llvm::StringRef host, path;
842 std::tie(host, path) = path_from_dwarf.split(
':');
844 if (host.contains(
'/'))
845 return path_from_dwarf;
849 if (host.size() == 1 && llvm::isAlpha(host[0]) &&
850 (path.starts_with(
"\\") || path.starts_with(
"/")))
851 return path_from_dwarf;
864 if (!comp_dir.empty()) {
893 if (load_all_debug_info)
896 return &llvm::cast<SymbolFileDWARFDwo>(
m_dwo->GetSymbolFileDWARF());
914 const bool minimize =
false;
920llvm::Expected<DWARFUnitSP>
930 llvm::DWARFSectionKind section_kind_llvm =
932 ? llvm::DWARFSectionKind::DW_SECT_INFO
933 : llvm::DWARFSectionKind::DW_SECT_EXT_TYPES;
935 llvm::DWARFDataExtractor debug_info_llvm = debug_info.
GetAsLLVMDWARF();
936 llvm::DWARFUnitHeader header;
937 if (llvm::Error extract_err = header.extract(
938 context.
GetAsLLVM(), debug_info_llvm, offset_ptr, section_kind_llvm))
939 return std::move(extract_err);
941 if (context.
isDwo()) {
942 const llvm::DWARFUnitIndex::Entry *entry =
nullptr;
943 const llvm::DWARFUnitIndex &index = header.isTypeUnit()
947 if (header.isTypeUnit())
948 entry = index.getFromHash(header.getTypeHash());
949 else if (
auto dwo_id = header.getDWOId())
950 entry = index.getFromHash(*dwo_id);
953 entry = index.getFromOffset(header.getOffset());
955 if (llvm::Error err = header.applyIndexEntry(entry))
956 return std::move(err);
959 const llvm::DWARFDebugAbbrev *abbr =
dwarf.DebugAbbrev();
961 return llvm::make_error<llvm::object::GenericBinaryError>(
962 "No debug_abbrev data");
964 bool abbr_offset_OK =
965 dwarf.GetDWARFContext().getOrLoadAbbrevData().ValidOffset(
966 header.getAbbrOffset());
968 return llvm::make_error<llvm::object::GenericBinaryError>(
969 "Abbreviation offset for unit is not valid");
971 llvm::Expected<const llvm::DWARFAbbreviationDeclarationSet *> abbrevs_or_err =
972 abbr->getAbbreviationDeclarationSet(header.getAbbrOffset());
974 return abbrevs_or_err.takeError();
976 const llvm::DWARFAbbreviationDeclarationSet *abbrevs = *abbrevs_or_err;
978 return llvm::make_error<llvm::object::GenericBinaryError>(
979 "No abbrev exists at the specified offset.");
981 bool is_dwo =
dwarf.GetDWARFContext().isDwo();
982 if (header.isTypeUnit())
997 case llvm::dwarf::DW_UT_compile:
998 case llvm::dwarf::DW_UT_partial:
1000 case llvm::dwarf::DW_UT_skeleton:
1001 case llvm::dwarf::DW_UT_split_compile:
1003 case llvm::dwarf::DW_UT_type:
1004 case llvm::dwarf::DW_UT_split_type:
1007 llvm_unreachable(
"invalid UnitType.");
1010std::optional<uint64_t>
1016llvm::Expected<llvm::DWARFAddressRangesVector>
1019 llvm::DWARFDataExtractor data =
1021 data.setAddressSize(
m_header.getAddressByteSize());
1023 llvm::DWARFDebugRangeList list;
1024 if (llvm::Error e = list.extract(data, &offset))
1026 return list.getAbsoluteRanges(
1032 return llvm::createStringError(std::errc::invalid_argument,
1033 "missing or invalid range list table");
1038 data.setAddressSize(
m_header.getAddressByteSize());
1040 if (!range_list_or_error)
1041 return range_list_or_error.takeError();
1043 return range_list_or_error->getAbsoluteRanges(
1045 [&](uint32_t index) {
1050 return llvm::object::SectionedAddress{
1052 &offset, index_size)};
1056llvm::Expected<llvm::DWARFAddressRangesVector>
1060 return maybe_offset.takeError();
1067 return m_dwo->HasAny(tags);
1070 for (
const auto tag : tags) {
1071 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.
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 static Status FromErrorStringWithFormatv(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 & getOrLoadRangesData()
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
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,...
const char * GetAttributeValueAsString(const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, bool check_elaborating_dies=false) const
uint64_t GetAttributeValueAsUnsigned(const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, bool check_elaborating_dies=false) const
bool Extract(const DWARFDataExtractor &data, const DWARFUnit &cu, lldb::offset_t *offset_ptr)
dw_offset_t GetAttributeValue(const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &formValue, dw_offset_t *end_attr_offset_ptr=nullptr, bool check_elaborating_dies=false) const
collection::const_iterator const_iterator
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()
llvm::Expected< llvm::DWARFAddressRangesVector > FindRnglistFromOffset(dw_offset_t offset)
Return a list of address ranges resulting from a (possibly encoded) range list starting at a given of...
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()
const DWARFDataExtractor & GetData() const
Get the data that contains the DIE information for this unit.
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)
void ExtractDIEsRWLocked()
void SetDwoError(Status &&error)
Set the fission .dwo file specific error for this compile unit.
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).
llvm::Expected< llvm::DWARFAddressRangesVector > FindRnglistFromIndex(uint32_t index)
Return a list of address ranges retrieved from an encoded range list whose offset is found via a tabl...
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)
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.
std::atomic< DWARFUnit * > m_skeleton_unit
DWARFDataExtractor GetRnglistData() const
FileSpec GetFile(size_t file_idx)
SymbolFileDWARF & GetBaseSymbolFile() const
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.