blob: 0e6b02f5656e11fdc111d6e899ff0ccb40c248fd [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- StackFrame.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
10#include "lldb/Target/StackFrame.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/Module.h"
Greg Claytona830adb2010-10-04 01:05:56 +000017#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Core/Disassembler.h"
19#include "lldb/Core/Value.h"
Greg Clayton17dae082010-09-02 02:59:18 +000020#include "lldb/Core/ValueObjectVariable.h"
Greg Claytonc3b61d22010-12-15 05:08:08 +000021#include "lldb/Core/ValueObjectConstResult.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Symbol/Function.h"
Greg Clayton17dae082010-09-02 02:59:18 +000023#include "lldb/Symbol/VariableList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/ExecutionContext.h"
25#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
27#include "lldb/Target/Target.h"
28#include "lldb/Target/Thread.h"
29
30using namespace lldb;
31using namespace lldb_private;
32
33// The first bits in the flags are reserved for the SymbolContext::Scope bits
34// so we know if we have tried to look up information in our internal symbol
35// context (m_sc) already.
Greg Clayton4fb08152010-08-30 18:11:35 +000036#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
Greg Clayton72b71582010-09-02 21:44:10 +000037#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
Greg Clayton4fb08152010-08-30 18:11:35 +000038#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
39#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
Sean Callanan89363592010-11-01 04:38:59 +000040#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner24943d22010-06-08 16:52:24 +000041
Greg Clayton33ed1702010-08-24 00:45:41 +000042StackFrame::StackFrame
43(
44 lldb::user_id_t frame_idx,
Greg Clayton4fb08152010-08-30 18:11:35 +000045 lldb::user_id_t unwind_frame_index,
Greg Clayton33ed1702010-08-24 00:45:41 +000046 Thread &thread,
47 lldb::addr_t cfa,
Greg Clayton33ed1702010-08-24 00:45:41 +000048 lldb::addr_t pc,
49 const SymbolContext *sc_ptr
50) :
Greg Claytonbdcb6ab2011-01-25 23:55:37 +000051 m_thread (thread),
Greg Clayton33ed1702010-08-24 00:45:41 +000052 m_frame_index (frame_idx),
Greg Clayton08d7d3a2011-01-06 22:15:06 +000053 m_concrete_frame_index (unwind_frame_index),
Greg Clayton33ed1702010-08-24 00:45:41 +000054 m_reg_context_sp (),
Greg Clayton72b71582010-09-02 21:44:10 +000055 m_id (pc, cfa, NULL),
Greg Clayton65124ea2010-08-26 22:05:43 +000056 m_frame_code_addr (NULL, pc),
Greg Clayton33ed1702010-08-24 00:45:41 +000057 m_sc (),
58 m_flags (),
59 m_frame_base (),
60 m_frame_base_error (),
Chris Lattner24943d22010-06-08 16:52:24 +000061 m_variable_list_sp (),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +000062 m_variable_list_value_objects (),
63 m_disassembly ()
Chris Lattner24943d22010-06-08 16:52:24 +000064{
65 if (sc_ptr != NULL)
Greg Clayton33ed1702010-08-24 00:45:41 +000066 {
Chris Lattner24943d22010-06-08 16:52:24 +000067 m_sc = *sc_ptr;
Greg Clayton33ed1702010-08-24 00:45:41 +000068 m_flags.Set(m_sc.GetResolvedMask ());
69 }
Chris Lattner24943d22010-06-08 16:52:24 +000070}
71
Greg Clayton33ed1702010-08-24 00:45:41 +000072StackFrame::StackFrame
73(
74 lldb::user_id_t frame_idx,
Greg Clayton4fb08152010-08-30 18:11:35 +000075 lldb::user_id_t unwind_frame_index,
Greg Clayton33ed1702010-08-24 00:45:41 +000076 Thread &thread,
77 const RegisterContextSP &reg_context_sp,
78 lldb::addr_t cfa,
Greg Clayton33ed1702010-08-24 00:45:41 +000079 lldb::addr_t pc,
80 const SymbolContext *sc_ptr
81) :
Greg Claytonbdcb6ab2011-01-25 23:55:37 +000082 m_thread (thread),
Greg Clayton33ed1702010-08-24 00:45:41 +000083 m_frame_index (frame_idx),
Greg Clayton08d7d3a2011-01-06 22:15:06 +000084 m_concrete_frame_index (unwind_frame_index),
Greg Clayton33ed1702010-08-24 00:45:41 +000085 m_reg_context_sp (reg_context_sp),
Greg Clayton72b71582010-09-02 21:44:10 +000086 m_id (pc, cfa, NULL),
Greg Clayton65124ea2010-08-26 22:05:43 +000087 m_frame_code_addr (NULL, pc),
Greg Clayton33ed1702010-08-24 00:45:41 +000088 m_sc (),
89 m_flags (),
90 m_frame_base (),
91 m_frame_base_error (),
Chris Lattner24943d22010-06-08 16:52:24 +000092 m_variable_list_sp (),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +000093 m_variable_list_value_objects (),
94 m_disassembly ()
Chris Lattner24943d22010-06-08 16:52:24 +000095{
96 if (sc_ptr != NULL)
Greg Clayton33ed1702010-08-24 00:45:41 +000097 {
Chris Lattner24943d22010-06-08 16:52:24 +000098 m_sc = *sc_ptr;
Greg Clayton33ed1702010-08-24 00:45:41 +000099 m_flags.Set(m_sc.GetResolvedMask ());
100 }
101
102 if (reg_context_sp && !m_sc.target_sp)
103 {
104 m_sc.target_sp = reg_context_sp->GetThread().GetProcess().GetTarget().GetSP();
105 m_flags.Set (eSymbolContextTarget);
106 }
107}
108
109StackFrame::StackFrame
110(
111 lldb::user_id_t frame_idx,
Greg Clayton4fb08152010-08-30 18:11:35 +0000112 lldb::user_id_t unwind_frame_index,
Greg Clayton33ed1702010-08-24 00:45:41 +0000113 Thread &thread,
114 const RegisterContextSP &reg_context_sp,
115 lldb::addr_t cfa,
Greg Clayton33ed1702010-08-24 00:45:41 +0000116 const Address& pc_addr,
117 const SymbolContext *sc_ptr
118) :
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000119 m_thread (thread),
Greg Clayton33ed1702010-08-24 00:45:41 +0000120 m_frame_index (frame_idx),
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000121 m_concrete_frame_index (unwind_frame_index),
Greg Clayton33ed1702010-08-24 00:45:41 +0000122 m_reg_context_sp (reg_context_sp),
Greg Claytoneea26402010-09-14 23:36:40 +0000123 m_id (pc_addr.GetLoadAddress (&thread.GetProcess().GetTarget()), cfa, NULL),
Greg Clayton65124ea2010-08-26 22:05:43 +0000124 m_frame_code_addr (pc_addr),
Greg Clayton33ed1702010-08-24 00:45:41 +0000125 m_sc (),
126 m_flags (),
127 m_frame_base (),
128 m_frame_base_error (),
129 m_variable_list_sp (),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000130 m_variable_list_value_objects (),
131 m_disassembly ()
Greg Clayton33ed1702010-08-24 00:45:41 +0000132{
133 if (sc_ptr != NULL)
134 {
135 m_sc = *sc_ptr;
136 m_flags.Set(m_sc.GetResolvedMask ());
137 }
138
139 if (m_sc.target_sp.get() == NULL && reg_context_sp)
140 {
141 m_sc.target_sp = reg_context_sp->GetThread().GetProcess().GetTarget().GetSP();
142 m_flags.Set (eSymbolContextTarget);
143 }
144
Greg Claytone2c5e452010-09-13 04:34:30 +0000145 Module *pc_module = pc_addr.GetModule();
146 if (m_sc.module_sp.get() == NULL || m_sc.module_sp.get() != pc_module)
Greg Clayton33ed1702010-08-24 00:45:41 +0000147 {
Greg Clayton33ed1702010-08-24 00:45:41 +0000148 if (pc_module)
149 {
150 m_sc.module_sp = pc_module->GetSP();
151 m_flags.Set (eSymbolContextModule);
152 }
Greg Claytone2c5e452010-09-13 04:34:30 +0000153 else
154 {
155 m_sc.module_sp.reset();
156 }
157
Greg Clayton33ed1702010-08-24 00:45:41 +0000158 }
Chris Lattner24943d22010-06-08 16:52:24 +0000159}
160
161
162//----------------------------------------------------------------------
163// Destructor
164//----------------------------------------------------------------------
165StackFrame::~StackFrame()
166{
167}
168
169StackID&
170StackFrame::GetStackID()
171{
Greg Clayton72b71582010-09-02 21:44:10 +0000172 // Make sure we have resolved the StackID object's symbol context scope if
173 // we already haven't looked it up.
Chris Lattner24943d22010-06-08 16:52:24 +0000174
Greg Clayton4fb08152010-08-30 18:11:35 +0000175 if (m_flags.IsClear (RESOLVED_FRAME_ID_SYMBOL_SCOPE))
176 {
Greg Clayton5205f0b2010-09-03 17:10:42 +0000177 if (m_id.GetSymbolContextScope ())
Greg Clayton4fb08152010-08-30 18:11:35 +0000178 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000179 // We already have a symbol context scope, we just don't have our
180 // flag bit set.
Greg Clayton4fb08152010-08-30 18:11:35 +0000181 m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
182 }
183 else
184 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000185 // Calculate the frame block and use this for the stack ID symbol
186 // context scope if we have one.
187 SymbolContextScope *scope = GetFrameBlock ();
188 if (scope == NULL)
Greg Clayton4fb08152010-08-30 18:11:35 +0000189 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000190 // We don't have a block, so use the symbol
191 if (m_flags.IsClear (eSymbolContextSymbol))
192 GetSymbolContext (eSymbolContextSymbol);
193
194 // It is ok if m_sc.symbol is NULL here
195 scope = m_sc.symbol;
Greg Clayton4fb08152010-08-30 18:11:35 +0000196 }
Greg Clayton69aa5d92010-09-07 04:20:48 +0000197 // Set the symbol context scope (the accessor will set the
198 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
199 SetSymbolContextScope (scope);
Greg Clayton4fb08152010-08-30 18:11:35 +0000200 }
Chris Lattner24943d22010-06-08 16:52:24 +0000201 }
202 return m_id;
203}
204
Greg Clayton4fb08152010-08-30 18:11:35 +0000205void
206StackFrame::SetSymbolContextScope (SymbolContextScope *symbol_scope)
207{
208 m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
209 m_id.SetSymbolContextScope (symbol_scope);
210}
211
Greg Clayton107e53d2011-07-06 04:07:21 +0000212const Address&
Greg Claytonb04e7a82010-08-24 21:05:24 +0000213StackFrame::GetFrameCodeAddress()
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Greg Clayton4fb08152010-08-30 18:11:35 +0000215 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && !m_frame_code_addr.IsSectionOffset())
Chris Lattner24943d22010-06-08 16:52:24 +0000216 {
Greg Clayton4fb08152010-08-30 18:11:35 +0000217 m_flags.Set (RESOLVED_FRAME_CODE_ADDR);
Chris Lattner24943d22010-06-08 16:52:24 +0000218
219 // Resolve the PC into a temporary address because if ResolveLoadAddress
220 // fails to resolve the address, it will clear the address object...
Greg Clayton107e53d2011-07-06 04:07:21 +0000221
222 if (m_frame_code_addr.SetOpcodeLoadAddress (m_frame_code_addr.GetOffset(), &m_thread.GetProcess().GetTarget()))
Chris Lattner24943d22010-06-08 16:52:24 +0000223 {
Greg Clayton65124ea2010-08-26 22:05:43 +0000224 const Section *section = m_frame_code_addr.GetSection();
Chris Lattner24943d22010-06-08 16:52:24 +0000225 if (section)
226 {
227 Module *module = section->GetModule();
228 if (module)
229 {
230 m_sc.module_sp = module->GetSP();
231 if (m_sc.module_sp)
232 m_flags.Set(eSymbolContextModule);
233 }
234 }
235 }
236 }
Greg Clayton65124ea2010-08-26 22:05:43 +0000237 return m_frame_code_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000238}
239
240void
241StackFrame::ChangePC (addr_t pc)
242{
Greg Clayton65124ea2010-08-26 22:05:43 +0000243 m_frame_code_addr.SetOffset(pc);
244 m_frame_code_addr.SetSection(NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000245 m_sc.Clear();
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000246 m_flags.Reset(0);
Chris Lattner24943d22010-06-08 16:52:24 +0000247 m_thread.ClearStackFrames ();
248}
249
250const char *
251StackFrame::Disassemble ()
252{
253 if (m_disassembly.GetSize() == 0)
254 {
255 ExecutionContext exe_ctx;
Greg Claytona830adb2010-10-04 01:05:56 +0000256 CalculateExecutionContext(exe_ctx);
Greg Clayton63094e02010-06-23 01:19:29 +0000257 Target &target = m_thread.GetProcess().GetTarget();
258 Disassembler::Disassemble (target.GetDebugger(),
259 target.GetArchitecture(),
Greg Clayton149731c2011-03-25 18:03:16 +0000260 NULL,
Chris Lattner24943d22010-06-08 16:52:24 +0000261 exe_ctx,
262 0,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000263 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000264 0,
Chris Lattner24943d22010-06-08 16:52:24 +0000265 m_disassembly);
266 if (m_disassembly.GetSize() == 0)
267 return NULL;
268 }
269 return m_disassembly.GetData();
270}
271
Greg Clayton69aa5d92010-09-07 04:20:48 +0000272Block *
273StackFrame::GetFrameBlock ()
274{
275 if (m_sc.block == NULL && m_flags.IsClear (eSymbolContextBlock))
276 GetSymbolContext (eSymbolContextBlock);
277
278 if (m_sc.block)
279 {
280 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
281 if (inline_block)
282 {
283 // Use the block with the inlined function info
284 // as the frame block we want this frame to have only the variables
285 // for the inlined function and its non-inlined block child blocks.
286 return inline_block;
287 }
288 else
289 {
290 // This block is not contained withing any inlined function blocks
291 // with so we want to use the top most function block.
292 return &m_sc.function->GetBlock (false);
293 }
294 }
295 return NULL;
296}
297
Chris Lattner24943d22010-06-08 16:52:24 +0000298//----------------------------------------------------------------------
299// Get the symbol context if we already haven't done so by resolving the
300// PC address as much as possible. This way when we pass around a
301// StackFrame object, everyone will have as much information as
302// possible and no one will ever have to look things up manually.
303//----------------------------------------------------------------------
304const SymbolContext&
305StackFrame::GetSymbolContext (uint32_t resolve_scope)
306{
307 // Copy our internal symbol context into "sc".
Greg Claytonf3d0b0c2010-10-27 03:32:59 +0000308 if ((m_flags.Get() & resolve_scope) != resolve_scope)
Chris Lattner24943d22010-06-08 16:52:24 +0000309 {
310 // Resolve our PC to section offset if we haven't alreday done so
311 // and if we don't have a module. The resolved address section will
312 // contain the module to which it belongs
Greg Clayton4fb08152010-08-30 18:11:35 +0000313 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
Greg Claytonb04e7a82010-08-24 21:05:24 +0000314 GetFrameCodeAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000315
316 // If this is not frame zero, then we need to subtract 1 from the PC
317 // value when doing address lookups since the PC will be on the
318 // instruction following the function call instruction...
319
Greg Claytonb04e7a82010-08-24 21:05:24 +0000320 Address lookup_addr(GetFrameCodeAddress());
Greg Clayton33ed1702010-08-24 00:45:41 +0000321 if (m_frame_index > 0 && lookup_addr.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000322 {
323 addr_t offset = lookup_addr.GetOffset();
324 if (offset > 0)
325 lookup_addr.SetOffset(offset - 1);
326 }
327
Greg Claytonb04e7a82010-08-24 21:05:24 +0000328
329 uint32_t resolved = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000330 if (m_sc.module_sp)
331 {
332 // We have something in our stack frame symbol context, lets check
333 // if we haven't already tried to lookup one of those things. If we
334 // haven't then we will do the query.
Greg Clayton33ed1702010-08-24 00:45:41 +0000335
336 uint32_t actual_resolve_scope = 0;
337
338 if (resolve_scope & eSymbolContextCompUnit)
339 {
340 if (m_flags.IsClear (eSymbolContextCompUnit))
341 {
342 if (m_sc.comp_unit)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000343 resolved |= eSymbolContextCompUnit;
Greg Clayton33ed1702010-08-24 00:45:41 +0000344 else
345 actual_resolve_scope |= eSymbolContextCompUnit;
346 }
347 }
348
349 if (resolve_scope & eSymbolContextFunction)
350 {
351 if (m_flags.IsClear (eSymbolContextFunction))
352 {
353 if (m_sc.function)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000354 resolved |= eSymbolContextFunction;
Greg Clayton33ed1702010-08-24 00:45:41 +0000355 else
356 actual_resolve_scope |= eSymbolContextFunction;
357 }
358 }
359
360 if (resolve_scope & eSymbolContextBlock)
361 {
362 if (m_flags.IsClear (eSymbolContextBlock))
363 {
364 if (m_sc.block)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000365 resolved |= eSymbolContextBlock;
Greg Clayton33ed1702010-08-24 00:45:41 +0000366 else
367 actual_resolve_scope |= eSymbolContextBlock;
368 }
369 }
370
371 if (resolve_scope & eSymbolContextSymbol)
372 {
373 if (m_flags.IsClear (eSymbolContextSymbol))
374 {
375 if (m_sc.symbol)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000376 resolved |= eSymbolContextSymbol;
Greg Clayton33ed1702010-08-24 00:45:41 +0000377 else
378 actual_resolve_scope |= eSymbolContextSymbol;
379 }
380 }
381
382 if (resolve_scope & eSymbolContextLineEntry)
383 {
384 if (m_flags.IsClear (eSymbolContextLineEntry))
385 {
386 if (m_sc.line_entry.IsValid())
Greg Claytonb04e7a82010-08-24 21:05:24 +0000387 resolved |= eSymbolContextLineEntry;
Greg Clayton33ed1702010-08-24 00:45:41 +0000388 else
389 actual_resolve_scope |= eSymbolContextLineEntry;
390 }
391 }
392
393 if (actual_resolve_scope)
Chris Lattner24943d22010-06-08 16:52:24 +0000394 {
395 // We might be resolving less information than what is already
396 // in our current symbol context so resolve into a temporary
397 // symbol context "sc" so we don't clear out data we have
398 // already found in "m_sc"
399 SymbolContext sc;
400 // Set flags that indicate what we have tried to resolve
Greg Claytonb04e7a82010-08-24 21:05:24 +0000401 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc);
Greg Clayton33ed1702010-08-24 00:45:41 +0000402 // Only replace what we didn't already have as we may have
403 // information for an inlined function scope that won't match
404 // what a standard lookup by address would match
Greg Claytonb04e7a82010-08-24 21:05:24 +0000405 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == NULL)
406 m_sc.comp_unit = sc.comp_unit;
407 if ((resolved & eSymbolContextFunction) && m_sc.function == NULL)
408 m_sc.function = sc.function;
409 if ((resolved & eSymbolContextBlock) && m_sc.block == NULL)
410 m_sc.block = sc.block;
411 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == NULL)
412 m_sc.symbol = sc.symbol;
413 if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid())
414 m_sc.line_entry = sc.line_entry;
415
Chris Lattner24943d22010-06-08 16:52:24 +0000416 }
417 }
418 else
419 {
420 // If we don't have a module, then we can't have the compile unit,
421 // function, block, line entry or symbol, so we can safely call
422 // ResolveSymbolContextForAddress with our symbol context member m_sc.
Greg Claytonb04e7a82010-08-24 21:05:24 +0000423 resolved |= m_thread.GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
Chris Lattner24943d22010-06-08 16:52:24 +0000424 }
425
426 // If the target was requested add that:
427 if (m_sc.target_sp.get() == NULL)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000428 {
Chris Lattner24943d22010-06-08 16:52:24 +0000429 m_sc.target_sp = CalculateProcess()->GetTarget().GetSP();
Greg Claytonb04e7a82010-08-24 21:05:24 +0000430 if (m_sc.target_sp)
431 resolved |= eSymbolContextTarget;
432 }
Chris Lattner24943d22010-06-08 16:52:24 +0000433
434 // Update our internal flags so we remember what we have tried to locate so
435 // we don't have to keep trying when more calls to this function are made.
Greg Claytonb04e7a82010-08-24 21:05:24 +0000436 // We might have dug up more information that was requested (for example
437 // if we were asked to only get the block, we will have gotten the
438 // compile unit, and function) so set any additional bits that we resolved
439 m_flags.Set (resolve_scope | resolved);
Chris Lattner24943d22010-06-08 16:52:24 +0000440 }
441
442 // Return the symbol context with everything that was possible to resolve
443 // resolved.
444 return m_sc;
445}
446
447
448VariableList *
Greg Clayton17dae082010-09-02 02:59:18 +0000449StackFrame::GetVariableList (bool get_file_globals)
Chris Lattner24943d22010-06-08 16:52:24 +0000450{
451 if (m_flags.IsClear(RESOLVED_VARIABLES))
452 {
453 m_flags.Set(RESOLVED_VARIABLES);
454
Greg Clayton69aa5d92010-09-07 04:20:48 +0000455 Block *frame_block = GetFrameBlock();
456
457 if (frame_block)
Chris Lattner24943d22010-06-08 16:52:24 +0000458 {
Greg Clayton69aa5d92010-09-07 04:20:48 +0000459 const bool get_child_variables = true;
460 const bool can_create = true;
Greg Clayton1bd2b2f2011-06-17 22:10:16 +0000461 const bool stop_if_child_block_is_inlined_function = true;
462 m_variable_list_sp.reset(new VariableList());
463 frame_block->AppendBlockVariables(can_create, get_child_variables, stop_if_child_block_is_inlined_function, m_variable_list_sp.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000464 }
Sean Callanan89363592010-11-01 04:38:59 +0000465 }
466
467 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
468 get_file_globals)
469 {
470 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
Greg Clayton17dae082010-09-02 02:59:18 +0000471
Sean Callanan89363592010-11-01 04:38:59 +0000472 if (m_flags.IsClear (eSymbolContextCompUnit))
473 GetSymbolContext (eSymbolContextCompUnit);
474
475 if (m_sc.comp_unit)
Greg Clayton17dae082010-09-02 02:59:18 +0000476 {
Sean Callanan89363592010-11-01 04:38:59 +0000477 VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
478 if (m_variable_list_sp)
479 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
480 else
481 m_variable_list_sp = global_variable_list_sp;
Greg Clayton17dae082010-09-02 02:59:18 +0000482 }
Chris Lattner24943d22010-06-08 16:52:24 +0000483 }
Sean Callanan89363592010-11-01 04:38:59 +0000484
Chris Lattner24943d22010-06-08 16:52:24 +0000485 return m_variable_list_sp.get();
486}
487
Greg Clayton6e2d2822011-08-02 23:35:43 +0000488VariableListSP
489StackFrame::GetInScopeVariableList (bool get_file_globals)
490{
491 VariableListSP var_list_sp(new VariableList);
492 GetSymbolContext (eSymbolContextCompUnit | eSymbolContextBlock);
493
494 if (m_sc.block)
495 {
496 const bool can_create = true;
497 const bool get_parent_variables = true;
498 const bool stop_if_block_is_inlined_function = true;
499 m_sc.block->AppendVariables (can_create,
500 get_parent_variables,
501 stop_if_block_is_inlined_function,
502 var_list_sp.get());
503 }
504
505 if (m_sc.comp_unit)
506 {
507 VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
508 if (global_variable_list_sp)
509 var_list_sp->AddVariables (global_variable_list_sp.get());
510 }
511
512 return var_list_sp;
513}
514
515
Greg Clayton427f2902010-12-14 02:59:59 +0000516ValueObjectSP
Jim Ingham10de7d12011-05-04 03:43:18 +0000517StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr,
518 lldb::DynamicValueType use_dynamic,
519 uint32_t options,
520 lldb::VariableSP &var_sp,
521 Error &error)
Greg Clayton427f2902010-12-14 02:59:59 +0000522{
Greg Claytonc3b61d22010-12-15 05:08:08 +0000523
524 if (var_expr_cstr && var_expr_cstr[0])
Greg Clayton427f2902010-12-14 02:59:59 +0000525 {
Greg Claytonc67efa42011-01-20 19:27:18 +0000526 const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
527 const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
Enrico Granataf6698502011-08-09 01:04:56 +0000528 const bool no_synth_child = (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
Enrico Granata13a54a12011-08-19 21:56:10 +0000529 //const bool no_synth_array = (options & eExpressionPathOptionsNoSyntheticArrayRange) != 0;
Greg Claytonc3b61d22010-12-15 05:08:08 +0000530 error.Clear();
531 bool deref = false;
532 bool address_of = false;
533 ValueObjectSP valobj_sp;
534 const bool get_file_globals = true;
Greg Clayton6e2d2822011-08-02 23:35:43 +0000535 // When looking up a variable for an expression, we need only consider the
536 // variables that are in scope.
537 VariableListSP var_list_sp (GetInScopeVariableList (get_file_globals));
538 VariableList *variable_list = var_list_sp.get();
Greg Claytonc3b61d22010-12-15 05:08:08 +0000539
540 if (variable_list)
Greg Clayton427f2902010-12-14 02:59:59 +0000541 {
Greg Claytonc3b61d22010-12-15 05:08:08 +0000542 // If first character is a '*', then show pointer contents
543 const char *var_expr = var_expr_cstr;
544 if (var_expr[0] == '*')
Greg Clayton427f2902010-12-14 02:59:59 +0000545 {
Greg Claytonc3b61d22010-12-15 05:08:08 +0000546 deref = true;
547 var_expr++; // Skip the '*'
548 }
549 else if (var_expr[0] == '&')
550 {
551 address_of = true;
552 var_expr++; // Skip the '&'
553 }
554
555 std::string var_path (var_expr);
556 size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
557 StreamString var_expr_path_strm;
558
559 ConstString name_const_string;
560 if (separator_idx == std::string::npos)
561 name_const_string.SetCString (var_path.c_str());
562 else
563 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
564
Jim Ingham10de7d12011-05-04 03:43:18 +0000565 var_sp = variable_list->FindVariable(name_const_string);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000566 if (var_sp)
567 {
Jim Ingham10de7d12011-05-04 03:43:18 +0000568 valobj_sp = GetValueObjectForFrameVariable (var_sp, use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +0000569 if (!valobj_sp)
570 return valobj_sp;
571
Greg Claytonc3b61d22010-12-15 05:08:08 +0000572 var_path.erase (0, name_const_string.GetLength ());
573 // We are dumping at least one child
574 while (separator_idx != std::string::npos)
Greg Clayton427f2902010-12-14 02:59:59 +0000575 {
Greg Claytonc3b61d22010-12-15 05:08:08 +0000576 // Calculate the next separator index ahead of time
577 ValueObjectSP child_valobj_sp;
578 const char separator_type = var_path[0];
579 switch (separator_type)
Greg Clayton427f2902010-12-14 02:59:59 +0000580 {
Greg Claytonc3b61d22010-12-15 05:08:08 +0000581
582 case '-':
583 if (var_path.size() >= 2 && var_path[1] != '>')
Greg Clayton427f2902010-12-14 02:59:59 +0000584 return ValueObjectSP();
Greg Clayton427f2902010-12-14 02:59:59 +0000585
Greg Claytonc67efa42011-01-20 19:27:18 +0000586 if (no_fragile_ivar)
587 {
588 // Make sure we aren't trying to deref an objective
589 // C ivar if this is not allowed
590 const uint32_t pointer_type_flags = ClangASTContext::GetTypeInfo (valobj_sp->GetClangType(), NULL, NULL);
591 if ((pointer_type_flags & ClangASTContext::eTypeIsObjC) &&
592 (pointer_type_flags & ClangASTContext::eTypeIsPointer))
593 {
594 // This was an objective C object pointer and
595 // it was requested we skip any fragile ivars
596 // so return nothing here
597 return ValueObjectSP();
598 }
599 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000600 var_path.erase (0, 1); // Remove the '-'
601 // Fall through
602 case '.':
Greg Clayton427f2902010-12-14 02:59:59 +0000603 {
Greg Claytonc3b61d22010-12-15 05:08:08 +0000604 const bool expr_is_ptr = var_path[0] == '>';
Greg Clayton427f2902010-12-14 02:59:59 +0000605
Greg Claytonc3b61d22010-12-15 05:08:08 +0000606 var_path.erase (0, 1); // Remove the '.' or '>'
607 separator_idx = var_path.find_first_of(".-[");
608 ConstString child_name;
609 if (separator_idx == std::string::npos)
610 child_name.SetCString (var_path.c_str());
Greg Clayton427f2902010-12-14 02:59:59 +0000611 else
Greg Claytonc3b61d22010-12-15 05:08:08 +0000612 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
613
614 if (check_ptr_vs_member)
Greg Clayton427f2902010-12-14 02:59:59 +0000615 {
Greg Claytonc3b61d22010-12-15 05:08:08 +0000616 // We either have a pointer type and need to verify
617 // valobj_sp is a pointer, or we have a member of a
618 // class/union/struct being accessed with the . syntax
619 // and need to verify we don't have a pointer.
620 const bool actual_is_ptr = valobj_sp->IsPointerType ();
621
622 if (actual_is_ptr != expr_is_ptr)
623 {
624 // Incorrect use of "." with a pointer, or "->" with
625 // a class/union/struct instance or reference.
Greg Claytonb01000f2011-01-17 03:46:26 +0000626 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000627 if (actual_is_ptr)
628 error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
629 var_expr_path_strm.GetString().c_str(),
630 child_name.GetCString(),
631 var_expr_path_strm.GetString().c_str(),
632 var_path.c_str());
633 else
634 error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
635 var_expr_path_strm.GetString().c_str(),
636 child_name.GetCString(),
637 var_expr_path_strm.GetString().c_str(),
638 var_path.c_str());
639 return ValueObjectSP();
640 }
Greg Clayton427f2902010-12-14 02:59:59 +0000641 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000642 child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
Greg Clayton427f2902010-12-14 02:59:59 +0000643 if (!child_valobj_sp)
644 {
Enrico Granata9c57fc02011-08-11 17:08:01 +0000645 if (no_synth_child == false)
646 child_valobj_sp = valobj_sp->GetSyntheticValue(lldb::eUseSyntheticFilter)->GetChildMemberWithName (child_name, true);
647
648 if (no_synth_child || !child_valobj_sp)
Greg Claytonc3b61d22010-12-15 05:08:08 +0000649 {
Enrico Granata9c57fc02011-08-11 17:08:01 +0000650 // No child member with name "child_name"
651 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
652 if (child_name)
653 {
654 error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
655 child_name.GetCString(),
656 valobj_sp->GetTypeName().AsCString("<invalid type>"),
657 var_expr_path_strm.GetString().c_str());
658 }
659 else
660 {
661 error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
662 var_expr_path_strm.GetString().c_str(),
663 var_expr_cstr);
664 }
665 return ValueObjectSP();
Greg Claytonc3b61d22010-12-15 05:08:08 +0000666 }
Greg Clayton427f2902010-12-14 02:59:59 +0000667 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000668 // Remove the child name from the path
669 var_path.erase(0, child_name.GetLength());
Jim Ingham10de7d12011-05-04 03:43:18 +0000670 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +0000671 {
Jim Ingham10de7d12011-05-04 03:43:18 +0000672 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
Jim Inghame41494a2011-04-16 00:01:13 +0000673 if (dynamic_value_sp)
674 child_valobj_sp = dynamic_value_sp;
675 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000676 }
677 break;
Greg Clayton427f2902010-12-14 02:59:59 +0000678
Greg Claytonc3b61d22010-12-15 05:08:08 +0000679 case '[':
680 // Array member access, or treating pointer as an array
681 if (var_path.size() > 2) // Need at least two brackets and a number
682 {
683 char *end = NULL;
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000684 long child_index = ::strtol (&var_path[1], &end, 0);
Enrico Granata9762e102011-07-06 02:13:41 +0000685 if (end && *end == ']'
686 && *(end-1) != '[') // this code forces an error in the case of arr[]. as bitfield[] is not a good syntax we're good to go
Greg Claytonc3b61d22010-12-15 05:08:08 +0000687 {
Enrico Granata9762e102011-07-06 02:13:41 +0000688 if (ClangASTContext::IsPointerToScalarType(valobj_sp->GetClangType()) && deref)
689 {
690 // what we have is *ptr[low]. the most similar C++ syntax is to deref ptr
691 // and extract bit low out of it. reading array item low
692 // would be done by saying ptr[low], without a deref * sign
693 Error error;
694 ValueObjectSP temp(valobj_sp->Dereference(error));
695 if (error.Fail())
696 {
697 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
698 error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
699 valobj_sp->GetTypeName().AsCString("<invalid type>"),
700 var_expr_path_strm.GetString().c_str());
701 return ValueObjectSP();
702 }
703 valobj_sp = temp;
704 deref = false;
705 }
706 else if (ClangASTContext::IsArrayOfScalarType(valobj_sp->GetClangType()) && deref)
707 {
708 // what we have is *arr[low]. the most similar C++ syntax is to get arr[0]
709 // (an operation that is equivalent to deref-ing arr)
710 // and extract bit low out of it. reading array item low
711 // would be done by saying arr[low], without a deref * sign
712 Error error;
713 ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
714 if (error.Fail())
715 {
716 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
717 error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
718 valobj_sp->GetTypeName().AsCString("<invalid type>"),
719 var_expr_path_strm.GetString().c_str());
720 return ValueObjectSP();
721 }
722 valobj_sp = temp;
723 deref = false;
724 }
725
Greg Claytonc3b61d22010-12-15 05:08:08 +0000726 if (valobj_sp->IsPointerType ())
727 {
Enrico Granataf6698502011-08-09 01:04:56 +0000728 if (no_synth_child == false
729 &&
730 ClangASTType::GetMinimumLanguage(valobj_sp->GetClangAST(),
731 valobj_sp->GetClangType()) == lldb::eLanguageTypeObjC /* is ObjC pointer */
732 &&
733 ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(valobj_sp->GetClangType())) == false /* is not double-ptr */)
Greg Claytonc3b61d22010-12-15 05:08:08 +0000734 {
Enrico Granataf6698502011-08-09 01:04:56 +0000735 // dereferencing ObjC variables is not valid.. so let's try and recur to synthetic children
736 lldb::ValueObjectSP synthetic = valobj_sp->GetSyntheticValue(lldb::eUseSyntheticFilter);
737 if (synthetic.get() == NULL /* no synthetic */
738 || synthetic == valobj_sp) /* synthetic is the same as the original object */
739 {
740 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
741 error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
742 valobj_sp->GetTypeName().AsCString("<invalid type>"),
743 var_expr_path_strm.GetString().c_str());
744 }
745 else if (child_index >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
746 {
747 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
748 error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
749 child_index,
750 valobj_sp->GetTypeName().AsCString("<invalid type>"),
751 var_expr_path_strm.GetString().c_str());
752 }
753 else
754 {
755 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
756 if (!child_valobj_sp)
757 {
758 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
759 error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
760 child_index,
761 valobj_sp->GetTypeName().AsCString("<invalid type>"),
762 var_expr_path_strm.GetString().c_str());
763 }
764 }
765 }
766 else
767 {
768 child_valobj_sp = valobj_sp->GetSyntheticArrayMemberFromPointer (child_index, true);
769 if (!child_valobj_sp)
770 {
771 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
772 error.SetErrorStringWithFormat ("failed to use pointer as array for index %i for \"(%s) %s\"",
773 child_index,
774 valobj_sp->GetTypeName().AsCString("<invalid type>"),
775 var_expr_path_strm.GetString().c_str());
776 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000777 }
778 }
779 else if (ClangASTContext::IsArrayType (valobj_sp->GetClangType(), NULL, NULL))
780 {
Jim Inghame41494a2011-04-16 00:01:13 +0000781 // Pass false to dynamic_value here so we can tell the difference between
782 // no dynamic value and no member of this type...
Greg Claytonc3b61d22010-12-15 05:08:08 +0000783 child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
784 if (!child_valobj_sp)
785 {
Greg Claytonb01000f2011-01-17 03:46:26 +0000786 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000787 error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
788 child_index,
789 valobj_sp->GetTypeName().AsCString("<invalid type>"),
790 var_expr_path_strm.GetString().c_str());
791 }
792 }
Enrico Granata9762e102011-07-06 02:13:41 +0000793 else if (ClangASTContext::IsScalarType(valobj_sp->GetClangType()))
794 {
795 // this is a bitfield asking to display just one bit
796 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, child_index, true);
797 if (!child_valobj_sp)
798 {
799 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
800 error.SetErrorStringWithFormat ("bitfield range %i-%i is not valid for \"(%s) %s\"",
801 child_index, child_index,
802 valobj_sp->GetTypeName().AsCString("<invalid type>"),
803 var_expr_path_strm.GetString().c_str());
804 }
805 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000806 else
807 {
Enrico Granataf6698502011-08-09 01:04:56 +0000808 lldb::ValueObjectSP synthetic = valobj_sp->GetSyntheticValue(lldb::eUseSyntheticFilter);
809 if (no_synth_child /* synthetic is forbidden */ ||
810 synthetic.get() == NULL /* no synthetic */
811 || synthetic == valobj_sp) /* synthetic is the same as the original object */
812 {
813 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
814 error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
815 valobj_sp->GetTypeName().AsCString("<invalid type>"),
816 var_expr_path_strm.GetString().c_str());
817 }
818 else if (child_index >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
819 {
820 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
821 error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
822 child_index,
823 valobj_sp->GetTypeName().AsCString("<invalid type>"),
824 var_expr_path_strm.GetString().c_str());
825 }
826 else
827 {
828 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
829 if (!child_valobj_sp)
830 {
831 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
832 error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
833 child_index,
834 valobj_sp->GetTypeName().AsCString("<invalid type>"),
835 var_expr_path_strm.GetString().c_str());
836 }
837 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000838 }
839
840 if (!child_valobj_sp)
841 {
842 // Invalid array index...
843 return ValueObjectSP();
844 }
845
846 // Erase the array member specification '[%i]' where
847 // %i is the array index
848 var_path.erase(0, (end - var_path.c_str()) + 1);
849 separator_idx = var_path.find_first_of(".-[");
Jim Ingham10de7d12011-05-04 03:43:18 +0000850 if (use_dynamic != lldb::eNoDynamicValues)
Jim Inghame41494a2011-04-16 00:01:13 +0000851 {
Jim Ingham10de7d12011-05-04 03:43:18 +0000852 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
Jim Inghame41494a2011-04-16 00:01:13 +0000853 if (dynamic_value_sp)
854 child_valobj_sp = dynamic_value_sp;
855 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000856 // Break out early from the switch since we were
857 // able to find the child member
858 break;
859 }
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000860 else if (end && *end == '-')
Enrico Granata9762e102011-07-06 02:13:41 +0000861 {
862 // this is most probably a BitField, let's take a look
863 char *real_end = NULL;
864 long final_index = ::strtol (end+1, &real_end, 0);
Enrico Granata6f302872011-08-19 21:13:46 +0000865 bool expand_bitfield = true;
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000866 if (real_end && *real_end == ']')
Enrico Granata9762e102011-07-06 02:13:41 +0000867 {
868 // if the format given is [high-low], swap range
Enrico Granata4c3fb4b2011-07-19 18:03:25 +0000869 if (child_index > final_index)
Enrico Granata9762e102011-07-06 02:13:41 +0000870 {
871 long temp = child_index;
872 child_index = final_index;
873 final_index = temp;
874 }
875
876 if (ClangASTContext::IsPointerToScalarType(valobj_sp->GetClangType()) && deref)
877 {
878 // what we have is *ptr[low-high]. the most similar C++ syntax is to deref ptr
879 // and extract bits low thru high out of it. reading array items low thru high
880 // would be done by saying ptr[low-high], without a deref * sign
881 Error error;
882 ValueObjectSP temp(valobj_sp->Dereference(error));
883 if (error.Fail())
884 {
885 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
886 error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
887 valobj_sp->GetTypeName().AsCString("<invalid type>"),
888 var_expr_path_strm.GetString().c_str());
889 return ValueObjectSP();
890 }
891 valobj_sp = temp;
892 deref = false;
893 }
894 else if (ClangASTContext::IsArrayOfScalarType(valobj_sp->GetClangType()) && deref)
895 {
896 // what we have is *arr[low-high]. the most similar C++ syntax is to get arr[0]
897 // (an operation that is equivalent to deref-ing arr)
898 // and extract bits low thru high out of it. reading array items low thru high
899 // would be done by saying arr[low-high], without a deref * sign
900 Error error;
901 ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
902 if (error.Fail())
903 {
904 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
905 error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
906 valobj_sp->GetTypeName().AsCString("<invalid type>"),
907 var_expr_path_strm.GetString().c_str());
908 return ValueObjectSP();
909 }
910 valobj_sp = temp;
911 deref = false;
912 }
Enrico Granata6f302872011-08-19 21:13:46 +0000913 /*else if (valobj_sp->IsArrayType() || valobj_sp->IsPointerType())
Enrico Granata9762e102011-07-06 02:13:41 +0000914 {
Enrico Granata6f302872011-08-19 21:13:46 +0000915 child_valobj_sp = valobj_sp->GetSyntheticArrayRangeChild(child_index, final_index, true);
916 expand_bitfield = false;
917 if (!child_valobj_sp)
918 {
919 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
920 error.SetErrorStringWithFormat ("array range %i-%i is not valid for \"(%s) %s\"",
921 child_index, final_index,
922 valobj_sp->GetTypeName().AsCString("<invalid type>"),
923 var_expr_path_strm.GetString().c_str());
924 }
925 }*/
926
927 if (expand_bitfield)
928 {
929 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
930 if (!child_valobj_sp)
931 {
932 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
933 error.SetErrorStringWithFormat ("bitfield range %i-%i is not valid for \"(%s) %s\"",
934 child_index, final_index,
935 valobj_sp->GetTypeName().AsCString("<invalid type>"),
936 var_expr_path_strm.GetString().c_str());
937 }
Enrico Granata9762e102011-07-06 02:13:41 +0000938 }
939 }
940
941 if (!child_valobj_sp)
942 {
943 // Invalid bitfield range...
944 return ValueObjectSP();
945 }
946
947 // Erase the bitfield member specification '[%i-%i]' where
948 // %i is the index
949 var_path.erase(0, (real_end - var_path.c_str()) + 1);
950 separator_idx = var_path.find_first_of(".-[");
951 if (use_dynamic != lldb::eNoDynamicValues)
952 {
953 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
954 if (dynamic_value_sp)
955 child_valobj_sp = dynamic_value_sp;
956 }
957 // Break out early from the switch since we were
958 // able to find the child member
959 break;
960
961 }
962 }
963 else
964 {
965 error.SetErrorStringWithFormat("invalid square bracket encountered after \"%s\" in \"%s\"",
966 var_expr_path_strm.GetString().c_str(),
967 var_path.c_str());
Greg Claytonc3b61d22010-12-15 05:08:08 +0000968 }
969 return ValueObjectSP();
970
971 default:
972 // Failure...
973 {
Greg Claytonb01000f2011-01-17 03:46:26 +0000974 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Claytonc3b61d22010-12-15 05:08:08 +0000975 error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
976 separator_type,
977 var_expr_path_strm.GetString().c_str(),
978 var_path.c_str());
979
980 return ValueObjectSP();
Greg Clayton427f2902010-12-14 02:59:59 +0000981 }
982 }
Greg Clayton427f2902010-12-14 02:59:59 +0000983
Greg Claytonc3b61d22010-12-15 05:08:08 +0000984 if (child_valobj_sp)
985 valobj_sp = child_valobj_sp;
986
987 if (var_path.empty())
988 break;
989
Greg Clayton427f2902010-12-14 02:59:59 +0000990 }
Greg Claytonc3b61d22010-12-15 05:08:08 +0000991 if (valobj_sp)
992 {
993 if (deref)
994 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000995 ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
Greg Claytonc3b61d22010-12-15 05:08:08 +0000996 valobj_sp = deref_valobj_sp;
997 }
998 else if (address_of)
999 {
1000 ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
1001 valobj_sp = address_of_valobj_sp;
1002 }
1003 }
1004 return valobj_sp;
Greg Clayton427f2902010-12-14 02:59:59 +00001005 }
Greg Claytonc3b61d22010-12-15 05:08:08 +00001006 else
Greg Clayton427f2902010-12-14 02:59:59 +00001007 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001008 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
1009 name_const_string.GetCString());
Greg Clayton427f2902010-12-14 02:59:59 +00001010 }
Greg Clayton427f2902010-12-14 02:59:59 +00001011 }
1012 }
Greg Claytonc3b61d22010-12-15 05:08:08 +00001013 else
1014 {
1015 error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1016 }
Greg Clayton427f2902010-12-14 02:59:59 +00001017 return ValueObjectSP();
1018}
Chris Lattner24943d22010-06-08 16:52:24 +00001019
1020bool
1021StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
1022{
1023 if (m_flags.IsClear(GOT_FRAME_BASE))
1024 {
1025 if (m_sc.function)
1026 {
1027 m_frame_base.Clear();
1028 m_frame_base_error.Clear();
1029
1030 m_flags.Set(GOT_FRAME_BASE);
1031 ExecutionContext exe_ctx (&m_thread.GetProcess(), &m_thread, this);
1032 Value expr_value;
Greg Clayton178710c2010-09-14 02:20:48 +00001033 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1034 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
Greg Claytoneea26402010-09-14 23:36:40 +00001035 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (&m_thread.GetProcess().GetTarget());
Greg Clayton178710c2010-09-14 02:20:48 +00001036
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001037 if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx, NULL, NULL, NULL, NULL, loclist_base_addr, NULL, expr_value, &m_frame_base_error) == false)
Chris Lattner24943d22010-06-08 16:52:24 +00001038 {
1039 // We should really have an error if evaluate returns, but in case
1040 // we don't, lets set the error to something at least.
1041 if (m_frame_base_error.Success())
1042 m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
1043 }
1044 else
1045 {
1046 m_frame_base = expr_value.ResolveValue(&exe_ctx, NULL);
1047 }
1048 }
1049 else
1050 {
1051 m_frame_base_error.SetErrorString ("No function in symbol context.");
1052 }
1053 }
1054
1055 if (m_frame_base_error.Success())
1056 frame_base = m_frame_base;
1057
1058 if (error_ptr)
1059 *error_ptr = m_frame_base_error;
1060 return m_frame_base_error.Success();
1061}
1062
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001063RegisterContextSP
Chris Lattner24943d22010-06-08 16:52:24 +00001064StackFrame::GetRegisterContext ()
1065{
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001066 if (!m_reg_context_sp)
1067 m_reg_context_sp = m_thread.CreateRegisterContextForFrame (this);
1068 return m_reg_context_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001069}
1070
1071bool
1072StackFrame::HasDebugInformation ()
1073{
Greg Claytonb04e7a82010-08-24 21:05:24 +00001074 GetSymbolContext (eSymbolContextLineEntry);
Chris Lattner24943d22010-06-08 16:52:24 +00001075 return m_sc.line_entry.IsValid();
1076}
1077
Greg Clayton17dae082010-09-02 02:59:18 +00001078
1079ValueObjectSP
Jim Ingham10de7d12011-05-04 03:43:18 +00001080StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp, lldb::DynamicValueType use_dynamic)
Chris Lattner24943d22010-06-08 16:52:24 +00001081{
Greg Clayton17dae082010-09-02 02:59:18 +00001082 ValueObjectSP valobj_sp;
1083 VariableList *var_list = GetVariableList (true);
1084 if (var_list)
1085 {
1086 // Make sure the variable is a frame variable
1087 const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
1088 const uint32_t num_variables = var_list->GetSize();
1089 if (var_idx < num_variables)
1090 {
1091 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
1092 if (valobj_sp.get() == NULL)
1093 {
1094 if (m_variable_list_value_objects.GetSize() < num_variables)
1095 m_variable_list_value_objects.Resize(num_variables);
Jim Ingham47da8102011-04-22 23:53:53 +00001096 valobj_sp = ValueObjectVariable::Create (this, variable_sp);
Greg Clayton17dae082010-09-02 02:59:18 +00001097 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
1098 }
1099 }
1100 }
Jim Ingham10de7d12011-05-04 03:43:18 +00001101 if (use_dynamic != lldb::eNoDynamicValues && valobj_sp)
Jim Inghame41494a2011-04-16 00:01:13 +00001102 {
Jim Ingham10de7d12011-05-04 03:43:18 +00001103 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue (use_dynamic);
Jim Inghame41494a2011-04-16 00:01:13 +00001104 if (dynamic_sp)
1105 return dynamic_sp;
1106 }
Greg Clayton17dae082010-09-02 02:59:18 +00001107 return valobj_sp;
1108}
1109
1110ValueObjectSP
Jim Ingham10de7d12011-05-04 03:43:18 +00001111StackFrame::TrackGlobalVariable (const VariableSP &variable_sp, lldb::DynamicValueType use_dynamic)
Greg Clayton17dae082010-09-02 02:59:18 +00001112{
1113 // Check to make sure we aren't already tracking this variable?
Jim Inghame41494a2011-04-16 00:01:13 +00001114 ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp, use_dynamic));
Greg Clayton17dae082010-09-02 02:59:18 +00001115 if (!valobj_sp)
1116 {
1117 // We aren't already tracking this global
1118 VariableList *var_list = GetVariableList (true);
1119 // If this frame has no variables, create a new list
1120 if (var_list == NULL)
1121 m_variable_list_sp.reset (new VariableList());
1122
1123 // Add the global/static variable to this frame
1124 m_variable_list_sp->AddVariable (variable_sp);
1125
1126 // Now make a value object for it so we can track its changes
Jim Inghame41494a2011-04-16 00:01:13 +00001127 valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
Greg Clayton17dae082010-09-02 02:59:18 +00001128 }
1129 return valobj_sp;
Chris Lattner24943d22010-06-08 16:52:24 +00001130}
1131
Jim Ingham2154da42010-08-26 20:44:45 +00001132bool
1133StackFrame::IsInlined ()
1134{
Greg Clayton4fb08152010-08-30 18:11:35 +00001135 if (m_sc.block == NULL)
1136 GetSymbolContext (eSymbolContextBlock);
1137 if (m_sc.block)
1138 return m_sc.block->GetContainingInlinedBlock() != NULL;
1139 return false;
Jim Ingham2154da42010-08-26 20:44:45 +00001140}
1141
Chris Lattner24943d22010-06-08 16:52:24 +00001142Target *
1143StackFrame::CalculateTarget ()
1144{
1145 return m_thread.CalculateTarget();
1146}
1147
1148Process *
1149StackFrame::CalculateProcess ()
1150{
1151 return m_thread.CalculateProcess();
1152}
1153
1154Thread *
1155StackFrame::CalculateThread ()
1156{
1157 return &m_thread;
1158}
1159
1160StackFrame *
1161StackFrame::CalculateStackFrame ()
1162{
1163 return this;
1164}
1165
1166
1167void
Greg Claytona830adb2010-10-04 01:05:56 +00001168StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001169{
Greg Claytona830adb2010-10-04 01:05:56 +00001170 m_thread.CalculateExecutionContext (exe_ctx);
Chris Lattner24943d22010-06-08 16:52:24 +00001171 exe_ctx.frame = this;
1172}
1173
1174void
Greg Claytona830adb2010-10-04 01:05:56 +00001175StackFrame::DumpUsingSettingsFormat (Stream *strm)
1176{
1177 if (strm == NULL)
1178 return;
1179
1180 GetSymbolContext(eSymbolContextEverything);
1181 ExecutionContext exe_ctx;
1182 CalculateExecutionContext(exe_ctx);
1183 const char *end = NULL;
1184 StreamString s;
1185 const char *frame_format = m_thread.GetProcess().GetTarget().GetDebugger().GetFrameFormat();
1186 if (frame_format && Debugger::FormatPrompt (frame_format, &m_sc, &exe_ctx, NULL, s, &end))
1187 {
1188 strm->Write(s.GetData(), s.GetSize());
1189 }
1190 else
1191 {
1192 Dump (strm, true, false);
1193 strm->EOL();
1194 }
1195}
1196
1197void
Greg Clayton72b71582010-09-02 21:44:10 +00001198StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
Chris Lattner24943d22010-06-08 16:52:24 +00001199{
1200 if (strm == NULL)
1201 return;
1202
1203 if (show_frame_index)
Greg Clayton33ed1702010-08-24 00:45:41 +00001204 strm->Printf("frame #%u: ", m_frame_index);
Greg Clayton395fc332011-02-15 21:59:32 +00001205 strm->Printf("0x%0*llx ", m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize() * 2, GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess().GetTarget()));
Greg Claytonb04e7a82010-08-24 21:05:24 +00001206 GetSymbolContext(eSymbolContextEverything);
Greg Clayton33ed1702010-08-24 00:45:41 +00001207 const bool show_module = true;
1208 const bool show_inline = true;
Greg Clayton72b71582010-09-02 21:44:10 +00001209 m_sc.DumpStopContext(strm, &m_thread.GetProcess(), GetFrameCodeAddress(), show_fullpaths, show_module, show_inline);
Chris Lattner24943d22010-06-08 16:52:24 +00001210}
1211
Greg Clayton1d66ef52010-08-27 18:24:16 +00001212void
Greg Clayton4fb08152010-08-30 18:11:35 +00001213StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
Greg Clayton1d66ef52010-08-27 18:24:16 +00001214{
Greg Clayton4fb08152010-08-30 18:11:35 +00001215 assert (GetStackID() == prev_frame.GetStackID()); // TODO: remove this after some testing
1216 m_variable_list_sp = prev_frame.m_variable_list_sp;
Greg Clayton17dae082010-09-02 02:59:18 +00001217 m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
Greg Clayton870a1cd2010-08-27 21:47:54 +00001218 if (!m_disassembly.GetString().empty())
1219 m_disassembly.GetString().swap (m_disassembly.GetString());
Greg Clayton1d66ef52010-08-27 18:24:16 +00001220}
Greg Clayton870a1cd2010-08-27 21:47:54 +00001221
1222
Greg Clayton4fb08152010-08-30 18:11:35 +00001223void
1224StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
1225{
Greg Clayton5205f0b2010-09-03 17:10:42 +00001226 assert (GetStackID() == curr_frame.GetStackID()); // TODO: remove this after some testing
1227 m_id.SetPC (curr_frame.m_id.GetPC()); // Update the Stack ID PC value
Greg Clayton4fb08152010-08-30 18:11:35 +00001228 assert (&m_thread == &curr_frame.m_thread);
1229 m_frame_index = curr_frame.m_frame_index;
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001230 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
Greg Clayton4fb08152010-08-30 18:11:35 +00001231 m_reg_context_sp = curr_frame.m_reg_context_sp;
1232 m_frame_code_addr = curr_frame.m_frame_code_addr;
1233 assert (m_sc.target_sp.get() == NULL || curr_frame.m_sc.target_sp.get() == NULL || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1234 assert (m_sc.module_sp.get() == NULL || curr_frame.m_sc.module_sp.get() == NULL || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1235 assert (m_sc.comp_unit == NULL || curr_frame.m_sc.comp_unit == NULL || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1236 assert (m_sc.function == NULL || curr_frame.m_sc.function == NULL || m_sc.function == curr_frame.m_sc.function);
Greg Clayton4fb08152010-08-30 18:11:35 +00001237 m_sc = curr_frame.m_sc;
1238 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1239 m_flags.Set (m_sc.GetResolvedMask());
1240 m_frame_base.Clear();
1241 m_frame_base_error.Clear();
1242}
1243
1244
Greg Clayton5205f0b2010-09-03 17:10:42 +00001245bool
1246StackFrame::HasCachedData () const
1247{
1248 if (m_variable_list_sp.get())
1249 return true;
1250 if (m_variable_list_value_objects.GetSize() > 0)
1251 return true;
1252 if (!m_disassembly.GetString().empty())
1253 return true;
1254 return false;
Jim Inghamccd584d2010-09-23 17:40:12 +00001255}
1256
1257lldb::StackFrameSP
1258StackFrame::GetSP ()
1259{
1260 return m_thread.GetStackFrameSPForStackFramePtr (this);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +00001261}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001262
1263
1264
1265bool
1266StackFrame::GetStatus (Stream& strm,
1267 bool show_frame_info,
1268 bool show_source,
1269 uint32_t source_lines_before,
1270 uint32_t source_lines_after)
1271{
1272 if (show_frame_info)
1273 {
1274 strm.Indent();
1275 DumpUsingSettingsFormat (&strm);
1276 }
1277
1278 if (show_source)
1279 {
1280 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1281
1282 if (m_sc.comp_unit && m_sc.line_entry.IsValid())
1283 {
Greg Claytonff44ab42011-04-23 02:04:55 +00001284 Target &target = GetThread().GetProcess().GetTarget();
1285 target.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbers (
1286 &target,
Greg Claytonabe0fed2011-04-18 08:33:37 +00001287 m_sc.line_entry.file,
1288 m_sc.line_entry.line,
1289 3,
1290 3,
1291 "->",
1292 &strm);
1293
1294 }
1295 }
1296 return true;
1297}
1298