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