blob: 425185488f92e10763ff3b4459e8d17d2267db73 [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
Greg Clayton8f1e08b2010-10-04 18:37:52 +0000102SBSymbol
103SBFrame::GetSymbol () const
104{
105 SBSymbol sb_symbol(m_opaque_sp->GetSymbolContext (eSymbolContextSymbol).symbol);
106 return sb_symbol;
107}
108
Chris Lattner24943d22010-06-08 16:52:24 +0000109SBBlock
110SBFrame::GetBlock () const
111{
Greg Clayton63094e02010-06-23 01:19:29 +0000112 SBBlock sb_block(m_opaque_sp->GetSymbolContext (eSymbolContextBlock).block);
Chris Lattner24943d22010-06-08 16:52:24 +0000113 return sb_block;
114}
115
Greg Clayton69aa5d92010-09-07 04:20:48 +0000116SBBlock
117SBFrame::GetFrameBlock () const
118{
119 SBBlock sb_block(m_opaque_sp->GetFrameBlock ());
120 return sb_block;
121}
122
Chris Lattner24943d22010-06-08 16:52:24 +0000123SBLineEntry
124SBFrame::GetLineEntry () const
125{
Greg Clayton63094e02010-06-23 01:19:29 +0000126 SBLineEntry sb_line_entry(&m_opaque_sp->GetSymbolContext (eSymbolContextLineEntry).line_entry);
Chris Lattner24943d22010-06-08 16:52:24 +0000127 return sb_line_entry;
128}
129
130uint32_t
131SBFrame::GetFrameID () const
132{
Greg Clayton63094e02010-06-23 01:19:29 +0000133 if (m_opaque_sp)
Greg Clayton33ed1702010-08-24 00:45:41 +0000134 return m_opaque_sp->GetFrameIndex ();
Chris Lattner24943d22010-06-08 16:52:24 +0000135 else
136 return UINT32_MAX;
137}
138
Chris Lattner24943d22010-06-08 16:52:24 +0000139lldb::addr_t
140SBFrame::GetPC () const
141{
Greg Clayton63094e02010-06-23 01:19:29 +0000142 if (m_opaque_sp)
Greg Claytoneea26402010-09-14 23:36:40 +0000143 return m_opaque_sp->GetFrameCodeAddress().GetLoadAddress (&m_opaque_sp->GetThread().GetProcess().GetTarget());
Chris Lattner24943d22010-06-08 16:52:24 +0000144 return LLDB_INVALID_ADDRESS;
145}
146
147bool
148SBFrame::SetPC (lldb::addr_t new_pc)
149{
Greg Clayton63094e02010-06-23 01:19:29 +0000150 if (m_opaque_sp)
151 return m_opaque_sp->GetRegisterContext()->SetPC (new_pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000152 return false;
153}
154
155lldb::addr_t
156SBFrame::GetSP () const
157{
Greg Clayton63094e02010-06-23 01:19:29 +0000158 if (m_opaque_sp)
159 return m_opaque_sp->GetRegisterContext()->GetSP();
Chris Lattner24943d22010-06-08 16:52:24 +0000160 return LLDB_INVALID_ADDRESS;
161}
162
163
164lldb::addr_t
165SBFrame::GetFP () const
166{
Greg Clayton63094e02010-06-23 01:19:29 +0000167 if (m_opaque_sp)
168 return m_opaque_sp->GetRegisterContext()->GetFP();
Chris Lattner24943d22010-06-08 16:52:24 +0000169 return LLDB_INVALID_ADDRESS;
170}
171
172
173SBAddress
174SBFrame::GetPCAddress () const
175{
176 SBAddress sb_addr;
Greg Clayton63094e02010-06-23 01:19:29 +0000177 if (m_opaque_sp)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000178 sb_addr.SetAddress (&m_opaque_sp->GetFrameCodeAddress());
Chris Lattner24943d22010-06-08 16:52:24 +0000179 return sb_addr;
180}
181
182void
183SBFrame::Clear()
184{
Greg Clayton63094e02010-06-23 01:19:29 +0000185 m_opaque_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
188SBValue
189SBFrame::LookupVar (const char *var_name)
190{
191 lldb::VariableSP var_sp;
192 if (IsValid ())
193 {
194 lldb_private::VariableList variable_list;
195 SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything);
196
197 SBBlock block = sc.GetBlock();
198 if (block.IsValid())
199 block.AppendVariables (true, true, &variable_list);
200
201 const uint32_t num_variables = variable_list.GetSize();
202
203 bool found = false;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000204 for (uint32_t i = 0; i < num_variables && !found; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000205 {
206 var_sp = variable_list.GetVariableAtIndex(i);
207 if (var_sp
208 && (var_sp.get()->GetName() == lldb_private::ConstString(var_name)))
209 found = true;
210 }
211 if (!found)
212 var_sp.reset();
213 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000214 if (var_sp)
215 {
216 SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp)));
217 return sb_value;
218 }
219
220 SBValue sb_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000221 return sb_value;
222}
223
224SBValue
225SBFrame::LookupVarInScope (const char *var_name, const char *scope)
226{
227 lldb::VariableSP var_sp;
228 if (IsValid())
229 {
230 std::string scope_str = scope;
231 lldb::ValueType var_scope = eValueTypeInvalid;
232 // Convert scope_str to be all lowercase;
233 std::transform (scope_str.begin(), scope_str.end(), scope_str.begin(), ::tolower);
234
235 if (scope_str.compare ("global") == 0)
236 var_scope = eValueTypeVariableGlobal;
237 else if (scope_str.compare ("local") == 0)
238 var_scope = eValueTypeVariableLocal;
239 else if (scope_str.compare ("parameter") == 0)
240 var_scope = eValueTypeVariableArgument;
241
242 if (var_scope != eValueTypeInvalid)
243 {
244 lldb_private::VariableList variable_list;
245 SBSymbolContext sc = GetSymbolContext (eSymbolContextEverything);
246
247 SBBlock block = sc.GetBlock();
248 if (block.IsValid())
249 block.AppendVariables (true, true, &variable_list);
250
251 const uint32_t num_variables = variable_list.GetSize();
252
253 bool found = false;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000254 for (uint32_t i = 0; i < num_variables && !found; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000255 {
256 var_sp = variable_list.GetVariableAtIndex(i);
257 if (var_sp
258 && (var_sp.get()->GetName() == lldb_private::ConstString(var_name))
259 && var_sp.get()->GetScope() == var_scope)
260 found = true;
261 }
262 if (!found)
263 var_sp.reset();
264 }
265 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000266
267 if (var_sp)
268 {
269 SBValue sb_value (ValueObjectSP (new ValueObjectVariable (var_sp)));
270 return sb_value;
271 }
272
273 SBValue sb_value;
Chris Lattner24943d22010-06-08 16:52:24 +0000274 return sb_value;
275}
276
277bool
278SBFrame::operator == (const SBFrame &rhs) const
279{
Greg Clayton63094e02010-06-23 01:19:29 +0000280 return m_opaque_sp.get() == rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000281}
282
283bool
284SBFrame::operator != (const SBFrame &rhs) const
285{
Greg Clayton63094e02010-06-23 01:19:29 +0000286 return m_opaque_sp.get() != rhs.m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000287}
288
289lldb_private::StackFrame *
290SBFrame::operator->() const
291{
Greg Clayton63094e02010-06-23 01:19:29 +0000292 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000293}
294
295lldb_private::StackFrame *
296SBFrame::get() const
297{
Greg Clayton63094e02010-06-23 01:19:29 +0000298 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000299}
300
301
302SBThread
303SBFrame::GetThread () const
304{
Greg Clayton63094e02010-06-23 01:19:29 +0000305 SBThread sb_thread (m_opaque_sp->GetThread().GetSP());
Chris Lattner24943d22010-06-08 16:52:24 +0000306 return sb_thread;
307}
308
309const char *
310SBFrame::Disassemble () const
311{
Greg Clayton63094e02010-06-23 01:19:29 +0000312 if (m_opaque_sp)
313 return m_opaque_sp->Disassemble();
Chris Lattner24943d22010-06-08 16:52:24 +0000314 return NULL;
315}
316
317
318
319lldb_private::StackFrame *
320SBFrame::GetLLDBObjectPtr ()
321{
Greg Clayton63094e02010-06-23 01:19:29 +0000322 return m_opaque_sp.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000323}
324
325SBValueList
326SBFrame::GetVariables (bool arguments,
327 bool locals,
328 bool statics,
329 bool in_scope_only)
330{
331 SBValueList value_list;
Greg Clayton63094e02010-06-23 01:19:29 +0000332 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000333 {
334 size_t i;
Greg Clayton17dae082010-09-02 02:59:18 +0000335 VariableList *variable_list = m_opaque_sp->GetVariableList(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000336 if (variable_list)
337 {
338 const size_t num_variables = variable_list->GetSize();
339 if (num_variables)
340 {
341 for (i = 0; i < num_variables; ++i)
342 {
343 VariableSP variable_sp (variable_list->GetVariableAtIndex(i));
344 if (variable_sp)
345 {
346 bool add_variable = false;
347 switch (variable_sp->GetScope())
348 {
349 case eValueTypeVariableGlobal:
350 case eValueTypeVariableStatic:
351 add_variable = statics;
352 break;
353
354 case eValueTypeVariableArgument:
355 add_variable = arguments;
356 break;
357
358 case eValueTypeVariableLocal:
359 add_variable = locals;
360 break;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000361
362 default:
363 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000364 }
365 if (add_variable)
366 {
Greg Clayton63094e02010-06-23 01:19:29 +0000367 if (in_scope_only && !variable_sp->IsInScope(m_opaque_sp.get()))
Chris Lattner24943d22010-06-08 16:52:24 +0000368 continue;
369
Greg Clayton17dae082010-09-02 02:59:18 +0000370 value_list.Append(m_opaque_sp->GetValueObjectForFrameVariable (variable_sp));
Chris Lattner24943d22010-06-08 16:52:24 +0000371 }
372 }
373 }
374 }
Greg Clayton17dae082010-09-02 02:59:18 +0000375 }
Chris Lattner24943d22010-06-08 16:52:24 +0000376 }
377 return value_list;
378}
379
380lldb::SBValueList
381SBFrame::GetRegisters ()
382{
383 SBValueList value_list;
Greg Clayton63094e02010-06-23 01:19:29 +0000384 if (m_opaque_sp)
Chris Lattner24943d22010-06-08 16:52:24 +0000385 {
Greg Clayton63094e02010-06-23 01:19:29 +0000386 RegisterContext *reg_ctx = m_opaque_sp->GetRegisterContext();
Chris Lattner24943d22010-06-08 16:52:24 +0000387 if (reg_ctx)
388 {
389 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
390 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx)
391 {
392 value_list.Append(ValueObjectSP (new ValueObjectRegisterSet (reg_ctx, set_idx)));
393 }
394 }
395 }
396 return value_list;
397}
398
Caroline Tice98f930f2010-09-20 05:20:02 +0000399bool
400SBFrame::GetDescription (SBStream &description)
401{
402 if (m_opaque_sp)
403 {
Caroline Ticee49ec182010-09-22 23:01:29 +0000404 description.ref();
Greg Claytona830adb2010-10-04 01:05:56 +0000405 m_opaque_sp->DumpUsingSettingsFormat (description.get());
Caroline Tice98f930f2010-09-20 05:20:02 +0000406 }
407 else
408 description.Printf ("No value");
409
410 return true;
411}