blob: 6fbd5fd1777c70735b8696bab5ae29d5d1013143 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBValue.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
Eli Friedman7a62c8b2010-06-09 07:44:37 +000010#include "lldb/API/SBValue.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000011#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000012
13#include "lldb/Core/DataExtractor.h"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/Stream.h"
16#include "lldb/Core/StreamFile.h"
17#include "lldb/Core/Value.h"
18#include "lldb/Core/ValueObject.h"
19#include "lldb/Symbol/Block.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Symbol/Variable.h"
22#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/Process.h"
24#include "lldb/Target/StackFrame.h"
25#include "lldb/Target/Thread.h"
26
Eli Friedman7a62c8b2010-06-09 07:44:37 +000027#include "lldb/API/SBProcess.h"
28#include "lldb/API/SBTarget.h"
29#include "lldb/API/SBThread.h"
30#include "lldb/API/SBFrame.h"
31#include "lldb/API/SBDebugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36SBValue::SBValue () :
Greg Clayton63094e02010-06-23 01:19:29 +000037 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000038{
39}
40
41SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000042 m_opaque_sp (value_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000043{
44}
45
46SBValue::~SBValue()
47{
48}
49
50bool
51SBValue::IsValid () const
52{
Greg Clayton63094e02010-06-23 01:19:29 +000053 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
Greg Claytonc5f728c2010-10-06 22:10:17 +000056SBError
57SBValue::GetError()
58{
59 SBError sb_error;
60
61 if (m_opaque_sp.get())
62 sb_error.SetError(m_opaque_sp->GetError());
63
64 return sb_error;
65}
66
Chris Lattner24943d22010-06-08 16:52:24 +000067const char *
68SBValue::GetName()
69{
70 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000071 return m_opaque_sp->GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000072 else
73 return NULL;
74}
75
76const char *
77SBValue::GetTypeName ()
78{
79 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000080 return m_opaque_sp->GetTypeName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000081 else
82 return NULL;
83}
84
85size_t
86SBValue::GetByteSize ()
87{
88 size_t result = 0;
89
90 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000091 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +000092
93 return result;
94}
95
96bool
97SBValue::IsInScope (const SBFrame &frame)
98{
99 bool result = false;
100
101 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000102 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000103
104 return result;
105}
106
107const char *
108SBValue::GetValue (const SBFrame &frame)
109{
110 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000111 if ( m_opaque_sp)
Greg Clayton17dae082010-09-02 02:59:18 +0000112 value_string = m_opaque_sp->GetValueAsCString (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000113 return value_string;
114}
115
Jim Ingham4ae51962010-09-10 23:12:17 +0000116const char *
117SBValue::GetObjectDescription (const SBFrame &frame)
118{
119 const char *value_string = NULL;
120 if ( m_opaque_sp)
121 value_string = m_opaque_sp->GetObjectDescription (frame.get());
122 return value_string;
123}
124
Chris Lattner24943d22010-06-08 16:52:24 +0000125bool
Greg Clayton17dae082010-09-02 02:59:18 +0000126SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000127{
128 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000129 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000130 return false;
131}
132
133const char *
134SBValue::GetSummary (const SBFrame &frame)
135{
136 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000137 if ( m_opaque_sp)
138 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000139 return value_string;
140}
141
142const char *
143SBValue::GetLocation (const SBFrame &frame)
144{
145 const char *value_string = NULL;
146 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000147 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000148 return value_string;
149}
150
151bool
152SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
153{
154 bool success = false;
155 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000156 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000157 return success;
158}
159
160SBValue
161SBValue::GetChildAtIndex (uint32_t idx)
162{
163 lldb::ValueObjectSP child_sp;
164
165 if (IsValid())
166 {
Greg Clayton63094e02010-06-23 01:19:29 +0000167 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000168 }
169
170 SBValue sb_value (child_sp);
171 return sb_value;
172}
173
174uint32_t
175SBValue::GetIndexOfChildWithName (const char *name)
176{
177 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000178 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000179 return UINT32_MAX;
180}
181
182SBValue
183SBValue::GetChildMemberWithName (const char *name)
184{
185 lldb::ValueObjectSP child_sp;
186 const ConstString str_name (name);
187
188 if (IsValid())
189 {
Greg Clayton63094e02010-06-23 01:19:29 +0000190 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000191 }
192
193 SBValue sb_value (child_sp);
194 return sb_value;
195}
196
197
198uint32_t
199SBValue::GetNumChildren ()
200{
201 uint32_t num_children = 0;
202
203 if (IsValid())
204 {
Greg Clayton63094e02010-06-23 01:19:29 +0000205 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000206 }
207
208 return num_children;
209}
210
211bool
212SBValue::ValueIsStale ()
213{
214 bool result = true;
215
216 if (IsValid())
217 {
Greg Clayton63094e02010-06-23 01:19:29 +0000218 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000219 }
220
221 return result;
222}
223
224
225SBValue
226SBValue::Dereference ()
227{
228 if (IsValid())
229 {
Greg Clayton63094e02010-06-23 01:19:29 +0000230 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000231 {
232 return GetChildAtIndex(0);
233 }
234 }
235 return *this;
236}
237
238bool
239SBValue::TypeIsPtrType ()
240{
241 bool is_ptr_type = false;
242
243 if (IsValid())
244 {
Greg Clayton63094e02010-06-23 01:19:29 +0000245 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000246 }
247
248 return is_ptr_type;
249}
250
Chris Lattner24943d22010-06-08 16:52:24 +0000251void *
252SBValue::GetOpaqueType()
253{
Greg Clayton63094e02010-06-23 01:19:29 +0000254 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000255 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000256 return NULL;
257}
258
259// Mimic shared pointer...
260lldb_private::ValueObject *
261SBValue::get() const
262{
Greg Clayton63094e02010-06-23 01:19:29 +0000263 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000264}
265
266lldb_private::ValueObject *
267SBValue::operator->() const
268{
Greg Clayton63094e02010-06-23 01:19:29 +0000269 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000270}
271
272lldb::ValueObjectSP &
273SBValue::operator*()
274{
Greg Clayton63094e02010-06-23 01:19:29 +0000275 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000276}
277
278const lldb::ValueObjectSP &
279SBValue::operator*() const
280{
Greg Clayton63094e02010-06-23 01:19:29 +0000281 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000282}
Caroline Tice98f930f2010-09-20 05:20:02 +0000283
284bool
285SBValue::GetDescription (SBStream &description)
286{
287 if (m_opaque_sp)
288 {
289 const char *name = GetName();
290 const char *type_name = GetTypeName ();
291 size_t byte_size = GetByteSize ();
292 uint32_t num_children = GetNumChildren ();
293 bool is_stale = ValueIsStale ();
294 description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
295 (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
296 if (num_children > 0)
297 description.Printf (", num_children: %d", num_children);
298
299 if (is_stale)
300 description.Printf (" [value is stale]");
301 }
302 else
303 description.Printf ("No value");
304
305 return true;
306}