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