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 "DWARFCompileUnit.h"
22 #include "DWARFDebugInfo.h"
23 #include "LogChannelDWARF.h"
24 #include "SymbolFileDWARF.h"
27 using namespace lldb_private;
29 //----------------------------------------------------------------------
31 //----------------------------------------------------------------------
32 DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {}
34 //----------------------------------------------------------------------
35 // CountArangeDescriptors
36 //----------------------------------------------------------------------
37 class CountArangeDescriptors {
39 CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) {
40 // printf("constructor CountArangeDescriptors()\n");
42 void operator()(const DWARFDebugArangeSet &set) {
43 count += set.NumDescriptors();
48 //----------------------------------------------------------------------
50 //----------------------------------------------------------------------
51 bool DWARFDebugAranges::Extract(const DWARFDataExtractor &debug_aranges_data) {
52 if (debug_aranges_data.ValidOffset(0)) {
53 lldb::offset_t offset = 0;
55 DWARFDebugArangeSet set;
57 while (set.Extract(debug_aranges_data, &offset)) {
58 const uint32_t num_descriptors = set.NumDescriptors();
59 if (num_descriptors > 0) {
60 const dw_offset_t cu_offset = set.GetCompileUnitDIEOffset();
62 for (uint32_t i = 0; i < num_descriptors; ++i) {
63 const DWARFDebugArangeSet::Descriptor &descriptor =
64 set.GetDescriptorRef(i);
65 m_aranges.Append(RangeToDIE::Entry(descriptor.address,
66 descriptor.length, cu_offset));
75 //----------------------------------------------------------------------
77 //----------------------------------------------------------------------
78 bool DWARFDebugAranges::Generate(SymbolFileDWARF *dwarf2Data) {
80 DWARFDebugInfo *debug_info = dwarf2Data->DebugInfo();
83 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
84 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
85 DWARFCompileUnit *cu = debug_info->GetCompileUnitAtIndex(cu_idx);
87 cu->BuildAddressRangeTable(dwarf2Data, this);
93 void DWARFDebugAranges::Dump(Log *log) const {
97 const size_t num_entries = m_aranges.GetSize();
98 for (size_t i = 0; i < num_entries; ++i) {
99 const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i);
101 log->Printf("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data,
102 entry->GetRangeBase(), entry->GetRangeEnd());
106 void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc,
108 if (high_pc > low_pc)
109 m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset));
112 void DWARFDebugAranges::Sort(bool minimize) {
113 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s this = %p", LLVM_PRETTY_FUNCTION,
114 static_cast<void *>(this));
116 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
117 size_t orig_arange_size = 0;
119 orig_arange_size = m_aranges.GetSize();
120 log->Printf("DWARFDebugAranges::Sort(minimize = %u) with %" PRIu64
122 minimize, (uint64_t)orig_arange_size);
126 m_aranges.CombineConsecutiveEntriesWithEqualData();
130 const size_t new_arange_size = m_aranges.GetSize();
131 const size_t delta = orig_arange_size - new_arange_size;
132 log->Printf("DWARFDebugAranges::Sort() %" PRIu64
133 " entries after minimizing (%" PRIu64
134 " entries combined for %" PRIu64 " bytes saved)",
135 (uint64_t)new_arange_size, (uint64_t)delta,
136 (uint64_t)delta * sizeof(Range));
142 //----------------------------------------------------------------------
144 //----------------------------------------------------------------------
145 dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const {
146 const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
149 return DW_INVALID_OFFSET;