Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
Line
Count
Source (jump to first uncovered line)
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"
16
#include "lldb/Utility/RegularExpression.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
33
DWARFDebugInfo::DWARFDebugInfo(SymbolFileDWARF &dwarf,
34
                               lldb_private::DWARFContext &context)
35
12.0k
    : m_dwarf(dwarf), m_context(context), m_units(), m_cu_aranges_up() {}
36
37
71.7k
const DWARFDebugAranges &DWARFDebugInfo::GetCompileUnitAranges() {
38
71.7k
  if (m_cu_aranges_up)
39
69.1k
    return *m_cu_aranges_up;
40
41
2.62k
  m_cu_aranges_up = std::make_unique<DWARFDebugAranges>();
42
2.62k
  const DWARFDataExtractor &debug_aranges_data =
43
2.62k
      m_context.getOrLoadArangesData();
44
45
  // Extract what we can from the .debug_aranges first.
46
2.62k
  m_cu_aranges_up->extract(debug_aranges_data);
47
48
  // Make a list of all CUs represented by the .debug_aranges data.
49
2.62k
  std::set<dw_offset_t> cus_with_data;
50
32.6k
  for (size_t n = 0; n < m_cu_aranges_up->GetNumRanges(); 
n++30.0k
) {
51
30.0k
    dw_offset_t offset = m_cu_aranges_up->OffsetAtIndex(n);
52
30.0k
    if (offset != DW_INVALID_OFFSET)
53
30.0k
      cus_with_data.insert(offset);
54
30.0k
  }
55
56
  // Manually build arange data for everything that wasn't in .debug_aranges.
57
2.62k
  const size_t num_units = GetNumUnits();
58
6.30k
  for (size_t idx = 0; idx < num_units; 
++idx3.68k
) {
59
3.68k
    DWARFUnit *cu = GetUnitAtIndex(idx);
60
61
3.68k
    dw_offset_t offset = cu->GetOffset();
62
3.68k
    if (cus_with_data.find(offset) == cus_with_data.end())
63
2.47k
      cu->BuildAddressRangeTable(m_cu_aranges_up.get());
64
3.68k
  }
65
66
2.62k
  const bool minimize = true;
67
2.62k
  m_cu_aranges_up->Sort(minimize);
68
2.62k
  return *m_cu_aranges_up;
69
71.7k
}
70
71
24.1k
void DWARFDebugInfo::ParseUnitsFor(DIERef::Section section) {
72
24.1k
  DWARFDataExtractor data = section == DIERef::Section::DebugTypes
73
24.1k
                                ? 
m_context.getOrLoadDebugTypesData()12.0k
74
24.1k
                                : 
m_context.getOrLoadDebugInfoData()12.0k
;
75
24.1k
  lldb::offset_t offset = 0;
76
46.5k
  while (data.ValidOffset(offset)) {
77
28.3k
    llvm::Expected<DWARFUnitSP> unit_sp = DWARFUnit::extract(
78
28.3k
        m_dwarf, m_units.size(), data, section, &offset);
79
80
28.3k
    if (!unit_sp) {
81
      // FIXME: Propagate this error up.
82
5.86k
      llvm::consumeError(unit_sp.takeError());
83
5.86k
      return;
84
5.86k
    }
85
86
    // If it didn't return an error, then it should be returning a valid Unit.
87
22.4k
    assert(*unit_sp);
88
0
    m_units.push_back(*unit_sp);
89
22.4k
    offset = (*unit_sp)->GetNextUnitOffset();
90
91
22.4k
    if (auto *type_unit = llvm::dyn_cast<DWARFTypeUnit>(unit_sp->get())) {
92
11
      m_type_hash_to_unit_index.emplace_back(type_unit->GetTypeHash(),
93
11
                                             unit_sp.get()->GetID());
94
11
    }
95
22.4k
  }
96
24.1k
}
97
98
2.09M
void DWARFDebugInfo::ParseUnitHeadersIfNeeded() {
99
2.09M
  llvm::call_once(m_units_once_flag, [&] {
100
12.0k
    ParseUnitsFor(DIERef::Section::DebugInfo);
101
12.0k
    ParseUnitsFor(DIERef::Section::DebugTypes);
102
12.0k
    llvm::sort(m_type_hash_to_unit_index, llvm::less_first());
103
12.0k
  });
104
2.09M
}
105
106
1.10M
size_t DWARFDebugInfo::GetNumUnits() {
107
1.10M
  ParseUnitHeadersIfNeeded();
108
1.10M
  return m_units.size();
109
1.10M
}
110
111
1.07M
DWARFUnit *DWARFDebugInfo::GetUnitAtIndex(size_t idx) {
112
1.07M
  DWARFUnit *cu = nullptr;
113
1.07M
  if (idx < GetNumUnits())
114
1.07M
    cu = m_units[idx].get();
115
1.07M
  return cu;
116
1.07M
}
117
118
uint32_t DWARFDebugInfo::FindUnitIndex(DIERef::Section section,
119
958k
                                       dw_offset_t offset) {
120
958k
  ParseUnitHeadersIfNeeded();
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
958k
  auto pos = llvm::upper_bound(
125
958k
      m_units, std::make_pair(section, offset),
126
958k
      [](const std::pair<DIERef::Section, dw_offset_t> &lhs,
127
1.33M
         const DWARFUnitSP &rhs) {
128
1.33M
        return lhs < std::make_pair(rhs->GetDebugSection(), rhs->GetOffset());
129
1.33M
      });
130
958k
  uint32_t idx = std::distance(m_units.begin(), pos);
131
958k
  if (idx == 0)
132
0
    return DW_INVALID_OFFSET;
133
958k
  return idx - 1;
134
958k
}
135
136
DWARFUnit *DWARFDebugInfo::GetUnitAtOffset(DIERef::Section section,
137
                                           dw_offset_t cu_offset,
138
70.0k
                                           uint32_t *idx_ptr) {
139
70.0k
  uint32_t idx = FindUnitIndex(section, cu_offset);
140
70.0k
  DWARFUnit *result = GetUnitAtIndex(idx);
141
70.0k
  if (result && 
result->GetOffset() != cu_offset70.0k
) {
142
0
    result = nullptr;
143
0
    idx = DW_INVALID_INDEX;
144
0
  }
145
70.0k
  if (idx_ptr)
146
70.0k
    *idx_ptr = idx;
147
70.0k
  return result;
148
70.0k
}
149
150
792k
DWARFUnit *DWARFDebugInfo::GetUnit(const DIERef &die_ref) {
151
792k
  return GetUnitContainingDIEOffset(die_ref.section(), die_ref.die_offset());
152
792k
}
153
154
DWARFUnit *
155
DWARFDebugInfo::GetUnitContainingDIEOffset(DIERef::Section section,
156
888k
                                           dw_offset_t die_offset) {
157
888k
  uint32_t idx = FindUnitIndex(section, die_offset);
158
888k
  DWARFUnit *result = GetUnitAtIndex(idx);
159
888k
  if (result && !result->ContainsDIEOffset(die_offset))
160
0
    return nullptr;
161
888k
  return result;
162
888k
}
163
164
12
DWARFTypeUnit *DWARFDebugInfo::GetTypeUnitForHash(uint64_t hash) {
165
12
  auto pos = llvm::lower_bound(m_type_hash_to_unit_index,
166
12
                               std::make_pair(hash, 0u), llvm::less_first());
167
12
  if (pos == m_type_hash_to_unit_index.end() || 
pos->first != hash9
)
168
3
    return nullptr;
169
9
  return llvm::cast<DWARFTypeUnit>(GetUnitAtIndex(pos->second));
170
12
}
171
172
36.5k
bool DWARFDebugInfo::ContainsTypeUnits() {
173
36.5k
  ParseUnitHeadersIfNeeded();
174
36.5k
  return !m_type_hash_to_unit_index.empty();
175
36.5k
}
176
177
// GetDIE()
178
//
179
// Get the DIE (Debug Information Entry) with the specified offset.
180
DWARFDIE
181
792k
DWARFDebugInfo::GetDIE(const DIERef &die_ref) {
182
792k
  DWARFUnit *cu = GetUnit(die_ref);
183
792k
  if (cu)
184
792k
    return cu->GetNonSkeletonUnit().GetDIE(die_ref.die_offset());
185
0
  return DWARFDIE(); // Not found
186
792k
}