Path: blob/main/contrib/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
39645 views
//===-- DWARFDebugInfo.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "SymbolFileDWARF.h"910#include <algorithm>11#include <set>1213#include "lldb/Host/PosixApi.h"14#include "lldb/Symbol/ObjectFile.h"15#include "lldb/Utility/RegularExpression.h"16#include "lldb/Utility/Stream.h"17#include "llvm/Support/Casting.h"1819#include "DWARFCompileUnit.h"20#include "DWARFContext.h"21#include "DWARFDebugAranges.h"22#include "DWARFDebugInfo.h"23#include "DWARFDebugInfoEntry.h"24#include "DWARFFormValue.h"25#include "DWARFTypeUnit.h"26#include "LogChannelDWARF.h"2728using namespace lldb;29using namespace lldb_private;30using namespace lldb_private::plugin::dwarf;3132// Constructor33DWARFDebugInfo::DWARFDebugInfo(SymbolFileDWARF &dwarf, DWARFContext &context)34: m_dwarf(dwarf), m_context(context), m_units(), m_cu_aranges_up() {}3536const DWARFDebugAranges &DWARFDebugInfo::GetCompileUnitAranges() {37if (m_cu_aranges_up)38return *m_cu_aranges_up;3940m_cu_aranges_up = std::make_unique<DWARFDebugAranges>();41const DWARFDataExtractor &debug_aranges_data =42m_context.getOrLoadArangesData();4344// Extract what we can from the .debug_aranges first.45m_cu_aranges_up->extract(debug_aranges_data);4647// Make a list of all CUs represented by the .debug_aranges data.48std::set<dw_offset_t> cus_with_data;49for (size_t n = 0; n < m_cu_aranges_up->GetNumRanges(); n++) {50dw_offset_t offset = m_cu_aranges_up->OffsetAtIndex(n);51if (offset != DW_INVALID_OFFSET)52cus_with_data.insert(offset);53}5455// Manually build arange data for everything that wasn't in .debug_aranges.56// The .debug_aranges accelerator is not guaranteed to be complete.57// Tools such as dsymutil can provide stronger guarantees than required by the58// standard. Without that guarantee, we have to iterate over every CU in the59// .debug_info and make sure there's a corresponding entry in the table and if60// not, add one for every subprogram.61ObjectFile *OF = m_dwarf.GetObjectFile();62if (!OF || !OF->CanTrustAddressRanges()) {63const size_t num_units = GetNumUnits();64for (size_t idx = 0; idx < num_units; ++idx) {65DWARFUnit *cu = GetUnitAtIndex(idx);6667dw_offset_t offset = cu->GetOffset();68if (cus_with_data.find(offset) == cus_with_data.end())69cu->BuildAddressRangeTable(m_cu_aranges_up.get());70}71}7273const bool minimize = true;74m_cu_aranges_up->Sort(minimize);75return *m_cu_aranges_up;76}7778void DWARFDebugInfo::ParseUnitsFor(DIERef::Section section) {79DWARFDataExtractor data = section == DIERef::Section::DebugTypes80? m_context.getOrLoadDebugTypesData()81: m_context.getOrLoadDebugInfoData();82lldb::offset_t offset = 0;83while (data.ValidOffset(offset)) {84const lldb::offset_t unit_header_offset = offset;85llvm::Expected<DWARFUnitSP> expected_unit_sp =86DWARFUnit::extract(m_dwarf, m_units.size(), data, section, &offset);8788if (!expected_unit_sp) {89Log *log = GetLog(DWARFLog::DebugInfo);90if (log)91LLDB_LOG(log, "Unable to extract DWARFUnitHeader at {0:x}: {1}",92unit_header_offset,93llvm::toString(expected_unit_sp.takeError()));94else95llvm::consumeError(expected_unit_sp.takeError());96return;97}9899DWARFUnitSP unit_sp = *expected_unit_sp;100101// If it didn't return an error, then it should be returning a valid Unit.102assert((bool)unit_sp);103104// Keep a map of DWO ID back to the skeleton units. Sometimes accelerator105// table lookups can cause the DWO files to be accessed before the skeleton106// compile unit is parsed, so we keep a map to allow us to match up the DWO107// file to the back to the skeleton compile units.108if (unit_sp->GetUnitType() == lldb_private::dwarf::DW_UT_skeleton) {109if (std::optional<uint64_t> unit_dwo_id = unit_sp->GetHeaderDWOId())110m_dwarf5_dwo_id_to_skeleton_unit[*unit_dwo_id] = unit_sp.get();111}112113m_units.push_back(unit_sp);114offset = unit_sp->GetNextUnitOffset();115116if (auto *type_unit = llvm::dyn_cast<DWARFTypeUnit>(unit_sp.get())) {117m_type_hash_to_unit_index.emplace_back(type_unit->GetTypeHash(),118unit_sp->GetID());119}120}121}122123DWARFUnit *DWARFDebugInfo::GetSkeletonUnit(DWARFUnit *dwo_unit) {124// If this isn't a DWO unit, don't try and find the skeleton unit.125if (!dwo_unit->IsDWOUnit())126return nullptr;127128auto dwo_id = dwo_unit->GetDWOId();129if (!dwo_id.has_value())130return nullptr;131132// Parse the unit headers so that m_dwarf5_dwo_id_to_skeleton_unit is filled133// in with all of the DWARF5 skeleton compile units DWO IDs since it is easy134// to access the DWO IDs in the DWARFUnitHeader for each DWARFUnit.135ParseUnitHeadersIfNeeded();136137// Find the value in our cache and return it we we find it. This cache may138// only contain DWARF5 units.139auto iter = m_dwarf5_dwo_id_to_skeleton_unit.find(*dwo_id);140if (iter != m_dwarf5_dwo_id_to_skeleton_unit.end())141return iter->second;142143// DWARF5 unit headers have the DWO ID and should have already been in the map144// so if it wasn't found in the above find() call, then we didn't find it and145// don't need to do the more expensive DWARF4 search.146if (dwo_unit->GetVersion() >= 5)147return nullptr;148149// Parse all DWO IDs from all DWARF4 and earlier compile units that have DWO150// IDs. It is more expensive to get the DWO IDs from DWARF4 compile units as151// we need to parse the unit DIE and extract the DW_AT_dwo_id or152// DW_AT_GNU_dwo_id attribute values, so do this only if we didn't find our153// match above search and only for DWARF4 and earlier compile units.154llvm::call_once(m_dwarf4_dwo_id_to_skeleton_unit_once_flag, [this]() {155for (uint32_t i = 0, num = GetNumUnits(); i < num; ++i) {156if (DWARFUnit *unit = GetUnitAtIndex(i)) {157if (unit->GetVersion() < 5) {158if (std::optional<uint64_t> unit_dwo_id = unit->GetDWOId())159m_dwarf4_dwo_id_to_skeleton_unit[*unit_dwo_id] = unit;160}161}162}163});164165// Search the DWARF4 DWO results that we parsed lazily.166iter = m_dwarf4_dwo_id_to_skeleton_unit.find(*dwo_id);167if (iter != m_dwarf4_dwo_id_to_skeleton_unit.end())168return iter->second;169return nullptr;170}171172void DWARFDebugInfo::ParseUnitHeadersIfNeeded() {173llvm::call_once(m_units_once_flag, [&] {174ParseUnitsFor(DIERef::Section::DebugInfo);175ParseUnitsFor(DIERef::Section::DebugTypes);176llvm::sort(m_type_hash_to_unit_index, llvm::less_first());177});178}179180size_t DWARFDebugInfo::GetNumUnits() {181ParseUnitHeadersIfNeeded();182return m_units.size();183}184185DWARFUnit *DWARFDebugInfo::GetUnitAtIndex(size_t idx) {186DWARFUnit *cu = nullptr;187if (idx < GetNumUnits())188cu = m_units[idx].get();189return cu;190}191192uint32_t DWARFDebugInfo::FindUnitIndex(DIERef::Section section,193dw_offset_t offset) {194ParseUnitHeadersIfNeeded();195196// llvm::lower_bound is not used as for DIE offsets it would still return197// index +1 and GetOffset() returning index itself would be a special case.198auto pos = llvm::upper_bound(199m_units, std::make_pair(section, offset),200[](const std::pair<DIERef::Section, dw_offset_t> &lhs,201const DWARFUnitSP &rhs) {202return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());203});204uint32_t idx = std::distance(m_units.begin(), pos);205if (idx == 0)206return DW_INVALID_INDEX;207return idx - 1;208}209210DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(DIERef::Section section,211dw_offset_t cu_offset,212uint32_t *idx_ptr) {213uint32_t idx = FindUnitIndex(section, cu_offset);214DWARFUnit *result = GetUnitAtIndex(idx);215if (result && result->GetOffset() != cu_offset) {216result = nullptr;217idx = DW_INVALID_INDEX;218}219if (idx_ptr)220*idx_ptr = idx;221return result;222}223224DWARFUnit *225DWARFDebugInfo::GetUnitContainingDIEOffset(DIERef::Section section,226dw_offset_t die_offset) {227uint32_t idx = FindUnitIndex(section, die_offset);228DWARFUnit *result = GetUnitAtIndex(idx);229if (result && !result->ContainsDIEOffset(die_offset))230return nullptr;231return result;232}233234const std::shared_ptr<SymbolFileDWARFDwo> &DWARFDebugInfo::GetDwpSymbolFile() {235return m_dwarf.GetDwpSymbolFile();236}237238DWARFTypeUnit *DWARFDebugInfo::GetTypeUnitForHash(uint64_t hash) {239auto pos = llvm::lower_bound(m_type_hash_to_unit_index,240std::make_pair(hash, 0u), llvm::less_first());241if (pos == m_type_hash_to_unit_index.end() || pos->first != hash)242return nullptr;243return llvm::cast<DWARFTypeUnit>(GetUnitAtIndex(pos->second));244}245246bool DWARFDebugInfo::ContainsTypeUnits() {247ParseUnitHeadersIfNeeded();248return !m_type_hash_to_unit_index.empty();249}250251// GetDIE()252//253// Get the DIE (Debug Information Entry) with the specified offset.254DWARFDIE255DWARFDebugInfo::GetDIE(DIERef::Section section, dw_offset_t die_offset) {256if (DWARFUnit *cu = GetUnitContainingDIEOffset(section, die_offset))257return cu->GetNonSkeletonUnit().GetDIE(die_offset);258return DWARFDIE(); // Not found259}260261262