blob: ac81823438924316ed99326c7d9c5156acd9d792 [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);
Caroline Tice61ba7ec2010-10-26 23:49:36 +000051 log->Printf ("SBValue::SBValue (value_sp=%p) => this.sp = %p (%s)",
52 value_sp.get(), m_opaque_sp.get(), sstr.GetData());
Caroline Tice7826c882010-10-26 03:11:13 +000053 }
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
56SBValue::~SBValue()
57{
58}
59
60bool
61SBValue::IsValid () const
62{
Greg Clayton63094e02010-06-23 01:19:29 +000063 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000064}
65
Greg Claytonc5f728c2010-10-06 22:10:17 +000066SBError
67SBValue::GetError()
68{
69 SBError sb_error;
70
71 if (m_opaque_sp.get())
72 sb_error.SetError(m_opaque_sp->GetError());
73
74 return sb_error;
75}
76
Chris Lattner24943d22010-06-08 16:52:24 +000077const char *
78SBValue::GetName()
79{
Caroline Tice7826c882010-10-26 03:11:13 +000080 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
81
Caroline Tice61ba7ec2010-10-26 23:49:36 +000082 //if (log)
83 // log->Printf ("SBValue::GetName ()");
Caroline Tice7826c882010-10-26 03:11:13 +000084
Chris Lattner24943d22010-06-08 16:52:24 +000085 if (IsValid())
Caroline Tice7826c882010-10-26 03:11:13 +000086 {
87 if (log)
Caroline Tice61ba7ec2010-10-26 23:49:36 +000088 log->Printf ("SBValue::GetName (this.sp=%p) => '%s'", m_opaque_sp.get(),
89 m_opaque_sp->GetName().AsCString());
90
Greg Clayton63094e02010-06-23 01:19:29 +000091 return m_opaque_sp->GetName().AsCString();
Caroline Tice7826c882010-10-26 03:11:13 +000092 }
Chris Lattner24943d22010-06-08 16:52:24 +000093 else
Caroline Tice7826c882010-10-26 03:11:13 +000094 {
95 if (log)
Caroline Tice61ba7ec2010-10-26 23:49:36 +000096 log->Printf ("SBValue::GetName (this.sp=%p) ==> NULL", m_opaque_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +000097 return NULL;
Caroline Tice7826c882010-10-26 03:11:13 +000098 }
Chris Lattner24943d22010-06-08 16:52:24 +000099}
100
101const char *
102SBValue::GetTypeName ()
103{
104 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000105 return m_opaque_sp->GetTypeName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000106 else
107 return NULL;
108}
109
110size_t
111SBValue::GetByteSize ()
112{
113 size_t result = 0;
114
115 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000116 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000117
118 return result;
119}
120
121bool
122SBValue::IsInScope (const SBFrame &frame)
123{
124 bool result = false;
125
126 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000127 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000128
129 return result;
130}
131
132const char *
133SBValue::GetValue (const SBFrame &frame)
134{
135 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000136 if ( m_opaque_sp)
Greg Clayton17dae082010-09-02 02:59:18 +0000137 value_string = m_opaque_sp->GetValueAsCString (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000138 return value_string;
139}
140
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000141ValueType
142SBValue::GetValueType ()
143{
144 if (m_opaque_sp)
145 return m_opaque_sp->GetValueType();
146 return eValueTypeInvalid;
147}
148
Jim Ingham4ae51962010-09-10 23:12:17 +0000149const char *
150SBValue::GetObjectDescription (const SBFrame &frame)
151{
152 const char *value_string = NULL;
153 if ( m_opaque_sp)
154 value_string = m_opaque_sp->GetObjectDescription (frame.get());
155 return value_string;
156}
157
Chris Lattner24943d22010-06-08 16:52:24 +0000158bool
Greg Clayton17dae082010-09-02 02:59:18 +0000159SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000160{
161 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000162 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000163 return false;
164}
165
166const char *
167SBValue::GetSummary (const SBFrame &frame)
168{
169 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000170 if ( m_opaque_sp)
171 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000172 return value_string;
173}
174
175const char *
176SBValue::GetLocation (const SBFrame &frame)
177{
178 const char *value_string = NULL;
179 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000180 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000181 return value_string;
182}
183
184bool
185SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
186{
187 bool success = false;
188 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000189 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000190 return success;
191}
192
193SBValue
194SBValue::GetChildAtIndex (uint32_t idx)
195{
196 lldb::ValueObjectSP child_sp;
197
198 if (IsValid())
199 {
Greg Clayton63094e02010-06-23 01:19:29 +0000200 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000201 }
202
203 SBValue sb_value (child_sp);
204 return sb_value;
205}
206
207uint32_t
208SBValue::GetIndexOfChildWithName (const char *name)
209{
210 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000211 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000212 return UINT32_MAX;
213}
214
215SBValue
216SBValue::GetChildMemberWithName (const char *name)
217{
218 lldb::ValueObjectSP child_sp;
219 const ConstString str_name (name);
220
221 if (IsValid())
222 {
Greg Clayton63094e02010-06-23 01:19:29 +0000223 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000224 }
225
226 SBValue sb_value (child_sp);
227 return sb_value;
228}
229
230
231uint32_t
232SBValue::GetNumChildren ()
233{
234 uint32_t num_children = 0;
235
236 if (IsValid())
237 {
Greg Clayton63094e02010-06-23 01:19:29 +0000238 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000239 }
240
241 return num_children;
242}
243
244bool
245SBValue::ValueIsStale ()
246{
247 bool result = true;
248
249 if (IsValid())
250 {
Greg Clayton63094e02010-06-23 01:19:29 +0000251 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000252 }
253
254 return result;
255}
256
257
258SBValue
259SBValue::Dereference ()
260{
261 if (IsValid())
262 {
Greg Clayton63094e02010-06-23 01:19:29 +0000263 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
265 return GetChildAtIndex(0);
266 }
267 }
268 return *this;
269}
270
271bool
272SBValue::TypeIsPtrType ()
273{
274 bool is_ptr_type = false;
275
276 if (IsValid())
277 {
Greg Clayton63094e02010-06-23 01:19:29 +0000278 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000279 }
280
281 return is_ptr_type;
282}
283
Chris Lattner24943d22010-06-08 16:52:24 +0000284void *
285SBValue::GetOpaqueType()
286{
Greg Clayton63094e02010-06-23 01:19:29 +0000287 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000288 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000289 return NULL;
290}
291
292// Mimic shared pointer...
293lldb_private::ValueObject *
294SBValue::get() const
295{
Greg Clayton63094e02010-06-23 01:19:29 +0000296 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000297}
298
299lldb_private::ValueObject *
300SBValue::operator->() const
301{
Greg Clayton63094e02010-06-23 01:19:29 +0000302 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000303}
304
305lldb::ValueObjectSP &
306SBValue::operator*()
307{
Greg Clayton63094e02010-06-23 01:19:29 +0000308 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000309}
310
311const lldb::ValueObjectSP &
312SBValue::operator*() const
313{
Greg Clayton63094e02010-06-23 01:19:29 +0000314 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000315}
Caroline Tice98f930f2010-09-20 05:20:02 +0000316
317bool
318SBValue::GetDescription (SBStream &description)
319{
320 if (m_opaque_sp)
321 {
322 const char *name = GetName();
323 const char *type_name = GetTypeName ();
324 size_t byte_size = GetByteSize ();
325 uint32_t num_children = GetNumChildren ();
326 bool is_stale = ValueIsStale ();
327 description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
328 (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
329 if (num_children > 0)
330 description.Printf (", num_children: %d", num_children);
331
332 if (is_stale)
333 description.Printf (" [value is stale]");
334 }
335 else
336 description.Printf ("No value");
337
338 return true;
339}