Skip to content

Commit 4e7682b

Browse files
authored
[lldb] rm DWARFDebugRanges (#116379)
The class is only used from one place, which is trivial to implement using the llvm class. The main difference is that in the new implementation, the ranges are parsed each time anew (instead of being parsed at startup and cached). I believe this is fine because: - this is already how things work with DWARF v5 debug_rnglists - parsing debug_ranges is fairly fast (definitely faster than rnglists) - generally, this result will be cached at a higher level anyway. Browsing the code I did find one instance where that is not the case -- SymbolFileDWARF::ResolveFunctionAndBlock -- which is called each time we resolve an address (to the block level). However, this function is already pretty suboptimal: it first traverses the DIE tree (which involves parsing all the DIE attributes) to find the correct block, then it parses them again to construct the `lldb_private::Block` representation, and *then* it uses the ID of the block DIE it found in the first step to look up the `Block` object. If this turns out to be a bottleneck, I think there are better ways to optimize it than caching the debug_ranges parse. The motiviation for this is that DWARFDebugRanges sorts the block ranges, even though the order of the ranges is load-bearing (in the absence of DW_AT_low_pc, the "base address" of a scope is determined by the first range entry). Delaying the parsing (and sorting) step makes it easier to access the first entry.
1 parent db90673 commit 4e7682b

File tree

9 files changed

+42
-145
lines changed

9 files changed

+42
-145
lines changed

lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ add_lldb_library(lldbPluginSymbolFileDWARF PLUGIN
2121
DWARFDebugInfo.cpp
2222
DWARFDebugInfoEntry.cpp
2323
DWARFDebugMacro.cpp
24-
DWARFDebugRanges.cpp
2524
DWARFDeclContext.cpp
2625
DWARFDefines.cpp
2726
DWARFDIE.cpp

lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "DWARFCompileUnit.h"
2626
#include "DWARFDebugAranges.h"
2727
#include "DWARFDebugInfo.h"
28-
#include "DWARFDebugRanges.h"
2928
#include "DWARFDeclContext.h"
3029
#include "DWARFFormValue.h"
3130
#include "DWARFUnit.h"

lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "DWARFAttribute.h"
1616
#include "DWARFBaseDIE.h"
17-
#include "DWARFDebugRanges.h"
1817
#include <map>
1918
#include <optional>
2019
#include <set>

lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugRanges.cpp

Lines changed: 0 additions & 56 deletions
This file was deleted.

lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugRanges.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include "lldb/Utility/LLDBAssert.h"
1414
#include "lldb/Utility/StreamString.h"
1515
#include "lldb/Utility/Timer.h"
16+
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
1617
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
1718
#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
19+
#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
1820
#include "llvm/Object/Error.h"
1921

2022
#include "DWARFCompileUnit.h"
@@ -1029,43 +1031,49 @@ DWARFUnit::GetStringOffsetSectionItem(uint32_t index) const {
10291031

10301032
llvm::Expected<DWARFRangeList>
10311033
DWARFUnit::FindRnglistFromOffset(dw_offset_t offset) {
1034+
llvm::DWARFAddressRangesVector llvm_ranges;
10321035
if (GetVersion() <= 4) {
1033-
const DWARFDebugRanges *debug_ranges = m_dwarf.GetDebugRanges();
1034-
if (!debug_ranges)
1035-
return llvm::make_error<llvm::object::GenericBinaryError>(
1036-
"No debug_ranges section");
1037-
return debug_ranges->FindRanges(this, offset);
1036+
llvm::DWARFDataExtractor data =
1037+
m_dwarf.GetDWARFContext().getOrLoadRangesData().GetAsLLVMDWARF();
1038+
data.setAddressSize(m_header.getAddressByteSize());
1039+
1040+
llvm::DWARFDebugRangeList list;
1041+
if (llvm::Error e = list.extract(data, &offset))
1042+
return e;
1043+
llvm_ranges = list.getAbsoluteRanges(
1044+
llvm::object::SectionedAddress{GetBaseAddress()});
1045+
} else {
1046+
if (!GetRnglistTable())
1047+
return llvm::createStringError(std::errc::invalid_argument,
1048+
"missing or invalid range list table");
1049+
1050+
llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVMDWARF();
1051+
1052+
// As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1053+
data.setAddressSize(m_header.getAddressByteSize());
1054+
auto range_list_or_error = GetRnglistTable()->findList(data, offset);
1055+
if (!range_list_or_error)
1056+
return range_list_or_error.takeError();
1057+
1058+
llvm::Expected<llvm::DWARFAddressRangesVector> expected_llvm_ranges =
1059+
range_list_or_error->getAbsoluteRanges(
1060+
llvm::object::SectionedAddress{GetBaseAddress()},
1061+
GetAddressByteSize(), [&](uint32_t index) {
1062+
uint32_t index_size = GetAddressByteSize();
1063+
dw_offset_t addr_base = GetAddrBase();
1064+
lldb::offset_t offset =
1065+
addr_base + static_cast<lldb::offset_t>(index) * index_size;
1066+
return llvm::object::SectionedAddress{
1067+
m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
1068+
&offset, index_size)};
1069+
});
1070+
if (!expected_llvm_ranges)
1071+
return expected_llvm_ranges.takeError();
1072+
llvm_ranges = std::move(*expected_llvm_ranges);
10381073
}
10391074

1040-
if (!GetRnglistTable())
1041-
return llvm::createStringError(std::errc::invalid_argument,
1042-
"missing or invalid range list table");
1043-
1044-
llvm::DWARFDataExtractor data = GetRnglistData().GetAsLLVMDWARF();
1045-
1046-
// As DW_AT_rnglists_base may be missing we need to call setAddressSize.
1047-
data.setAddressSize(m_header.getAddressByteSize());
1048-
auto range_list_or_error = GetRnglistTable()->findList(data, offset);
1049-
if (!range_list_or_error)
1050-
return range_list_or_error.takeError();
1051-
1052-
llvm::Expected<llvm::DWARFAddressRangesVector> llvm_ranges =
1053-
range_list_or_error->getAbsoluteRanges(
1054-
llvm::object::SectionedAddress{GetBaseAddress()},
1055-
GetAddressByteSize(), [&](uint32_t index) {
1056-
uint32_t index_size = GetAddressByteSize();
1057-
dw_offset_t addr_base = GetAddrBase();
1058-
lldb::offset_t offset =
1059-
addr_base + static_cast<lldb::offset_t>(index) * index_size;
1060-
return llvm::object::SectionedAddress{
1061-
m_dwarf.GetDWARFContext().getOrLoadAddrData().GetMaxU64(
1062-
&offset, index_size)};
1063-
});
1064-
if (!llvm_ranges)
1065-
return llvm_ranges.takeError();
1066-
10671075
DWARFRangeList ranges;
1068-
for (const llvm::DWARFAddressRange &llvm_range : *llvm_ranges) {
1076+
for (const llvm::DWARFAddressRange &llvm_range : llvm_ranges) {
10691077
ranges.Append(DWARFRangeList::Entry(llvm_range.LowPC,
10701078
llvm_range.HighPC - llvm_range.LowPC));
10711079
}

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
#include "DWARFDebugAranges.h"
6464
#include "DWARFDebugInfo.h"
6565
#include "DWARFDebugMacro.h"
66-
#include "DWARFDebugRanges.h"
6766
#include "DWARFDeclContext.h"
6867
#include "DWARFFormValue.h"
6968
#include "DWARFTypeUnit.h"
@@ -737,19 +736,6 @@ DWARFCompileUnit *SymbolFileDWARF::GetDWARFCompileUnit(CompileUnit *comp_unit) {
737736
return llvm::cast_or_null<DWARFCompileUnit>(dwarf_cu);
738737
}
739738

740-
DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() {
741-
if (!m_ranges) {
742-
LLDB_SCOPED_TIMER();
743-
744-
if (m_context.getOrLoadRangesData().GetByteSize() > 0)
745-
m_ranges = std::make_unique<DWARFDebugRanges>();
746-
747-
if (m_ranges)
748-
m_ranges->Extract(m_context);
749-
}
750-
return m_ranges.get();
751-
}
752-
753739
/// Make an absolute path out of \p file_spec and remap it using the
754740
/// module's source remapping dictionary.
755741
static void MakeAbsoluteAndRemap(FileSpec &file_spec, DWARFUnit &dwarf_cu,

lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class DWARFDebugAranges;
5454
class DWARFDebugInfo;
5555
class DWARFDebugInfoEntry;
5656
class DWARFDebugLine;
57-
class DWARFDebugRanges;
5857
class DWARFDeclContext;
5958
class DWARFFormValue;
6059
class DWARFTypeUnit;
@@ -212,8 +211,6 @@ class SymbolFileDWARF : public SymbolFileCommon {
212211

213212
DWARFDebugInfo &DebugInfo();
214213

215-
DWARFDebugRanges *GetDebugRanges();
216-
217214
static bool SupportedVersion(uint16_t version);
218215

219216
DWARFDIE
@@ -533,7 +530,6 @@ class SymbolFileDWARF : public SymbolFileCommon {
533530
typedef std::set<DIERef> DIERefSet;
534531
typedef llvm::StringMap<DIERefSet> NameToOffsetMap;
535532
NameToOffsetMap m_function_scope_qualified_name_map;
536-
std::unique_ptr<DWARFDebugRanges> m_ranges;
537533
UniqueDWARFASTTypeMap m_unique_ast_type_map;
538534
// A map from DIE to lldb_private::Type. For record type, the key might be
539535
// either declaration DIE or definition DIE.

lldb/test/Shell/SymbolFile/DWARF/x86/debug_ranges-missing-section.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# RUN: %lldb %t -o "image lookup -v -s lookup_ranges" -o exit 2>%t.error | FileCheck %s
33
# RUN: cat %t.error | FileCheck %s --check-prefix ERROR
44

5-
# ERROR: DIE has DW_AT_ranges(DW_FORM_sec_offset 0x0000000000000047) attribute, but range extraction failed (No debug_ranges section),
5+
# ERROR: DIE has DW_AT_ranges(DW_FORM_sec_offset 0x0000000000000047) attribute, but range extraction failed (invalid range list offset 0x47),
66
# CHECK: Function: id = {0x0000001c}, name = "ranges", range = [0x0000000000000000-0x0000000000000004)
77
# CHECK: Blocks: id = {0x0000001c}, range = [0x00000000-0x00000004)
88

0 commit comments

Comments
 (0)