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 #include "DWARFDebugArangeSet.h" 11 #include "DWARFUnit.h" 12 #include "lldb/Utility/Log.h" 13 #include "lldb/Utility/Timer.h" 14 15 using namespace lldb; 16 using namespace lldb_private; 17 18 // Constructor 19 DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {} 20 21 // CountArangeDescriptors 22 class CountArangeDescriptors { 23 public: 24 CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) { 25 // printf("constructor CountArangeDescriptors()\n"); 26 } 27 void operator()(const DWARFDebugArangeSet &set) { 28 count += set.NumDescriptors(); 29 } 30 uint32_t &count; 31 }; 32 33 // Extract 34 llvm::Error 35 DWARFDebugAranges::extract(const DWARFDataExtractor &debug_aranges_data) { 36 lldb::offset_t offset = 0; 37 38 DWARFDebugArangeSet set; 39 Range range; 40 while (debug_aranges_data.ValidOffset(offset)) { 41 llvm::Error error = set.extract(debug_aranges_data, &offset); 42 if (!error) 43 return error; 44 45 const uint32_t num_descriptors = set.NumDescriptors(); 46 if (num_descriptors > 0) { 47 const dw_offset_t cu_offset = set.GetHeader().cu_offset; 48 49 for (uint32_t i = 0; i < num_descriptors; ++i) { 50 const DWARFDebugArangeSet::Descriptor &descriptor = 51 set.GetDescriptorRef(i); 52 m_aranges.Append(RangeToDIE::Entry(descriptor.address, 53 descriptor.length, cu_offset)); 54 } 55 } 56 set.Clear(); 57 } 58 return llvm::ErrorSuccess(); 59 } 60 61 void DWARFDebugAranges::Dump(Log *log) const { 62 if (log == nullptr) 63 return; 64 65 const size_t num_entries = m_aranges.GetSize(); 66 for (size_t i = 0; i < num_entries; ++i) { 67 const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i); 68 if (entry) 69 LLDB_LOGF(log, "0x%8.8x: [0x%" PRIx64 " - 0x%" PRIx64 ")", entry->data, 70 entry->GetRangeBase(), entry->GetRangeEnd()); 71 } 72 } 73 74 void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc, 75 dw_addr_t high_pc) { 76 if (high_pc > low_pc) 77 m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset)); 78 } 79 80 void DWARFDebugAranges::Sort(bool minimize) { 81 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 82 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION, 83 static_cast<void *>(this)); 84 85 m_aranges.Sort(); 86 m_aranges.CombineConsecutiveEntriesWithEqualData(); 87 } 88 89 // FindAddress 90 dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const { 91 const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address); 92 if (entry) 93 return entry->data; 94 return DW_INVALID_OFFSET; 95 } 96