blob: 538324cb25ab49e7506cbdba26d333ae8a077c48 [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
Greg Clayton538eb822010-11-05 23:17:00 +000047SBValue::SBValue(const SBValue &rhs) :
48 m_opaque_sp (rhs.m_opaque_sp)
49{
50}
51
52const SBValue &
53SBValue::operator = (const SBValue &rhs)
54{
55 if (this != &rhs)
56 m_opaque_sp = rhs.m_opaque_sp;
57 return *this;
58}
59
Chris Lattner24943d22010-06-08 16:52:24 +000060SBValue::~SBValue()
61{
62}
63
64bool
65SBValue::IsValid () const
66{
Greg Clayton49ce6822010-10-31 03:01:06 +000067 // If this function ever changes to anything that does more than just
68 // check if the opaque shared pointer is non NULL, then we need to update
69 // all "if (m_opaque_sp)" code in this file.
70 return m_opaque_sp.get() != NULL;
Chris Lattner24943d22010-06-08 16:52:24 +000071}
72
Greg Claytonc5f728c2010-10-06 22:10:17 +000073SBError
74SBValue::GetError()
75{
76 SBError sb_error;
77
78 if (m_opaque_sp.get())
79 sb_error.SetError(m_opaque_sp->GetError());
80
81 return sb_error;
82}
83
Chris Lattner24943d22010-06-08 16:52:24 +000084const char *
85SBValue::GetName()
86{
Greg Clayton49ce6822010-10-31 03:01:06 +000087
88 const char *name = NULL;
89 if (m_opaque_sp)
90 name = m_opaque_sp->GetName().GetCString();
91
Greg Claytone005f2c2010-11-06 01:53:30 +000092 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton3f5ee7f2010-10-29 04:59:35 +000093 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000094 {
95 if (name)
96 log->Printf ("SBValue(%p)::GetName () => \"%s\"", m_opaque_sp.get(), name);
97 else
98 log->Printf ("SBValue(%p)::GetName () => NULL", m_opaque_sp.get(), name);
99 }
Caroline Tice7826c882010-10-26 03:11:13 +0000100
Greg Clayton49ce6822010-10-31 03:01:06 +0000101 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000102}
103
104const char *
105SBValue::GetTypeName ()
106{
Greg Clayton49ce6822010-10-31 03:01:06 +0000107 const char *name = NULL;
108 if (m_opaque_sp)
109 name = m_opaque_sp->GetTypeName().GetCString();
Greg Claytone005f2c2010-11-06 01:53:30 +0000110 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000111 if (log)
112 {
113 if (name)
114 log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"", m_opaque_sp.get(), name);
115 else
116 log->Printf ("SBValue(%p)::GetTypeName () => NULL", m_opaque_sp.get());
117 }
118
119 return name;
Chris Lattner24943d22010-06-08 16:52:24 +0000120}
121
122size_t
123SBValue::GetByteSize ()
124{
125 size_t result = 0;
126
Greg Clayton49ce6822010-10-31 03:01:06 +0000127 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000128 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000129
Greg Claytone005f2c2010-11-06 01:53:30 +0000130 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000131 if (log)
132 log->Printf ("SBValue(%p)::GetByteSize () => %zu", m_opaque_sp.get(), result);
133
Chris Lattner24943d22010-06-08 16:52:24 +0000134 return result;
135}
136
137bool
138SBValue::IsInScope (const SBFrame &frame)
139{
140 bool result = false;
141
Greg Clayton49ce6822010-10-31 03:01:06 +0000142 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000143 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000144
Greg Claytone005f2c2010-11-06 01:53:30 +0000145 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000146 if (log)
147 log->Printf ("SBValue(%p)::IsInScope () => %i", m_opaque_sp.get(), result);
148
Chris Lattner24943d22010-06-08 16:52:24 +0000149 return result;
150}
151
152const char *
153SBValue::GetValue (const SBFrame &frame)
154{
Greg Clayton49ce6822010-10-31 03:01:06 +0000155 const char *cstr = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000156 if ( m_opaque_sp)
Greg Clayton49ce6822010-10-31 03:01:06 +0000157 cstr = m_opaque_sp->GetValueAsCString (frame.get());
Greg Claytone005f2c2010-11-06 01:53:30 +0000158 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000159 if (log)
160 {
161 if (cstr)
162 log->Printf ("SBValue(%p)::GetValue (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
163 else
164 log->Printf ("SBValue(%p)::GetValue (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
165 }
166
167 return cstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000168}
169
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000170ValueType
171SBValue::GetValueType ()
172{
Greg Clayton49ce6822010-10-31 03:01:06 +0000173 ValueType result = eValueTypeInvalid;
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000174 if (m_opaque_sp)
Greg Clayton49ce6822010-10-31 03:01:06 +0000175 result = m_opaque_sp->GetValueType();
Greg Claytone005f2c2010-11-06 01:53:30 +0000176 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000177 if (log)
178 {
179 switch (result)
180 {
181 case eValueTypeInvalid: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid", m_opaque_sp.get()); break;
182 case eValueTypeVariableGlobal: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal", m_opaque_sp.get()); break;
183 case eValueTypeVariableStatic: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic", m_opaque_sp.get()); break;
184 case eValueTypeVariableArgument:log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument", m_opaque_sp.get()); break;
185 case eValueTypeVariableLocal: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal", m_opaque_sp.get()); break;
186 case eValueTypeRegister: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister", m_opaque_sp.get()); break;
187 case eValueTypeRegisterSet: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet", m_opaque_sp.get()); break;
188 case eValueTypeConstResult: log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult", m_opaque_sp.get()); break;
189 default: log->Printf ("SBValue(%p)::GetValueType () => %i ???", m_opaque_sp.get(), result); break;
190 }
191 }
192 return result;
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000193}
194
Jim Ingham4ae51962010-09-10 23:12:17 +0000195const char *
196SBValue::GetObjectDescription (const SBFrame &frame)
197{
Greg Clayton49ce6822010-10-31 03:01:06 +0000198 const char *cstr = NULL;
Jim Ingham4ae51962010-09-10 23:12:17 +0000199 if ( m_opaque_sp)
Greg Clayton49ce6822010-10-31 03:01:06 +0000200 cstr = m_opaque_sp->GetObjectDescription (frame.get());
Greg Claytone005f2c2010-11-06 01:53:30 +0000201 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000202 if (log)
203 {
204 if (cstr)
205 log->Printf ("SBValue(%p)::GetObjectDescription (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
206 else
207 log->Printf ("SBValue(%p)::GetObjectDescription (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
208 }
209 return cstr;
Jim Ingham4ae51962010-09-10 23:12:17 +0000210}
211
Chris Lattner24943d22010-06-08 16:52:24 +0000212bool
Greg Clayton17dae082010-09-02 02:59:18 +0000213SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Greg Clayton49ce6822010-10-31 03:01:06 +0000215 bool result = false;
216 if (m_opaque_sp)
217 result = m_opaque_sp->GetValueDidChange (frame.get());
Greg Claytone005f2c2010-11-06 01:53:30 +0000218 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000219 if (log)
220 log->Printf ("SBValue(%p)::GetValueDidChange (SBFrame(%p)) => %i", m_opaque_sp.get(), frame.get(), result);
221
222 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000223}
224
225const char *
226SBValue::GetSummary (const SBFrame &frame)
227{
Greg Clayton49ce6822010-10-31 03:01:06 +0000228 const char *cstr = NULL;
229 if (m_opaque_sp)
230 cstr = m_opaque_sp->GetSummaryAsCString(frame.get());
Greg Claytone005f2c2010-11-06 01:53:30 +0000231 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000232 if (log)
233 {
234 if (cstr)
235 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
236 else
237 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
238 }
239 return cstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000240}
241
242const char *
243SBValue::GetLocation (const SBFrame &frame)
244{
Greg Clayton49ce6822010-10-31 03:01:06 +0000245 const char *cstr = NULL;
246 if (m_opaque_sp)
247 cstr = m_opaque_sp->GetLocationAsCString(frame.get());
Greg Claytone005f2c2010-11-06 01:53:30 +0000248 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000249 if (log)
250 {
251 if (cstr)
252 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => \"%s\"", m_opaque_sp.get(), frame.get(), cstr);
253 else
254 log->Printf ("SBValue(%p)::GetSummary (SBFrame(%p)) => NULL", m_opaque_sp.get(), frame.get());
255 }
256 return cstr;
Chris Lattner24943d22010-06-08 16:52:24 +0000257}
258
259bool
260SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
261{
262 bool success = false;
Greg Clayton49ce6822010-10-31 03:01:06 +0000263 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000264 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000265 return success;
266}
267
268SBValue
269SBValue::GetChildAtIndex (uint32_t idx)
270{
271 lldb::ValueObjectSP child_sp;
272
Greg Clayton49ce6822010-10-31 03:01:06 +0000273 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000274 {
Greg Clayton63094e02010-06-23 01:19:29 +0000275 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000276 }
277
278 SBValue sb_value (child_sp);
Greg Claytone005f2c2010-11-06 01:53:30 +0000279 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000280 if (log)
281 log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)", m_opaque_sp.get(), idx, sb_value.get());
282
Chris Lattner24943d22010-06-08 16:52:24 +0000283 return sb_value;
284}
285
286uint32_t
287SBValue::GetIndexOfChildWithName (const char *name)
288{
Greg Clayton49ce6822010-10-31 03:01:06 +0000289 uint32_t idx = UINT32_MAX;
290 if (m_opaque_sp)
291 idx = m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Greg Claytone005f2c2010-11-06 01:53:30 +0000292 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000293 if (log)
294 {
295 if (idx == UINT32_MAX)
296 log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND", m_opaque_sp.get(), name, idx);
297 else
298 log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u", m_opaque_sp.get(), name, idx);
299 }
300 return idx;
Chris Lattner24943d22010-06-08 16:52:24 +0000301}
302
303SBValue
304SBValue::GetChildMemberWithName (const char *name)
305{
306 lldb::ValueObjectSP child_sp;
307 const ConstString str_name (name);
308
Greg Clayton49ce6822010-10-31 03:01:06 +0000309 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000310 {
Greg Clayton63094e02010-06-23 01:19:29 +0000311 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000312 }
313
314 SBValue sb_value (child_sp);
Greg Clayton49ce6822010-10-31 03:01:06 +0000315
Greg Claytone005f2c2010-11-06 01:53:30 +0000316 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000317 if (log)
318 log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)", m_opaque_sp.get(), name, sb_value.get());
319
Chris Lattner24943d22010-06-08 16:52:24 +0000320 return sb_value;
321}
322
323
324uint32_t
325SBValue::GetNumChildren ()
326{
327 uint32_t num_children = 0;
328
Greg Clayton49ce6822010-10-31 03:01:06 +0000329 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000330 num_children = m_opaque_sp->GetNumChildren();
Greg Clayton49ce6822010-10-31 03:01:06 +0000331
Greg Claytone005f2c2010-11-06 01:53:30 +0000332 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000333 if (log)
334 log->Printf ("SBValue(%p)::GetNumChildren () => %u", m_opaque_sp.get(), num_children);
Chris Lattner24943d22010-06-08 16:52:24 +0000335
336 return num_children;
337}
338
339bool
340SBValue::ValueIsStale ()
341{
342 bool result = true;
343
Greg Clayton49ce6822010-10-31 03:01:06 +0000344 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000345 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000346
347 return result;
348}
349
350
351SBValue
352SBValue::Dereference ()
353{
Greg Clayton49ce6822010-10-31 03:01:06 +0000354 SBValue sb_value;
355 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000356 {
Greg Clayton63094e02010-06-23 01:19:29 +0000357 if (m_opaque_sp->IsPointerType())
Greg Clayton49ce6822010-10-31 03:01:06 +0000358 sb_value = GetChildAtIndex(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000359 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000360 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000361 if (log)
362 log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)", m_opaque_sp.get(), sb_value.get());
363
364 return sb_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000365}
366
367bool
Greg Clayton49ce6822010-10-31 03:01:06 +0000368SBValue::TypeIsPointerType ()
Chris Lattner24943d22010-06-08 16:52:24 +0000369{
370 bool is_ptr_type = false;
371
Greg Clayton49ce6822010-10-31 03:01:06 +0000372 if (m_opaque_sp)
Greg Clayton63094e02010-06-23 01:19:29 +0000373 is_ptr_type = m_opaque_sp->IsPointerType();
Greg Clayton49ce6822010-10-31 03:01:06 +0000374
Greg Claytone005f2c2010-11-06 01:53:30 +0000375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton49ce6822010-10-31 03:01:06 +0000376 if (log)
377 log->Printf ("SBValue(%p)::TypeIsPointerType () => %i", m_opaque_sp.get(), is_ptr_type);
378
Chris Lattner24943d22010-06-08 16:52:24 +0000379
380 return is_ptr_type;
381}
382
Chris Lattner24943d22010-06-08 16:52:24 +0000383void *
384SBValue::GetOpaqueType()
385{
Greg Clayton63094e02010-06-23 01:19:29 +0000386 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000387 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000388 return NULL;
389}
390
391// Mimic shared pointer...
392lldb_private::ValueObject *
393SBValue::get() const
394{
Greg Clayton63094e02010-06-23 01:19:29 +0000395 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000396}
397
398lldb_private::ValueObject *
399SBValue::operator->() const
400{
Greg Clayton63094e02010-06-23 01:19:29 +0000401 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000402}
403
404lldb::ValueObjectSP &
405SBValue::operator*()
406{
Greg Clayton63094e02010-06-23 01:19:29 +0000407 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000408}
409
410const lldb::ValueObjectSP &
411SBValue::operator*() const
412{
Greg Clayton63094e02010-06-23 01:19:29 +0000413 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000414}
Caroline Tice98f930f2010-09-20 05:20:02 +0000415
416bool
Greg Clayton49ce6822010-10-31 03:01:06 +0000417SBValue::GetExpressionPath (SBStream &description)
418{
419 if (m_opaque_sp)
420 {
421 m_opaque_sp->GetExpressionPath (description.ref());
422 return true;
423 }
424 return false;
425}
426
427bool
Caroline Tice98f930f2010-09-20 05:20:02 +0000428SBValue::GetDescription (SBStream &description)
429{
430 if (m_opaque_sp)
431 {
Greg Clayton3f5ee7f2010-10-29 04:59:35 +0000432 // Don't call all these APIs and cause more logging!
433// const char *name = GetName();
434// const char *type_name = GetTypeName ();
435// size_t byte_size = GetByteSize ();
436// uint32_t num_children = GetNumChildren ();
437// bool is_stale = ValueIsStale ();
438// description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
439// (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
440// if (num_children > 0)
441// description.Printf (", num_children: %d", num_children);
442//
443// if (is_stale)
444// description.Printf (" [value is stale]");
445
446 description.Printf ("name: '%s'", m_opaque_sp->GetName().GetCString());
Caroline Tice98f930f2010-09-20 05:20:02 +0000447 }
448 else
449 description.Printf ("No value");
450
451 return true;
452}