blob: d0f7835efef8c71b1153594f85f76221b4ff9d09 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- StackFrame.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenkod70a6e72016-02-18 18:52:47 +00009#include "lldb/Target/StackFrame.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000010#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Core/Disassembler.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000012#include "lldb/Core/FormatEntity.h"
Enrico Granata592afe72016-03-15 21:50:51 +000013#include "lldb/Core/Mangled.h"
14#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Value.h"
Greg Clayton54979cd2010-12-15 05:08:08 +000016#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan4740a732016-09-06 04:48:36 +000017#include "lldb/Core/ValueObjectMemory.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton1f746072012-08-29 21:13:06 +000019#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/Symbol.h"
22#include "lldb/Symbol/SymbolContextScope.h"
Enrico Granata46252392015-11-19 22:28:58 +000023#include "lldb/Symbol/Type.h"
Greg Clayton288bdf92010-09-02 02:59:18 +000024#include "lldb/Symbol/VariableList.h"
Sean Callanan4740a732016-09-06 04:48:36 +000025#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
Kuba Mracek41ae8e72018-10-31 04:00:22 +000029#include "lldb/Target/StackFrameRecognizer.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/Target.h"
31#include "lldb/Target/Thread.h"
Pavel Labathd821c992018-08-07 11:07:21 +000032#include "lldb/Utility/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
Zachary Turner991e4452018-10-25 20:45:19 +000034#include "lldb/lldb-enumerations.h"
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036using namespace lldb;
37using namespace lldb_private;
38
39// The first bits in the flags are reserved for the SymbolContext::Scope bits
40// so we know if we have tried to look up information in our internal symbol
41// context (m_sc) already.
Kate Stoneb9c1b512016-09-06 20:57:50 +000042#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
43#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
44#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
45#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
46#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
49 user_id_t unwind_frame_index, addr_t cfa,
Vedant Kumar4b36f792018-10-05 23:23:15 +000050 bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000051 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
53 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
54 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
55 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
Vedant Kumar4b36f792018-10-05 23:23:15 +000056 m_stack_frame_kind(kind), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +000057 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
58 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 // If we don't have a CFA value, use the frame index for our StackID so that
Adrian Prantl05097242018-04-30 16:49:04 +000060 // recursive functions properly aren't confused with one another on a history
61 // stack.
Vedant Kumar4b36f792018-10-05 23:23:15 +000062 if (IsHistorical() && !m_cfa_is_valid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 m_id.SetCFA(m_frame_index);
64 }
Jason Molenda99618472013-11-04 11:02:52 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 if (sc_ptr != nullptr) {
67 m_sc = *sc_ptr;
68 m_flags.Set(m_sc.GetResolvedMask());
69 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070}
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
73 user_id_t unwind_frame_index,
74 const RegisterContextSP &reg_context_sp, addr_t cfa,
75 addr_t pc, const SymbolContext *sc_ptr)
76 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000077 m_concrete_frame_index(unwind_frame_index),
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
79 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +000080 m_frame_base_error(), m_cfa_is_valid(true),
81 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +000082 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
83 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 if (sc_ptr != nullptr) {
85 m_sc = *sc_ptr;
86 m_flags.Set(m_sc.GetResolvedMask());
87 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 if (reg_context_sp && !m_sc.target_sp) {
90 m_sc.target_sp = reg_context_sp->CalculateTarget();
91 if (m_sc.target_sp)
92 m_flags.Set(eSymbolContextTarget);
93 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
97 user_id_t unwind_frame_index,
98 const RegisterContextSP &reg_context_sp, addr_t cfa,
99 const Address &pc_addr, const SymbolContext *sc_ptr)
100 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000101 m_concrete_frame_index(unwind_frame_index),
102 m_reg_context_sp(reg_context_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
104 nullptr),
105 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +0000106 m_frame_base_error(), m_cfa_is_valid(true),
107 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +0000108 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
109 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 if (sc_ptr != nullptr) {
111 m_sc = *sc_ptr;
112 m_flags.Set(m_sc.GetResolvedMask());
113 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 if (!m_sc.target_sp && reg_context_sp) {
116 m_sc.target_sp = reg_context_sp->CalculateTarget();
117 if (m_sc.target_sp)
118 m_flags.Set(eSymbolContextTarget);
119 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 ModuleSP pc_module_sp(pc_addr.GetModule());
122 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
123 if (pc_module_sp) {
124 m_sc.module_sp = pc_module_sp;
125 m_flags.Set(eSymbolContextModule);
126 } else {
127 m_sc.module_sp.reset();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130}
131
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000132StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134StackID &StackFrame::GetStackID() {
135 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Adrian Prantl05097242018-04-30 16:49:04 +0000136 // Make sure we have resolved the StackID object's symbol context scope if we
137 // already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
140 if (m_id.GetSymbolContextScope()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000141 // We already have a symbol context scope, we just don't have our flag
142 // bit set.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
144 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000145 // Calculate the frame block and use this for the stack ID symbol context
146 // scope if we have one.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000147 SymbolContextScope *scope = GetFrameBlock();
148 if (scope == nullptr) {
149 // We don't have a block, so use the symbol
150 if (m_flags.IsClear(eSymbolContextSymbol))
151 GetSymbolContext(eSymbolContextSymbol);
152
153 // It is ok if m_sc.symbol is nullptr here
154 scope = m_sc.symbol;
155 }
156 // Set the symbol context scope (the accessor will set the
157 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
158 SetSymbolContextScope(scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 }
161 return m_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162}
163
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164uint32_t StackFrame::GetFrameIndex() const {
165 ThreadSP thread_sp = GetThread();
166 if (thread_sp)
167 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
168 m_frame_index);
169 else
170 return m_frame_index;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000171}
172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
174 std::lock_guard<std::recursive_mutex> guard(m_mutex);
175 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
176 m_id.SetSymbolContextScope(symbol_scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179const Address &StackFrame::GetFrameCodeAddress() {
180 std::lock_guard<std::recursive_mutex> guard(m_mutex);
181 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
182 !m_frame_code_addr.IsSectionOffset()) {
183 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185 // Resolve the PC into a temporary address because if ResolveLoadAddress
186 // fails to resolve the address, it will clear the address object...
187 ThreadSP thread_sp(GetThread());
188 if (thread_sp) {
189 TargetSP target_sp(thread_sp->CalculateTarget());
190 if (target_sp) {
Pavel Labathc3c72122017-06-08 13:26:35 +0000191 const bool allow_section_end = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 if (m_frame_code_addr.SetOpcodeLoadAddress(
193 m_frame_code_addr.GetOffset(), target_sp.get(),
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000194 AddressClass::eCode, allow_section_end)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 ModuleSP module_sp(m_frame_code_addr.GetModule());
196 if (module_sp) {
197 m_sc.module_sp = module_sp;
198 m_flags.Set(eSymbolContextModule);
199 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 }
204 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205}
206
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207bool StackFrame::ChangePC(addr_t pc) {
208 std::lock_guard<std::recursive_mutex> guard(m_mutex);
209 // We can't change the pc value of a history stack frame - it is immutable.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000210 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 return false;
212 m_frame_code_addr.SetRawAddress(pc);
213 m_sc.Clear(false);
214 m_flags.Reset(0);
215 ThreadSP thread_sp(GetThread());
216 if (thread_sp)
217 thread_sp->ClearStackFrames();
218 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219}
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221const char *StackFrame::Disassemble() {
222 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner8b3f2162017-02-28 17:59:59 +0000223 if (m_disassembly.Empty()) {
224 ExecutionContext exe_ctx(shared_from_this());
225 Target *target = exe_ctx.GetTargetPtr();
226 if (target) {
227 const char *plugin_name = nullptr;
228 const char *flavor = nullptr;
229 Disassembler::Disassemble(target->GetDebugger(),
230 target->GetArchitecture(), plugin_name, flavor,
231 exe_ctx, 0, false, 0, 0, m_disassembly);
232 }
233 if (m_disassembly.Empty())
234 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 }
Zachary Turner8b3f2162017-02-28 17:59:59 +0000236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240Block *StackFrame::GetFrameBlock() {
241 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
242 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (m_sc.block) {
245 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
246 if (inline_block) {
Adrian Prantl05097242018-04-30 16:49:04 +0000247 // Use the block with the inlined function info as the frame block we
248 // want this frame to have only the variables for the inlined function
249 // and its non-inlined block child blocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 return inline_block;
251 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000252 // This block is not contained within any inlined function blocks with so
253 // we want to use the top most function block.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254 return &m_sc.function->GetBlock(false);
255 }
256 }
257 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000258}
259
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260//----------------------------------------------------------------------
261// Get the symbol context if we already haven't done so by resolving the
262// PC address as much as possible. This way when we pass around a
Adrian Prantl05097242018-04-30 16:49:04 +0000263// StackFrame object, everyone will have as much information as possible and no
264// one will ever have to look things up manually.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265//----------------------------------------------------------------------
Zachary Turner991e4452018-10-25 20:45:19 +0000266const SymbolContext &
267StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 std::lock_guard<std::recursive_mutex> guard(m_mutex);
269 // Copy our internal symbol context into "sc".
270 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
271 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 // If the target was requested add that:
274 if (!m_sc.target_sp) {
275 m_sc.target_sp = CalculateTarget();
276 if (m_sc.target_sp)
277 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 }
279
Adrian Prantl05097242018-04-30 16:49:04 +0000280 // Resolve our PC to section offset if we haven't already done so and if we
281 // don't have a module. The resolved address section will contain the
282 // module to which it belongs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
284 GetFrameCodeAddress();
285
Adrian Prantl05097242018-04-30 16:49:04 +0000286 // If this is not frame zero, then we need to subtract 1 from the PC value
287 // when doing address lookups since the PC will be on the instruction
288 // following the function call instruction...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000289
290 Address lookup_addr(GetFrameCodeAddress());
291 if (m_frame_index > 0 && lookup_addr.IsValid()) {
292 addr_t offset = lookup_addr.GetOffset();
293 if (offset > 0) {
294 lookup_addr.SetOffset(offset - 1);
295
296 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000297 // lookup_addr is the start of a section. We need do the math on the
298 // actual load address and re-compute the section. We're working with
299 // a 'noreturn' function at the end of a section.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300 ThreadSP thread_sp(GetThread());
301 if (thread_sp) {
302 TargetSP target_sp(thread_sp->CalculateTarget());
303 if (target_sp) {
304 addr_t addr_minus_one =
305 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
306 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
307 } else {
308 lookup_addr.SetOffset(offset - 1);
309 }
310 }
311 }
312 }
313
314 if (m_sc.module_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000315 // We have something in our stack frame symbol context, lets check if we
316 // haven't already tried to lookup one of those things. If we haven't
317 // then we will do the query.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318
Zachary Turner991e4452018-10-25 20:45:19 +0000319 SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320
321 if (resolve_scope & eSymbolContextCompUnit) {
322 if (m_flags.IsClear(eSymbolContextCompUnit)) {
323 if (m_sc.comp_unit)
324 resolved |= eSymbolContextCompUnit;
325 else
326 actual_resolve_scope |= eSymbolContextCompUnit;
327 }
328 }
329
330 if (resolve_scope & eSymbolContextFunction) {
331 if (m_flags.IsClear(eSymbolContextFunction)) {
332 if (m_sc.function)
333 resolved |= eSymbolContextFunction;
334 else
335 actual_resolve_scope |= eSymbolContextFunction;
336 }
337 }
338
339 if (resolve_scope & eSymbolContextBlock) {
340 if (m_flags.IsClear(eSymbolContextBlock)) {
341 if (m_sc.block)
342 resolved |= eSymbolContextBlock;
343 else
344 actual_resolve_scope |= eSymbolContextBlock;
345 }
346 }
347
348 if (resolve_scope & eSymbolContextSymbol) {
349 if (m_flags.IsClear(eSymbolContextSymbol)) {
350 if (m_sc.symbol)
351 resolved |= eSymbolContextSymbol;
352 else
353 actual_resolve_scope |= eSymbolContextSymbol;
354 }
355 }
356
357 if (resolve_scope & eSymbolContextLineEntry) {
358 if (m_flags.IsClear(eSymbolContextLineEntry)) {
359 if (m_sc.line_entry.IsValid())
360 resolved |= eSymbolContextLineEntry;
361 else
362 actual_resolve_scope |= eSymbolContextLineEntry;
363 }
364 }
365
366 if (actual_resolve_scope) {
Adrian Prantl05097242018-04-30 16:49:04 +0000367 // We might be resolving less information than what is already in our
368 // current symbol context so resolve into a temporary symbol context
369 // "sc" so we don't clear out data we have already found in "m_sc"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 SymbolContext sc;
371 // Set flags that indicate what we have tried to resolve
372 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
373 lookup_addr, actual_resolve_scope, sc);
Adrian Prantl05097242018-04-30 16:49:04 +0000374 // Only replace what we didn't already have as we may have information
375 // for an inlined function scope that won't match what a standard
376 // lookup by address would match
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
378 m_sc.comp_unit = sc.comp_unit;
379 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
380 m_sc.function = sc.function;
381 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
382 m_sc.block = sc.block;
383 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
384 m_sc.symbol = sc.symbol;
385 if ((resolved & eSymbolContextLineEntry) &&
386 !m_sc.line_entry.IsValid()) {
387 m_sc.line_entry = sc.line_entry;
388 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
389 }
390 }
391 } else {
392 // If we don't have a module, then we can't have the compile unit,
393 // function, block, line entry or symbol, so we can safely call
394 // ResolveSymbolContextForAddress with our symbol context member m_sc.
395 if (m_sc.target_sp) {
396 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
397 lookup_addr, resolve_scope, m_sc);
398 }
399 }
400
401 // Update our internal flags so we remember what we have tried to locate so
402 // we don't have to keep trying when more calls to this function are made.
Adrian Prantl05097242018-04-30 16:49:04 +0000403 // We might have dug up more information that was requested (for example if
404 // we were asked to only get the block, we will have gotten the compile
405 // unit, and function) so set any additional bits that we resolved
Kate Stoneb9c1b512016-09-06 20:57:50 +0000406 m_flags.Set(resolve_scope | resolved);
407 }
408
409 // Return the symbol context with everything that was possible to resolve
410 // resolved.
411 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414VariableList *StackFrame::GetVariableList(bool get_file_globals) {
415 std::lock_guard<std::recursive_mutex> guard(m_mutex);
416 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
417 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 Block *frame_block = GetFrameBlock();
420
421 if (frame_block) {
422 const bool get_child_variables = true;
423 const bool can_create = true;
424 const bool stop_if_child_block_is_inlined_function = true;
425 m_variable_list_sp.reset(new VariableList());
426 frame_block->AppendBlockVariables(can_create, get_child_variables,
427 stop_if_child_block_is_inlined_function,
Zachary Turner3bc714b2017-03-02 00:05:25 +0000428 [](Variable *v) { return true; },
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000430 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 }
432
433 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
434 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
435
436 if (m_flags.IsClear(eSymbolContextCompUnit))
437 GetSymbolContext(eSymbolContextCompUnit);
438
439 if (m_sc.comp_unit) {
440 VariableListSP global_variable_list_sp(
441 m_sc.comp_unit->GetVariableList(true));
442 if (m_variable_list_sp)
443 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
444 else
445 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 }
448
449 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450}
451
Greg Claytond41f0322011-08-02 23:35:43 +0000452VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453StackFrame::GetInScopeVariableList(bool get_file_globals,
454 bool must_have_valid_location) {
455 std::lock_guard<std::recursive_mutex> guard(m_mutex);
456 // We can't fetch variable information for a history stack frame.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000457 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 VariableListSP var_list_sp(new VariableList);
461 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 if (m_sc.block) {
464 const bool can_create = true;
465 const bool get_parent_variables = true;
466 const bool stop_if_block_is_inlined_function = true;
467 m_sc.block->AppendVariables(
468 can_create, get_parent_variables, stop_if_block_is_inlined_function,
469 [this, must_have_valid_location](Variable *v) {
470 return v->IsInScope(this) && (!must_have_valid_location ||
471 v->LocationIsValidForFrame(this));
472 },
473 var_list_sp.get());
474 }
475
476 if (m_sc.comp_unit && get_file_globals) {
477 VariableListSP global_variable_list_sp(
478 m_sc.comp_unit->GetVariableList(true));
479 if (global_variable_list_sp)
480 var_list_sp->AddVariables(global_variable_list_sp.get());
481 }
482
483 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000484}
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner0eb31a12016-11-17 05:14:32 +0000487 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
Zachary Turner97206d52017-05-12 04:51:55 +0000488 VariableSP &var_sp, Status &error) {
Zachary Turner0eb31a12016-11-17 05:14:32 +0000489 llvm::StringRef original_var_expr = var_expr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000490 // We can't fetch variable information for a history stack frame.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000491 if (IsHistorical())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000492 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
Zachary Turner0eb31a12016-11-17 05:14:32 +0000494 if (var_expr.empty()) {
495 error.SetErrorStringWithFormat("invalid variable path '%s'",
496 var_expr.str().c_str());
Zachary Turner24bd3172016-11-17 01:37:52 +0000497 return ValueObjectSP();
498 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000499
Zachary Turner24bd3172016-11-17 01:37:52 +0000500 const bool check_ptr_vs_member =
501 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
502 const bool no_fragile_ivar =
503 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
504 const bool no_synth_child =
505 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
506 // const bool no_synth_array = (options &
507 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
508 error.Clear();
509 bool deref = false;
510 bool address_of = false;
511 ValueObjectSP valobj_sp;
512 const bool get_file_globals = true;
513 // When looking up a variable for an expression, we need only consider the
514 // variables that are in scope.
515 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
516 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517
Zachary Turner24bd3172016-11-17 01:37:52 +0000518 if (!variable_list)
519 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520
Zachary Turner24bd3172016-11-17 01:37:52 +0000521 // If first character is a '*', then show pointer contents
Zachary Turner24bd3172016-11-17 01:37:52 +0000522 std::string var_expr_storage;
523 if (var_expr[0] == '*') {
524 deref = true;
525 var_expr = var_expr.drop_front(); // Skip the '*'
526 } else if (var_expr[0] == '&') {
527 address_of = true;
528 var_expr = var_expr.drop_front(); // Skip the '&'
529 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000530
Zachary Turner24bd3172016-11-17 01:37:52 +0000531 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
532 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533
Zachary Turner24bd3172016-11-17 01:37:52 +0000534 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535
Zachary Turner24bd3172016-11-17 01:37:52 +0000536 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537
Zachary Turner24bd3172016-11-17 01:37:52 +0000538 bool synthetically_added_instance_object = false;
539
540 if (var_sp) {
541 var_expr = var_expr.drop_front(name_const_string.GetLength());
542 }
543
544 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000545 // Check for direct ivars access which helps us with implicit access to
546 // ivars with the "this->" or "self->"
Zachary Turner24bd3172016-11-17 01:37:52 +0000547 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
548 lldb::LanguageType method_language = eLanguageTypeUnknown;
549 bool is_instance_method = false;
550 ConstString method_object_name;
551 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
552 method_object_name)) {
553 if (is_instance_method && method_object_name) {
554 var_sp = variable_list->FindVariable(method_object_name);
555 if (var_sp) {
556 separator_idx = 0;
557 var_expr_storage = "->";
558 var_expr_storage += var_expr;
559 var_expr = var_expr_storage;
560 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000561 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000563 }
564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565
Zachary Turner24bd3172016-11-17 01:37:52 +0000566 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
567 // Check if any anonymous unions are there which contain a variable with
568 // the name we need
569 for (size_t i = 0; i < variable_list->GetSize(); i++) {
570 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
571 if (!variable_sp)
572 continue;
573 if (!variable_sp->GetName().IsEmpty())
574 continue;
575
576 Type *var_type = variable_sp->GetType();
577 if (!var_type)
578 continue;
579
580 if (!var_type->GetForwardCompilerType().IsAnonymousType())
581 continue;
582 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
583 if (!valobj_sp)
584 return valobj_sp;
585 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
586 if (valobj_sp)
587 break;
588 }
589 }
590
591 if (var_sp && !valobj_sp) {
592 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
593 if (!valobj_sp)
594 return valobj_sp;
595 }
596 if (!valobj_sp) {
597 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
598 name_const_string.GetCString());
599 return ValueObjectSP();
600 }
601
602 // We are dumping at least one child
603 while (separator_idx != std::string::npos) {
604 // Calculate the next separator index ahead of time
605 ValueObjectSP child_valobj_sp;
606 const char separator_type = var_expr[0];
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000607 bool expr_is_ptr = false;
Zachary Turner24bd3172016-11-17 01:37:52 +0000608 switch (separator_type) {
609 case '-':
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000610 expr_is_ptr = true;
Zachary Turner24bd3172016-11-17 01:37:52 +0000611 if (var_expr.size() >= 2 && var_expr[1] != '>')
612 return ValueObjectSP();
613
614 if (no_fragile_ivar) {
615 // Make sure we aren't trying to deref an objective
616 // C ivar if this is not allowed
617 const uint32_t pointer_type_flags =
618 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
619 if ((pointer_type_flags & eTypeIsObjC) &&
620 (pointer_type_flags & eTypeIsPointer)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000621 // This was an objective C object pointer and it was requested we
622 // skip any fragile ivars so return nothing here
Zachary Turner24bd3172016-11-17 01:37:52 +0000623 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 }
625 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000626
627 // If we have a non pointer type with a sythetic value then lets check if
628 // we have an sythetic dereference specified.
629 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000630 Status deref_error;
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000631 if (valobj_sp->GetCompilerType().IsReferenceType()) {
632 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
633 if (error.Fail()) {
634 error.SetErrorStringWithFormatv(
635 "Failed to dereference reference type: %s", deref_error);
636 return ValueObjectSP();
637 }
638 }
639
640 valobj_sp = valobj_sp->Dereference(deref_error);
641 if (error.Fail()) {
642 error.SetErrorStringWithFormatv(
643 "Failed to dereference sythetic value: %s", deref_error);
644 return ValueObjectSP();
645 }
646 expr_is_ptr = false;
647 }
648
Zachary Turner24bd3172016-11-17 01:37:52 +0000649 var_expr = var_expr.drop_front(); // Remove the '-'
650 LLVM_FALLTHROUGH;
651 case '.': {
Zachary Turner24bd3172016-11-17 01:37:52 +0000652 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
653 separator_idx = var_expr.find_first_of(".-[");
654 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
655
656 if (check_ptr_vs_member) {
Adrian Prantl05097242018-04-30 16:49:04 +0000657 // We either have a pointer type and need to verify valobj_sp is a
658 // pointer, or we have a member of a class/union/struct being accessed
659 // with the . syntax and need to verify we don't have a pointer.
Zachary Turner24bd3172016-11-17 01:37:52 +0000660 const bool actual_is_ptr = valobj_sp->IsPointerType();
661
662 if (actual_is_ptr != expr_is_ptr) {
Adrian Prantl05097242018-04-30 16:49:04 +0000663 // Incorrect use of "." with a pointer, or "->" with a
664 // class/union/struct instance or reference.
Zachary Turner24bd3172016-11-17 01:37:52 +0000665 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
666 if (actual_is_ptr)
667 error.SetErrorStringWithFormat(
668 "\"%s\" is a pointer and . was used to attempt to access "
669 "\"%s\". Did you mean \"%s->%s\"?",
670 var_expr_path_strm.GetData(), child_name.GetCString(),
671 var_expr_path_strm.GetData(), var_expr.str().c_str());
672 else
673 error.SetErrorStringWithFormat(
674 "\"%s\" is not a pointer and -> was used to attempt to "
675 "access \"%s\". Did you mean \"%s.%s\"?",
676 var_expr_path_strm.GetData(), child_name.GetCString(),
677 var_expr_path_strm.GetData(), var_expr.str().c_str());
678 return ValueObjectSP();
679 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000681 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
682 if (!child_valobj_sp) {
683 if (!no_synth_child) {
684 child_valobj_sp = valobj_sp->GetSyntheticValue();
685 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000687 child_valobj_sp->GetChildMemberWithName(child_name, true);
688 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000689
Zachary Turner24bd3172016-11-17 01:37:52 +0000690 if (no_synth_child || !child_valobj_sp) {
691 // No child member with name "child_name"
692 if (synthetically_added_instance_object) {
693 // We added a "this->" or "self->" to the beginning of the
Adrian Prantl05097242018-04-30 16:49:04 +0000694 // expression and this is the first pointer ivar access, so just
695 // return the normal error
Zachary Turner24bd3172016-11-17 01:37:52 +0000696 error.SetErrorStringWithFormat(
697 "no variable or instance variable named '%s' found in "
698 "this frame",
699 name_const_string.GetCString());
700 } else {
701 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
702 if (child_name) {
703 error.SetErrorStringWithFormat(
704 "\"%s\" is not a member of \"(%s) %s\"",
705 child_name.GetCString(),
706 valobj_sp->GetTypeName().AsCString("<invalid type>"),
707 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 } else {
709 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000710 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000711 var_expr_path_strm.GetData(),
712 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000714 }
715 return ValueObjectSP();
716 }
717 }
718 synthetically_added_instance_object = false;
719 // Remove the child name from the path
720 var_expr = var_expr.drop_front(child_name.GetLength());
721 if (use_dynamic != eNoDynamicValues) {
722 ValueObjectSP dynamic_value_sp(
723 child_valobj_sp->GetDynamicValue(use_dynamic));
724 if (dynamic_value_sp)
725 child_valobj_sp = dynamic_value_sp;
726 }
727 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728
Zachary Turner24bd3172016-11-17 01:37:52 +0000729 case '[': {
Adrian Prantl05097242018-04-30 16:49:04 +0000730 // Array member access, or treating pointer as an array Need at least two
731 // brackets and a number
Zachary Turner24bd3172016-11-17 01:37:52 +0000732 if (var_expr.size() <= 2) {
733 error.SetErrorStringWithFormat(
734 "invalid square bracket encountered after \"%s\" in \"%s\"",
735 var_expr_path_strm.GetData(), var_expr.str().c_str());
736 return ValueObjectSP();
737 }
738
739 // Drop the open brace.
740 var_expr = var_expr.drop_front();
741 long child_index = 0;
742
743 // If there's no closing brace, this is an invalid expression.
744 size_t end_pos = var_expr.find_first_of(']');
745 if (end_pos == llvm::StringRef::npos) {
746 error.SetErrorStringWithFormat(
747 "missing closing square bracket in expression \"%s\"",
748 var_expr_path_strm.GetData());
749 return ValueObjectSP();
750 }
751 llvm::StringRef index_expr = var_expr.take_front(end_pos);
752 llvm::StringRef original_index_expr = index_expr;
753 // Drop all of "[index_expr]"
754 var_expr = var_expr.drop_front(end_pos + 1);
755
756 if (index_expr.consumeInteger(0, child_index)) {
757 // If there was no integer anywhere in the index expression, this is
758 // erroneous expression.
759 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
760 index_expr.str().c_str());
761 return ValueObjectSP();
762 }
763
764 if (index_expr.empty()) {
765 // The entire index expression was a single integer.
766
767 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
768 // what we have is *ptr[low]. the most similar C++ syntax is to deref
769 // ptr and extract bit low out of it. reading array item low would be
770 // done by saying ptr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000771 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000772 ValueObjectSP temp(valobj_sp->Dereference(error));
773 if (error.Fail()) {
774 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
775 error.SetErrorStringWithFormat(
776 "could not dereference \"(%s) %s\"",
777 valobj_sp->GetTypeName().AsCString("<invalid type>"),
778 var_expr_path_strm.GetData());
779 return ValueObjectSP();
780 }
781 valobj_sp = temp;
782 deref = false;
783 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
784 deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000785 // what we have is *arr[low]. the most similar C++ syntax is to get
786 // arr[0] (an operation that is equivalent to deref-ing arr) and
787 // extract bit low out of it. reading array item low would be done by
788 // saying arr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000789 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000790 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
791 if (error.Fail()) {
792 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
793 error.SetErrorStringWithFormat(
794 "could not get item 0 for \"(%s) %s\"",
795 valobj_sp->GetTypeName().AsCString("<invalid type>"),
796 var_expr_path_strm.GetData());
797 return ValueObjectSP();
798 }
799 valobj_sp = temp;
800 deref = false;
801 }
802
803 bool is_incomplete_array = false;
804 if (valobj_sp->IsPointerType()) {
805 bool is_objc_pointer = true;
806
807 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
808 eLanguageTypeObjC)
809 is_objc_pointer = false;
810 else if (!valobj_sp->GetCompilerType().IsPointerType())
811 is_objc_pointer = false;
812
813 if (no_synth_child && is_objc_pointer) {
814 error.SetErrorStringWithFormat(
815 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
816 "subscripted",
817 valobj_sp->GetTypeName().AsCString("<invalid type>"),
818 var_expr_path_strm.GetData());
819
820 return ValueObjectSP();
821 } else if (is_objc_pointer) {
Adrian Prantl05097242018-04-30 16:49:04 +0000822 // dereferencing ObjC variables is not valid.. so let's try and
823 // recur to synthetic children
Zachary Turner24bd3172016-11-17 01:37:52 +0000824 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
825 if (!synthetic /* no synthetic */
826 || synthetic == valobj_sp) /* synthetic is the same as
827 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 {
829 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
830 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000831 "\"(%s) %s\" is not an array type",
832 valobj_sp->GetTypeName().AsCString("<invalid type>"),
833 var_expr_path_strm.GetData());
834 } else if (
835 static_cast<uint32_t>(child_index) >=
836 synthetic
837 ->GetNumChildren() /* synthetic does not have that many values */) {
838 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
839 error.SetErrorStringWithFormat(
840 "array index %ld is not valid for \"(%s) %s\"", child_index,
841 valobj_sp->GetTypeName().AsCString("<invalid type>"),
842 var_expr_path_strm.GetData());
843 } else {
844 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
845 if (!child_valobj_sp) {
846 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
847 error.SetErrorStringWithFormat(
848 "array index %ld is not valid for \"(%s) %s\"", child_index,
849 valobj_sp->GetTypeName().AsCString("<invalid type>"),
850 var_expr_path_strm.GetData());
851 }
852 }
853 } else {
854 child_valobj_sp =
855 valobj_sp->GetSyntheticArrayMember(child_index, true);
856 if (!child_valobj_sp) {
857 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
858 error.SetErrorStringWithFormat(
859 "failed to use pointer as array for index %ld for "
860 "\"(%s) %s\"",
861 child_index,
862 valobj_sp->GetTypeName().AsCString("<invalid type>"),
863 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 }
865 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000866 } else if (valobj_sp->GetCompilerType().IsArrayType(
867 nullptr, nullptr, &is_incomplete_array)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000868 // Pass false to dynamic_value here so we can tell the difference
869 // between no dynamic value and no member of this type...
Zachary Turner24bd3172016-11-17 01:37:52 +0000870 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
871 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
872 child_valobj_sp =
873 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000874
Zachary Turner24bd3172016-11-17 01:37:52 +0000875 if (!child_valobj_sp) {
876 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
877 error.SetErrorStringWithFormat(
878 "array index %ld is not valid for \"(%s) %s\"", child_index,
879 valobj_sp->GetTypeName().AsCString("<invalid type>"),
880 var_expr_path_strm.GetData());
881 }
882 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
883 // this is a bitfield asking to display just one bit
884 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
885 child_index, child_index, true);
886 if (!child_valobj_sp) {
887 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
888 error.SetErrorStringWithFormat(
889 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
890 child_index, child_index,
891 valobj_sp->GetTypeName().AsCString("<invalid type>"),
892 var_expr_path_strm.GetData());
893 }
894 } else {
895 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
896 if (no_synth_child /* synthetic is forbidden */ ||
897 !synthetic /* no synthetic */
898 || synthetic == valobj_sp) /* synthetic is the same as the
899 original object */
900 {
901 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
902 error.SetErrorStringWithFormat(
903 "\"(%s) %s\" is not an array type",
904 valobj_sp->GetTypeName().AsCString("<invalid type>"),
905 var_expr_path_strm.GetData());
906 } else if (
907 static_cast<uint32_t>(child_index) >=
908 synthetic
909 ->GetNumChildren() /* synthetic does not have that many values */) {
910 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
911 error.SetErrorStringWithFormat(
912 "array index %ld is not valid for \"(%s) %s\"", child_index,
913 valobj_sp->GetTypeName().AsCString("<invalid type>"),
914 var_expr_path_strm.GetData());
915 } else {
916 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
917 if (!child_valobj_sp) {
918 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
919 error.SetErrorStringWithFormat(
920 "array index %ld is not valid for \"(%s) %s\"", child_index,
921 valobj_sp->GetTypeName().AsCString("<invalid type>"),
922 var_expr_path_strm.GetData());
923 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000924 }
925 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000926
927 if (!child_valobj_sp) {
928 // Invalid array index...
929 return ValueObjectSP();
930 }
931
932 separator_idx = var_expr.find_first_of(".-[");
933 if (use_dynamic != eNoDynamicValues) {
934 ValueObjectSP dynamic_value_sp(
935 child_valobj_sp->GetDynamicValue(use_dynamic));
936 if (dynamic_value_sp)
937 child_valobj_sp = dynamic_value_sp;
938 }
939 // Break out early from the switch since we were able to find the child
940 // member
941 break;
942 }
943
944 // this is most probably a BitField, let's take a look
945 if (index_expr.front() != '-') {
946 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
947 original_index_expr.str().c_str());
948 return ValueObjectSP();
949 }
950
Zachary Turner7edc3a62016-11-21 23:18:13 +0000951 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000952 long final_index = 0;
953 if (index_expr.getAsInteger(0, final_index)) {
954 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
955 original_index_expr.str().c_str());
956 return ValueObjectSP();
957 }
958
959 // if the format given is [high-low], swap range
960 if (child_index > final_index) {
961 long temp = child_index;
962 child_index = final_index;
963 final_index = temp;
964 }
965
966 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
967 // what we have is *ptr[low-high]. the most similar C++ syntax is to
968 // deref ptr and extract bits low thru high out of it. reading array
Adrian Prantl05097242018-04-30 16:49:04 +0000969 // items low thru high would be done by saying ptr[low-high], without a
970 // deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000971 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000972 ValueObjectSP temp(valobj_sp->Dereference(error));
973 if (error.Fail()) {
974 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
975 error.SetErrorStringWithFormat(
976 "could not dereference \"(%s) %s\"",
977 valobj_sp->GetTypeName().AsCString("<invalid type>"),
978 var_expr_path_strm.GetData());
979 return ValueObjectSP();
980 }
981 valobj_sp = temp;
982 deref = false;
983 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000984 // what we have is *arr[low-high]. the most similar C++ syntax is to
985 // get arr[0] (an operation that is equivalent to deref-ing arr) and
986 // extract bits low thru high out of it. reading array items low thru
987 // high would be done by saying arr[low-high], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000988 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000989 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
990 if (error.Fail()) {
991 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
992 error.SetErrorStringWithFormat(
993 "could not get item 0 for \"(%s) %s\"",
994 valobj_sp->GetTypeName().AsCString("<invalid type>"),
995 var_expr_path_strm.GetData());
996 return ValueObjectSP();
997 }
998 valobj_sp = temp;
999 deref = false;
1000 }
1001
1002 child_valobj_sp =
1003 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1004 if (!child_valobj_sp) {
1005 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +00001007 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1008 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1009 var_expr_path_strm.GetData());
1010 }
1011
1012 if (!child_valobj_sp) {
1013 // Invalid bitfield range...
1014 return ValueObjectSP();
1015 }
1016
1017 separator_idx = var_expr.find_first_of(".-[");
1018 if (use_dynamic != eNoDynamicValues) {
1019 ValueObjectSP dynamic_value_sp(
1020 child_valobj_sp->GetDynamicValue(use_dynamic));
1021 if (dynamic_value_sp)
1022 child_valobj_sp = dynamic_value_sp;
1023 }
1024 // Break out early from the switch since we were able to find the child
1025 // member
1026 break;
1027 }
1028 default:
1029 // Failure...
1030 {
1031 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1032 error.SetErrorStringWithFormat(
1033 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1034 separator_type, var_expr_path_strm.GetData(),
1035 var_expr.str().c_str());
1036
1037 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001039 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001040
1041 if (child_valobj_sp)
1042 valobj_sp = child_valobj_sp;
1043
1044 if (var_expr.empty())
1045 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001047 if (valobj_sp) {
1048 if (deref) {
1049 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1050 valobj_sp = deref_valobj_sp;
1051 } else if (address_of) {
1052 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1053 valobj_sp = address_of_valobj_sp;
1054 }
1055 }
1056 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057}
1058
Zachary Turner97206d52017-05-12 04:51:55 +00001059bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1061 if (!m_cfa_is_valid) {
1062 m_frame_base_error.SetErrorString(
1063 "No frame base available for this historical stack frame.");
1064 return false;
1065 }
1066
1067 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1068 if (m_sc.function) {
1069 m_frame_base.Clear();
1070 m_frame_base_error.Clear();
1071
1072 m_flags.Set(GOT_FRAME_BASE);
1073 ExecutionContext exe_ctx(shared_from_this());
1074 Value expr_value;
1075 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1076 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1077 loclist_base_addr =
1078 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1079 exe_ctx.GetTargetPtr());
1080
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001081 if (!m_sc.function->GetFrameBaseExpression().Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001082 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001083 expr_value, &m_frame_base_error)) {
Adrian Prantl05097242018-04-30 16:49:04 +00001084 // We should really have an error if evaluate returns, but in case we
1085 // don't, lets set the error to something at least.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001086 if (m_frame_base_error.Success())
1087 m_frame_base_error.SetErrorString(
1088 "Evaluation of the frame base expression failed.");
1089 } else {
1090 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1091 }
1092 } else {
1093 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 }
1096
1097 if (m_frame_base_error.Success())
1098 frame_base = m_frame_base;
1099
1100 if (error_ptr)
1101 *error_ptr = m_frame_base_error;
1102 return m_frame_base_error.Success();
1103}
1104
Zachary Turner97206d52017-05-12 04:51:55 +00001105DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001106 if (!m_sc.function) {
1107 if (error_ptr) {
1108 error_ptr->SetErrorString("No function in symbol context.");
1109 }
1110 return nullptr;
1111 }
1112
1113 return &m_sc.function->GetFrameBaseExpression();
1114}
1115
1116RegisterContextSP StackFrame::GetRegisterContext() {
1117 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1118 if (!m_reg_context_sp) {
1119 ThreadSP thread_sp(GetThread());
1120 if (thread_sp)
1121 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1122 }
1123 return m_reg_context_sp;
1124}
1125
1126bool StackFrame::HasDebugInformation() {
1127 GetSymbolContext(eSymbolContextLineEntry);
1128 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001129}
1130
1131ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1133 DynamicValueType use_dynamic) {
1134 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1135 ValueObjectSP valobj_sp;
Vedant Kumar4b36f792018-10-05 23:23:15 +00001136 if (IsHistorical()) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001137 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 }
1139 VariableList *var_list = GetVariableList(true);
1140 if (var_list) {
1141 // Make sure the variable is a frame variable
1142 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1143 const uint32_t num_variables = var_list->GetSize();
1144 if (var_idx < num_variables) {
1145 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1146 if (!valobj_sp) {
1147 if (m_variable_list_value_objects.GetSize() < num_variables)
1148 m_variable_list_value_objects.Resize(num_variables);
1149 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1150 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1151 }
Enrico Granata592afe72016-03-15 21:50:51 +00001152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001153 }
1154 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1155 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1156 if (dynamic_sp)
1157 return dynamic_sp;
1158 }
1159 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001160}
1161
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1163 DynamicValueType use_dynamic) {
1164 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Vedant Kumar4b36f792018-10-05 23:23:15 +00001165 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166 return ValueObjectSP();
1167
1168 // Check to make sure we aren't already tracking this variable?
1169 ValueObjectSP valobj_sp(
1170 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1171 if (!valobj_sp) {
1172 // We aren't already tracking this global
1173 VariableList *var_list = GetVariableList(true);
1174 // If this frame has no variables, create a new list
1175 if (var_list == nullptr)
1176 m_variable_list_sp.reset(new VariableList());
1177
1178 // Add the global/static variable to this frame
1179 m_variable_list_sp->AddVariable(variable_sp);
1180
1181 // Now make a value object for it so we can track its changes
1182 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1183 }
1184 return valobj_sp;
1185}
1186
1187bool StackFrame::IsInlined() {
1188 if (m_sc.block == nullptr)
1189 GetSymbolContext(eSymbolContextBlock);
1190 if (m_sc.block)
1191 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1192 return false;
1193}
1194
Vedant Kumar4b36f792018-10-05 23:23:15 +00001195bool StackFrame::IsHistorical() const {
1196 return m_stack_frame_kind == StackFrame::Kind::History;
1197}
1198
1199bool StackFrame::IsArtificial() const {
1200 return m_stack_frame_kind == StackFrame::Kind::Artificial;
1201}
1202
Kate Stoneb9c1b512016-09-06 20:57:50 +00001203lldb::LanguageType StackFrame::GetLanguage() {
1204 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1205 if (cu)
1206 return cu->GetLanguage();
1207 return lldb::eLanguageTypeUnknown;
1208}
1209
1210lldb::LanguageType StackFrame::GuessLanguage() {
1211 LanguageType lang_type = GetLanguage();
1212
1213 if (lang_type == eLanguageTypeUnknown) {
Jim Inghambdbdd222017-04-12 00:19:54 +00001214 SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1215 | eSymbolContextSymbol);
1216 if (sc.function) {
1217 lang_type = sc.function->GetMangled().GuessLanguage();
1218 }
1219 else if (sc.symbol)
1220 {
1221 lang_type = sc.symbol->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001222 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001223 }
1224
1225 return lang_type;
1226}
1227
1228namespace {
1229std::pair<const Instruction::Operand *, int64_t>
1230GetBaseExplainingValue(const Instruction::Operand &operand,
1231 RegisterContext &register_context, lldb::addr_t value) {
1232 switch (operand.m_type) {
1233 case Instruction::Operand::Type::Dereference:
1234 case Instruction::Operand::Type::Immediate:
1235 case Instruction::Operand::Type::Invalid:
1236 case Instruction::Operand::Type::Product:
1237 // These are not currently interesting
1238 return std::make_pair(nullptr, 0);
1239 case Instruction::Operand::Type::Sum: {
1240 const Instruction::Operand *immediate_child = nullptr;
1241 const Instruction::Operand *variable_child = nullptr;
1242 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1243 immediate_child = &operand.m_children[0];
1244 variable_child = &operand.m_children[1];
1245 } else if (operand.m_children[1].m_type ==
1246 Instruction::Operand::Type::Immediate) {
1247 immediate_child = &operand.m_children[1];
1248 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001249 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 if (!immediate_child) {
1251 return std::make_pair(nullptr, 0);
1252 }
1253 lldb::addr_t adjusted_value = value;
1254 if (immediate_child->m_negative) {
1255 adjusted_value += immediate_child->m_immediate;
1256 } else {
1257 adjusted_value -= immediate_child->m_immediate;
1258 }
1259 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1260 GetBaseExplainingValue(*variable_child, register_context,
1261 adjusted_value);
1262 if (!base_and_offset.first) {
1263 return std::make_pair(nullptr, 0);
1264 }
1265 if (immediate_child->m_negative) {
1266 base_and_offset.second -= immediate_child->m_immediate;
1267 } else {
1268 base_and_offset.second += immediate_child->m_immediate;
1269 }
1270 return base_and_offset;
1271 }
1272 case Instruction::Operand::Type::Register: {
1273 const RegisterInfo *info =
1274 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1275 if (!info) {
1276 return std::make_pair(nullptr, 0);
1277 }
1278 RegisterValue reg_value;
1279 if (!register_context.ReadRegister(info, reg_value)) {
1280 return std::make_pair(nullptr, 0);
1281 }
1282 if (reg_value.GetAsUInt64() == value) {
1283 return std::make_pair(&operand, 0);
1284 } else {
1285 return std::make_pair(nullptr, 0);
1286 }
1287 }
1288 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001289 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001290}
1291
1292std::pair<const Instruction::Operand *, int64_t>
1293GetBaseExplainingDereference(const Instruction::Operand &operand,
1294 RegisterContext &register_context,
1295 lldb::addr_t addr) {
1296 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1297 return GetBaseExplainingValue(operand.m_children[0], register_context,
1298 addr);
1299 }
1300 return std::make_pair(nullptr, 0);
1301}
Ilia K4f730dc2016-09-12 05:25:33 +00001302}
Sean Callanan4740a732016-09-06 04:48:36 +00001303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1305 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001306
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 const ArchSpec &target_arch = target_sp->GetArchitecture();
1308
1309 AddressRange pc_range;
1310 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1311 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1312
1313 ExecutionContext exe_ctx(shared_from_this());
1314
1315 const char *plugin_name = nullptr;
1316 const char *flavor = nullptr;
1317 const bool prefer_file_cache = false;
1318
1319 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1320 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1321
Jim Ingham99d1e282017-03-31 22:39:55 +00001322 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001323 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 }
Sean Callanan4740a732016-09-06 04:48:36 +00001325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 InstructionSP instruction_sp =
1327 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1328
1329 llvm::SmallVector<Instruction::Operand, 3> operands;
1330
1331 if (!instruction_sp->ParseOperands(operands)) {
1332 return ValueObjectSP();
1333 }
1334
1335 RegisterContextSP register_context_sp = GetRegisterContext();
1336
1337 if (!register_context_sp) {
1338 return ValueObjectSP();
1339 }
1340
1341 for (const Instruction::Operand &operand : operands) {
1342 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1343 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1344
1345 if (!base_and_offset.first) {
1346 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001347 }
Sean Callanan4740a732016-09-06 04:48:36 +00001348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349 switch (base_and_offset.first->m_type) {
1350 case Instruction::Operand::Type::Immediate: {
1351 lldb_private::Address addr;
1352 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1353 base_and_offset.second,
1354 addr)) {
1355 TypeSystem *c_type_system =
1356 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1357 if (!c_type_system) {
1358 return ValueObjectSP();
1359 } else {
1360 CompilerType void_ptr_type =
1361 c_type_system
1362 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1363 .GetPointerType();
1364 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001365 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001367 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 }
1369 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 case Instruction::Operand::Type::Register: {
1372 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1373 base_and_offset.second);
1374 }
1375 default:
1376 return ValueObjectSP();
1377 }
1378 }
1379
1380 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001381}
1382
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383namespace {
1384ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1385 int64_t offset) {
1386 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1387 return ValueObjectSP();
1388 }
Sean Callanan4740a732016-09-06 04:48:36 +00001389
Kate Stoneb9c1b512016-09-06 20:57:50 +00001390 if (parent->IsPointerOrReferenceType()) {
1391 return parent;
1392 }
1393
1394 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1395 const bool can_create = true;
1396 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1397
1398 if (!child_sp) {
1399 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001400 }
1401
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402 int64_t child_offset = child_sp->GetByteOffset();
1403 int64_t child_size = child_sp->GetByteSize();
1404
1405 if (offset >= child_offset && offset < (child_offset + child_size)) {
1406 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001407 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 }
Sean Callanan4740a732016-09-06 04:48:36 +00001409
Kate Stoneb9c1b512016-09-06 20:57:50 +00001410 if (offset == 0) {
1411 return parent;
1412 } else {
1413 return ValueObjectSP();
1414 }
1415}
1416
1417ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1418 ValueObjectSP &base,
1419 int64_t offset) {
1420 // base is a pointer to something
Adrian Prantl05097242018-04-30 16:49:04 +00001421 // offset is the thing to add to the pointer We return the most sensible
1422 // ValueObject for the result of *(base+offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423
1424 if (!base->IsPointerOrReferenceType()) {
1425 return ValueObjectSP();
1426 }
1427
Zachary Turner97206d52017-05-12 04:51:55 +00001428 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001430
1431 if (!pointee) {
1432 return ValueObjectSP();
1433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434
Ilia K4f730dc2016-09-12 05:25:33 +00001435 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 int64_t index = offset / pointee->GetByteSize();
1437 offset = offset % pointee->GetByteSize();
1438 const bool can_create = true;
1439 pointee = base->GetSyntheticArrayMember(index, can_create);
1440 }
1441
1442 if (!pointee || error.Fail()) {
1443 return ValueObjectSP();
1444 }
1445
1446 return GetValueForOffset(frame, pointee, offset);
1447}
1448
1449//------------------------------------------------------------------
1450/// Attempt to reconstruct the ValueObject for the address contained in a
1451/// given register plus an offset.
1452///
1453/// @params [in] frame
1454/// The current stack frame.
1455///
1456/// @params [in] reg
1457/// The register.
1458///
1459/// @params [in] offset
1460/// The offset from the register.
1461///
1462/// @param [in] disassembler
1463/// A disassembler containing instructions valid up to the current PC.
1464///
1465/// @param [in] variables
1466/// The variable list from the current frame,
1467///
1468/// @param [in] pc
1469/// The program counter for the instruction considered the 'user'.
1470///
1471/// @return
1472/// A string describing the base for the ExpressionPath. This could be a
1473/// variable, a register value, an argument, or a function return value.
1474/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1475//------------------------------------------------------------------
1476lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1477 int64_t offset, Disassembler &disassembler,
1478 VariableList &variables, const Address &pc) {
1479 // Example of operation for Intel:
1480 //
1481 // +14: movq -0x8(%rbp), %rdi
1482 // +18: movq 0x8(%rdi), %rdi
1483 // +22: addl 0x4(%rdi), %eax
1484 //
1485 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1486 //
Adrian Prantl05097242018-04-30 16:49:04 +00001487 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1488 // +18 that assigns to rdi, and calls itself recursively for that dereference
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1490 // +14 that assigns to rdi, and calls itself recursively for that
1491 // derefernece
1492 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1493 // variable list.
1494 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1495 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1496 // at +18)
1497 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1498 // rdi+4 at +22)
1499
1500 // First, check the variable list to see if anything is at the specified
1501 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001502
Sean Callanan50857102016-09-14 00:48:19 +00001503 using namespace OperandMatchers;
1504
Sean Callanan0ac172d2016-09-14 20:29:57 +00001505 const RegisterInfo *reg_info =
1506 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1507 if (!reg_info) {
1508 return ValueObjectSP();
1509 }
1510
Sean Callanan807ee2f2016-09-13 21:18:27 +00001511 Instruction::Operand op =
1512 offset ? Instruction::Operand::BuildDereference(
1513 Instruction::Operand::BuildSum(
1514 Instruction::Operand::BuildRegister(reg),
1515 Instruction::Operand::BuildImmediate(offset)))
1516 : Instruction::Operand::BuildDereference(
1517 Instruction::Operand::BuildRegister(reg));
1518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1520 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001521 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1522 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001523 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001524 }
1525
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 const uint32_t current_inst =
1527 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1528 if (current_inst == UINT32_MAX) {
1529 return ValueObjectSP();
1530 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1533 // This is not an exact algorithm, and it sacrifices accuracy for
Adrian Prantl05097242018-04-30 16:49:04 +00001534 // generality. Recognizing "mov" and "ld" instructions –– and which
1535 // are their source and destination operands -- is something the
1536 // disassembler should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 InstructionSP instruction_sp =
1538 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001539
Sean Callanan50857102016-09-14 00:48:19 +00001540 if (instruction_sp->IsCall()) {
1541 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1542 if (!abi_sp) {
1543 continue;
1544 }
1545
1546 const char *return_register_name;
1547 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1548 continue;
1549 }
1550
1551 const RegisterInfo *return_register_info =
1552 frame.GetRegisterContext()->GetRegisterInfoByName(
1553 return_register_name);
1554 if (!return_register_info) {
1555 continue;
1556 }
1557
1558 int64_t offset = 0;
1559
1560 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1561 MatchRegOp(*return_register_info))(op) &&
1562 !MatchUnaryOp(
1563 MatchOpType(Instruction::Operand::Type::Dereference),
1564 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1565 MatchRegOp(*return_register_info),
1566 FetchImmOp(offset)))(op)) {
1567 continue;
1568 }
1569
Kate Stoneb9c1b512016-09-06 20:57:50 +00001570 llvm::SmallVector<Instruction::Operand, 1> operands;
1571 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1572 continue;
1573 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574
Kate Stoneb9c1b512016-09-06 20:57:50 +00001575 switch (operands[0].m_type) {
1576 default:
1577 break;
1578 case Instruction::Operand::Type::Immediate: {
1579 SymbolContext sc;
1580 Address load_address;
1581 if (!frame.CalculateTarget()->ResolveLoadAddress(
1582 operands[0].m_immediate, load_address)) {
1583 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001584 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001585 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1586 load_address, eSymbolContextFunction, sc);
1587 if (!sc.function) {
1588 break;
1589 }
1590 CompilerType function_type = sc.function->GetCompilerType();
1591 if (!function_type.IsFunctionType()) {
1592 break;
1593 }
1594 CompilerType return_type = function_type.GetFunctionReturnType();
1595 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001596 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 return_value)) {
1598 break;
1599 }
1600 std::string name_str(
1601 sc.function->GetName().AsCString("<unknown function>"));
1602 name_str.append("()");
1603 Address return_value_address(return_value.GetAsUInt64());
1604 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001605 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1607 }
1608 }
1609
1610 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001611 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612
1613 llvm::SmallVector<Instruction::Operand, 2> operands;
1614 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1615 continue;
1616 }
1617
Kate Stoneb9c1b512016-09-06 20:57:50 +00001618 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001619 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1620 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1621 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001622
1623 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001625 }
1626 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001627 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001628 }
1629 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 continue;
1631 }
1632
1633 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001634 ValueObjectSP source_path;
1635 ConstString origin_register;
1636 int64_t origin_offset = 0;
1637
1638 if (FetchRegOp(origin_register)(*origin_operand)) {
1639 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1640 variables, instruction_sp->GetAddress());
1641 } else if (MatchUnaryOp(
1642 MatchOpType(Instruction::Operand::Type::Dereference),
1643 FetchRegOp(origin_register))(*origin_operand) ||
1644 MatchUnaryOp(
1645 MatchOpType(Instruction::Operand::Type::Dereference),
1646 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1647 FetchRegOp(origin_register),
1648 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001649 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001650 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001652 if (!source_path) {
1653 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001655 source_path =
1656 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 }
1658
1659 if (source_path) {
1660 return source_path;
1661 }
1662 }
1663
1664 return ValueObjectSP();
1665}
1666}
1667
1668lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1669 int64_t offset) {
1670 TargetSP target_sp = CalculateTarget();
1671
1672 const ArchSpec &target_arch = target_sp->GetArchitecture();
1673
1674 Block *frame_block = GetFrameBlock();
1675
1676 if (!frame_block) {
1677 return ValueObjectSP();
1678 }
1679
1680 Function *function = frame_block->CalculateSymbolContextFunction();
1681 if (!function) {
1682 return ValueObjectSP();
1683 }
1684
1685 AddressRange pc_range = function->GetAddressRange();
1686
1687 if (GetFrameCodeAddress().GetFileAddress() <
1688 pc_range.GetBaseAddress().GetFileAddress() ||
1689 GetFrameCodeAddress().GetFileAddress() -
1690 pc_range.GetBaseAddress().GetFileAddress() >=
1691 pc_range.GetByteSize()) {
1692 return ValueObjectSP();
1693 }
1694
1695 ExecutionContext exe_ctx(shared_from_this());
1696
1697 const char *plugin_name = nullptr;
1698 const char *flavor = nullptr;
1699 const bool prefer_file_cache = false;
1700 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1701 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1702
1703 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1704 return ValueObjectSP();
1705 }
1706
1707 const bool get_file_globals = false;
1708 VariableList *variables = GetVariableList(get_file_globals);
1709
1710 if (!variables) {
1711 return ValueObjectSP();
1712 }
1713
1714 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1715 GetFrameCodeAddress());
1716}
1717
Shafik Yaghmoure23d0b62018-09-20 17:06:34 +00001718lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1719 ValueObjectSP value_sp;
1720
1721 if (!name)
1722 return value_sp;
1723
1724 TargetSP target_sp = CalculateTarget();
1725 ProcessSP process_sp = CalculateProcess();
1726
1727 if (!target_sp && !process_sp)
1728 return value_sp;
1729
1730 VariableList variable_list;
1731 VariableSP var_sp;
1732 SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1733
1734 if (sc.block) {
1735 const bool can_create = true;
1736 const bool get_parent_variables = true;
1737 const bool stop_if_block_is_inlined_function = true;
1738
1739 if (sc.block->AppendVariables(
1740 can_create, get_parent_variables, stop_if_block_is_inlined_function,
1741 [this](Variable *v) { return v->IsInScope(this); },
1742 &variable_list)) {
1743 var_sp = variable_list.FindVariable(name);
1744 }
1745
1746 if (var_sp)
1747 value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1748 }
1749
1750 return value_sp;
1751}
1752
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753TargetSP StackFrame::CalculateTarget() {
1754 TargetSP target_sp;
1755 ThreadSP thread_sp(GetThread());
1756 if (thread_sp) {
1757 ProcessSP process_sp(thread_sp->CalculateProcess());
1758 if (process_sp)
1759 target_sp = process_sp->CalculateTarget();
1760 }
1761 return target_sp;
1762}
1763
1764ProcessSP StackFrame::CalculateProcess() {
1765 ProcessSP process_sp;
1766 ThreadSP thread_sp(GetThread());
1767 if (thread_sp)
1768 process_sp = thread_sp->CalculateProcess();
1769 return process_sp;
1770}
1771
1772ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1773
1774StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1775
1776void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1777 exe_ctx.SetContext(shared_from_this());
1778}
1779
Pavel Labath7f1c1212017-06-12 16:25:24 +00001780void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 const char *frame_marker) {
1782 if (strm == nullptr)
1783 return;
1784
1785 GetSymbolContext(eSymbolContextEverything);
1786 ExecutionContext exe_ctx(shared_from_this());
1787 StreamString s;
1788
1789 if (frame_marker)
1790 s.PutCString(frame_marker);
1791
1792 const FormatEntity::Entry *frame_format = nullptr;
1793 Target *target = exe_ctx.GetTargetPtr();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001794 if (target) {
1795 if (show_unique) {
1796 frame_format = target->GetDebugger().GetFrameFormatUnique();
1797 } else {
1798 frame_format = target->GetDebugger().GetFrameFormat();
1799 }
1800 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001801 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1802 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001803 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001804 } else {
1805 Dump(strm, true, false);
1806 strm->EOL();
1807 }
1808}
1809
1810void StackFrame::Dump(Stream *strm, bool show_frame_index,
1811 bool show_fullpaths) {
1812 if (strm == nullptr)
1813 return;
1814
1815 if (show_frame_index)
1816 strm->Printf("frame #%u: ", m_frame_index);
1817 ExecutionContext exe_ctx(shared_from_this());
1818 Target *target = exe_ctx.GetTargetPtr();
1819 strm->Printf("0x%0*" PRIx64 " ",
1820 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1821 : 16,
1822 GetFrameCodeAddress().GetLoadAddress(target));
1823 GetSymbolContext(eSymbolContextEverything);
1824 const bool show_module = true;
1825 const bool show_inline = true;
1826 const bool show_function_arguments = true;
1827 const bool show_function_name = true;
1828 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1829 GetFrameCodeAddress(), show_fullpaths, show_module,
1830 show_inline, show_function_arguments,
1831 show_function_name);
1832}
1833
1834void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1835 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1836 assert(GetStackID() ==
1837 prev_frame.GetStackID()); // TODO: remove this after some testing
1838 m_variable_list_sp = prev_frame.m_variable_list_sp;
1839 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001840 if (!m_disassembly.GetString().empty()) {
1841 m_disassembly.Clear();
1842 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1843 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844}
1845
1846void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1847 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1848 assert(GetStackID() ==
1849 curr_frame.GetStackID()); // TODO: remove this after some testing
1850 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1851 assert(GetThread() == curr_frame.GetThread());
1852 m_frame_index = curr_frame.m_frame_index;
1853 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1854 m_reg_context_sp = curr_frame.m_reg_context_sp;
1855 m_frame_code_addr = curr_frame.m_frame_code_addr;
1856 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1857 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1858 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1859 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1860 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1861 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1862 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1863 m_sc.function == curr_frame.m_sc.function);
1864 m_sc = curr_frame.m_sc;
1865 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1866 m_flags.Set(m_sc.GetResolvedMask());
1867 m_frame_base.Clear();
1868 m_frame_base_error.Clear();
1869}
1870
1871bool StackFrame::HasCachedData() const {
1872 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001873 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001874 if (m_variable_list_value_objects.GetSize() > 0)
1875 return true;
1876 if (!m_disassembly.GetString().empty())
1877 return true;
1878 return false;
1879}
1880
1881bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
Pavel Labath7f1c1212017-06-12 16:25:24 +00001882 bool show_unique, const char *frame_marker) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001883 if (show_frame_info) {
1884 strm.Indent();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001885 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886 }
1887
1888 if (show_source) {
1889 ExecutionContext exe_ctx(shared_from_this());
1890 bool have_source = false, have_debuginfo = false;
1891 Debugger::StopDisassemblyType disasm_display =
1892 Debugger::eStopDisassemblyTypeNever;
1893 Target *target = exe_ctx.GetTargetPtr();
1894 if (target) {
1895 Debugger &debugger = target->GetDebugger();
1896 const uint32_t source_lines_before =
1897 debugger.GetStopSourceLineCount(true);
1898 const uint32_t source_lines_after =
1899 debugger.GetStopSourceLineCount(false);
1900 disasm_display = debugger.GetStopDisassemblyDisplay();
1901
1902 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1903 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1904 have_debuginfo = true;
1905 if (source_lines_before > 0 || source_lines_after > 0) {
1906 size_t num_lines =
1907 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1908 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001909 m_sc.line_entry.column, source_lines_before,
1910 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911 if (num_lines != 0)
1912 have_source = true;
1913 // TODO: Give here a one time warning if source file is missing.
1914 }
1915 }
1916 switch (disasm_display) {
1917 case Debugger::eStopDisassemblyTypeNever:
1918 break;
1919
1920 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1921 if (have_debuginfo)
1922 break;
1923 LLVM_FALLTHROUGH;
1924
1925 case Debugger::eStopDisassemblyTypeNoSource:
1926 if (have_source)
1927 break;
1928 LLVM_FALLTHROUGH;
1929
1930 case Debugger::eStopDisassemblyTypeAlways:
1931 if (target) {
1932 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1933 if (disasm_lines > 0) {
1934 const ArchSpec &target_arch = target->GetArchitecture();
1935 AddressRange pc_range;
1936 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1937 pc_range.SetByteSize(disasm_lines *
1938 target_arch.GetMaximumOpcodeByteSize());
1939 const char *plugin_name = nullptr;
1940 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001941 const bool mixed_source_and_assembly = false;
1942 Disassembler::Disassemble(
1943 target->GetDebugger(), target_arch, plugin_name, flavor,
1944 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1945 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001946 }
1947 }
1948 break;
1949 }
1950 }
1951 }
1952 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001953}
Kuba Mracek41ae8e72018-10-31 04:00:22 +00001954
1955RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
1956 if (!m_recognized_frame_sp) {
1957 m_recognized_frame_sp =
1958 StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame());
1959 }
1960 return m_recognized_frame_sp;
1961}