blob: f8b22d96e16fbd4fbda3cf6dee7a390282d07f56 [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
Jonas Devlieghere796ac802019-02-11 23:13:08 +000036#include <memory>
37
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038using namespace lldb;
39using namespace lldb_private;
40
41// The first bits in the flags are reserved for the SymbolContext::Scope bits
42// so we know if we have tried to look up information in our internal symbol
43// context (m_sc) already.
Kate Stoneb9c1b512016-09-06 20:57:50 +000044#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
45#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
46#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
47#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
48#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
51 user_id_t unwind_frame_index, addr_t cfa,
Vedant Kumar4b36f792018-10-05 23:23:15 +000052 bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000053 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
55 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
56 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
57 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
Vedant Kumar4b36f792018-10-05 23:23:15 +000058 m_stack_frame_kind(kind), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +000059 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
60 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 // If we don't have a CFA value, use the frame index for our StackID so that
Adrian Prantl05097242018-04-30 16:49:04 +000062 // recursive functions properly aren't confused with one another on a history
63 // stack.
Vedant Kumar4b36f792018-10-05 23:23:15 +000064 if (IsHistorical() && !m_cfa_is_valid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 m_id.SetCFA(m_frame_index);
66 }
Jason Molenda99618472013-11-04 11:02:52 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 if (sc_ptr != nullptr) {
69 m_sc = *sc_ptr;
70 m_flags.Set(m_sc.GetResolvedMask());
71 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
75 user_id_t unwind_frame_index,
76 const RegisterContextSP &reg_context_sp, addr_t cfa,
77 addr_t pc, const SymbolContext *sc_ptr)
78 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000079 m_concrete_frame_index(unwind_frame_index),
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
81 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +000082 m_frame_base_error(), m_cfa_is_valid(true),
83 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +000084 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
85 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 if (sc_ptr != nullptr) {
87 m_sc = *sc_ptr;
88 m_flags.Set(m_sc.GetResolvedMask());
89 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (reg_context_sp && !m_sc.target_sp) {
92 m_sc.target_sp = reg_context_sp->CalculateTarget();
93 if (m_sc.target_sp)
94 m_flags.Set(eSymbolContextTarget);
95 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
99 user_id_t unwind_frame_index,
100 const RegisterContextSP &reg_context_sp, addr_t cfa,
101 const Address &pc_addr, const SymbolContext *sc_ptr)
102 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000103 m_concrete_frame_index(unwind_frame_index),
104 m_reg_context_sp(reg_context_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
106 nullptr),
107 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +0000108 m_frame_base_error(), m_cfa_is_valid(true),
109 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +0000110 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
111 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (sc_ptr != nullptr) {
113 m_sc = *sc_ptr;
114 m_flags.Set(m_sc.GetResolvedMask());
115 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 if (!m_sc.target_sp && reg_context_sp) {
118 m_sc.target_sp = reg_context_sp->CalculateTarget();
119 if (m_sc.target_sp)
120 m_flags.Set(eSymbolContextTarget);
121 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 ModuleSP pc_module_sp(pc_addr.GetModule());
124 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
125 if (pc_module_sp) {
126 m_sc.module_sp = pc_module_sp;
127 m_flags.Set(eSymbolContextModule);
128 } else {
129 m_sc.module_sp.reset();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000130 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000134StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136StackID &StackFrame::GetStackID() {
137 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Adrian Prantl05097242018-04-30 16:49:04 +0000138 // Make sure we have resolved the StackID object's symbol context scope if we
139 // already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
142 if (m_id.GetSymbolContextScope()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000143 // We already have a symbol context scope, we just don't have our flag
144 // bit set.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
146 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000147 // Calculate the frame block and use this for the stack ID symbol context
148 // scope if we have one.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 SymbolContextScope *scope = GetFrameBlock();
150 if (scope == nullptr) {
151 // We don't have a block, so use the symbol
152 if (m_flags.IsClear(eSymbolContextSymbol))
153 GetSymbolContext(eSymbolContextSymbol);
154
155 // It is ok if m_sc.symbol is nullptr here
156 scope = m_sc.symbol;
157 }
158 // Set the symbol context scope (the accessor will set the
159 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
160 SetSymbolContextScope(scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
163 return m_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164}
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166uint32_t StackFrame::GetFrameIndex() const {
167 ThreadSP thread_sp = GetThread();
168 if (thread_sp)
169 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
170 m_frame_index);
171 else
172 return m_frame_index;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000173}
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
176 std::lock_guard<std::recursive_mutex> guard(m_mutex);
177 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
178 m_id.SetSymbolContextScope(symbol_scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181const Address &StackFrame::GetFrameCodeAddress() {
182 std::lock_guard<std::recursive_mutex> guard(m_mutex);
183 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
184 !m_frame_code_addr.IsSectionOffset()) {
185 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 // Resolve the PC into a temporary address because if ResolveLoadAddress
188 // fails to resolve the address, it will clear the address object...
189 ThreadSP thread_sp(GetThread());
190 if (thread_sp) {
191 TargetSP target_sp(thread_sp->CalculateTarget());
192 if (target_sp) {
Pavel Labathc3c72122017-06-08 13:26:35 +0000193 const bool allow_section_end = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 if (m_frame_code_addr.SetOpcodeLoadAddress(
195 m_frame_code_addr.GetOffset(), target_sp.get(),
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000196 AddressClass::eCode, allow_section_end)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 ModuleSP module_sp(m_frame_code_addr.GetModule());
198 if (module_sp) {
199 m_sc.module_sp = module_sp;
200 m_flags.Set(eSymbolContextModule);
201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 }
206 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209bool StackFrame::ChangePC(addr_t pc) {
210 std::lock_guard<std::recursive_mutex> guard(m_mutex);
211 // We can't change the pc value of a history stack frame - it is immutable.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000212 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return false;
214 m_frame_code_addr.SetRawAddress(pc);
215 m_sc.Clear(false);
216 m_flags.Reset(0);
217 ThreadSP thread_sp(GetThread());
218 if (thread_sp)
219 thread_sp->ClearStackFrames();
220 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223const char *StackFrame::Disassemble() {
224 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner8b3f2162017-02-28 17:59:59 +0000225 if (m_disassembly.Empty()) {
226 ExecutionContext exe_ctx(shared_from_this());
227 Target *target = exe_ctx.GetTargetPtr();
228 if (target) {
229 const char *plugin_name = nullptr;
230 const char *flavor = nullptr;
231 Disassembler::Disassemble(target->GetDebugger(),
232 target->GetArchitecture(), plugin_name, flavor,
233 exe_ctx, 0, false, 0, 0, m_disassembly);
234 }
235 if (m_disassembly.Empty())
236 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
Zachary Turner8b3f2162017-02-28 17:59:59 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242Block *StackFrame::GetFrameBlock() {
243 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
244 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 if (m_sc.block) {
247 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
248 if (inline_block) {
Adrian Prantl05097242018-04-30 16:49:04 +0000249 // Use the block with the inlined function info as the frame block we
250 // want this frame to have only the variables for the inlined function
251 // and its non-inlined block child blocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252 return inline_block;
253 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000254 // This block is not contained within any inlined function blocks with so
255 // we want to use the top most function block.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 return &m_sc.function->GetBlock(false);
257 }
258 }
259 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000260}
261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262// Get the symbol context if we already haven't done so by resolving the
263// PC address as much as possible. This way when we pass around a
Adrian Prantl05097242018-04-30 16:49:04 +0000264// StackFrame object, everyone will have as much information as possible and no
265// one will ever have to look things up manually.
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;
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000425 m_variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 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(
Greg Clayton0d6f6812019-03-11 18:16:20 +0000643 "Failed to dereference sythetic value: {0}", deref_error);
644 return ValueObjectSP();
645 }
646 // Some synthetic plug-ins fail to set the error in Dereference
647 if (!valobj_sp) {
648 error.SetErrorString("Failed to dereference sythetic value");
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000649 return ValueObjectSP();
650 }
651 expr_is_ptr = false;
652 }
653
Zachary Turner24bd3172016-11-17 01:37:52 +0000654 var_expr = var_expr.drop_front(); // Remove the '-'
655 LLVM_FALLTHROUGH;
656 case '.': {
Zachary Turner24bd3172016-11-17 01:37:52 +0000657 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
658 separator_idx = var_expr.find_first_of(".-[");
659 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
660
661 if (check_ptr_vs_member) {
Adrian Prantl05097242018-04-30 16:49:04 +0000662 // We either have a pointer type and need to verify valobj_sp is a
663 // pointer, or we have a member of a class/union/struct being accessed
664 // with the . syntax and need to verify we don't have a pointer.
Zachary Turner24bd3172016-11-17 01:37:52 +0000665 const bool actual_is_ptr = valobj_sp->IsPointerType();
666
667 if (actual_is_ptr != expr_is_ptr) {
Adrian Prantl05097242018-04-30 16:49:04 +0000668 // Incorrect use of "." with a pointer, or "->" with a
669 // class/union/struct instance or reference.
Zachary Turner24bd3172016-11-17 01:37:52 +0000670 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
671 if (actual_is_ptr)
672 error.SetErrorStringWithFormat(
673 "\"%s\" is a pointer and . was used to attempt to access "
674 "\"%s\". Did you mean \"%s->%s\"?",
675 var_expr_path_strm.GetData(), child_name.GetCString(),
676 var_expr_path_strm.GetData(), var_expr.str().c_str());
677 else
678 error.SetErrorStringWithFormat(
679 "\"%s\" is not a pointer and -> was used to attempt to "
680 "access \"%s\". Did you mean \"%s.%s\"?",
681 var_expr_path_strm.GetData(), child_name.GetCString(),
682 var_expr_path_strm.GetData(), var_expr.str().c_str());
683 return ValueObjectSP();
684 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000686 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
687 if (!child_valobj_sp) {
688 if (!no_synth_child) {
689 child_valobj_sp = valobj_sp->GetSyntheticValue();
690 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000692 child_valobj_sp->GetChildMemberWithName(child_name, true);
693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694
Zachary Turner24bd3172016-11-17 01:37:52 +0000695 if (no_synth_child || !child_valobj_sp) {
696 // No child member with name "child_name"
697 if (synthetically_added_instance_object) {
698 // We added a "this->" or "self->" to the beginning of the
Adrian Prantl05097242018-04-30 16:49:04 +0000699 // expression and this is the first pointer ivar access, so just
700 // return the normal error
Zachary Turner24bd3172016-11-17 01:37:52 +0000701 error.SetErrorStringWithFormat(
702 "no variable or instance variable named '%s' found in "
703 "this frame",
704 name_const_string.GetCString());
705 } else {
706 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
707 if (child_name) {
708 error.SetErrorStringWithFormat(
709 "\"%s\" is not a member of \"(%s) %s\"",
710 child_name.GetCString(),
711 valobj_sp->GetTypeName().AsCString("<invalid type>"),
712 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 } else {
714 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000715 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000716 var_expr_path_strm.GetData(),
717 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000719 }
720 return ValueObjectSP();
721 }
722 }
723 synthetically_added_instance_object = false;
724 // Remove the child name from the path
725 var_expr = var_expr.drop_front(child_name.GetLength());
726 if (use_dynamic != eNoDynamicValues) {
727 ValueObjectSP dynamic_value_sp(
728 child_valobj_sp->GetDynamicValue(use_dynamic));
729 if (dynamic_value_sp)
730 child_valobj_sp = dynamic_value_sp;
731 }
732 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733
Zachary Turner24bd3172016-11-17 01:37:52 +0000734 case '[': {
Adrian Prantl05097242018-04-30 16:49:04 +0000735 // Array member access, or treating pointer as an array Need at least two
736 // brackets and a number
Zachary Turner24bd3172016-11-17 01:37:52 +0000737 if (var_expr.size() <= 2) {
738 error.SetErrorStringWithFormat(
739 "invalid square bracket encountered after \"%s\" in \"%s\"",
740 var_expr_path_strm.GetData(), var_expr.str().c_str());
741 return ValueObjectSP();
742 }
743
744 // Drop the open brace.
745 var_expr = var_expr.drop_front();
746 long child_index = 0;
747
748 // If there's no closing brace, this is an invalid expression.
749 size_t end_pos = var_expr.find_first_of(']');
750 if (end_pos == llvm::StringRef::npos) {
751 error.SetErrorStringWithFormat(
752 "missing closing square bracket in expression \"%s\"",
753 var_expr_path_strm.GetData());
754 return ValueObjectSP();
755 }
756 llvm::StringRef index_expr = var_expr.take_front(end_pos);
757 llvm::StringRef original_index_expr = index_expr;
758 // Drop all of "[index_expr]"
759 var_expr = var_expr.drop_front(end_pos + 1);
760
761 if (index_expr.consumeInteger(0, child_index)) {
762 // If there was no integer anywhere in the index expression, this is
763 // erroneous expression.
764 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
765 index_expr.str().c_str());
766 return ValueObjectSP();
767 }
768
769 if (index_expr.empty()) {
770 // The entire index expression was a single integer.
771
772 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
773 // what we have is *ptr[low]. the most similar C++ syntax is to deref
774 // ptr and extract bit low out of it. reading array item low would be
775 // done by saying ptr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000776 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000777 ValueObjectSP temp(valobj_sp->Dereference(error));
778 if (error.Fail()) {
779 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
780 error.SetErrorStringWithFormat(
781 "could not dereference \"(%s) %s\"",
782 valobj_sp->GetTypeName().AsCString("<invalid type>"),
783 var_expr_path_strm.GetData());
784 return ValueObjectSP();
785 }
786 valobj_sp = temp;
787 deref = false;
788 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
789 deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000790 // what we have is *arr[low]. the most similar C++ syntax is to get
791 // arr[0] (an operation that is equivalent to deref-ing arr) and
792 // extract bit low out of it. reading array item low would be done by
793 // saying arr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000794 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000795 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
796 if (error.Fail()) {
797 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
798 error.SetErrorStringWithFormat(
799 "could not get item 0 for \"(%s) %s\"",
800 valobj_sp->GetTypeName().AsCString("<invalid type>"),
801 var_expr_path_strm.GetData());
802 return ValueObjectSP();
803 }
804 valobj_sp = temp;
805 deref = false;
806 }
807
808 bool is_incomplete_array = false;
809 if (valobj_sp->IsPointerType()) {
810 bool is_objc_pointer = true;
811
812 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
813 eLanguageTypeObjC)
814 is_objc_pointer = false;
815 else if (!valobj_sp->GetCompilerType().IsPointerType())
816 is_objc_pointer = false;
817
818 if (no_synth_child && is_objc_pointer) {
819 error.SetErrorStringWithFormat(
820 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
821 "subscripted",
822 valobj_sp->GetTypeName().AsCString("<invalid type>"),
823 var_expr_path_strm.GetData());
824
825 return ValueObjectSP();
826 } else if (is_objc_pointer) {
Adrian Prantl05097242018-04-30 16:49:04 +0000827 // dereferencing ObjC variables is not valid.. so let's try and
828 // recur to synthetic children
Zachary Turner24bd3172016-11-17 01:37:52 +0000829 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
830 if (!synthetic /* no synthetic */
831 || synthetic == valobj_sp) /* synthetic is the same as
832 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 {
834 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
835 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000836 "\"(%s) %s\" is not an array type",
837 valobj_sp->GetTypeName().AsCString("<invalid type>"),
838 var_expr_path_strm.GetData());
839 } else if (
840 static_cast<uint32_t>(child_index) >=
841 synthetic
842 ->GetNumChildren() /* synthetic does not have that many values */) {
843 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
844 error.SetErrorStringWithFormat(
845 "array index %ld is not valid for \"(%s) %s\"", child_index,
846 valobj_sp->GetTypeName().AsCString("<invalid type>"),
847 var_expr_path_strm.GetData());
848 } else {
849 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
850 if (!child_valobj_sp) {
851 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
852 error.SetErrorStringWithFormat(
853 "array index %ld is not valid for \"(%s) %s\"", child_index,
854 valobj_sp->GetTypeName().AsCString("<invalid type>"),
855 var_expr_path_strm.GetData());
856 }
857 }
858 } else {
859 child_valobj_sp =
860 valobj_sp->GetSyntheticArrayMember(child_index, true);
861 if (!child_valobj_sp) {
862 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
863 error.SetErrorStringWithFormat(
864 "failed to use pointer as array for index %ld for "
865 "\"(%s) %s\"",
866 child_index,
867 valobj_sp->GetTypeName().AsCString("<invalid type>"),
868 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000869 }
870 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000871 } else if (valobj_sp->GetCompilerType().IsArrayType(
872 nullptr, nullptr, &is_incomplete_array)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000873 // Pass false to dynamic_value here so we can tell the difference
874 // between no dynamic value and no member of this type...
Zachary Turner24bd3172016-11-17 01:37:52 +0000875 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
876 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
877 child_valobj_sp =
878 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000879
Zachary Turner24bd3172016-11-17 01:37:52 +0000880 if (!child_valobj_sp) {
881 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
882 error.SetErrorStringWithFormat(
883 "array index %ld is not valid for \"(%s) %s\"", child_index,
884 valobj_sp->GetTypeName().AsCString("<invalid type>"),
885 var_expr_path_strm.GetData());
886 }
887 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
888 // this is a bitfield asking to display just one bit
889 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
890 child_index, child_index, true);
891 if (!child_valobj_sp) {
892 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
893 error.SetErrorStringWithFormat(
894 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
895 child_index, child_index,
896 valobj_sp->GetTypeName().AsCString("<invalid type>"),
897 var_expr_path_strm.GetData());
898 }
899 } else {
900 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
901 if (no_synth_child /* synthetic is forbidden */ ||
902 !synthetic /* no synthetic */
903 || synthetic == valobj_sp) /* synthetic is the same as the
904 original object */
905 {
906 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
907 error.SetErrorStringWithFormat(
908 "\"(%s) %s\" is not an array type",
909 valobj_sp->GetTypeName().AsCString("<invalid type>"),
910 var_expr_path_strm.GetData());
911 } else if (
912 static_cast<uint32_t>(child_index) >=
913 synthetic
914 ->GetNumChildren() /* synthetic does not have that many values */) {
915 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
916 error.SetErrorStringWithFormat(
917 "array index %ld is not valid for \"(%s) %s\"", child_index,
918 valobj_sp->GetTypeName().AsCString("<invalid type>"),
919 var_expr_path_strm.GetData());
920 } else {
921 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
922 if (!child_valobj_sp) {
923 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
924 error.SetErrorStringWithFormat(
925 "array index %ld is not valid for \"(%s) %s\"", child_index,
926 valobj_sp->GetTypeName().AsCString("<invalid type>"),
927 var_expr_path_strm.GetData());
928 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000929 }
930 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000931
932 if (!child_valobj_sp) {
933 // Invalid array index...
934 return ValueObjectSP();
935 }
936
937 separator_idx = var_expr.find_first_of(".-[");
938 if (use_dynamic != eNoDynamicValues) {
939 ValueObjectSP dynamic_value_sp(
940 child_valobj_sp->GetDynamicValue(use_dynamic));
941 if (dynamic_value_sp)
942 child_valobj_sp = dynamic_value_sp;
943 }
944 // Break out early from the switch since we were able to find the child
945 // member
946 break;
947 }
948
949 // this is most probably a BitField, let's take a look
950 if (index_expr.front() != '-') {
951 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
952 original_index_expr.str().c_str());
953 return ValueObjectSP();
954 }
955
Zachary Turner7edc3a62016-11-21 23:18:13 +0000956 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000957 long final_index = 0;
958 if (index_expr.getAsInteger(0, final_index)) {
959 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
960 original_index_expr.str().c_str());
961 return ValueObjectSP();
962 }
963
964 // if the format given is [high-low], swap range
965 if (child_index > final_index) {
966 long temp = child_index;
967 child_index = final_index;
968 final_index = temp;
969 }
970
971 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
972 // what we have is *ptr[low-high]. the most similar C++ syntax is to
973 // deref ptr and extract bits low thru high out of it. reading array
Adrian Prantl05097242018-04-30 16:49:04 +0000974 // items low thru high would be done by saying ptr[low-high], without a
975 // deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000976 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000977 ValueObjectSP temp(valobj_sp->Dereference(error));
978 if (error.Fail()) {
979 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
980 error.SetErrorStringWithFormat(
981 "could not dereference \"(%s) %s\"",
982 valobj_sp->GetTypeName().AsCString("<invalid type>"),
983 var_expr_path_strm.GetData());
984 return ValueObjectSP();
985 }
986 valobj_sp = temp;
987 deref = false;
988 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000989 // what we have is *arr[low-high]. the most similar C++ syntax is to
990 // get arr[0] (an operation that is equivalent to deref-ing arr) and
991 // extract bits low thru high out of it. reading array items low thru
992 // high would be done by saying arr[low-high], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000993 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000994 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
995 if (error.Fail()) {
996 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
997 error.SetErrorStringWithFormat(
998 "could not get item 0 for \"(%s) %s\"",
999 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1000 var_expr_path_strm.GetData());
1001 return ValueObjectSP();
1002 }
1003 valobj_sp = temp;
1004 deref = false;
1005 }
1006
1007 child_valobj_sp =
1008 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1009 if (!child_valobj_sp) {
1010 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +00001012 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1013 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1014 var_expr_path_strm.GetData());
1015 }
1016
1017 if (!child_valobj_sp) {
1018 // Invalid bitfield range...
1019 return ValueObjectSP();
1020 }
1021
1022 separator_idx = var_expr.find_first_of(".-[");
1023 if (use_dynamic != eNoDynamicValues) {
1024 ValueObjectSP dynamic_value_sp(
1025 child_valobj_sp->GetDynamicValue(use_dynamic));
1026 if (dynamic_value_sp)
1027 child_valobj_sp = dynamic_value_sp;
1028 }
1029 // Break out early from the switch since we were able to find the child
1030 // member
1031 break;
1032 }
1033 default:
1034 // Failure...
1035 {
1036 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1037 error.SetErrorStringWithFormat(
1038 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1039 separator_type, var_expr_path_strm.GetData(),
1040 var_expr.str().c_str());
1041
1042 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001044 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001045
1046 if (child_valobj_sp)
1047 valobj_sp = child_valobj_sp;
1048
1049 if (var_expr.empty())
1050 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001052 if (valobj_sp) {
1053 if (deref) {
1054 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1055 valobj_sp = deref_valobj_sp;
1056 } else if (address_of) {
1057 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1058 valobj_sp = address_of_valobj_sp;
1059 }
1060 }
1061 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062}
1063
Zachary Turner97206d52017-05-12 04:51:55 +00001064bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001065 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1066 if (!m_cfa_is_valid) {
1067 m_frame_base_error.SetErrorString(
1068 "No frame base available for this historical stack frame.");
1069 return false;
1070 }
1071
1072 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1073 if (m_sc.function) {
1074 m_frame_base.Clear();
1075 m_frame_base_error.Clear();
1076
1077 m_flags.Set(GOT_FRAME_BASE);
1078 ExecutionContext exe_ctx(shared_from_this());
1079 Value expr_value;
1080 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1081 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1082 loclist_base_addr =
1083 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1084 exe_ctx.GetTargetPtr());
1085
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001086 if (!m_sc.function->GetFrameBaseExpression().Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001087 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001088 expr_value, &m_frame_base_error)) {
Adrian Prantl05097242018-04-30 16:49:04 +00001089 // We should really have an error if evaluate returns, but in case we
1090 // don't, lets set the error to something at least.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001091 if (m_frame_base_error.Success())
1092 m_frame_base_error.SetErrorString(
1093 "Evaluation of the frame base expression failed.");
1094 } else {
1095 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1096 }
1097 } else {
1098 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001099 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001100 }
1101
1102 if (m_frame_base_error.Success())
1103 frame_base = m_frame_base;
1104
1105 if (error_ptr)
1106 *error_ptr = m_frame_base_error;
1107 return m_frame_base_error.Success();
1108}
1109
Zachary Turner97206d52017-05-12 04:51:55 +00001110DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 if (!m_sc.function) {
1112 if (error_ptr) {
1113 error_ptr->SetErrorString("No function in symbol context.");
1114 }
1115 return nullptr;
1116 }
1117
1118 return &m_sc.function->GetFrameBaseExpression();
1119}
1120
1121RegisterContextSP StackFrame::GetRegisterContext() {
1122 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1123 if (!m_reg_context_sp) {
1124 ThreadSP thread_sp(GetThread());
1125 if (thread_sp)
1126 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1127 }
1128 return m_reg_context_sp;
1129}
1130
1131bool StackFrame::HasDebugInformation() {
1132 GetSymbolContext(eSymbolContextLineEntry);
1133 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001134}
1135
1136ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1138 DynamicValueType use_dynamic) {
1139 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1140 ValueObjectSP valobj_sp;
Vedant Kumar4b36f792018-10-05 23:23:15 +00001141 if (IsHistorical()) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001142 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143 }
1144 VariableList *var_list = GetVariableList(true);
1145 if (var_list) {
1146 // Make sure the variable is a frame variable
1147 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1148 const uint32_t num_variables = var_list->GetSize();
1149 if (var_idx < num_variables) {
1150 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1151 if (!valobj_sp) {
1152 if (m_variable_list_value_objects.GetSize() < num_variables)
1153 m_variable_list_value_objects.Resize(num_variables);
1154 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1155 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1156 }
Enrico Granata592afe72016-03-15 21:50:51 +00001157 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158 }
1159 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1160 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1161 if (dynamic_sp)
1162 return dynamic_sp;
1163 }
1164 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001165}
1166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1168 DynamicValueType use_dynamic) {
1169 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Vedant Kumar4b36f792018-10-05 23:23:15 +00001170 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 return ValueObjectSP();
1172
1173 // Check to make sure we aren't already tracking this variable?
1174 ValueObjectSP valobj_sp(
1175 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1176 if (!valobj_sp) {
1177 // We aren't already tracking this global
1178 VariableList *var_list = GetVariableList(true);
1179 // If this frame has no variables, create a new list
1180 if (var_list == nullptr)
Jonas Devlieghere796ac802019-02-11 23:13:08 +00001181 m_variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182
1183 // Add the global/static variable to this frame
1184 m_variable_list_sp->AddVariable(variable_sp);
1185
1186 // Now make a value object for it so we can track its changes
1187 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1188 }
1189 return valobj_sp;
1190}
1191
1192bool StackFrame::IsInlined() {
1193 if (m_sc.block == nullptr)
1194 GetSymbolContext(eSymbolContextBlock);
1195 if (m_sc.block)
1196 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1197 return false;
1198}
1199
Vedant Kumar4b36f792018-10-05 23:23:15 +00001200bool StackFrame::IsHistorical() const {
1201 return m_stack_frame_kind == StackFrame::Kind::History;
1202}
1203
1204bool StackFrame::IsArtificial() const {
1205 return m_stack_frame_kind == StackFrame::Kind::Artificial;
1206}
1207
Kate Stoneb9c1b512016-09-06 20:57:50 +00001208lldb::LanguageType StackFrame::GetLanguage() {
1209 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1210 if (cu)
1211 return cu->GetLanguage();
1212 return lldb::eLanguageTypeUnknown;
1213}
1214
1215lldb::LanguageType StackFrame::GuessLanguage() {
1216 LanguageType lang_type = GetLanguage();
1217
1218 if (lang_type == eLanguageTypeUnknown) {
Jim Inghambdbdd222017-04-12 00:19:54 +00001219 SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1220 | eSymbolContextSymbol);
1221 if (sc.function) {
1222 lang_type = sc.function->GetMangled().GuessLanguage();
1223 }
1224 else if (sc.symbol)
1225 {
1226 lang_type = sc.symbol->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001227 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 }
1229
1230 return lang_type;
1231}
1232
1233namespace {
1234std::pair<const Instruction::Operand *, int64_t>
1235GetBaseExplainingValue(const Instruction::Operand &operand,
1236 RegisterContext &register_context, lldb::addr_t value) {
1237 switch (operand.m_type) {
1238 case Instruction::Operand::Type::Dereference:
1239 case Instruction::Operand::Type::Immediate:
1240 case Instruction::Operand::Type::Invalid:
1241 case Instruction::Operand::Type::Product:
1242 // These are not currently interesting
1243 return std::make_pair(nullptr, 0);
1244 case Instruction::Operand::Type::Sum: {
1245 const Instruction::Operand *immediate_child = nullptr;
1246 const Instruction::Operand *variable_child = nullptr;
1247 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1248 immediate_child = &operand.m_children[0];
1249 variable_child = &operand.m_children[1];
1250 } else if (operand.m_children[1].m_type ==
1251 Instruction::Operand::Type::Immediate) {
1252 immediate_child = &operand.m_children[1];
1253 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 if (!immediate_child) {
1256 return std::make_pair(nullptr, 0);
1257 }
1258 lldb::addr_t adjusted_value = value;
1259 if (immediate_child->m_negative) {
1260 adjusted_value += immediate_child->m_immediate;
1261 } else {
1262 adjusted_value -= immediate_child->m_immediate;
1263 }
1264 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1265 GetBaseExplainingValue(*variable_child, register_context,
1266 adjusted_value);
1267 if (!base_and_offset.first) {
1268 return std::make_pair(nullptr, 0);
1269 }
1270 if (immediate_child->m_negative) {
1271 base_and_offset.second -= immediate_child->m_immediate;
1272 } else {
1273 base_and_offset.second += immediate_child->m_immediate;
1274 }
1275 return base_and_offset;
1276 }
1277 case Instruction::Operand::Type::Register: {
1278 const RegisterInfo *info =
1279 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1280 if (!info) {
1281 return std::make_pair(nullptr, 0);
1282 }
1283 RegisterValue reg_value;
1284 if (!register_context.ReadRegister(info, reg_value)) {
1285 return std::make_pair(nullptr, 0);
1286 }
1287 if (reg_value.GetAsUInt64() == value) {
1288 return std::make_pair(&operand, 0);
1289 } else {
1290 return std::make_pair(nullptr, 0);
1291 }
1292 }
1293 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001294 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295}
1296
1297std::pair<const Instruction::Operand *, int64_t>
1298GetBaseExplainingDereference(const Instruction::Operand &operand,
1299 RegisterContext &register_context,
1300 lldb::addr_t addr) {
1301 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1302 return GetBaseExplainingValue(operand.m_children[0], register_context,
1303 addr);
1304 }
1305 return std::make_pair(nullptr, 0);
1306}
Ilia K4f730dc2016-09-12 05:25:33 +00001307}
Sean Callanan4740a732016-09-06 04:48:36 +00001308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1310 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001311
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312 const ArchSpec &target_arch = target_sp->GetArchitecture();
1313
1314 AddressRange pc_range;
1315 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1316 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1317
1318 ExecutionContext exe_ctx(shared_from_this());
1319
1320 const char *plugin_name = nullptr;
1321 const char *flavor = nullptr;
1322 const bool prefer_file_cache = false;
1323
1324 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1325 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1326
Jim Ingham99d1e282017-03-31 22:39:55 +00001327 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001328 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 }
Sean Callanan4740a732016-09-06 04:48:36 +00001330
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331 InstructionSP instruction_sp =
1332 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1333
1334 llvm::SmallVector<Instruction::Operand, 3> operands;
1335
1336 if (!instruction_sp->ParseOperands(operands)) {
1337 return ValueObjectSP();
1338 }
1339
1340 RegisterContextSP register_context_sp = GetRegisterContext();
1341
1342 if (!register_context_sp) {
1343 return ValueObjectSP();
1344 }
1345
1346 for (const Instruction::Operand &operand : operands) {
1347 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1348 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1349
1350 if (!base_and_offset.first) {
1351 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001352 }
Sean Callanan4740a732016-09-06 04:48:36 +00001353
Kate Stoneb9c1b512016-09-06 20:57:50 +00001354 switch (base_and_offset.first->m_type) {
1355 case Instruction::Operand::Type::Immediate: {
1356 lldb_private::Address addr;
1357 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1358 base_and_offset.second,
1359 addr)) {
1360 TypeSystem *c_type_system =
1361 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1362 if (!c_type_system) {
1363 return ValueObjectSP();
1364 } else {
1365 CompilerType void_ptr_type =
1366 c_type_system
1367 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1368 .GetPointerType();
1369 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001370 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001372 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 }
1374 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001375 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 case Instruction::Operand::Type::Register: {
1377 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1378 base_and_offset.second);
1379 }
1380 default:
1381 return ValueObjectSP();
1382 }
1383 }
1384
1385 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001386}
1387
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388namespace {
1389ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1390 int64_t offset) {
1391 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1392 return ValueObjectSP();
1393 }
Sean Callanan4740a732016-09-06 04:48:36 +00001394
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 if (parent->IsPointerOrReferenceType()) {
1396 return parent;
1397 }
1398
1399 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1400 const bool can_create = true;
1401 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1402
1403 if (!child_sp) {
1404 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001405 }
1406
Kate Stoneb9c1b512016-09-06 20:57:50 +00001407 int64_t child_offset = child_sp->GetByteOffset();
1408 int64_t child_size = child_sp->GetByteSize();
1409
1410 if (offset >= child_offset && offset < (child_offset + child_size)) {
1411 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001412 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 }
Sean Callanan4740a732016-09-06 04:48:36 +00001414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415 if (offset == 0) {
1416 return parent;
1417 } else {
1418 return ValueObjectSP();
1419 }
1420}
1421
1422ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1423 ValueObjectSP &base,
1424 int64_t offset) {
1425 // base is a pointer to something
Adrian Prantl05097242018-04-30 16:49:04 +00001426 // offset is the thing to add to the pointer We return the most sensible
1427 // ValueObject for the result of *(base+offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428
1429 if (!base->IsPointerOrReferenceType()) {
1430 return ValueObjectSP();
1431 }
1432
Zachary Turner97206d52017-05-12 04:51:55 +00001433 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001435
1436 if (!pointee) {
1437 return ValueObjectSP();
1438 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439
Ilia K4f730dc2016-09-12 05:25:33 +00001440 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001441 int64_t index = offset / pointee->GetByteSize();
1442 offset = offset % pointee->GetByteSize();
1443 const bool can_create = true;
1444 pointee = base->GetSyntheticArrayMember(index, can_create);
1445 }
1446
1447 if (!pointee || error.Fail()) {
1448 return ValueObjectSP();
1449 }
1450
1451 return GetValueForOffset(frame, pointee, offset);
1452}
1453
Kate Stoneb9c1b512016-09-06 20:57:50 +00001454/// Attempt to reconstruct the ValueObject for the address contained in a
1455/// given register plus an offset.
1456///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001457/// \params [in] frame
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458/// The current stack frame.
1459///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001460/// \params [in] reg
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461/// The register.
1462///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001463/// \params [in] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464/// The offset from the register.
1465///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001466/// \param [in] disassembler
Kate Stoneb9c1b512016-09-06 20:57:50 +00001467/// A disassembler containing instructions valid up to the current PC.
1468///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001469/// \param [in] variables
Kate Stoneb9c1b512016-09-06 20:57:50 +00001470/// The variable list from the current frame,
1471///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001472/// \param [in] pc
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473/// The program counter for the instruction considered the 'user'.
1474///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001475/// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +00001476/// A string describing the base for the ExpressionPath. This could be a
1477/// variable, a register value, an argument, or a function return value.
1478/// The ValueObject if found. If valid, it has a valid ExpressionPath.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001479lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1480 int64_t offset, Disassembler &disassembler,
1481 VariableList &variables, const Address &pc) {
1482 // Example of operation for Intel:
1483 //
1484 // +14: movq -0x8(%rbp), %rdi
1485 // +18: movq 0x8(%rdi), %rdi
1486 // +22: addl 0x4(%rdi), %eax
1487 //
1488 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1489 //
Adrian Prantl05097242018-04-30 16:49:04 +00001490 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1491 // +18 that assigns to rdi, and calls itself recursively for that dereference
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1493 // +14 that assigns to rdi, and calls itself recursively for that
1494 // derefernece
1495 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1496 // variable list.
1497 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1498 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1499 // at +18)
1500 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1501 // rdi+4 at +22)
1502
1503 // First, check the variable list to see if anything is at the specified
1504 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001505
Sean Callanan50857102016-09-14 00:48:19 +00001506 using namespace OperandMatchers;
1507
Sean Callanan0ac172d2016-09-14 20:29:57 +00001508 const RegisterInfo *reg_info =
1509 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1510 if (!reg_info) {
1511 return ValueObjectSP();
1512 }
1513
Sean Callanan807ee2f2016-09-13 21:18:27 +00001514 Instruction::Operand op =
1515 offset ? Instruction::Operand::BuildDereference(
1516 Instruction::Operand::BuildSum(
1517 Instruction::Operand::BuildRegister(reg),
1518 Instruction::Operand::BuildImmediate(offset)))
1519 : Instruction::Operand::BuildDereference(
1520 Instruction::Operand::BuildRegister(reg));
1521
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1523 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001524 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1525 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001526 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001527 }
1528
Kate Stoneb9c1b512016-09-06 20:57:50 +00001529 const uint32_t current_inst =
1530 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1531 if (current_inst == UINT32_MAX) {
1532 return ValueObjectSP();
1533 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001534
Kate Stoneb9c1b512016-09-06 20:57:50 +00001535 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1536 // This is not an exact algorithm, and it sacrifices accuracy for
Adrian Prantl05097242018-04-30 16:49:04 +00001537 // generality. Recognizing "mov" and "ld" instructions –– and which
1538 // are their source and destination operands -- is something the
1539 // disassembler should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 InstructionSP instruction_sp =
1541 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001542
Sean Callanan50857102016-09-14 00:48:19 +00001543 if (instruction_sp->IsCall()) {
1544 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1545 if (!abi_sp) {
1546 continue;
1547 }
1548
1549 const char *return_register_name;
1550 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1551 continue;
1552 }
1553
1554 const RegisterInfo *return_register_info =
1555 frame.GetRegisterContext()->GetRegisterInfoByName(
1556 return_register_name);
1557 if (!return_register_info) {
1558 continue;
1559 }
1560
1561 int64_t offset = 0;
1562
1563 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1564 MatchRegOp(*return_register_info))(op) &&
1565 !MatchUnaryOp(
1566 MatchOpType(Instruction::Operand::Type::Dereference),
1567 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1568 MatchRegOp(*return_register_info),
1569 FetchImmOp(offset)))(op)) {
1570 continue;
1571 }
1572
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 llvm::SmallVector<Instruction::Operand, 1> operands;
1574 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1575 continue;
1576 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 switch (operands[0].m_type) {
1579 default:
1580 break;
1581 case Instruction::Operand::Type::Immediate: {
1582 SymbolContext sc;
1583 Address load_address;
1584 if (!frame.CalculateTarget()->ResolveLoadAddress(
1585 operands[0].m_immediate, load_address)) {
1586 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001587 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1589 load_address, eSymbolContextFunction, sc);
1590 if (!sc.function) {
1591 break;
1592 }
1593 CompilerType function_type = sc.function->GetCompilerType();
1594 if (!function_type.IsFunctionType()) {
1595 break;
1596 }
1597 CompilerType return_type = function_type.GetFunctionReturnType();
1598 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001599 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 return_value)) {
1601 break;
1602 }
1603 std::string name_str(
1604 sc.function->GetName().AsCString("<unknown function>"));
1605 name_str.append("()");
1606 Address return_value_address(return_value.GetAsUInt64());
1607 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001608 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1610 }
1611 }
1612
1613 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001614 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615
1616 llvm::SmallVector<Instruction::Operand, 2> operands;
1617 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1618 continue;
1619 }
1620
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001622 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1623 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1624 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001625
1626 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001627 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001628 }
1629 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001630 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001631 }
1632 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001633 continue;
1634 }
1635
1636 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001637 ValueObjectSP source_path;
1638 ConstString origin_register;
1639 int64_t origin_offset = 0;
1640
1641 if (FetchRegOp(origin_register)(*origin_operand)) {
1642 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1643 variables, instruction_sp->GetAddress());
1644 } else if (MatchUnaryOp(
1645 MatchOpType(Instruction::Operand::Type::Dereference),
1646 FetchRegOp(origin_register))(*origin_operand) ||
1647 MatchUnaryOp(
1648 MatchOpType(Instruction::Operand::Type::Dereference),
1649 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1650 FetchRegOp(origin_register),
1651 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001653 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001655 if (!source_path) {
1656 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001658 source_path =
1659 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 }
1661
1662 if (source_path) {
1663 return source_path;
1664 }
1665 }
1666
1667 return ValueObjectSP();
1668}
1669}
1670
1671lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1672 int64_t offset) {
1673 TargetSP target_sp = CalculateTarget();
1674
1675 const ArchSpec &target_arch = target_sp->GetArchitecture();
1676
1677 Block *frame_block = GetFrameBlock();
1678
1679 if (!frame_block) {
1680 return ValueObjectSP();
1681 }
1682
1683 Function *function = frame_block->CalculateSymbolContextFunction();
1684 if (!function) {
1685 return ValueObjectSP();
1686 }
1687
1688 AddressRange pc_range = function->GetAddressRange();
1689
1690 if (GetFrameCodeAddress().GetFileAddress() <
1691 pc_range.GetBaseAddress().GetFileAddress() ||
1692 GetFrameCodeAddress().GetFileAddress() -
1693 pc_range.GetBaseAddress().GetFileAddress() >=
1694 pc_range.GetByteSize()) {
1695 return ValueObjectSP();
1696 }
1697
1698 ExecutionContext exe_ctx(shared_from_this());
1699
1700 const char *plugin_name = nullptr;
1701 const char *flavor = nullptr;
1702 const bool prefer_file_cache = false;
1703 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1704 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1705
1706 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1707 return ValueObjectSP();
1708 }
1709
1710 const bool get_file_globals = false;
1711 VariableList *variables = GetVariableList(get_file_globals);
1712
1713 if (!variables) {
1714 return ValueObjectSP();
1715 }
1716
1717 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1718 GetFrameCodeAddress());
1719}
1720
Shafik Yaghmoure23d0b62018-09-20 17:06:34 +00001721lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1722 ValueObjectSP value_sp;
1723
1724 if (!name)
1725 return value_sp;
1726
1727 TargetSP target_sp = CalculateTarget();
1728 ProcessSP process_sp = CalculateProcess();
1729
1730 if (!target_sp && !process_sp)
1731 return value_sp;
1732
1733 VariableList variable_list;
1734 VariableSP var_sp;
1735 SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1736
1737 if (sc.block) {
1738 const bool can_create = true;
1739 const bool get_parent_variables = true;
1740 const bool stop_if_block_is_inlined_function = true;
1741
1742 if (sc.block->AppendVariables(
1743 can_create, get_parent_variables, stop_if_block_is_inlined_function,
1744 [this](Variable *v) { return v->IsInScope(this); },
1745 &variable_list)) {
1746 var_sp = variable_list.FindVariable(name);
1747 }
1748
1749 if (var_sp)
1750 value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1751 }
1752
1753 return value_sp;
1754}
1755
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756TargetSP StackFrame::CalculateTarget() {
1757 TargetSP target_sp;
1758 ThreadSP thread_sp(GetThread());
1759 if (thread_sp) {
1760 ProcessSP process_sp(thread_sp->CalculateProcess());
1761 if (process_sp)
1762 target_sp = process_sp->CalculateTarget();
1763 }
1764 return target_sp;
1765}
1766
1767ProcessSP StackFrame::CalculateProcess() {
1768 ProcessSP process_sp;
1769 ThreadSP thread_sp(GetThread());
1770 if (thread_sp)
1771 process_sp = thread_sp->CalculateProcess();
1772 return process_sp;
1773}
1774
1775ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1776
1777StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1778
1779void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1780 exe_ctx.SetContext(shared_from_this());
1781}
1782
Pavel Labath7f1c1212017-06-12 16:25:24 +00001783void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001784 const char *frame_marker) {
1785 if (strm == nullptr)
1786 return;
1787
1788 GetSymbolContext(eSymbolContextEverything);
1789 ExecutionContext exe_ctx(shared_from_this());
1790 StreamString s;
1791
1792 if (frame_marker)
1793 s.PutCString(frame_marker);
1794
1795 const FormatEntity::Entry *frame_format = nullptr;
1796 Target *target = exe_ctx.GetTargetPtr();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001797 if (target) {
1798 if (show_unique) {
1799 frame_format = target->GetDebugger().GetFrameFormatUnique();
1800 } else {
1801 frame_format = target->GetDebugger().GetFrameFormat();
1802 }
1803 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001804 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1805 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001806 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001807 } else {
1808 Dump(strm, true, false);
1809 strm->EOL();
1810 }
1811}
1812
1813void StackFrame::Dump(Stream *strm, bool show_frame_index,
1814 bool show_fullpaths) {
1815 if (strm == nullptr)
1816 return;
1817
1818 if (show_frame_index)
1819 strm->Printf("frame #%u: ", m_frame_index);
1820 ExecutionContext exe_ctx(shared_from_this());
1821 Target *target = exe_ctx.GetTargetPtr();
1822 strm->Printf("0x%0*" PRIx64 " ",
1823 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1824 : 16,
1825 GetFrameCodeAddress().GetLoadAddress(target));
1826 GetSymbolContext(eSymbolContextEverything);
1827 const bool show_module = true;
1828 const bool show_inline = true;
1829 const bool show_function_arguments = true;
1830 const bool show_function_name = true;
1831 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1832 GetFrameCodeAddress(), show_fullpaths, show_module,
1833 show_inline, show_function_arguments,
1834 show_function_name);
1835}
1836
1837void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1838 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1839 assert(GetStackID() ==
1840 prev_frame.GetStackID()); // TODO: remove this after some testing
1841 m_variable_list_sp = prev_frame.m_variable_list_sp;
1842 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001843 if (!m_disassembly.GetString().empty()) {
1844 m_disassembly.Clear();
1845 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001847}
1848
1849void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1850 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1851 assert(GetStackID() ==
1852 curr_frame.GetStackID()); // TODO: remove this after some testing
1853 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1854 assert(GetThread() == curr_frame.GetThread());
1855 m_frame_index = curr_frame.m_frame_index;
1856 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1857 m_reg_context_sp = curr_frame.m_reg_context_sp;
1858 m_frame_code_addr = curr_frame.m_frame_code_addr;
1859 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1860 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1861 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1862 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1863 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1864 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1865 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1866 m_sc.function == curr_frame.m_sc.function);
1867 m_sc = curr_frame.m_sc;
1868 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1869 m_flags.Set(m_sc.GetResolvedMask());
1870 m_frame_base.Clear();
1871 m_frame_base_error.Clear();
1872}
1873
1874bool StackFrame::HasCachedData() const {
1875 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001876 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001877 if (m_variable_list_value_objects.GetSize() > 0)
1878 return true;
1879 if (!m_disassembly.GetString().empty())
1880 return true;
1881 return false;
1882}
1883
1884bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
Pavel Labath7f1c1212017-06-12 16:25:24 +00001885 bool show_unique, const char *frame_marker) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001886 if (show_frame_info) {
1887 strm.Indent();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001888 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001889 }
1890
1891 if (show_source) {
1892 ExecutionContext exe_ctx(shared_from_this());
1893 bool have_source = false, have_debuginfo = false;
1894 Debugger::StopDisassemblyType disasm_display =
1895 Debugger::eStopDisassemblyTypeNever;
1896 Target *target = exe_ctx.GetTargetPtr();
1897 if (target) {
1898 Debugger &debugger = target->GetDebugger();
1899 const uint32_t source_lines_before =
1900 debugger.GetStopSourceLineCount(true);
1901 const uint32_t source_lines_after =
1902 debugger.GetStopSourceLineCount(false);
1903 disasm_display = debugger.GetStopDisassemblyDisplay();
1904
1905 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1906 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1907 have_debuginfo = true;
1908 if (source_lines_before > 0 || source_lines_after > 0) {
1909 size_t num_lines =
1910 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1911 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001912 m_sc.line_entry.column, source_lines_before,
1913 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001914 if (num_lines != 0)
1915 have_source = true;
1916 // TODO: Give here a one time warning if source file is missing.
1917 }
1918 }
1919 switch (disasm_display) {
1920 case Debugger::eStopDisassemblyTypeNever:
1921 break;
1922
1923 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1924 if (have_debuginfo)
1925 break;
1926 LLVM_FALLTHROUGH;
1927
1928 case Debugger::eStopDisassemblyTypeNoSource:
1929 if (have_source)
1930 break;
1931 LLVM_FALLTHROUGH;
1932
1933 case Debugger::eStopDisassemblyTypeAlways:
1934 if (target) {
1935 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1936 if (disasm_lines > 0) {
1937 const ArchSpec &target_arch = target->GetArchitecture();
1938 AddressRange pc_range;
1939 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1940 pc_range.SetByteSize(disasm_lines *
1941 target_arch.GetMaximumOpcodeByteSize());
1942 const char *plugin_name = nullptr;
1943 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001944 const bool mixed_source_and_assembly = false;
1945 Disassembler::Disassemble(
1946 target->GetDebugger(), target_arch, plugin_name, flavor,
1947 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1948 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 }
1950 }
1951 break;
1952 }
1953 }
1954 }
1955 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001956}
Kuba Mracek41ae8e72018-10-31 04:00:22 +00001957
1958RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
1959 if (!m_recognized_frame_sp) {
1960 m_recognized_frame_sp =
1961 StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame());
1962 }
1963 return m_recognized_frame_sp;
1964}