blob: a90c1bff6b7e8bd0e2d90ecfc6a302e9801405cf [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"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12#include "lldb/Core/DataExtractor.h"
13#include "lldb/Core/Module.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamFile.h"
16#include "lldb/Core/Value.h"
17#include "lldb/Core/ValueObject.h"
18#include "lldb/Symbol/Block.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/Variable.h"
21#include "lldb/Target/ExecutionContext.h"
22#include "lldb/Target/Process.h"
23#include "lldb/Target/StackFrame.h"
24#include "lldb/Target/Thread.h"
25
Eli Friedman7a62c8b2010-06-09 07:44:37 +000026#include "lldb/API/SBProcess.h"
27#include "lldb/API/SBTarget.h"
28#include "lldb/API/SBThread.h"
29#include "lldb/API/SBFrame.h"
30#include "lldb/API/SBDebugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031
32using namespace lldb;
33using namespace lldb_private;
34
35SBValue::SBValue () :
Greg Clayton63094e02010-06-23 01:19:29 +000036 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000037{
38}
39
40SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000041 m_opaque_sp (value_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000042{
43}
44
45SBValue::~SBValue()
46{
47}
48
49bool
50SBValue::IsValid () const
51{
Greg Clayton63094e02010-06-23 01:19:29 +000052 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000053}
54
55void
56SBValue::Print (FILE *out_file, SBFrame *frame, bool print_type, bool print_value)
57{
58 if (out_file == NULL)
59 return;
60
61 if (IsValid())
62 {
63
64 SBThread sb_thread = frame->GetThread();
65 SBProcess sb_process = sb_thread.GetProcess();
66
67 lldb_private::StackFrame *lldb_frame = frame->GetLLDBObjectPtr();
68 lldb_private::Thread *lldb_thread = sb_thread.GetLLDBObjectPtr();
69 lldb_private::Process *lldb_process = sb_process.get();
70
71 lldb_private::ExecutionContext context (lldb_process, lldb_thread, lldb_frame);
72
73 lldb_private::StreamFile out_stream (out_file);
74
Greg Clayton63094e02010-06-23 01:19:29 +000075 out_stream.Printf ("%s ", m_opaque_sp->GetName().AsCString (NULL));
76 if (! m_opaque_sp->IsInScope (lldb_frame))
Chris Lattner24943d22010-06-08 16:52:24 +000077 out_stream.Printf ("[out-of-scope] ");
78 if (print_type)
79 {
Greg Clayton63094e02010-06-23 01:19:29 +000080 out_stream.Printf ("(%s) ", m_opaque_sp->GetTypeName().AsCString ("<unknown-type>"));
Chris Lattner24943d22010-06-08 16:52:24 +000081 }
82
83 if (print_value)
84 {
85 ExecutionContextScope *exe_scope = frame->get();
Greg Clayton63094e02010-06-23 01:19:29 +000086 const char *val_cstr = m_opaque_sp->GetValueAsCString(exe_scope);
87 const char *err_cstr = m_opaque_sp->GetError().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +000088
89 if (!err_cstr)
90 {
Greg Clayton63094e02010-06-23 01:19:29 +000091 const char *sum_cstr = m_opaque_sp->GetSummaryAsCString(exe_scope);
Chris Lattner24943d22010-06-08 16:52:24 +000092 const bool is_aggregate =
Greg Clayton63094e02010-06-23 01:19:29 +000093 ClangASTContext::IsAggregateType (m_opaque_sp->GetOpaqueClangQualType());
Chris Lattner24943d22010-06-08 16:52:24 +000094 if (val_cstr)
95 out_stream.Printf ("= %s ", val_cstr);
96
97 if (sum_cstr)
98 out_stream.Printf ("%s ", sum_cstr);
99
100 if (is_aggregate)
101 {
102 out_stream.PutChar ('{');
Greg Clayton63094e02010-06-23 01:19:29 +0000103 const uint32_t num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000104 if (num_children)
105 {
106 out_stream.IndentMore();
107 for (uint32_t idx = 0; idx < num_children; ++idx)
108 {
Greg Clayton63094e02010-06-23 01:19:29 +0000109 lldb::ValueObjectSP child_sp (m_opaque_sp->GetChildAtIndex (idx, true));
Chris Lattner24943d22010-06-08 16:52:24 +0000110 if (child_sp.get())
111 {
112 out_stream.EOL();
113 out_stream.Indent();
114 out_stream.Printf ("%s (%s) = %s", child_sp.get()->GetName().AsCString (""),
115 child_sp.get()->GetTypeName().AsCString ("<unknown type>"),
116 child_sp.get()->GetValueAsCString(exe_scope));
117 }
118 }
119 out_stream.IndentLess();
120 }
121 out_stream.EOL();
122 out_stream.Indent ("}");
123 }
124 }
125 }
126 out_stream.EOL ();
127 }
128}
129
130const char *
131SBValue::GetName()
132{
133 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000134 return m_opaque_sp->GetName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000135 else
136 return NULL;
137}
138
139const char *
140SBValue::GetTypeName ()
141{
142 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000143 return m_opaque_sp->GetTypeName().AsCString();
Chris Lattner24943d22010-06-08 16:52:24 +0000144 else
145 return NULL;
146}
147
148size_t
149SBValue::GetByteSize ()
150{
151 size_t result = 0;
152
153 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000154 result = m_opaque_sp->GetByteSize();
Chris Lattner24943d22010-06-08 16:52:24 +0000155
156 return result;
157}
158
159bool
160SBValue::IsInScope (const SBFrame &frame)
161{
162 bool result = false;
163
164 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000165 result = m_opaque_sp->IsInScope (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000166
167 return result;
168}
169
170const char *
171SBValue::GetValue (const SBFrame &frame)
172{
173 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000174 if ( m_opaque_sp)
Greg Clayton17dae082010-09-02 02:59:18 +0000175 value_string = m_opaque_sp->GetValueAsCString (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000176 return value_string;
177}
178
Jim Ingham4ae51962010-09-10 23:12:17 +0000179const char *
180SBValue::GetObjectDescription (const SBFrame &frame)
181{
182 const char *value_string = NULL;
183 if ( m_opaque_sp)
184 value_string = m_opaque_sp->GetObjectDescription (frame.get());
185 return value_string;
186}
187
Chris Lattner24943d22010-06-08 16:52:24 +0000188bool
Greg Clayton17dae082010-09-02 02:59:18 +0000189SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000190{
191 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000192 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000193 return false;
194}
195
196const char *
197SBValue::GetSummary (const SBFrame &frame)
198{
199 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000200 if ( m_opaque_sp)
201 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000202 return value_string;
203}
204
205const char *
206SBValue::GetLocation (const SBFrame &frame)
207{
208 const char *value_string = NULL;
209 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000210 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000211 return value_string;
212}
213
214bool
215SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
216{
217 bool success = false;
218 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000219 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000220 return success;
221}
222
223SBValue
224SBValue::GetChildAtIndex (uint32_t idx)
225{
226 lldb::ValueObjectSP child_sp;
227
228 if (IsValid())
229 {
Greg Clayton63094e02010-06-23 01:19:29 +0000230 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000231 }
232
233 SBValue sb_value (child_sp);
234 return sb_value;
235}
236
237uint32_t
238SBValue::GetIndexOfChildWithName (const char *name)
239{
240 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000241 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000242 return UINT32_MAX;
243}
244
245SBValue
246SBValue::GetChildMemberWithName (const char *name)
247{
248 lldb::ValueObjectSP child_sp;
249 const ConstString str_name (name);
250
251 if (IsValid())
252 {
Greg Clayton63094e02010-06-23 01:19:29 +0000253 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000254 }
255
256 SBValue sb_value (child_sp);
257 return sb_value;
258}
259
260
261uint32_t
262SBValue::GetNumChildren ()
263{
264 uint32_t num_children = 0;
265
266 if (IsValid())
267 {
Greg Clayton63094e02010-06-23 01:19:29 +0000268 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000269 }
270
271 return num_children;
272}
273
274bool
275SBValue::ValueIsStale ()
276{
277 bool result = true;
278
279 if (IsValid())
280 {
Greg Clayton63094e02010-06-23 01:19:29 +0000281 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000282 }
283
284 return result;
285}
286
287
288SBValue
289SBValue::Dereference ()
290{
291 if (IsValid())
292 {
Greg Clayton63094e02010-06-23 01:19:29 +0000293 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000294 {
295 return GetChildAtIndex(0);
296 }
297 }
298 return *this;
299}
300
301bool
302SBValue::TypeIsPtrType ()
303{
304 bool is_ptr_type = false;
305
306 if (IsValid())
307 {
Greg Clayton63094e02010-06-23 01:19:29 +0000308 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000309 }
310
311 return is_ptr_type;
312}
313
Chris Lattner24943d22010-06-08 16:52:24 +0000314void *
315SBValue::GetOpaqueType()
316{
Greg Clayton63094e02010-06-23 01:19:29 +0000317 if (m_opaque_sp)
318 return m_opaque_sp->GetOpaqueClangQualType();
Chris Lattner24943d22010-06-08 16:52:24 +0000319 return NULL;
320}
321
322// Mimic shared pointer...
323lldb_private::ValueObject *
324SBValue::get() const
325{
Greg Clayton63094e02010-06-23 01:19:29 +0000326 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000327}
328
329lldb_private::ValueObject *
330SBValue::operator->() const
331{
Greg Clayton63094e02010-06-23 01:19:29 +0000332 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000333}
334
335lldb::ValueObjectSP &
336SBValue::operator*()
337{
Greg Clayton63094e02010-06-23 01:19:29 +0000338 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000339}
340
341const lldb::ValueObjectSP &
342SBValue::operator*() const
343{
Greg Clayton63094e02010-06-23 01:19:29 +0000344 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000345}