[DWARF] Reimplement/simplify DWARFUnit::GetID

Summary:
The implementation of GetID used a relatively complicated algorithm,
which returned some kind of an offset of the unit in some file
(depending on the debug info flavour). The only thing this ID was used
for was to enable subseqent retrieval of the unit from the SymbolFile.

This can be made simpler if we just make the "ID" of the unit an index
into the list of the units belonging to the symbol file. We already
support indexed access to the units, so each unit already has a well
"index" -- this just makes it accessible from within the unit.

To make the distincion between "id" and "offset" clearer (and help catch
any misuses), I also rename DWARFDebugInfo::GetCompileUnit (which
accesses by offset) into DWARFDebugInfo::GetCompileUnitAtOffset.

On its own, this only brings a minor simplification, but it enables
further simplifications in the DIERef class (coming in a follow-up
patch).

Reviewers: JDevlieghere, clayborg, aprantl

Subscribers: arphaman, jdoerfert, lldb-commits, tberghammer, jankratochvil

Differential Revision: https://reviews.llvm.org/D61481

llvm-svn: 360014
This commit is contained in:
Pavel Labath 2019-05-06 07:45:28 +00:00
parent a778074165
commit 2a0cfcce6a
8 changed files with 31 additions and 41 deletions

View File

@ -15,17 +15,19 @@
using namespace lldb; using namespace lldb;
using namespace lldb_private; using namespace lldb_private;
DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF *dwarf2Data) DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF *dwarf2Data,
: DWARFUnit(dwarf2Data) {} lldb::user_id_t uid)
: DWARFUnit(dwarf2Data, uid) {}
llvm::Expected<DWARFUnitSP>
DWARFCompileUnit::extract(SymbolFileDWARF *dwarf2Data, llvm::Expected<DWARFUnitSP> DWARFCompileUnit::extract(
const DWARFDataExtractor &debug_info, SymbolFileDWARF *dwarf2Data, user_id_t uid,
lldb::offset_t *offset_ptr) { const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr) {
assert(debug_info.ValidOffset(*offset_ptr)); assert(debug_info.ValidOffset(*offset_ptr));
// std::make_shared would require the ctor to be public. // std::make_shared would require the ctor to be public.
std::shared_ptr<DWARFCompileUnit> cu_sp(new DWARFCompileUnit(dwarf2Data)); std::shared_ptr<DWARFCompileUnit> cu_sp(
new DWARFCompileUnit(dwarf2Data, uid));
cu_sp->m_offset = *offset_ptr; cu_sp->m_offset = *offset_ptr;

View File

@ -15,7 +15,7 @@
class DWARFCompileUnit : public DWARFUnit { class DWARFCompileUnit : public DWARFUnit {
public: public:
static llvm::Expected<DWARFUnitSP> static llvm::Expected<DWARFUnitSP>
extract(SymbolFileDWARF *dwarf2Data, extract(SymbolFileDWARF *dwarf2Data, lldb::user_id_t uid,
const lldb_private::DWARFDataExtractor &debug_info, const lldb_private::DWARFDataExtractor &debug_info,
lldb::offset_t *offset_ptr); lldb::offset_t *offset_ptr);
void Dump(lldb_private::Stream *s) const override; void Dump(lldb_private::Stream *s) const override;
@ -35,7 +35,7 @@ public:
uint32_t GetHeaderByteSize() const override; uint32_t GetHeaderByteSize() const override;
private: private:
DWARFCompileUnit(SymbolFileDWARF *dwarf2Data); DWARFCompileUnit(SymbolFileDWARF *dwarf2Data, lldb::user_id_t uid);
DISALLOW_COPY_AND_ASSIGN(DWARFCompileUnit); DISALLOW_COPY_AND_ASSIGN(DWARFCompileUnit);
}; };

View File

@ -87,8 +87,8 @@ void DWARFDebugInfo::ParseCompileUnitHeadersIfNeeded() {
const auto &debug_info_data = m_dwarf2Data->get_debug_info_data(); const auto &debug_info_data = m_dwarf2Data->get_debug_info_data();
while (debug_info_data.ValidOffset(offset)) { while (debug_info_data.ValidOffset(offset)) {
llvm::Expected<DWARFUnitSP> cu_sp = llvm::Expected<DWARFUnitSP> cu_sp = DWARFCompileUnit::extract(
DWARFCompileUnit::extract(m_dwarf2Data, debug_info_data, &offset); m_dwarf2Data, m_compile_units.size(), debug_info_data, &offset);
if (!cu_sp) { if (!cu_sp) {
// FIXME: Propagate this error up. // FIXME: Propagate this error up.
@ -111,7 +111,7 @@ size_t DWARFDebugInfo::GetNumCompileUnits() {
return m_compile_units.size(); return m_compile_units.size();
} }
DWARFUnit *DWARFDebugInfo::GetCompileUnitAtIndex(uint32_t idx) { DWARFUnit *DWARFDebugInfo::GetCompileUnitAtIndex(user_id_t idx) {
DWARFUnit *cu = NULL; DWARFUnit *cu = NULL;
if (idx < GetNumCompileUnits()) if (idx < GetNumCompileUnits())
cu = m_compile_units[idx].get(); cu = m_compile_units[idx].get();
@ -123,8 +123,8 @@ bool DWARFDebugInfo::OffsetLessThanCompileUnitOffset(
return offset < cu_sp->GetOffset(); return offset < cu_sp->GetOffset();
} }
DWARFUnit *DWARFDebugInfo::GetCompileUnit(dw_offset_t cu_offset, DWARFUnit *DWARFDebugInfo::GetCompileUnitAtOffset(dw_offset_t cu_offset,
uint32_t *idx_ptr) { uint32_t *idx_ptr) {
DWARFUnitSP cu_sp; DWARFUnitSP cu_sp;
uint32_t cu_idx = DW_INVALID_INDEX; uint32_t cu_idx = DW_INVALID_INDEX;
if (cu_offset != DW_INVALID_OFFSET) { if (cu_offset != DW_INVALID_OFFSET) {
@ -160,7 +160,7 @@ DWARFUnit *DWARFDebugInfo::GetCompileUnit(const DIERef &die_ref) {
if (die_ref.cu_offset == DW_INVALID_OFFSET) if (die_ref.cu_offset == DW_INVALID_OFFSET)
return GetCompileUnitContainingDIEOffset(die_ref.die_offset); return GetCompileUnitContainingDIEOffset(die_ref.die_offset);
else else
return GetCompileUnit(die_ref.cu_offset); return GetCompileUnitAtOffset(die_ref.cu_offset);
} }
DWARFUnit * DWARFUnit *

View File

@ -40,8 +40,9 @@ public:
void SetDwarfData(SymbolFileDWARF *dwarf2Data); void SetDwarfData(SymbolFileDWARF *dwarf2Data);
size_t GetNumCompileUnits(); size_t GetNumCompileUnits();
DWARFUnit *GetCompileUnitAtIndex(uint32_t idx); DWARFUnit *GetCompileUnitAtIndex(lldb::user_id_t idx);
DWARFUnit *GetCompileUnit(dw_offset_t cu_offset, uint32_t *idx_ptr = NULL); DWARFUnit *GetCompileUnitAtOffset(dw_offset_t cu_offset,
uint32_t *idx_ptr = NULL);
DWARFUnit *GetCompileUnitContainingDIEOffset(dw_offset_t die_offset); DWARFUnit *GetCompileUnitContainingDIEOffset(dw_offset_t die_offset);
DWARFUnit *GetCompileUnit(const DIERef &die_ref); DWARFUnit *GetCompileUnit(const DIERef &die_ref);
DWARFDIE GetDIEForDIEOffset(dw_offset_t die_offset); DWARFDIE GetDIEForDIEOffset(dw_offset_t die_offset);

View File

@ -29,8 +29,8 @@ using namespace std;
extern int g_verbose; extern int g_verbose;
DWARFUnit::DWARFUnit(SymbolFileDWARF *dwarf) DWARFUnit::DWARFUnit(SymbolFileDWARF *dwarf, user_id_t uid)
: m_dwarf(dwarf), m_cancel_scopes(false) {} : UserID(uid), m_dwarf(dwarf), m_cancel_scopes(false) {}
DWARFUnit::~DWARFUnit() {} DWARFUnit::~DWARFUnit() {}
@ -366,15 +366,6 @@ size_t DWARFUnit::AppendDIEsWithTag(const dw_tag_t tag,
return dies.size() - old_size; return dies.size() - old_size;
} }
lldb::user_id_t DWARFUnit::GetID() const {
dw_offset_t local_id =
m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset;
if (m_dwarf)
return DIERef(local_id, local_id).GetUID(m_dwarf);
else
return local_id;
}
dw_offset_t DWARFUnit::GetNextCompileUnitOffset() const { dw_offset_t DWARFUnit::GetNextCompileUnitOffset() const {
return m_offset + GetLengthByteSize() + GetLength(); return m_offset + GetLengthByteSize() + GetLength();
} }

View File

@ -31,7 +31,7 @@ enum DWARFProducer {
eProcucerOther eProcucerOther
}; };
class DWARFUnit { class DWARFUnit : public lldb_private::UserID {
using die_iterator_range = using die_iterator_range =
llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>; llvm::iterator_range<DWARFDebugInfoEntry::collection::iterator>;
@ -74,7 +74,6 @@ public:
virtual uint32_t GetHeaderByteSize() const = 0; virtual uint32_t GetHeaderByteSize() const = 0;
// Offset of the initial length field. // Offset of the initial length field.
dw_offset_t GetOffset() const { return m_offset; } dw_offset_t GetOffset() const { return m_offset; }
lldb::user_id_t GetID() const;
/// Get the size in bytes of the length field in the header. /// Get the size in bytes of the length field in the header.
/// ///
/// In DWARF32 this is just 4 bytes /// In DWARF32 this is just 4 bytes
@ -169,7 +168,7 @@ public:
} }
protected: protected:
DWARFUnit(SymbolFileDWARF *dwarf); DWARFUnit(SymbolFileDWARF *dwarf, lldb::user_id_t uid);
SymbolFileDWARF *m_dwarf = nullptr; SymbolFileDWARF *m_dwarf = nullptr;
std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file; std::unique_ptr<SymbolFileDWARFDwo> m_dwo_symbol_file;

View File

@ -55,7 +55,7 @@ DIERef DebugNamesDWARFIndex::ToDIERef(const DebugNames::Entry &entry) {
if (!cu_offset) if (!cu_offset)
return DIERef(); return DIERef();
DWARFUnit *cu = m_debug_info.GetCompileUnit(*cu_offset); DWARFUnit *cu = m_debug_info.GetCompileUnitAtOffset(*cu_offset);
if (!cu) if (!cu)
return DIERef(); return DIERef();
@ -164,7 +164,7 @@ void DebugNamesDWARFIndex::GetCompleteObjCClass(ConstString class_name,
if (!ref) if (!ref)
continue; continue;
DWARFUnit *cu = m_debug_info.GetCompileUnit(ref.cu_offset); DWARFUnit *cu = m_debug_info.GetCompileUnitAtOffset(ref.cu_offset);
if (!cu || !cu->Supports_DW_AT_APPLE_objc_complete_type()) { if (!cu || !cu->Supports_DW_AT_APPLE_objc_complete_type()) {
incomplete_types.push_back(ref); incomplete_types.push_back(ref);
continue; continue;

View File

@ -689,11 +689,8 @@ SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
DWARFDebugInfo *info = DebugInfo(); DWARFDebugInfo *info = DebugInfo();
if (info) { if (info) {
// Just a normal DWARF file whose user ID for the compile unit is the DWARF // The compile unit ID is the index of the DWARF unit.
// offset itself DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(comp_unit->GetID());
DWARFUnit *dwarf_cu =
info->GetCompileUnit((dw_offset_t)comp_unit->GetID());
if (dwarf_cu && dwarf_cu->GetUserData() == NULL) if (dwarf_cu && dwarf_cu->GetUserData() == NULL)
dwarf_cu->SetUserData(comp_unit); dwarf_cu->SetUserData(comp_unit);
return dwarf_cu; return dwarf_cu;
@ -781,7 +778,7 @@ lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu,
// Figure out the compile unit index if we weren't given one // Figure out the compile unit index if we weren't given one
if (cu_idx == UINT32_MAX) if (cu_idx == UINT32_MAX)
DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx); cu_idx = dwarf_cu->GetID();
m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
cu_idx, cu_sp); cu_idx, cu_sp);
@ -1764,7 +1761,7 @@ uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr,
} else { } else {
uint32_t cu_idx = DW_INVALID_INDEX; uint32_t cu_idx = DW_INVALID_INDEX;
DWARFUnit *dwarf_cu = DWARFUnit *dwarf_cu =
debug_info->GetCompileUnit(cu_offset, &cu_idx); debug_info->GetCompileUnitAtOffset(cu_offset, &cu_idx);
if (dwarf_cu) { if (dwarf_cu) {
sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
if (sc.comp_unit) { if (sc.comp_unit) {
@ -3121,7 +3118,7 @@ size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) {
return num_variables; return num_variables;
} }
} else if (sc.comp_unit) { } else if (sc.comp_unit) {
DWARFUnit *dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()); DWARFUnit *dwarf_cu = info->GetCompileUnitAtIndex(sc.comp_unit->GetID());
if (dwarf_cu == NULL) if (dwarf_cu == NULL)
return 0; return 0;