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 using namespace std;
31 
32 // Constructor
35  : m_dwarf(dwarf), m_context(context), m_units(), m_cu_aranges_up() {}
36 
38  if (m_cu_aranges_up)
39  return *m_cu_aranges_up;
40 
41  m_cu_aranges_up = std::make_unique<DWARFDebugAranges>();
42  const DWARFDataExtractor &debug_aranges_data =
44 
45  // Extract what we can from the .debug_aranges first.
46  m_cu_aranges_up->extract(debug_aranges_data);
47 
48  // Make a list of all CUs represented by the .debug_aranges data.
49  std::set<dw_offset_t> cus_with_data;
50  for (size_t n = 0; n < m_cu_aranges_up->GetNumRanges(); n++) {
51  dw_offset_t offset = m_cu_aranges_up->OffsetAtIndex(n);
52  if (offset != DW_INVALID_OFFSET)
53  cus_with_data.insert(offset);
54  }
55 
56  // Manually build arange data for everything that wasn't in .debug_aranges.
57  const size_t num_units = GetNumUnits();
58  for (size_t idx = 0; idx < num_units; ++idx) {
59  DWARFUnit *cu = GetUnitAtIndex(idx);
60 
61  dw_offset_t offset = cu->GetOffset();
62  if (cus_with_data.find(offset) == cus_with_data.end())
64  }
65 
66  const bool minimize = true;
67  m_cu_aranges_up->Sort(minimize);
68  return *m_cu_aranges_up;
69 }
70 
72  DWARFDataExtractor data = section == DIERef::Section::DebugTypes
75  lldb::offset_t offset = 0;
76  while (data.ValidOffset(offset)) {
77  llvm::Expected<DWARFUnitSP> unit_sp = DWARFUnit::extract(
78  m_dwarf, m_units.size(), data, section, &offset);
79 
80  if (!unit_sp) {
81  // FIXME: Propagate this error up.
82  llvm::consumeError(unit_sp.takeError());
83  return;
84  }
85 
86  // If it didn't return an error, then it should be returning a valid Unit.
87  assert(*unit_sp);
88  m_units.push_back(*unit_sp);
89  offset = (*unit_sp)->GetNextUnitOffset();
90 
91  if (auto *type_unit = llvm::dyn_cast<DWARFTypeUnit>(unit_sp->get())) {
92  m_type_hash_to_unit_index.emplace_back(type_unit->GetTypeHash(),
93  unit_sp.get()->GetID());
94  }
95  }
96 }
97 
99  llvm::call_once(m_units_once_flag, [&] {
100  ParseUnitsFor(DIERef::Section::DebugInfo);
101  ParseUnitsFor(DIERef::Section::DebugTypes);
102  llvm::sort(m_type_hash_to_unit_index, llvm::less_first());
103  });
104 }
105 
108  return m_units.size();
109 }
110 
112  DWARFUnit *cu = nullptr;
113  if (idx < GetNumUnits())
114  cu = m_units[idx].get();
115  return cu;
116 }
117 
119  dw_offset_t offset) {
121 
122  // llvm::lower_bound is not used as for DIE offsets it would still return
123  // index +1 and GetOffset() returning index itself would be a special case.
124  auto pos = llvm::upper_bound(
125  m_units, std::make_pair(section, offset),
126  [](const std::pair<DIERef::Section, dw_offset_t> &lhs,
127  const DWARFUnitSP &rhs) {
128  return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());
129  });
130  uint32_t idx = std::distance(m_units.begin(), pos);
131  if (idx == 0)
132  return DW_INVALID_OFFSET;
133  return idx - 1;
134 }
135 
137  dw_offset_t cu_offset,
138  uint32_t *idx_ptr) {
139  uint32_t idx = FindUnitIndex(section, cu_offset);
140  DWARFUnit *result = GetUnitAtIndex(idx);
141  if (result && result->GetOffset() != cu_offset) {
142  result = nullptr;
143  idx = DW_INVALID_INDEX;
144  }
145  if (idx_ptr)
146  *idx_ptr = idx;
147  return result;
148 }
149 
151  return GetUnitContainingDIEOffset(die_ref.section(), die_ref.die_offset());
152 }
153 
154 DWARFUnit *
156  dw_offset_t die_offset) {
157  uint32_t idx = FindUnitIndex(section, die_offset);
158  DWARFUnit *result = GetUnitAtIndex(idx);
159  if (result && !result->ContainsDIEOffset(die_offset))
160  return nullptr;
161  return result;
162 }
163 
165  auto pos = llvm::lower_bound(m_type_hash_to_unit_index,
166  std::make_pair(hash, 0u), llvm::less_first());
167  if (pos == m_type_hash_to_unit_index.end() || pos->first != hash)
168  return nullptr;
169  return llvm::cast<DWARFTypeUnit>(GetUnitAtIndex(pos->second));
170 }
171 
174  return !m_type_hash_to_unit_index.empty();
175 }
176 
177 DWARFDIE
179  dw_offset_t die_offset) {
180  DWARFUnit *cu = GetUnitContainingDIEOffset(section, die_offset);
181  if (cu)
182  return cu->GetDIE(die_offset);
183  return DWARFDIE();
184 }
185 
186 // GetDIE()
187 //
188 // Get the DIE (Debug Information Entry) with the specified offset.
189 DWARFDIE
191  DWARFUnit *cu = GetUnit(die_ref);
192  if (cu)
193  return cu->GetNonSkeletonUnit().GetDIE(die_ref.die_offset());
194  return DWARFDIE(); // Not found
195 }
RegularExpression.h
DWARFDebugInfo::ContainsTypeUnits
bool ContainsTypeUnits()
Definition: DWARFDebugInfo.cpp:172
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:98
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:945
DWARFDebugInfo::FindUnitIndex
uint32_t FindUnitIndex(DIERef::Section section, dw_offset_t offset)
Definition: DWARFDebugInfo.cpp:118
DWARFContext.h
DWARFTypeUnit
Definition: DWARFTypeUnit.h:15
DWARFDebugInfo::GetNumUnits
size_t GetNumUnits()
Definition: DWARFDebugInfo.cpp:106
SymbolFileDWARF
Definition: SymbolFileDWARF.h:58
SymbolFileDWARF.h
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
DWARFDebugInfo::m_units
UnitColl m_units
Definition: DWARFDebugInfo.h:66
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:895
DWARFDebugInfo::m_type_hash_to_unit_index
std::vector< std::pair< uint64_t, uint32_t > > m_type_hash_to_unit_index
Definition: DWARFDebugInfo.h:71
DW_INVALID_OFFSET
#define DW_INVALID_OFFSET
Definition: dwarf.h:34
DIERef::die_offset
dw_offset_t die_offset() const
Definition: DIERef.h:45
DWARFDebugInfo::GetCompileUnitAranges
const DWARFDebugAranges & GetCompileUnitAranges()
Definition: DWARFDebugInfo.cpp:37
lldb_private::DWARFContext
Definition: DWARFContext.h:20
DWARFDebugInfo::m_units_once_flag
llvm::once_flag m_units_once_flag
Definition: DWARFDebugInfo.h:65
DWARFDIE
Definition: DWARFDIE.h:15
DWARFDebugInfo::DWARFDebugInfo
DWARFDebugInfo(SymbolFileDWARF &dwarf, lldb_private::DWARFContext &context)
Definition: DWARFDebugInfo.cpp:33
DWARFUnit
Definition: DWARFUnit.h:80
dwarf
Definition: ABISysV_arc.cpp:61
DWARFDebugInfo::GetTypeUnitForHash
DWARFTypeUnit * GetTypeUnitForHash(uint64_t hash)
Definition: DWARFDebugInfo.cpp:164
DWARFDebugInfo::m_dwarf
SymbolFileDWARF & m_dwarf
Definition: DWARFDebugInfo.h:62
DWARFUnitSP
std::shared_ptr< DWARFUnit > DWARFUnitSP
Definition: DWARFUnit.h:23
DWARFDebugInfo::GetUnitAtIndex
DWARFUnit * GetUnitAtIndex(size_t idx)
Definition: DWARFDebugInfo.cpp:111
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:129
DWARFDebugInfo::GetUnit
DWARFUnit * GetUnit(const DIERef &die_ref)
Definition: DWARFDebugInfo.cpp:150
DW_INVALID_INDEX
#define DW_INVALID_INDEX
Definition: dwarf.h:35
lldb_private::DWARFContext::getOrLoadArangesData
const DWARFDataExtractor & getOrLoadArangesData()
Definition: DWARFContext.cpp:58
DWARFDebugInfo::ParseUnitsFor
void ParseUnitsFor(DIERef::Section section)
Definition: DWARFDebugInfo.cpp:71
DWARFDebugInfo::GetDIEForDIEOffset
DWARFDIE GetDIEForDIEOffset(DIERef::Section section, dw_offset_t die_offset)
Definition: DWARFDebugInfo.cpp:178
uint32_t
DWARFDebugInfo::GetUnitAtOffset
DWARFUnit * GetUnitAtOffset(DIERef::Section section, dw_offset_t cu_offset, uint32_t *idx_ptr=nullptr)
Definition: DWARFDebugInfo.cpp:136
DWARFDebugInfoEntry.h
DIERef::section
Section section() const
Definition: DIERef.h:43
DWARFUnit::GetNonSkeletonUnit
DWARFUnit & GetNonSkeletonUnit()
Definition: DWARFUnit.cpp:581
DWARFDebugInfo::m_cu_aranges_up
std::unique_ptr< DWARFDebugAranges > m_cu_aranges_up
Definition: DWARFDebugInfo.h:69
DWARFDebugInfo::GetUnitContainingDIEOffset
DWARFUnit * GetUnitContainingDIEOffset(DIERef::Section section, dw_offset_t die_offset)
Definition: DWARFDebugInfo.cpp:155
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:138
Stream.h
lldb
Definition: SBAddress.h:15
DWARFDebugInfo::GetDIE
DWARFDIE GetDIE(const DIERef &die_ref)
Definition: DWARFDebugInfo.cpp:190
DIERef::Section
Section
Definition: DIERef.h:28
DWARFDebugInfo::m_context
lldb_private::DWARFContext & m_context
Definition: DWARFDebugInfo.h:63
DWARFUnit::GetDIE
DWARFDIE GetDIE(dw_offset_t die_offset)
Definition: DWARFUnit.cpp:560