1414#include < limits>
1515#include < optional>
1616
17- #include " llvm/Support/LEB128.h"
18-
17+ #include " LogChannelDWARF.h"
1918#include " lldb/Core/Module.h"
2019#include " lldb/Expression/DWARFExpression.h"
2120#include " lldb/Symbol/ObjectFile.h"
22- #include " lldb/Utility/Stream.h"
23- #include " lldb/Utility/StreamString.h"
21+ #include " llvm/ADT/STLExtras.h"
22+ #include " llvm/DebugInfo/DWARF/DWARFAddressRange.h"
23+ #include " llvm/Support/Error.h"
24+ #include " llvm/Support/FormatAdapters.h"
25+ #include " llvm/Support/LEB128.h"
2426
2527#include " DWARFCompileUnit.h"
2628#include " DWARFDebugAranges.h"
3133#include " SymbolFileDWARF.h"
3234#include " SymbolFileDWARFDwo.h"
3335
34- #include " llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
35-
3636using namespace lldb_private ;
3737using namespace lldb_private ::dwarf;
3838using namespace lldb_private ::plugin::dwarf;
@@ -82,24 +82,11 @@ bool DWARFDebugInfoEntry::Extract(const DWARFDataExtractor &data,
8282 return true ;
8383}
8484
85- static DWARFRangeList GetRangesOrReportError (DWARFUnit &unit,
86- const DWARFDebugInfoEntry &die,
87- const DWARFFormValue &value) {
88- llvm::Expected<DWARFRangeList> expected_ranges =
89- (value.Form () == DW_FORM_rnglistx)
90- ? unit.FindRnglistFromIndex (value.Unsigned ())
91- : unit.FindRnglistFromOffset (value.Unsigned ());
92- if (expected_ranges)
93- return std::move (*expected_ranges);
94-
95- unit.GetSymbolFileDWARF ().GetObjectFile ()->GetModule ()->ReportError (
96- " [{0:x16}]: DIE has DW_AT_ranges({1} {2:x16}) attribute, but "
97- " range extraction failed ({3}), please file a bug "
98- " and attach the file at the start of this error message" ,
99- die.GetOffset (),
100- llvm::dwarf::FormEncodingString (value.Form ()).str ().c_str (),
101- value.Unsigned (), toString (expected_ranges.takeError ()).c_str ());
102- return DWARFRangeList ();
85+ static llvm::Expected<llvm::DWARFAddressRangesVector>
86+ GetRanges (DWARFUnit &unit, const DWARFFormValue &value) {
87+ return (value.Form () == DW_FORM_rnglistx)
88+ ? unit.FindRnglistFromIndex (value.Unsigned ())
89+ : unit.FindRnglistFromOffset (value.Unsigned ());
10390}
10491
10592static void ExtractAttrAndFormValue (
@@ -117,7 +104,7 @@ static void ExtractAttrAndFormValue(
117104// DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes.
118105bool DWARFDebugInfoEntry::GetDIENamesAndRanges (
119106 DWARFUnit *cu, const char *&name, const char *&mangled,
120- DWARFRangeList &ranges, std::optional<int > &decl_file,
107+ llvm::DWARFAddressRangesVector &ranges, std::optional<int > &decl_file,
121108 std::optional<int > &decl_line, std::optional<int > &decl_column,
122109 std::optional<int > &call_file, std::optional<int > &call_line,
123110 std::optional<int > &call_column, DWARFExpressionList *frame_base) const {
@@ -173,7 +160,17 @@ bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
173160 break ;
174161
175162 case DW_AT_ranges:
176- ranges = GetRangesOrReportError (*cu, *this , form_value);
163+ if (llvm::Expected<llvm::DWARFAddressRangesVector> r =
164+ GetRanges (*cu, form_value)) {
165+ ranges = std::move (*r);
166+ } else {
167+ module ->ReportError (
168+ " [{0:x16}]: DIE has DW_AT_ranges({1} {2:x16}) attribute, but "
169+ " range extraction failed ({3}), please file a bug "
170+ " and attach the file at the start of this error message" ,
171+ GetOffset (), llvm::dwarf::FormEncodingString (form_value.Form ()),
172+ form_value.Unsigned (), fmt_consume (r.takeError ()));
173+ }
177174 break ;
178175
179176 case DW_AT_name:
@@ -259,22 +256,20 @@ bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
259256 }
260257 }
261258
262- if (ranges.IsEmpty ()) {
263- if (lo_pc != LLDB_INVALID_ADDRESS) {
264- if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc)
265- ranges.Append (DWARFRangeList::Entry (lo_pc, hi_pc - lo_pc));
266- else
267- ranges.Append (DWARFRangeList::Entry (lo_pc, 0 ));
268- }
259+ if (ranges.empty () && lo_pc != LLDB_INVALID_ADDRESS) {
260+ lldb::addr_t range_hi_pc =
261+ (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) ? hi_pc : lo_pc;
262+ ranges.emplace_back (lo_pc, range_hi_pc);
269263 }
270264
271- if (set_frame_base_loclist_addr) {
272- dw_addr_t lowest_range_pc = ranges.GetMinRangeBase (0 );
265+ if (set_frame_base_loclist_addr && !ranges.empty ()) {
266+ // TODO: Use the first range instead.
267+ dw_addr_t lowest_range_pc = llvm::min_element (ranges)->LowPC ;
273268 assert (lowest_range_pc >= cu->GetBaseAddress ());
274269 frame_base->SetFuncFileAddress (lowest_range_pc);
275270 }
276271
277- if (ranges.IsEmpty () || name == nullptr || mangled == nullptr ) {
272+ if (ranges.empty () || name == nullptr || mangled == nullptr ) {
278273 for (const DWARFDIE &die : dies) {
279274 if (die) {
280275 die.GetDIE ()->GetDIENamesAndRanges (die.GetCU (), name, mangled, ranges,
@@ -283,7 +278,7 @@ bool DWARFDebugInfoEntry::GetDIENamesAndRanges(
283278 }
284279 }
285280 }
286- return !ranges.IsEmpty ();
281+ return !ranges.empty ();
287282}
288283
289284// Get all attribute values for a given DIE, including following any
@@ -499,24 +494,23 @@ bool DWARFDebugInfoEntry::GetAttributeAddressRange(
499494 return false ;
500495}
501496
502- DWARFRangeList DWARFDebugInfoEntry::GetAttributeAddressRanges (
497+ llvm::Expected<llvm::DWARFAddressRangesVector>
498+ DWARFDebugInfoEntry::GetAttributeAddressRanges (
503499 DWARFUnit *cu, bool check_hi_lo_pc, bool check_elaborating_dies) const {
504500
505501 DWARFFormValue form_value;
506502 if (GetAttributeValue (cu, DW_AT_ranges, form_value))
507- return GetRangesOrReportError (*cu, * this , form_value);
503+ return GetRanges (*cu, form_value);
508504
509- DWARFRangeList ranges;
510505 if (check_hi_lo_pc) {
511506 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS;
512507 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS;
513508 if (GetAttributeAddressRange (cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS,
514- check_elaborating_dies)) {
515- if (lo_pc < hi_pc)
516- ranges.Append (DWARFRangeList::Entry (lo_pc, hi_pc - lo_pc));
517- }
509+ check_elaborating_dies) &&
510+ lo_pc < hi_pc)
511+ return llvm::DWARFAddressRangesVector{{lo_pc, hi_pc}};
518512 }
519- return ranges ;
513+ return llvm::createStringError ( " DIE has no address range information " ) ;
520514}
521515
522516// GetName
@@ -577,13 +571,15 @@ const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const {
577571// / table instead of the compile unit offset.
578572void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable (
579573 DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const {
574+ Log *log = GetLog (DWARFLog::DebugInfo);
580575 if (m_tag) {
581576 if (m_tag == DW_TAG_subprogram) {
582- DWARFRangeList ranges =
583- GetAttributeAddressRanges (cu, /* check_hi_lo_pc=*/ true );
584- for (const auto &r : ranges) {
585- debug_aranges->AppendRange (GetOffset (), r.GetRangeBase (),
586- r.GetRangeEnd ());
577+ if (llvm::Expected<llvm::DWARFAddressRangesVector> ranges =
578+ GetAttributeAddressRanges (cu, /* check_hi_lo_pc=*/ true )) {
579+ for (const auto &r : *ranges)
580+ debug_aranges->AppendRange (GetOffset (), r.LowPC , r.HighPC );
581+ } else {
582+ LLDB_LOG_ERROR (log, ranges.takeError (), " DIE({1:x}): {0}" , GetOffset ());
587583 }
588584 }
589585
0 commit comments