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