Coverage Report

Created: 2022-01-15 10:30

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/Language/CPlusPlus/LibCxxVector.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LibCxxVector.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 "LibCxx.h"
10
11
#include "lldb/Core/ValueObject.h"
12
#include "lldb/DataFormatters/FormattersHelpers.h"
13
#include "lldb/Utility/ConstString.h"
14
15
using namespace lldb;
16
using namespace lldb_private;
17
using namespace lldb_private::formatters;
18
19
namespace lldb_private {
20
namespace formatters {
21
class LibcxxStdVectorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
22
public:
23
  LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
24
25
  ~LibcxxStdVectorSyntheticFrontEnd() override;
26
27
  size_t CalculateNumChildren() override;
28
29
  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
30
31
  bool Update() override;
32
33
  bool MightHaveChildren() override;
34
35
  size_t GetIndexOfChildWithName(ConstString name) override;
36
37
private:
38
  ValueObject *m_start;
39
  ValueObject *m_finish;
40
  CompilerType m_element_type;
41
  uint32_t m_element_size;
42
};
43
44
class LibcxxVectorBoolSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
45
public:
46
  LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
47
48
  size_t CalculateNumChildren() override;
49
50
  lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
51
52
  bool Update() override;
53
54
0
  bool MightHaveChildren() override { return true; }
55
56
  size_t GetIndexOfChildWithName(ConstString name) override;
57
58
private:
59
  CompilerType m_bool_type;
60
  ExecutionContextRef m_exe_ctx_ref;
61
  uint64_t m_count;
62
  lldb::addr_t m_base_data_address;
63
  std::map<size_t, lldb::ValueObjectSP> m_children;
64
};
65
66
} // namespace formatters
67
} // namespace lldb_private
68
69
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
70
    LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
71
    : SyntheticChildrenFrontEnd(*valobj_sp), m_start(nullptr),
72
143
      m_finish(nullptr), m_element_type(), m_element_size(0) {
73
143
  if (valobj_sp)
74
143
    Update();
75
143
}
76
77
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
78
143
    ~LibcxxStdVectorSyntheticFrontEnd() {
79
  // these need to stay around because they are child objects who will follow
80
  // their parent's life cycle
81
  // delete m_start;
82
  // delete m_finish;
83
143
}
84
85
size_t lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
86
137
    CalculateNumChildren() {
87
137
  if (!m_start || !m_finish)
88
0
    return 0;
89
137
  uint64_t start_val = m_start->GetValueAsUnsigned(0);
90
137
  uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
91
92
137
  if (start_val == 0 || 
finish_val == 0129
)
93
8
    return 0;
94
95
129
  if (start_val >= finish_val)
96
6
    return 0;
97
98
123
  size_t num_children = (finish_val - start_val);
99
123
  if (num_children % m_element_size)
100
0
    return 0;
101
123
  return num_children / m_element_size;
102
123
}
103
104
lldb::ValueObjectSP
105
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::GetChildAtIndex(
106
444
    size_t idx) {
107
444
  if (!m_start || !m_finish)
108
0
    return lldb::ValueObjectSP();
109
110
444
  uint64_t offset = idx * m_element_size;
111
444
  offset = offset + m_start->GetValueAsUnsigned(0);
112
444
  StreamString name;
113
444
  name.Printf("[%" PRIu64 "]", (uint64_t)idx);
114
444
  return CreateValueObjectFromAddress(name.GetString(), offset,
115
444
                                      m_backend.GetExecutionContextRef(),
116
444
                                      m_element_type);
117
444
}
118
119
286
bool lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::Update() {
120
286
  m_start = m_finish = nullptr;
121
286
  ValueObjectSP data_type_finder_sp(
122
286
      m_backend.GetChildMemberWithName(ConstString("__end_cap_"), true));
123
286
  if (!data_type_finder_sp)
124
0
    return false;
125
126
286
  switch (data_type_finder_sp->GetCompilerType().GetNumDirectBaseClasses()) {
127
0
  case 1:
128
    // Assume a pre llvm r300140 __compressed_pair implementation:
129
0
    data_type_finder_sp = data_type_finder_sp->GetChildMemberWithName(
130
0
      ConstString("__first_"), true);
131
0
    break;
132
286
  case 2: {
133
    // Assume a post llvm r300140 __compressed_pair implementation:
134
286
    ValueObjectSP first_elem_parent_sp =
135
286
      data_type_finder_sp->GetChildAtIndex(0, true);
136
286
    data_type_finder_sp = first_elem_parent_sp->GetChildMemberWithName(
137
286
      ConstString("__value_"), true);
138
286
    break;
139
0
  }
140
0
  default:
141
0
    return false;
142
286
  }
143
144
286
  if (!data_type_finder_sp)
145
0
    return false;
146
286
  m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType();
147
286
  if (llvm::Optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
148
286
    m_element_size = *size;
149
150
286
    if (m_element_size > 0) {
151
      // store raw pointers or end up with a circular dependency
152
286
      m_start =
153
286
          m_backend.GetChildMemberWithName(ConstString("__begin_"), true).get();
154
286
      m_finish =
155
286
          m_backend.GetChildMemberWithName(ConstString("__end_"), true).get();
156
286
    }
157
286
  }
158
286
  return false;
159
286
}
160
161
bool lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
162
38
    MightHaveChildren() {
163
38
  return true;
164
38
}
165
166
size_t lldb_private::formatters::LibcxxStdVectorSyntheticFrontEnd::
167
0
    GetIndexOfChildWithName(ConstString name) {
168
0
  if (!m_start || !m_finish)
169
0
    return UINT32_MAX;
170
0
  return ExtractIndexFromString(name.GetCString());
171
0
}
172
173
lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::
174
    LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
175
    : SyntheticChildrenFrontEnd(*valobj_sp), m_bool_type(), m_exe_ctx_ref(),
176
9
      m_count(0), m_base_data_address(0), m_children() {
177
9
  if (valobj_sp) {
178
9
    Update();
179
9
    m_bool_type =
180
9
        valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
181
9
  }
182
9
}
183
184
size_t lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::
185
9
    CalculateNumChildren() {
186
9
  return m_count;
187
9
}
188
189
lldb::ValueObjectSP
190
lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::GetChildAtIndex(
191
306
    size_t idx) {
192
306
  auto iter = m_children.find(idx), end = m_children.end();
193
306
  if (iter != end)
194
0
    return iter->second;
195
306
  if (idx >= m_count)
196
0
    return {};
197
306
  if (m_base_data_address == 0 || m_count == 0)
198
0
    return {};
199
306
  if (!m_bool_type)
200
0
    return {};
201
306
  size_t byte_idx = (idx >> 3); // divide by 8 to get byte index
202
306
  size_t bit_index = (idx & 7); // efficient idx % 8 for bit index
203
306
  lldb::addr_t byte_location = m_base_data_address + byte_idx;
204
306
  ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
205
306
  if (!process_sp)
206
0
    return {};
207
306
  uint8_t byte = 0;
208
306
  uint8_t mask = 0;
209
306
  Status err;
210
306
  size_t bytes_read = process_sp->ReadMemory(byte_location, &byte, 1, err);
211
306
  if (err.Fail() || bytes_read == 0)
212
0
    return {};
213
306
  mask = 1 << bit_index;
214
306
  bool bit_set = ((byte & mask) != 0);
215
306
  llvm::Optional<uint64_t> size = m_bool_type.GetByteSize(nullptr);
216
306
  if (!size)
217
0
    return {};
218
306
  DataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
219
306
  if (bit_set && 
buffer_sp156
&&
buffer_sp->GetBytes()156
) {
220
    // regardless of endianness, anything non-zero is true
221
156
    *(buffer_sp->GetBytes()) = 1;
222
156
  }
223
306
  StreamString name;
224
306
  name.Printf("[%" PRIu64 "]", (uint64_t)idx);
225
306
  ValueObjectSP retval_sp(CreateValueObjectFromData(
226
306
      name.GetString(),
227
306
      DataExtractor(buffer_sp, process_sp->GetByteOrder(),
228
306
                    process_sp->GetAddressByteSize()),
229
306
      m_exe_ctx_ref, m_bool_type));
230
306
  if (retval_sp)
231
306
    m_children[idx] = retval_sp;
232
306
  return retval_sp;
233
306
}
234
235
/*(std::__1::vector<std::__1::allocator<bool> >) vBool = {
236
 __begin_ = 0x00000001001000e0
237
 __size_ = 56
238
 __cap_alloc_ = {
239
 std::__1::__libcpp_compressed_pair_imp<unsigned long,
240
 std::__1::allocator<unsigned long> > = {
241
 __first_ = 1
242
 }
243
 }
244
 }*/
245
246
18
bool lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::Update() {
247
18
  m_children.clear();
248
18
  ValueObjectSP valobj_sp = m_backend.GetSP();
249
18
  if (!valobj_sp)
250
0
    return false;
251
18
  m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
252
18
  ValueObjectSP size_sp(
253
18
      valobj_sp->GetChildMemberWithName(ConstString("__size_"), true));
254
18
  if (!size_sp)
255
0
    return false;
256
18
  m_count = size_sp->GetValueAsUnsigned(0);
257
18
  if (!m_count)
258
0
    return true;
259
18
  ValueObjectSP begin_sp(
260
18
      valobj_sp->GetChildMemberWithName(ConstString("__begin_"), true));
261
18
  if (!begin_sp) {
262
0
    m_count = 0;
263
0
    return false;
264
0
  }
265
18
  m_base_data_address = begin_sp->GetValueAsUnsigned(0);
266
18
  if (!m_base_data_address) {
267
0
    m_count = 0;
268
0
    return false;
269
0
  }
270
18
  return false;
271
18
}
272
273
size_t lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::
274
0
    GetIndexOfChildWithName(ConstString name) {
275
0
  if (!m_count || !m_base_data_address)
276
0
    return UINT32_MAX;
277
0
  const char *item_name = name.GetCString();
278
0
  uint32_t idx = ExtractIndexFromString(item_name);
279
0
  if (idx < UINT32_MAX && idx >= CalculateNumChildren())
280
0
    return UINT32_MAX;
281
0
  return idx;
282
0
}
283
284
lldb_private::SyntheticChildrenFrontEnd *
285
lldb_private::formatters::LibcxxStdVectorSyntheticFrontEndCreator(
286
152
    CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
287
152
  if (!valobj_sp)
288
0
    return nullptr;
289
152
  CompilerType type = valobj_sp->GetCompilerType();
290
152
  if (!type.IsValid() || type.GetNumTemplateArguments() == 0)
291
0
    return nullptr;
292
152
  CompilerType arg_type = type.GetTypeTemplateArgument(0);
293
152
  if (arg_type.GetTypeName() == "bool")
294
9
    return new LibcxxVectorBoolSyntheticFrontEnd(valobj_sp);
295
143
  return new LibcxxStdVectorSyntheticFrontEnd(valobj_sp);
296
152
}