blob: e5476ff277c96a3913bfc065e7898e7ab40caf66 [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"
Caroline Tice7826c882010-10-26 03:11:13 +000014#include "lldb/Core/Log.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/Module.h"
16#include "lldb/Core/Stream.h"
17#include "lldb/Core/StreamFile.h"
18#include "lldb/Core/Value.h"
19#include "lldb/Core/ValueObject.h"
20#include "lldb/Symbol/Block.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/Variable.h"
23#include "lldb/Target/ExecutionContext.h"
24#include "lldb/Target/Process.h"
25#include "lldb/Target/StackFrame.h"
26#include "lldb/Target/Thread.h"
27
Eli Friedman7a62c8b2010-06-09 07:44:37 +000028#include "lldb/API/SBProcess.h"
29#include "lldb/API/SBTarget.h"
30#include "lldb/API/SBThread.h"
31#include "lldb/API/SBFrame.h"
32#include "lldb/API/SBDebugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000033
34using namespace lldb;
35using namespace lldb_private;
36
37SBValue::SBValue () :
Greg Clayton63094e02010-06-23 01:19:29 +000038 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000039{
40}
41
42SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000043 m_opaque_sp (value_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000044{
Caroline Tice61ba7ec2010-10-26 23:49:36 +000045 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +000046
47 if (log)
48 {
49 SBStream sstr;
50 GetDescription (sstr);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000051 log->Printf ("SBValue::SBValue (%p) => (%s)", m_opaque_sp.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +000052 }
Chris Lattner24943d22010-06-08 16:52:24 +000053}
54
55SBValue::~SBValue()
56{
57}
58
59bool
60SBValue::IsValid () const
61{
Greg Clayton63094e02010-06-23 01:19:29 +000062 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000063}
64
Greg Claytonc5f728c2010-10-06 22:10:17 +000065SBError
66SBValue::GetError()
67{
68 SBError sb_error;
69
70 if (m_opaque_sp.get())
71 sb_error.SetError(m_opaque_sp->GetError());
72
73 return sb_error;
74}
75
Chris Lattner24943d22010-06-08 16:52:24 +000076const char *
77SBValue::GetName()
78{
Caroline Tice7826c882010-10-26 03:11:13 +000079 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
80
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000081 if (log)
82 log->Printf ("SBValue::GetName () ptr=%p => '%s'",
83 m_opaque_sp.get(),
84 m_opaque_sp ? m_opaque_sp->GetName().AsCString() : "<invalid>");
Caroline Tice7826c882010-10-26 03:11:13 +000085
Chris Lattner24943d22010-06-08 16:52:24 +000086 if (IsValid())
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000087 return m_opaque_sp->GetName().GetCString();
Caroline Tice61ba7ec2010-10-26 23:49:36 +000088
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000089 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000090}
91
92const char *
93SBValue::GetTypeName ()
94{
95 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000096 return m_opaque_sp->GetTypeName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000097 else
98 return NULL;
99}
100
101size_t
102SBValue::GetByteSize ()
103{
104 size_t result = 0;
105
106 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000107 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000108
109 return result;
110}
111
112bool
113SBValue::IsInScope (const SBFrame &frame)
114{
115 bool result = false;
116
117 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000118 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000119
120 return result;
121}
122
123const char *
124SBValue::GetValue (const SBFrame &frame)
125{
126 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000127 if ( m_opaque_sp)
Greg Clayton17dae082010-09-02 02:59:18 +0000128 value_string = m_opaque_sp->GetValueAsCString (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000129 return value_string;
130}
131
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000132ValueType
133SBValue::GetValueType ()
134{
135 if (m_opaque_sp)
136 return m_opaque_sp->GetValueType();
137 return eValueTypeInvalid;
138}
139
Jim Ingham4ae51962010-09-10 23:12:17 +0000140const char *
141SBValue::GetObjectDescription (const SBFrame &frame)
142{
143 const char *value_string = NULL;
144 if ( m_opaque_sp)
145 value_string = m_opaque_sp->GetObjectDescription (frame.get());
146 return value_string;
147}
148
Chris Lattner24943d22010-06-08 16:52:24 +0000149bool
Greg Clayton17dae082010-09-02 02:59:18 +0000150SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000151{
152 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000153 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000154 return false;
155}
156
157const char *
158SBValue::GetSummary (const SBFrame &frame)
159{
160 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000161 if ( m_opaque_sp)
162 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000163 return value_string;
164}
165
166const char *
167SBValue::GetLocation (const SBFrame &frame)
168{
169 const char *value_string = NULL;
170 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000171 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000172 return value_string;
173}
174
175bool
176SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
177{
178 bool success = false;
179 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000180 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000181 return success;
182}
183
184SBValue
185SBValue::GetChildAtIndex (uint32_t idx)
186{
187 lldb::ValueObjectSP child_sp;
188
189 if (IsValid())
190 {
Greg Clayton63094e02010-06-23 01:19:29 +0000191 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000192 }
193
194 SBValue sb_value (child_sp);
195 return sb_value;
196}
197
198uint32_t
199SBValue::GetIndexOfChildWithName (const char *name)
200{
201 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000202 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000203 return UINT32_MAX;
204}
205
206SBValue
207SBValue::GetChildMemberWithName (const char *name)
208{
209 lldb::ValueObjectSP child_sp;
210 const ConstString str_name (name);
211
212 if (IsValid())
213 {
Greg Clayton63094e02010-06-23 01:19:29 +0000214 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000215 }
216
217 SBValue sb_value (child_sp);
218 return sb_value;
219}
220
221
222uint32_t
223SBValue::GetNumChildren ()
224{
225 uint32_t num_children = 0;
226
227 if (IsValid())
228 {
Greg Clayton63094e02010-06-23 01:19:29 +0000229 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000230 }
231
232 return num_children;
233}
234
235bool
236SBValue::ValueIsStale ()
237{
238 bool result = true;
239
240 if (IsValid())
241 {
Greg Clayton63094e02010-06-23 01:19:29 +0000242 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000243 }
244
245 return result;
246}
247
248
249SBValue
250SBValue::Dereference ()
251{
252 if (IsValid())
253 {
Greg Clayton63094e02010-06-23 01:19:29 +0000254 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000255 {
256 return GetChildAtIndex(0);
257 }
258 }
259 return *this;
260}
261
262bool
263SBValue::TypeIsPtrType ()
264{
265 bool is_ptr_type = false;
266
267 if (IsValid())
268 {
Greg Clayton63094e02010-06-23 01:19:29 +0000269 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000270 }
271
272 return is_ptr_type;
273}
274
Chris Lattner24943d22010-06-08 16:52:24 +0000275void *
276SBValue::GetOpaqueType()
277{
Greg Clayton63094e02010-06-23 01:19:29 +0000278 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000279 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000280 return NULL;
281}
282
283// Mimic shared pointer...
284lldb_private::ValueObject *
285SBValue::get() const
286{
Greg Clayton63094e02010-06-23 01:19:29 +0000287 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000288}
289
290lldb_private::ValueObject *
291SBValue::operator->() const
292{
Greg Clayton63094e02010-06-23 01:19:29 +0000293 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000294}
295
296lldb::ValueObjectSP &
297SBValue::operator*()
298{
Greg Clayton63094e02010-06-23 01:19:29 +0000299 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000300}
301
302const lldb::ValueObjectSP &
303SBValue::operator*() const
304{
Greg Clayton63094e02010-06-23 01:19:29 +0000305 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000306}
Caroline Tice98f930f2010-09-20 05:20:02 +0000307
308bool
309SBValue::GetDescription (SBStream &description)
310{
311 if (m_opaque_sp)
312 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000313 // Don't call all these APIs and cause more logging!
314// const char *name = GetName();
315// const char *type_name = GetTypeName ();
316// size_t byte_size = GetByteSize ();
317// uint32_t num_children = GetNumChildren ();
318// bool is_stale = ValueIsStale ();
319// description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
320// (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
321// if (num_children > 0)
322// description.Printf (", num_children: %d", num_children);
323//
324// if (is_stale)
325// description.Printf (" [value is stale]");
326
327 description.Printf ("name: '%s'", m_opaque_sp->GetName().GetCString());
Caroline Tice98f930f2010-09-20 05:20:02 +0000328 }
329 else
330 description.Printf ("No value");
331
332 return true;
333}