blob: aaf0a4d7ac7db5181afd13f158b17628ec4606fb [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"
Sean Callanan4740a732016-09-06 04:48:36 +000023#include "lldb/Core/ValueObjectMemory.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000026#include "lldb/Symbol/Symbol.h"
27#include "lldb/Symbol/SymbolContextScope.h"
Enrico Granata46252392015-11-19 22:28:58 +000028#include "lldb/Symbol/Type.h"
Greg Clayton288bdf92010-09-02 02:59:18 +000029#include "lldb/Symbol/VariableList.h"
Sean Callanan4740a732016-09-06 04:48:36 +000030#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
34#include "lldb/Target/Target.h"
35#include "lldb/Target/Thread.h"
36
37using namespace lldb;
38using namespace lldb_private;
39
40// The first bits in the flags are reserved for the SymbolContext::Scope bits
41// so we know if we have tried to look up information in our internal symbol
42// context (m_sc) already.
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000043#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
Greg Clayton6dadd502010-09-02 21:44:10 +000044#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +000045#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
46#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
Sean Callanan7c0962d2010-11-01 04:38:59 +000047#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000049StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, user_id_t unwind_frame_index, addr_t cfa,
50 bool cfa_is_valid, addr_t pc, uint32_t stop_id, bool stop_id_is_valid, bool is_history_frame,
51 const SymbolContext *sc_ptr)
52 : m_thread_wp(thread_sp),
53 m_frame_index(frame_idx),
54 m_concrete_frame_index(unwind_frame_index),
55 m_reg_context_sp(),
56 m_id(pc, cfa, nullptr),
57 m_frame_code_addr(pc),
58 m_sc(),
59 m_flags(),
60 m_frame_base(),
61 m_frame_base_error(),
62 m_cfa_is_valid(cfa_is_valid),
63 m_stop_id(stop_id),
64 m_stop_id_is_valid(stop_id_is_valid),
65 m_is_history_frame(is_history_frame),
66 m_variable_list_sp(),
67 m_variable_list_value_objects(),
68 m_disassembly(),
69 m_mutex()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070{
Jason Molenda99618472013-11-04 11:02:52 +000071 // If we don't have a CFA value, use the frame index for our StackID so that recursive
72 // functions properly aren't confused with one another on a history stack.
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000073 if (m_is_history_frame && !m_cfa_is_valid)
Jason Molenda99618472013-11-04 11:02:52 +000074 {
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000075 m_id.SetCFA(m_frame_index);
Jason Molenda99618472013-11-04 11:02:52 +000076 }
77
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000078 if (sc_ptr != nullptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +000079 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 m_sc = *sc_ptr;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000081 m_flags.Set(m_sc.GetResolvedMask());
Greg Clayton1b72fcb2010-08-24 00:45:41 +000082 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083}
84
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000085StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, user_id_t unwind_frame_index,
86 const RegisterContextSP &reg_context_sp, addr_t cfa, addr_t pc, const SymbolContext *sc_ptr)
87 : m_thread_wp(thread_sp),
88 m_frame_index(frame_idx),
89 m_concrete_frame_index(unwind_frame_index),
90 m_reg_context_sp(reg_context_sp),
91 m_id(pc, cfa, nullptr),
92 m_frame_code_addr(pc),
93 m_sc(),
94 m_flags(),
95 m_frame_base(),
96 m_frame_base_error(),
97 m_cfa_is_valid(true),
98 m_stop_id(0),
99 m_stop_id_is_valid(false),
100 m_is_history_frame(false),
101 m_variable_list_sp(),
102 m_variable_list_value_objects(),
103 m_disassembly(),
104 m_mutex()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000106 if (sc_ptr != nullptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000107 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 m_sc = *sc_ptr;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000109 m_flags.Set(m_sc.GetResolvedMask());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000110 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000111
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000112 if (reg_context_sp && !m_sc.target_sp)
113 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000114 m_sc.target_sp = reg_context_sp->CalculateTarget();
115 if (m_sc.target_sp)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000116 m_flags.Set(eSymbolContextTarget);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000117 }
118}
119
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000120StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, user_id_t unwind_frame_index,
121 const RegisterContextSP &reg_context_sp, addr_t cfa, const Address &pc_addr,
122 const SymbolContext *sc_ptr)
123 : m_thread_wp(thread_sp),
124 m_frame_index(frame_idx),
125 m_concrete_frame_index(unwind_frame_index),
126 m_reg_context_sp(reg_context_sp),
127 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa, nullptr),
128 m_frame_code_addr(pc_addr),
129 m_sc(),
130 m_flags(),
131 m_frame_base(),
132 m_frame_base_error(),
133 m_cfa_is_valid(true),
134 m_stop_id(0),
135 m_stop_id_is_valid(false),
136 m_is_history_frame(false),
137 m_variable_list_sp(),
138 m_variable_list_value_objects(),
139 m_disassembly(),
140 m_mutex()
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000141{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000142 if (sc_ptr != nullptr)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000143 {
144 m_sc = *sc_ptr;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000145 m_flags.Set(m_sc.GetResolvedMask());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000146 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000147
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000148 if (!m_sc.target_sp && reg_context_sp)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000149 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000150 m_sc.target_sp = reg_context_sp->CalculateTarget();
151 if (m_sc.target_sp)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000152 m_flags.Set(eSymbolContextTarget);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000153 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000154
155 ModuleSP pc_module_sp(pc_addr.GetModule());
Greg Claytone72dfb32012-02-24 01:59:29 +0000156 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000157 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000158 if (pc_module_sp)
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000159 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000160 m_sc.module_sp = pc_module_sp;
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000161 m_flags.Set(eSymbolContextModule);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000162 }
Greg Claytonffc1d662010-09-13 04:34:30 +0000163 else
164 {
165 m_sc.module_sp.reset();
166 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000167 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000170StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171
172StackID&
173StackFrame::GetStackID()
174{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000175 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton6dadd502010-09-02 21:44:10 +0000176 // Make sure we have resolved the StackID object's symbol context scope if
177 // we already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000179 if (m_flags.IsClear (RESOLVED_FRAME_ID_SYMBOL_SCOPE))
180 {
Greg Clayton2cad65a2010-09-03 17:10:42 +0000181 if (m_id.GetSymbolContextScope ())
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000182 {
Greg Clayton95897c62010-09-07 04:20:48 +0000183 // We already have a symbol context scope, we just don't have our
184 // flag bit set.
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000185 m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
186 }
187 else
188 {
Greg Clayton95897c62010-09-07 04:20:48 +0000189 // Calculate the frame block and use this for the stack ID symbol
190 // context scope if we have one.
191 SymbolContextScope *scope = GetFrameBlock ();
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000192 if (scope == nullptr)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000193 {
Greg Clayton95897c62010-09-07 04:20:48 +0000194 // We don't have a block, so use the symbol
195 if (m_flags.IsClear (eSymbolContextSymbol))
196 GetSymbolContext (eSymbolContextSymbol);
197
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000198 // It is ok if m_sc.symbol is nullptr here
Greg Clayton95897c62010-09-07 04:20:48 +0000199 scope = m_sc.symbol;
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000200 }
Greg Clayton95897c62010-09-07 04:20:48 +0000201 // Set the symbol context scope (the accessor will set the
202 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
203 SetSymbolContextScope (scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000204 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 }
206 return m_id;
207}
208
Jim Ingham513c6bb2012-09-01 01:02:41 +0000209uint32_t
210StackFrame::GetFrameIndex () const
211{
212 ThreadSP thread_sp = GetThread();
213 if (thread_sp)
Jason Molendab57e4a12013-11-04 09:33:30 +0000214 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(m_frame_index);
Jim Ingham513c6bb2012-09-01 01:02:41 +0000215 else
216 return m_frame_index;
217}
218
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000219void
220StackFrame::SetSymbolContextScope (SymbolContextScope *symbol_scope)
221{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000222 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000223 m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
224 m_id.SetSymbolContextScope (symbol_scope);
225}
226
Greg Clayton34132752011-07-06 04:07:21 +0000227const Address&
Greg Clayton9da7bd02010-08-24 21:05:24 +0000228StackFrame::GetFrameCodeAddress()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000230 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000231 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && !m_frame_code_addr.IsSectionOffset())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 {
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000233 m_flags.Set (RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234
235 // Resolve the PC into a temporary address because if ResolveLoadAddress
236 // fails to resolve the address, it will clear the address object...
Greg Claytond9e416c2012-02-18 05:35:26 +0000237 ThreadSP thread_sp (GetThread());
238 if (thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000240 TargetSP target_sp (thread_sp->CalculateTarget());
241 if (target_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 {
Tamas Berghammer25b9f7e2015-09-07 09:58:09 +0000243 if (m_frame_code_addr.SetOpcodeLoadAddress (m_frame_code_addr.GetOffset(), target_sp.get(), eAddressClassCode))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000245 ModuleSP module_sp (m_frame_code_addr.GetModule());
246 if (module_sp)
Greg Claytond9e416c2012-02-18 05:35:26 +0000247 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000248 m_sc.module_sp = module_sp;
249 m_flags.Set(eSymbolContextModule);
Greg Claytond9e416c2012-02-18 05:35:26 +0000250 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 }
252 }
253 }
254 }
Greg Clayton12fc3e02010-08-26 22:05:43 +0000255 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}
257
Jason Molenda99618472013-11-04 11:02:52 +0000258bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259StackFrame::ChangePC (addr_t pc)
260{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000261 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda99618472013-11-04 11:02:52 +0000262 // We can't change the pc value of a history stack frame - it is immutable.
263 if (m_is_history_frame)
264 return false;
Greg Claytone72dfb32012-02-24 01:59:29 +0000265 m_frame_code_addr.SetRawAddress(pc);
Greg Clayton72310352013-02-23 04:12:47 +0000266 m_sc.Clear(false);
Greg Clayton73b472d2010-10-27 03:32:59 +0000267 m_flags.Reset(0);
Greg Claytond9e416c2012-02-18 05:35:26 +0000268 ThreadSP thread_sp (GetThread());
269 if (thread_sp)
270 thread_sp->ClearStackFrames ();
Jason Molenda99618472013-11-04 11:02:52 +0000271 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272}
273
274const char *
275StackFrame::Disassemble ()
276{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000277 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 if (m_disassembly.GetSize() == 0)
279 {
Greg Claytond9e416c2012-02-18 05:35:26 +0000280 ExecutionContext exe_ctx (shared_from_this());
281 Target *target = exe_ctx.GetTargetPtr();
282 if (target)
283 {
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000284 const char *plugin_name = nullptr;
285 const char *flavor = nullptr;
Greg Claytond9e416c2012-02-18 05:35:26 +0000286 Disassembler::Disassemble (target->GetDebugger(),
287 target->GetArchitecture(),
Jim Ingham0f063ba2013-03-02 00:26:47 +0000288 plugin_name,
289 flavor,
Greg Claytond9e416c2012-02-18 05:35:26 +0000290 exe_ctx,
291 0,
292 0,
293 0,
294 m_disassembly);
295 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 if (m_disassembly.GetSize() == 0)
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000297 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 }
299 return m_disassembly.GetData();
300}
301
Greg Clayton95897c62010-09-07 04:20:48 +0000302Block *
303StackFrame::GetFrameBlock ()
304{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000305 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
Greg Clayton95897c62010-09-07 04:20:48 +0000306 GetSymbolContext (eSymbolContextBlock);
307
308 if (m_sc.block)
309 {
310 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
311 if (inline_block)
312 {
313 // Use the block with the inlined function info
314 // as the frame block we want this frame to have only the variables
315 // for the inlined function and its non-inlined block child blocks.
316 return inline_block;
317 }
318 else
319 {
Ed Maste75500e72016-07-19 15:28:02 +0000320 // This block is not contained within any inlined function blocks
Greg Clayton95897c62010-09-07 04:20:48 +0000321 // with so we want to use the top most function block.
322 return &m_sc.function->GetBlock (false);
323 }
324 }
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000325 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000326}
327
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328//----------------------------------------------------------------------
329// Get the symbol context if we already haven't done so by resolving the
330// PC address as much as possible. This way when we pass around a
331// StackFrame object, everyone will have as much information as
332// possible and no one will ever have to look things up manually.
333//----------------------------------------------------------------------
334const SymbolContext&
335StackFrame::GetSymbolContext (uint32_t resolve_scope)
336{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000337 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 // Copy our internal symbol context into "sc".
Greg Clayton73b472d2010-10-27 03:32:59 +0000339 if ((m_flags.Get() & resolve_scope) != resolve_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 {
Greg Clayton75a03332012-11-29 00:53:06 +0000341 uint32_t resolved = 0;
342
343 // If the target was requested add that:
344 if (!m_sc.target_sp)
345 {
346 m_sc.target_sp = CalculateTarget();
347 if (m_sc.target_sp)
348 resolved |= eSymbolContextTarget;
349 }
350
Bruce Mitcheneraaa0ba32014-07-08 18:05:41 +0000351 // Resolve our PC to section offset if we haven't already done so
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 // and if we don't have a module. The resolved address section will
353 // contain the module to which it belongs
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000354 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
Greg Clayton9da7bd02010-08-24 21:05:24 +0000355 GetFrameCodeAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356
357 // If this is not frame zero, then we need to subtract 1 from the PC
358 // value when doing address lookups since the PC will be on the
359 // instruction following the function call instruction...
360
Greg Clayton9da7bd02010-08-24 21:05:24 +0000361 Address lookup_addr(GetFrameCodeAddress());
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000362 if (m_frame_index > 0 && lookup_addr.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 {
364 addr_t offset = lookup_addr.GetOffset();
365 if (offset > 0)
Jason Molendacf296752014-11-08 05:38:17 +0000366 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 lookup_addr.SetOffset(offset - 1);
Jason Molendacf296752014-11-08 05:38:17 +0000368
369 }
370 else
371 {
372 // lookup_addr is the start of a section. We need
373 // do the math on the actual load address and re-compute
374 // the section. We're working with a 'noreturn' function
375 // at the end of a section.
376 ThreadSP thread_sp (GetThread());
377 if (thread_sp)
378 {
379 TargetSP target_sp (thread_sp->CalculateTarget());
380 if (target_sp)
381 {
382 addr_t addr_minus_one = lookup_addr.GetLoadAddress(target_sp.get()) - 1;
383 lookup_addr.SetLoadAddress (addr_minus_one, target_sp.get());
384 }
385 else
386 {
387 lookup_addr.SetOffset(offset - 1);
388 }
389 }
390 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 }
392
393 if (m_sc.module_sp)
394 {
395 // We have something in our stack frame symbol context, lets check
396 // if we haven't already tried to lookup one of those things. If we
397 // haven't then we will do the query.
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000398
399 uint32_t actual_resolve_scope = 0;
400
401 if (resolve_scope & eSymbolContextCompUnit)
402 {
403 if (m_flags.IsClear (eSymbolContextCompUnit))
404 {
405 if (m_sc.comp_unit)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000406 resolved |= eSymbolContextCompUnit;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000407 else
408 actual_resolve_scope |= eSymbolContextCompUnit;
409 }
410 }
411
412 if (resolve_scope & eSymbolContextFunction)
413 {
414 if (m_flags.IsClear (eSymbolContextFunction))
415 {
416 if (m_sc.function)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000417 resolved |= eSymbolContextFunction;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000418 else
419 actual_resolve_scope |= eSymbolContextFunction;
420 }
421 }
422
423 if (resolve_scope & eSymbolContextBlock)
424 {
425 if (m_flags.IsClear (eSymbolContextBlock))
426 {
427 if (m_sc.block)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000428 resolved |= eSymbolContextBlock;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000429 else
430 actual_resolve_scope |= eSymbolContextBlock;
431 }
432 }
433
434 if (resolve_scope & eSymbolContextSymbol)
435 {
436 if (m_flags.IsClear (eSymbolContextSymbol))
437 {
438 if (m_sc.symbol)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000439 resolved |= eSymbolContextSymbol;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000440 else
441 actual_resolve_scope |= eSymbolContextSymbol;
442 }
443 }
444
445 if (resolve_scope & eSymbolContextLineEntry)
446 {
447 if (m_flags.IsClear (eSymbolContextLineEntry))
448 {
449 if (m_sc.line_entry.IsValid())
Greg Clayton9da7bd02010-08-24 21:05:24 +0000450 resolved |= eSymbolContextLineEntry;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000451 else
452 actual_resolve_scope |= eSymbolContextLineEntry;
453 }
454 }
455
456 if (actual_resolve_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 {
458 // We might be resolving less information than what is already
459 // in our current symbol context so resolve into a temporary
460 // symbol context "sc" so we don't clear out data we have
461 // already found in "m_sc"
462 SymbolContext sc;
463 // Set flags that indicate what we have tried to resolve
Greg Clayton9da7bd02010-08-24 21:05:24 +0000464 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000465 // Only replace what we didn't already have as we may have
466 // information for an inlined function scope that won't match
467 // what a standard lookup by address would match
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000468 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000469 m_sc.comp_unit = sc.comp_unit;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000470 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000471 m_sc.function = sc.function;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000472 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000473 m_sc.block = sc.block;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000474 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
Greg Clayton9da7bd02010-08-24 21:05:24 +0000475 m_sc.symbol = sc.symbol;
Greg Clayton75a03332012-11-29 00:53:06 +0000476 if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid())
477 {
Greg Clayton9da7bd02010-08-24 21:05:24 +0000478 m_sc.line_entry = sc.line_entry;
Ted Woodward911d5782016-05-11 22:46:53 +0000479 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
Greg Clayton75a03332012-11-29 00:53:06 +0000480 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 }
482 }
483 else
484 {
485 // If we don't have a module, then we can't have the compile unit,
486 // function, block, line entry or symbol, so we can safely call
487 // ResolveSymbolContextForAddress with our symbol context member m_sc.
Greg Clayton9da7bd02010-08-24 21:05:24 +0000488 if (m_sc.target_sp)
Sean Callananf4be2272013-02-21 20:54:33 +0000489 {
Greg Clayton75a03332012-11-29 00:53:06 +0000490 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
Sean Callananf4be2272013-02-21 20:54:33 +0000491 }
Greg Clayton9da7bd02010-08-24 21:05:24 +0000492 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
494 // Update our internal flags so we remember what we have tried to locate so
495 // we don't have to keep trying when more calls to this function are made.
Greg Clayton9da7bd02010-08-24 21:05:24 +0000496 // We might have dug up more information that was requested (for example
497 // if we were asked to only get the block, we will have gotten the
498 // compile unit, and function) so set any additional bits that we resolved
499 m_flags.Set (resolve_scope | resolved);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500 }
501
502 // Return the symbol context with everything that was possible to resolve
503 // resolved.
504 return m_sc;
505}
506
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507VariableList *
Greg Clayton288bdf92010-09-02 02:59:18 +0000508StackFrame::GetVariableList (bool get_file_globals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000510 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 if (m_flags.IsClear(RESOLVED_VARIABLES))
512 {
513 m_flags.Set(RESOLVED_VARIABLES);
514
Greg Clayton95897c62010-09-07 04:20:48 +0000515 Block *frame_block = GetFrameBlock();
516
517 if (frame_block)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518 {
Greg Clayton95897c62010-09-07 04:20:48 +0000519 const bool get_child_variables = true;
520 const bool can_create = true;
Greg Claytonc662ec82011-06-17 22:10:16 +0000521 const bool stop_if_child_block_is_inlined_function = true;
522 m_variable_list_sp.reset(new VariableList());
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000523 frame_block->AppendBlockVariables(can_create,
524 get_child_variables,
525 stop_if_child_block_is_inlined_function,
Greg Claytona32532b2016-04-25 21:54:10 +0000526 [this](Variable* v) { return true; },
Tamas Berghammer72ac8a82016-02-25 12:23:37 +0000527 m_variable_list_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528 }
Sean Callanan7c0962d2010-11-01 04:38:59 +0000529 }
530
531 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
532 get_file_globals)
533 {
534 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
Greg Clayton288bdf92010-09-02 02:59:18 +0000535
Sean Callanan7c0962d2010-11-01 04:38:59 +0000536 if (m_flags.IsClear (eSymbolContextCompUnit))
537 GetSymbolContext (eSymbolContextCompUnit);
538
539 if (m_sc.comp_unit)
Greg Clayton288bdf92010-09-02 02:59:18 +0000540 {
Sean Callanan7c0962d2010-11-01 04:38:59 +0000541 VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
542 if (m_variable_list_sp)
543 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
544 else
545 m_variable_list_sp = global_variable_list_sp;
Greg Clayton288bdf92010-09-02 02:59:18 +0000546 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 }
Sean Callanan7c0962d2010-11-01 04:38:59 +0000548
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 return m_variable_list_sp.get();
550}
551
Greg Claytond41f0322011-08-02 23:35:43 +0000552VariableListSP
Jim Inghamcef46172016-04-26 00:29:59 +0000553StackFrame::GetInScopeVariableList (bool get_file_globals, bool must_have_valid_location)
Greg Claytond41f0322011-08-02 23:35:43 +0000554{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000555 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda99618472013-11-04 11:02:52 +0000556 // We can't fetch variable information for a history stack frame.
557 if (m_is_history_frame)
558 return VariableListSP();
559
Greg Claytond41f0322011-08-02 23:35:43 +0000560 VariableListSP var_list_sp(new VariableList);
561 GetSymbolContext (eSymbolContextCompUnit | eSymbolContextBlock);
562
563 if (m_sc.block)
564 {
565 const bool can_create = true;
566 const bool get_parent_variables = true;
567 const bool stop_if_block_is_inlined_function = true;
568 m_sc.block->AppendVariables (can_create,
569 get_parent_variables,
570 stop_if_block_is_inlined_function,
Jim Inghamcef46172016-04-26 00:29:59 +0000571 [this, must_have_valid_location](Variable* v)
572 {
573 return v->IsInScope(this) && (!must_have_valid_location || v->LocationIsValidForFrame(this));
574 },
Greg Claytond41f0322011-08-02 23:35:43 +0000575 var_list_sp.get());
576 }
577
Siva Chandrab90168f2016-02-02 23:49:41 +0000578 if (m_sc.comp_unit && get_file_globals)
Greg Claytond41f0322011-08-02 23:35:43 +0000579 {
580 VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
581 if (global_variable_list_sp)
582 var_list_sp->AddVariables (global_variable_list_sp.get());
583 }
584
585 return var_list_sp;
586}
587
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000588ValueObjectSP
Greg Clayton685c88c2012-07-14 00:53:55 +0000589StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr,
Greg Clayton4d122c42011-09-17 08:33:22 +0000590 DynamicValueType use_dynamic,
Jim Ingham2837b762011-05-04 03:43:18 +0000591 uint32_t options,
Greg Clayton4d122c42011-09-17 08:33:22 +0000592 VariableSP &var_sp,
Jim Ingham2837b762011-05-04 03:43:18 +0000593 Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000594{
Jason Molenda99618472013-11-04 11:02:52 +0000595 // We can't fetch variable information for a history stack frame.
596 if (m_is_history_frame)
597 return ValueObjectSP();
Greg Clayton54979cd2010-12-15 05:08:08 +0000598
599 if (var_expr_cstr && var_expr_cstr[0])
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000600 {
Greg Clayton6d5e68e2011-01-20 19:27:18 +0000601 const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
602 const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
Enrico Granata27b625e2011-08-09 01:04:56 +0000603 const bool no_synth_child = (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
Enrico Granata58ad3342011-08-19 21:56:10 +0000604 //const bool no_synth_array = (options & eExpressionPathOptionsNoSyntheticArrayRange) != 0;
Greg Clayton54979cd2010-12-15 05:08:08 +0000605 error.Clear();
606 bool deref = false;
607 bool address_of = false;
608 ValueObjectSP valobj_sp;
609 const bool get_file_globals = true;
Greg Claytond41f0322011-08-02 23:35:43 +0000610 // When looking up a variable for an expression, we need only consider the
611 // variables that are in scope.
612 VariableListSP var_list_sp (GetInScopeVariableList (get_file_globals));
613 VariableList *variable_list = var_list_sp.get();
Greg Clayton54979cd2010-12-15 05:08:08 +0000614
615 if (variable_list)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000616 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000617 // If first character is a '*', then show pointer contents
618 const char *var_expr = var_expr_cstr;
619 if (var_expr[0] == '*')
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000620 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000621 deref = true;
622 var_expr++; // Skip the '*'
623 }
624 else if (var_expr[0] == '&')
625 {
626 address_of = true;
627 var_expr++; // Skip the '&'
628 }
629
630 std::string var_path (var_expr);
631 size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
632 StreamString var_expr_path_strm;
633
634 ConstString name_const_string;
635 if (separator_idx == std::string::npos)
636 name_const_string.SetCString (var_path.c_str());
637 else
638 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
639
Paul Herman10bc1a42015-08-18 22:46:57 +0000640 var_sp = variable_list->FindVariable(name_const_string, false);
Greg Clayton685c88c2012-07-14 00:53:55 +0000641
642 bool synthetically_added_instance_object = false;
643
644 if (var_sp)
645 {
646 var_path.erase (0, name_const_string.GetLength ());
647 }
Enrico Granata46252392015-11-19 22:28:58 +0000648
649 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess))
Greg Clayton685c88c2012-07-14 00:53:55 +0000650 {
651 // Check for direct ivars access which helps us with implicit
652 // access to ivars with the "this->" or "self->"
653 GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock);
654 lldb::LanguageType method_language = eLanguageTypeUnknown;
655 bool is_instance_method = false;
656 ConstString method_object_name;
657 if (m_sc.GetFunctionMethodInfo (method_language, is_instance_method, method_object_name))
658 {
659 if (is_instance_method && method_object_name)
660 {
661 var_sp = variable_list->FindVariable(method_object_name);
662 if (var_sp)
663 {
664 separator_idx = 0;
665 var_path.insert(0, "->");
666 synthetically_added_instance_object = true;
667 }
668 }
669 }
670 }
Enrico Granata46252392015-11-19 22:28:58 +0000671
672 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions))
673 {
674 // Check if any anonymous unions are there which contain a variable with the name we need
675 for (size_t i = 0;
676 i < variable_list->GetSize();
677 i++)
678 {
679 if (VariableSP variable_sp = variable_list->GetVariableAtIndex(i))
680 {
681 if (variable_sp->GetName().IsEmpty())
682 {
683 if (Type *var_type = variable_sp->GetType())
684 {
685 if (var_type->GetForwardCompilerType().IsAnonymousType())
686 {
687 valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
688 if (!valobj_sp)
689 return valobj_sp;
690 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
691 if (valobj_sp)
692 break;
693 }
694 }
695 }
696 }
697 }
698 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000699
Enrico Granata46252392015-11-19 22:28:58 +0000700 if (var_sp && !valobj_sp)
Greg Clayton54979cd2010-12-15 05:08:08 +0000701 {
Jim Ingham2837b762011-05-04 03:43:18 +0000702 valobj_sp = GetValueObjectForFrameVariable (var_sp, use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +0000703 if (!valobj_sp)
704 return valobj_sp;
Enrico Granata46252392015-11-19 22:28:58 +0000705 }
706 if (valobj_sp)
707 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000708 // We are dumping at least one child
709 while (separator_idx != std::string::npos)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000710 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000711 // Calculate the next separator index ahead of time
712 ValueObjectSP child_valobj_sp;
713 const char separator_type = var_path[0];
714 switch (separator_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000715 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000716 case '-':
717 if (var_path.size() >= 2 && var_path[1] != '>')
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000718 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000719
Greg Clayton6d5e68e2011-01-20 19:27:18 +0000720 if (no_fragile_ivar)
721 {
722 // Make sure we aren't trying to deref an objective
723 // C ivar if this is not allowed
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000724 const uint32_t pointer_type_flags = valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
Enrico Granata622be232014-10-21 20:52:14 +0000725 if ((pointer_type_flags & eTypeIsObjC) &&
726 (pointer_type_flags & eTypeIsPointer))
Greg Clayton6d5e68e2011-01-20 19:27:18 +0000727 {
728 // This was an objective C object pointer and
729 // it was requested we skip any fragile ivars
730 // so return nothing here
731 return ValueObjectSP();
732 }
733 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000734 var_path.erase (0, 1); // Remove the '-'
Jason Molenda62e06812016-02-16 04:14:33 +0000735 LLVM_FALLTHROUGH;
Greg Clayton54979cd2010-12-15 05:08:08 +0000736 case '.':
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000737 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000738 const bool expr_is_ptr = var_path[0] == '>';
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000739
Greg Clayton54979cd2010-12-15 05:08:08 +0000740 var_path.erase (0, 1); // Remove the '.' or '>'
741 separator_idx = var_path.find_first_of(".-[");
742 ConstString child_name;
743 if (separator_idx == std::string::npos)
744 child_name.SetCString (var_path.c_str());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000745 else
Greg Clayton54979cd2010-12-15 05:08:08 +0000746 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
747
748 if (check_ptr_vs_member)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000749 {
Greg Clayton54979cd2010-12-15 05:08:08 +0000750 // We either have a pointer type and need to verify
751 // valobj_sp is a pointer, or we have a member of a
752 // class/union/struct being accessed with the . syntax
753 // and need to verify we don't have a pointer.
754 const bool actual_is_ptr = valobj_sp->IsPointerType ();
755
756 if (actual_is_ptr != expr_is_ptr)
757 {
758 // Incorrect use of "." with a pointer, or "->" with
759 // a class/union/struct instance or reference.
Greg Clayton6beaaa62011-01-17 03:46:26 +0000760 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Clayton54979cd2010-12-15 05:08:08 +0000761 if (actual_is_ptr)
762 error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
763 var_expr_path_strm.GetString().c_str(),
764 child_name.GetCString(),
765 var_expr_path_strm.GetString().c_str(),
766 var_path.c_str());
767 else
768 error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
769 var_expr_path_strm.GetString().c_str(),
770 child_name.GetCString(),
771 var_expr_path_strm.GetString().c_str(),
772 var_path.c_str());
773 return ValueObjectSP();
774 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000775 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000776 child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000777 if (!child_valobj_sp)
778 {
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000779 if (!no_synth_child)
Enrico Granata86cc9822012-03-19 22:58:49 +0000780 {
781 child_valobj_sp = valobj_sp->GetSyntheticValue();
782 if (child_valobj_sp)
783 child_valobj_sp = child_valobj_sp->GetChildMemberWithName (child_name, true);
784 }
Enrico Granata8c9d3562011-08-11 17:08:01 +0000785
786 if (no_synth_child || !child_valobj_sp)
Greg Clayton54979cd2010-12-15 05:08:08 +0000787 {
Enrico Granata8c9d3562011-08-11 17:08:01 +0000788 // No child member with name "child_name"
Greg Clayton685c88c2012-07-14 00:53:55 +0000789 if (synthetically_added_instance_object)
Enrico Granata8c9d3562011-08-11 17:08:01 +0000790 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000791 // We added a "this->" or "self->" to the beginning of the expression
792 // and this is the first pointer ivar access, so just return the normal
793 // error
794 error.SetErrorStringWithFormat("no variable or instance variable named '%s' found in this frame",
795 name_const_string.GetCString());
Enrico Granata8c9d3562011-08-11 17:08:01 +0000796 }
797 else
798 {
Greg Clayton685c88c2012-07-14 00:53:55 +0000799 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
800 if (child_name)
801 {
802 error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
803 child_name.GetCString(),
804 valobj_sp->GetTypeName().AsCString("<invalid type>"),
805 var_expr_path_strm.GetString().c_str());
806 }
807 else
808 {
809 error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
810 var_expr_path_strm.GetString().c_str(),
811 var_expr_cstr);
812 }
Enrico Granata8c9d3562011-08-11 17:08:01 +0000813 }
814 return ValueObjectSP();
Greg Clayton54979cd2010-12-15 05:08:08 +0000815 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000816 }
Greg Clayton685c88c2012-07-14 00:53:55 +0000817 synthetically_added_instance_object = false;
Greg Clayton54979cd2010-12-15 05:08:08 +0000818 // Remove the child name from the path
819 var_path.erase(0, child_name.GetLength());
Greg Clayton4d122c42011-09-17 08:33:22 +0000820 if (use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +0000821 {
Jim Ingham2837b762011-05-04 03:43:18 +0000822 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
Jim Ingham78a685a2011-04-16 00:01:13 +0000823 if (dynamic_value_sp)
824 child_valobj_sp = dynamic_value_sp;
825 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000826 }
827 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000828
Greg Clayton54979cd2010-12-15 05:08:08 +0000829 case '[':
830 // Array member access, or treating pointer as an array
831 if (var_path.size() > 2) // Need at least two brackets and a number
832 {
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000833 char *end = nullptr;
Greg Clayton1a65ae12011-01-25 23:55:37 +0000834 long child_index = ::strtol (&var_path[1], &end, 0);
Enrico Granata9fc19442011-07-06 02:13:41 +0000835 if (end && *end == ']'
836 && *(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 +0000837 {
Greg Clayton99558cc42015-08-24 23:46:31 +0000838 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +0000839 {
840 // what we have is *ptr[low]. the most similar C++ syntax is to deref ptr
841 // and extract bit low out of it. reading array item low
842 // would be done by saying ptr[low], without a deref * sign
843 Error error;
844 ValueObjectSP temp(valobj_sp->Dereference(error));
845 if (error.Fail())
846 {
847 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
848 error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
849 valobj_sp->GetTypeName().AsCString("<invalid type>"),
850 var_expr_path_strm.GetString().c_str());
851 return ValueObjectSP();
852 }
853 valobj_sp = temp;
854 deref = false;
855 }
Greg Clayton99558cc42015-08-24 23:46:31 +0000856 else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +0000857 {
858 // what we have is *arr[low]. the most similar C++ syntax is to get arr[0]
859 // (an operation that is equivalent to deref-ing arr)
860 // and extract bit low out of it. reading array item low
861 // would be done by saying arr[low], without a deref * sign
862 Error error;
863 ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
864 if (error.Fail())
865 {
866 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
867 error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
868 valobj_sp->GetTypeName().AsCString("<invalid type>"),
869 var_expr_path_strm.GetString().c_str());
870 return ValueObjectSP();
871 }
872 valobj_sp = temp;
873 deref = false;
874 }
875
Greg Clayton4ef877f2012-12-06 02:33:54 +0000876 bool is_incomplete_array = false;
Greg Clayton54979cd2010-12-15 05:08:08 +0000877 if (valobj_sp->IsPointerType ())
878 {
Sean Callanan226b70c2012-03-08 02:39:03 +0000879 bool is_objc_pointer = true;
880
Greg Clayton99558cc42015-08-24 23:46:31 +0000881 if (valobj_sp->GetCompilerType().GetMinimumLanguage() != eLanguageTypeObjC)
Sean Callanan226b70c2012-03-08 02:39:03 +0000882 is_objc_pointer = false;
Greg Clayton99558cc42015-08-24 23:46:31 +0000883 else if (!valobj_sp->GetCompilerType().IsPointerType())
Sean Callanan226b70c2012-03-08 02:39:03 +0000884 is_objc_pointer = false;
885
886 if (no_synth_child && is_objc_pointer)
Greg Clayton54979cd2010-12-15 05:08:08 +0000887 {
Sean Callanan226b70c2012-03-08 02:39:03 +0000888 error.SetErrorStringWithFormat("\"(%s) %s\" is an Objective-C pointer, and cannot be subscripted",
889 valobj_sp->GetTypeName().AsCString("<invalid type>"),
890 var_expr_path_strm.GetString().c_str());
891
892 return ValueObjectSP();
893 }
894 else if (is_objc_pointer)
895 {
Enrico Granata27b625e2011-08-09 01:04:56 +0000896 // dereferencing ObjC variables is not valid.. so let's try and recur to synthetic children
Enrico Granata86cc9822012-03-19 22:58:49 +0000897 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000898 if (!synthetic /* no synthetic */
Enrico Granata27b625e2011-08-09 01:04:56 +0000899 || synthetic == valobj_sp) /* synthetic is the same as the original object */
900 {
901 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
902 error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
903 valobj_sp->GetTypeName().AsCString("<invalid type>"),
904 var_expr_path_strm.GetString().c_str());
905 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000906 else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
Enrico Granata27b625e2011-08-09 01:04:56 +0000907 {
908 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000909 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000910 child_index,
911 valobj_sp->GetTypeName().AsCString("<invalid type>"),
912 var_expr_path_strm.GetString().c_str());
913 }
914 else
915 {
916 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
917 if (!child_valobj_sp)
918 {
919 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000920 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000921 child_index,
922 valobj_sp->GetTypeName().AsCString("<invalid type>"),
923 var_expr_path_strm.GetString().c_str());
924 }
925 }
926 }
927 else
928 {
Bruce Mitchener11d86362015-02-26 23:55:39 +0000929 child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +0000930 if (!child_valobj_sp)
931 {
932 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000933 error.SetErrorStringWithFormat ("failed to use pointer as array for index %ld for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000934 child_index,
935 valobj_sp->GetTypeName().AsCString("<invalid type>"),
936 var_expr_path_strm.GetString().c_str());
937 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000938 }
939 }
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000940 else if (valobj_sp->GetCompilerType().IsArrayType(nullptr, nullptr, &is_incomplete_array))
Greg Clayton54979cd2010-12-15 05:08:08 +0000941 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000942 // Pass false to dynamic_value here so we can tell the difference between
943 // no dynamic value and no member of this type...
Greg Clayton54979cd2010-12-15 05:08:08 +0000944 child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000945 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
Greg Clayton4ef877f2012-12-06 02:33:54 +0000946 child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
947
Greg Clayton54979cd2010-12-15 05:08:08 +0000948 if (!child_valobj_sp)
949 {
Greg Clayton6beaaa62011-01-17 03:46:26 +0000950 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000951 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Greg Clayton54979cd2010-12-15 05:08:08 +0000952 child_index,
953 valobj_sp->GetTypeName().AsCString("<invalid type>"),
954 var_expr_path_strm.GetString().c_str());
955 }
956 }
Greg Clayton99558cc42015-08-24 23:46:31 +0000957 else if (valobj_sp->GetCompilerType().IsScalarType())
Enrico Granata9fc19442011-07-06 02:13:41 +0000958 {
959 // this is a bitfield asking to display just one bit
960 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, child_index, true);
961 if (!child_valobj_sp)
962 {
963 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000964 error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
Enrico Granata9fc19442011-07-06 02:13:41 +0000965 child_index, child_index,
966 valobj_sp->GetTypeName().AsCString("<invalid type>"),
967 var_expr_path_strm.GetString().c_str());
968 }
969 }
Greg Clayton54979cd2010-12-15 05:08:08 +0000970 else
971 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000972 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
Enrico Granata27b625e2011-08-09 01:04:56 +0000973 if (no_synth_child /* synthetic is forbidden */ ||
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000974 !synthetic /* no synthetic */
Enrico Granata27b625e2011-08-09 01:04:56 +0000975 || synthetic == valobj_sp) /* synthetic is the same as the original object */
976 {
977 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
978 error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
979 valobj_sp->GetTypeName().AsCString("<invalid type>"),
980 var_expr_path_strm.GetString().c_str());
981 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +0000982 else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
Enrico Granata27b625e2011-08-09 01:04:56 +0000983 {
984 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000985 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000986 child_index,
987 valobj_sp->GetTypeName().AsCString("<invalid type>"),
988 var_expr_path_strm.GetString().c_str());
989 }
990 else
991 {
992 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
993 if (!child_valobj_sp)
994 {
995 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +0000996 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
Enrico Granata27b625e2011-08-09 01:04:56 +0000997 child_index,
998 valobj_sp->GetTypeName().AsCString("<invalid type>"),
999 var_expr_path_strm.GetString().c_str());
1000 }
1001 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001002 }
1003
1004 if (!child_valobj_sp)
1005 {
1006 // Invalid array index...
1007 return ValueObjectSP();
1008 }
1009
1010 // Erase the array member specification '[%i]' where
1011 // %i is the array index
1012 var_path.erase(0, (end - var_path.c_str()) + 1);
1013 separator_idx = var_path.find_first_of(".-[");
Greg Clayton4d122c42011-09-17 08:33:22 +00001014 if (use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001015 {
Jim Ingham2837b762011-05-04 03:43:18 +00001016 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
Jim Ingham78a685a2011-04-16 00:01:13 +00001017 if (dynamic_value_sp)
1018 child_valobj_sp = dynamic_value_sp;
1019 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001020 // Break out early from the switch since we were
1021 // able to find the child member
1022 break;
1023 }
Enrico Granata20edcdb2011-07-19 18:03:25 +00001024 else if (end && *end == '-')
Enrico Granata9fc19442011-07-06 02:13:41 +00001025 {
1026 // this is most probably a BitField, let's take a look
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001027 char *real_end = nullptr;
Enrico Granata9fc19442011-07-06 02:13:41 +00001028 long final_index = ::strtol (end+1, &real_end, 0);
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001029 bool expand_bitfield = true;
Enrico Granata20edcdb2011-07-19 18:03:25 +00001030 if (real_end && *real_end == ']')
Enrico Granata9fc19442011-07-06 02:13:41 +00001031 {
1032 // if the format given is [high-low], swap range
Enrico Granata20edcdb2011-07-19 18:03:25 +00001033 if (child_index > final_index)
Enrico Granata9fc19442011-07-06 02:13:41 +00001034 {
1035 long temp = child_index;
1036 child_index = final_index;
1037 final_index = temp;
1038 }
1039
Greg Clayton99558cc42015-08-24 23:46:31 +00001040 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +00001041 {
1042 // what we have is *ptr[low-high]. the most similar C++ syntax is to deref ptr
1043 // and extract bits low thru high out of it. reading array items low thru high
1044 // would be done by saying ptr[low-high], without a deref * sign
1045 Error error;
1046 ValueObjectSP temp(valobj_sp->Dereference(error));
1047 if (error.Fail())
1048 {
1049 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1050 error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
1051 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1052 var_expr_path_strm.GetString().c_str());
1053 return ValueObjectSP();
1054 }
1055 valobj_sp = temp;
1056 deref = false;
1057 }
Greg Clayton99558cc42015-08-24 23:46:31 +00001058 else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
Enrico Granata9fc19442011-07-06 02:13:41 +00001059 {
1060 // what we have is *arr[low-high]. the most similar C++ syntax is to get arr[0]
1061 // (an operation that is equivalent to deref-ing arr)
1062 // and extract bits low thru high out of it. reading array items low thru high
1063 // would be done by saying arr[low-high], without a deref * sign
1064 Error error;
1065 ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
1066 if (error.Fail())
1067 {
1068 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1069 error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
1070 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1071 var_expr_path_strm.GetString().c_str());
1072 return ValueObjectSP();
1073 }
1074 valobj_sp = temp;
1075 deref = false;
1076 }
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001077 /*else if (valobj_sp->IsArrayType() || valobj_sp->IsPointerType())
Enrico Granata9fc19442011-07-06 02:13:41 +00001078 {
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001079 child_valobj_sp = valobj_sp->GetSyntheticArrayRangeChild(child_index, final_index, true);
1080 expand_bitfield = false;
1081 if (!child_valobj_sp)
1082 {
1083 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1084 error.SetErrorStringWithFormat ("array range %i-%i is not valid for \"(%s) %s\"",
1085 child_index, final_index,
1086 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1087 var_expr_path_strm.GetString().c_str());
1088 }
1089 }*/
1090
1091 if (expand_bitfield)
1092 {
1093 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1094 if (!child_valobj_sp)
1095 {
1096 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Jason Molenda7e589a62011-09-20 00:26:08 +00001097 error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001098 child_index, final_index,
1099 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1100 var_expr_path_strm.GetString().c_str());
1101 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001102 }
1103 }
1104
1105 if (!child_valobj_sp)
1106 {
1107 // Invalid bitfield range...
1108 return ValueObjectSP();
1109 }
1110
1111 // Erase the bitfield member specification '[%i-%i]' where
1112 // %i is the index
1113 var_path.erase(0, (real_end - var_path.c_str()) + 1);
1114 separator_idx = var_path.find_first_of(".-[");
Greg Clayton4d122c42011-09-17 08:33:22 +00001115 if (use_dynamic != eNoDynamicValues)
Enrico Granata9fc19442011-07-06 02:13:41 +00001116 {
1117 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
1118 if (dynamic_value_sp)
1119 child_valobj_sp = dynamic_value_sp;
1120 }
1121 // Break out early from the switch since we were
1122 // able to find the child member
1123 break;
1124
1125 }
1126 }
1127 else
1128 {
1129 error.SetErrorStringWithFormat("invalid square bracket encountered after \"%s\" in \"%s\"",
1130 var_expr_path_strm.GetString().c_str(),
1131 var_path.c_str());
Greg Clayton54979cd2010-12-15 05:08:08 +00001132 }
1133 return ValueObjectSP();
1134
1135 default:
1136 // Failure...
1137 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001138 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
Greg Clayton54979cd2010-12-15 05:08:08 +00001139 error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
1140 separator_type,
1141 var_expr_path_strm.GetString().c_str(),
1142 var_path.c_str());
1143
1144 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001145 }
1146 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001147
Greg Clayton54979cd2010-12-15 05:08:08 +00001148 if (child_valobj_sp)
1149 valobj_sp = child_valobj_sp;
1150
1151 if (var_path.empty())
1152 break;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001153 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001154 if (valobj_sp)
1155 {
1156 if (deref)
1157 {
Greg Claytonaf67cec2010-12-20 20:49:23 +00001158 ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
Greg Clayton54979cd2010-12-15 05:08:08 +00001159 valobj_sp = deref_valobj_sp;
1160 }
1161 else if (address_of)
1162 {
1163 ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
1164 valobj_sp = address_of_valobj_sp;
1165 }
1166 }
1167 return valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001168 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001169 else
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001170 {
Jim Ingham2837b762011-05-04 03:43:18 +00001171 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
1172 name_const_string.GetCString());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001173 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001174 }
1175 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001176 else
1177 {
1178 error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1179 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001180 return ValueObjectSP();
1181}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182
1183bool
1184StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
1185{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001186 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001187 if (!m_cfa_is_valid)
Jason Molenda99618472013-11-04 11:02:52 +00001188 {
1189 m_frame_base_error.SetErrorString("No frame base available for this historical stack frame.");
1190 return false;
1191 }
1192
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 if (m_flags.IsClear(GOT_FRAME_BASE))
1194 {
1195 if (m_sc.function)
1196 {
1197 m_frame_base.Clear();
1198 m_frame_base_error.Clear();
1199
1200 m_flags.Set(GOT_FRAME_BASE);
Greg Claytond9e416c2012-02-18 05:35:26 +00001201 ExecutionContext exe_ctx (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 Value expr_value;
Greg Clayton016a95e2010-09-14 02:20:48 +00001203 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1204 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
Greg Claytond9e416c2012-02-18 05:35:26 +00001205 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.GetTargetPtr());
Greg Clayton016a95e2010-09-14 02:20:48 +00001206
Tamas Berghammer5b42c7a2016-02-26 14:21:10 +00001207 if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx,
1208 nullptr,
1209 nullptr,
1210 nullptr,
1211 loclist_base_addr,
1212 nullptr,
1213 nullptr,
1214 expr_value,
1215 &m_frame_base_error) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 {
1217 // We should really have an error if evaluate returns, but in case
1218 // we don't, lets set the error to something at least.
1219 if (m_frame_base_error.Success())
1220 m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
1221 }
1222 else
1223 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001224 m_frame_base = expr_value.ResolveValue(&exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 }
1226 }
1227 else
1228 {
1229 m_frame_base_error.SetErrorString ("No function in symbol context.");
1230 }
1231 }
1232
1233 if (m_frame_base_error.Success())
1234 frame_base = m_frame_base;
1235
1236 if (error_ptr)
1237 *error_ptr = m_frame_base_error;
1238 return m_frame_base_error.Success();
1239}
1240
Sean Callanan4740a732016-09-06 04:48:36 +00001241DWARFExpression *
1242StackFrame::GetFrameBaseExpression(Error *error_ptr)
1243{
1244 if (!m_sc.function)
1245 {
1246 if (error_ptr)
1247 {
1248 error_ptr->SetErrorString ("No function in symbol context.");
1249 }
1250 return nullptr;
1251 }
1252
1253 return &m_sc.function->GetFrameBaseExpression();
1254}
1255
Greg Clayton5ccbd292011-01-06 22:15:06 +00001256RegisterContextSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257StackFrame::GetRegisterContext ()
1258{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001259 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001260 if (!m_reg_context_sp)
Greg Claytond9e416c2012-02-18 05:35:26 +00001261 {
1262 ThreadSP thread_sp (GetThread());
1263 if (thread_sp)
1264 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame (this);
1265 }
Greg Clayton5ccbd292011-01-06 22:15:06 +00001266 return m_reg_context_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267}
1268
1269bool
1270StackFrame::HasDebugInformation ()
1271{
Greg Clayton9da7bd02010-08-24 21:05:24 +00001272 GetSymbolContext (eSymbolContextLineEntry);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 return m_sc.line_entry.IsValid();
1274}
1275
Greg Clayton288bdf92010-09-02 02:59:18 +00001276ValueObjectSP
Greg Clayton4d122c42011-09-17 08:33:22 +00001277StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001279 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton288bdf92010-09-02 02:59:18 +00001280 ValueObjectSP valobj_sp;
Jason Molenda99618472013-11-04 11:02:52 +00001281 if (m_is_history_frame)
1282 {
1283 return valobj_sp;
1284 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001285 VariableList *var_list = GetVariableList (true);
1286 if (var_list)
1287 {
1288 // Make sure the variable is a frame variable
1289 const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
1290 const uint32_t num_variables = var_list->GetSize();
1291 if (var_idx < num_variables)
1292 {
1293 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001294 if (!valobj_sp)
Greg Clayton288bdf92010-09-02 02:59:18 +00001295 {
1296 if (m_variable_list_value_objects.GetSize() < num_variables)
1297 m_variable_list_value_objects.Resize(num_variables);
Jim Ingham58b59f92011-04-22 23:53:53 +00001298 valobj_sp = ValueObjectVariable::Create (this, variable_sp);
Greg Clayton288bdf92010-09-02 02:59:18 +00001299 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
1300 }
1301 }
1302 }
Greg Clayton4d122c42011-09-17 08:33:22 +00001303 if (use_dynamic != eNoDynamicValues && valobj_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +00001304 {
Jim Ingham2837b762011-05-04 03:43:18 +00001305 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue (use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001306 if (dynamic_sp)
1307 return dynamic_sp;
1308 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001309 return valobj_sp;
1310}
1311
1312ValueObjectSP
Greg Clayton4d122c42011-09-17 08:33:22 +00001313StackFrame::TrackGlobalVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
Greg Clayton288bdf92010-09-02 02:59:18 +00001314{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00001315 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Jason Molenda99618472013-11-04 11:02:52 +00001316 if (m_is_history_frame)
1317 return ValueObjectSP();
1318
Greg Clayton288bdf92010-09-02 02:59:18 +00001319 // Check to make sure we aren't already tracking this variable?
Jim Ingham78a685a2011-04-16 00:01:13 +00001320 ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp, use_dynamic));
Greg Clayton288bdf92010-09-02 02:59:18 +00001321 if (!valobj_sp)
1322 {
1323 // We aren't already tracking this global
1324 VariableList *var_list = GetVariableList (true);
1325 // If this frame has no variables, create a new list
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001326 if (var_list == nullptr)
Greg Clayton288bdf92010-09-02 02:59:18 +00001327 m_variable_list_sp.reset (new VariableList());
1328
1329 // Add the global/static variable to this frame
1330 m_variable_list_sp->AddVariable (variable_sp);
1331
1332 // Now make a value object for it so we can track its changes
Jim Ingham78a685a2011-04-16 00:01:13 +00001333 valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
Greg Clayton288bdf92010-09-02 02:59:18 +00001334 }
1335 return valobj_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336}
1337
Jim Ingham6b8379c2010-08-26 20:44:45 +00001338bool
1339StackFrame::IsInlined ()
1340{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001341 if (m_sc.block == nullptr)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001342 GetSymbolContext (eSymbolContextBlock);
1343 if (m_sc.block)
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001344 return m_sc.block->GetContainingInlinedBlock() != nullptr;
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00001345 return false;
Jim Ingham6b8379c2010-08-26 20:44:45 +00001346}
1347
Dawn Perchik009d1102015-09-04 01:02:30 +00001348lldb::LanguageType
1349StackFrame::GetLanguage ()
1350{
1351 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1352 if (cu)
1353 return cu->GetLanguage();
1354 return lldb::eLanguageTypeUnknown;
1355}
1356
Enrico Granata592afe72016-03-15 21:50:51 +00001357lldb::LanguageType
1358StackFrame::GuessLanguage ()
1359{
1360 LanguageType lang_type = GetLanguage();
1361
1362 if (lang_type == eLanguageTypeUnknown)
1363 {
1364 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1365 if (f)
1366 {
1367 lang_type = f->GetMangled().GuessLanguage();
1368 }
1369 }
1370
1371 return lang_type;
1372}
1373
Sean Callanan4740a732016-09-06 04:48:36 +00001374namespace
1375{
1376 std::pair<const Instruction::Operand *, int64_t>
1377 GetBaseExplainingValue(const Instruction::Operand &operand,
1378 RegisterContext &register_context,
1379 lldb::addr_t value)
1380 {
1381 switch(operand.m_type)
1382 {
1383 case Instruction::Operand::Type::Dereference:
1384 case Instruction::Operand::Type::Immediate:
1385 case Instruction::Operand::Type::Invalid:
1386 case Instruction::Operand::Type::Product:
1387 // These are not currently interesting
1388 return std::make_pair(nullptr, 0);
1389 case Instruction::Operand::Type::Sum:
1390 {
1391 const Instruction::Operand *immediate_child = nullptr;
1392 const Instruction::Operand *variable_child = nullptr;
1393 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate)
1394 {
1395 immediate_child = &operand.m_children[0];
1396 variable_child = &operand.m_children[1];
1397 }
1398 else if (operand.m_children[1].m_type == Instruction::Operand::Type::Immediate)
1399 {
1400 immediate_child = &operand.m_children[1];
1401 variable_child = &operand.m_children[0];
1402 }
1403 if (!immediate_child)
1404 {
1405 return std::make_pair(nullptr, 0);
1406 }
1407 lldb::addr_t adjusted_value = value;
1408 if (immediate_child->m_negative)
1409 {
1410 adjusted_value += immediate_child->m_immediate;
1411 }
1412 else
1413 {
1414 adjusted_value -= immediate_child->m_immediate;
1415 }
1416 std::pair<const Instruction::Operand *, int64_t> base_and_offset = GetBaseExplainingValue(*variable_child, register_context, adjusted_value);
1417 if (!base_and_offset.first)
1418 {
1419 return std::make_pair(nullptr, 0);
1420 }
1421 if (immediate_child->m_negative)
1422 {
1423 base_and_offset.second -= immediate_child->m_immediate;
1424 }
1425 else
1426 {
1427 base_and_offset.second += immediate_child->m_immediate;
1428 }
1429 return base_and_offset;
1430 }
1431 case Instruction::Operand::Type::Register:
1432 {
1433 const RegisterInfo *info = register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1434 if (!info)
1435 {
1436 return std::make_pair(nullptr, 0);
1437 }
1438 RegisterValue reg_value;
1439 if (!register_context.ReadRegister(info, reg_value))
1440 {
1441 return std::make_pair(nullptr, 0);
1442 }
1443 if (reg_value.GetAsUInt64() == value)
1444 {
1445 return std::make_pair(&operand, 0);
1446 }
1447 else
1448 {
1449 return std::make_pair(nullptr, 0);
1450 }
1451 }
1452 }
1453 }
1454
1455 std::pair<const Instruction::Operand *, int64_t>
1456 GetBaseExplainingDereference(const Instruction::Operand &operand,
1457 RegisterContext &register_context,
1458 lldb::addr_t addr)
1459 {
1460 if (operand.m_type == Instruction::Operand::Type::Dereference)
1461 {
1462 return GetBaseExplainingValue(operand.m_children[0],
1463 register_context,
1464 addr);
1465 }
1466 return std::make_pair(nullptr, 0);
1467 }
1468};
1469
1470lldb::ValueObjectSP
1471StackFrame::GuessValueForAddress(lldb::addr_t addr)
1472{
1473 TargetSP target_sp = CalculateTarget();
1474
1475 const ArchSpec &target_arch = target_sp->GetArchitecture();
1476
1477 AddressRange pc_range;
1478 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1479 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1480
1481 ExecutionContext exe_ctx (shared_from_this());
1482
1483 const char *plugin_name = nullptr;
1484 const char *flavor = nullptr;
1485 const bool prefer_file_cache = false;
1486
1487 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange (target_arch,
1488 plugin_name,
1489 flavor,
1490 exe_ctx,
1491 pc_range,
1492 prefer_file_cache);
1493
1494 if (!disassembler_sp->GetInstructionList().GetSize())
1495 {
1496 return ValueObjectSP();
1497 }
1498
1499 InstructionSP instruction_sp = disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1500
1501 llvm::SmallVector<Instruction::Operand, 3> operands;
1502
1503 if (!instruction_sp->ParseOperands(operands))
1504 {
1505 return ValueObjectSP();
1506 }
1507
1508 RegisterContextSP register_context_sp = GetRegisterContext();
1509
1510 if (!register_context_sp)
1511 {
1512 return ValueObjectSP();
1513 }
1514
1515 for (const Instruction::Operand &operand : operands)
1516 {
1517 std::pair<const Instruction::Operand *, int64_t>
1518 base_and_offset = GetBaseExplainingDereference(operand, *register_context_sp, addr);
1519
1520 if (!base_and_offset.first)
1521 {
1522 continue;
1523 }
1524
1525 switch (base_and_offset.first->m_type)
1526 {
1527 case Instruction::Operand::Type::Immediate:
1528 {
1529 lldb_private::Address addr;
1530 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate + base_and_offset.second, addr))
1531 {
1532 TypeSystem *c_type_system = target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1533 if (!c_type_system)
1534 {
1535 return ValueObjectSP();
1536 }
1537 else
1538 {
1539 CompilerType void_ptr_type = c_type_system->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar).GetPointerType();
1540 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
1541 }
1542 }
1543 else
1544 {
1545 return ValueObjectSP();
1546 }
1547 break;
1548 }
1549 case Instruction::Operand::Type::Register:
1550 {
1551 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register, base_and_offset.second);
1552 }
1553 default:
1554 return ValueObjectSP();
1555 }
1556
1557 }
1558
1559 return ValueObjectSP();
1560}
1561
1562namespace
1563{
1564 ValueObjectSP
1565 GetValueForOffset(StackFrame &frame, ValueObjectSP &parent, int64_t offset)
1566 {
1567 if (offset < 0 || offset >= parent->GetByteSize())
1568 {
1569 return ValueObjectSP();
1570 }
1571
1572 if (parent->IsPointerOrReferenceType())
1573 {
1574 return parent;
1575 }
1576
1577 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci)
1578 {
1579 const bool can_create = true;
1580 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1581
1582 if (!child_sp)
1583 {
1584 return ValueObjectSP();
1585 }
1586
1587 int64_t child_offset = child_sp->GetByteOffset();
1588 int64_t child_size = child_sp->GetByteSize();
1589
1590 if (offset >= child_offset &&
1591 offset < (child_offset + child_size))
1592 {
1593 return GetValueForOffset(frame, child_sp, offset - child_offset);
1594 }
1595 }
1596
1597 if (offset == 0)
1598 {
1599 return parent;
1600 }
1601 else
1602 {
1603 return ValueObjectSP();
1604 }
1605 }
1606
1607 ValueObjectSP
1608 GetValueForDereferincingOffset(StackFrame &frame, ValueObjectSP &base, int64_t offset)
1609 {
1610 // base is a pointer to something
1611 // offset is the thing to add to the pointer
1612 // We return the most sensible ValueObject for the result of *(base+offset)
1613
1614 if (!base->IsPointerOrReferenceType())
1615 {
1616 return ValueObjectSP();
1617 }
1618
1619 Error error;
1620 ValueObjectSP pointee = base->Dereference(error);
1621
1622 if (offset >= pointee->GetByteSize())
1623 {
1624 int64_t index = offset / pointee->GetByteSize();
1625 offset = offset % pointee->GetByteSize();
1626 const bool can_create = true;
1627 pointee = base->GetSyntheticArrayMember(index, can_create);
1628 }
1629
1630 if (!pointee || error.Fail())
1631 {
1632 return ValueObjectSP();
1633 }
1634
1635 return GetValueForOffset(frame, pointee, offset);
1636 }
1637
1638 //------------------------------------------------------------------
1639 /// Attempt to reconstruct the ValueObject for the address contained in a
1640 /// given register plus an offset.
1641 ///
1642 /// @params [in] frame
1643 /// The current stack frame.
1644 ///
1645 /// @params [in] reg
1646 /// The register.
1647 ///
1648 /// @params [in] offset
1649 /// The offset from the register.
1650 ///
1651 /// @param [in] disassembler
1652 /// A disassembler containing instructions valid up to the current PC.
1653 ///
1654 /// @param [in] variables
1655 /// The variable list from the current frame,
1656 ///
1657 /// @param [in] pc
1658 /// The program counter for the instruction considered the 'user'.
1659 ///
1660 /// @return
1661 /// A string describing the base for the ExpressionPath. This could be a
1662 /// variable, a register value, an argument, or a function return value.
1663 /// The ValueObject if found. If valid, it has a valid ExpressionPath.
1664 //------------------------------------------------------------------
1665 lldb::ValueObjectSP
1666 DoGuessValueAt(StackFrame &frame, ConstString reg, int64_t offset, Disassembler &disassembler, VariableList &variables, const Address &pc)
1667 {
1668 // Example of operation for Intel:
1669 //
1670 // +14: movq -0x8(%rbp), %rdi
1671 // +18: movq 0x8(%rdi), %rdi
1672 // +22: addl 0x4(%rdi), %eax
1673 //
1674 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1675 //
1676 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18 that assigns to rdi, and calls itself recursively for that dereference
1677 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at +14 that assigns to rdi, and calls itself recursively for that derefernece
1678 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the variable list.
1679 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1680 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8 at +18)
1681 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at rdi+4 at +22)
1682
1683 // First, check the variable list to see if anything is at the specified location.
1684 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi)
1685 {
1686 VariableSP var_sp = variables.GetVariableAtIndex(vi);
1687 DWARFExpression &dwarf_expression = var_sp->LocationExpression();
1688
1689 const RegisterInfo *expression_reg;
1690 int64_t expression_offset;
1691 ExecutionContext exe_ctx;
1692
1693 if (dwarf_expression.IsDereferenceOfRegister(frame, expression_reg, expression_offset))
1694 {
1695 if ((reg == ConstString(expression_reg->name) ||
1696 reg == ConstString(expression_reg->alt_name)) &&
1697 expression_offset == offset)
1698 {
1699 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1700 }
1701 }
1702 }
1703
1704 bool is_in_return_register = false;
1705 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1706 RegisterInfo return_register_info;
1707
1708 if (abi_sp)
1709 {
1710 const char *return_register_name;
1711 const RegisterInfo *reg_info = nullptr;
1712 if (abi_sp->GetPointerReturnRegister(return_register_name) &&
1713 reg == ConstString(return_register_name) &&
1714 (reg_info = frame.GetRegisterContext()->GetRegisterInfoByName(return_register_name)))
1715 {
1716 is_in_return_register = true;
1717 return_register_info = *reg_info;
1718 }
1719 }
1720
1721 const uint32_t current_inst = disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1722 if (current_inst == UINT32_MAX)
1723 {
1724 return ValueObjectSP();
1725 }
1726
1727 ValueObjectSP source_path;
1728
1729 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii)
1730 {
1731 // This is not an exact algorithm, and it sacrifices accuracy for generality.
1732 // Recognizing "mov" and "ld" instructions –– and which are their source and
1733 // destination operands -- is something the disassembler should do for us.
1734 InstructionSP instruction_sp = disassembler.GetInstructionList().GetInstructionAtIndex(ii);
1735
1736 if (is_in_return_register && instruction_sp->IsCall())
1737 {
1738 llvm::SmallVector<Instruction::Operand, 1> operands;
1739 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1)
1740 {
1741 continue;
1742 }
1743
1744 switch (operands[0].m_type)
1745 {
1746 default:
1747 break;
1748 case Instruction::Operand::Type::Immediate:
1749 {
1750 SymbolContext sc;
1751 Address load_address;
1752 if (!frame.CalculateTarget()->ResolveLoadAddress(operands[0].m_immediate, load_address))
1753 {
1754 break;
1755 }
1756 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(load_address, eSymbolContextFunction, sc);
1757 if (!sc.function)
1758 {
1759 break;
1760 }
1761 CompilerType function_type = sc.function->GetCompilerType();
1762 if (!function_type.IsFunctionType())
1763 {
1764 break;
1765 }
1766 CompilerType return_type = function_type.GetFunctionReturnType();
1767 RegisterValue return_value;
1768 if (!frame.GetRegisterContext()->ReadRegister(&return_register_info, return_value))
1769 {
1770 break;
1771 }
1772 std::string name_str(sc.function->GetName().AsCString("<unknown function>"));
1773 name_str.append("()");
1774 Address return_value_address(return_value.GetAsUInt64());
1775 ValueObjectSP return_value_sp = ValueObjectMemory::Create(&frame, name_str.c_str(), return_value_address, return_type);
1776 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1777 }
1778 }
1779
1780 continue;
1781 }
1782
1783 llvm::SmallVector<Instruction::Operand, 2> operands;
1784 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2)
1785 {
1786 continue;
1787 }
1788
1789 Instruction::Operand *register_operand = nullptr;
1790 Instruction::Operand *origin_operand = nullptr;
1791 if (operands[0].m_type == Instruction::Operand::Type::Register &&
1792 operands[0].m_clobbered == true &&
1793 operands[0].m_register == reg)
1794 {
1795 register_operand = &operands[0];
1796 origin_operand = &operands[1];
1797 }
1798 else if (operands[1].m_type == Instruction::Operand::Type::Register &&
1799 operands[1].m_clobbered == true &&
1800 operands[1].m_register == reg)
1801 {
1802 register_operand = &operands[1];
1803 origin_operand = &operands[0];
1804 }
1805 else
1806 {
1807 continue;
1808 }
1809
1810 // We have an origin operand. Can we track its value down?
1811 switch (origin_operand->m_type)
1812 {
1813 default:
1814 break;
1815 case Instruction::Operand::Type::Register:
1816 source_path = DoGuessValueAt(frame, origin_operand->m_register, 0, disassembler, variables, instruction_sp->GetAddress());
1817 break;
1818 case Instruction::Operand::Type::Dereference:
1819 {
1820 const Instruction::Operand &pointer = origin_operand->m_children[0];
1821 switch (pointer.m_type)
1822 {
1823 default:
1824 break;
1825 case Instruction::Operand::Type::Register:
1826 source_path = DoGuessValueAt(frame, pointer.m_register, 0, disassembler, variables, instruction_sp->GetAddress());
1827 if (source_path)
1828 {
1829 Error err;
1830 source_path = source_path->Dereference(err);
1831 if (!err.Success())
1832 {
1833 source_path.reset();
1834 }
1835 }
1836 break;
1837 case Instruction::Operand::Type::Sum:
1838 {
1839 const Instruction::Operand *origin_register = nullptr;
1840 const Instruction::Operand *origin_offset = nullptr;
1841 if (pointer.m_children.size() != 2)
1842 {
1843 break;
1844 }
1845 if (pointer.m_children[0].m_type == Instruction::Operand::Type::Register &&
1846 pointer.m_children[1].m_type == Instruction::Operand::Type::Immediate)
1847 {
1848 origin_register = &pointer.m_children[0];
1849 origin_offset = &pointer.m_children[1];
1850 }
1851 else if (pointer.m_children[1].m_type == Instruction::Operand::Type::Register &&
1852 pointer.m_children[0].m_type == Instruction::Operand::Type::Immediate)
1853 {
1854 origin_register = &pointer.m_children[1];
1855 origin_offset = &pointer.m_children[0];
1856 }
1857 if (!origin_register)
1858 {
1859 break;
1860 }
1861 int64_t signed_origin_offset = origin_offset->m_negative ? -((int64_t)origin_offset->m_immediate) : origin_offset->m_immediate;
1862 source_path = DoGuessValueAt(frame, origin_register->m_register, signed_origin_offset, disassembler, variables, instruction_sp->GetAddress());
1863 if (!source_path)
1864 {
1865 break;
1866 }
1867 source_path = GetValueForDereferincingOffset(frame, source_path, offset);
1868 break;
1869 }
1870 }
1871 }
1872 }
1873
1874 if (source_path)
1875 {
1876 return source_path;
1877 }
1878 }
1879
1880 return ValueObjectSP();
1881 }
1882}
1883
1884lldb::ValueObjectSP
1885StackFrame::GuessValueForRegisterAndOffset(ConstString reg, int64_t offset)
1886{
1887 TargetSP target_sp = CalculateTarget();
1888
1889 const ArchSpec &target_arch = target_sp->GetArchitecture();
1890
1891 Block *frame_block = GetFrameBlock();
1892
1893 if (!frame_block)
1894 {
1895 return ValueObjectSP();
1896 }
1897
1898 Function *function = frame_block->CalculateSymbolContextFunction();
1899 if (!function)
1900 {
1901 return ValueObjectSP();
1902 }
1903
1904 AddressRange pc_range = function->GetAddressRange();
1905
1906 if (GetFrameCodeAddress().GetFileAddress() < pc_range.GetBaseAddress().GetFileAddress() ||
1907 GetFrameCodeAddress().GetFileAddress() - pc_range.GetBaseAddress().GetFileAddress() >= pc_range.GetByteSize())
1908 {
1909 return ValueObjectSP();
1910 }
1911
1912 ExecutionContext exe_ctx (shared_from_this());
1913
1914 const char *plugin_name = nullptr;
1915 const char *flavor = nullptr;
1916 const bool prefer_file_cache = false;
1917 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange (target_arch,
1918 plugin_name,
1919 flavor,
1920 exe_ctx,
1921 pc_range,
1922 prefer_file_cache);
1923
1924 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize())
1925 {
1926 return ValueObjectSP();
1927 }
1928
1929 const bool get_file_globals = false;
1930 VariableList *variables = GetVariableList(get_file_globals);
1931
1932 if (!variables)
1933 {
1934 return ValueObjectSP();
1935 }
1936
1937 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables, GetFrameCodeAddress());
1938}
1939
Greg Claytond9e416c2012-02-18 05:35:26 +00001940TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001941StackFrame::CalculateTarget ()
1942{
Greg Claytond9e416c2012-02-18 05:35:26 +00001943 TargetSP target_sp;
1944 ThreadSP thread_sp(GetThread());
1945 if (thread_sp)
1946 {
1947 ProcessSP process_sp (thread_sp->CalculateProcess());
1948 if (process_sp)
1949 target_sp = process_sp->CalculateTarget();
1950 }
1951 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952}
1953
Greg Claytond9e416c2012-02-18 05:35:26 +00001954ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001955StackFrame::CalculateProcess ()
1956{
Greg Claytond9e416c2012-02-18 05:35:26 +00001957 ProcessSP process_sp;
1958 ThreadSP thread_sp(GetThread());
1959 if (thread_sp)
1960 process_sp = thread_sp->CalculateProcess();
1961 return process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962}
1963
Greg Claytond9e416c2012-02-18 05:35:26 +00001964ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965StackFrame::CalculateThread ()
1966{
Greg Claytond9e416c2012-02-18 05:35:26 +00001967 return GetThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001968}
1969
Jason Molendab57e4a12013-11-04 09:33:30 +00001970StackFrameSP
1971StackFrame::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972{
Greg Claytond9e416c2012-02-18 05:35:26 +00001973 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974}
1975
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976void
Greg Clayton0603aa92010-10-04 01:05:56 +00001977StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001978{
Greg Claytond9e416c2012-02-18 05:35:26 +00001979 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001980}
1981
1982void
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001983StackFrame::DumpUsingSettingsFormat (Stream *strm, const char *frame_marker)
Greg Clayton0603aa92010-10-04 01:05:56 +00001984{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001985 if (strm == nullptr)
Greg Clayton0603aa92010-10-04 01:05:56 +00001986 return;
1987
1988 GetSymbolContext(eSymbolContextEverything);
Greg Claytond9e416c2012-02-18 05:35:26 +00001989 ExecutionContext exe_ctx (shared_from_this());
Greg Clayton0603aa92010-10-04 01:05:56 +00001990 StreamString s;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00001991
1992 if (frame_marker)
1993 s.PutCString(frame_marker);
1994
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001995 const FormatEntity::Entry *frame_format = nullptr;
Greg Claytond9e416c2012-02-18 05:35:26 +00001996 Target *target = exe_ctx.GetTargetPtr();
1997 if (target)
1998 frame_format = target->GetDebugger().GetFrameFormat();
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00001999 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx, nullptr, nullptr, false, false))
Greg Clayton0603aa92010-10-04 01:05:56 +00002000 {
2001 strm->Write(s.GetData(), s.GetSize());
2002 }
2003 else
2004 {
2005 Dump (strm, true, false);
2006 strm->EOL();
2007 }
2008}
2009
2010void
Greg Clayton6dadd502010-09-02 21:44:10 +00002011StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00002013 if (strm == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014 return;
2015
2016 if (show_frame_index)
Greg Clayton1b72fcb2010-08-24 00:45:41 +00002017 strm->Printf("frame #%u: ", m_frame_index);
Greg Claytond9e416c2012-02-18 05:35:26 +00002018 ExecutionContext exe_ctx (shared_from_this());
2019 Target *target = exe_ctx.GetTargetPtr();
Daniel Malead01b2952012-11-29 21:49:15 +00002020 strm->Printf("0x%0*" PRIx64 " ",
Greg Claytond9e416c2012-02-18 05:35:26 +00002021 target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16,
2022 GetFrameCodeAddress().GetLoadAddress(target));
Greg Clayton9da7bd02010-08-24 21:05:24 +00002023 GetSymbolContext(eSymbolContextEverything);
Greg Clayton1b72fcb2010-08-24 00:45:41 +00002024 const bool show_module = true;
2025 const bool show_inline = true;
Jason Molendaaff1b352014-10-10 23:07:36 +00002026 const bool show_function_arguments = true;
Jason Molendac980fa92015-02-13 23:24:21 +00002027 const bool show_function_name = true;
Greg Claytond9e416c2012-02-18 05:35:26 +00002028 m_sc.DumpStopContext (strm,
2029 exe_ctx.GetBestExecutionContextScope(),
2030 GetFrameCodeAddress(),
2031 show_fullpaths,
2032 show_module,
Jason Molendaaff1b352014-10-10 23:07:36 +00002033 show_inline,
Jason Molendac980fa92015-02-13 23:24:21 +00002034 show_function_arguments,
2035 show_function_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036}
2037
Greg Clayton5082c5f2010-08-27 18:24:16 +00002038void
Jason Molendab57e4a12013-11-04 09:33:30 +00002039StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
Greg Clayton5082c5f2010-08-27 18:24:16 +00002040{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00002041 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00002042 assert (GetStackID() == prev_frame.GetStackID()); // TODO: remove this after some testing
Jason Molendab57e4a12013-11-04 09:33:30 +00002043 m_variable_list_sp = prev_frame.m_variable_list_sp;
2044 m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
2045 if (!m_disassembly.GetString().empty())
2046 m_disassembly.GetString().swap (m_disassembly.GetString());
Greg Clayton5082c5f2010-08-27 18:24:16 +00002047}
Greg Clayton68275d52010-08-27 21:47:54 +00002048
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00002049void
Jason Molendab57e4a12013-11-04 09:33:30 +00002050StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00002051{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +00002052 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Greg Clayton2cad65a2010-09-03 17:10:42 +00002053 assert (GetStackID() == curr_frame.GetStackID()); // TODO: remove this after some testing
Jason Molendab57e4a12013-11-04 09:33:30 +00002054 m_id.SetPC (curr_frame.m_id.GetPC()); // Update the Stack ID PC value
2055 assert (GetThread() == curr_frame.GetThread());
2056 m_frame_index = curr_frame.m_frame_index;
2057 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
2058 m_reg_context_sp = curr_frame.m_reg_context_sp;
2059 m_frame_code_addr = curr_frame.m_frame_code_addr;
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00002060 assert (!m_sc.target_sp || !curr_frame.m_sc.target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
2061 assert (!m_sc.module_sp || !curr_frame.m_sc.module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
2062 assert (m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
2063 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 +00002064 m_sc = curr_frame.m_sc;
2065 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
2066 m_flags.Set (m_sc.GetResolvedMask());
2067 m_frame_base.Clear();
2068 m_frame_base_error.Clear();
Greg Clayton59e8fc1c2010-08-30 18:11:35 +00002069}
2070
Greg Clayton2cad65a2010-09-03 17:10:42 +00002071bool
Jason Molendab57e4a12013-11-04 09:33:30 +00002072StackFrame::HasCachedData () const
2073{
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00002074 if (m_variable_list_sp)
Jason Molendab57e4a12013-11-04 09:33:30 +00002075 return true;
2076 if (m_variable_list_value_objects.GetSize() > 0)
2077 return true;
2078 if (!m_disassembly.GetString().empty())
2079 return true;
2080 return false;
2081}
2082
2083bool
Greg Clayton7260f622011-04-18 08:33:37 +00002084StackFrame::GetStatus (Stream& strm,
2085 bool show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002086 bool show_source,
2087 const char *frame_marker)
Greg Clayton7260f622011-04-18 08:33:37 +00002088{
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002089
Greg Clayton7260f622011-04-18 08:33:37 +00002090 if (show_frame_info)
2091 {
2092 strm.Indent();
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002093 DumpUsingSettingsFormat (&strm, frame_marker);
Greg Clayton7260f622011-04-18 08:33:37 +00002094 }
2095
2096 if (show_source)
2097 {
Greg Claytond9e416c2012-02-18 05:35:26 +00002098 ExecutionContext exe_ctx (shared_from_this());
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002099 bool have_source = false, have_debuginfo = false;
Greg Clayton67cc0632012-08-22 17:17:09 +00002100 Debugger::StopDisassemblyType disasm_display = Debugger::eStopDisassemblyTypeNever;
Greg Claytond9e416c2012-02-18 05:35:26 +00002101 Target *target = exe_ctx.GetTargetPtr();
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002102 if (target)
Greg Clayton7260f622011-04-18 08:33:37 +00002103 {
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002104 Debugger &debugger = target->GetDebugger();
2105 const uint32_t source_lines_before = debugger.GetStopSourceLineCount(true);
2106 const uint32_t source_lines_after = debugger.GetStopSourceLineCount(false);
2107 disasm_display = debugger.GetStopDisassemblyDisplay ();
Greg Claytone372b982011-11-21 21:44:34 +00002108
Todd Fiala6d1fbc92014-07-07 20:47:24 +00002109 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
2110 if (m_sc.comp_unit && m_sc.line_entry.IsValid())
Greg Claytone372b982011-11-21 21:44:34 +00002111 {
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002112 have_debuginfo = true;
Todd Fiala6d1fbc92014-07-07 20:47:24 +00002113 if (source_lines_before > 0 || source_lines_after > 0)
Greg Claytone372b982011-11-21 21:44:34 +00002114 {
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002115 size_t num_lines = target->GetSourceManager().DisplaySourceLinesWithLineNumbers (m_sc.line_entry.file,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002116 m_sc.line_entry.line,
2117 source_lines_before,
2118 source_lines_after,
2119 "->",
Jason Molenda7cd81c52013-04-29 09:59:31 +00002120 &strm);
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002121 if (num_lines != 0)
2122 have_source = true;
2123 // TODO: Give here a one time warning if source file is missing.
Greg Claytone372b982011-11-21 21:44:34 +00002124 }
2125 }
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002126 switch (disasm_display)
2127 {
Greg Clayton67cc0632012-08-22 17:17:09 +00002128 case Debugger::eStopDisassemblyTypeNever:
Greg Claytone372b982011-11-21 21:44:34 +00002129 break;
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002130
2131 case Debugger::eStopDisassemblyTypeNoDebugInfo:
2132 if (have_debuginfo)
2133 break;
Jason Molenda62e06812016-02-16 04:14:33 +00002134 LLVM_FALLTHROUGH;
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002135
Greg Clayton67cc0632012-08-22 17:17:09 +00002136 case Debugger::eStopDisassemblyTypeNoSource:
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002137 if (have_source)
2138 break;
Jason Molenda62e06812016-02-16 04:14:33 +00002139 LLVM_FALLTHROUGH;
Mohit K. Bhakkad8be74992015-12-03 04:56:16 +00002140
Greg Clayton67cc0632012-08-22 17:17:09 +00002141 case Debugger::eStopDisassemblyTypeAlways:
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002142 if (target)
Greg Claytone372b982011-11-21 21:44:34 +00002143 {
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002144 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
2145 if (disasm_lines > 0)
2146 {
2147 const ArchSpec &target_arch = target->GetArchitecture();
2148 AddressRange pc_range;
2149 pc_range.GetBaseAddress() = GetFrameCodeAddress();
2150 pc_range.SetByteSize(disasm_lines * target_arch.GetMaximumOpcodeByteSize());
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00002151 const char *plugin_name = nullptr;
2152 const char *flavor = nullptr;
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002153 Disassembler::Disassemble (target->GetDebugger(),
2154 target_arch,
Jim Ingham0f063ba2013-03-02 00:26:47 +00002155 plugin_name,
2156 flavor,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002157 exe_ctx,
2158 pc_range,
2159 disasm_lines,
2160 0,
2161 Disassembler::eOptionMarkPCAddress,
2162 strm);
2163 }
Greg Claytone372b982011-11-21 21:44:34 +00002164 }
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002165 break;
Greg Claytone372b982011-11-21 21:44:34 +00002166 }
Greg Clayton7260f622011-04-18 08:33:37 +00002167 }
2168 }
2169 return true;
2170}