blob: 90952691df0e6655cafcb36890806cbe92ddfc22 [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"
Caroline Tice98f930f2010-09-20 05:20:02 +000037#include "lldb/API/SBStream.h"
Eli Friedman7a62c8b2010-06-09 07:44:37 +000038#include "lldb/API/SBSymbolContext.h"
39#include "lldb/API/SBThread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000040
41using namespace lldb;
42using namespace lldb_private;
43
44SBFrame::SBFrame () :
Greg Clayton63094e02010-06-23 01:19:29 +000045 m_opaque_sp ()
Chris Lattner24943d22010-06-08 16:52:24 +000046{
47}
48
49SBFrame::SBFrame (const lldb::StackFrameSP &lldb_object_sp) :
Greg Clayton63094e02010-06-23 01:19:29 +000050 m_opaque_sp (lldb_object_sp)
Chris Lattner24943d22010-06-08 16:52:24 +000051{
52}
53
54SBFrame::~SBFrame()
55{
56}
57
58
59void
60SBFrame::SetFrame (const lldb::StackFrameSP &lldb_object_sp)
61{
Greg Clayton63094e02010-06-23 01:19:29 +000062 m_opaque_sp = lldb_object_sp;
Chris Lattner24943d22010-06-08 16:52:24 +000063}
64
65
66bool
67SBFrame::IsValid() const
68{
Greg Clayton63094e02010-06-23 01:19:29 +000069 return (m_opaque_sp.get() != NULL);
Chris Lattner24943d22010-06-08 16:52:24 +000070}
71
72SBSymbolContext
73SBFrame::GetSymbolContext (uint32_t resolve_scope) const
74{
75 SBSymbolContext sb_sym_ctx;
Greg Clayton63094e02010-06-23 01:19:29 +000076 if (m_opaque_sp)
77 sb_sym_ctx.SetSymbolContext(&m_opaque_sp->GetSymbolContext (resolve_scope));
Chris Lattner24943d22010-06-08 16:52:24 +000078 return sb_sym_ctx;
79}
80
81SBModule
82SBFrame::GetModule () const
83{
Greg Clayton63094e02010-06-23 01:19:29 +000084 SBModule sb_module (m_opaque_sp->GetSymbolContext (eSymbolContextModule).module_sp);
Chris Lattner24943d22010-06-08 16:52:24 +000085 return sb_module;
86}
87
88SBCompileUnit
89SBFrame::GetCompileUnit () const
90{
Greg Clayton63094e02010-06-23 01:19:29 +000091 SBCompileUnit sb_comp_unit(m_opaque_sp->GetSymbolContext (eSymbolContextCompUnit).comp_unit);
Chris Lattner24943d22010-06-08 16:52:24 +000092 return sb_comp_unit;
93}
94
95SBFunction
96SBFrame::GetFunction () const
97{
Greg Clayton63094e02010-06-23 01:19:29 +000098 SBFunction sb_function(m_opaque_sp->GetSymbolContext (eSymbolContextFunction).function);
Chris Lattner24943d22010-06-08 16:52:24 +000099 return sb_function;
100}
101
102SBBlock
103SBFrame::GetBlock () const
104{
Greg Clayton63094e02010-06-23 01:19:29 +0000105 SBBlock sb_block(m_opaque_sp->GetSymbolContext (eSymbolContextBlock).block);
Chris Lattner24943d22010-06-08 16:52:24 +0000106 return sb_block;
107}
108
Greg Clayton69aa5d92010-09-07 04:20:48 +0000109SBBlock
110SBFrame::GetFrameBlock () const
111{
112 SBBlock sb_block(m_opaque_sp->GetFrameBlock ());
113 return sb_block;
114}
115
Chris Lattner24943d22010-06-08 16:52:24 +0000116SBLineEntry
117SBFrame::GetLineEntry () const
118{
Greg Clayton63094e02010-06-23 01:19:29 +0000119 SBLineEntry sb_line_entry(&m_opaque_sp->GetSymbolContext (eSymbolContextLineEntry).line_entry);
Chris Lattner24943d22010-06-08 16:52:24 +0000120 return sb_line_entry;
121}
122
123uint32_t
124SBFrame::GetFrameID () const
125{
Greg Clayton63094e02010-06-23 01:19:29 +0000126 if (m_opaque_sp)
Greg Clayton33ed1702010-08-24 00:45:41 +0000127 return m_opaque_sp->GetFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000128 else
129 return UINT32_MAX;
130}
131
Chris Lattner24943d22010-06-08 16:52:24 +0000132lldb::addr_t
133SBFrame::GetPC () const
134{
Greg Clayton63094e02010-06-23 01:19:29 +0000135 if (m_opaque_sp)
Greg Claytoneea26402010-09-14 23:36:40 +0000136 return m_opaque_sp->GetFrameCodeAddress().GetLoadAddress (&m_opaque_sp->GetThread().GetProcess().GetTarget());
Chris Lattner24943d22010-06-08 16:52:24 +0000137 return LLDB_INVALID_ADDRESS;
138}
139
140bool
141SBFrame::SetPC (lldb::addr_t new_pc)
142{
Greg Clayton63094e02010-06-23 01:19:29 +0000143 if (m_opaque_sp)
144 return m_opaque_sp->GetRegisterContext()->SetPC (new_pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000145 return false;
146}
147
148lldb::addr_t
149SBFrame::GetSP () const
150{
Greg Clayton63094e02010-06-23 01:19:29 +0000151 if (m_opaque_sp)
152 return m_opaque_sp->GetRegisterContext()->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000153 return LLDB_INVALID_ADDRESS;
154}
155
156
157lldb::addr_t
158SBFrame::GetFP () const
159{
Greg Clayton63094e02010-06-23 01:19:29 +0000160 if (m_opaque_sp)
161 return m_opaque_sp->GetRegisterContext()->GetFP();
Chris Lattner24943d22010-06-08 16:52:24 +0000162 return LLDB_INVALID_ADDRESS;
163}
164
165
166SBAddress
167SBFrame::GetPCAddress () const
168{
169 SBAddress sb_addr;
Greg Clayton63094e02010-06-23 01:19:29 +0000170 if (m_opaque_sp)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000171 sb_addr.SetAddress (&m_opaque_sp->GetFrameCodeAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000172 return sb_addr;
173}
174
175void
176SBFrame::Clear()
177{
Greg Clayton63094e02010-06-23 01:19:29 +0000178 m_opaque_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000179}
180
181SBValue
182SBFrame::LookupVar (const char *var_name)
183{
184 lldb::VariableSP var_sp;
185 if (IsValid ())
186 {
187 lldb_private::VariableList variable_list;
188 SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything);
189
190 SBBlock block = sc.GetBlock();
191 if (block.IsValid())
192 block.AppendVariables (true, true, &variable_list);
193
194 const uint32_t num_variables = variable_list.GetSize();
195
196 bool found = false;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000197 for (uint32_t i = 0; i < num_variables && !found; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000198 {
199 var_sp = variable_list.GetVariableAtIndex(i);
200 if (var_sp
201 && (var_sp.get()->GetName() == lldb_private::ConstString(var_name)))
202 found = true;
203 }
204 if (!found)
205 var_sp.reset();
206 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000207 if (var_sp)
208 {
209 SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp)));
210 return sb_value;
211 }
212
213 SBValue sb_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000214 return sb_value;
215}
216
217SBValue
218SBFrame::LookupVarInScope (const char *var_name, const char *scope)
219{
220 lldb::VariableSP var_sp;
221 if (IsValid())
222 {
223 std::string scope_str = scope;
224 lldb::ValueType var_scope = eValueTypeInvalid;
225 // Convert scope_str to be all lowercase;
226 std::transform (scope_str.begin(), scope_str.end(), scope_str.begin(), ::tolower);
227
228 if (scope_str.compare ("global") == 0)
229 var_scope = eValueTypeVariableGlobal;
230 else if (scope_str.compare ("local") == 0)
231 var_scope = eValueTypeVariableLocal;
232 else if (scope_str.compare ("parameter") == 0)
233 var_scope = eValueTypeVariableArgument;
234
235 if (var_scope != eValueTypeInvalid)
236 {
237 lldb_private::VariableList variable_list;
238 SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything);
239
240 SBBlock block = sc.GetBlock();
241 if (block.IsValid())
242 block.AppendVariables (true, true, &variable_list);
243
244 const uint32_t num_variables = variable_list.GetSize();
245
246 bool found = false;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000247 for (uint32_t i = 0; i < num_variables && !found; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000248 {
249 var_sp = variable_list.GetVariableAtIndex(i);
250 if (var_sp
251 && (var_sp.get()->GetName() == lldb_private::ConstString(var_name))
252 && var_sp.get()->GetScope() == var_scope)
253 found = true;
254 }
255 if (!found)
256 var_sp.reset();
257 }
258 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000259
260 if (var_sp)
261 {
262 SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp)));
263 return sb_value;
264 }
265
266 SBValue sb_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000267 return sb_value;
268}
269
270bool
271SBFrame::operator == (const SBFrame &rhs) const
272{
Greg Clayton63094e02010-06-23 01:19:29 +0000273 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000274}
275
276bool
277SBFrame::operator != (const SBFrame &rhs) const
278{
Greg Clayton63094e02010-06-23 01:19:29 +0000279 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000280}
281
282lldb_private::StackFrame *
283SBFrame::operator->() const
284{
Greg Clayton63094e02010-06-23 01:19:29 +0000285 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000286}
287
288lldb_private::StackFrame *
289SBFrame::get() const
290{
Greg Clayton63094e02010-06-23 01:19:29 +0000291 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000292}
293
294
295SBThread
296SBFrame::GetThread () const
297{
Greg Clayton63094e02010-06-23 01:19:29 +0000298 SBThread sb_thread (m_opaque_sp->GetThread().GetSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000299 return sb_thread;
300}
301
302const char *
303SBFrame::Disassemble () const
304{
Greg Clayton63094e02010-06-23 01:19:29 +0000305 if (m_opaque_sp)
306 return m_opaque_sp->Disassemble();
Chris Lattner24943d22010-06-08 16:52:24 +0000307 return NULL;
308}
309
310
311
312lldb_private::StackFrame *
313SBFrame::GetLLDBObjectPtr ()
314{
Greg Clayton63094e02010-06-23 01:19:29 +0000315 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000316}
317
318SBValueList
319SBFrame::GetVariables (bool arguments,
320 bool locals,
321 bool statics,
322 bool in_scope_only)
323{
324 SBValueList value_list;
Greg Clayton63094e02010-06-23 01:19:29 +0000325 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000326 {
327 size_t i;
Greg Clayton17dae082010-09-02 02:59:18 +0000328 VariableList *variable_list = m_opaque_sp->GetVariableList(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000329 if (variable_list)
330 {
331 const size_t num_variables = variable_list->GetSize();
332 if (num_variables)
333 {
334 for (i = 0; i < num_variables; ++i)
335 {
336 VariableSP variable_sp (variable_list->GetVariableAtIndex(i));
337 if (variable_sp)
338 {
339 bool add_variable = false;
340 switch (variable_sp->GetScope())
341 {
342 case eValueTypeVariableGlobal:
343 case eValueTypeVariableStatic:
344 add_variable = statics;
345 break;
346
347 case eValueTypeVariableArgument:
348 add_variable = arguments;
349 break;
350
351 case eValueTypeVariableLocal:
352 add_variable = locals;
353 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000354
355 default:
356 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000357 }
358 if (add_variable)
359 {
Greg Clayton63094e02010-06-23 01:19:29 +0000360 if (in_scope_only && !variable_sp->IsInScope(m_opaque_sp.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000361 continue;
362
Greg Clayton17dae082010-09-02 02:59:18 +0000363 value_list.Append(m_opaque_sp->GetValueObjectForFrameVariable (variable_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000364 }
365 }
366 }
367 }
Greg Clayton17dae082010-09-02 02:59:18 +0000368 }
Chris Lattner24943d22010-06-08 16:52:24 +0000369 }
370 return value_list;
371}
372
373lldb::SBValueList
374SBFrame::GetRegisters ()
375{
376 SBValueList value_list;
Greg Clayton63094e02010-06-23 01:19:29 +0000377 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000378 {
Greg Clayton63094e02010-06-23 01:19:29 +0000379 RegisterContext *reg_ctx = m_opaque_sp->GetRegisterContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000380 if (reg_ctx)
381 {
382 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
383 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx)
384 {
385 value_list.Append(ValueObjectSP (new ValueObjectRegisterSet (reg_ctx, set_idx)));
386 }
387 }
388 }
389 return value_list;
390}
391
Caroline Tice98f930f2010-09-20 05:20:02 +0000392bool
393SBFrame::GetDescription (SBStream &description)
394{
395 if (m_opaque_sp)
396 {
397 m_opaque_sp->Dump (description.get(), true, false);
398 }
399 else
400 description.Printf ("No value");
401
402 return true;
403}
404
405PyObject *
406SBFrame::__repr__ ()
407{
408 SBStream description;
409 description.ref();
410 GetDescription (description);
411 return PyString_FromString (description.GetData());
412}