blob: ce294cafa7b4ebef7332e4153cdb6c4c81ace8d1 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBFrame.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/SBFrame.h"
Chris Lattner24943d22010-06-08 16:52:24 +000011
12#include <string>
13#include <algorithm>
14
15#include "lldb/lldb-types.h"
16
17#include "lldb/Core/Address.h"
18#include "lldb/Core/ConstString.h"
19#include "lldb/Core/Stream.h"
20#include "lldb/Core/StreamFile.h"
21#include "lldb/Core/ValueObjectRegister.h"
22#include "lldb/Core/ValueObjectVariable.h"
23#include "lldb/Symbol/Block.h"
24#include "lldb/Symbol/SymbolContext.h"
25#include "lldb/Symbol/VariableList.h"
26#include "lldb/Symbol/Variable.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Target.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/RegisterContext.h"
31#include "lldb/Target/StackFrame.h"
32#include "lldb/Target/Thread.h"
33
Eli Friedman7a62c8b2010-06-09 07:44:37 +000034#include "lldb/API/SBDebugger.h"
35#include "lldb/API/SBValue.h"
36#include "lldb/API/SBAddress.h"
37#include "lldb/API/SBSymbolContext.h"
38#include "lldb/API/SBThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
43SBFrame::SBFrame () :
Greg Clayton63094e02010-06-23 01:19:29 +000044 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000045{
46}
47
48SBFrame::SBFrame (const lldb::StackFrameSP &lldb_object_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000049 m_opaque_sp (lldb_object_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000050{
51}
52
53SBFrame::~SBFrame()
54{
55}
56
57
58void
59SBFrame::SetFrame (const lldb::StackFrameSP &lldb_object_sp)
60{
Greg Clayton63094e02010-06-23 01:19:29 +000061 m_opaque_sp = lldb_object_sp;
Chris Lattner24943d22010-06-08 16:52:24 +000062}
63
64
65bool
66SBFrame::IsValid() const
67{
Greg Clayton63094e02010-06-23 01:19:29 +000068 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000069}
70
71SBSymbolContext
72SBFrame::GetSymbolContext (uint32_t resolve_scope) const
73{
74 SBSymbolContext sb_sym_ctx;
Greg Clayton63094e02010-06-23 01:19:29 +000075 if (m_opaque_sp)
76 sb_sym_ctx.SetSymbolContext(&m_opaque_sp->GetSymbolContext (resolve_scope));
Chris Lattner24943d22010-06-08 16:52:24 +000077 return sb_sym_ctx;
78}
79
80SBModule
81SBFrame::GetModule () const
82{
Greg Clayton63094e02010-06-23 01:19:29 +000083 SBModule sb_module (m_opaque_sp->GetSymbolContext (eSymbolContextModule).module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +000084 return sb_module;
85}
86
87SBCompileUnit
88SBFrame::GetCompileUnit () const
89{
Greg Clayton63094e02010-06-23 01:19:29 +000090 SBCompileUnit sb_comp_unit(m_opaque_sp->GetSymbolContext (eSymbolContextCompUnit).comp_unit);
Chris Lattner24943d22010-06-08 16:52:24 +000091 return sb_comp_unit;
92}
93
94SBFunction
95SBFrame::GetFunction () const
96{
Greg Clayton63094e02010-06-23 01:19:29 +000097 SBFunction sb_function(m_opaque_sp->GetSymbolContext (eSymbolContextFunction).function);
Chris Lattner24943d22010-06-08 16:52:24 +000098 return sb_function;
99}
100
101SBBlock
102SBFrame::GetBlock () const
103{
Greg Clayton63094e02010-06-23 01:19:29 +0000104 SBBlock sb_block(m_opaque_sp->GetSymbolContext (eSymbolContextBlock).block);
Chris Lattner24943d22010-06-08 16:52:24 +0000105 return sb_block;
106}
107
108SBLineEntry
109SBFrame::GetLineEntry () const
110{
Greg Clayton63094e02010-06-23 01:19:29 +0000111 SBLineEntry sb_line_entry(&m_opaque_sp->GetSymbolContext (eSymbolContextLineEntry).line_entry);
Chris Lattner24943d22010-06-08 16:52:24 +0000112 return sb_line_entry;
113}
114
115uint32_t
116SBFrame::GetFrameID () const
117{
Greg Clayton63094e02010-06-23 01:19:29 +0000118 if (m_opaque_sp)
Greg Clayton33ed1702010-08-24 00:45:41 +0000119 return m_opaque_sp->GetFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000120 else
121 return UINT32_MAX;
122}
123
Chris Lattner24943d22010-06-08 16:52:24 +0000124lldb::addr_t
125SBFrame::GetPC () const
126{
Greg Clayton63094e02010-06-23 01:19:29 +0000127 if (m_opaque_sp)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000128 return m_opaque_sp->GetFrameCodeAddress().GetLoadAddress (&m_opaque_sp->GetThread().GetProcess());
Chris Lattner24943d22010-06-08 16:52:24 +0000129 return LLDB_INVALID_ADDRESS;
130}
131
132bool
133SBFrame::SetPC (lldb::addr_t new_pc)
134{
Greg Clayton63094e02010-06-23 01:19:29 +0000135 if (m_opaque_sp)
136 return m_opaque_sp->GetRegisterContext()->SetPC (new_pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000137 return false;
138}
139
140lldb::addr_t
141SBFrame::GetSP () const
142{
Greg Clayton63094e02010-06-23 01:19:29 +0000143 if (m_opaque_sp)
144 return m_opaque_sp->GetRegisterContext()->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000145 return LLDB_INVALID_ADDRESS;
146}
147
148
149lldb::addr_t
150SBFrame::GetFP () const
151{
Greg Clayton63094e02010-06-23 01:19:29 +0000152 if (m_opaque_sp)
153 return m_opaque_sp->GetRegisterContext()->GetFP();
Chris Lattner24943d22010-06-08 16:52:24 +0000154 return LLDB_INVALID_ADDRESS;
155}
156
157
158SBAddress
159SBFrame::GetPCAddress () const
160{
161 SBAddress sb_addr;
Greg Clayton63094e02010-06-23 01:19:29 +0000162 if (m_opaque_sp)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000163 sb_addr.SetAddress (&m_opaque_sp->GetFrameCodeAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000164 return sb_addr;
165}
166
167void
168SBFrame::Clear()
169{
Greg Clayton63094e02010-06-23 01:19:29 +0000170 m_opaque_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000171}
172
173SBValue
174SBFrame::LookupVar (const char *var_name)
175{
176 lldb::VariableSP var_sp;
177 if (IsValid ())
178 {
179 lldb_private::VariableList variable_list;
180 SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything);
181
182 SBBlock block = sc.GetBlock();
183 if (block.IsValid())
184 block.AppendVariables (true, true, &variable_list);
185
186 const uint32_t num_variables = variable_list.GetSize();
187
188 bool found = false;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000189 for (uint32_t i = 0; i < num_variables && !found; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000190 {
191 var_sp = variable_list.GetVariableAtIndex(i);
192 if (var_sp
193 && (var_sp.get()->GetName() == lldb_private::ConstString(var_name)))
194 found = true;
195 }
196 if (!found)
197 var_sp.reset();
198 }
199 SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp)));
200 return sb_value;
201}
202
203SBValue
204SBFrame::LookupVarInScope (const char *var_name, const char *scope)
205{
206 lldb::VariableSP var_sp;
207 if (IsValid())
208 {
209 std::string scope_str = scope;
210 lldb::ValueType var_scope = eValueTypeInvalid;
211 // Convert scope_str to be all lowercase;
212 std::transform (scope_str.begin(), scope_str.end(), scope_str.begin(), ::tolower);
213
214 if (scope_str.compare ("global") == 0)
215 var_scope = eValueTypeVariableGlobal;
216 else if (scope_str.compare ("local") == 0)
217 var_scope = eValueTypeVariableLocal;
218 else if (scope_str.compare ("parameter") == 0)
219 var_scope = eValueTypeVariableArgument;
220
221 if (var_scope != eValueTypeInvalid)
222 {
223 lldb_private::VariableList variable_list;
224 SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything);
225
226 SBBlock block = sc.GetBlock();
227 if (block.IsValid())
228 block.AppendVariables (true, true, &variable_list);
229
230 const uint32_t num_variables = variable_list.GetSize();
231
232 bool found = false;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000233 for (uint32_t i = 0; i < num_variables && !found; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000234 {
235 var_sp = variable_list.GetVariableAtIndex(i);
236 if (var_sp
237 && (var_sp.get()->GetName() == lldb_private::ConstString(var_name))
238 && var_sp.get()->GetScope() == var_scope)
239 found = true;
240 }
241 if (!found)
242 var_sp.reset();
243 }
244 }
245 SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp)));
246 return sb_value;
247}
248
249bool
250SBFrame::operator == (const SBFrame &rhs) const
251{
Greg Clayton63094e02010-06-23 01:19:29 +0000252 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000253}
254
255bool
256SBFrame::operator != (const SBFrame &rhs) const
257{
Greg Clayton63094e02010-06-23 01:19:29 +0000258 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000259}
260
261lldb_private::StackFrame *
262SBFrame::operator->() const
263{
Greg Clayton63094e02010-06-23 01:19:29 +0000264 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000265}
266
267lldb_private::StackFrame *
268SBFrame::get() const
269{
Greg Clayton63094e02010-06-23 01:19:29 +0000270 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000271}
272
273
274SBThread
275SBFrame::GetThread () const
276{
Greg Clayton63094e02010-06-23 01:19:29 +0000277 SBThread sb_thread (m_opaque_sp->GetThread().GetSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000278 return sb_thread;
279}
280
281const char *
282SBFrame::Disassemble () const
283{
Greg Clayton63094e02010-06-23 01:19:29 +0000284 if (m_opaque_sp)
285 return m_opaque_sp->Disassemble();
Chris Lattner24943d22010-06-08 16:52:24 +0000286 return NULL;
287}
288
289
290
291lldb_private::StackFrame *
292SBFrame::GetLLDBObjectPtr ()
293{
Greg Clayton63094e02010-06-23 01:19:29 +0000294 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000295}
296
297SBValueList
298SBFrame::GetVariables (bool arguments,
299 bool locals,
300 bool statics,
301 bool in_scope_only)
302{
303 SBValueList value_list;
Greg Clayton63094e02010-06-23 01:19:29 +0000304 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000305 {
306 size_t i;
Greg Clayton17dae082010-09-02 02:59:18 +0000307 VariableList *variable_list = m_opaque_sp->GetVariableList(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000308 if (variable_list)
309 {
310 const size_t num_variables = variable_list->GetSize();
311 if (num_variables)
312 {
313 for (i = 0; i < num_variables; ++i)
314 {
315 VariableSP variable_sp (variable_list->GetVariableAtIndex(i));
316 if (variable_sp)
317 {
318 bool add_variable = false;
319 switch (variable_sp->GetScope())
320 {
321 case eValueTypeVariableGlobal:
322 case eValueTypeVariableStatic:
323 add_variable = statics;
324 break;
325
326 case eValueTypeVariableArgument:
327 add_variable = arguments;
328 break;
329
330 case eValueTypeVariableLocal:
331 add_variable = locals;
332 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000333
334 default:
335 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000336 }
337 if (add_variable)
338 {
Greg Clayton63094e02010-06-23 01:19:29 +0000339 if (in_scope_only && !variable_sp->IsInScope(m_opaque_sp.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000340 continue;
341
Greg Clayton17dae082010-09-02 02:59:18 +0000342 value_list.Append(m_opaque_sp->GetValueObjectForFrameVariable (variable_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000343 }
344 }
345 }
346 }
Greg Clayton17dae082010-09-02 02:59:18 +0000347 }
Chris Lattner24943d22010-06-08 16:52:24 +0000348 }
349 return value_list;
350}
351
352lldb::SBValueList
353SBFrame::GetRegisters ()
354{
355 SBValueList value_list;
Greg Clayton63094e02010-06-23 01:19:29 +0000356 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000357 {
Greg Clayton63094e02010-06-23 01:19:29 +0000358 RegisterContext *reg_ctx = m_opaque_sp->GetRegisterContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000359 if (reg_ctx)
360 {
361 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
362 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx)
363 {
364 value_list.Append(ValueObjectSP (new ValueObjectRegisterSet (reg_ctx, set_idx)));
365 }
366 }
367 }
368 return value_list;
369}
370