Skip to content

[lldb/DWARF] s/DWARFRangeList/llvm::DWARFAddressRangeVector #116620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions lldb/include/lldb/Core/dwarf.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef LLDB_CORE_DWARF_H
#define LLDB_CORE_DWARF_H

#include "lldb/Utility/RangeMap.h"
#include <cstdint>

// Get the DWARF constant definitions from llvm
Expand Down Expand Up @@ -40,6 +39,4 @@ typedef uint64_t dw_offset_t; // Dwarf Debug Information Entry offset for any

#define DW_EH_PE_MASK_ENCODING 0x0F

typedef lldb_private::RangeVector<dw_addr_t, dw_addr_t, 2> DWARFRangeList;

#endif // LLDB_CORE_DWARF_H
3 changes: 2 additions & 1 deletion lldb/source/Plugins/SymbolFile/DWARF/DIERef.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DIEREF_H

#include "lldb/Core/dwarf.h"
#include "lldb/Utility/LLDBAssert.h"
#include "lldb/lldb-defines.h"
#include "lldb/lldb-types.h"
#include <cassert>
#include <optional>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "clang/AST/Type.h"
#include "clang/Basic/Specifiers.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
#include "llvm/DebugInfo/DWARF/DWARFTypePrinter.h"
#include "llvm/Demangle/Demangle.h"

Expand Down Expand Up @@ -2360,7 +2361,7 @@ DWARFASTParserClang::ConstructDemangledNameFromDWARF(const DWARFDIE &die) {

Function *DWARFASTParserClang::ParseFunctionFromDWARF(
CompileUnit &comp_unit, const DWARFDIE &die, AddressRanges func_ranges) {
DWARFRangeList unused_func_ranges;
llvm::DWARFAddressRangesVector unused_func_ranges;
const char *name = nullptr;
const char *mangled = nullptr;
std::optional<int> decl_file;
Expand Down
19 changes: 12 additions & 7 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#include "DWARFCompileUnit.h"
#include "DWARFDebugAranges.h"
#include "LogChannelDWARF.h"
#include "SymbolFileDWARFDebugMap.h"

#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/LineTable.h"
#include "lldb/Utility/Stream.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"

using namespace lldb;
using namespace lldb_private;
Expand Down Expand Up @@ -41,14 +43,17 @@ void DWARFCompileUnit::BuildAddressRangeTable(

const dw_offset_t cu_offset = GetOffset();
if (die) {
DWARFRangeList ranges =
llvm::Expected<llvm::DWARFAddressRangesVector> ranges =
die->GetAttributeAddressRanges(this, /*check_hi_lo_pc=*/true);
for (const DWARFRangeList::Entry &range : ranges)
debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
range.GetRangeEnd());

if (!ranges.IsEmpty())
return;
if (ranges) {
for (const llvm::DWARFAddressRange &range : *ranges)
debug_aranges->AppendRange(cu_offset, range.LowPC, range.HighPC);
if (!ranges->empty())
return;
} else {
LLDB_LOG_ERROR(GetLog(DWARFLog::DebugInfo), ranges.takeError(),
"{1:x}: {0}", cu_offset);
}
}

if (debug_aranges->GetNumRanges() == num_debug_aranges) {
Expand Down
43 changes: 26 additions & 17 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
#include "DWARFDebugInfoEntry.h"
#include "DWARFDeclContext.h"
#include "DWARFUnit.h"
#include "LogChannelDWARF.h"
#include "lldb/Symbol/Type.h"

#include "llvm/ADT/iterator.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"

using namespace lldb_private;
using namespace lldb_private::dwarf;
Expand Down Expand Up @@ -172,21 +174,27 @@ DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
}

if (match_addr_range) {
DWARFRangeList ranges =
m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true);
if (ranges.FindEntryThatContains(address)) {
check_children = true;
switch (Tag()) {
default:
break;

case DW_TAG_inlined_subroutine: // Inlined Function
case DW_TAG_lexical_block: // Block { } in code
result = *this;
break;
if (llvm::Expected<llvm::DWARFAddressRangesVector> ranges =
m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true)) {
bool addr_in_range =
llvm::any_of(*ranges, [&](const llvm::DWARFAddressRange &r) {
return r.LowPC <= address && address < r.HighPC;
});
if (addr_in_range) {
switch (Tag()) {
default:
break;

case DW_TAG_inlined_subroutine: // Inlined Function
case DW_TAG_lexical_block: // Block { } in code
result = *this;
break;
}
}
check_children = addr_in_range;
} else {
check_children = false;
LLDB_LOG_ERROR(GetLog(DWARFLog::DebugInfo), ranges.takeError(),
"DIE({1:x}): {0}", GetID());
}
}

Expand Down Expand Up @@ -559,10 +567,11 @@ bool DWARFDIE::IsMethod() const {
}

bool DWARFDIE::GetDIENamesAndRanges(
const char *&name, const char *&mangled, DWARFRangeList &ranges,
std::optional<int> &decl_file, std::optional<int> &decl_line,
std::optional<int> &decl_column, std::optional<int> &call_file,
std::optional<int> &call_line, std::optional<int> &call_column,
const char *&name, const char *&mangled,
llvm::DWARFAddressRangesVector &ranges, std::optional<int> &decl_file,
std::optional<int> &decl_line, std::optional<int> &decl_column,
std::optional<int> &call_file, std::optional<int> &call_line,
std::optional<int> &call_column,
lldb_private::DWARFExpressionList *frame_base) const {
if (IsValid()) {
return m_die->GetDIENamesAndRanges(
Expand Down
11 changes: 6 additions & 5 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFDIE.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "DWARFBaseDIE.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"

namespace lldb_private::plugin {
namespace dwarf {
Expand Down Expand Up @@ -97,11 +98,11 @@ class DWARFDIE : public DWARFBaseDIE {
GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const;

bool GetDIENamesAndRanges(
const char *&name, const char *&mangled, DWARFRangeList &ranges,
std::optional<int> &decl_file, std::optional<int> &decl_line,
std::optional<int> &decl_column, std::optional<int> &call_file,
std::optional<int> &call_line, std::optional<int> &call_column,
DWARFExpressionList *frame_base) const;
const char *&name, const char *&mangled,
llvm::DWARFAddressRangesVector &ranges, std::optional<int> &decl_file,
std::optional<int> &decl_line, std::optional<int> &decl_column,
std::optional<int> &call_file, std::optional<int> &call_line,
std::optional<int> &call_column, DWARFExpressionList *frame_base) const;

// The following methods use LLVM naming convension in order to be are used by
// LLVM libraries.
Expand Down
96 changes: 46 additions & 50 deletions lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#include <limits>
#include <optional>

#include "llvm/Support/LEB128.h"

#include "LogChannelDWARF.h"
#include "lldb/Core/Module.h"
#include "lldb/Expression/DWARFExpression.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/StreamString.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FormatAdapters.h"
#include "llvm/Support/LEB128.h"

#include "DWARFCompileUnit.h"
#include "DWARFDebugAranges.h"
Expand All @@ -31,8 +33,6 @@
#include "SymbolFileDWARF.h"
#include "SymbolFileDWARFDwo.h"

#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"

using namespace lldb_private;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;
Expand Down Expand Up @@ -82,24 +82,11 @@ bool DWARFDebugInfoEntry::Extract(const DWARFDataExtractor &data,
return true;
}

static DWARFRangeList GetRangesOrReportError(DWARFUnit &unit,
const DWARFDebugInfoEntry &die,
const DWARFFormValue &value) {
llvm::Expected<DWARFRangeList> expected_ranges =
(value.Form() == DW_FORM_rnglistx)
? unit.FindRnglistFromIndex(value.Unsigned())
: unit.FindRnglistFromOffset(value.Unsigned());
if (expected_ranges)
return std::move(*expected_ranges);

unit.GetSymbolFileDWARF().GetObjectFile()->GetModule()->ReportError(
"[{0:x16}]: DIE has DW_AT_ranges({1} {2:x16}) attribute, but "
"range extraction failed ({3}), please file a bug "
"and attach the file at the start of this error message",
die.GetOffset(),
llvm::dwarf::FormEncodingString(value.Form()).str().c_str(),
value.Unsigned(), toString(expected_ranges.takeError()).c_str());
return DWARFRangeList();
static llvm::Expected<llvm::DWARFAddressRangesVector>
GetRanges(DWARFUnit &unit, const DWARFFormValue &value) {
return (value.Form() == DW_FORM_rnglistx)
? unit.FindRnglistFromIndex(value.Unsigned())
: unit.FindRnglistFromOffset(value.Unsigned());
}

static void ExtractAttrAndFormValue(
Expand All @@ -117,7 +104,7 @@ static void ExtractAttrAndFormValue(
// DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
DWARFUnit *cu, const char *&name, const char *&mangled,
DWARFRangeList &ranges, std::optional<int> &decl_file,
llvm::DWARFAddressRangesVector &ranges, std::optional<int> &decl_file,
std::optional<int> &decl_line, std::optional<int> &decl_column,
std::optional<int> &call_file, std::optional<int> &call_line,
std::optional<int> &call_column, DWARFExpressionList *frame_base) const {
Expand Down Expand Up @@ -173,7 +160,17 @@ bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
break;

case DW_AT_ranges:
ranges = GetRangesOrReportError(*cu, *this, form_value);
if (llvm::Expected<llvm::DWARFAddressRangesVector> r =
GetRanges(*cu, form_value)) {
ranges = std::move(*r);
} else {
module->ReportError(
"[{0:x16}]: DIE has DW_AT_ranges({1} {2:x16}) attribute, but "
"range extraction failed ({3}), please file a bug "
"and attach the file at the start of this error message",
GetOffset(), llvm::dwarf::FormEncodingString(form_value.Form()),
form_value.Unsigned(), fmt_consume(r.takeError()));
}
break;

case DW_AT_name:
Expand Down Expand Up @@ -259,22 +256,20 @@ bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
}
}

if (ranges.IsEmpty()) {
if (lo_pc != LLDB_INVALID_ADDRESS) {
if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
else
ranges.Append(DWARFRangeList::Entry(lo_pc, 0));
}
if (ranges.empty() && lo_pc != LLDB_INVALID_ADDRESS) {
lldb::addr_t range_hi_pc =
(hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) ? hi_pc : lo_pc;
ranges.emplace_back(lo_pc, range_hi_pc);
}

if (set_frame_base_loclist_addr) {
dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0);
if (set_frame_base_loclist_addr && !ranges.empty()) {
// TODO: Use the first range instead.
dw_addr_t lowest_range_pc = llvm::min_element(ranges)->LowPC;
assert(lowest_range_pc >= cu->GetBaseAddress());
frame_base->SetFuncFileAddress(lowest_range_pc);
}

if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) {
if (ranges.empty() || name == nullptr || mangled == nullptr) {
for (const DWARFDIE &die : dies) {
if (die) {
die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges,
Expand All @@ -283,7 +278,7 @@ bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
}
}
}
return !ranges.IsEmpty();
return !ranges.empty();
}

// Get all attribute values for a given DIE, including following any
Expand Down Expand Up @@ -499,24 +494,23 @@ bool DWARFDebugInfoEntry::GetAttributeAddressRange(
return false;
}

DWARFRangeList DWARFDebugInfoEntry::GetAttributeAddressRanges(
llvm::Expected<llvm::DWARFAddressRangesVector>
DWARFDebugInfoEntry::GetAttributeAddressRanges(
DWARFUnit *cu, bool check_hi_lo_pc, bool check_elaborating_dies) const {

DWARFFormValue form_value;
if (GetAttributeValue(cu, DW_AT_ranges, form_value))
return GetRangesOrReportError(*cu, *this, form_value);
return GetRanges(*cu, form_value);

DWARFRangeList ranges;
if (check_hi_lo_pc) {
dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
check_elaborating_dies)) {
if (lo_pc < hi_pc)
ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc));
}
check_elaborating_dies) &&
lo_pc < hi_pc)
return llvm::DWARFAddressRangesVector{{lo_pc, hi_pc}};
}
return ranges;
return llvm::createStringError("DIE has no address range information");
}

// GetName
Expand Down Expand Up @@ -577,13 +571,15 @@ const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
/// table instead of the compile unit offset.
void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable(
DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
Log *log = GetLog(DWARFLog::DebugInfo);
if (m_tag) {
if (m_tag == DW_TAG_subprogram) {
DWARFRangeList ranges =
GetAttributeAddressRanges(cu, /*check_hi_lo_pc=*/true);
for (const auto &r : ranges) {
debug_aranges->AppendRange(GetOffset(), r.GetRangeBase(),
r.GetRangeEnd());
if (llvm::Expected<llvm::DWARFAddressRangesVector> ranges =
GetAttributeAddressRanges(cu, /*check_hi_lo_pc=*/true)) {
for (const auto &r : *ranges)
debug_aranges->AppendRange(GetOffset(), r.LowPC, r.HighPC);
} else {
LLDB_LOG_ERROR(log, ranges.takeError(), "DIE({1:x}): {0}", GetOffset());
}
}

Expand Down
Loading
Loading