blob: 780a61f9578ac82e6d72d55d186c2b14ebf90887 [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{
Caroline Tice7826c882010-10-26 03:11:13 +000040 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API | LIBLLDB_LOG_VERBOSE);
41
42 if (log)
43 log->Printf ("SBValue::SBValue () ==> this = %p", this);
Chris Lattner24943d22010-06-08 16:52:24 +000044}
45
46SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000047 m_opaque_sp (value_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000048{
Caroline Tice7826c882010-10-26 03:11:13 +000049 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API | LIBLLDB_LOG_VERBOSE);
50
51 if (log)
52 {
53 SBStream sstr;
54 GetDescription (sstr);
55 log->Printf ("SBValue::SBValue (const lldb::ValueObjectSP &value_sp) value_sp.get() = %p ==> this = %p (%s)",
56 value_sp.get(), this, sstr.GetData());
57 }
Chris Lattner24943d22010-06-08 16:52:24 +000058}
59
60SBValue::~SBValue()
61{
62}
63
64bool
65SBValue::IsValid () const
66{
Greg Clayton63094e02010-06-23 01:19:29 +000067 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000068}
69
Greg Claytonc5f728c2010-10-06 22:10:17 +000070SBError
71SBValue::GetError()
72{
73 SBError sb_error;
74
75 if (m_opaque_sp.get())
76 sb_error.SetError(m_opaque_sp->GetError());
77
78 return sb_error;
79}
80
Chris Lattner24943d22010-06-08 16:52:24 +000081const char *
82SBValue::GetName()
83{
Caroline Tice7826c882010-10-26 03:11:13 +000084 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
85
86 if (log)
87 log->Printf ("SBValue::GetName ()");
88
Chris Lattner24943d22010-06-08 16:52:24 +000089 if (IsValid())
Caroline Tice7826c882010-10-26 03:11:13 +000090 {
91 if (log)
92 log->Printf ("SBValue::GetName ==> %s", m_opaque_sp->GetName().AsCString());
Greg Clayton63094e02010-06-23 01:19:29 +000093 return m_opaque_sp->GetName().AsCString();
Caroline Tice7826c882010-10-26 03:11:13 +000094 }
Chris Lattner24943d22010-06-08 16:52:24 +000095 else
Caroline Tice7826c882010-10-26 03:11:13 +000096 {
97 if (log)
98 log->Printf ("SBValue::GetName ==> NULL");
Chris Lattner24943d22010-06-08 16:52:24 +000099 return NULL;
Caroline Tice7826c882010-10-26 03:11:13 +0000100 }
Chris Lattner24943d22010-06-08 16:52:24 +0000101}
102
103const char *
104SBValue::GetTypeName ()
105{
106 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000107 return m_opaque_sp->GetTypeName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000108 else
109 return NULL;
110}
111
112size_t
113SBValue::GetByteSize ()
114{
115 size_t result = 0;
116
117 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000118 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000119
120 return result;
121}
122
123bool
124SBValue::IsInScope (const SBFrame &frame)
125{
126 bool result = false;
127
128 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000129 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000130
131 return result;
132}
133
134const char *
135SBValue::GetValue (const SBFrame &frame)
136{
137 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000138 if ( m_opaque_sp)
Greg Clayton17dae082010-09-02 02:59:18 +0000139 value_string = m_opaque_sp->GetValueAsCString (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000140 return value_string;
141}
142
Jim Ingham4ae51962010-09-10 23:12:17 +0000143const char *
144SBValue::GetObjectDescription (const SBFrame &frame)
145{
146 const char *value_string = NULL;
147 if ( m_opaque_sp)
148 value_string = m_opaque_sp->GetObjectDescription (frame.get());
149 return value_string;
150}
151
Chris Lattner24943d22010-06-08 16:52:24 +0000152bool
Greg Clayton17dae082010-09-02 02:59:18 +0000153SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000154{
155 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000156 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000157 return false;
158}
159
160const char *
161SBValue::GetSummary (const SBFrame &frame)
162{
163 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000164 if ( m_opaque_sp)
165 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000166 return value_string;
167}
168
169const char *
170SBValue::GetLocation (const SBFrame &frame)
171{
172 const char *value_string = NULL;
173 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000174 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000175 return value_string;
176}
177
178bool
179SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
180{
181 bool success = false;
182 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000183 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000184 return success;
185}
186
187SBValue
188SBValue::GetChildAtIndex (uint32_t idx)
189{
190 lldb::ValueObjectSP child_sp;
191
192 if (IsValid())
193 {
Greg Clayton63094e02010-06-23 01:19:29 +0000194 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000195 }
196
197 SBValue sb_value (child_sp);
198 return sb_value;
199}
200
201uint32_t
202SBValue::GetIndexOfChildWithName (const char *name)
203{
204 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000205 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000206 return UINT32_MAX;
207}
208
209SBValue
210SBValue::GetChildMemberWithName (const char *name)
211{
212 lldb::ValueObjectSP child_sp;
213 const ConstString str_name (name);
214
215 if (IsValid())
216 {
Greg Clayton63094e02010-06-23 01:19:29 +0000217 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000218 }
219
220 SBValue sb_value (child_sp);
221 return sb_value;
222}
223
224
225uint32_t
226SBValue::GetNumChildren ()
227{
228 uint32_t num_children = 0;
229
230 if (IsValid())
231 {
Greg Clayton63094e02010-06-23 01:19:29 +0000232 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000233 }
234
235 return num_children;
236}
237
238bool
239SBValue::ValueIsStale ()
240{
241 bool result = true;
242
243 if (IsValid())
244 {
Greg Clayton63094e02010-06-23 01:19:29 +0000245 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000246 }
247
248 return result;
249}
250
251
252SBValue
253SBValue::Dereference ()
254{
255 if (IsValid())
256 {
Greg Clayton63094e02010-06-23 01:19:29 +0000257 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000258 {
259 return GetChildAtIndex(0);
260 }
261 }
262 return *this;
263}
264
265bool
266SBValue::TypeIsPtrType ()
267{
268 bool is_ptr_type = false;
269
270 if (IsValid())
271 {
Greg Clayton63094e02010-06-23 01:19:29 +0000272 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000273 }
274
275 return is_ptr_type;
276}
277
Chris Lattner24943d22010-06-08 16:52:24 +0000278void *
279SBValue::GetOpaqueType()
280{
Greg Clayton63094e02010-06-23 01:19:29 +0000281 if (m_opaque_sp)
Greg Clayton462d4142010-09-29 01:12:09 +0000282 return m_opaque_sp->GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +0000283 return NULL;
284}
285
286// Mimic shared pointer...
287lldb_private::ValueObject *
288SBValue::get() const
289{
Greg Clayton63094e02010-06-23 01:19:29 +0000290 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000291}
292
293lldb_private::ValueObject *
294SBValue::operator->() 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::ValueObjectSP &
300SBValue::operator*()
301{
Greg Clayton63094e02010-06-23 01:19:29 +0000302 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000303}
304
305const lldb::ValueObjectSP &
306SBValue::operator*() const
307{
Greg Clayton63094e02010-06-23 01:19:29 +0000308 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000309}
Caroline Tice98f930f2010-09-20 05:20:02 +0000310
311bool
312SBValue::GetDescription (SBStream &description)
313{
314 if (m_opaque_sp)
315 {
316 const char *name = GetName();
317 const char *type_name = GetTypeName ();
318 size_t byte_size = GetByteSize ();
319 uint32_t num_children = GetNumChildren ();
320 bool is_stale = ValueIsStale ();
321 description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
322 (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
323 if (num_children > 0)
324 description.Printf (", num_children: %d", num_children);
325
326 if (is_stale)
327 description.Printf (" [value is stale]");
328 }
329 else
330 description.Printf ("No value");
331
332 return true;
333}