1 //===-- DWARFDebugAranges.cpp -----------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "DWARFDebugAranges.h"
11
12 #include <assert.h>
13 #include <stdio.h>
14
15 #include <algorithm>
16
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Stream.h"
19 #include "lldb/Utility/Timer.h"
20
21 #include "DWARFUnit.h"
22 #include "DWARFDebugInfo.h"
23 #include "LogChannelDWARF.h"
24 #include "SymbolFileDWARF.h"
25
26 using namespace lldb;
27 using namespace lldb_private;
28
29 //----------------------------------------------------------------------
30 // Constructor
31 //----------------------------------------------------------------------
DWARFDebugAranges()32 DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {}
33
34 //----------------------------------------------------------------------
35 // CountArangeDescriptors
36 //----------------------------------------------------------------------
37 class CountArangeDescriptors {
38 public:
CountArangeDescriptors(uint32_t & count_ref)39 CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) {
40 // printf("constructor CountArangeDescriptors()\n");
41 }
operator ()(const DWARFDebugArangeSet & set)42 void operator()(const DWARFDebugArangeSet &set) {
43 count += set.NumDescriptors();
44 }
45 uint32_t &count;
46 };
47
48 //----------------------------------------------------------------------
49 // Extract
50 //----------------------------------------------------------------------
Extract(const DWARFDataExtractor & debug_aranges_data)51 bool DWARFDebugAranges::Extract(const DWARFDataExtractor &debug_aranges_data) {
52 if (debug_aranges_data.ValidOffset(0)) {
53 lldb::offset_t offset = 0;
54
55 DWARFDebugArangeSet set;
56 Range range;
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();
61
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));
67 }
68 }
69 set.Clear();
70 }
71 }
72 return false;
73 }
74
75 //----------------------------------------------------------------------
76 // Generate
77 //----------------------------------------------------------------------
Generate(SymbolFileDWARF * dwarf2Data)78 bool DWARFDebugAranges::Generate(SymbolFileDWARF *dwarf2Data) {
79 Clear();
80 DWARFDebugInfo *debug_info = dwarf2Data->DebugInfo();
81 if (debug_info) {
82 uint32_t cu_idx = 0;
83 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
84 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) {
85 DWARFUnit *cu = debug_info->GetCompileUnitAtIndex(cu_idx);
86 if (cu)
87 cu->BuildAddressRangeTable(dwarf2Data, this);
88 }
89 }
90 return !IsEmpty();
91 }
92
Dump(Log * log) const93 void DWARFDebugAranges::Dump(Log *log) const {
94 if (log == NULL)
95 return;
96
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);
100 if (entry)
101 log->Printf("0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data,
102 entry->GetRangeBase(), entry->GetRangeEnd());
103 }
104 }
105
AppendRange(dw_offset_t offset,dw_addr_t low_pc,dw_addr_t high_pc)106 void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc,
107 dw_addr_t high_pc) {
108 if (high_pc > low_pc)
109 m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset));
110 }
111
Sort(bool minimize)112 void DWARFDebugAranges::Sort(bool minimize) {
113 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
114 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
115 static_cast<void *>(this));
116
117 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
118 size_t orig_arange_size = 0;
119 if (log) {
120 orig_arange_size = m_aranges.GetSize();
121 log->Printf("DWARFDebugAranges::Sort(minimize = %u) with %" PRIu64
122 " entries",
123 minimize, (uint64_t)orig_arange_size);
124 }
125
126 m_aranges.Sort();
127 m_aranges.CombineConsecutiveEntriesWithEqualData();
128
129 if (log) {
130 if (minimize) {
131 const size_t new_arange_size = m_aranges.GetSize();
132 const size_t delta = orig_arange_size - new_arange_size;
133 log->Printf("DWARFDebugAranges::Sort() %" PRIu64
134 " entries after minimizing (%" PRIu64
135 " entries combined for %" PRIu64 " bytes saved)",
136 (uint64_t)new_arange_size, (uint64_t)delta,
137 (uint64_t)delta * sizeof(Range));
138 }
139 Dump(log);
140 }
141 }
142
143 //----------------------------------------------------------------------
144 // FindAddress
145 //----------------------------------------------------------------------
FindAddress(dw_addr_t address) const146 dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const {
147 const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address);
148 if (entry)
149 return entry->data;
150 return DW_INVALID_OFFSET;
151 }
152