LLDB  mainline
DWARFDebugInfo.cpp
Go to the documentation of this file.
1 //===-- DWARFDebugInfo.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 "SymbolFileDWARF.h"
10 
11 #include <algorithm>
12 #include <set>
13 
14 #include "lldb/Host/PosixApi.h"
15 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/Stream.h"
18 #include "llvm/Support/Casting.h"
19 
20 #include "DWARFCompileUnit.h"
21 #include "DWARFContext.h"
22 #include "DWARFDebugAranges.h"
23 #include "DWARFDebugInfo.h"
24 #include "DWARFDebugInfoEntry.h"
25 #include "DWARFFormValue.h"
26 #include "DWARFTypeUnit.h"
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 // Constructor
34  : m_dwarf(dwarf), m_context(context), m_units(), m_cu_aranges_up() {}
35 
37  if (m_cu_aranges_up)
38  return *m_cu_aranges_up;
39 
40  m_cu_aranges_up = std::make_unique<DWARFDebugAranges>();
41  const DWARFDataExtractor &debug_aranges_data =
43 
44  // Extract what we can from the .debug_aranges first.
45  m_cu_aranges_up->extract(debug_aranges_data);
46 
47  // Make a list of all CUs represented by the .debug_aranges data.
48  std::set<dw_offset_t> cus_with_data;
49  for (size_t n = 0; n < m_cu_aranges_up->GetNumRanges(); n++) {
50  dw_offset_t offset = m_cu_aranges_up->OffsetAtIndex(n);
51  if (offset != DW_INVALID_OFFSET)
52  cus_with_data.insert(offset);
53  }
54 
55  // Manually build arange data for everything that wasn't in .debug_aranges.
56  const size_t num_units = GetNumUnits();
57  for (size_t idx = 0; idx < num_units; ++idx) {
58  DWARFUnit *cu = GetUnitAtIndex(idx);
59 
60  dw_offset_t offset = cu->GetOffset();
61  if (cus_with_data.find(offset) == cus_with_data.end())
63  }
64 
65  const bool minimize = true;
66  m_cu_aranges_up->Sort(minimize);
67  return *m_cu_aranges_up;
68 }
69 
71  DWARFDataExtractor data = section == DIERef::Section::DebugTypes
74  lldb::offset_t offset = 0;
75  while (data.ValidOffset(offset)) {
76  llvm::Expected<DWARFUnitSP> unit_sp = DWARFUnit::extract(
77  m_dwarf, m_units.size(), data, section, &offset);
78 
79  if (!unit_sp) {
80  // FIXME: Propagate this error up.
81  llvm::consumeError(unit_sp.takeError());
82  return;
83  }
84 
85  // If it didn't return an error, then it should be returning a valid Unit.
86  assert(*unit_sp);
87  m_units.push_back(*unit_sp);
88  offset = (*unit_sp)->GetNextUnitOffset();
89 
90  if (auto *type_unit = llvm::dyn_cast<DWARFTypeUnit>(unit_sp->get())) {
91  m_type_hash_to_unit_index.emplace_back(type_unit->GetTypeHash(),
92  unit_sp.get()->GetID());
93  }
94  }
95 }
96 
98  llvm::call_once(m_units_once_flag, [&] {
99  ParseUnitsFor(DIERef::Section::DebugInfo);
100  ParseUnitsFor(DIERef::Section::DebugTypes);
101  llvm::sort(m_type_hash_to_unit_index, llvm::less_first());
102  });
103 }
104 
107  return m_units.size();
108 }
109 
111  DWARFUnit *cu = nullptr;
112  if (idx < GetNumUnits())
113  cu = m_units[idx].get();
114  return cu;
115 }
116 
118  dw_offset_t offset) {
120 
121  // llvm::lower_bound is not used as for DIE offsets it would still return
122  // index +1 and GetOffset() returning index itself would be a special case.
123  auto pos = llvm::upper_bound(
124  m_units, std::make_pair(section, offset),
125  [](const std::pair<DIERef::Section, dw_offset_t> &lhs,
126  const DWARFUnitSP &rhs) {
127  return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());
128  });
129  uint32_t idx = std::distance(m_units.begin(), pos);
130  if (idx == 0)
131  return DW_INVALID_OFFSET;
132  return idx - 1;
133 }
134 
136  dw_offset_t cu_offset,
137  uint32_t *idx_ptr) {
138  uint32_t idx = FindUnitIndex(section, cu_offset);
139  DWARFUnit *result = GetUnitAtIndex(idx);
140  if (result && result->GetOffset() != cu_offset) {
141  result = nullptr;
142  idx = DW_INVALID_INDEX;
143  }
144  if (idx_ptr)
145  *idx_ptr = idx;
146  return result;
147 }
148 
150  return GetUnitContainingDIEOffset(die_ref.section(), die_ref.die_offset());
151 }
152 
153 DWARFUnit *
155  dw_offset_t die_offset) {
156  uint32_t idx = FindUnitIndex(section, die_offset);
157  DWARFUnit *result = GetUnitAtIndex(idx);
158  if (result && !result->ContainsDIEOffset(die_offset))
159  return nullptr;
160  return result;
161 }
162 
164  auto pos = llvm::lower_bound(m_type_hash_to_unit_index,
165  std::make_pair(hash, 0u), llvm::less_first());
166  if (pos == m_type_hash_to_unit_index.end() || pos->first != hash)
167  return nullptr;
168  return llvm::cast<DWARFTypeUnit>(GetUnitAtIndex(pos->second));
169 }
170 
173  return !m_type_hash_to_unit_index.empty();
174 }
175 
176 // GetDIE()
177 //
178 // Get the DIE (Debug Information Entry) with the specified offset.
179 DWARFDIE
181  DWARFUnit *cu = GetUnit(die_ref);
182  if (cu)
183  return cu->GetNonSkeletonUnit().GetDIE(die_ref.die_offset());
184  return DWARFDIE(); // Not found
185 }
RegularExpression.h
DWARFDebugInfo::ContainsTypeUnits
bool ContainsTypeUnits()
Definition: DWARFDebugInfo.cpp:171
DWARFUnit::BuildAddressRangeTable
virtual void BuildAddressRangeTable(DWARFDebugAranges *debug_aranges)=0
DWARFFormValue.h
DIERef
Identifies a DWARF debug info entry within a given Module.
Definition: DIERef.h:26
DWARFDebugInfo::ParseUnitHeadersIfNeeded
void ParseUnitHeadersIfNeeded()
Definition: DWARFDebugInfo.cpp:97
DWARFCompileUnit.h
DWARFDebugAranges
Definition: DWARFDebugAranges.h:16
lldb_private::DataExtractor::ValidOffset
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
Definition: DataExtractor.h:956
DWARFDebugInfo::FindUnitIndex
uint32_t FindUnitIndex(DIERef::Section section, dw_offset_t offset)
Definition: DWARFDebugInfo.cpp:117
DWARFContext.h
DWARFTypeUnit
Definition: DWARFTypeUnit.h:15
DWARFDebugInfo::GetNumUnits
size_t GetNumUnits()
Definition: DWARFDebugInfo.cpp:105
SymbolFileDWARF
Definition: SymbolFileDWARF.h:59
SymbolFileDWARF.h
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
DWARFDebugInfo::m_units
UnitColl m_units
Definition: DWARFDebugInfo.h:64
lldb_private::DWARFDataExtractor
Definition: DWARFDataExtractor.h:18
DWARFUnit::extract
static llvm::Expected< DWARFUnitSP > extract(SymbolFileDWARF &dwarf2Data, lldb::user_id_t uid, const lldb_private::DWARFDataExtractor &debug_info, DIERef::Section section, lldb::offset_t *offset_ptr)
Definition: DWARFUnit.cpp:932
DWARFDebugInfo::m_type_hash_to_unit_index
std::vector< std::pair< uint64_t, uint32_t > > m_type_hash_to_unit_index
Definition: DWARFDebugInfo.h:69
DW_INVALID_OFFSET
#define DW_INVALID_OFFSET
Definition: dwarf.h:37
DIERef::die_offset
dw_offset_t die_offset() const
Definition: DIERef.h:45
DWARFDebugInfo::GetCompileUnitAranges
const DWARFDebugAranges & GetCompileUnitAranges()
Definition: DWARFDebugInfo.cpp:36
lldb_private::DWARFContext
Definition: DWARFContext.h:20
DWARFDebugInfo::m_units_once_flag
llvm::once_flag m_units_once_flag
Definition: DWARFDebugInfo.h:63
DWARFDIE
Definition: DWARFDIE.h:16
DWARFDebugInfo::DWARFDebugInfo
DWARFDebugInfo(SymbolFileDWARF &dwarf, lldb_private::DWARFContext &context)
Definition: DWARFDebugInfo.cpp:32
DWARFUnit
Definition: DWARFUnit.h:83
dwarf
Definition: ABISysV_arc.cpp:61
DWARFDebugInfo::GetTypeUnitForHash
DWARFTypeUnit * GetTypeUnitForHash(uint64_t hash)
Definition: DWARFDebugInfo.cpp:163
DWARFDebugInfo::m_dwarf
SymbolFileDWARF & m_dwarf
Definition: DWARFDebugInfo.h:60
DWARFUnitSP
std::shared_ptr< DWARFUnit > DWARFUnitSP
Definition: DWARFUnit.h:24
DWARFDebugInfo::GetUnitAtIndex
DWARFUnit * GetUnitAtIndex(size_t idx)
Definition: DWARFDebugInfo.cpp:110
DWARFDebugAranges.h
lldb_private::DWARFContext::getOrLoadDebugInfoData
const DWARFDataExtractor & getOrLoadDebugInfoData()
Definition: DWARFContext.cpp:68
DWARFDebugInfo.h
ObjectFile.h
DWARFUnit::GetOffset
dw_offset_t GetOffset() const
Definition: DWARFUnit.h:133
DWARFDebugInfo::GetUnit
DWARFUnit * GetUnit(const DIERef &die_ref)
Definition: DWARFDebugInfo.cpp:149
DW_INVALID_INDEX
#define DW_INVALID_INDEX
Definition: dwarf.h:38
lldb_private::DWARFContext::getOrLoadArangesData
const DWARFDataExtractor & getOrLoadArangesData()
Definition: DWARFContext.cpp:58
DWARFDebugInfo::ParseUnitsFor
void ParseUnitsFor(DIERef::Section section)
Definition: DWARFDebugInfo.cpp:70
uint32_t
DWARFDebugInfo::GetUnitAtOffset
DWARFUnit * GetUnitAtOffset(DIERef::Section section, dw_offset_t cu_offset, uint32_t *idx_ptr=nullptr)
Definition: DWARFDebugInfo.cpp:135
DWARFDebugInfoEntry.h
DIERef::section
Section section() const
Definition: DIERef.h:43
DWARFUnit::GetNonSkeletonUnit
DWARFUnit & GetNonSkeletonUnit()
Definition: DWARFUnit.cpp:630
DWARFDebugInfo::m_cu_aranges_up
std::unique_ptr< DWARFDebugAranges > m_cu_aranges_up
Definition: DWARFDebugInfo.h:67
DWARFDebugInfo::GetUnitContainingDIEOffset
DWARFUnit * GetUnitContainingDIEOffset(DIERef::Section section, dw_offset_t die_offset)
Definition: DWARFDebugInfo.cpp:154
DWARFTypeUnit.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
PosixApi.h
lldb_private::DWARFContext::getOrLoadDebugTypesData
const DWARFDataExtractor & getOrLoadDebugTypesData()
Definition: DWARFContext.cpp:121
DWARFUnit::ContainsDIEOffset
bool ContainsDIEOffset(dw_offset_t die_offset) const
Definition: DWARFUnit.h:142
Stream.h
lldb
Definition: SBAddress.h:15
DWARFDebugInfo::GetDIE
DWARFDIE GetDIE(const DIERef &die_ref)
Definition: DWARFDebugInfo.cpp:180
DIERef::Section
Section
Definition: DIERef.h:28
DWARFDebugInfo::m_context
lldb_private::DWARFContext & m_context
Definition: DWARFDebugInfo.h:61
DWARFUnit::GetDIE
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:609