blob: 7110051d28e489970f44a1d76bc8f0b7e5081524 [file] [log] [blame]
Chris Lattner30fdc8d2010-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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000014#include "lldb/Target/StackFrame.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000015#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Disassembler.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000017#include "lldb/Core/FormatEntity.h"
Enrico Granata592afe72016-03-15 21:50:51 +000018#include "lldb/Core/Mangled.h"
19#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Value.h"
Greg Clayton288bdf92010-09-02 02:59:18 +000021#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton54979cd2010-12-15 05:08:08 +000022#include "lldb/Core/ValueObjectConstResult.h"
Greg Clayton1f746072012-08-29 21:13:06 +000023#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Symbol/Symbol.h"
26#include "lldb/Symbol/SymbolContextScope.h"
Enrico Granata46252392015-11-19 22:28:58 +000027#include "lldb/Symbol/Type.h"
Greg Clayton288bdf92010-09-02 02:59:18 +000028#include "lldb/Symbol/VariableList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/ExecutionContext.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/RegisterContext.h"
32#include "lldb/Target/Target.h"
33#include "lldb/Target/Thread.h"
34
35using namespace lldb;
36using namespace lldb_private;
37
38// The first bits in the flags are reserved for the SymbolContext::Scope bits
39// so we know if we have tried to look up information in our internal symbol
40// context (m_sc) already.
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000041#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
Greg Clayton6dadd502010-09-02 21:44:10 +000042#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000043#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
44#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
Sean Callanan7c0962d2010-11-01 04:38:59 +000045#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000047StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, user_id_t unwind_frame_index, addr_t cfa,
48 bool cfa_is_valid, addr_t pc, uint32_t stop_id, bool stop_id_is_valid, bool is_history_frame,
49 const SymbolContext *sc_ptr)
50 : m_thread_wp(thread_sp),
51 m_frame_index(frame_idx),
52 m_concrete_frame_index(unwind_frame_index),
53 m_reg_context_sp(),
54 m_id(pc, cfa, nullptr),
55 m_frame_code_addr(pc),
56 m_sc(),
57 m_flags(),
58 m_frame_base(),
59 m_frame_base_error(),
60 m_cfa_is_valid(cfa_is_valid),
61 m_stop_id(stop_id),
62 m_stop_id_is_valid(stop_id_is_valid),
63 m_is_history_frame(is_history_frame),
64 m_variable_list_sp(),
65 m_variable_list_value_objects(),
66 m_disassembly(),
67 m_mutex()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068{
Jason Molenda99618472013-11-04 11:02:52 +000069 // If we don't have a CFA value, use the frame index for our StackID so that recursive
70 // functions properly aren't confused with one another on a history stack.
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000071 if (m_is_history_frame && !m_cfa_is_valid)
Jason Molenda99618472013-11-04 11:02:52 +000072 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000073 m_id.SetCFA(m_frame_index);
Jason Molenda99618472013-11-04 11:02:52 +000074 }
75
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000076 if (sc_ptr != nullptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +000077 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 m_sc = *sc_ptr;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000079 m_flags.Set(m_sc.GetResolvedMask());
Greg Clayton1b72fcb2010-08-24 00:45:41 +000080 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081}
82
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000083StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, user_id_t unwind_frame_index,
84 const RegisterContextSP &reg_context_sp, addr_t cfa, addr_t pc, const SymbolContext *sc_ptr)
85 : m_thread_wp(thread_sp),
86 m_frame_index(frame_idx),
87 m_concrete_frame_index(unwind_frame_index),
88 m_reg_context_sp(reg_context_sp),
89 m_id(pc, cfa, nullptr),
90 m_frame_code_addr(pc),
91 m_sc(),
92 m_flags(),
93 m_frame_base(),
94 m_frame_base_error(),
95 m_cfa_is_valid(true),
96 m_stop_id(0),
97 m_stop_id_is_valid(false),
98 m_is_history_frame(false),
99 m_variable_list_sp(),
100 m_variable_list_value_objects(),
101 m_disassembly(),
102 m_mutex()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000104 if (sc_ptr != nullptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000105 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 m_sc = *sc_ptr;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000107 m_flags.Set(m_sc.GetResolvedMask());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000108 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000109
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000110 if (reg_context_sp && !m_sc.target_sp)
111 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000112 m_sc.target_sp = reg_context_sp->CalculateTarget();
113 if (m_sc.target_sp)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000114 m_flags.Set(eSymbolContextTarget);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000115 }
116}
117
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000118StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, user_id_t unwind_frame_index,
119 const RegisterContextSP &reg_context_sp, addr_t cfa, const Address &pc_addr,
120 const SymbolContext *sc_ptr)
121 : m_thread_wp(thread_sp),
122 m_frame_index(frame_idx),
123 m_concrete_frame_index(unwind_frame_index),
124 m_reg_context_sp(reg_context_sp),
125 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa, nullptr),
126 m_frame_code_addr(pc_addr),
127 m_sc(),
128 m_flags(),
129 m_frame_base(),
130 m_frame_base_error(),
131 m_cfa_is_valid(true),
132 m_stop_id(0),
133 m_stop_id_is_valid(false),
134 m_is_history_frame(false),
135 m_variable_list_sp(),
136 m_variable_list_value_objects(),
137 m_disassembly(),
138 m_mutex()
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000139{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000140 if (sc_ptr != nullptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000141 {
142 m_sc = *sc_ptr;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000143 m_flags.Set(m_sc.GetResolvedMask());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000144 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000145
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000146 if (!m_sc.target_sp && reg_context_sp)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000147 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000148 m_sc.target_sp = reg_context_sp->CalculateTarget();
149 if (m_sc.target_sp)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000150 m_flags.Set(eSymbolContextTarget);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000151 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000152
153 ModuleSP pc_module_sp(pc_addr.GetModule());
Greg Claytone72dfb32012-02-24 01:59:29 +0000154 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000155 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000156 if (pc_module_sp)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000157 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000158 m_sc.module_sp = pc_module_sp;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000159 m_flags.Set(eSymbolContextModule);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000160 }
Greg Claytonffc1d662010-09-13 04:34:30 +0000161 else
162 {
163 m_sc.module_sp.reset();
164 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000165 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166}
167
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000168StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169
170StackID&
171StackFrame::GetStackID()
172{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000173 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton6dadd502010-09-02 21:44:10 +0000174 // Make sure we have resolved the StackID object's symbol context scope if
175 // we already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000177 if (m_flags.IsClear (RESOLVED_FRAME_ID_SYMBOL_SCOPE))
178 {
Greg Clayton2cad65a2010-09-03 17:10:42 +0000179 if (m_id.GetSymbolContextScope ())
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000180 {
Greg Clayton95897c62010-09-07 04:20:48 +0000181 // We already have a symbol context scope, we just don't have our
182 // flag bit set.
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000183 m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
184 }
185 else
186 {
Greg Clayton95897c62010-09-07 04:20:48 +0000187 // Calculate the frame block and use this for the stack ID symbol
188 // context scope if we have one.
189 SymbolContextScope *scope = GetFrameBlock ();
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000190 if (scope == nullptr)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000191 {
Greg Clayton95897c62010-09-07 04:20:48 +0000192 // We don't have a block, so use the symbol
193 if (m_flags.IsClear (eSymbolContextSymbol))
194 GetSymbolContext (eSymbolContextSymbol);
195
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000196 // It is ok if m_sc.symbol is nullptr here
Greg Clayton95897c62010-09-07 04:20:48 +0000197 scope = m_sc.symbol;
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000198 }
Greg Clayton95897c62010-09-07 04:20:48 +0000199 // Set the symbol context scope (the accessor will set the
200 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
201 SetSymbolContextScope (scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000202 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 }
204 return m_id;
205}
206
Jim Ingham513c6bb2012-09-01 01:02:41 +0000207uint32_t
208StackFrame::GetFrameIndex () const
209{
210 ThreadSP thread_sp = GetThread();
211 if (thread_sp)
Jason Molendab57e4a12013-11-04 09:33:30 +0000212 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(m_frame_index);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000213 else
214 return m_frame_index;
215}
216
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000217void
218StackFrame::SetSymbolContextScope (SymbolContextScope *symbol_scope)
219{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000220 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000221 m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
222 m_id.SetSymbolContextScope (symbol_scope);
223}
224
Greg Clayton34132752011-07-06 04:07:21 +0000225const Address&
Greg Clayton9da7bd02010-08-24 21:05:24 +0000226StackFrame::GetFrameCodeAddress()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000228 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000229 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && !m_frame_code_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230 {
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000231 m_flags.Set (RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232
233 // Resolve the PC into a temporary address because if ResolveLoadAddress
234 // fails to resolve the address, it will clear the address object...
Greg Claytond9e416c2012-02-18 05:35:26 +0000235 ThreadSP thread_sp (GetThread());
236 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000238 TargetSP target_sp (thread_sp->CalculateTarget());
239 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 {
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000241 if (m_frame_code_addr.SetOpcodeLoadAddress (m_frame_code_addr.GetOffset(), target_sp.get(), eAddressClassCode))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000243 ModuleSP module_sp (m_frame_code_addr.GetModule());
244 if (module_sp)
Greg Claytond9e416c2012-02-18 05:35:26 +0000245 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000246 m_sc.module_sp = module_sp;
247 m_flags.Set(eSymbolContextModule);
Greg Claytond9e416c2012-02-18 05:35:26 +0000248 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 }
250 }
251 }
252 }
Greg Clayton12fc3e02010-08-26 22:05:43 +0000253 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
Jason Molenda99618472013-11-04 11:02:52 +0000256bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257StackFrame::ChangePC (addr_t pc)
258{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000259 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda99618472013-11-04 11:02:52 +0000260 // We can't change the pc value of a history stack frame - it is immutable.
261 if (m_is_history_frame)
262 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000263 m_frame_code_addr.SetRawAddress(pc);
Greg Clayton72310352013-02-23 04:12:47 +0000264 m_sc.Clear(false);
Greg Clayton73b472d2010-10-27 03:32:59 +0000265 m_flags.Reset(0);
Greg Claytond9e416c2012-02-18 05:35:26 +0000266 ThreadSP thread_sp (GetThread());
267 if (thread_sp)
268 thread_sp->ClearStackFrames ();
Jason Molenda99618472013-11-04 11:02:52 +0000269 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270}
271
272const char *
273StackFrame::Disassemble ()
274{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000275 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 if (m_disassembly.GetSize() == 0)
277 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000278 ExecutionContext exe_ctx (shared_from_this());
279 Target *target = exe_ctx.GetTargetPtr();
280 if (target)
281 {
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000282 const char *plugin_name = nullptr;
283 const char *flavor = nullptr;
Greg Claytond9e416c2012-02-18 05:35:26 +0000284 Disassembler::Disassemble (target->GetDebugger(),
285 target->GetArchitecture(),
Jim Ingham0f063ba2013-03-02 00:26:47 +0000286 plugin_name,
287 flavor,
Greg Claytond9e416c2012-02-18 05:35:26 +0000288 exe_ctx,
289 0,
290 0,
291 0,
292 m_disassembly);
293 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 if (m_disassembly.GetSize() == 0)
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000295 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 }
297 return m_disassembly.GetData();
298}
299
Greg Clayton95897c62010-09-07 04:20:48 +0000300Block *
301StackFrame::GetFrameBlock ()
302{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000303 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
Greg Clayton95897c62010-09-07 04:20:48 +0000304 GetSymbolContext (eSymbolContextBlock);
305
306 if (m_sc.block)
307 {
308 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
309 if (inline_block)
310 {
311 // Use the block with the inlined function info
312 // as the frame block we want this frame to have only the variables
313 // for the inlined function and its non-inlined block child blocks.
314 return inline_block;
315 }
316 else
317 {
Ed Maste75500e72016-07-19 15:28:02 +0000318 // This block is not contained within any inlined function blocks
Greg Clayton95897c62010-09-07 04:20:48 +0000319 // with so we want to use the top most function block.
320 return &m_sc.function->GetBlock (false);
321 }
322 }
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000323 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000324}
325
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326//----------------------------------------------------------------------
327// Get the symbol context if we already haven't done so by resolving the
328// PC address as much as possible. This way when we pass around a
329// StackFrame object, everyone will have as much information as
330// possible and no one will ever have to look things up manually.
331//----------------------------------------------------------------------
332const SymbolContext&
333StackFrame::GetSymbolContext (uint32_t resolve_scope)
334{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000335 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 // Copy our internal symbol context into "sc".
Greg Clayton73b472d2010-10-27 03:32:59 +0000337 if ((m_flags.Get() & resolve_scope) != resolve_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 {
Greg Clayton75a03332012-11-29 00:53:06 +0000339 uint32_t resolved = 0;
340
341 // If the target was requested add that:
342 if (!m_sc.target_sp)
343 {
344 m_sc.target_sp = CalculateTarget();
345 if (m_sc.target_sp)
346 resolved |= eSymbolContextTarget;
347 }
348
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000349 // Resolve our PC to section offset if we haven't already done so
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 // and if we don't have a module. The resolved address section will
351 // contain the module to which it belongs
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000352 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
Greg Clayton9da7bd02010-08-24 21:05:24 +0000353 GetFrameCodeAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354
355 // If this is not frame zero, then we need to subtract 1 from the PC
356 // value when doing address lookups since the PC will be on the
357 // instruction following the function call instruction...
358
Greg Clayton9da7bd02010-08-24 21:05:24 +0000359 Address lookup_addr(GetFrameCodeAddress());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000360 if (m_frame_index > 0 && lookup_addr.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361 {
362 addr_t offset = lookup_addr.GetOffset();
363 if (offset > 0)
Jason Molendacf296752014-11-08 05:38:17 +0000364 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 lookup_addr.SetOffset(offset - 1);
Jason Molendacf296752014-11-08 05:38:17 +0000366
367 }
368 else
369 {
370 // lookup_addr is the start of a section. We need
371 // do the math on the actual load address and re-compute
372 // the section. We're working with a 'noreturn' function
373 // at the end of a section.
374 ThreadSP thread_sp (GetThread());
375 if (thread_sp)
376 {
377 TargetSP target_sp (thread_sp->CalculateTarget());
378 if (target_sp)
379 {
380 addr_t addr_minus_one = lookup_addr.GetLoadAddress(target_sp.get()) - 1;
381 lookup_addr.SetLoadAddress (addr_minus_one, target_sp.get());
382 }
383 else
384 {
385 lookup_addr.SetOffset(offset - 1);
386 }
387 }
388 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389 }
390
391 if (m_sc.module_sp)
392 {
393 // We have something in our stack frame symbol context, lets check
394 // if we haven't already tried to lookup one of those things. If we
395 // haven't then we will do the query.
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000396
397 uint32_t actual_resolve_scope = 0;
398
399 if (resolve_scope & eSymbolContextCompUnit)
400 {
401 if (m_flags.IsClear (eSymbolContextCompUnit))
402 {
403 if (m_sc.comp_unit)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000404 resolved |= eSymbolContextCompUnit;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000405 else
406 actual_resolve_scope |= eSymbolContextCompUnit;
407 }
408 }
409
410 if (resolve_scope & eSymbolContextFunction)
411 {
412 if (m_flags.IsClear (eSymbolContextFunction))
413 {
414 if (m_sc.function)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000415 resolved |= eSymbolContextFunction;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000416 else
417 actual_resolve_scope |= eSymbolContextFunction;
418 }
419 }
420
421 if (resolve_scope & eSymbolContextBlock)
422 {
423 if (m_flags.IsClear (eSymbolContextBlock))
424 {
425 if (m_sc.block)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000426 resolved |= eSymbolContextBlock;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000427 else
428 actual_resolve_scope |= eSymbolContextBlock;
429 }
430 }
431
432 if (resolve_scope & eSymbolContextSymbol)
433 {
434 if (m_flags.IsClear (eSymbolContextSymbol))
435 {
436 if (m_sc.symbol)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000437 resolved |= eSymbolContextSymbol;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000438 else
439 actual_resolve_scope |= eSymbolContextSymbol;
440 }
441 }
442
443 if (resolve_scope & eSymbolContextLineEntry)
444 {
445 if (m_flags.IsClear (eSymbolContextLineEntry))
446 {
447 if (m_sc.line_entry.IsValid())
Greg Clayton9da7bd02010-08-24 21:05:24 +0000448 resolved |= eSymbolContextLineEntry;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000449 else
450 actual_resolve_scope |= eSymbolContextLineEntry;
451 }
452 }
453
454 if (actual_resolve_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 {
456 // We might be resolving less information than what is already
457 // in our current symbol context so resolve into a temporary
458 // symbol context "sc" so we don't clear out data we have
459 // already found in "m_sc"
460 SymbolContext sc;
461 // Set flags that indicate what we have tried to resolve
Greg Clayton9da7bd02010-08-24 21:05:24 +0000462 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000463 // Only replace what we didn't already have as we may have
464 // information for an inlined function scope that won't match
465 // what a standard lookup by address would match
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000466 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000467 m_sc.comp_unit = sc.comp_unit;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000468 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000469 m_sc.function = sc.function;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000470 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000471 m_sc.block = sc.block;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000472 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000473 m_sc.symbol = sc.symbol;
Greg Clayton75a03332012-11-29 00:53:06 +0000474 if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid())
475 {
Greg Clayton9da7bd02010-08-24 21:05:24 +0000476 m_sc.line_entry = sc.line_entry;
Ted Woodward911d5782016-05-11 22:46:53 +0000477 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
Greg Clayton75a03332012-11-29 00:53:06 +0000478 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 }
480 }
481 else
482 {
483 // If we don't have a module, then we can't have the compile unit,
484 // function, block, line entry or symbol, so we can safely call
485 // ResolveSymbolContextForAddress with our symbol context member m_sc.
Greg Clayton9da7bd02010-08-24 21:05:24 +0000486 if (m_sc.target_sp)
Sean Callananf4be2272013-02-21 20:54:33 +0000487 {
Greg Clayton75a03332012-11-29 00:53:06 +0000488 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
Sean Callananf4be2272013-02-21 20:54:33 +0000489 }
Greg Clayton9da7bd02010-08-24 21:05:24 +0000490 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491
492 // Update our internal flags so we remember what we have tried to locate so
493 // we don't have to keep trying when more calls to this function are made.
Greg Clayton9da7bd02010-08-24 21:05:24 +0000494 // We might have dug up more information that was requested (for example
495 // if we were asked to only get the block, we will have gotten the
496 // compile unit, and function) so set any additional bits that we resolved
497 m_flags.Set (resolve_scope | resolved);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498 }
499
500 // Return the symbol context with everything that was possible to resolve
501 // resolved.
502 return m_sc;
503}
504
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000505VariableList *
Greg Clayton288bdf92010-09-02 02:59:18 +0000506StackFrame::GetVariableList (bool get_file_globals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000508 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 if (m_flags.IsClear(RESOLVED_VARIABLES))
510 {
511 m_flags.Set(RESOLVED_VARIABLES);
512
Greg Clayton95897c62010-09-07 04:20:48 +0000513 Block *frame_block = GetFrameBlock();
514
515 if (frame_block)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 {
Greg Clayton95897c62010-09-07 04:20:48 +0000517 const bool get_child_variables = true;
518 const bool can_create = true;
Greg Claytonc662ec82011-06-17 22:10:16 +0000519 const bool stop_if_child_block_is_inlined_function = true;
520 m_variable_list_sp.reset(new VariableList());
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000521 frame_block->AppendBlockVariables(can_create,
522 get_child_variables,
523 stop_if_child_block_is_inlined_function,
Greg Claytona32532b2016-04-25 21:54:10 +0000524 [this](Variable* v) { return true; },
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000525 m_variable_list_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 }
Sean Callanan7c0962d2010-11-01 04:38:59 +0000527 }
528
529 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
530 get_file_globals)
531 {
532 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
Greg Clayton288bdf92010-09-02 02:59:18 +0000533
Sean Callanan7c0962d2010-11-01 04:38:59 +0000534 if (m_flags.IsClear (eSymbolContextCompUnit))
535 GetSymbolContext (eSymbolContextCompUnit);
536
537 if (m_sc.comp_unit)
Greg Clayton288bdf92010-09-02 02:59:18 +0000538 {
Sean Callanan7c0962d2010-11-01 04:38:59 +0000539 VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
540 if (m_variable_list_sp)
541 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
542 else
543 m_variable_list_sp = global_variable_list_sp;
Greg Clayton288bdf92010-09-02 02:59:18 +0000544 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 }
Sean Callanan7c0962d2010-11-01 04:38:59 +0000546
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 return m_variable_list_sp.get();
548}
549
Greg Claytond41f0322011-08-02 23:35:43 +0000550VariableListSP
Jim Inghamcef46172016-04-26 00:29:59 +0000551StackFrame::GetInScopeVariableList (bool get_file_globals, bool must_have_valid_location)
Greg Claytond41f0322011-08-02 23:35:43 +0000552{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000553 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda99618472013-11-04 11:02:52 +0000554 // We can't fetch variable information for a history stack frame.
555 if (m_is_history_frame)
556 return VariableListSP();
557
Greg Claytond41f0322011-08-02 23:35:43 +0000558 VariableListSP var_list_sp(new VariableList);
559 GetSymbolContext (eSymbolContextCompUnit | eSymbolContextBlock);
560
561 if (m_sc.block)
562 {
563 const bool can_create = true;
564 const bool get_parent_variables = true;
565 const bool stop_if_block_is_inlined_function = true;
566 m_sc.block->AppendVariables (can_create,
567 get_parent_variables,
568 stop_if_block_is_inlined_function,
Jim Inghamcef46172016-04-26 00:29:59 +0000569 [this, must_have_valid_location](Variable* v)
570 {
571 return v->IsInScope(this) && (!must_have_valid_location || v->LocationIsValidForFrame(this));
572 },
Greg Claytond41f0322011-08-02 23:35:43 +0000573 var_list_sp.get());
574 }
575
Siva Chandrab90168f2016-02-02 23:49:41 +0000576 if (m_sc.comp_unit && get_file_globals)
Greg Claytond41f0322011-08-02 23:35:43 +0000577 {
578 VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
579 if (global_variable_list_sp)
580 var_list_sp->AddVariables (global_variable_list_sp.get());
581 }
582
583 return var_list_sp;
584}
585
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000586ValueObjectSP
Greg Clayton685c88c2012-07-14 00:53:55 +0000587StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr,
Greg Clayton4d122c42011-09-17 08:33:22 +0000588 DynamicValueType use_dynamic,
Jim Ingham2837b762011-05-04 03:43:18 +0000589 uint32_t options,
Greg Clayton4d122c42011-09-17 08:33:22 +0000590 VariableSP &var_sp,
Jim Ingham2837b762011-05-04 03:43:18 +0000591 Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000592{
Jason Molenda99618472013-11-04 11:02:52 +0000593 // We can't fetch variable information for a history stack frame.
594 if (m_is_history_frame)
595 return ValueObjectSP();
Greg Clayton54979cd2010-12-15 05:08:08 +0000596
597 if (var_expr_cstr && var_expr_cstr[0])
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000598 {
Greg Clayton6d5e68e2011-01-20 19:27:18 +0000599 const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
600 const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
Enrico Granata27b625e2011-08-09 01:04:56 +0000601 const bool no_synth_child = (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
Enrico Granata58ad3342011-08-19 21:56:10 +0000602 //const bool no_synth_array = (options & eExpressionPathOptionsNoSyntheticArrayRange) != 0;
Greg Clayton54979cd2010-12-15 05:08:08 +0000603 error.Clear();
604 bool deref = false;
605 bool address_of = false;
606 ValueObjectSP valobj_sp;
607 const bool get_file_globals = true;
Greg Claytond41f0322011-08-02 23:35:43 +0000608 // When looking up a variable for an expression, we need only consider the
609 // variables that are in scope.
610 VariableListSP var_list_sp (GetInScopeVariableList (get_file_globals));
611 VariableList *variable_list = var_list_sp.get();
Greg Clayton54979cd2010-12-15 05:08:08 +0000612
613 if (variable_list)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000614 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000615 // If first character is a '*', then show pointer contents
616 const char *var_expr = var_expr_cstr;
617 if (var_expr[0] == '*')
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000618 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000619 deref = true;
620 var_expr++; // Skip the '*'
621 }
622 else if (var_expr[0] == '&')
623 {
624 address_of = true;
625 var_expr++; // Skip the '&'
626 }
627
628 std::string var_path (var_expr);
629 size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
630 StreamString var_expr_path_strm;
631
632 ConstString name_const_string;
633 if (separator_idx == std::string::npos)
634 name_const_string.SetCString (var_path.c_str());
635 else
636 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
637
Paul Herman10bc1a42015-08-18 22:46:57 +0000638 var_sp = variable_list->FindVariable(name_const_string, false);
Greg Clayton685c88c2012-07-14 00:53:55 +0000639
640 bool synthetically_added_instance_object = false;
641
642 if (var_sp)
643 {
644 var_path.erase (0, name_const_string.GetLength ());
645 }
Enrico Granata46252392015-11-19 22:28:58 +0000646
647 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess))
Greg Clayton685c88c2012-07-14 00:53:55 +0000648 {
649 // Check for direct ivars access which helps us with implicit
650 // access to ivars with the "this->" or "self->"
651 GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock);
652 lldb::LanguageType method_language = eLanguageTypeUnknown;
653 bool is_instance_method = false;
654 ConstString method_object_name;
655 if (m_sc.GetFunctionMethodInfo (method_language, is_instance_method, method_object_name))
656 {
657 if (is_instance_method && method_object_name)
658 {
659 var_sp = variable_list->FindVariable(method_object_name);
660 if (var_sp)
661 {
662 separator_idx = 0;
663 var_path.insert(0, "->");
664 synthetically_added_instance_object = true;
665 }
666 }
667 }
668 }
Enrico Granata46252392015-11-19 22:28:58 +0000669
670 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions))
671 {
672 // Check if any anonymous unions are there which contain a variable with the name we need
673 for (size_t i = 0;
674 i < variable_list->GetSize();
675 i++)
676 {
677 if (VariableSP variable_sp = variable_list->GetVariableAtIndex(i))
678 {
679 if (variable_sp->GetName().IsEmpty())
680 {
681 if (Type *var_type = variable_sp->GetType())
682 {
683 if (var_type->GetForwardCompilerType().IsAnonymousType())
684 {
685 valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
686 if (!valobj_sp)
687 return valobj_sp;
688 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
689 if (valobj_sp)
690 break;
691 }
692 }
693 }
694 }
695 }
696 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000697
Enrico Granata46252392015-11-19 22:28:58 +0000698 if (var_sp && !valobj_sp)
Greg Clayton54979cd2010-12-15 05:08:08 +0000699 {
Jim Ingham2837b762011-05-04 03:43:18 +0000700 valobj_sp = GetValueObjectForFrameVariable (var_sp, use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +0000701 if (!valobj_sp)
702 return valobj_sp;
Enrico Granata46252392015-11-19 22:28:58 +0000703 }
704 if (valobj_sp)
705 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000706 // We are dumping at least one child
707 while (separator_idx != std::string::npos)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000708 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000709 // Calculate the next separator index ahead of time
710 ValueObjectSP child_valobj_sp;
711 const char separator_type = var_path[0];
712 switch (separator_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000713 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000714 case '-':
715 if (var_path.size() >= 2 && var_path[1] != '>')
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000716 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000717
Greg Clayton6d5e68e2011-01-20 19:27:18 +0000718 if (no_fragile_ivar)
719 {
720 // Make sure we aren't trying to deref an objective
721 // C ivar if this is not allowed
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000722 const uint32_t pointer_type_flags = valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
Enrico Granata622be232014-10-21 20:52:14 +0000723 if ((pointer_type_flags & eTypeIsObjC) &&
724 (pointer_type_flags & eTypeIsPointer))
Greg Clayton6d5e68e2011-01-20 19:27:18 +0000725 {
726 // This was an objective C object pointer and
727 // it was requested we skip any fragile ivars
728 // so return nothing here
729 return ValueObjectSP();
730 }
731 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000732 var_path.erase (0, 1); // Remove the '-'
Jason Molenda62e06812016-02-16 04:14:33 +0000733 LLVM_FALLTHROUGH;
Greg Clayton54979cd2010-12-15 05:08:08 +0000734 case '.':
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000735 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000736 const bool expr_is_ptr = var_path[0] == '>';
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000737
Greg Clayton54979cd2010-12-15 05:08:08 +0000738 var_path.erase (0, 1); // Remove the '.' or '>'
739 separator_idx = var_path.find_first_of(".-[");
740 ConstString child_name;
741 if (separator_idx == std::string::npos)
742 child_name.SetCString (var_path.c_str());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000743 else
Greg Clayton54979cd2010-12-15 05:08:08 +0000744 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
745
746 if (check_ptr_vs_member)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000747 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000748 // We either have a pointer type and need to verify
749 // valobj_sp is a pointer, or we have a member of a
750 // class/union/struct being accessed with the . syntax
751 // and need to verify we don't have a pointer.
752 const bool actual_is_ptr = valobj_sp->IsPointerType ();
753
754 if (actual_is_ptr != expr_is_ptr)
755 {
756 // Incorrect use of "." with a pointer, or "->" with
757 // a class/union/struct instance or reference.
Greg Clayton6beaaa62011-01-17 03:46:26 +0000758 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Clayton54979cd2010-12-15 05:08:08 +0000759 if (actual_is_ptr)
760 error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
761 var_expr_path_strm.GetString().c_str(),
762 child_name.GetCString(),
763 var_expr_path_strm.GetString().c_str(),
764 var_path.c_str());
765 else
766 error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
767 var_expr_path_strm.GetString().c_str(),
768 child_name.GetCString(),
769 var_expr_path_strm.GetString().c_str(),
770 var_path.c_str());
771 return ValueObjectSP();
772 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000773 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000774 child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000775 if (!child_valobj_sp)
776 {
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000777 if (!no_synth_child)
Enrico Granata86cc9822012-03-19 22:58:49 +0000778 {
779 child_valobj_sp = valobj_sp->GetSyntheticValue();
780 if (child_valobj_sp)
781 child_valobj_sp = child_valobj_sp->GetChildMemberWithName (child_name, true);
782 }
Enrico Granata8c9d3562011-08-11 17:08:01 +0000783
784 if (no_synth_child || !child_valobj_sp)
Greg Clayton54979cd2010-12-15 05:08:08 +0000785 {
Enrico Granata8c9d3562011-08-11 17:08:01 +0000786 // No child member with name "child_name"
Greg Clayton685c88c2012-07-14 00:53:55 +0000787 if (synthetically_added_instance_object)
Enrico Granata8c9d3562011-08-11 17:08:01 +0000788 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000789 // We added a "this->" or "self->" to the beginning of the expression
790 // and this is the first pointer ivar access, so just return the normal
791 // error
792 error.SetErrorStringWithFormat("no variable or instance variable named '%s' found in this frame",
793 name_const_string.GetCString());
Enrico Granata8c9d3562011-08-11 17:08:01 +0000794 }
795 else
796 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000797 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
798 if (child_name)
799 {
800 error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
801 child_name.GetCString(),
802 valobj_sp->GetTypeName().AsCString("<invalid type>"),
803 var_expr_path_strm.GetString().c_str());
804 }
805 else
806 {
807 error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
808 var_expr_path_strm.GetString().c_str(),
809 var_expr_cstr);
810 }
Enrico Granata8c9d3562011-08-11 17:08:01 +0000811 }
812 return ValueObjectSP();
Greg Clayton54979cd2010-12-15 05:08:08 +0000813 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000814 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000815 synthetically_added_instance_object = false;
Greg Clayton54979cd2010-12-15 05:08:08 +0000816 // Remove the child name from the path
817 var_path.erase(0, child_name.GetLength());
Greg Clayton4d122c42011-09-17 08:33:22 +0000818 if (use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +0000819 {
Jim Ingham2837b762011-05-04 03:43:18 +0000820 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
Jim Ingham78a685a2011-04-16 00:01:13 +0000821 if (dynamic_value_sp)
822 child_valobj_sp = dynamic_value_sp;
823 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000824 }
825 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000826
Greg Clayton54979cd2010-12-15 05:08:08 +0000827 case '[':
828 // Array member access, or treating pointer as an array
829 if (var_path.size() > 2) // Need at least two brackets and a number
830 {
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000831 char *end = nullptr;
Greg Clayton1a65ae12011-01-25 23:55:37 +0000832 long child_index = ::strtol (&var_path[1], &end, 0);
Enrico Granata9fc19442011-07-06 02:13:41 +0000833 if (end && *end == ']'
834 && *(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 Clayton54979cd2010-12-15 05:08:08 +0000835 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000836 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +0000837 {
838 // what we have is *ptr[low]. the most similar C++ syntax is to deref ptr
839 // and extract bit low out of it. reading array item low
840 // would be done by saying ptr[low], without a deref * sign
841 Error error;
842 ValueObjectSP temp(valobj_sp->Dereference(error));
843 if (error.Fail())
844 {
845 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
846 error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
847 valobj_sp->GetTypeName().AsCString("<invalid type>"),
848 var_expr_path_strm.GetString().c_str());
849 return ValueObjectSP();
850 }
851 valobj_sp = temp;
852 deref = false;
853 }
Greg Clayton99558cc42015-08-24 23:46:31 +0000854 else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +0000855 {
856 // what we have is *arr[low]. the most similar C++ syntax is to get arr[0]
857 // (an operation that is equivalent to deref-ing arr)
858 // and extract bit low out of it. reading array item low
859 // would be done by saying arr[low], without a deref * sign
860 Error error;
861 ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
862 if (error.Fail())
863 {
864 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
865 error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
866 valobj_sp->GetTypeName().AsCString("<invalid type>"),
867 var_expr_path_strm.GetString().c_str());
868 return ValueObjectSP();
869 }
870 valobj_sp = temp;
871 deref = false;
872 }
873
Greg Clayton4ef877f2012-12-06 02:33:54 +0000874 bool is_incomplete_array = false;
Greg Clayton54979cd2010-12-15 05:08:08 +0000875 if (valobj_sp->IsPointerType ())
876 {
Sean Callanan226b70c2012-03-08 02:39:03 +0000877 bool is_objc_pointer = true;
878
Greg Clayton99558cc42015-08-24 23:46:31 +0000879 if (valobj_sp->GetCompilerType().GetMinimumLanguage() != eLanguageTypeObjC)
Sean Callanan226b70c2012-03-08 02:39:03 +0000880 is_objc_pointer = false;
Greg Clayton99558cc42015-08-24 23:46:31 +0000881 else if (!valobj_sp->GetCompilerType().IsPointerType())
Sean Callanan226b70c2012-03-08 02:39:03 +0000882 is_objc_pointer = false;
883
884 if (no_synth_child && is_objc_pointer)
Greg Clayton54979cd2010-12-15 05:08:08 +0000885 {
Sean Callanan226b70c2012-03-08 02:39:03 +0000886 error.SetErrorStringWithFormat("\"(%s) %s\" is an Objective-C pointer, and cannot be subscripted",
887 valobj_sp->GetTypeName().AsCString("<invalid type>"),
888 var_expr_path_strm.GetString().c_str());
889
890 return ValueObjectSP();
891 }
892 else if (is_objc_pointer)
893 {
Enrico Granata27b625e2011-08-09 01:04:56 +0000894 // dereferencing ObjC variables is not valid.. so let's try and recur to synthetic children
Enrico Granata86cc9822012-03-19 22:58:49 +0000895 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000896 if (!synthetic /* no synthetic */
Enrico Granata27b625e2011-08-09 01:04:56 +0000897 || synthetic == valobj_sp) /* synthetic is the same as the original object */
898 {
899 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
900 error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
901 valobj_sp->GetTypeName().AsCString("<invalid type>"),
902 var_expr_path_strm.GetString().c_str());
903 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000904 else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
Enrico Granata27b625e2011-08-09 01:04:56 +0000905 {
906 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000907 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000908 child_index,
909 valobj_sp->GetTypeName().AsCString("<invalid type>"),
910 var_expr_path_strm.GetString().c_str());
911 }
912 else
913 {
914 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
915 if (!child_valobj_sp)
916 {
917 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000918 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000919 child_index,
920 valobj_sp->GetTypeName().AsCString("<invalid type>"),
921 var_expr_path_strm.GetString().c_str());
922 }
923 }
924 }
925 else
926 {
Bruce Mitchener11d86362015-02-26 23:55:39 +0000927 child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +0000928 if (!child_valobj_sp)
929 {
930 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000931 error.SetErrorStringWithFormat ("failed to use pointer as array for index %ld for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000932 child_index,
933 valobj_sp->GetTypeName().AsCString("<invalid type>"),
934 var_expr_path_strm.GetString().c_str());
935 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000936 }
937 }
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000938 else if (valobj_sp->GetCompilerType().IsArrayType(nullptr, nullptr, &is_incomplete_array))
Greg Clayton54979cd2010-12-15 05:08:08 +0000939 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000940 // Pass false to dynamic_value here so we can tell the difference between
941 // no dynamic value and no member of this type...
Greg Clayton54979cd2010-12-15 05:08:08 +0000942 child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000943 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
Greg Clayton4ef877f2012-12-06 02:33:54 +0000944 child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
945
Greg Clayton54979cd2010-12-15 05:08:08 +0000946 if (!child_valobj_sp)
947 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000948 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000949 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Greg Clayton54979cd2010-12-15 05:08:08 +0000950 child_index,
951 valobj_sp->GetTypeName().AsCString("<invalid type>"),
952 var_expr_path_strm.GetString().c_str());
953 }
954 }
Greg Clayton99558cc42015-08-24 23:46:31 +0000955 else if (valobj_sp->GetCompilerType().IsScalarType())
Enrico Granata9fc19442011-07-06 02:13:41 +0000956 {
957 // this is a bitfield asking to display just one bit
958 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, child_index, true);
959 if (!child_valobj_sp)
960 {
961 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000962 error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
Enrico Granata9fc19442011-07-06 02:13:41 +0000963 child_index, child_index,
964 valobj_sp->GetTypeName().AsCString("<invalid type>"),
965 var_expr_path_strm.GetString().c_str());
966 }
967 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000968 else
969 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000970 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
Enrico Granata27b625e2011-08-09 01:04:56 +0000971 if (no_synth_child /* synthetic is forbidden */ ||
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000972 !synthetic /* no synthetic */
Enrico Granata27b625e2011-08-09 01:04:56 +0000973 || synthetic == valobj_sp) /* synthetic is the same as the original object */
974 {
975 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
976 error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
977 valobj_sp->GetTypeName().AsCString("<invalid type>"),
978 var_expr_path_strm.GetString().c_str());
979 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000980 else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
Enrico Granata27b625e2011-08-09 01:04:56 +0000981 {
982 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000983 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000984 child_index,
985 valobj_sp->GetTypeName().AsCString("<invalid type>"),
986 var_expr_path_strm.GetString().c_str());
987 }
988 else
989 {
990 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
991 if (!child_valobj_sp)
992 {
993 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000994 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000995 child_index,
996 valobj_sp->GetTypeName().AsCString("<invalid type>"),
997 var_expr_path_strm.GetString().c_str());
998 }
999 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001000 }
1001
1002 if (!child_valobj_sp)
1003 {
1004 // Invalid array index...
1005 return ValueObjectSP();
1006 }
1007
1008 // Erase the array member specification '[%i]' where
1009 // %i is the array index
1010 var_path.erase(0, (end - var_path.c_str()) + 1);
1011 separator_idx = var_path.find_first_of(".-[");
Greg Clayton4d122c42011-09-17 08:33:22 +00001012 if (use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001013 {
Jim Ingham2837b762011-05-04 03:43:18 +00001014 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
Jim Ingham78a685a2011-04-16 00:01:13 +00001015 if (dynamic_value_sp)
1016 child_valobj_sp = dynamic_value_sp;
1017 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001018 // Break out early from the switch since we were
1019 // able to find the child member
1020 break;
1021 }
Enrico Granata20edcdb2011-07-19 18:03:25 +00001022 else if (end && *end == '-')
Enrico Granata9fc19442011-07-06 02:13:41 +00001023 {
1024 // this is most probably a BitField, let's take a look
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001025 char *real_end = nullptr;
Enrico Granata9fc19442011-07-06 02:13:41 +00001026 long final_index = ::strtol (end+1, &real_end, 0);
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001027 bool expand_bitfield = true;
Enrico Granata20edcdb2011-07-19 18:03:25 +00001028 if (real_end && *real_end == ']')
Enrico Granata9fc19442011-07-06 02:13:41 +00001029 {
1030 // if the format given is [high-low], swap range
Enrico Granata20edcdb2011-07-19 18:03:25 +00001031 if (child_index > final_index)
Enrico Granata9fc19442011-07-06 02:13:41 +00001032 {
1033 long temp = child_index;
1034 child_index = final_index;
1035 final_index = temp;
1036 }
1037
Greg Clayton99558cc42015-08-24 23:46:31 +00001038 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +00001039 {
1040 // what we have is *ptr[low-high]. the most similar C++ syntax is to deref ptr
1041 // and extract bits low thru high out of it. reading array items low thru high
1042 // would be done by saying ptr[low-high], without a deref * sign
1043 Error error;
1044 ValueObjectSP temp(valobj_sp->Dereference(error));
1045 if (error.Fail())
1046 {
1047 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1048 error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
1049 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1050 var_expr_path_strm.GetString().c_str());
1051 return ValueObjectSP();
1052 }
1053 valobj_sp = temp;
1054 deref = false;
1055 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001056 else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +00001057 {
1058 // what we have is *arr[low-high]. the most similar C++ syntax is to get arr[0]
1059 // (an operation that is equivalent to deref-ing arr)
1060 // and extract bits low thru high out of it. reading array items low thru high
1061 // would be done by saying arr[low-high], without a deref * sign
1062 Error error;
1063 ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
1064 if (error.Fail())
1065 {
1066 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1067 error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
1068 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1069 var_expr_path_strm.GetString().c_str());
1070 return ValueObjectSP();
1071 }
1072 valobj_sp = temp;
1073 deref = false;
1074 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001075 /*else if (valobj_sp->IsArrayType() || valobj_sp->IsPointerType())
Enrico Granata9fc19442011-07-06 02:13:41 +00001076 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001077 child_valobj_sp = valobj_sp->GetSyntheticArrayRangeChild(child_index, final_index, true);
1078 expand_bitfield = false;
1079 if (!child_valobj_sp)
1080 {
1081 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1082 error.SetErrorStringWithFormat ("array range %i-%i is not valid for \"(%s) %s\"",
1083 child_index, final_index,
1084 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1085 var_expr_path_strm.GetString().c_str());
1086 }
1087 }*/
1088
1089 if (expand_bitfield)
1090 {
1091 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1092 if (!child_valobj_sp)
1093 {
1094 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +00001095 error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001096 child_index, final_index,
1097 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1098 var_expr_path_strm.GetString().c_str());
1099 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001100 }
1101 }
1102
1103 if (!child_valobj_sp)
1104 {
1105 // Invalid bitfield range...
1106 return ValueObjectSP();
1107 }
1108
1109 // Erase the bitfield member specification '[%i-%i]' where
1110 // %i is the index
1111 var_path.erase(0, (real_end - var_path.c_str()) + 1);
1112 separator_idx = var_path.find_first_of(".-[");
Greg Clayton4d122c42011-09-17 08:33:22 +00001113 if (use_dynamic != eNoDynamicValues)
Enrico Granata9fc19442011-07-06 02:13:41 +00001114 {
1115 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
1116 if (dynamic_value_sp)
1117 child_valobj_sp = dynamic_value_sp;
1118 }
1119 // Break out early from the switch since we were
1120 // able to find the child member
1121 break;
1122
1123 }
1124 }
1125 else
1126 {
1127 error.SetErrorStringWithFormat("invalid square bracket encountered after \"%s\" in \"%s\"",
1128 var_expr_path_strm.GetString().c_str(),
1129 var_path.c_str());
Greg Clayton54979cd2010-12-15 05:08:08 +00001130 }
1131 return ValueObjectSP();
1132
1133 default:
1134 // Failure...
1135 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001136 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Clayton54979cd2010-12-15 05:08:08 +00001137 error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
1138 separator_type,
1139 var_expr_path_strm.GetString().c_str(),
1140 var_path.c_str());
1141
1142 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001143 }
1144 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001145
Greg Clayton54979cd2010-12-15 05:08:08 +00001146 if (child_valobj_sp)
1147 valobj_sp = child_valobj_sp;
1148
1149 if (var_path.empty())
1150 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001151 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001152 if (valobj_sp)
1153 {
1154 if (deref)
1155 {
Greg Claytonaf67cec2010-12-20 20:49:23 +00001156 ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
Greg Clayton54979cd2010-12-15 05:08:08 +00001157 valobj_sp = deref_valobj_sp;
1158 }
1159 else if (address_of)
1160 {
1161 ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
1162 valobj_sp = address_of_valobj_sp;
1163 }
1164 }
1165 return valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001166 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001167 else
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001168 {
Jim Ingham2837b762011-05-04 03:43:18 +00001169 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
1170 name_const_string.GetCString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001171 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001172 }
1173 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001174 else
1175 {
1176 error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1177 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001178 return ValueObjectSP();
1179}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180
1181bool
1182StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
1183{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001184 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001185 if (!m_cfa_is_valid)
Jason Molenda99618472013-11-04 11:02:52 +00001186 {
1187 m_frame_base_error.SetErrorString("No frame base available for this historical stack frame.");
1188 return false;
1189 }
1190
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 if (m_flags.IsClear(GOT_FRAME_BASE))
1192 {
1193 if (m_sc.function)
1194 {
1195 m_frame_base.Clear();
1196 m_frame_base_error.Clear();
1197
1198 m_flags.Set(GOT_FRAME_BASE);
Greg Claytond9e416c2012-02-18 05:35:26 +00001199 ExecutionContext exe_ctx (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001200 Value expr_value;
Greg Clayton016a95e2010-09-14 02:20:48 +00001201 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1202 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
Greg Claytond9e416c2012-02-18 05:35:26 +00001203 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.GetTargetPtr());
Greg Clayton016a95e2010-09-14 02:20:48 +00001204
Tamas Berghammer5b42c7a2016-02-26 14:21:10 +00001205 if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx,
1206 nullptr,
1207 nullptr,
1208 nullptr,
1209 loclist_base_addr,
1210 nullptr,
1211 nullptr,
1212 expr_value,
1213 &m_frame_base_error) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 {
1215 // We should really have an error if evaluate returns, but in case
1216 // we don't, lets set the error to something at least.
1217 if (m_frame_base_error.Success())
1218 m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
1219 }
1220 else
1221 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001222 m_frame_base = expr_value.ResolveValue(&exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223 }
1224 }
1225 else
1226 {
1227 m_frame_base_error.SetErrorString ("No function in symbol context.");
1228 }
1229 }
1230
1231 if (m_frame_base_error.Success())
1232 frame_base = m_frame_base;
1233
1234 if (error_ptr)
1235 *error_ptr = m_frame_base_error;
1236 return m_frame_base_error.Success();
1237}
1238
Greg Clayton5ccbd292011-01-06 22:15:06 +00001239RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240StackFrame::GetRegisterContext ()
1241{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001242 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001243 if (!m_reg_context_sp)
Greg Claytond9e416c2012-02-18 05:35:26 +00001244 {
1245 ThreadSP thread_sp (GetThread());
1246 if (thread_sp)
1247 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame (this);
1248 }
Greg Clayton5ccbd292011-01-06 22:15:06 +00001249 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250}
1251
1252bool
1253StackFrame::HasDebugInformation ()
1254{
Greg Clayton9da7bd02010-08-24 21:05:24 +00001255 GetSymbolContext (eSymbolContextLineEntry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 return m_sc.line_entry.IsValid();
1257}
1258
Greg Clayton288bdf92010-09-02 02:59:18 +00001259ValueObjectSP
Greg Clayton4d122c42011-09-17 08:33:22 +00001260StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001262 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton288bdf92010-09-02 02:59:18 +00001263 ValueObjectSP valobj_sp;
Jason Molenda99618472013-11-04 11:02:52 +00001264 if (m_is_history_frame)
1265 {
1266 return valobj_sp;
1267 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001268 VariableList *var_list = GetVariableList (true);
1269 if (var_list)
1270 {
1271 // Make sure the variable is a frame variable
1272 const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
1273 const uint32_t num_variables = var_list->GetSize();
1274 if (var_idx < num_variables)
1275 {
1276 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001277 if (!valobj_sp)
Greg Clayton288bdf92010-09-02 02:59:18 +00001278 {
1279 if (m_variable_list_value_objects.GetSize() < num_variables)
1280 m_variable_list_value_objects.Resize(num_variables);
Jim Ingham58b59f92011-04-22 23:53:53 +00001281 valobj_sp = ValueObjectVariable::Create (this, variable_sp);
Greg Clayton288bdf92010-09-02 02:59:18 +00001282 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
1283 }
1284 }
1285 }
Greg Clayton4d122c42011-09-17 08:33:22 +00001286 if (use_dynamic != eNoDynamicValues && valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00001287 {
Jim Ingham2837b762011-05-04 03:43:18 +00001288 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue (use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001289 if (dynamic_sp)
1290 return dynamic_sp;
1291 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001292 return valobj_sp;
1293}
1294
1295ValueObjectSP
Greg Clayton4d122c42011-09-17 08:33:22 +00001296StackFrame::TrackGlobalVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
Greg Clayton288bdf92010-09-02 02:59:18 +00001297{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001298 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda99618472013-11-04 11:02:52 +00001299 if (m_is_history_frame)
1300 return ValueObjectSP();
1301
Greg Clayton288bdf92010-09-02 02:59:18 +00001302 // Check to make sure we aren't already tracking this variable?
Jim Ingham78a685a2011-04-16 00:01:13 +00001303 ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp, use_dynamic));
Greg Clayton288bdf92010-09-02 02:59:18 +00001304 if (!valobj_sp)
1305 {
1306 // We aren't already tracking this global
1307 VariableList *var_list = GetVariableList (true);
1308 // If this frame has no variables, create a new list
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001309 if (var_list == nullptr)
Greg Clayton288bdf92010-09-02 02:59:18 +00001310 m_variable_list_sp.reset (new VariableList());
1311
1312 // Add the global/static variable to this frame
1313 m_variable_list_sp->AddVariable (variable_sp);
1314
1315 // Now make a value object for it so we can track its changes
Jim Ingham78a685a2011-04-16 00:01:13 +00001316 valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
Greg Clayton288bdf92010-09-02 02:59:18 +00001317 }
1318 return valobj_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319}
1320
Jim Ingham6b8379c2010-08-26 20:44:45 +00001321bool
1322StackFrame::IsInlined ()
1323{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001324 if (m_sc.block == nullptr)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001325 GetSymbolContext (eSymbolContextBlock);
1326 if (m_sc.block)
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001327 return m_sc.block->GetContainingInlinedBlock() != nullptr;
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001328 return false;
Jim Ingham6b8379c2010-08-26 20:44:45 +00001329}
1330
Dawn Perchik009d1102015-09-04 01:02:30 +00001331lldb::LanguageType
1332StackFrame::GetLanguage ()
1333{
1334 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1335 if (cu)
1336 return cu->GetLanguage();
1337 return lldb::eLanguageTypeUnknown;
1338}
1339
Enrico Granata592afe72016-03-15 21:50:51 +00001340lldb::LanguageType
1341StackFrame::GuessLanguage ()
1342{
1343 LanguageType lang_type = GetLanguage();
1344
1345 if (lang_type == eLanguageTypeUnknown)
1346 {
1347 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1348 if (f)
1349 {
1350 lang_type = f->GetMangled().GuessLanguage();
1351 }
1352 }
1353
1354 return lang_type;
1355}
1356
Greg Claytond9e416c2012-02-18 05:35:26 +00001357TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358StackFrame::CalculateTarget ()
1359{
Greg Claytond9e416c2012-02-18 05:35:26 +00001360 TargetSP target_sp;
1361 ThreadSP thread_sp(GetThread());
1362 if (thread_sp)
1363 {
1364 ProcessSP process_sp (thread_sp->CalculateProcess());
1365 if (process_sp)
1366 target_sp = process_sp->CalculateTarget();
1367 }
1368 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369}
1370
Greg Claytond9e416c2012-02-18 05:35:26 +00001371ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372StackFrame::CalculateProcess ()
1373{
Greg Claytond9e416c2012-02-18 05:35:26 +00001374 ProcessSP process_sp;
1375 ThreadSP thread_sp(GetThread());
1376 if (thread_sp)
1377 process_sp = thread_sp->CalculateProcess();
1378 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379}
1380
Greg Claytond9e416c2012-02-18 05:35:26 +00001381ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001382StackFrame::CalculateThread ()
1383{
Greg Claytond9e416c2012-02-18 05:35:26 +00001384 return GetThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385}
1386
Jason Molendab57e4a12013-11-04 09:33:30 +00001387StackFrameSP
1388StackFrame::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389{
Greg Claytond9e416c2012-02-18 05:35:26 +00001390 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391}
1392
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393void
Greg Clayton0603aa92010-10-04 01:05:56 +00001394StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395{
Greg Claytond9e416c2012-02-18 05:35:26 +00001396 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397}
1398
1399void
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001400StackFrame::DumpUsingSettingsFormat (Stream *strm, const char *frame_marker)
Greg Clayton0603aa92010-10-04 01:05:56 +00001401{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001402 if (strm == nullptr)
Greg Clayton0603aa92010-10-04 01:05:56 +00001403 return;
1404
1405 GetSymbolContext(eSymbolContextEverything);
Greg Claytond9e416c2012-02-18 05:35:26 +00001406 ExecutionContext exe_ctx (shared_from_this());
Greg Clayton0603aa92010-10-04 01:05:56 +00001407 StreamString s;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001408
1409 if (frame_marker)
1410 s.PutCString(frame_marker);
1411
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001412 const FormatEntity::Entry *frame_format = nullptr;
Greg Claytond9e416c2012-02-18 05:35:26 +00001413 Target *target = exe_ctx.GetTargetPtr();
1414 if (target)
1415 frame_format = target->GetDebugger().GetFrameFormat();
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001416 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx, nullptr, nullptr, false, false))
Greg Clayton0603aa92010-10-04 01:05:56 +00001417 {
1418 strm->Write(s.GetData(), s.GetSize());
1419 }
1420 else
1421 {
1422 Dump (strm, true, false);
1423 strm->EOL();
1424 }
1425}
1426
1427void
Greg Clayton6dadd502010-09-02 21:44:10 +00001428StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001430 if (strm == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 return;
1432
1433 if (show_frame_index)
Greg Clayton1b72fcb2010-08-24 00:45:41 +00001434 strm->Printf("frame #%u: ", m_frame_index);
Greg Claytond9e416c2012-02-18 05:35:26 +00001435 ExecutionContext exe_ctx (shared_from_this());
1436 Target *target = exe_ctx.GetTargetPtr();
Daniel Malead01b2952012-11-29 21:49:15 +00001437 strm->Printf("0x%0*" PRIx64 " ",
Greg Claytond9e416c2012-02-18 05:35:26 +00001438 target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16,
1439 GetFrameCodeAddress().GetLoadAddress(target));
Greg Clayton9da7bd02010-08-24 21:05:24 +00001440 GetSymbolContext(eSymbolContextEverything);
Greg Clayton1b72fcb2010-08-24 00:45:41 +00001441 const bool show_module = true;
1442 const bool show_inline = true;
Jason Molendaaff1b352014-10-10 23:07:36 +00001443 const bool show_function_arguments = true;
Jason Molendac980fa92015-02-13 23:24:21 +00001444 const bool show_function_name = true;
Greg Claytond9e416c2012-02-18 05:35:26 +00001445 m_sc.DumpStopContext (strm,
1446 exe_ctx.GetBestExecutionContextScope(),
1447 GetFrameCodeAddress(),
1448 show_fullpaths,
1449 show_module,
Jason Molendaaff1b352014-10-10 23:07:36 +00001450 show_inline,
Jason Molendac980fa92015-02-13 23:24:21 +00001451 show_function_arguments,
1452 show_function_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453}
1454
Greg Clayton5082c5f2010-08-27 18:24:16 +00001455void
Jason Molendab57e4a12013-11-04 09:33:30 +00001456StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
Greg Clayton5082c5f2010-08-27 18:24:16 +00001457{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001458 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001459 assert (GetStackID() == prev_frame.GetStackID()); // TODO: remove this after some testing
Jason Molendab57e4a12013-11-04 09:33:30 +00001460 m_variable_list_sp = prev_frame.m_variable_list_sp;
1461 m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
1462 if (!m_disassembly.GetString().empty())
1463 m_disassembly.GetString().swap (m_disassembly.GetString());
Greg Clayton5082c5f2010-08-27 18:24:16 +00001464}
Greg Clayton68275d52010-08-27 21:47:54 +00001465
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001466void
Jason Molendab57e4a12013-11-04 09:33:30 +00001467StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001468{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001469 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton2cad65a2010-09-03 17:10:42 +00001470 assert (GetStackID() == curr_frame.GetStackID()); // TODO: remove this after some testing
Jason Molendab57e4a12013-11-04 09:33:30 +00001471 m_id.SetPC (curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1472 assert (GetThread() == curr_frame.GetThread());
1473 m_frame_index = curr_frame.m_frame_index;
1474 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1475 m_reg_context_sp = curr_frame.m_reg_context_sp;
1476 m_frame_code_addr = curr_frame.m_frame_code_addr;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001477 assert (!m_sc.target_sp || !curr_frame.m_sc.target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1478 assert (!m_sc.module_sp || !curr_frame.m_sc.module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1479 assert (m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1480 assert (m_sc.function == nullptr || curr_frame.m_sc.function == nullptr || m_sc.function == curr_frame.m_sc.function);
Jason Molendab57e4a12013-11-04 09:33:30 +00001481 m_sc = curr_frame.m_sc;
1482 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1483 m_flags.Set (m_sc.GetResolvedMask());
1484 m_frame_base.Clear();
1485 m_frame_base_error.Clear();
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001486}
1487
Greg Clayton2cad65a2010-09-03 17:10:42 +00001488bool
Jason Molendab57e4a12013-11-04 09:33:30 +00001489StackFrame::HasCachedData () const
1490{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001491 if (m_variable_list_sp)
Jason Molendab57e4a12013-11-04 09:33:30 +00001492 return true;
1493 if (m_variable_list_value_objects.GetSize() > 0)
1494 return true;
1495 if (!m_disassembly.GetString().empty())
1496 return true;
1497 return false;
1498}
1499
1500bool
Greg Clayton7260f622011-04-18 08:33:37 +00001501StackFrame::GetStatus (Stream& strm,
1502 bool show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001503 bool show_source,
1504 const char *frame_marker)
Greg Clayton7260f622011-04-18 08:33:37 +00001505{
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001506
Greg Clayton7260f622011-04-18 08:33:37 +00001507 if (show_frame_info)
1508 {
1509 strm.Indent();
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001510 DumpUsingSettingsFormat (&strm, frame_marker);
Greg Clayton7260f622011-04-18 08:33:37 +00001511 }
1512
1513 if (show_source)
1514 {
Greg Claytond9e416c2012-02-18 05:35:26 +00001515 ExecutionContext exe_ctx (shared_from_this());
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001516 bool have_source = false, have_debuginfo = false;
Greg Clayton67cc0632012-08-22 17:17:09 +00001517 Debugger::StopDisassemblyType disasm_display = Debugger::eStopDisassemblyTypeNever;
Greg Claytond9e416c2012-02-18 05:35:26 +00001518 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001519 if (target)
Greg Clayton7260f622011-04-18 08:33:37 +00001520 {
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001521 Debugger &debugger = target->GetDebugger();
1522 const uint32_t source_lines_before = debugger.GetStopSourceLineCount(true);
1523 const uint32_t source_lines_after = debugger.GetStopSourceLineCount(false);
1524 disasm_display = debugger.GetStopDisassemblyDisplay ();
Greg Claytone372b982011-11-21 21:44:34 +00001525
Todd Fiala6d1fbc92014-07-07 20:47:24 +00001526 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1527 if (m_sc.comp_unit && m_sc.line_entry.IsValid())
Greg Claytone372b982011-11-21 21:44:34 +00001528 {
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001529 have_debuginfo = true;
Todd Fiala6d1fbc92014-07-07 20:47:24 +00001530 if (source_lines_before > 0 || source_lines_after > 0)
Greg Claytone372b982011-11-21 21:44:34 +00001531 {
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001532 size_t num_lines = target->GetSourceManager().DisplaySourceLinesWithLineNumbers (m_sc.line_entry.file,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001533 m_sc.line_entry.line,
1534 source_lines_before,
1535 source_lines_after,
1536 "->",
Jason Molenda7cd81c52013-04-29 09:59:31 +00001537 &strm);
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001538 if (num_lines != 0)
1539 have_source = true;
1540 // TODO: Give here a one time warning if source file is missing.
Greg Claytone372b982011-11-21 21:44:34 +00001541 }
1542 }
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001543 switch (disasm_display)
1544 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001545 case Debugger::eStopDisassemblyTypeNever:
Greg Claytone372b982011-11-21 21:44:34 +00001546 break;
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001547
1548 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1549 if (have_debuginfo)
1550 break;
Jason Molenda62e06812016-02-16 04:14:33 +00001551 LLVM_FALLTHROUGH;
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001552
Greg Clayton67cc0632012-08-22 17:17:09 +00001553 case Debugger::eStopDisassemblyTypeNoSource:
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001554 if (have_source)
1555 break;
Jason Molenda62e06812016-02-16 04:14:33 +00001556 LLVM_FALLTHROUGH;
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00001557
Greg Clayton67cc0632012-08-22 17:17:09 +00001558 case Debugger::eStopDisassemblyTypeAlways:
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001559 if (target)
Greg Claytone372b982011-11-21 21:44:34 +00001560 {
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001561 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1562 if (disasm_lines > 0)
1563 {
1564 const ArchSpec &target_arch = target->GetArchitecture();
1565 AddressRange pc_range;
1566 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1567 pc_range.SetByteSize(disasm_lines * target_arch.GetMaximumOpcodeByteSize());
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001568 const char *plugin_name = nullptr;
1569 const char *flavor = nullptr;
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001570 Disassembler::Disassemble (target->GetDebugger(),
1571 target_arch,
Jim Ingham0f063ba2013-03-02 00:26:47 +00001572 plugin_name,
1573 flavor,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001574 exe_ctx,
1575 pc_range,
1576 disasm_lines,
1577 0,
1578 Disassembler::eOptionMarkPCAddress,
1579 strm);
1580 }
Greg Claytone372b982011-11-21 21:44:34 +00001581 }
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001582 break;
Greg Claytone372b982011-11-21 21:44:34 +00001583 }
Greg Clayton7260f622011-04-18 08:33:37 +00001584 }
1585 }
1586 return true;
1587}