1//===-- DWARFDebugAranges.cpp ---------------------------------------------===// 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 "LogChannelDWARF.h" 13#include "lldb/Utility/Log.h" 14#include "lldb/Utility/Timer.h" 15 16using namespace lldb; 17using namespace lldb_private; 18using namespace lldb_private::plugin::dwarf; 19 20// Constructor 21DWARFDebugAranges::DWARFDebugAranges() : m_aranges() {} 22 23// CountArangeDescriptors 24class CountArangeDescriptors { 25public: 26 CountArangeDescriptors(uint32_t &count_ref) : count(count_ref) { 27 // printf("constructor CountArangeDescriptors()\n"); 28 } 29 void operator()(const DWARFDebugArangeSet &set) { 30 count += set.NumDescriptors(); 31 } 32 uint32_t &count; 33}; 34 35// Extract 36void DWARFDebugAranges::extract(const DWARFDataExtractor &debug_aranges_data) { 37 lldb::offset_t offset = 0; 38 39 DWARFDebugArangeSet set; 40 Range range; 41 while (debug_aranges_data.ValidOffset(offset)) { 42 const lldb::offset_t set_offset = offset; 43 if (llvm::Error error = set.extract(debug_aranges_data, &offset)) { 44 Log *log = GetLog(DWARFLog::DebugInfo); 45 LLDB_LOG_ERROR(log, std::move(error), 46 "DWARFDebugAranges::extract failed to extract " 47 ".debug_aranges set at offset {1:x}: {0}", 48 set_offset); 49 } else { 50 const uint32_t num_descriptors = set.NumDescriptors(); 51 if (num_descriptors > 0) { 52 const dw_offset_t cu_offset = set.GetHeader().cu_offset; 53 54 for (uint32_t i = 0; i < num_descriptors; ++i) { 55 const DWARFDebugArangeSet::Descriptor &descriptor = 56 set.GetDescriptorRef(i); 57 m_aranges.Append(RangeToDIE::Entry(descriptor.address, 58 descriptor.length, cu_offset)); 59 } 60 } 61 } 62 // Always use the previous DWARFDebugArangeSet's information to calculate 63 // the offset of the next DWARFDebugArangeSet in case we entouncter an 64 // error in the current DWARFDebugArangeSet and our offset position is 65 // still in the middle of the data. If we do this, we can parse all valid 66 // DWARFDebugArangeSet objects without returning invalid errors. 67 offset = set.GetNextOffset(); 68 set.Clear(); 69 } 70} 71 72void DWARFDebugAranges::Dump(Log *log) const { 73 if (log == nullptr) 74 return; 75 76 const size_t num_entries = m_aranges.GetSize(); 77 for (size_t i = 0; i < num_entries; ++i) { 78 const RangeToDIE::Entry *entry = m_aranges.GetEntryAtIndex(i); 79 if (entry) 80 LLDB_LOG(log, "{0:x8}: [{1:x16} - {2:x16})", entry->data, 81 entry->GetRangeBase(), entry->GetRangeEnd()); 82 } 83} 84 85void DWARFDebugAranges::AppendRange(dw_offset_t offset, dw_addr_t low_pc, 86 dw_addr_t high_pc) { 87 if (high_pc > low_pc) 88 m_aranges.Append(RangeToDIE::Entry(low_pc, high_pc - low_pc, offset)); 89} 90 91void DWARFDebugAranges::Sort(bool minimize) { 92 LLDB_SCOPED_TIMERF("%s this = %p", LLVM_PRETTY_FUNCTION, 93 static_cast<void *>(this)); 94 95 m_aranges.Sort(); 96 m_aranges.CombineConsecutiveEntriesWithEqualData(); 97} 98 99// FindAddress 100dw_offset_t DWARFDebugAranges::FindAddress(dw_addr_t address) const { 101 const RangeToDIE::Entry *entry = m_aranges.FindEntryThatContains(address); 102 if (entry) 103 return entry->data; 104 return DW_INVALID_OFFSET; 105} 106