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 TemplateArgumentKind kind;
121 CompilerType pair_type = my_type.GetTemplateArgument(0, kind);
122 if (kind != eTemplateArgumentKindType &&
123 kind != eTemplateArgumentKindTemplate &&
124 kind != eTemplateArgumentKindTemplateExpansion)
126 m_pair_type = pair_type;
133 size_t LibstdcppMapIteratorSyntheticFrontEnd::CalculateNumChildren() {
138 LibstdcppMapIteratorSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
139 if (m_pair_address != 0 && m_pair_type) {
141 m_pair_sp = CreateValueObjectFromAddress("pair", m_pair_address,
142 m_exe_ctx_ref, m_pair_type);
144 return m_pair_sp->GetChildAtIndex(idx, true);
146 return lldb::ValueObjectSP();
149 bool LibstdcppMapIteratorSyntheticFrontEnd::MightHaveChildren() { return true; }
151 size_t LibstdcppMapIteratorSyntheticFrontEnd::GetIndexOfChildWithName(
152 const ConstString &name) {
153 if (name == ConstString("first"))
155 if (name == ConstString("second"))
160 SyntheticChildrenFrontEnd *
161 lldb_private::formatters::LibstdcppMapIteratorSyntheticFrontEndCreator(
162 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
163 return (valobj_sp ? new LibstdcppMapIteratorSyntheticFrontEnd(valobj_sp)
168 (lldb) fr var ibeg --ptr-depth 1
169 (__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >)
171 _M_current = 0x00000001001037a0 {
177 SyntheticChildrenFrontEnd *
178 lldb_private::formatters::LibStdcppVectorIteratorSyntheticFrontEndCreator(
179 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
180 static ConstString g_item_name;
182 g_item_name.SetCString("_M_current");
184 ? new VectorIteratorSyntheticFrontEnd(valobj_sp, g_item_name)
188 lldb_private::formatters::VectorIteratorSyntheticFrontEnd::
189 VectorIteratorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp,
190 ConstString item_name)
191 : SyntheticChildrenFrontEnd(*valobj_sp), m_exe_ctx_ref(),
192 m_item_name(item_name), m_item_sp() {
197 bool VectorIteratorSyntheticFrontEnd::Update() {
200 ValueObjectSP valobj_sp = m_backend.GetSP();
207 ValueObjectSP item_ptr(valobj_sp->GetChildMemberWithName(m_item_name, true));
210 if (item_ptr->GetValueAsUnsigned(0) == 0)
213 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
214 m_item_sp = CreateValueObjectFromAddress(
215 "item", item_ptr->GetValueAsUnsigned(0), m_exe_ctx_ref,
216 item_ptr->GetCompilerType().GetPointeeType());
222 size_t VectorIteratorSyntheticFrontEnd::CalculateNumChildren() { return 1; }
225 VectorIteratorSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
228 return lldb::ValueObjectSP();
231 bool VectorIteratorSyntheticFrontEnd::MightHaveChildren() { return true; }
233 size_t VectorIteratorSyntheticFrontEnd::GetIndexOfChildWithName(
234 const ConstString &name) {
235 if (name == ConstString("item"))
240 bool lldb_private::formatters::LibStdcppStringSummaryProvider(
241 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
242 const bool scalar_is_load_addr = true;
243 AddressType addr_type;
244 lldb::addr_t addr_of_string =
245 valobj.GetAddressOf(scalar_is_load_addr, &addr_type);
246 if (addr_of_string != LLDB_INVALID_ADDRESS) {
248 case eAddressTypeLoad: {
249 ProcessSP process_sp(valobj.GetProcessSP());
253 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
255 lldb::addr_t addr_of_data =
256 process_sp->ReadPointerFromMemory(addr_of_string, error);
257 if (error.Fail() || addr_of_data == 0 ||
258 addr_of_data == LLDB_INVALID_ADDRESS)
260 options.SetLocation(addr_of_data);
261 options.SetProcessSP(process_sp);
262 options.SetStream(&stream);
263 options.SetNeedsZeroTermination(false);
264 options.SetBinaryZeroIsTerminator(true);
265 lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
266 addr_of_string + process_sp->GetAddressByteSize(), error);
269 options.SetSourceSize(size_of_data);
271 if (!StringPrinter::ReadStringAndDumpToStream<
272 StringPrinter::StringElementType::UTF8>(options)) {
273 stream.Printf("Summary Unavailable");
278 case eAddressTypeHost:
280 case eAddressTypeInvalid:
281 case eAddressTypeFile:
288 bool lldb_private::formatters::LibStdcppWStringSummaryProvider(
289 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
290 const bool scalar_is_load_addr = true;
291 AddressType addr_type;
292 lldb::addr_t addr_of_string =
293 valobj.GetAddressOf(scalar_is_load_addr, &addr_type);
294 if (addr_of_string != LLDB_INVALID_ADDRESS) {
296 case eAddressTypeLoad: {
297 ProcessSP process_sp(valobj.GetProcessSP());
301 CompilerType wchar_compiler_type =
302 valobj.GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeWChar);
304 if (!wchar_compiler_type)
307 const uint32_t wchar_size = wchar_compiler_type.GetBitSize(
308 nullptr); // Safe to pass NULL for exe_scope here
310 StringPrinter::ReadStringAndDumpToStreamOptions options(valobj);
312 lldb::addr_t addr_of_data =
313 process_sp->ReadPointerFromMemory(addr_of_string, error);
314 if (error.Fail() || addr_of_data == 0 ||
315 addr_of_data == LLDB_INVALID_ADDRESS)
317 options.SetLocation(addr_of_data);
318 options.SetProcessSP(process_sp);
319 options.SetStream(&stream);
320 options.SetNeedsZeroTermination(false);
321 options.SetBinaryZeroIsTerminator(false);
322 lldb::addr_t size_of_data = process_sp->ReadPointerFromMemory(
323 addr_of_string + process_sp->GetAddressByteSize(), error);
326 options.SetSourceSize(size_of_data);
327 options.SetPrefixToken("L");
329 switch (wchar_size) {
331 return StringPrinter::ReadStringAndDumpToStream<
332 StringPrinter::StringElementType::UTF8>(options);
334 return StringPrinter::ReadStringAndDumpToStream<
335 StringPrinter::StringElementType::UTF16>(options);
337 return StringPrinter::ReadStringAndDumpToStream<
338 StringPrinter::StringElementType::UTF32>(options);
340 stream.Printf("size for wchar_t is not valid");
345 case eAddressTypeHost:
347 case eAddressTypeInvalid:
348 case eAddressTypeFile:
355 LibStdcppSharedPtrSyntheticFrontEnd::LibStdcppSharedPtrSyntheticFrontEnd(
356 lldb::ValueObjectSP valobj_sp)
357 : SyntheticChildrenFrontEnd(*valobj_sp) {
362 size_t LibStdcppSharedPtrSyntheticFrontEnd::CalculateNumChildren() { return 1; }
365 LibStdcppSharedPtrSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
366 ValueObjectSP valobj_sp = m_backend.GetSP();
368 return lldb::ValueObjectSP();
371 return valobj_sp->GetChildMemberWithName(ConstString("_M_ptr"), true);
373 return lldb::ValueObjectSP();
376 bool LibStdcppSharedPtrSyntheticFrontEnd::Update() { return false; }
378 bool LibStdcppSharedPtrSyntheticFrontEnd::MightHaveChildren() { return true; }
380 size_t LibStdcppSharedPtrSyntheticFrontEnd::GetIndexOfChildWithName(
381 const ConstString &name) {
382 if (name == ConstString("_M_ptr"))
387 SyntheticChildrenFrontEnd *
388 lldb_private::formatters::LibStdcppSharedPtrSyntheticFrontEndCreator(
389 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
390 return (valobj_sp ? new LibStdcppSharedPtrSyntheticFrontEnd(valobj_sp)
394 bool lldb_private::formatters::LibStdcppSmartPointerSummaryProvider(
395 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
396 ValueObjectSP valobj_sp(valobj.GetNonSyntheticValue());
400 ValueObjectSP ptr_sp(
401 valobj_sp->GetChildMemberWithName(ConstString("_M_ptr"), true));
405 ValueObjectSP usecount_sp(valobj_sp->GetChildAtNamePath(
406 {ConstString("_M_refcount"), ConstString("_M_pi"),
407 ConstString("_M_use_count")}));
411 if (ptr_sp->GetValueAsUnsigned(0) == 0 ||
412 usecount_sp->GetValueAsUnsigned(0) == 0) {
413 stream.Printf("nullptr");
418 ValueObjectSP pointee_sp = ptr_sp->Dereference(error);
419 if (pointee_sp && error.Success()) {
420 if (pointee_sp->DumpPrintableRepresentation(
421 stream, ValueObject::eValueObjectRepresentationStyleSummary,
422 lldb::eFormatInvalid,
423 ValueObject::PrintableRepresentationSpecialCases::eDisable,
429 stream.Printf("ptr = 0x%" PRIx64, ptr_sp->GetValueAsUnsigned(0));