1 //===-- LibStdcpp.cpp ---------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/lldb-python.h"
11
12 #include "lldb/DataFormatters/CXXFormatterFunctions.h"
13
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/ValueObject.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Host/Endian.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/ObjCLanguageRuntime.h"
22 #include "lldb/Target/Target.h"
23
24 using namespace lldb;
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
27
LibstdcppVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)28 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::LibstdcppVectorBoolSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
29 SyntheticChildrenFrontEnd(*valobj_sp.get()),
30 m_exe_ctx_ref(),
31 m_count(0),
32 m_base_data_address(0),
33 m_options()
34 {
35 if (valobj_sp)
36 Update();
37 m_options.SetCoerceToId(false)
38 .SetUnwindOnError(true)
39 .SetKeepInMemory(true)
40 .SetUseDynamic(lldb::eDynamicCanRunTarget);
41 }
42
43 size_t
CalculateNumChildren()44 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::CalculateNumChildren ()
45 {
46 return m_count;
47 }
48
49 lldb::ValueObjectSP
GetChildAtIndex(size_t idx)50 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::GetChildAtIndex (size_t idx)
51 {
52 if (idx >= m_count)
53 return ValueObjectSP();
54 if (m_base_data_address == 0 || m_count == 0)
55 return ValueObjectSP();
56 size_t byte_idx = (idx >> 3); // divide by 8 to get byte index
57 size_t bit_index = (idx & 7); // efficient idx % 8 for bit index
58 lldb::addr_t byte_location = m_base_data_address + byte_idx;
59 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
60 if (!process_sp)
61 return ValueObjectSP();
62 uint8_t byte = 0;
63 uint8_t mask = 0;
64 Error err;
65 size_t bytes_read = process_sp->ReadMemory(byte_location, &byte, 1, err);
66 if (err.Fail() || bytes_read == 0)
67 return ValueObjectSP();
68 switch (bit_index)
69 {
70 case 0:
71 mask = 1; break;
72 case 1:
73 mask = 2; break;
74 case 2:
75 mask = 4; break;
76 case 3:
77 mask = 8; break;
78 case 4:
79 mask = 16; break;
80 case 5:
81 mask = 32; break;
82 case 6:
83 mask = 64; break;
84 case 7:
85 mask = 128; break;
86 default:
87 return ValueObjectSP();
88 }
89 bool bit_set = ((byte & mask) != 0);
90 Target& target(process_sp->GetTarget());
91 ValueObjectSP retval_sp;
92 if (bit_set)
93 target.EvaluateExpression("(bool)true", NULL, retval_sp);
94 else
95 target.EvaluateExpression("(bool)false", NULL, retval_sp);
96 StreamString name; name.Printf("[%zu]",idx);
97 if (retval_sp)
98 retval_sp->SetName(ConstString(name.GetData()));
99 return retval_sp;
100 }
101
102 /*((std::vector<std::allocator<bool> >) vBool = {
103 (std::_Bvector_base<std::allocator<bool> >) std::_Bvector_base<std::allocator<bool> > = {
104 (std::_Bvector_base<std::allocator<bool> >::_Bvector_impl) _M_impl = {
105 (std::_Bit_iterator) _M_start = {
106 (std::_Bit_iterator_base) std::_Bit_iterator_base = {
107 (_Bit_type *) _M_p = 0x0016b160
108 (unsigned int) _M_offset = 0
109 }
110 }
111 (std::_Bit_iterator) _M_finish = {
112 (std::_Bit_iterator_base) std::_Bit_iterator_base = {
113 (_Bit_type *) _M_p = 0x0016b16c
114 (unsigned int) _M_offset = 16
115 }
116 }
117 (_Bit_type *) _M_end_of_storage = 0x0016b170
118 }
119 }
120 }
121 */
122
123 bool
Update()124 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::Update()
125 {
126 ValueObjectSP valobj_sp = m_backend.GetSP();
127 if (!valobj_sp)
128 return false;
129 if (!valobj_sp)
130 return false;
131 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
132
133 ValueObjectSP m_impl_sp(valobj_sp->GetChildMemberWithName(ConstString("_M_impl"), true));
134 if (!m_impl_sp)
135 return false;
136
137 ValueObjectSP m_start_sp(m_impl_sp->GetChildMemberWithName(ConstString("_M_start"), true));
138 ValueObjectSP m_finish_sp(m_impl_sp->GetChildMemberWithName(ConstString("_M_finish"), true));
139
140 ValueObjectSP start_p_sp, finish_p_sp, finish_offset_sp;
141
142 if (!m_start_sp || !m_finish_sp)
143 return false;
144
145 start_p_sp = m_start_sp->GetChildMemberWithName(ConstString("_M_p"), true);
146 finish_p_sp = m_finish_sp->GetChildMemberWithName(ConstString("_M_p"), true);
147 finish_offset_sp = m_finish_sp->GetChildMemberWithName(ConstString("_M_offset"), true);
148
149 if (!start_p_sp || !finish_offset_sp || !finish_p_sp)
150 return false;
151
152 m_base_data_address = start_p_sp->GetValueAsUnsigned(0);
153 if (!m_base_data_address)
154 return false;
155
156 lldb::addr_t end_data_address(finish_p_sp->GetValueAsUnsigned(0));
157 if (!end_data_address)
158 return false;
159
160 if (end_data_address < m_base_data_address)
161 return false;
162 else
163 m_count = finish_offset_sp->GetValueAsUnsigned(0) + (end_data_address-m_base_data_address)*8;
164
165 return true;
166 }
167
168 bool
MightHaveChildren()169 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::MightHaveChildren ()
170 {
171 return true;
172 }
173
174 size_t
GetIndexOfChildWithName(const ConstString & name)175 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
176 {
177 if (!m_count || !m_base_data_address)
178 return UINT32_MAX;
179 const char* item_name = name.GetCString();
180 uint32_t idx = ExtractIndexFromString(item_name);
181 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
182 return UINT32_MAX;
183 return idx;
184 }
185
~LibstdcppVectorBoolSyntheticFrontEnd()186 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEnd::~LibstdcppVectorBoolSyntheticFrontEnd ()
187 {}
188
189 SyntheticChildrenFrontEnd*
LibstdcppVectorBoolSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)190 lldb_private::formatters::LibstdcppVectorBoolSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
191 {
192 if (!valobj_sp)
193 return NULL;
194 return (new LibstdcppVectorBoolSyntheticFrontEnd(valobj_sp));
195 }
196
197 /*
198 (std::_Rb_tree_iterator<std::pair<const int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >) ibeg = {
199 (_Base_ptr) _M_node = 0x0000000100103910 {
200 (std::_Rb_tree_color) _M_color = _S_black
201 (std::_Rb_tree_node_base::_Base_ptr) _M_parent = 0x00000001001038c0
202 (std::_Rb_tree_node_base::_Base_ptr) _M_left = 0x0000000000000000
203 (std::_Rb_tree_node_base::_Base_ptr) _M_right = 0x0000000000000000
204 }
205 }
206 */
207
LibstdcppMapIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)208 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::LibstdcppMapIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
209 SyntheticChildrenFrontEnd(*valobj_sp.get()),
210 m_exe_ctx_ref(),
211 m_pair_address(0),
212 m_pair_type(),
213 m_options(),
214 m_pair_sp()
215 {
216 if (valobj_sp)
217 Update();
218 m_options.SetCoerceToId(false)
219 .SetUnwindOnError(true)
220 .SetKeepInMemory(true)
221 .SetUseDynamic(lldb::eDynamicCanRunTarget);
222 }
223
224 bool
Update()225 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::Update()
226 {
227 ValueObjectSP valobj_sp = m_backend.GetSP();
228 if (!valobj_sp)
229 return false;
230
231 TargetSP target_sp(valobj_sp->GetTargetSP());
232
233 if (!target_sp)
234 return false;
235
236 bool is_64bit = (target_sp->GetArchitecture().GetAddressByteSize() == 8);
237
238 if (!valobj_sp)
239 return false;
240 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
241
242 ValueObjectSP _M_node_sp(valobj_sp->GetChildMemberWithName(ConstString("_M_node"), true));
243 if (!_M_node_sp)
244 return false;
245
246 m_pair_address = _M_node_sp->GetValueAsUnsigned(0);
247 if (m_pair_address == 0)
248 return false;
249
250 m_pair_address += (is_64bit ? 32 : 16);
251
252 ClangASTType my_type(valobj_sp->GetClangType());
253 if (my_type.GetNumTemplateArguments() >= 1)
254 {
255 TemplateArgumentKind kind;
256 ClangASTType pair_type = my_type.GetTemplateArgument(0, kind);
257 if (kind != eTemplateArgumentKindType && kind != eTemplateArgumentKindTemplate && kind != eTemplateArgumentKindTemplateExpansion)
258 return false;
259 m_pair_type = pair_type;
260 }
261 else
262 return false;
263
264 return true;
265 }
266
267 size_t
CalculateNumChildren()268 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::CalculateNumChildren ()
269 {
270 return 2;
271 }
272
273 lldb::ValueObjectSP
GetChildAtIndex(size_t idx)274 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx)
275 {
276 if (m_pair_address != 0 && m_pair_type)
277 {
278 if (!m_pair_sp)
279 m_pair_sp = ValueObject::CreateValueObjectFromAddress("pair", m_pair_address, m_exe_ctx_ref, m_pair_type);
280 if (m_pair_sp)
281 return m_pair_sp->GetChildAtIndex(idx, true);
282 }
283 return lldb::ValueObjectSP();
284 }
285
286 bool
MightHaveChildren()287 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::MightHaveChildren ()
288 {
289 return true;
290 }
291
292 size_t
GetIndexOfChildWithName(const ConstString & name)293 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
294 {
295 if (name == ConstString("first"))
296 return 0;
297 if (name == ConstString("second"))
298 return 1;
299 return UINT32_MAX;
300 }
301
~LibstdcppMapIteratorSyntheticFrontEnd()302 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEnd::~LibstdcppMapIteratorSyntheticFrontEnd ()
303 {}
304
305 SyntheticChildrenFrontEnd*
LibstdcppMapIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)306 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
307 {
308 if (!valobj_sp)
309 return NULL;
310 return (new LibstdcppMapIteratorSyntheticFrontEnd(valobj_sp));
311 }
312
313 /*
314 (lldb) fr var ibeg --ptr-depth 1
315 (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >) ibeg = {
316 _M_current = 0x00000001001037a0 {
317 *_M_current = 1
318 }
319 }
320 */
321
322 SyntheticChildrenFrontEnd*
LibStdcppVectorIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *,lldb::ValueObjectSP valobj_sp)323 lldb_private::formatters::LibStdcppVectorIteratorSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
324 {
325 static ConstString g_item_name;
326 if (!g_item_name)
327 g_item_name.SetCString("_M_current");
328 if (!valobj_sp)
329 return NULL;
330 return (new VectorIteratorSyntheticFrontEnd(valobj_sp,g_item_name));
331 }
332