blob: 7f0cfc2c93dcdcca06379076b751144d1f5cded8 [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
Jim Ingham4ae51962010-09-10 23:12:17 +0000141const char *
142SBValue::GetObjectDescription (const SBFrame &frame)
143{
144 const char *value_string = NULL;
145 if ( m_opaque_sp)
146 value_string = m_opaque_sp->GetObjectDescription (frame.get());
147 return value_string;
148}
149
Chris Lattner24943d22010-06-08 16:52:24 +0000150bool
Greg Clayton17dae082010-09-02 02:59:18 +0000151SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000152{
153 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000154 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000155 return false;
156}
157
158const char *
159SBValue::GetSummary (const SBFrame &frame)
160{
161 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000162 if ( m_opaque_sp)
163 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000164 return value_string;
165}
166
167const char *
168SBValue::GetLocation (const SBFrame &frame)
169{
170 const char *value_string = NULL;
171 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000172 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000173 return value_string;
174}
175
176bool
177SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
178{
179 bool success = false;
180 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000181 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000182 return success;
183}
184
185SBValue
186SBValue::GetChildAtIndex (uint32_t idx)
187{
188 lldb::ValueObjectSP child_sp;
189
190 if (IsValid())
191 {
Greg Clayton63094e02010-06-23 01:19:29 +0000192 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000193 }
194
195 SBValue sb_value (child_sp);
196 return sb_value;
197}
198
199uint32_t
200SBValue::GetIndexOfChildWithName (const char *name)
201{
202 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000203 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000204 return UINT32_MAX;
205}
206
207SBValue
208SBValue::GetChildMemberWithName (const char *name)
209{
210 lldb::ValueObjectSP child_sp;
211 const ConstString str_name (name);
212
213 if (IsValid())
214 {
Greg Clayton63094e02010-06-23 01:19:29 +0000215 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000216 }
217
218 SBValue sb_value (child_sp);
219 return sb_value;
220}
221
222
223uint32_t
224SBValue::GetNumChildren ()
225{
226 uint32_t num_children = 0;
227
228 if (IsValid())
229 {
Greg Clayton63094e02010-06-23 01:19:29 +0000230 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000231 }
232
233 return num_children;
234}
235
236bool
237SBValue::ValueIsStale ()
238{
239 bool result = true;
240
241 if (IsValid())
242 {
Greg Clayton63094e02010-06-23 01:19:29 +0000243 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000244 }
245
246 return result;
247}
248
249
250SBValue
251SBValue::Dereference ()
252{
253 if (IsValid())
254 {
Greg Clayton63094e02010-06-23 01:19:29 +0000255 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000256 {
257 return GetChildAtIndex(0);
258 }
259 }
260 return *this;
261}
262
263bool
264SBValue::TypeIsPtrType ()
265{
266 bool is_ptr_type = false;
267
268 if (IsValid())
269 {
Greg Clayton63094e02010-06-23 01:19:29 +0000270 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000271 }
272
273 return is_ptr_type;
274}
275
Chris Lattner24943d22010-06-08 16:52:24 +0000276void *
277SBValue::GetOpaqueType()
278{
Greg Clayton63094e02010-06-23 01:19:29 +0000279 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000280 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000281 return NULL;
282}
283
284// Mimic shared pointer...
285lldb_private::ValueObject *
286SBValue::get() const
287{
Greg Clayton63094e02010-06-23 01:19:29 +0000288 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000289}
290
291lldb_private::ValueObject *
292SBValue::operator->() const
293{
Greg Clayton63094e02010-06-23 01:19:29 +0000294 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000295}
296
297lldb::ValueObjectSP &
298SBValue::operator*()
299{
Greg Clayton63094e02010-06-23 01:19:29 +0000300 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000301}
302
303const lldb::ValueObjectSP &
304SBValue::operator*() const
305{
Greg Clayton63094e02010-06-23 01:19:29 +0000306 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000307}
Caroline Tice98f930f2010-09-20 05:20:02 +0000308
309bool
310SBValue::GetDescription (SBStream &description)
311{
312 if (m_opaque_sp)
313 {
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 else
328 description.Printf ("No value");
329
330 return true;
331}