1 //===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugAranges.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Core/Timer.h"
21 #include "LogChannelDWARF.h"
22 #include "SymbolFileDWARF.h"
23 #include "DWARFDebugInfo.h"
24 #include "DWARFCompileUnit.h"
27 using namespace lldb_private;
29 //----------------------------------------------------------------------
31 //----------------------------------------------------------------------
32 DWARFDebugAranges::DWARFDebugAranges() :
37 //----------------------------------------------------------------------
38 // CountArangeDescriptors
39 //----------------------------------------------------------------------
40 class CountArangeDescriptors
43 CountArangeDescriptors (uint32_t& count_ref) : count(count_ref)
45 // printf("constructor CountArangeDescriptors()\n");
47 void operator() (const DWARFDebugArangeSet& set)
49 count += set.NumDescriptors();
55 //----------------------------------------------------------------------
57 //----------------------------------------------------------------------
59 DWARFDebugAranges::Extract(const DWARFDataExtractor &debug_aranges_data)
61 if (debug_aranges_data.ValidOffset(0))
63 lldb::offset_t offset = 0;
65 DWARFDebugArangeSet set;
67 while (set.Extract(debug_aranges_data, &offset))
69 const uint32_t num_descriptors = set.NumDescriptors();
70 if (num_descriptors > 0)
72 const dw_offset_t cu_offset = set.GetCompileUnitDIEOffset();
74 for (uint32_t i=0; i<num_descriptors; ++i)
76 const DWARFDebugArangeSet::Descriptor &descriptor = set.GetDescriptorRef(i);
77 m_aranges.Append(RangeToDIE::Entry (descriptor.address, descriptor.length, cu_offset));
86 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
90 DWARFDebugAranges::Generate(SymbolFileDWARF* dwarf2Data)
93 DWARFDebugInfo* debug_info = dwarf2Data->DebugInfo();
97 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
98 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
100 DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx);
102 cu->BuildAddressRangeTable(dwarf2Data, this);
110 DWARFDebugAranges::Dump (Log *log) const
115 const size_t num_entries = m_aranges.GetSize();
116 for (size_t i=0; i<num_entries; ++i)
118 const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i);
120 log->Printf ("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")",
122 entry->GetRangeBase(),
123 entry->GetRangeEnd());
128 DWARFDebugAranges::AppendRange (dw_offset_t offset, dw_addr_t low_pc, dw_addr_t high_pc)
130 if (high_pc > low_pc)
131 m_aranges.Append(RangeToDIE::Entry (low_pc, high_pc - low_pc, offset));
135 DWARFDebugAranges::Sort (bool minimize)
137 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p",
138 __PRETTY_FUNCTION__, static_cast<void*>(this));
140 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
141 size_t orig_arange_size = 0;
144 orig_arange_size = m_aranges.GetSize();
145 log->Printf ("DWARFDebugAranges::Sort(minimize = %u) with %" PRIu64 " entries", minimize, (uint64_t)orig_arange_size);
149 m_aranges.CombineConsecutiveEntriesWithEqualData();
155 const size_t new_arange_size = m_aranges.GetSize();
156 const size_t delta = orig_arange_size - new_arange_size;
157 log->Printf ("DWARFDebugAranges::Sort() %" PRIu64 " entries after minimizing (%" PRIu64 " entries combined for %" PRIu64 " bytes saved)",
158 (uint64_t)new_arange_size,
160 (uint64_t)delta * sizeof(Range));
166 //----------------------------------------------------------------------
168 //----------------------------------------------------------------------
170 DWARFDebugAranges::FindAddress(dw_addr_t address) const
172 const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
175 return DW_INVALID_OFFSET;