blob: 54f822ee574c937d6b0d376c2474aedf95fa16cf [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{
45}
46
47SBValue::~SBValue()
48{
49}
50
51bool
52SBValue::IsValid () const
53{
Greg Clayton49ce6822010-10-31 03:01:06 +000054 // If this function ever changes to anything that does more than just
55 // check if the opaque shared pointer is non NULL, then we need to update
56 // all "if (m_opaque_sp)" code in this file.
57 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000058}
59
Greg Claytonc5f728c2010-10-06 22:10:17 +000060SBError
61SBValue::GetError()
62{
63 SBError sb_error;
64
65 if (m_opaque_sp.get())
66 sb_error.SetError(m_opaque_sp->GetError());
67
68 return sb_error;
69}
70
Chris Lattner24943d22010-06-08 16:52:24 +000071const char *
72SBValue::GetName()
73{
Greg Clayton49ce6822010-10-31 03:01:06 +000074
75 const char *name = NULL;
76 if (m_opaque_sp)
77 name = m_opaque_sp->GetName().GetCString();
78
Caroline Tice7826c882010-10-26 03:11:13 +000079 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000080 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000081 {
82 if (name)
83 log->Printf ("SBValue(%p)::GetName () => \"%s\"", m_opaque_sp.get(), name);
84 else
85 log->Printf ("SBValue(%p)::GetName () => NULL", m_opaque_sp.get(), name);
86 }
Caroline Tice7826c882010-10-26 03:11:13 +000087
Greg Clayton49ce6822010-10-31 03:01:06 +000088 return name;
Chris Lattner24943d22010-06-08 16:52:24 +000089}
90
91const char *
92SBValue::GetTypeName ()
93{
Greg Clayton49ce6822010-10-31 03:01:06 +000094 const char *name = NULL;
95 if (m_opaque_sp)
96 name = m_opaque_sp->GetTypeName().GetCString();
97 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
98 if (log)
99 {
100 if (name)
101 log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"", m_opaque_sp.get(), name);
102 else
103 log->Printf ("SBValue(%p)::GetTypeName () => NULL", m_opaque_sp.get());
104 }
105
106 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000107}
108
109size_t
110SBValue::GetByteSize ()
111{
112 size_t result = 0;
113
Greg Clayton49ce6822010-10-31 03:01:06 +0000114 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000115 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000116
Greg Clayton49ce6822010-10-31 03:01:06 +0000117 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
118 if (log)
119 log->Printf ("SBValue(%p)::GetByteSize () => %zu", m_opaque_sp.get(), result);
120
Chris Lattner24943d22010-06-08 16:52:24 +0000121 return result;
122}
123
124bool
125SBValue::IsInScope (const SBFrame &frame)
126{
127 bool result = false;
128
Greg Clayton49ce6822010-10-31 03:01:06 +0000129 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000130 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000131
Greg Clayton49ce6822010-10-31 03:01:06 +0000132 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
133 if (log)
134 log->Printf ("SBValue(%p)::IsInScope () => %i", m_opaque_sp.get(), result);
135
Chris Lattner24943d22010-06-08 16:52:24 +0000136 return result;
137}
138
139const char *
140SBValue::GetValue (const SBFrame &frame)
141{
Greg Clayton49ce6822010-10-31 03:01:06 +0000142 const char *cstr = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000143 if ( m_opaque_sp)
Greg Clayton49ce6822010-10-31 03:01:06 +0000144 cstr = m_opaque_sp->GetValueAsCString (frame.get());
145 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
146 if (log)
147 {
148 if (cstr)
149 log->Printf ("SBValue(%p)::GetValue (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
150 else
151 log->Printf ("SBValue(%p)::GetValue (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
152 }
153
154 return cstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000155}
156
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000157ValueType
158SBValue::GetValueType ()
159{
Greg Clayton49ce6822010-10-31 03:01:06 +0000160 ValueType result = eValueTypeInvalid;
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000161 if (m_opaque_sp)
Greg Clayton49ce6822010-10-31 03:01:06 +0000162 result = m_opaque_sp->GetValueType();
163 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
164 if (log)
165 {
166 switch (result)
167 {
168 case eValueTypeInvalid: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid", m_opaque_sp.get()); break;
169 case eValueTypeVariableGlobal: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal", m_opaque_sp.get()); break;
170 case eValueTypeVariableStatic: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic", m_opaque_sp.get()); break;
171 case eValueTypeVariableArgument:log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument", m_opaque_sp.get()); break;
172 case eValueTypeVariableLocal: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal", m_opaque_sp.get()); break;
173 case eValueTypeRegister: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister", m_opaque_sp.get()); break;
174 case eValueTypeRegisterSet: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet", m_opaque_sp.get()); break;
175 case eValueTypeConstResult: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult", m_opaque_sp.get()); break;
176 default: log->Printf ("SBValue(%p)::GetValueType () => %i ???", m_opaque_sp.get(), result); break;
177 }
178 }
179 return result;
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000180}
181
Jim Ingham4ae51962010-09-10 23:12:17 +0000182const char *
183SBValue::GetObjectDescription (const SBFrame &frame)
184{
Greg Clayton49ce6822010-10-31 03:01:06 +0000185 const char *cstr = NULL;
Jim Ingham4ae51962010-09-10 23:12:17 +0000186 if ( m_opaque_sp)
Greg Clayton49ce6822010-10-31 03:01:06 +0000187 cstr = m_opaque_sp->GetObjectDescription (frame.get());
188 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
189 if (log)
190 {
191 if (cstr)
192 log->Printf ("SBValue(%p)::GetObjectDescription (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
193 else
194 log->Printf ("SBValue(%p)::GetObjectDescription (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
195 }
196 return cstr;
Jim Ingham4ae51962010-09-10 23:12:17 +0000197}
198
Chris Lattner24943d22010-06-08 16:52:24 +0000199bool
Greg Clayton17dae082010-09-02 02:59:18 +0000200SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000201{
Greg Clayton49ce6822010-10-31 03:01:06 +0000202 bool result = false;
203 if (m_opaque_sp)
204 result = m_opaque_sp->GetValueDidChange (frame.get());
205 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
206 if (log)
207 log->Printf ("SBValue(%p)::GetValueDidChange (SBFrame(%p)) => %i", m_opaque_sp.get(), frame.get(), result);
208
209 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000210}
211
212const char *
213SBValue::GetSummary (const SBFrame &frame)
214{
Greg Clayton49ce6822010-10-31 03:01:06 +0000215 const char *cstr = NULL;
216 if (m_opaque_sp)
217 cstr = m_opaque_sp->GetSummaryAsCString(frame.get());
218 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
219 if (log)
220 {
221 if (cstr)
222 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
223 else
224 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
225 }
226 return cstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000227}
228
229const char *
230SBValue::GetLocation (const SBFrame &frame)
231{
Greg Clayton49ce6822010-10-31 03:01:06 +0000232 const char *cstr = NULL;
233 if (m_opaque_sp)
234 cstr = m_opaque_sp->GetLocationAsCString(frame.get());
235 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
236 if (log)
237 {
238 if (cstr)
239 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
240 else
241 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
242 }
243 return cstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000244}
245
246bool
247SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
248{
249 bool success = false;
Greg Clayton49ce6822010-10-31 03:01:06 +0000250 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000251 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000252 return success;
253}
254
255SBValue
256SBValue::GetChildAtIndex (uint32_t idx)
257{
258 lldb::ValueObjectSP child_sp;
259
Greg Clayton49ce6822010-10-31 03:01:06 +0000260 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000261 {
Greg Clayton63094e02010-06-23 01:19:29 +0000262 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000263 }
264
265 SBValue sb_value (child_sp);
Greg Clayton49ce6822010-10-31 03:01:06 +0000266 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
267 if (log)
268 log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)", m_opaque_sp.get(), idx, sb_value.get());
269
Chris Lattner24943d22010-06-08 16:52:24 +0000270 return sb_value;
271}
272
273uint32_t
274SBValue::GetIndexOfChildWithName (const char *name)
275{
Greg Clayton49ce6822010-10-31 03:01:06 +0000276 uint32_t idx = UINT32_MAX;
277 if (m_opaque_sp)
278 idx = m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
279 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
280 if (log)
281 {
282 if (idx == UINT32_MAX)
283 log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND", m_opaque_sp.get(), name, idx);
284 else
285 log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u", m_opaque_sp.get(), name, idx);
286 }
287 return idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000288}
289
290SBValue
291SBValue::GetChildMemberWithName (const char *name)
292{
293 lldb::ValueObjectSP child_sp;
294 const ConstString str_name (name);
295
Greg Clayton49ce6822010-10-31 03:01:06 +0000296 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000297 {
Greg Clayton63094e02010-06-23 01:19:29 +0000298 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000299 }
300
301 SBValue sb_value (child_sp);
Greg Clayton49ce6822010-10-31 03:01:06 +0000302
303 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
304 if (log)
305 log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)", m_opaque_sp.get(), name, sb_value.get());
306
Chris Lattner24943d22010-06-08 16:52:24 +0000307 return sb_value;
308}
309
310
311uint32_t
312SBValue::GetNumChildren ()
313{
314 uint32_t num_children = 0;
315
Greg Clayton49ce6822010-10-31 03:01:06 +0000316 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000317 num_children = m_opaque_sp->GetNumChildren();
Greg Clayton49ce6822010-10-31 03:01:06 +0000318
319 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
320 if (log)
321 log->Printf ("SBValue(%p)::GetNumChildren () => %u", m_opaque_sp.get(), num_children);
Chris Lattner24943d22010-06-08 16:52:24 +0000322
323 return num_children;
324}
325
326bool
327SBValue::ValueIsStale ()
328{
329 bool result = true;
330
Greg Clayton49ce6822010-10-31 03:01:06 +0000331 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000332 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000333
334 return result;
335}
336
337
338SBValue
339SBValue::Dereference ()
340{
Greg Clayton49ce6822010-10-31 03:01:06 +0000341 SBValue sb_value;
342 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000343 {
Greg Clayton63094e02010-06-23 01:19:29 +0000344 if (m_opaque_sp->IsPointerType())
Greg Clayton49ce6822010-10-31 03:01:06 +0000345 sb_value = GetChildAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000346 }
Greg Clayton49ce6822010-10-31 03:01:06 +0000347 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
348 if (log)
349 log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)", m_opaque_sp.get(), sb_value.get());
350
351 return sb_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000352}
353
354bool
Greg Clayton49ce6822010-10-31 03:01:06 +0000355SBValue::TypeIsPointerType ()
Chris Lattner24943d22010-06-08 16:52:24 +0000356{
357 bool is_ptr_type = false;
358
Greg Clayton49ce6822010-10-31 03:01:06 +0000359 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000360 is_ptr_type = m_opaque_sp->IsPointerType();
Greg Clayton49ce6822010-10-31 03:01:06 +0000361
362 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
363 if (log)
364 log->Printf ("SBValue(%p)::TypeIsPointerType () => %i", m_opaque_sp.get(), is_ptr_type);
365
Chris Lattner24943d22010-06-08 16:52:24 +0000366
367 return is_ptr_type;
368}
369
Chris Lattner24943d22010-06-08 16:52:24 +0000370void *
371SBValue::GetOpaqueType()
372{
Greg Clayton63094e02010-06-23 01:19:29 +0000373 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000374 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000375 return NULL;
376}
377
378// Mimic shared pointer...
379lldb_private::ValueObject *
380SBValue::get() const
381{
Greg Clayton63094e02010-06-23 01:19:29 +0000382 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000383}
384
385lldb_private::ValueObject *
386SBValue::operator->() const
387{
Greg Clayton63094e02010-06-23 01:19:29 +0000388 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000389}
390
391lldb::ValueObjectSP &
392SBValue::operator*()
393{
Greg Clayton63094e02010-06-23 01:19:29 +0000394 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000395}
396
397const lldb::ValueObjectSP &
398SBValue::operator*() const
399{
Greg Clayton63094e02010-06-23 01:19:29 +0000400 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000401}
Caroline Tice98f930f2010-09-20 05:20:02 +0000402
403bool
Greg Clayton49ce6822010-10-31 03:01:06 +0000404SBValue::GetExpressionPath (SBStream &description)
405{
406 if (m_opaque_sp)
407 {
408 m_opaque_sp->GetExpressionPath (description.ref());
409 return true;
410 }
411 return false;
412}
413
414bool
Caroline Tice98f930f2010-09-20 05:20:02 +0000415SBValue::GetDescription (SBStream &description)
416{
417 if (m_opaque_sp)
418 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000419 // Don't call all these APIs and cause more logging!
420// const char *name = GetName();
421// const char *type_name = GetTypeName ();
422// size_t byte_size = GetByteSize ();
423// uint32_t num_children = GetNumChildren ();
424// bool is_stale = ValueIsStale ();
425// description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
426// (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
427// if (num_children > 0)
428// description.Printf (", num_children: %d", num_children);
429//
430// if (is_stale)
431// description.Printf (" [value is stale]");
432
433 description.Printf ("name: '%s'", m_opaque_sp->GetName().GetCString());
Caroline Tice98f930f2010-09-20 05:20:02 +0000434 }
435 else
436 description.Printf ("No value");
437
438 return true;
439}