blob: 1bc8165d76b0d91c757bdf073a58a63f7c6f1430 [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"
Alex Langford0e252e32019-07-30 22:12:34 +000032#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000033#include "lldb/Utility/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
Zachary Turner991e4452018-10-25 20:45:19 +000035#include "lldb/lldb-enumerations.h"
36
Jonas Devlieghere796ac802019-02-11 23:13:08 +000037#include <memory>
38
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039using namespace lldb;
40using namespace lldb_private;
41
42// The first bits in the flags are reserved for the SymbolContext::Scope bits
43// so we know if we have tried to look up information in our internal symbol
44// context (m_sc) already.
Kate Stoneb9c1b512016-09-06 20:57:50 +000045#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
46#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
47#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
48#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
49#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
52 user_id_t unwind_frame_index, addr_t cfa,
Vedant Kumar4b36f792018-10-05 23:23:15 +000053 bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000054 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
56 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
57 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
58 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
Vedant Kumar4b36f792018-10-05 23:23:15 +000059 m_stack_frame_kind(kind), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +000060 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
61 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 // If we don't have a CFA value, use the frame index for our StackID so that
Adrian Prantl05097242018-04-30 16:49:04 +000063 // recursive functions properly aren't confused with one another on a history
64 // stack.
Vedant Kumar4b36f792018-10-05 23:23:15 +000065 if (IsHistorical() && !m_cfa_is_valid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 m_id.SetCFA(m_frame_index);
67 }
Jason Molenda99618472013-11-04 11:02:52 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 if (sc_ptr != nullptr) {
70 m_sc = *sc_ptr;
71 m_flags.Set(m_sc.GetResolvedMask());
72 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
76 user_id_t unwind_frame_index,
77 const RegisterContextSP &reg_context_sp, addr_t cfa,
78 addr_t pc, const SymbolContext *sc_ptr)
79 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000080 m_concrete_frame_index(unwind_frame_index),
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
82 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +000083 m_frame_base_error(), m_cfa_is_valid(true),
84 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +000085 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
86 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 if (sc_ptr != nullptr) {
88 m_sc = *sc_ptr;
89 m_flags.Set(m_sc.GetResolvedMask());
90 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 if (reg_context_sp && !m_sc.target_sp) {
93 m_sc.target_sp = reg_context_sp->CalculateTarget();
94 if (m_sc.target_sp)
95 m_flags.Set(eSymbolContextTarget);
96 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
100 user_id_t unwind_frame_index,
101 const RegisterContextSP &reg_context_sp, addr_t cfa,
102 const Address &pc_addr, const SymbolContext *sc_ptr)
103 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000104 m_concrete_frame_index(unwind_frame_index),
105 m_reg_context_sp(reg_context_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
107 nullptr),
108 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +0000109 m_frame_base_error(), m_cfa_is_valid(true),
110 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
Kuba Mracek41ae8e72018-10-31 04:00:22 +0000111 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
112 m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 if (sc_ptr != nullptr) {
114 m_sc = *sc_ptr;
115 m_flags.Set(m_sc.GetResolvedMask());
116 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (!m_sc.target_sp && reg_context_sp) {
119 m_sc.target_sp = reg_context_sp->CalculateTarget();
120 if (m_sc.target_sp)
121 m_flags.Set(eSymbolContextTarget);
122 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 ModuleSP pc_module_sp(pc_addr.GetModule());
125 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
126 if (pc_module_sp) {
127 m_sc.module_sp = pc_module_sp;
128 m_flags.Set(eSymbolContextModule);
129 } else {
130 m_sc.module_sp.reset();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133}
134
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000135StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137StackID &StackFrame::GetStackID() {
138 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Adrian Prantl05097242018-04-30 16:49:04 +0000139 // Make sure we have resolved the StackID object's symbol context scope if we
140 // already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
143 if (m_id.GetSymbolContextScope()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000144 // We already have a symbol context scope, we just don't have our flag
145 // bit set.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
147 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000148 // Calculate the frame block and use this for the stack ID symbol context
149 // scope if we have one.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 SymbolContextScope *scope = GetFrameBlock();
151 if (scope == nullptr) {
152 // We don't have a block, so use the symbol
153 if (m_flags.IsClear(eSymbolContextSymbol))
154 GetSymbolContext(eSymbolContextSymbol);
155
156 // It is ok if m_sc.symbol is nullptr here
157 scope = m_sc.symbol;
158 }
159 // Set the symbol context scope (the accessor will set the
160 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
161 SetSymbolContextScope(scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164 return m_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167uint32_t StackFrame::GetFrameIndex() const {
168 ThreadSP thread_sp = GetThread();
169 if (thread_sp)
170 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
171 m_frame_index);
172 else
173 return m_frame_index;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000174}
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
177 std::lock_guard<std::recursive_mutex> guard(m_mutex);
178 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
179 m_id.SetSymbolContextScope(symbol_scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182const Address &StackFrame::GetFrameCodeAddress() {
183 std::lock_guard<std::recursive_mutex> guard(m_mutex);
184 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
185 !m_frame_code_addr.IsSectionOffset()) {
186 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 // Resolve the PC into a temporary address because if ResolveLoadAddress
189 // fails to resolve the address, it will clear the address object...
190 ThreadSP thread_sp(GetThread());
191 if (thread_sp) {
192 TargetSP target_sp(thread_sp->CalculateTarget());
193 if (target_sp) {
Pavel Labathc3c72122017-06-08 13:26:35 +0000194 const bool allow_section_end = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 if (m_frame_code_addr.SetOpcodeLoadAddress(
196 m_frame_code_addr.GetOffset(), target_sp.get(),
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000197 AddressClass::eCode, allow_section_end)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 ModuleSP module_sp(m_frame_code_addr.GetModule());
199 if (module_sp) {
200 m_sc.module_sp = module_sp;
201 m_flags.Set(eSymbolContextModule);
202 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 }
207 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208}
209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210bool StackFrame::ChangePC(addr_t pc) {
211 std::lock_guard<std::recursive_mutex> guard(m_mutex);
212 // We can't change the pc value of a history stack frame - it is immutable.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000213 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 return false;
215 m_frame_code_addr.SetRawAddress(pc);
216 m_sc.Clear(false);
217 m_flags.Reset(0);
218 ThreadSP thread_sp(GetThread());
219 if (thread_sp)
220 thread_sp->ClearStackFrames();
221 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222}
223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224const char *StackFrame::Disassemble() {
225 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner8b3f2162017-02-28 17:59:59 +0000226 if (m_disassembly.Empty()) {
227 ExecutionContext exe_ctx(shared_from_this());
228 Target *target = exe_ctx.GetTargetPtr();
229 if (target) {
230 const char *plugin_name = nullptr;
231 const char *flavor = nullptr;
232 Disassembler::Disassemble(target->GetDebugger(),
233 target->GetArchitecture(), plugin_name, flavor,
234 exe_ctx, 0, false, 0, 0, m_disassembly);
235 }
236 if (m_disassembly.Empty())
237 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 }
Zachary Turner8b3f2162017-02-28 17:59:59 +0000239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241}
242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243Block *StackFrame::GetFrameBlock() {
244 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
245 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 if (m_sc.block) {
248 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
249 if (inline_block) {
Adrian Prantl05097242018-04-30 16:49:04 +0000250 // Use the block with the inlined function info as the frame block we
251 // want this frame to have only the variables for the inlined function
252 // and its non-inlined block child blocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 return inline_block;
254 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000255 // This block is not contained within any inlined function blocks with so
256 // we want to use the top most function block.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 return &m_sc.function->GetBlock(false);
258 }
259 }
260 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000261}
262
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263// Get the symbol context if we already haven't done so by resolving the
264// PC address as much as possible. This way when we pass around a
Adrian Prantl05097242018-04-30 16:49:04 +0000265// StackFrame object, everyone will have as much information as possible and no
266// one will ever have to look things up manually.
Zachary Turner991e4452018-10-25 20:45:19 +0000267const SymbolContext &
268StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 std::lock_guard<std::recursive_mutex> guard(m_mutex);
270 // Copy our internal symbol context into "sc".
271 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
272 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 // If the target was requested add that:
275 if (!m_sc.target_sp) {
276 m_sc.target_sp = CalculateTarget();
277 if (m_sc.target_sp)
278 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 }
280
Adrian Prantl05097242018-04-30 16:49:04 +0000281 // Resolve our PC to section offset if we haven't already done so and if we
282 // don't have a module. The resolved address section will contain the
283 // module to which it belongs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
285 GetFrameCodeAddress();
286
Adrian Prantl05097242018-04-30 16:49:04 +0000287 // If this is not frame zero, then we need to subtract 1 from the PC value
288 // when doing address lookups since the PC will be on the instruction
289 // following the function call instruction...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290
291 Address lookup_addr(GetFrameCodeAddress());
292 if (m_frame_index > 0 && lookup_addr.IsValid()) {
293 addr_t offset = lookup_addr.GetOffset();
294 if (offset > 0) {
295 lookup_addr.SetOffset(offset - 1);
296
297 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000298 // lookup_addr is the start of a section. We need do the math on the
299 // actual load address and re-compute the section. We're working with
300 // a 'noreturn' function at the end of a section.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000301 ThreadSP thread_sp(GetThread());
302 if (thread_sp) {
303 TargetSP target_sp(thread_sp->CalculateTarget());
304 if (target_sp) {
305 addr_t addr_minus_one =
306 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
307 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
308 } else {
309 lookup_addr.SetOffset(offset - 1);
310 }
311 }
312 }
313 }
314
315 if (m_sc.module_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000316 // We have something in our stack frame symbol context, lets check if we
317 // haven't already tried to lookup one of those things. If we haven't
318 // then we will do the query.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319
Zachary Turner991e4452018-10-25 20:45:19 +0000320 SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321
322 if (resolve_scope & eSymbolContextCompUnit) {
323 if (m_flags.IsClear(eSymbolContextCompUnit)) {
324 if (m_sc.comp_unit)
325 resolved |= eSymbolContextCompUnit;
326 else
327 actual_resolve_scope |= eSymbolContextCompUnit;
328 }
329 }
330
331 if (resolve_scope & eSymbolContextFunction) {
332 if (m_flags.IsClear(eSymbolContextFunction)) {
333 if (m_sc.function)
334 resolved |= eSymbolContextFunction;
335 else
336 actual_resolve_scope |= eSymbolContextFunction;
337 }
338 }
339
340 if (resolve_scope & eSymbolContextBlock) {
341 if (m_flags.IsClear(eSymbolContextBlock)) {
342 if (m_sc.block)
343 resolved |= eSymbolContextBlock;
344 else
345 actual_resolve_scope |= eSymbolContextBlock;
346 }
347 }
348
349 if (resolve_scope & eSymbolContextSymbol) {
350 if (m_flags.IsClear(eSymbolContextSymbol)) {
351 if (m_sc.symbol)
352 resolved |= eSymbolContextSymbol;
353 else
354 actual_resolve_scope |= eSymbolContextSymbol;
355 }
356 }
357
358 if (resolve_scope & eSymbolContextLineEntry) {
359 if (m_flags.IsClear(eSymbolContextLineEntry)) {
360 if (m_sc.line_entry.IsValid())
361 resolved |= eSymbolContextLineEntry;
362 else
363 actual_resolve_scope |= eSymbolContextLineEntry;
364 }
365 }
366
367 if (actual_resolve_scope) {
Adrian Prantl05097242018-04-30 16:49:04 +0000368 // We might be resolving less information than what is already in our
369 // current symbol context so resolve into a temporary symbol context
370 // "sc" so we don't clear out data we have already found in "m_sc"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 SymbolContext sc;
372 // Set flags that indicate what we have tried to resolve
373 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
374 lookup_addr, actual_resolve_scope, sc);
Adrian Prantl05097242018-04-30 16:49:04 +0000375 // Only replace what we didn't already have as we may have information
376 // for an inlined function scope that won't match what a standard
377 // lookup by address would match
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
379 m_sc.comp_unit = sc.comp_unit;
380 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
381 m_sc.function = sc.function;
382 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
383 m_sc.block = sc.block;
384 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
385 m_sc.symbol = sc.symbol;
386 if ((resolved & eSymbolContextLineEntry) &&
387 !m_sc.line_entry.IsValid()) {
388 m_sc.line_entry = sc.line_entry;
389 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
390 }
391 }
392 } else {
393 // If we don't have a module, then we can't have the compile unit,
394 // function, block, line entry or symbol, so we can safely call
395 // ResolveSymbolContextForAddress with our symbol context member m_sc.
396 if (m_sc.target_sp) {
397 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
398 lookup_addr, resolve_scope, m_sc);
399 }
400 }
401
402 // Update our internal flags so we remember what we have tried to locate so
403 // we don't have to keep trying when more calls to this function are made.
Adrian Prantl05097242018-04-30 16:49:04 +0000404 // We might have dug up more information that was requested (for example if
405 // we were asked to only get the block, we will have gotten the compile
406 // unit, and function) so set any additional bits that we resolved
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 m_flags.Set(resolve_scope | resolved);
408 }
409
410 // Return the symbol context with everything that was possible to resolve
411 // resolved.
412 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413}
414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415VariableList *StackFrame::GetVariableList(bool get_file_globals) {
416 std::lock_guard<std::recursive_mutex> guard(m_mutex);
417 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
418 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 Block *frame_block = GetFrameBlock();
421
422 if (frame_block) {
423 const bool get_child_variables = true;
424 const bool can_create = true;
425 const bool stop_if_child_block_is_inlined_function = true;
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000426 m_variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 frame_block->AppendBlockVariables(can_create, get_child_variables,
428 stop_if_child_block_is_inlined_function,
Zachary Turner3bc714b2017-03-02 00:05:25 +0000429 [](Variable *v) { return true; },
Kate Stoneb9c1b512016-09-06 20:57:50 +0000430 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000431 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 }
433
434 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
435 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
436
437 if (m_flags.IsClear(eSymbolContextCompUnit))
438 GetSymbolContext(eSymbolContextCompUnit);
439
440 if (m_sc.comp_unit) {
441 VariableListSP global_variable_list_sp(
442 m_sc.comp_unit->GetVariableList(true));
443 if (m_variable_list_sp)
444 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
445 else
446 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 }
449
450 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451}
452
Greg Claytond41f0322011-08-02 23:35:43 +0000453VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454StackFrame::GetInScopeVariableList(bool get_file_globals,
455 bool must_have_valid_location) {
456 std::lock_guard<std::recursive_mutex> guard(m_mutex);
457 // We can't fetch variable information for a history stack frame.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000458 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 VariableListSP var_list_sp(new VariableList);
462 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 if (m_sc.block) {
465 const bool can_create = true;
466 const bool get_parent_variables = true;
467 const bool stop_if_block_is_inlined_function = true;
468 m_sc.block->AppendVariables(
469 can_create, get_parent_variables, stop_if_block_is_inlined_function,
470 [this, must_have_valid_location](Variable *v) {
471 return v->IsInScope(this) && (!must_have_valid_location ||
472 v->LocationIsValidForFrame(this));
473 },
474 var_list_sp.get());
475 }
476
477 if (m_sc.comp_unit && get_file_globals) {
478 VariableListSP global_variable_list_sp(
479 m_sc.comp_unit->GetVariableList(true));
480 if (global_variable_list_sp)
481 var_list_sp->AddVariables(global_variable_list_sp.get());
482 }
483
484 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner0eb31a12016-11-17 05:14:32 +0000488 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
Zachary Turner97206d52017-05-12 04:51:55 +0000489 VariableSP &var_sp, Status &error) {
Zachary Turner0eb31a12016-11-17 05:14:32 +0000490 llvm::StringRef original_var_expr = var_expr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000491 // We can't fetch variable information for a history stack frame.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000492 if (IsHistorical())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000493 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494
Zachary Turner0eb31a12016-11-17 05:14:32 +0000495 if (var_expr.empty()) {
496 error.SetErrorStringWithFormat("invalid variable path '%s'",
497 var_expr.str().c_str());
Zachary Turner24bd3172016-11-17 01:37:52 +0000498 return ValueObjectSP();
499 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500
Zachary Turner24bd3172016-11-17 01:37:52 +0000501 const bool check_ptr_vs_member =
502 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
503 const bool no_fragile_ivar =
504 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
505 const bool no_synth_child =
506 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
507 // const bool no_synth_array = (options &
508 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
509 error.Clear();
510 bool deref = false;
511 bool address_of = false;
512 ValueObjectSP valobj_sp;
513 const bool get_file_globals = true;
514 // When looking up a variable for an expression, we need only consider the
515 // variables that are in scope.
516 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
517 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518
Zachary Turner24bd3172016-11-17 01:37:52 +0000519 if (!variable_list)
520 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000521
Zachary Turner24bd3172016-11-17 01:37:52 +0000522 // If first character is a '*', then show pointer contents
Zachary Turner24bd3172016-11-17 01:37:52 +0000523 std::string var_expr_storage;
524 if (var_expr[0] == '*') {
525 deref = true;
526 var_expr = var_expr.drop_front(); // Skip the '*'
527 } else if (var_expr[0] == '&') {
528 address_of = true;
529 var_expr = var_expr.drop_front(); // Skip the '&'
530 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531
Zachary Turner24bd3172016-11-17 01:37:52 +0000532 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
533 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534
Zachary Turner24bd3172016-11-17 01:37:52 +0000535 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536
Zachary Turner24bd3172016-11-17 01:37:52 +0000537 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538
Zachary Turner24bd3172016-11-17 01:37:52 +0000539 bool synthetically_added_instance_object = false;
540
541 if (var_sp) {
542 var_expr = var_expr.drop_front(name_const_string.GetLength());
543 }
544
545 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000546 // Check for direct ivars access which helps us with implicit access to
547 // ivars with the "this->" or "self->"
Zachary Turner24bd3172016-11-17 01:37:52 +0000548 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
549 lldb::LanguageType method_language = eLanguageTypeUnknown;
550 bool is_instance_method = false;
551 ConstString method_object_name;
552 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
553 method_object_name)) {
554 if (is_instance_method && method_object_name) {
555 var_sp = variable_list->FindVariable(method_object_name);
556 if (var_sp) {
557 separator_idx = 0;
558 var_expr_storage = "->";
559 var_expr_storage += var_expr;
560 var_expr = var_expr_storage;
561 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000562 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000563 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000564 }
565 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566
Zachary Turner24bd3172016-11-17 01:37:52 +0000567 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
568 // Check if any anonymous unions are there which contain a variable with
569 // the name we need
570 for (size_t i = 0; i < variable_list->GetSize(); i++) {
571 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
572 if (!variable_sp)
573 continue;
574 if (!variable_sp->GetName().IsEmpty())
575 continue;
576
577 Type *var_type = variable_sp->GetType();
578 if (!var_type)
579 continue;
580
581 if (!var_type->GetForwardCompilerType().IsAnonymousType())
582 continue;
583 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
584 if (!valobj_sp)
585 return valobj_sp;
586 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
587 if (valobj_sp)
588 break;
589 }
590 }
591
592 if (var_sp && !valobj_sp) {
593 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
594 if (!valobj_sp)
595 return valobj_sp;
596 }
597 if (!valobj_sp) {
598 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
599 name_const_string.GetCString());
600 return ValueObjectSP();
601 }
602
603 // We are dumping at least one child
604 while (separator_idx != std::string::npos) {
605 // Calculate the next separator index ahead of time
606 ValueObjectSP child_valobj_sp;
607 const char separator_type = var_expr[0];
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000608 bool expr_is_ptr = false;
Zachary Turner24bd3172016-11-17 01:37:52 +0000609 switch (separator_type) {
610 case '-':
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000611 expr_is_ptr = true;
Zachary Turner24bd3172016-11-17 01:37:52 +0000612 if (var_expr.size() >= 2 && var_expr[1] != '>')
613 return ValueObjectSP();
614
615 if (no_fragile_ivar) {
616 // Make sure we aren't trying to deref an objective
617 // C ivar if this is not allowed
618 const uint32_t pointer_type_flags =
619 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
620 if ((pointer_type_flags & eTypeIsObjC) &&
621 (pointer_type_flags & eTypeIsPointer)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000622 // This was an objective C object pointer and it was requested we
623 // skip any fragile ivars so return nothing here
Zachary Turner24bd3172016-11-17 01:37:52 +0000624 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 }
626 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000627
628 // If we have a non pointer type with a sythetic value then lets check if
629 // we have an sythetic dereference specified.
630 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000631 Status deref_error;
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000632 if (valobj_sp->GetCompilerType().IsReferenceType()) {
633 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
634 if (error.Fail()) {
635 error.SetErrorStringWithFormatv(
636 "Failed to dereference reference type: %s", deref_error);
637 return ValueObjectSP();
638 }
639 }
640
641 valobj_sp = valobj_sp->Dereference(deref_error);
642 if (error.Fail()) {
643 error.SetErrorStringWithFormatv(
Greg Clayton0d6f6812019-03-11 18:16:20 +0000644 "Failed to dereference sythetic value: {0}", deref_error);
645 return ValueObjectSP();
646 }
647 // Some synthetic plug-ins fail to set the error in Dereference
648 if (!valobj_sp) {
649 error.SetErrorString("Failed to dereference sythetic value");
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000650 return ValueObjectSP();
651 }
652 expr_is_ptr = false;
653 }
654
Zachary Turner24bd3172016-11-17 01:37:52 +0000655 var_expr = var_expr.drop_front(); // Remove the '-'
656 LLVM_FALLTHROUGH;
657 case '.': {
Zachary Turner24bd3172016-11-17 01:37:52 +0000658 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
659 separator_idx = var_expr.find_first_of(".-[");
660 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
661
662 if (check_ptr_vs_member) {
Adrian Prantl05097242018-04-30 16:49:04 +0000663 // We either have a pointer type and need to verify valobj_sp is a
664 // pointer, or we have a member of a class/union/struct being accessed
665 // with the . syntax and need to verify we don't have a pointer.
Zachary Turner24bd3172016-11-17 01:37:52 +0000666 const bool actual_is_ptr = valobj_sp->IsPointerType();
667
668 if (actual_is_ptr != expr_is_ptr) {
Adrian Prantl05097242018-04-30 16:49:04 +0000669 // Incorrect use of "." with a pointer, or "->" with a
670 // class/union/struct instance or reference.
Zachary Turner24bd3172016-11-17 01:37:52 +0000671 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
672 if (actual_is_ptr)
673 error.SetErrorStringWithFormat(
674 "\"%s\" is a pointer and . was used to attempt to access "
675 "\"%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 else
679 error.SetErrorStringWithFormat(
680 "\"%s\" is not a pointer and -> was used to attempt to "
681 "access \"%s\". Did you mean \"%s.%s\"?",
682 var_expr_path_strm.GetData(), child_name.GetCString(),
683 var_expr_path_strm.GetData(), var_expr.str().c_str());
684 return ValueObjectSP();
685 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000686 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000687 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
688 if (!child_valobj_sp) {
689 if (!no_synth_child) {
690 child_valobj_sp = valobj_sp->GetSyntheticValue();
691 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000693 child_valobj_sp->GetChildMemberWithName(child_name, true);
694 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695
Zachary Turner24bd3172016-11-17 01:37:52 +0000696 if (no_synth_child || !child_valobj_sp) {
697 // No child member with name "child_name"
698 if (synthetically_added_instance_object) {
699 // We added a "this->" or "self->" to the beginning of the
Adrian Prantl05097242018-04-30 16:49:04 +0000700 // expression and this is the first pointer ivar access, so just
701 // return the normal error
Zachary Turner24bd3172016-11-17 01:37:52 +0000702 error.SetErrorStringWithFormat(
703 "no variable or instance variable named '%s' found in "
704 "this frame",
705 name_const_string.GetCString());
706 } else {
707 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
708 if (child_name) {
709 error.SetErrorStringWithFormat(
710 "\"%s\" is not a member of \"(%s) %s\"",
711 child_name.GetCString(),
712 valobj_sp->GetTypeName().AsCString("<invalid type>"),
713 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000714 } else {
715 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000716 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000717 var_expr_path_strm.GetData(),
718 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000719 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000720 }
721 return ValueObjectSP();
722 }
723 }
724 synthetically_added_instance_object = false;
725 // Remove the child name from the path
726 var_expr = var_expr.drop_front(child_name.GetLength());
727 if (use_dynamic != eNoDynamicValues) {
728 ValueObjectSP dynamic_value_sp(
729 child_valobj_sp->GetDynamicValue(use_dynamic));
730 if (dynamic_value_sp)
731 child_valobj_sp = dynamic_value_sp;
732 }
733 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000734
Zachary Turner24bd3172016-11-17 01:37:52 +0000735 case '[': {
Adrian Prantl05097242018-04-30 16:49:04 +0000736 // Array member access, or treating pointer as an array Need at least two
737 // brackets and a number
Zachary Turner24bd3172016-11-17 01:37:52 +0000738 if (var_expr.size() <= 2) {
739 error.SetErrorStringWithFormat(
740 "invalid square bracket encountered after \"%s\" in \"%s\"",
741 var_expr_path_strm.GetData(), var_expr.str().c_str());
742 return ValueObjectSP();
743 }
744
745 // Drop the open brace.
746 var_expr = var_expr.drop_front();
747 long child_index = 0;
748
749 // If there's no closing brace, this is an invalid expression.
750 size_t end_pos = var_expr.find_first_of(']');
751 if (end_pos == llvm::StringRef::npos) {
752 error.SetErrorStringWithFormat(
753 "missing closing square bracket in expression \"%s\"",
754 var_expr_path_strm.GetData());
755 return ValueObjectSP();
756 }
757 llvm::StringRef index_expr = var_expr.take_front(end_pos);
758 llvm::StringRef original_index_expr = index_expr;
759 // Drop all of "[index_expr]"
760 var_expr = var_expr.drop_front(end_pos + 1);
761
762 if (index_expr.consumeInteger(0, child_index)) {
763 // If there was no integer anywhere in the index expression, this is
764 // erroneous expression.
765 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
766 index_expr.str().c_str());
767 return ValueObjectSP();
768 }
769
770 if (index_expr.empty()) {
771 // The entire index expression was a single integer.
772
773 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
774 // what we have is *ptr[low]. the most similar C++ syntax is to deref
775 // ptr and extract bit low out of it. reading array item low would be
776 // done by saying ptr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000777 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000778 ValueObjectSP temp(valobj_sp->Dereference(error));
779 if (error.Fail()) {
780 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
781 error.SetErrorStringWithFormat(
782 "could not dereference \"(%s) %s\"",
783 valobj_sp->GetTypeName().AsCString("<invalid type>"),
784 var_expr_path_strm.GetData());
785 return ValueObjectSP();
786 }
787 valobj_sp = temp;
788 deref = false;
789 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
790 deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000791 // what we have is *arr[low]. the most similar C++ syntax is to get
792 // arr[0] (an operation that is equivalent to deref-ing arr) and
793 // extract bit low out of it. reading array item low would be done by
794 // saying arr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000795 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000796 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
797 if (error.Fail()) {
798 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
799 error.SetErrorStringWithFormat(
800 "could not get item 0 for \"(%s) %s\"",
801 valobj_sp->GetTypeName().AsCString("<invalid type>"),
802 var_expr_path_strm.GetData());
803 return ValueObjectSP();
804 }
805 valobj_sp = temp;
806 deref = false;
807 }
808
809 bool is_incomplete_array = false;
810 if (valobj_sp->IsPointerType()) {
811 bool is_objc_pointer = true;
812
813 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
814 eLanguageTypeObjC)
815 is_objc_pointer = false;
816 else if (!valobj_sp->GetCompilerType().IsPointerType())
817 is_objc_pointer = false;
818
819 if (no_synth_child && is_objc_pointer) {
820 error.SetErrorStringWithFormat(
821 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
822 "subscripted",
823 valobj_sp->GetTypeName().AsCString("<invalid type>"),
824 var_expr_path_strm.GetData());
825
826 return ValueObjectSP();
827 } else if (is_objc_pointer) {
Adrian Prantl05097242018-04-30 16:49:04 +0000828 // dereferencing ObjC variables is not valid.. so let's try and
829 // recur to synthetic children
Zachary Turner24bd3172016-11-17 01:37:52 +0000830 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
831 if (!synthetic /* no synthetic */
832 || synthetic == valobj_sp) /* synthetic is the same as
833 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000834 {
835 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
836 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000837 "\"(%s) %s\" is not an array type",
838 valobj_sp->GetTypeName().AsCString("<invalid type>"),
839 var_expr_path_strm.GetData());
840 } else if (
841 static_cast<uint32_t>(child_index) >=
842 synthetic
843 ->GetNumChildren() /* synthetic does not have that many values */) {
844 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
845 error.SetErrorStringWithFormat(
846 "array index %ld is not valid for \"(%s) %s\"", child_index,
847 valobj_sp->GetTypeName().AsCString("<invalid type>"),
848 var_expr_path_strm.GetData());
849 } else {
850 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
851 if (!child_valobj_sp) {
852 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
853 error.SetErrorStringWithFormat(
854 "array index %ld is not valid for \"(%s) %s\"", child_index,
855 valobj_sp->GetTypeName().AsCString("<invalid type>"),
856 var_expr_path_strm.GetData());
857 }
858 }
859 } else {
860 child_valobj_sp =
861 valobj_sp->GetSyntheticArrayMember(child_index, true);
862 if (!child_valobj_sp) {
863 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
864 error.SetErrorStringWithFormat(
865 "failed to use pointer as array for index %ld for "
866 "\"(%s) %s\"",
867 child_index,
868 valobj_sp->GetTypeName().AsCString("<invalid type>"),
869 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000870 }
871 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000872 } else if (valobj_sp->GetCompilerType().IsArrayType(
873 nullptr, nullptr, &is_incomplete_array)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000874 // Pass false to dynamic_value here so we can tell the difference
875 // between no dynamic value and no member of this type...
Zachary Turner24bd3172016-11-17 01:37:52 +0000876 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
877 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
878 child_valobj_sp =
879 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000880
Zachary Turner24bd3172016-11-17 01:37:52 +0000881 if (!child_valobj_sp) {
882 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
883 error.SetErrorStringWithFormat(
884 "array index %ld is not valid for \"(%s) %s\"", child_index,
885 valobj_sp->GetTypeName().AsCString("<invalid type>"),
886 var_expr_path_strm.GetData());
887 }
888 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
889 // this is a bitfield asking to display just one bit
890 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
891 child_index, child_index, true);
892 if (!child_valobj_sp) {
893 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
894 error.SetErrorStringWithFormat(
895 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
896 child_index, child_index,
897 valobj_sp->GetTypeName().AsCString("<invalid type>"),
898 var_expr_path_strm.GetData());
899 }
900 } else {
901 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
902 if (no_synth_child /* synthetic is forbidden */ ||
903 !synthetic /* no synthetic */
904 || synthetic == valobj_sp) /* synthetic is the same as the
905 original object */
906 {
907 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
908 error.SetErrorStringWithFormat(
909 "\"(%s) %s\" is not an array type",
910 valobj_sp->GetTypeName().AsCString("<invalid type>"),
911 var_expr_path_strm.GetData());
912 } else if (
913 static_cast<uint32_t>(child_index) >=
914 synthetic
915 ->GetNumChildren() /* synthetic does not have that many values */) {
916 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
917 error.SetErrorStringWithFormat(
918 "array index %ld is not valid for \"(%s) %s\"", child_index,
919 valobj_sp->GetTypeName().AsCString("<invalid type>"),
920 var_expr_path_strm.GetData());
921 } else {
922 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
923 if (!child_valobj_sp) {
924 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
925 error.SetErrorStringWithFormat(
926 "array index %ld is not valid for \"(%s) %s\"", child_index,
927 valobj_sp->GetTypeName().AsCString("<invalid type>"),
928 var_expr_path_strm.GetData());
929 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000930 }
931 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000932
933 if (!child_valobj_sp) {
934 // Invalid array index...
935 return ValueObjectSP();
936 }
937
938 separator_idx = var_expr.find_first_of(".-[");
939 if (use_dynamic != eNoDynamicValues) {
940 ValueObjectSP dynamic_value_sp(
941 child_valobj_sp->GetDynamicValue(use_dynamic));
942 if (dynamic_value_sp)
943 child_valobj_sp = dynamic_value_sp;
944 }
945 // Break out early from the switch since we were able to find the child
946 // member
947 break;
948 }
949
950 // this is most probably a BitField, let's take a look
951 if (index_expr.front() != '-') {
952 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
953 original_index_expr.str().c_str());
954 return ValueObjectSP();
955 }
956
Zachary Turner7edc3a62016-11-21 23:18:13 +0000957 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000958 long final_index = 0;
959 if (index_expr.getAsInteger(0, final_index)) {
960 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
961 original_index_expr.str().c_str());
962 return ValueObjectSP();
963 }
964
965 // if the format given is [high-low], swap range
966 if (child_index > final_index) {
967 long temp = child_index;
968 child_index = final_index;
969 final_index = temp;
970 }
971
972 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
973 // what we have is *ptr[low-high]. the most similar C++ syntax is to
974 // deref ptr and extract bits low thru high out of it. reading array
Adrian Prantl05097242018-04-30 16:49:04 +0000975 // items low thru high would be done by saying ptr[low-high], without a
976 // deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000977 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000978 ValueObjectSP temp(valobj_sp->Dereference(error));
979 if (error.Fail()) {
980 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
981 error.SetErrorStringWithFormat(
982 "could not dereference \"(%s) %s\"",
983 valobj_sp->GetTypeName().AsCString("<invalid type>"),
984 var_expr_path_strm.GetData());
985 return ValueObjectSP();
986 }
987 valobj_sp = temp;
988 deref = false;
989 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000990 // what we have is *arr[low-high]. the most similar C++ syntax is to
991 // get arr[0] (an operation that is equivalent to deref-ing arr) and
992 // extract bits low thru high out of it. reading array items low thru
993 // high would be done by saying arr[low-high], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000994 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000995 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
996 if (error.Fail()) {
997 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
998 error.SetErrorStringWithFormat(
999 "could not get item 0 for \"(%s) %s\"",
1000 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1001 var_expr_path_strm.GetData());
1002 return ValueObjectSP();
1003 }
1004 valobj_sp = temp;
1005 deref = false;
1006 }
1007
1008 child_valobj_sp =
1009 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1010 if (!child_valobj_sp) {
1011 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001012 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +00001013 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1014 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1015 var_expr_path_strm.GetData());
1016 }
1017
1018 if (!child_valobj_sp) {
1019 // Invalid bitfield range...
1020 return ValueObjectSP();
1021 }
1022
1023 separator_idx = var_expr.find_first_of(".-[");
1024 if (use_dynamic != eNoDynamicValues) {
1025 ValueObjectSP dynamic_value_sp(
1026 child_valobj_sp->GetDynamicValue(use_dynamic));
1027 if (dynamic_value_sp)
1028 child_valobj_sp = dynamic_value_sp;
1029 }
1030 // Break out early from the switch since we were able to find the child
1031 // member
1032 break;
1033 }
1034 default:
1035 // Failure...
1036 {
1037 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1038 error.SetErrorStringWithFormat(
1039 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1040 separator_type, var_expr_path_strm.GetData(),
1041 var_expr.str().c_str());
1042
1043 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001044 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001045 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001046
1047 if (child_valobj_sp)
1048 valobj_sp = child_valobj_sp;
1049
1050 if (var_expr.empty())
1051 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001053 if (valobj_sp) {
1054 if (deref) {
1055 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1056 valobj_sp = deref_valobj_sp;
1057 } else if (address_of) {
1058 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1059 valobj_sp = address_of_valobj_sp;
1060 }
1061 }
1062 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063}
1064
Zachary Turner97206d52017-05-12 04:51:55 +00001065bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1067 if (!m_cfa_is_valid) {
1068 m_frame_base_error.SetErrorString(
1069 "No frame base available for this historical stack frame.");
1070 return false;
1071 }
1072
1073 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1074 if (m_sc.function) {
1075 m_frame_base.Clear();
1076 m_frame_base_error.Clear();
1077
1078 m_flags.Set(GOT_FRAME_BASE);
1079 ExecutionContext exe_ctx(shared_from_this());
1080 Value expr_value;
1081 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1082 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1083 loclist_base_addr =
1084 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1085 exe_ctx.GetTargetPtr());
1086
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001087 if (!m_sc.function->GetFrameBaseExpression().Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001088 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001089 expr_value, &m_frame_base_error)) {
Adrian Prantl05097242018-04-30 16:49:04 +00001090 // We should really have an error if evaluate returns, but in case we
1091 // don't, lets set the error to something at least.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 if (m_frame_base_error.Success())
1093 m_frame_base_error.SetErrorString(
1094 "Evaluation of the frame base expression failed.");
1095 } else {
1096 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1097 }
1098 } else {
1099 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001100 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001101 }
1102
1103 if (m_frame_base_error.Success())
1104 frame_base = m_frame_base;
1105
1106 if (error_ptr)
1107 *error_ptr = m_frame_base_error;
1108 return m_frame_base_error.Success();
1109}
1110
Zachary Turner97206d52017-05-12 04:51:55 +00001111DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001112 if (!m_sc.function) {
1113 if (error_ptr) {
1114 error_ptr->SetErrorString("No function in symbol context.");
1115 }
1116 return nullptr;
1117 }
1118
1119 return &m_sc.function->GetFrameBaseExpression();
1120}
1121
1122RegisterContextSP StackFrame::GetRegisterContext() {
1123 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1124 if (!m_reg_context_sp) {
1125 ThreadSP thread_sp(GetThread());
1126 if (thread_sp)
1127 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1128 }
1129 return m_reg_context_sp;
1130}
1131
1132bool StackFrame::HasDebugInformation() {
1133 GetSymbolContext(eSymbolContextLineEntry);
1134 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001135}
1136
1137ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1139 DynamicValueType use_dynamic) {
1140 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1141 ValueObjectSP valobj_sp;
Vedant Kumar4b36f792018-10-05 23:23:15 +00001142 if (IsHistorical()) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001143 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144 }
1145 VariableList *var_list = GetVariableList(true);
1146 if (var_list) {
1147 // Make sure the variable is a frame variable
1148 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1149 const uint32_t num_variables = var_list->GetSize();
1150 if (var_idx < num_variables) {
1151 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1152 if (!valobj_sp) {
1153 if (m_variable_list_value_objects.GetSize() < num_variables)
1154 m_variable_list_value_objects.Resize(num_variables);
1155 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1156 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1157 }
Enrico Granata592afe72016-03-15 21:50:51 +00001158 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001159 }
1160 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1161 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1162 if (dynamic_sp)
1163 return dynamic_sp;
1164 }
1165 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001166}
1167
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1169 DynamicValueType use_dynamic) {
1170 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Vedant Kumar4b36f792018-10-05 23:23:15 +00001171 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172 return ValueObjectSP();
1173
1174 // Check to make sure we aren't already tracking this variable?
1175 ValueObjectSP valobj_sp(
1176 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1177 if (!valobj_sp) {
1178 // We aren't already tracking this global
1179 VariableList *var_list = GetVariableList(true);
1180 // If this frame has no variables, create a new list
1181 if (var_list == nullptr)
Jonas Devlieghere796ac802019-02-11 23:13:08 +00001182 m_variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001183
1184 // Add the global/static variable to this frame
1185 m_variable_list_sp->AddVariable(variable_sp);
1186
1187 // Now make a value object for it so we can track its changes
1188 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1189 }
1190 return valobj_sp;
1191}
1192
1193bool StackFrame::IsInlined() {
1194 if (m_sc.block == nullptr)
1195 GetSymbolContext(eSymbolContextBlock);
1196 if (m_sc.block)
1197 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1198 return false;
1199}
1200
Vedant Kumar4b36f792018-10-05 23:23:15 +00001201bool StackFrame::IsHistorical() const {
1202 return m_stack_frame_kind == StackFrame::Kind::History;
1203}
1204
1205bool StackFrame::IsArtificial() const {
1206 return m_stack_frame_kind == StackFrame::Kind::Artificial;
1207}
1208
Kate Stoneb9c1b512016-09-06 20:57:50 +00001209lldb::LanguageType StackFrame::GetLanguage() {
1210 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1211 if (cu)
1212 return cu->GetLanguage();
1213 return lldb::eLanguageTypeUnknown;
1214}
1215
1216lldb::LanguageType StackFrame::GuessLanguage() {
1217 LanguageType lang_type = GetLanguage();
1218
1219 if (lang_type == eLanguageTypeUnknown) {
Jim Inghambdbdd222017-04-12 00:19:54 +00001220 SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1221 | eSymbolContextSymbol);
1222 if (sc.function) {
1223 lang_type = sc.function->GetMangled().GuessLanguage();
1224 }
1225 else if (sc.symbol)
1226 {
1227 lang_type = sc.symbol->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001228 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001229 }
1230
1231 return lang_type;
1232}
1233
1234namespace {
1235std::pair<const Instruction::Operand *, int64_t>
1236GetBaseExplainingValue(const Instruction::Operand &operand,
1237 RegisterContext &register_context, lldb::addr_t value) {
1238 switch (operand.m_type) {
1239 case Instruction::Operand::Type::Dereference:
1240 case Instruction::Operand::Type::Immediate:
1241 case Instruction::Operand::Type::Invalid:
1242 case Instruction::Operand::Type::Product:
1243 // These are not currently interesting
1244 return std::make_pair(nullptr, 0);
1245 case Instruction::Operand::Type::Sum: {
1246 const Instruction::Operand *immediate_child = nullptr;
1247 const Instruction::Operand *variable_child = nullptr;
1248 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1249 immediate_child = &operand.m_children[0];
1250 variable_child = &operand.m_children[1];
1251 } else if (operand.m_children[1].m_type ==
1252 Instruction::Operand::Type::Immediate) {
1253 immediate_child = &operand.m_children[1];
1254 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001255 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 if (!immediate_child) {
1257 return std::make_pair(nullptr, 0);
1258 }
1259 lldb::addr_t adjusted_value = value;
1260 if (immediate_child->m_negative) {
1261 adjusted_value += immediate_child->m_immediate;
1262 } else {
1263 adjusted_value -= immediate_child->m_immediate;
1264 }
1265 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1266 GetBaseExplainingValue(*variable_child, register_context,
1267 adjusted_value);
1268 if (!base_and_offset.first) {
1269 return std::make_pair(nullptr, 0);
1270 }
1271 if (immediate_child->m_negative) {
1272 base_and_offset.second -= immediate_child->m_immediate;
1273 } else {
1274 base_and_offset.second += immediate_child->m_immediate;
1275 }
1276 return base_and_offset;
1277 }
1278 case Instruction::Operand::Type::Register: {
1279 const RegisterInfo *info =
1280 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1281 if (!info) {
1282 return std::make_pair(nullptr, 0);
1283 }
1284 RegisterValue reg_value;
1285 if (!register_context.ReadRegister(info, reg_value)) {
1286 return std::make_pair(nullptr, 0);
1287 }
1288 if (reg_value.GetAsUInt64() == value) {
1289 return std::make_pair(&operand, 0);
1290 } else {
1291 return std::make_pair(nullptr, 0);
1292 }
1293 }
1294 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001295 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296}
1297
1298std::pair<const Instruction::Operand *, int64_t>
1299GetBaseExplainingDereference(const Instruction::Operand &operand,
1300 RegisterContext &register_context,
1301 lldb::addr_t addr) {
1302 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1303 return GetBaseExplainingValue(operand.m_children[0], register_context,
1304 addr);
1305 }
1306 return std::make_pair(nullptr, 0);
1307}
Ilia K4f730dc2016-09-12 05:25:33 +00001308}
Sean Callanan4740a732016-09-06 04:48:36 +00001309
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1311 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001312
Kate Stoneb9c1b512016-09-06 20:57:50 +00001313 const ArchSpec &target_arch = target_sp->GetArchitecture();
1314
1315 AddressRange pc_range;
1316 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1317 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1318
1319 ExecutionContext exe_ctx(shared_from_this());
1320
1321 const char *plugin_name = nullptr;
1322 const char *flavor = nullptr;
1323 const bool prefer_file_cache = false;
1324
1325 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1326 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1327
Jim Ingham99d1e282017-03-31 22:39:55 +00001328 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001329 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 }
Sean Callanan4740a732016-09-06 04:48:36 +00001331
Kate Stoneb9c1b512016-09-06 20:57:50 +00001332 InstructionSP instruction_sp =
1333 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1334
1335 llvm::SmallVector<Instruction::Operand, 3> operands;
1336
1337 if (!instruction_sp->ParseOperands(operands)) {
1338 return ValueObjectSP();
1339 }
1340
1341 RegisterContextSP register_context_sp = GetRegisterContext();
1342
1343 if (!register_context_sp) {
1344 return ValueObjectSP();
1345 }
1346
1347 for (const Instruction::Operand &operand : operands) {
1348 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1349 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1350
1351 if (!base_and_offset.first) {
1352 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001353 }
Sean Callanan4740a732016-09-06 04:48:36 +00001354
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355 switch (base_and_offset.first->m_type) {
1356 case Instruction::Operand::Type::Immediate: {
1357 lldb_private::Address addr;
1358 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1359 base_and_offset.second,
1360 addr)) {
Alex Langford0e252e32019-07-30 22:12:34 +00001361 auto c_type_system_or_err =
1362 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
1363 if (auto err = c_type_system_or_err.takeError()) {
1364 LLDB_LOG_ERROR(
1365 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD),
1366 std::move(err), "Unable to guess value for given address");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 return ValueObjectSP();
1368 } else {
1369 CompilerType void_ptr_type =
Alex Langford0e252e32019-07-30 22:12:34 +00001370 c_type_system_or_err
Kate Stoneb9c1b512016-09-06 20:57:50 +00001371 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1372 .GetPointerType();
1373 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001374 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001376 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 }
1378 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001379 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 case Instruction::Operand::Type::Register: {
1381 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1382 base_and_offset.second);
1383 }
1384 default:
1385 return ValueObjectSP();
1386 }
1387 }
1388
1389 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392namespace {
1393ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1394 int64_t offset) {
1395 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1396 return ValueObjectSP();
1397 }
Sean Callanan4740a732016-09-06 04:48:36 +00001398
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 if (parent->IsPointerOrReferenceType()) {
1400 return parent;
1401 }
1402
1403 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1404 const bool can_create = true;
1405 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1406
1407 if (!child_sp) {
1408 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001409 }
1410
Kate Stoneb9c1b512016-09-06 20:57:50 +00001411 int64_t child_offset = child_sp->GetByteOffset();
1412 int64_t child_size = child_sp->GetByteSize();
1413
1414 if (offset >= child_offset && offset < (child_offset + child_size)) {
1415 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001416 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 }
Sean Callanan4740a732016-09-06 04:48:36 +00001418
Kate Stoneb9c1b512016-09-06 20:57:50 +00001419 if (offset == 0) {
1420 return parent;
1421 } else {
1422 return ValueObjectSP();
1423 }
1424}
1425
1426ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1427 ValueObjectSP &base,
1428 int64_t offset) {
1429 // base is a pointer to something
Adrian Prantl05097242018-04-30 16:49:04 +00001430 // offset is the thing to add to the pointer We return the most sensible
1431 // ValueObject for the result of *(base+offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432
1433 if (!base->IsPointerOrReferenceType()) {
1434 return ValueObjectSP();
1435 }
1436
Zachary Turner97206d52017-05-12 04:51:55 +00001437 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001439
1440 if (!pointee) {
1441 return ValueObjectSP();
1442 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443
Ilia K4f730dc2016-09-12 05:25:33 +00001444 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 int64_t index = offset / pointee->GetByteSize();
1446 offset = offset % pointee->GetByteSize();
1447 const bool can_create = true;
1448 pointee = base->GetSyntheticArrayMember(index, can_create);
1449 }
1450
1451 if (!pointee || error.Fail()) {
1452 return ValueObjectSP();
1453 }
1454
1455 return GetValueForOffset(frame, pointee, offset);
1456}
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458/// Attempt to reconstruct the ValueObject for the address contained in a
1459/// given register plus an offset.
1460///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001461/// \params [in] frame
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462/// The current stack frame.
1463///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001464/// \params [in] reg
Kate Stoneb9c1b512016-09-06 20:57:50 +00001465/// The register.
1466///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001467/// \params [in] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468/// The offset from the register.
1469///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001470/// \param [in] disassembler
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471/// A disassembler containing instructions valid up to the current PC.
1472///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001473/// \param [in] variables
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474/// The variable list from the current frame,
1475///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001476/// \param [in] pc
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477/// The program counter for the instruction considered the 'user'.
1478///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001479/// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480/// A string describing the base for the ExpressionPath. This could be a
1481/// variable, a register value, an argument, or a function return value.
1482/// The ValueObject if found. If valid, it has a valid ExpressionPath.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1484 int64_t offset, Disassembler &disassembler,
1485 VariableList &variables, const Address &pc) {
1486 // Example of operation for Intel:
1487 //
1488 // +14: movq -0x8(%rbp), %rdi
1489 // +18: movq 0x8(%rdi), %rdi
1490 // +22: addl 0x4(%rdi), %eax
1491 //
1492 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1493 //
Adrian Prantl05097242018-04-30 16:49:04 +00001494 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1495 // +18 that assigns to rdi, and calls itself recursively for that dereference
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1497 // +14 that assigns to rdi, and calls itself recursively for that
1498 // derefernece
1499 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1500 // variable list.
1501 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1502 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1503 // at +18)
1504 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1505 // rdi+4 at +22)
1506
1507 // First, check the variable list to see if anything is at the specified
1508 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001509
Sean Callanan50857102016-09-14 00:48:19 +00001510 using namespace OperandMatchers;
1511
Sean Callanan0ac172d2016-09-14 20:29:57 +00001512 const RegisterInfo *reg_info =
1513 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1514 if (!reg_info) {
1515 return ValueObjectSP();
1516 }
1517
Sean Callanan807ee2f2016-09-13 21:18:27 +00001518 Instruction::Operand op =
1519 offset ? Instruction::Operand::BuildDereference(
1520 Instruction::Operand::BuildSum(
1521 Instruction::Operand::BuildRegister(reg),
1522 Instruction::Operand::BuildImmediate(offset)))
1523 : Instruction::Operand::BuildDereference(
1524 Instruction::Operand::BuildRegister(reg));
1525
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1527 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001528 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1529 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001530 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 }
1532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 const uint32_t current_inst =
1534 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1535 if (current_inst == UINT32_MAX) {
1536 return ValueObjectSP();
1537 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1540 // This is not an exact algorithm, and it sacrifices accuracy for
Adrian Prantl05097242018-04-30 16:49:04 +00001541 // generality. Recognizing "mov" and "ld" instructions –– and which
1542 // are their source and destination operands -- is something the
1543 // disassembler should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001544 InstructionSP instruction_sp =
1545 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546
Sean Callanan50857102016-09-14 00:48:19 +00001547 if (instruction_sp->IsCall()) {
1548 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1549 if (!abi_sp) {
1550 continue;
1551 }
1552
1553 const char *return_register_name;
1554 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1555 continue;
1556 }
1557
1558 const RegisterInfo *return_register_info =
1559 frame.GetRegisterContext()->GetRegisterInfoByName(
1560 return_register_name);
1561 if (!return_register_info) {
1562 continue;
1563 }
1564
1565 int64_t offset = 0;
1566
1567 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1568 MatchRegOp(*return_register_info))(op) &&
1569 !MatchUnaryOp(
1570 MatchOpType(Instruction::Operand::Type::Dereference),
1571 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1572 MatchRegOp(*return_register_info),
1573 FetchImmOp(offset)))(op)) {
1574 continue;
1575 }
1576
Kate Stoneb9c1b512016-09-06 20:57:50 +00001577 llvm::SmallVector<Instruction::Operand, 1> operands;
1578 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1579 continue;
1580 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581
Kate Stoneb9c1b512016-09-06 20:57:50 +00001582 switch (operands[0].m_type) {
1583 default:
1584 break;
1585 case Instruction::Operand::Type::Immediate: {
1586 SymbolContext sc;
1587 Address load_address;
1588 if (!frame.CalculateTarget()->ResolveLoadAddress(
1589 operands[0].m_immediate, load_address)) {
1590 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001591 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1593 load_address, eSymbolContextFunction, sc);
1594 if (!sc.function) {
1595 break;
1596 }
1597 CompilerType function_type = sc.function->GetCompilerType();
1598 if (!function_type.IsFunctionType()) {
1599 break;
1600 }
1601 CompilerType return_type = function_type.GetFunctionReturnType();
1602 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001603 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604 return_value)) {
1605 break;
1606 }
1607 std::string name_str(
1608 sc.function->GetName().AsCString("<unknown function>"));
1609 name_str.append("()");
1610 Address return_value_address(return_value.GetAsUInt64());
1611 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001612 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1614 }
1615 }
1616
1617 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001618 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619
1620 llvm::SmallVector<Instruction::Operand, 2> operands;
1621 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1622 continue;
1623 }
1624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001626 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1627 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1628 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001629
1630 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001632 }
1633 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001634 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001635 }
1636 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 continue;
1638 }
1639
1640 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001641 ValueObjectSP source_path;
1642 ConstString origin_register;
1643 int64_t origin_offset = 0;
1644
1645 if (FetchRegOp(origin_register)(*origin_operand)) {
1646 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1647 variables, instruction_sp->GetAddress());
1648 } else if (MatchUnaryOp(
1649 MatchOpType(Instruction::Operand::Type::Dereference),
1650 FetchRegOp(origin_register))(*origin_operand) ||
1651 MatchUnaryOp(
1652 MatchOpType(Instruction::Operand::Type::Dereference),
1653 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1654 FetchRegOp(origin_register),
1655 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001656 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001657 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001659 if (!source_path) {
1660 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001661 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001662 source_path =
1663 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 }
1665
1666 if (source_path) {
1667 return source_path;
1668 }
1669 }
1670
1671 return ValueObjectSP();
1672}
1673}
1674
1675lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1676 int64_t offset) {
1677 TargetSP target_sp = CalculateTarget();
1678
1679 const ArchSpec &target_arch = target_sp->GetArchitecture();
1680
1681 Block *frame_block = GetFrameBlock();
1682
1683 if (!frame_block) {
1684 return ValueObjectSP();
1685 }
1686
1687 Function *function = frame_block->CalculateSymbolContextFunction();
1688 if (!function) {
1689 return ValueObjectSP();
1690 }
1691
1692 AddressRange pc_range = function->GetAddressRange();
1693
1694 if (GetFrameCodeAddress().GetFileAddress() <
1695 pc_range.GetBaseAddress().GetFileAddress() ||
1696 GetFrameCodeAddress().GetFileAddress() -
1697 pc_range.GetBaseAddress().GetFileAddress() >=
1698 pc_range.GetByteSize()) {
1699 return ValueObjectSP();
1700 }
1701
1702 ExecutionContext exe_ctx(shared_from_this());
1703
1704 const char *plugin_name = nullptr;
1705 const char *flavor = nullptr;
1706 const bool prefer_file_cache = false;
1707 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1708 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1709
1710 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1711 return ValueObjectSP();
1712 }
1713
1714 const bool get_file_globals = false;
1715 VariableList *variables = GetVariableList(get_file_globals);
1716
1717 if (!variables) {
1718 return ValueObjectSP();
1719 }
1720
1721 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1722 GetFrameCodeAddress());
1723}
1724
Shafik Yaghmoure23d0b62018-09-20 17:06:34 +00001725lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1726 ValueObjectSP value_sp;
1727
1728 if (!name)
1729 return value_sp;
1730
1731 TargetSP target_sp = CalculateTarget();
1732 ProcessSP process_sp = CalculateProcess();
1733
1734 if (!target_sp && !process_sp)
1735 return value_sp;
1736
1737 VariableList variable_list;
1738 VariableSP var_sp;
1739 SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1740
1741 if (sc.block) {
1742 const bool can_create = true;
1743 const bool get_parent_variables = true;
1744 const bool stop_if_block_is_inlined_function = true;
1745
1746 if (sc.block->AppendVariables(
1747 can_create, get_parent_variables, stop_if_block_is_inlined_function,
1748 [this](Variable *v) { return v->IsInScope(this); },
1749 &variable_list)) {
1750 var_sp = variable_list.FindVariable(name);
1751 }
1752
1753 if (var_sp)
1754 value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1755 }
1756
1757 return value_sp;
1758}
1759
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760TargetSP StackFrame::CalculateTarget() {
1761 TargetSP target_sp;
1762 ThreadSP thread_sp(GetThread());
1763 if (thread_sp) {
1764 ProcessSP process_sp(thread_sp->CalculateProcess());
1765 if (process_sp)
1766 target_sp = process_sp->CalculateTarget();
1767 }
1768 return target_sp;
1769}
1770
1771ProcessSP StackFrame::CalculateProcess() {
1772 ProcessSP process_sp;
1773 ThreadSP thread_sp(GetThread());
1774 if (thread_sp)
1775 process_sp = thread_sp->CalculateProcess();
1776 return process_sp;
1777}
1778
1779ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1780
1781StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1782
1783void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1784 exe_ctx.SetContext(shared_from_this());
1785}
1786
Pavel Labath7f1c1212017-06-12 16:25:24 +00001787void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001788 const char *frame_marker) {
1789 if (strm == nullptr)
1790 return;
1791
1792 GetSymbolContext(eSymbolContextEverything);
1793 ExecutionContext exe_ctx(shared_from_this());
1794 StreamString s;
1795
1796 if (frame_marker)
1797 s.PutCString(frame_marker);
1798
1799 const FormatEntity::Entry *frame_format = nullptr;
1800 Target *target = exe_ctx.GetTargetPtr();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001801 if (target) {
1802 if (show_unique) {
1803 frame_format = target->GetDebugger().GetFrameFormatUnique();
1804 } else {
1805 frame_format = target->GetDebugger().GetFrameFormat();
1806 }
1807 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001808 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1809 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001810 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001811 } else {
1812 Dump(strm, true, false);
1813 strm->EOL();
1814 }
1815}
1816
1817void StackFrame::Dump(Stream *strm, bool show_frame_index,
1818 bool show_fullpaths) {
1819 if (strm == nullptr)
1820 return;
1821
1822 if (show_frame_index)
1823 strm->Printf("frame #%u: ", m_frame_index);
1824 ExecutionContext exe_ctx(shared_from_this());
1825 Target *target = exe_ctx.GetTargetPtr();
1826 strm->Printf("0x%0*" PRIx64 " ",
1827 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1828 : 16,
1829 GetFrameCodeAddress().GetLoadAddress(target));
1830 GetSymbolContext(eSymbolContextEverything);
1831 const bool show_module = true;
1832 const bool show_inline = true;
1833 const bool show_function_arguments = true;
1834 const bool show_function_name = true;
1835 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1836 GetFrameCodeAddress(), show_fullpaths, show_module,
1837 show_inline, show_function_arguments,
1838 show_function_name);
1839}
1840
1841void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1842 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1843 assert(GetStackID() ==
1844 prev_frame.GetStackID()); // TODO: remove this after some testing
1845 m_variable_list_sp = prev_frame.m_variable_list_sp;
1846 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001847 if (!m_disassembly.GetString().empty()) {
1848 m_disassembly.Clear();
1849 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1850 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001851}
1852
1853void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1854 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1855 assert(GetStackID() ==
1856 curr_frame.GetStackID()); // TODO: remove this after some testing
1857 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1858 assert(GetThread() == curr_frame.GetThread());
1859 m_frame_index = curr_frame.m_frame_index;
1860 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1861 m_reg_context_sp = curr_frame.m_reg_context_sp;
1862 m_frame_code_addr = curr_frame.m_frame_code_addr;
1863 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1864 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1865 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1866 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1867 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1868 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1869 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1870 m_sc.function == curr_frame.m_sc.function);
1871 m_sc = curr_frame.m_sc;
1872 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1873 m_flags.Set(m_sc.GetResolvedMask());
1874 m_frame_base.Clear();
1875 m_frame_base_error.Clear();
1876}
1877
1878bool StackFrame::HasCachedData() const {
1879 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001880 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001881 if (m_variable_list_value_objects.GetSize() > 0)
1882 return true;
1883 if (!m_disassembly.GetString().empty())
1884 return true;
1885 return false;
1886}
1887
1888bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
Pavel Labath7f1c1212017-06-12 16:25:24 +00001889 bool show_unique, const char *frame_marker) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001890 if (show_frame_info) {
1891 strm.Indent();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001892 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001893 }
1894
1895 if (show_source) {
1896 ExecutionContext exe_ctx(shared_from_this());
1897 bool have_source = false, have_debuginfo = false;
1898 Debugger::StopDisassemblyType disasm_display =
1899 Debugger::eStopDisassemblyTypeNever;
1900 Target *target = exe_ctx.GetTargetPtr();
1901 if (target) {
1902 Debugger &debugger = target->GetDebugger();
1903 const uint32_t source_lines_before =
1904 debugger.GetStopSourceLineCount(true);
1905 const uint32_t source_lines_after =
1906 debugger.GetStopSourceLineCount(false);
1907 disasm_display = debugger.GetStopDisassemblyDisplay();
1908
1909 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1910 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1911 have_debuginfo = true;
1912 if (source_lines_before > 0 || source_lines_after > 0) {
1913 size_t num_lines =
1914 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1915 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001916 m_sc.line_entry.column, source_lines_before,
1917 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001918 if (num_lines != 0)
1919 have_source = true;
1920 // TODO: Give here a one time warning if source file is missing.
1921 }
1922 }
1923 switch (disasm_display) {
1924 case Debugger::eStopDisassemblyTypeNever:
1925 break;
1926
1927 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1928 if (have_debuginfo)
1929 break;
1930 LLVM_FALLTHROUGH;
1931
1932 case Debugger::eStopDisassemblyTypeNoSource:
1933 if (have_source)
1934 break;
1935 LLVM_FALLTHROUGH;
1936
1937 case Debugger::eStopDisassemblyTypeAlways:
1938 if (target) {
1939 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1940 if (disasm_lines > 0) {
1941 const ArchSpec &target_arch = target->GetArchitecture();
1942 AddressRange pc_range;
1943 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1944 pc_range.SetByteSize(disasm_lines *
1945 target_arch.GetMaximumOpcodeByteSize());
1946 const char *plugin_name = nullptr;
1947 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001948 const bool mixed_source_and_assembly = false;
1949 Disassembler::Disassemble(
1950 target->GetDebugger(), target_arch, plugin_name, flavor,
1951 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1952 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001953 }
1954 }
1955 break;
1956 }
1957 }
1958 }
1959 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001960}
Kuba Mracek41ae8e72018-10-31 04:00:22 +00001961
1962RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
1963 if (!m_recognized_frame_sp) {
1964 m_recognized_frame_sp =
1965 StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame());
1966 }
1967 return m_recognized_frame_sp;
1968}