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/Core/Log.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Core/Timer.h" 20 21 #include "DWARFCompileUnit.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 //---------------------------------------------------------------------- 32 DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {} 33 34 //---------------------------------------------------------------------- 35 // CountArangeDescriptors 36 //---------------------------------------------------------------------- 37 class CountArangeDescriptors { 38 public: 39 CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) { 40 // printf("constructor CountArangeDescriptors()\n"); 41 } 42 void operator()(const DWARFDebugArangeSet &set) { 43 count += set.NumDescriptors(); 44 } 45 uint32_t &count; 46 }; 47 48 //---------------------------------------------------------------------- 49 // Extract 50 //---------------------------------------------------------------------- 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 //---------------------------------------------------------------------- 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 DWARFCompileUnit *cu = debug_info->GetCompileUnitAtIndex(cu_idx); 86 if (cu) 87 cu->BuildAddressRangeTable(dwarf2Data, this); 88 } 89 } 90 return !IsEmpty(); 91 } 92 93 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 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 112 void DWARFDebugAranges::Sort(bool minimize) { 113 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s this = %p", LLVM_PRETTY_FUNCTION, 114 static_cast<void *>(this)); 115 116 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); 117 size_t orig_arange_size = 0; 118 if (log) { 119 orig_arange_size = m_aranges.GetSize(); 120 log->Printf("DWARFDebugAranges::Sort(minimize = %u) with %" PRIu64 121 " entries", 122 minimize, (uint64_t)orig_arange_size); 123 } 124 125 m_aranges.Sort(); 126 m_aranges.CombineConsecutiveEntriesWithEqualData(); 127 128 if (log) { 129 if (minimize) { 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)); 137 } 138 Dump(log); 139 } 140 } 141 142 //---------------------------------------------------------------------- 143 // FindAddress 144 //---------------------------------------------------------------------- 145 dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const { 146 const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address); 147 if (entry) 148 return entry->data; 149 return DW_INVALID_OFFSET; 150 } 151