blob: c1e45e1f48de8c6d0ce62488c15d8eabf526b4f9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ValueObjectChild.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/Core/ValueObjectChild.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/ValueObjectList.h"
14
Greg Claytone1a916a2010-07-21 22:12:05 +000015#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Symbol/ObjectFile.h"
17#include "lldb/Symbol/SymbolContext.h"
18#include "lldb/Symbol/Type.h"
19#include "lldb/Symbol/Variable.h"
20
21#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/Target.h"
24
25using namespace lldb_private;
26
27ValueObjectChild::ValueObjectChild
28(
Jim Ingham6035b672011-03-31 00:19:25 +000029 ValueObject &parent,
Greg Clayton57ee3062013-07-11 22:46:58 +000030 const ClangASTType &clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031 const ConstString &name,
Greg Claytonfaac1112013-03-14 18:31:44 +000032 uint64_t byte_size,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 int32_t byte_offset,
34 uint32_t bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +000035 uint32_t bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +000036 bool is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +000037 bool is_deref_of_parent,
38 AddressType child_ptr_or_ref_addr_type
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039) :
Greg Clayton8f92f0a2010-10-14 22:52:14 +000040 ValueObject (parent),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 m_clang_type (clang_type),
42 m_byte_size (byte_size),
43 m_byte_offset (byte_offset),
44 m_bitfield_bit_size (bitfield_bit_size),
Greg Clayton8f92f0a2010-10-14 22:52:14 +000045 m_bitfield_bit_offset (bitfield_bit_offset),
Greg Claytone221f822011-01-21 01:59:00 +000046 m_is_base_class (is_base_class),
47 m_is_deref_of_parent (is_deref_of_parent)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048{
49 m_name = name;
Enrico Granata9128ee22011-09-06 19:20:51 +000050 SetAddressTypeOfChildren(child_ptr_or_ref_addr_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
53ValueObjectChild::~ValueObjectChild()
54{
55}
56
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057lldb::ValueType
58ValueObjectChild::GetValueType() const
59{
60 return m_parent->GetValueType();
61}
62
Greg Claytonc7bece562013-01-25 18:06:21 +000063size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064ValueObjectChild::CalculateNumChildren()
65{
Greg Clayton57ee3062013-07-11 22:46:58 +000066 return GetClangType().GetNumChildren (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067}
68
Enrico Granatae8daa2f2014-05-17 19:14:17 +000069static void
70AdjustForBitfieldness(ConstString& name,
71 uint8_t bitfield_bit_size)
72{
73 if (name && bitfield_bit_size)
74 {
75 const char *clang_type_name = name.AsCString();
76 if (clang_type_name)
77 {
78 std::vector<char> bitfield_type_name (strlen(clang_type_name) + 32, 0);
79 ::snprintf (&bitfield_type_name.front(), bitfield_type_name.size(), "%s:%u", clang_type_name, bitfield_bit_size);
80 name.SetCString(&bitfield_type_name.front());
81 }
82 }
83}
84
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085ConstString
86ValueObjectChild::GetTypeName()
87{
88 if (m_type_name.IsEmpty())
89 {
Greg Clayton57ee3062013-07-11 22:46:58 +000090 m_type_name = GetClangType().GetConstTypeName ();
Enrico Granatae8daa2f2014-05-17 19:14:17 +000091 AdjustForBitfieldness(m_type_name, m_bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 }
93 return m_type_name;
94}
95
Greg Clayton84db9102012-03-26 23:03:23 +000096ConstString
97ValueObjectChild::GetQualifiedTypeName()
98{
Greg Clayton57ee3062013-07-11 22:46:58 +000099 ConstString qualified_name = GetClangType().GetConstTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +0000100 AdjustForBitfieldness(qualified_name, m_bitfield_bit_size);
Greg Clayton84db9102012-03-26 23:03:23 +0000101 return qualified_name;
102}
103
Enrico Granatae8daa2f2014-05-17 19:14:17 +0000104ConstString
105ValueObjectChild::GetDisplayTypeName()
106{
107 ConstString display_name = GetClangType().GetDisplayTypeName();
108 AdjustForBitfieldness(display_name, m_bitfield_bit_size);
109 return display_name;
110}
111
Jim Ingham6035b672011-03-31 00:19:25 +0000112bool
Enrico Granata45185862015-05-19 18:53:13 +0000113ValueObjectChild::CanUpdateWithInvalidExecutionContext ()
114{
115 if (m_parent)
116 return m_parent->CanUpdateWithInvalidExecutionContext();
117 return this->ValueObject::CanUpdateWithInvalidExecutionContext();
118}
119
120bool
Jim Ingham6035b672011-03-31 00:19:25 +0000121ValueObjectChild::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122{
123 m_error.Clear();
124 SetValueIsValid (false);
125 ValueObject* parent = m_parent;
126 if (parent)
127 {
Enrico Granatac3e320a2011-08-02 17:27:39 +0000128 if (parent->UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000130 m_value.SetClangType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131
132 // Copy the parent scalar value and the scalar value type
133 m_value.GetScalar() = parent->GetValue().GetScalar();
134 Value::ValueType value_type = parent->GetValue().GetValueType();
135 m_value.SetValueType (value_type);
136
Greg Clayton57ee3062013-07-11 22:46:58 +0000137 if (parent->GetClangType().IsPointerOrReferenceType ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 {
Enrico Granata9128ee22011-09-06 19:20:51 +0000139 lldb::addr_t addr = parent->GetPointerValue ();
Greg Clayton6fdfc7e2011-08-16 00:44:29 +0000140 m_value.GetScalar() = addr;
Greg Clayton7c8a9662010-11-02 01:50:16 +0000141
142 if (addr == LLDB_INVALID_ADDRESS)
143 {
144 m_error.SetErrorString ("parent address is invalid.");
145 }
146 else if (addr == 0)
147 {
148 m_error.SetErrorString ("parent is NULL");
149 }
150 else
151 {
Greg Claytondf797c12010-11-02 21:21:20 +0000152 m_value.GetScalar() += m_byte_offset;
Enrico Granata9128ee22011-09-06 19:20:51 +0000153 AddressType addr_type = parent->GetAddressTypeOfChildren();
154
155 switch (addr_type)
156 {
157 case eAddressTypeFile:
Greg Claytoncc4d0142012-02-17 07:49:44 +0000158 {
159 lldb::ProcessSP process_sp (GetProcessSP());
160 if (process_sp && process_sp->IsAlive() == true)
161 m_value.SetValueType (Value::eValueTypeLoadAddress);
162 else
163 m_value.SetValueType(Value::eValueTypeFileAddress);
164 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000165 break;
166 case eAddressTypeLoad:
167 m_value.SetValueType (Value::eValueTypeLoadAddress);
168 break;
169 case eAddressTypeHost:
170 m_value.SetValueType(Value::eValueTypeHostAddress);
171 break;
172 case eAddressTypeInvalid:
Enrico Granata9128ee22011-09-06 19:20:51 +0000173 // TODO: does this make sense?
174 m_value.SetValueType(Value::eValueTypeScalar);
175 break;
176 }
Greg Clayton7c8a9662010-11-02 01:50:16 +0000177 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 }
179 else
180 {
181 switch (value_type)
182 {
183 case Value::eValueTypeLoadAddress:
184 case Value::eValueTypeFileAddress:
185 case Value::eValueTypeHostAddress:
186 {
187 lldb::addr_t addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton7c8a9662010-11-02 01:50:16 +0000188 if (addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 {
Greg Clayton7c8a9662010-11-02 01:50:16 +0000190 m_error.SetErrorString ("parent address is invalid.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 }
Greg Clayton7c8a9662010-11-02 01:50:16 +0000192 else if (addr == 0)
193 {
194 m_error.SetErrorString ("parent is NULL");
195 }
196 else
197 {
198 // Set this object's scalar value to the address of its
Jim Ingham16e0c682011-08-12 23:34:31 +0000199 // value by adding its byte offset to the parent address
Greg Clayton7c8a9662010-11-02 01:50:16 +0000200 m_value.GetScalar() += GetByteOffset();
201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 }
203 break;
204
205 case Value::eValueTypeScalar:
206 // TODO: What if this is a register value? Do we try and
207 // extract the child value from within the parent data?
208 // Probably...
209 default:
Greg Clayton86edbf42011-10-26 00:56:27 +0000210 m_error.SetErrorString ("parent has invalid value.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 break;
212 }
213 }
214
215 if (m_error.Success())
216 {
Greg Clayton44d93782014-01-27 23:43:24 +0000217 const bool thread_and_frame_only_if_stopped = true;
218 ExecutionContext exe_ctx (GetExecutionContextRef().Lock(thread_and_frame_only_if_stopped));
Enrico Granata622be232014-10-21 20:52:14 +0000219 if (GetClangType().GetTypeInfo() & lldb::eTypeHasValue)
Greg Clayton1e3be5b2014-01-23 22:55:05 +0000220 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
221 else
222 m_error.Clear(); // No value so nothing to read...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 }
224 }
225 else
226 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000227 m_error.SetErrorStringWithFormat("parent failed to evaluate: %s", parent->GetError().AsCString());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 }
229 }
230 else
231 {
232 m_error.SetErrorString("ValueObjectChild has a NULL parent ValueObject.");
233 }
Jim Ingham6035b672011-03-31 00:19:25 +0000234
235 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
238
239bool
Jim Ingham6035b672011-03-31 00:19:25 +0000240ValueObjectChild::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241{
Enrico Granata4873e522013-04-11 22:48:58 +0000242 ValueObject* root(GetRoot());
243 if (root)
244 return root->IsInScope ();
245 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}