blob: f92be9f1037bc231ebd08da633030af4c3943649 [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
179bool
Greg Clayton17dae082010-09-02 02:59:18 +0000180SBValue::GetValueDidChange (const SBFrame &frame)
Chris Lattner24943d22010-06-08 16:52:24 +0000181{
182 if (IsValid())
Greg Clayton17dae082010-09-02 02:59:18 +0000183 return m_opaque_sp->GetValueDidChange (frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000184 return false;
185}
186
187const char *
188SBValue::GetSummary (const SBFrame &frame)
189{
190 const char *value_string = NULL;
Greg Clayton63094e02010-06-23 01:19:29 +0000191 if ( m_opaque_sp)
192 value_string = m_opaque_sp->GetSummaryAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000193 return value_string;
194}
195
196const char *
197SBValue::GetLocation (const SBFrame &frame)
198{
199 const char *value_string = NULL;
200 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000201 value_string = m_opaque_sp->GetLocationAsCString(frame.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000202 return value_string;
203}
204
205bool
206SBValue::SetValueFromCString (const SBFrame &frame, const char *value_str)
207{
208 bool success = false;
209 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000210 success = m_opaque_sp->SetValueFromCString (frame.get(), value_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000211 return success;
212}
213
214SBValue
215SBValue::GetChildAtIndex (uint32_t idx)
216{
217 lldb::ValueObjectSP child_sp;
218
219 if (IsValid())
220 {
Greg Clayton63094e02010-06-23 01:19:29 +0000221 child_sp = m_opaque_sp->GetChildAtIndex (idx, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000222 }
223
224 SBValue sb_value (child_sp);
225 return sb_value;
226}
227
228uint32_t
229SBValue::GetIndexOfChildWithName (const char *name)
230{
231 if (IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +0000232 return m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
Chris Lattner24943d22010-06-08 16:52:24 +0000233 return UINT32_MAX;
234}
235
236SBValue
237SBValue::GetChildMemberWithName (const char *name)
238{
239 lldb::ValueObjectSP child_sp;
240 const ConstString str_name (name);
241
242 if (IsValid())
243 {
Greg Clayton63094e02010-06-23 01:19:29 +0000244 child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000245 }
246
247 SBValue sb_value (child_sp);
248 return sb_value;
249}
250
251
252uint32_t
253SBValue::GetNumChildren ()
254{
255 uint32_t num_children = 0;
256
257 if (IsValid())
258 {
Greg Clayton63094e02010-06-23 01:19:29 +0000259 num_children = m_opaque_sp->GetNumChildren();
Chris Lattner24943d22010-06-08 16:52:24 +0000260 }
261
262 return num_children;
263}
264
265bool
266SBValue::ValueIsStale ()
267{
268 bool result = true;
269
270 if (IsValid())
271 {
Greg Clayton63094e02010-06-23 01:19:29 +0000272 result = m_opaque_sp->GetValueIsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000273 }
274
275 return result;
276}
277
278
279SBValue
280SBValue::Dereference ()
281{
282 if (IsValid())
283 {
Greg Clayton63094e02010-06-23 01:19:29 +0000284 if (m_opaque_sp->IsPointerType())
Chris Lattner24943d22010-06-08 16:52:24 +0000285 {
286 return GetChildAtIndex(0);
287 }
288 }
289 return *this;
290}
291
292bool
293SBValue::TypeIsPtrType ()
294{
295 bool is_ptr_type = false;
296
297 if (IsValid())
298 {
Greg Clayton63094e02010-06-23 01:19:29 +0000299 is_ptr_type = m_opaque_sp->IsPointerType();
Chris Lattner24943d22010-06-08 16:52:24 +0000300 }
301
302 return is_ptr_type;
303}
304
Chris Lattner24943d22010-06-08 16:52:24 +0000305void *
306SBValue::GetOpaqueType()
307{
Greg Clayton63094e02010-06-23 01:19:29 +0000308 if (m_opaque_sp)
309 return m_opaque_sp->GetOpaqueClangQualType();
Chris Lattner24943d22010-06-08 16:52:24 +0000310 return NULL;
311}
312
313// Mimic shared pointer...
314lldb_private::ValueObject *
315SBValue::get() const
316{
Greg Clayton63094e02010-06-23 01:19:29 +0000317 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000318}
319
320lldb_private::ValueObject *
321SBValue::operator->() const
322{
Greg Clayton63094e02010-06-23 01:19:29 +0000323 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000324}
325
326lldb::ValueObjectSP &
327SBValue::operator*()
328{
Greg Clayton63094e02010-06-23 01:19:29 +0000329 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000330}
331
332const lldb::ValueObjectSP &
333SBValue::operator*() const
334{
Greg Clayton63094e02010-06-23 01:19:29 +0000335 return m_opaque_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000336}