blob: e2fdffab5929b4f06e2f4cf5e8c010d5374e19f6 [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"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/Stream.h"
16#include "lldb/Core/StreamFile.h"
17#include "lldb/Core/Value.h"
18#include "lldb/Core/ValueObject.h"
19#include "lldb/Symbol/Block.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Symbol/Variable.h"
22#include "lldb/Target/ExecutionContext.h"
23#include "lldb/Target/Process.h"
24#include "lldb/Target/StackFrame.h"
25#include "lldb/Target/Thread.h"
26
Eli Friedman7a62c8b2010-06-09 07:44:37 +000027#include "lldb/API/SBProcess.h"
28#include "lldb/API/SBTarget.h"
29#include "lldb/API/SBThread.h"
30#include "lldb/API/SBFrame.h"
31#include "lldb/API/SBDebugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36SBValue::SBValue () :
Greg Clayton63094e02010-06-23 01:19:29 +000037 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000038{
39}
40
41SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000042 m_opaque_sp (value_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000043{
44}
45
46SBValue::~SBValue()
47{
48}
49
50bool
51SBValue::IsValid () const
52{
Greg Clayton63094e02010-06-23 01:19:29 +000053 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
Chris Lattner24943d22010-06-08 16:52:24 +000056const char *
57SBValue::GetName()
58{
59 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000060 return m_opaque_sp->GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000061 else
62 return NULL;
63}
64
65const char *
66SBValue::GetTypeName ()
67{
68 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000069 return m_opaque_sp->GetTypeName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000070 else
71 return NULL;
72}
73
74size_t
75SBValue::GetByteSize ()
76{
77 size_t result = 0;
78
79 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000080 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +000081
82 return result;
83}
84
85bool
86SBValue::IsInScope (const SBFrame &frame)
87{
88 bool result = false;
89
90 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000091 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +000092
93 return result;
94}
95
96const char *
97SBValue::GetValue (const SBFrame &frame)
98{
99 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000100 if ( m_opaque_sp)
Greg Clayton17dae082010-09-02 02:59:18 +0000101 value_string = m_opaque_sp->GetValueAsCString (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000102 return value_string;
103}
104
Jim Ingham4ae51962010-09-10 23:12:17 +0000105const char *
106SBValue::GetObjectDescription (const SBFrame &frame)
107{
108 const char *value_string = NULL;
109 if ( m_opaque_sp)
110 value_string = m_opaque_sp->GetObjectDescription (frame.get());
111 return value_string;
112}
113
Chris Lattner24943d22010-06-08 16:52:24 +0000114bool
Greg Clayton17dae082010-09-02 02:59:18 +0000115SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000116{
117 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000118 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000119 return false;
120}
121
122const char *
123SBValue::GetSummary (const SBFrame &frame)
124{
125 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000126 if ( m_opaque_sp)
127 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000128 return value_string;
129}
130
131const char *
132SBValue::GetLocation (const SBFrame &frame)
133{
134 const char *value_string = NULL;
135 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000136 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000137 return value_string;
138}
139
140bool
141SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
142{
143 bool success = false;
144 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000145 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000146 return success;
147}
148
149SBValue
150SBValue::GetChildAtIndex (uint32_t idx)
151{
152 lldb::ValueObjectSP child_sp;
153
154 if (IsValid())
155 {
Greg Clayton63094e02010-06-23 01:19:29 +0000156 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000157 }
158
159 SBValue sb_value (child_sp);
160 return sb_value;
161}
162
163uint32_t
164SBValue::GetIndexOfChildWithName (const char *name)
165{
166 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000167 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000168 return UINT32_MAX;
169}
170
171SBValue
172SBValue::GetChildMemberWithName (const char *name)
173{
174 lldb::ValueObjectSP child_sp;
175 const ConstString str_name (name);
176
177 if (IsValid())
178 {
Greg Clayton63094e02010-06-23 01:19:29 +0000179 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000180 }
181
182 SBValue sb_value (child_sp);
183 return sb_value;
184}
185
186
187uint32_t
188SBValue::GetNumChildren ()
189{
190 uint32_t num_children = 0;
191
192 if (IsValid())
193 {
Greg Clayton63094e02010-06-23 01:19:29 +0000194 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000195 }
196
197 return num_children;
198}
199
200bool
201SBValue::ValueIsStale ()
202{
203 bool result = true;
204
205 if (IsValid())
206 {
Greg Clayton63094e02010-06-23 01:19:29 +0000207 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000208 }
209
210 return result;
211}
212
213
214SBValue
215SBValue::Dereference ()
216{
217 if (IsValid())
218 {
Greg Clayton63094e02010-06-23 01:19:29 +0000219 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000220 {
221 return GetChildAtIndex(0);
222 }
223 }
224 return *this;
225}
226
227bool
228SBValue::TypeIsPtrType ()
229{
230 bool is_ptr_type = false;
231
232 if (IsValid())
233 {
Greg Clayton63094e02010-06-23 01:19:29 +0000234 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000235 }
236
237 return is_ptr_type;
238}
239
Chris Lattner24943d22010-06-08 16:52:24 +0000240void *
241SBValue::GetOpaqueType()
242{
Greg Clayton63094e02010-06-23 01:19:29 +0000243 if (m_opaque_sp)
244 return m_opaque_sp->GetOpaqueClangQualType();
Chris Lattner24943d22010-06-08 16:52:24 +0000245 return NULL;
246}
247
248// Mimic shared pointer...
249lldb_private::ValueObject *
250SBValue::get() const
251{
Greg Clayton63094e02010-06-23 01:19:29 +0000252 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000253}
254
255lldb_private::ValueObject *
256SBValue::operator->() const
257{
Greg Clayton63094e02010-06-23 01:19:29 +0000258 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000259}
260
261lldb::ValueObjectSP &
262SBValue::operator*()
263{
Greg Clayton63094e02010-06-23 01:19:29 +0000264 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000265}
266
267const lldb::ValueObjectSP &
268SBValue::operator*() const
269{
Greg Clayton63094e02010-06-23 01:19:29 +0000270 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000271}
Caroline Tice98f930f2010-09-20 05:20:02 +0000272
273bool
274SBValue::GetDescription (SBStream &description)
275{
276 if (m_opaque_sp)
277 {
278 const char *name = GetName();
279 const char *type_name = GetTypeName ();
280 size_t byte_size = GetByteSize ();
281 uint32_t num_children = GetNumChildren ();
282 bool is_stale = ValueIsStale ();
283 description.Printf ("name: '%s', type: %s, size: %d", (name != NULL ? name : "<unknown name>"),
284 (type_name != NULL ? type_name : "<unknown type name>"), (int) byte_size);
285 if (num_children > 0)
286 description.Printf (", num_children: %d", num_children);
287
288 if (is_stale)
289 description.Printf (" [value is stale]");
290 }
291 else
292 description.Printf ("No value");
293
294 return true;
295}
296
297PyObject *
298SBValue::__repr__ ()
299{
300 SBStream description;
301 GetDescription (description);
302 return PyString_FromString (description.GetData());
303}