1 | //===-- LibCxxProxyArray.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/DataFormatters/FormattersHelpers.h" |
12 | #include "lldb/ValueObject/ValueObject.h" |
13 | #include <optional> |
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 | |
22 | /// Data formatter for libc++'s std::"proxy_array". |
23 | /// |
24 | /// A proxy_array's are created by using: |
25 | /// std::gslice_array operator[](const std::gslice& gslicearr); |
26 | /// std::mask_array operator[](const std::valarray<bool>& boolarr); |
27 | /// std::indirect_array operator[](const std::valarray<std::size_t>& indarr); |
28 | /// |
29 | /// These arrays have the following members: |
30 | /// - __vp_ points to std::valarray::__begin_ |
31 | /// - __1d_ an array of offsets of the elements from @a __vp_ |
32 | class LibcxxStdProxyArraySyntheticFrontEnd : public SyntheticChildrenFrontEnd { |
33 | public: |
34 | LibcxxStdProxyArraySyntheticFrontEnd(lldb::ValueObjectSP valobj_sp); |
35 | |
36 | ~LibcxxStdProxyArraySyntheticFrontEnd() override; |
37 | |
38 | llvm::Expected<uint32_t> CalculateNumChildren() override; |
39 | |
40 | lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override; |
41 | |
42 | lldb::ChildCacheState Update() override; |
43 | |
44 | llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override; |
45 | |
46 | private: |
47 | /// A non-owning pointer to the array's __vp_. |
48 | ValueObject *m_base = nullptr; |
49 | /// The type of the array's template argument T. |
50 | CompilerType m_element_type; |
51 | /// The sizeof the array's template argument T. |
52 | uint32_t m_element_size = 0; |
53 | |
54 | /// A non-owning pointer to the array's __1d_.__begin_. |
55 | ValueObject *m_start = nullptr; |
56 | /// A non-owning pointer to the array's __1d_.__end_. |
57 | ValueObject *m_finish = nullptr; |
58 | /// The type of the __1d_ array's template argument T (size_t). |
59 | CompilerType m_element_type_size_t; |
60 | /// The sizeof the __1d_ array's template argument T (size_t) |
61 | uint32_t m_element_size_size_t = 0; |
62 | }; |
63 | |
64 | } // namespace formatters |
65 | } // namespace lldb_private |
66 | |
67 | lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd:: |
68 | LibcxxStdProxyArraySyntheticFrontEnd(lldb::ValueObjectSP valobj_sp) |
69 | : SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() { |
70 | if (valobj_sp) |
71 | Update(); |
72 | } |
73 | |
74 | lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd:: |
75 | ~LibcxxStdProxyArraySyntheticFrontEnd() { |
76 | // these need to stay around because they are child objects who will follow |
77 | // their parent's life cycle |
78 | // delete m_base; |
79 | } |
80 | |
81 | llvm::Expected<uint32_t> lldb_private::formatters:: |
82 | LibcxxStdProxyArraySyntheticFrontEnd::CalculateNumChildren() { |
83 | |
84 | if (!m_start || !m_finish) |
85 | return 0; |
86 | uint64_t start_val = m_start->GetValueAsUnsigned(fail_value: 0); |
87 | uint64_t finish_val = m_finish->GetValueAsUnsigned(fail_value: 0); |
88 | |
89 | if (start_val == 0 || finish_val == 0) |
90 | return 0; |
91 | |
92 | if (start_val >= finish_val) |
93 | return 0; |
94 | |
95 | size_t num_children = (finish_val - start_val); |
96 | if (num_children % m_element_size_size_t) |
97 | return 0; |
98 | return num_children / m_element_size_size_t; |
99 | } |
100 | |
101 | lldb::ValueObjectSP |
102 | lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd::GetChildAtIndex( |
103 | uint32_t idx) { |
104 | if (!m_base) |
105 | return lldb::ValueObjectSP(); |
106 | |
107 | uint64_t offset = idx * m_element_size_size_t; |
108 | offset = offset + m_start->GetValueAsUnsigned(fail_value: 0); |
109 | |
110 | lldb::ValueObjectSP indirect = CreateValueObjectFromAddress( |
111 | name: "" , address: offset, exe_ctx: m_backend.GetExecutionContextRef(), type: m_element_type_size_t); |
112 | if (!indirect) |
113 | return lldb::ValueObjectSP(); |
114 | |
115 | const size_t value = indirect->GetValueAsUnsigned(fail_value: 0); |
116 | if (!value) |
117 | return lldb::ValueObjectSP(); |
118 | |
119 | offset = value * m_element_size; |
120 | offset = offset + m_base->GetValueAsUnsigned(fail_value: 0); |
121 | |
122 | StreamString name; |
123 | name.Printf(format: "[%" PRIu64 "] -> [%zu]" , (uint64_t)idx, value); |
124 | return CreateValueObjectFromAddress(name: name.GetString(), address: offset, |
125 | exe_ctx: m_backend.GetExecutionContextRef(), |
126 | type: m_element_type); |
127 | } |
128 | |
129 | lldb::ChildCacheState |
130 | lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd::Update() { |
131 | m_base = nullptr; |
132 | m_start = nullptr; |
133 | m_finish = nullptr; |
134 | |
135 | CompilerType type = m_backend.GetCompilerType(); |
136 | if (type.GetNumTemplateArguments() == 0) |
137 | return ChildCacheState::eRefetch; |
138 | |
139 | m_element_type = type.GetTypeTemplateArgument(idx: 0); |
140 | if (std::optional<uint64_t> size = |
141 | llvm::expectedToOptional(E: m_element_type.GetByteSize(exe_scope: nullptr))) |
142 | m_element_size = *size; |
143 | |
144 | if (m_element_size == 0) |
145 | return ChildCacheState::eRefetch; |
146 | |
147 | ValueObjectSP vector = m_backend.GetChildMemberWithName(name: "__1d_" ); |
148 | if (!vector) |
149 | return ChildCacheState::eRefetch; |
150 | |
151 | type = vector->GetCompilerType(); |
152 | if (type.GetNumTemplateArguments() == 0) |
153 | return ChildCacheState::eRefetch; |
154 | |
155 | m_element_type_size_t = type.GetTypeTemplateArgument(idx: 0); |
156 | if (std::optional<uint64_t> size = |
157 | llvm::expectedToOptional(E: m_element_type_size_t.GetByteSize(exe_scope: nullptr))) |
158 | m_element_size_size_t = *size; |
159 | |
160 | if (m_element_size_size_t == 0) |
161 | return ChildCacheState::eRefetch; |
162 | |
163 | ValueObjectSP base = m_backend.GetChildMemberWithName(name: "__vp_" ); |
164 | ValueObjectSP start = vector->GetChildMemberWithName(name: "__begin_" ); |
165 | ValueObjectSP finish = vector->GetChildMemberWithName(name: "__end_" ); |
166 | if (!base || !start || !finish) |
167 | return ChildCacheState::eRefetch; |
168 | |
169 | m_base = base.get(); |
170 | m_start = start.get(); |
171 | m_finish = finish.get(); |
172 | |
173 | return ChildCacheState::eRefetch; |
174 | } |
175 | |
176 | llvm::Expected<size_t> |
177 | lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEnd:: |
178 | GetIndexOfChildWithName(ConstString name) { |
179 | if (!m_base) |
180 | return llvm::createStringError(Fmt: "Type has no child named '%s'" , |
181 | Vals: name.AsCString()); |
182 | auto optional_idx = formatters::ExtractIndexFromString(item_name: name.GetCString()); |
183 | if (!optional_idx) { |
184 | return llvm::createStringError(Fmt: "Type has no child named '%s'" , |
185 | Vals: name.AsCString()); |
186 | } |
187 | return *optional_idx; |
188 | } |
189 | |
190 | lldb_private::SyntheticChildrenFrontEnd * |
191 | lldb_private::formatters::LibcxxStdProxyArraySyntheticFrontEndCreator( |
192 | CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) { |
193 | if (!valobj_sp) |
194 | return nullptr; |
195 | return new LibcxxStdProxyArraySyntheticFrontEnd(valobj_sp); |
196 | } |
197 | |