1 //===-- LibStdcpp.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 "LibStdcpp.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/ValueObject.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/DataFormatters/StringPrinter.h"
19 #include "lldb/DataFormatters/VectorIterator.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/DataBufferHeap.h"
23 #include "lldb/Utility/Endian.h"
24 #include "lldb/Utility/Status.h"
25 #include "lldb/Utility/Stream.h"
28 using namespace lldb_private;
29 using namespace lldb_private::formatters;
33 class LibstdcppMapIteratorSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
35 (std::_Rb_tree_iterator<std::pair<const int, std::basic_string<char,
36 std::char_traits<char>, std::allocator<char> > > >) ibeg = {
37 (_Base_ptr) _M_node = 0x0000000100103910 {
38 (std::_Rb_tree_color) _M_color = _S_black
39 (std::_Rb_tree_node_base::_Base_ptr) _M_parent = 0x00000001001038c0
40 (std::_Rb_tree_node_base::_Base_ptr) _M_left = 0x0000000000000000
41 (std::_Rb_tree_node_base::_Base_ptr) _M_right = 0x0000000000000000
47 explicit LibstdcppMapIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
49 size_t CalculateNumChildren() override;
51 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
53 bool Update() override;
55 bool MightHaveChildren() override;
57 size_t GetIndexOfChildWithName(const ConstString &name) override;
60 ExecutionContextRef m_exe_ctx_ref;
61 lldb::addr_t m_pair_address;
62 CompilerType m_pair_type;
63 lldb::ValueObjectSP m_pair_sp;
66 class LibStdcppSharedPtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
68 explicit LibStdcppSharedPtrSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
70 size_t CalculateNumChildren() override;
72 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
74 bool Update() override;
76 bool MightHaveChildren() override;
78 size_t GetIndexOfChildWithName(const ConstString &name) override;
81 } // end of anonymous namespace
83 LibstdcppMapIteratorSyntheticFrontEnd::LibstdcppMapIteratorSyntheticFrontEnd(
84 lldb::ValueObjectSP valobj_sp)
85 : SyntheticChildrenFrontEnd(*valobj_sp), m_exe_ctx_ref(), m_pair_address(0),
86 m_pair_type(), m_pair_sp() {
91 bool LibstdcppMapIteratorSyntheticFrontEnd::Update() {
92 ValueObjectSP valobj_sp = m_backend.GetSP();
96 TargetSP target_sp(valobj_sp->GetTargetSP());
101 bool is_64bit = (target_sp->GetArchitecture().GetAddressByteSize() == 8);
105 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
107 ValueObjectSP _M_node_sp(
108 valobj_sp->GetChildMemberWithName(ConstString("_M_node"), true));
112 m_pair_address = _M_node_sp->GetValueAsUnsigned(0);
113 if (m_pair_address == 0)
116 m_pair_address += (is_64bit ? 32 : 16);
118 CompilerType my_type(valobj_sp->GetCompilerType());
119 if (my_type.GetNumTemplateArguments() >= 1) {
120 CompilerType pair_type = my_type.GetTypeTemplateArgument(0);
123 m_pair_type = pair_type;
130 size_t LibstdcppMapIteratorSyntheticFrontEnd::CalculateNumChildren() {
135 LibstdcppMapIteratorSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
136 if (m_pair_address != 0 && m_pair_type) {
138 m_pair_sp = CreateValueObjectFromAddress("pair", m_pair_address,
139 m_exe_ctx_ref, m_pair_type);
141 return m_pair_sp->GetChildAtIndex(idx, true);
143 return lldb::ValueObjectSP();
146 bool LibstdcppMapIteratorSyntheticFrontEnd::MightHaveChildren() { return true; }
148 size_t LibstdcppMapIteratorSyntheticFrontEnd::GetIndexOfChildWithName(
149 const ConstString &name) {
150 if (name == ConstString("first"))
152 if (name == ConstString("second"))
157 SyntheticChildrenFrontEnd *
158 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEndCreator(
159 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
160 return (valobj_sp ? new LibstdcppMapIteratorSyntheticFrontEnd(valobj_sp)
165 (lldb) fr var ibeg --ptr-depth 1
166 (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >)
168 _M_current = 0x00000001001037a0 {
174 SyntheticChildrenFrontEnd *
175 lldb_private::formatters::LibStdcppVectorIteratorSyntheticFrontEndCreator(
176 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
177 static ConstString g_item_name;
179 g_item_name.SetCString("_M_current");
181 ? new VectorIteratorSyntheticFrontEnd(valobj_sp, g_item_name)
185 lldb_private::formatters::VectorIteratorSyntheticFrontEnd::
186 VectorIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp,
187 ConstString item_name)
188 : SyntheticChildrenFrontEnd(*valobj_sp), m_exe_ctx_ref(),
189 m_item_name(item_name), m_item_sp() {
194 bool VectorIteratorSyntheticFrontEnd::Update() {
197 ValueObjectSP valobj_sp = m_backend.GetSP();
204 ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name, true));
207 if (item_ptr->GetValueAsUnsigned(0) == 0)
210 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
211 m_item_sp = CreateValueObjectFromAddress(
212 "item", item_ptr->GetValueAsUnsigned(0), m_exe_ctx_ref,
213 item_ptr->GetCompilerType().GetPointeeType());
219 size_t VectorIteratorSyntheticFrontEnd::CalculateNumChildren() { return 1; }
222 VectorIteratorSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
225 return lldb::ValueObjectSP();
228 bool VectorIteratorSyntheticFrontEnd::MightHaveChildren() { return true; }
230 size_t VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName(
231 const ConstString &name) {
232 if (name == ConstString("item"))
237 bool lldb_private::formatters::LibStdcppStringSummaryProvider(
238 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
239 const bool scalar_is_load_addr = true;
240 AddressType addr_type;
241 lldb::addr_t addr_of_string =
242 valobj.GetAddressOf(scalar_is_load_addr, &addr_type);
243 if (addr_of_string != LLDB_INVALID_ADDRESS) {
245 case eAddressTypeLoad: {
246 ProcessSP process_sp(valobj.GetProcessSP());
250 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
252 lldb::addr_t addr_of_data =
253 process_sp->ReadPointerFromMemory(addr_of_string, error);
254 if (error.Fail() || addr_of_data == 0 ||
255 addr_of_data == LLDB_INVALID_ADDRESS)
257 options.SetLocation(addr_of_data);
258 options.SetProcessSP(process_sp);
259 options.SetStream(&stream);
260 options.SetNeedsZeroTermination(false);
261 options.SetBinaryZeroIsTerminator(true);
262 lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
263 addr_of_string + process_sp->GetAddressByteSize(), error);
266 options.SetSourceSize(size_of_data);
268 if (!StringPrinter::ReadStringAndDumpToStream<
269 StringPrinter::StringElementType::UTF8>(options)) {
270 stream.Printf("Summary Unavailable");
275 case eAddressTypeHost:
277 case eAddressTypeInvalid:
278 case eAddressTypeFile:
285 bool lldb_private::formatters::LibStdcppWStringSummaryProvider(
286 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
287 const bool scalar_is_load_addr = true;
288 AddressType addr_type;
289 lldb::addr_t addr_of_string =
290 valobj.GetAddressOf(scalar_is_load_addr, &addr_type);
291 if (addr_of_string != LLDB_INVALID_ADDRESS) {
293 case eAddressTypeLoad: {
294 ProcessSP process_sp(valobj.GetProcessSP());
298 CompilerType wchar_compiler_type =
299 valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);
301 if (!wchar_compiler_type)
304 const uint32_t wchar_size = wchar_compiler_type.GetBitSize(
305 nullptr); // Safe to pass NULL for exe_scope here
307 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
309 lldb::addr_t addr_of_data =
310 process_sp->ReadPointerFromMemory(addr_of_string, error);
311 if (error.Fail() || addr_of_data == 0 ||
312 addr_of_data == LLDB_INVALID_ADDRESS)
314 options.SetLocation(addr_of_data);
315 options.SetProcessSP(process_sp);
316 options.SetStream(&stream);
317 options.SetNeedsZeroTermination(false);
318 options.SetBinaryZeroIsTerminator(false);
319 lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
320 addr_of_string + process_sp->GetAddressByteSize(), error);
323 options.SetSourceSize(size_of_data);
324 options.SetPrefixToken("L");
326 switch (wchar_size) {
328 return StringPrinter::ReadStringAndDumpToStream<
329 StringPrinter::StringElementType::UTF8>(options);
331 return StringPrinter::ReadStringAndDumpToStream<
332 StringPrinter::StringElementType::UTF16>(options);
334 return StringPrinter::ReadStringAndDumpToStream<
335 StringPrinter::StringElementType::UTF32>(options);
337 stream.Printf("size for wchar_t is not valid");
342 case eAddressTypeHost:
344 case eAddressTypeInvalid:
345 case eAddressTypeFile:
352 LibStdcppSharedPtrSyntheticFrontEnd::LibStdcppSharedPtrSyntheticFrontEnd(
353 lldb::ValueObjectSP valobj_sp)
354 : SyntheticChildrenFrontEnd(*valobj_sp) {
359 size_t LibStdcppSharedPtrSyntheticFrontEnd::CalculateNumChildren() { return 1; }
362 LibStdcppSharedPtrSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
363 ValueObjectSP valobj_sp = m_backend.GetSP();
365 return lldb::ValueObjectSP();
368 return valobj_sp->GetChildMemberWithName(ConstString("_M_ptr"), true);
370 return lldb::ValueObjectSP();
373 bool LibStdcppSharedPtrSyntheticFrontEnd::Update() { return false; }
375 bool LibStdcppSharedPtrSyntheticFrontEnd::MightHaveChildren() { return true; }
377 size_t LibStdcppSharedPtrSyntheticFrontEnd::GetIndexOfChildWithName(
378 const ConstString &name) {
379 if (name == ConstString("_M_ptr"))
384 SyntheticChildrenFrontEnd *
385 lldb_private::formatters::LibStdcppSharedPtrSyntheticFrontEndCreator(
386 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
387 return (valobj_sp ? new LibStdcppSharedPtrSyntheticFrontEnd(valobj_sp)
391 bool lldb_private::formatters::LibStdcppSmartPointerSummaryProvider(
392 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
393 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
397 ValueObjectSP ptr_sp(
398 valobj_sp->GetChildMemberWithName(ConstString("_M_ptr"), true));
402 ValueObjectSP usecount_sp(valobj_sp->GetChildAtNamePath(
403 {ConstString("_M_refcount"), ConstString("_M_pi"),
404 ConstString("_M_use_count")}));
408 if (ptr_sp->GetValueAsUnsigned(0) == 0 ||
409 usecount_sp->GetValueAsUnsigned(0) == 0) {
410 stream.Printf("nullptr");
415 ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
416 if (pointee_sp && error.Success()) {
417 if (pointee_sp->DumpPrintableRepresentation(
418 stream, ValueObject::eValueObjectRepresentationStyleSummary,
419 lldb::eFormatInvalid,
420 ValueObject::PrintableRepresentationSpecialCases::eDisable,
426 stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));