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