LLDB mainline
LibStdcppUniquePointer.cpp
Go to the documentation of this file.
1//===-- LibStdcppUniquePointer.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 "LibStdcpp.h"
10
15
16#include <memory>
17#include <vector>
18
19using namespace lldb;
20using namespace lldb_private;
21using namespace lldb_private::formatters;
22
23namespace {
24
25class LibStdcppUniquePtrSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
26public:
27 explicit LibStdcppUniquePtrSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
28
29 size_t CalculateNumChildren() override;
30
31 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
32
33 bool Update() override;
34
35 bool MightHaveChildren() override;
36
37 size_t GetIndexOfChildWithName(ConstString name) override;
38
39 bool GetSummary(Stream &stream, const TypeSummaryOptions &options);
40
41private:
42 // The lifetime of a ValueObject and all its derivative ValueObjects
43 // (children, clones, etc.) is managed by a ClusterManager. These
44 // objects are only destroyed when every shared pointer to any of them
45 // is destroyed, so we must not store a shared pointer to any ValueObject
46 // derived from our backend ValueObject (since we're in the same cluster).
47 ValueObject* m_ptr_obj = nullptr;
48 ValueObject* m_obj_obj = nullptr;
49 ValueObject* m_del_obj = nullptr;
50
51 ValueObjectSP GetTuple();
52};
53
54} // end of anonymous namespace
55
56LibStdcppUniquePtrSyntheticFrontEnd::LibStdcppUniquePtrSyntheticFrontEnd(
57 lldb::ValueObjectSP valobj_sp)
58 : SyntheticChildrenFrontEnd(*valobj_sp) {
59 Update();
60}
61
62ValueObjectSP LibStdcppUniquePtrSyntheticFrontEnd::GetTuple() {
63 ValueObjectSP valobj_backend_sp = m_backend.GetSP();
64
65 if (!valobj_backend_sp)
66 return nullptr;
67
68 ValueObjectSP valobj_sp = valobj_backend_sp->GetNonSyntheticValue();
69 if (!valobj_sp)
70 return nullptr;
71
72 ValueObjectSP obj_child_sp = valobj_sp->GetChildMemberWithName("_M_t", true);
73 if (!obj_child_sp)
74 return nullptr;
75
76 ValueObjectSP obj_subchild_sp =
77 obj_child_sp->GetChildMemberWithName("_M_t", true);
78
79 // if there is a _M_t subchild, the tuple is found in the obj_subchild_sp
80 // (for libstdc++ 6.0.23).
81 if (obj_subchild_sp) {
82 return obj_subchild_sp;
83 }
84
85 return obj_child_sp;
86}
87
88bool LibStdcppUniquePtrSyntheticFrontEnd::Update() {
89 ValueObjectSP tuple_sp = GetTuple();
90
91 if (!tuple_sp)
92 return false;
93
94 std::unique_ptr<SyntheticChildrenFrontEnd> tuple_frontend(
96
97 ValueObjectSP ptr_obj = tuple_frontend->GetChildAtIndex(0);
98 if (ptr_obj)
99 m_ptr_obj = ptr_obj->Clone(ConstString("pointer")).get();
100
101 // Add a 'deleter' child if there was a non-empty deleter type specified.
102 //
103 // The object might have size=1 in the TypeSystem but occupies no dedicated
104 // storage due to no_unique_address, so infer the actual size from the total
105 // size of the unique_ptr class. If sizeof(unique_ptr) == sizeof(void*) then
106 // the deleter is empty and should be hidden.
107 if (tuple_sp->GetByteSize() > ptr_obj->GetByteSize()) {
108 ValueObjectSP del_obj = tuple_frontend->GetChildAtIndex(1);
109 if (del_obj)
110 m_del_obj = del_obj->Clone(ConstString("deleter")).get();
111 }
112
113 if (m_ptr_obj) {
115 ValueObjectSP obj_obj = m_ptr_obj->Dereference(error);
116 if (error.Success()) {
117 m_obj_obj = obj_obj->Clone(ConstString("object")).get();
118 }
119 }
120
121 return false;
122}
123
124bool LibStdcppUniquePtrSyntheticFrontEnd::MightHaveChildren() { return true; }
125
126lldb::ValueObjectSP
127LibStdcppUniquePtrSyntheticFrontEnd::GetChildAtIndex(size_t idx) {
128 if (idx == 0 && m_ptr_obj)
129 return m_ptr_obj->GetSP();
130 if (idx == 1 && m_del_obj)
131 return m_del_obj->GetSP();
132 if (idx == 2 && m_obj_obj)
133 return m_obj_obj->GetSP();
134 return lldb::ValueObjectSP();
135}
136
137size_t LibStdcppUniquePtrSyntheticFrontEnd::CalculateNumChildren() {
138 if (m_del_obj)
139 return 2;
140 return 1;
141}
142
143size_t LibStdcppUniquePtrSyntheticFrontEnd::GetIndexOfChildWithName(
144 ConstString name) {
145 if (name == "ptr" || name == "pointer")
146 return 0;
147 if (name == "del" || name == "deleter")
148 return 1;
149 if (name == "obj" || name == "object" || name == "$$dereference$$")
150 return 2;
151 return UINT32_MAX;
152}
153
154bool LibStdcppUniquePtrSyntheticFrontEnd::GetSummary(
155 Stream &stream, const TypeSummaryOptions &options) {
156 if (!m_ptr_obj)
157 return false;
158
159 bool success;
160 uint64_t ptr_value = m_ptr_obj->GetValueAsUnsigned(0, &success);
161 if (!success)
162 return false;
163 if (ptr_value == 0)
164 stream.Printf("nullptr");
165 else
166 stream.Printf("0x%" PRIx64, ptr_value);
167 return true;
168}
169
172 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
173 return (valobj_sp ? new LibStdcppUniquePtrSyntheticFrontEnd(valobj_sp)
174 : nullptr);
175}
176
178 ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
179 LibStdcppUniquePtrSyntheticFrontEnd formatter(valobj.GetSP());
180 return formatter.GetSummary(stream, options);
181}
static llvm::raw_ostream & error(Stream &strm)
A uniqued constant string class.
Definition: ConstString.h:40
An error handling class.
Definition: Status.h:44
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
virtual size_t GetIndexOfChildWithName(ConstString name)=0
virtual lldb::ValueObjectSP GetChildAtIndex(size_t idx)=0
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:554
#define UINT32_MAX
Definition: lldb-defines.h:19
SyntheticChildrenFrontEnd * LibStdcppUniquePtrSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
SyntheticChildrenFrontEnd * LibStdcppTupleSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
bool LibStdcppUniquePointerSummaryProvider(ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15