1 //===-- LibCxx.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/DataFormatters/CXXFormatterFunctions.h"
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Error.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/ValueObject.h"
19 #include "lldb/Core/ValueObjectConstResult.h"
20 #include "lldb/Host/Endian.h"
21 #include "lldb/Symbol/ClangASTContext.h"
22 #include "lldb/Target/ObjCLanguageRuntime.h"
23 #include "lldb/Target/Target.h"
26 using namespace lldb_private;
27 using namespace lldb_private::formatters;
30 lldb_private::formatters::LibcxxSmartPointerSummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
32 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
35 ValueObjectSP ptr_sp(valobj_sp->GetChildMemberWithName(ConstString("__ptr_"), true));
36 ValueObjectSP count_sp(valobj_sp->GetChildAtNamePath( {ConstString("__cntrl_"),ConstString("__shared_owners_")} ));
37 ValueObjectSP weakcount_sp(valobj_sp->GetChildAtNamePath( {ConstString("__cntrl_"),ConstString("__shared_weak_owners_")} ));
42 if (ptr_sp->GetValueAsUnsigned(0) == 0)
44 stream.Printf("nullptr");
49 bool print_pointee = false;
51 ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
52 if (pointee_sp && error.Success())
54 if (pointee_sp->DumpPrintableRepresentation(stream,
55 ValueObject::eValueObjectRepresentationStyleSummary,
57 ValueObject::ePrintableRepresentationSpecialCasesDisable,
62 stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));
66 stream.Printf(" strong=%" PRIu64, 1+count_sp->GetValueAsUnsigned(0));
69 stream.Printf(" weak=%" PRIu64, 1+weakcount_sp->GetValueAsUnsigned(0));
74 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::LibcxxVectorBoolSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
75 SyntheticChildrenFrontEnd(*valobj_sp.get()),
79 m_base_data_address(0),
85 m_bool_type = valobj_sp->GetClangType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
90 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::CalculateNumChildren ()
96 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::GetChildAtIndex (size_t idx)
98 auto iter = m_children.find(idx),
99 end = m_children.end();
103 return ValueObjectSP();
104 if (m_base_data_address == 0 || m_count == 0)
105 return ValueObjectSP();
107 return ValueObjectSP();
108 size_t byte_idx = (idx >> 3); // divide by 8 to get byte index
109 size_t bit_index = (idx & 7); // efficient idx % 8 for bit index
110 lldb::addr_t byte_location = m_base_data_address + byte_idx;
111 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
113 return ValueObjectSP();
117 size_t bytes_read = process_sp->ReadMemory(byte_location, &byte, 1, err);
118 if (err.Fail() || bytes_read == 0)
119 return ValueObjectSP();
139 return ValueObjectSP();
141 bool bit_set = ((byte & mask) != 0);
142 DataBufferSP buffer_sp(new DataBufferHeap(m_bool_type.GetByteSize(),0));
143 if (bit_set && buffer_sp && buffer_sp->GetBytes())
144 *(buffer_sp->GetBytes()) = 1; // regardless of endianness, anything non-zero is true
145 StreamString name; name.Printf("[%" PRIu64 "]", (uint64_t)idx);
146 ValueObjectSP retval_sp(CreateValueObjectFromData(name.GetData(), DataExtractor(buffer_sp, process_sp->GetByteOrder(), process_sp->GetAddressByteSize()), m_exe_ctx_ref, m_bool_type));
148 m_children[idx] = retval_sp;
152 /*(std::__1::vector<std::__1::allocator<bool> >) vBool = {
153 __begin_ = 0x00000001001000e0
156 std::__1::__libcpp_compressed_pair_imp<unsigned long, std::__1::allocator<unsigned long> > = {
163 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::Update()
166 ValueObjectSP valobj_sp = m_backend.GetSP();
169 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
170 ValueObjectSP size_sp(valobj_sp->GetChildMemberWithName(ConstString("__size_"), true));
173 m_count = size_sp->GetValueAsUnsigned(0);
176 ValueObjectSP begin_sp(valobj_sp->GetChildMemberWithName(ConstString("__begin_"), true));
182 m_base_data_address = begin_sp->GetValueAsUnsigned(0);
183 if (!m_base_data_address)
192 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::MightHaveChildren ()
198 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
200 if (!m_count || !m_base_data_address)
202 const char* item_name = name.GetCString();
203 uint32_t idx = ExtractIndexFromString(item_name);
204 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
209 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEnd::~LibcxxVectorBoolSyntheticFrontEnd ()
212 SyntheticChildrenFrontEnd*
213 lldb_private::formatters::LibcxxVectorBoolSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
217 return (new LibcxxVectorBoolSyntheticFrontEnd(valobj_sp));
221 (lldb) fr var ibeg --raw --ptr-depth 1
222 (std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::pair<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::__tree_node<std::__1::pair<int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, void *> *, long> >) ibeg = {
224 __ptr_ = 0x0000000100103870 {
225 std::__1::__tree_node_base<void *> = {
226 std::__1::__tree_end_node<std::__1::__tree_node_base<void *> *> = {
227 __left_ = 0x0000000000000000
229 __right_ = 0x0000000000000000
230 __parent_ = 0x00000001001038b0
235 second = { std::string }
238 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::LibCxxMapIteratorSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
239 SyntheticChildrenFrontEnd(*valobj_sp.get()),
247 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::Update()
249 ValueObjectSP valobj_sp = m_backend.GetSP();
253 TargetSP target_sp(valobj_sp->GetTargetSP());
261 // this must be a ValueObject* because it is a child of the ValueObject we are producing children for
262 // it if were a ValueObjectSP, we would end up with a loop (iterator -> synthetic -> child -> parent == iterator)
263 // and that would in turn leak memory by never allowing the ValueObjects to die and free their memory
264 m_pair_ptr = valobj_sp->GetValueForExpressionPath(".__i_.__ptr_->__value_",
268 ValueObject::GetValueForExpressionPathOptions().DontCheckDotVsArrowSyntax().DontAllowSyntheticChildren(),
275 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::CalculateNumChildren ()
281 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::GetChildAtIndex (size_t idx)
284 return lldb::ValueObjectSP();
285 return m_pair_ptr->GetChildAtIndex(idx, true);
289 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::MightHaveChildren ()
295 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
297 if (name == ConstString("first"))
299 if (name == ConstString("second"))
304 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEnd::~LibCxxMapIteratorSyntheticFrontEnd ()
306 // this will be deleted when its parent dies (since it's a child object)
310 SyntheticChildrenFrontEnd*
311 lldb_private::formatters::LibCxxMapIteratorSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
315 return (new LibCxxMapIteratorSyntheticFrontEnd(valobj_sp));
319 (lldb) fr var ibeg --raw --ptr-depth 1 -T
320 (std::__1::__wrap_iter<int *>) ibeg = {
321 (std::__1::__wrap_iter<int *>::iterator_type) __i = 0x00000001001037a0 {
327 SyntheticChildrenFrontEnd*
328 lldb_private::formatters::LibCxxVectorIteratorSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
330 static ConstString g_item_name;
332 g_item_name.SetCString("__i");
335 return (new VectorIteratorSyntheticFrontEnd(valobj_sp,g_item_name));
338 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::LibcxxSharedPtrSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
339 SyntheticChildrenFrontEnd(*valobj_sp.get()),
344 m_byte_order(lldb::eByteOrderInvalid)
351 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::CalculateNumChildren ()
353 return (m_cntrl ? 1 : 0);
357 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetChildAtIndex (size_t idx)
360 return lldb::ValueObjectSP();
362 ValueObjectSP valobj_sp = m_backend.GetSP();
364 return lldb::ValueObjectSP();
367 return valobj_sp->GetChildMemberWithName(ConstString("__ptr_"), true);
370 return lldb::ValueObjectSP();
376 ValueObjectSP shared_owners_sp(m_cntrl->GetChildMemberWithName(ConstString("__shared_owners_"),true));
377 if (!shared_owners_sp)
378 return lldb::ValueObjectSP();
379 uint64_t count = 1 + shared_owners_sp->GetValueAsUnsigned(0);
380 DataExtractor data(&count, 8, m_byte_order, m_ptr_size);
381 m_count_sp = CreateValueObjectFromData("count", data, valobj_sp->GetExecutionContextRef(), shared_owners_sp->GetClangType());
385 else /* if (idx == 2) */
387 if (!m_weak_count_sp)
389 ValueObjectSP shared_weak_owners_sp(m_cntrl->GetChildMemberWithName(ConstString("__shared_weak_owners_"),true));
390 if (!shared_weak_owners_sp)
391 return lldb::ValueObjectSP();
392 uint64_t count = 1 + shared_weak_owners_sp->GetValueAsUnsigned(0);
393 DataExtractor data(&count, 8, m_byte_order, m_ptr_size);
394 m_weak_count_sp = CreateValueObjectFromData("count", data, valobj_sp->GetExecutionContextRef(), shared_weak_owners_sp->GetClangType());
396 return m_weak_count_sp;
401 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::Update()
404 m_weak_count_sp.reset();
407 ValueObjectSP valobj_sp = m_backend.GetSP();
411 TargetSP target_sp(valobj_sp->GetTargetSP());
415 m_byte_order = target_sp->GetArchitecture().GetByteOrder();
416 m_ptr_size = target_sp->GetArchitecture().GetAddressByteSize();
418 lldb::ValueObjectSP cntrl_sp(valobj_sp->GetChildMemberWithName(ConstString("__cntrl_"),true));
420 m_cntrl = cntrl_sp.get(); // need to store the raw pointer to avoid a circular dependency
425 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::MightHaveChildren ()
431 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
433 if (name == ConstString("__ptr_"))
435 if (name == ConstString("count"))
437 if (name == ConstString("weak_count"))
442 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEnd::~LibcxxSharedPtrSyntheticFrontEnd ()
445 SyntheticChildrenFrontEnd*
446 lldb_private::formatters::LibcxxSharedPtrSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
450 return (new LibcxxSharedPtrSyntheticFrontEnd(valobj_sp));
454 lldb_private::formatters::LibcxxContainerSummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
456 if (valobj.IsPointerType())
458 uint64_t value = valobj.GetValueAsUnsigned(0);
461 stream.Printf("0x%016" PRIx64 " ", value);
463 return Debugger::FormatPrompt("size=${svar%#}", NULL, NULL, NULL, stream, &valobj);