blob: 5e5a596e471d1876c5ad6213c5f00a2b8011b5bc [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,
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +000054 bool behaves_like_zeroth_frame,
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000055 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
57 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
58 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
59 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +000060 m_stack_frame_kind(kind),
61 m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
62 m_variable_list_sp(), m_variable_list_value_objects(),
63 m_recognized_frame_sp(), m_disassembly(), m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 // If we don't have a CFA value, use the frame index for our StackID so that
Adrian Prantl05097242018-04-30 16:49:04 +000065 // recursive functions properly aren't confused with one another on a history
66 // stack.
Vedant Kumar4b36f792018-10-05 23:23:15 +000067 if (IsHistorical() && !m_cfa_is_valid) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 m_id.SetCFA(m_frame_index);
69 }
Jason Molenda99618472013-11-04 11:02:52 +000070
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 if (sc_ptr != nullptr) {
72 m_sc = *sc_ptr;
73 m_flags.Set(m_sc.GetResolvedMask());
74 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
78 user_id_t unwind_frame_index,
79 const RegisterContextSP &reg_context_sp, addr_t cfa,
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +000080 addr_t pc, bool behaves_like_zeroth_frame,
81 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000083 m_concrete_frame_index(unwind_frame_index),
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
85 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +000086 m_frame_base_error(), m_cfa_is_valid(true),
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +000087 m_stack_frame_kind(StackFrame::Kind::Regular),
88 m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
89 m_variable_list_sp(), m_variable_list_value_objects(),
90 m_recognized_frame_sp(), m_disassembly(), m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (sc_ptr != nullptr) {
92 m_sc = *sc_ptr;
93 m_flags.Set(m_sc.GetResolvedMask());
94 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 if (reg_context_sp && !m_sc.target_sp) {
97 m_sc.target_sp = reg_context_sp->CalculateTarget();
98 if (m_sc.target_sp)
99 m_flags.Set(eSymbolContextTarget);
100 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
104 user_id_t unwind_frame_index,
105 const RegisterContextSP &reg_context_sp, addr_t cfa,
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +0000106 const Address &pc_addr, bool behaves_like_zeroth_frame,
107 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000109 m_concrete_frame_index(unwind_frame_index),
110 m_reg_context_sp(reg_context_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
112 nullptr),
113 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
Vedant Kumar4b36f792018-10-05 23:23:15 +0000114 m_frame_base_error(), m_cfa_is_valid(true),
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +0000115 m_stack_frame_kind(StackFrame::Kind::Regular),
116 m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
117 m_variable_list_sp(), m_variable_list_value_objects(),
118 m_recognized_frame_sp(), m_disassembly(), m_mutex() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 if (sc_ptr != nullptr) {
120 m_sc = *sc_ptr;
121 m_flags.Set(m_sc.GetResolvedMask());
122 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 if (!m_sc.target_sp && reg_context_sp) {
125 m_sc.target_sp = reg_context_sp->CalculateTarget();
126 if (m_sc.target_sp)
127 m_flags.Set(eSymbolContextTarget);
128 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 ModuleSP pc_module_sp(pc_addr.GetModule());
131 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
132 if (pc_module_sp) {
133 m_sc.module_sp = pc_module_sp;
134 m_flags.Set(eSymbolContextModule);
135 } else {
136 m_sc.module_sp.reset();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139}
140
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000141StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143StackID &StackFrame::GetStackID() {
144 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Adrian Prantl05097242018-04-30 16:49:04 +0000145 // Make sure we have resolved the StackID object's symbol context scope if we
146 // already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
149 if (m_id.GetSymbolContextScope()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000150 // We already have a symbol context scope, we just don't have our flag
151 // bit set.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
153 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000154 // Calculate the frame block and use this for the stack ID symbol context
155 // scope if we have one.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 SymbolContextScope *scope = GetFrameBlock();
157 if (scope == nullptr) {
158 // We don't have a block, so use the symbol
159 if (m_flags.IsClear(eSymbolContextSymbol))
160 GetSymbolContext(eSymbolContextSymbol);
161
162 // It is ok if m_sc.symbol is nullptr here
163 scope = m_sc.symbol;
164 }
165 // Set the symbol context scope (the accessor will set the
166 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
167 SetSymbolContextScope(scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 }
170 return m_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171}
172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173uint32_t StackFrame::GetFrameIndex() const {
174 ThreadSP thread_sp = GetThread();
175 if (thread_sp)
176 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
177 m_frame_index);
178 else
179 return m_frame_index;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
183 std::lock_guard<std::recursive_mutex> guard(m_mutex);
184 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
185 m_id.SetSymbolContextScope(symbol_scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188const Address &StackFrame::GetFrameCodeAddress() {
189 std::lock_guard<std::recursive_mutex> guard(m_mutex);
190 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
191 !m_frame_code_addr.IsSectionOffset()) {
192 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 // Resolve the PC into a temporary address because if ResolveLoadAddress
195 // fails to resolve the address, it will clear the address object...
196 ThreadSP thread_sp(GetThread());
197 if (thread_sp) {
198 TargetSP target_sp(thread_sp->CalculateTarget());
199 if (target_sp) {
Pavel Labathc3c72122017-06-08 13:26:35 +0000200 const bool allow_section_end = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (m_frame_code_addr.SetOpcodeLoadAddress(
202 m_frame_code_addr.GetOffset(), target_sp.get(),
Tatyana Krasnukha04803b32018-06-26 13:06:54 +0000203 AddressClass::eCode, allow_section_end)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 ModuleSP module_sp(m_frame_code_addr.GetModule());
205 if (module_sp) {
206 m_sc.module_sp = module_sp;
207 m_flags.Set(eSymbolContextModule);
208 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 }
213 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216bool StackFrame::ChangePC(addr_t pc) {
217 std::lock_guard<std::recursive_mutex> guard(m_mutex);
218 // We can't change the pc value of a history stack frame - it is immutable.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000219 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 return false;
221 m_frame_code_addr.SetRawAddress(pc);
222 m_sc.Clear(false);
223 m_flags.Reset(0);
224 ThreadSP thread_sp(GetThread());
225 if (thread_sp)
226 thread_sp->ClearStackFrames();
227 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230const char *StackFrame::Disassemble() {
231 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner8b3f2162017-02-28 17:59:59 +0000232 if (m_disassembly.Empty()) {
233 ExecutionContext exe_ctx(shared_from_this());
234 Target *target = exe_ctx.GetTargetPtr();
235 if (target) {
236 const char *plugin_name = nullptr;
237 const char *flavor = nullptr;
238 Disassembler::Disassemble(target->GetDebugger(),
239 target->GetArchitecture(), plugin_name, flavor,
240 exe_ctx, 0, false, 0, 0, m_disassembly);
241 }
242 if (m_disassembly.Empty())
243 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 }
Zachary Turner8b3f2162017-02-28 17:59:59 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249Block *StackFrame::GetFrameBlock() {
250 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
251 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 if (m_sc.block) {
254 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
255 if (inline_block) {
Adrian Prantl05097242018-04-30 16:49:04 +0000256 // Use the block with the inlined function info as the frame block we
257 // want this frame to have only the variables for the inlined function
258 // and its non-inlined block child blocks.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 return inline_block;
260 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000261 // This block is not contained within any inlined function blocks with so
262 // we want to use the top most function block.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 return &m_sc.function->GetBlock(false);
264 }
265 }
266 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000267}
268
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269// Get the symbol context if we already haven't done so by resolving the
270// PC address as much as possible. This way when we pass around a
Adrian Prantl05097242018-04-30 16:49:04 +0000271// StackFrame object, everyone will have as much information as possible and no
272// one will ever have to look things up manually.
Zachary Turner991e4452018-10-25 20:45:19 +0000273const SymbolContext &
274StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 std::lock_guard<std::recursive_mutex> guard(m_mutex);
276 // Copy our internal symbol context into "sc".
277 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
278 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 // If the target was requested add that:
281 if (!m_sc.target_sp) {
282 m_sc.target_sp = CalculateTarget();
283 if (m_sc.target_sp)
284 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 }
286
Adrian Prantl05097242018-04-30 16:49:04 +0000287 // Resolve our PC to section offset if we haven't already done so and if we
288 // don't have a module. The resolved address section will contain the
289 // module to which it belongs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
291 GetFrameCodeAddress();
292
Adrian Prantl05097242018-04-30 16:49:04 +0000293 // If this is not frame zero, then we need to subtract 1 from the PC value
294 // when doing address lookups since the PC will be on the instruction
295 // following the function call instruction...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296
297 Address lookup_addr(GetFrameCodeAddress());
Joseph Tremoulet31e6dbe2019-08-02 16:53:42 +0000298 if (!m_behaves_like_zeroth_frame && lookup_addr.IsValid()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 addr_t offset = lookup_addr.GetOffset();
300 if (offset > 0) {
301 lookup_addr.SetOffset(offset - 1);
302
303 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000304 // lookup_addr is the start of a section. We need do the math on the
305 // actual load address and re-compute the section. We're working with
306 // a 'noreturn' function at the end of a section.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307 ThreadSP thread_sp(GetThread());
308 if (thread_sp) {
309 TargetSP target_sp(thread_sp->CalculateTarget());
310 if (target_sp) {
311 addr_t addr_minus_one =
312 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
313 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
314 } else {
315 lookup_addr.SetOffset(offset - 1);
316 }
317 }
318 }
319 }
320
321 if (m_sc.module_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000322 // We have something in our stack frame symbol context, lets check if we
323 // haven't already tried to lookup one of those things. If we haven't
324 // then we will do the query.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325
Zachary Turner991e4452018-10-25 20:45:19 +0000326 SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327
328 if (resolve_scope & eSymbolContextCompUnit) {
329 if (m_flags.IsClear(eSymbolContextCompUnit)) {
330 if (m_sc.comp_unit)
331 resolved |= eSymbolContextCompUnit;
332 else
333 actual_resolve_scope |= eSymbolContextCompUnit;
334 }
335 }
336
337 if (resolve_scope & eSymbolContextFunction) {
338 if (m_flags.IsClear(eSymbolContextFunction)) {
339 if (m_sc.function)
340 resolved |= eSymbolContextFunction;
341 else
342 actual_resolve_scope |= eSymbolContextFunction;
343 }
344 }
345
346 if (resolve_scope & eSymbolContextBlock) {
347 if (m_flags.IsClear(eSymbolContextBlock)) {
348 if (m_sc.block)
349 resolved |= eSymbolContextBlock;
350 else
351 actual_resolve_scope |= eSymbolContextBlock;
352 }
353 }
354
355 if (resolve_scope & eSymbolContextSymbol) {
356 if (m_flags.IsClear(eSymbolContextSymbol)) {
357 if (m_sc.symbol)
358 resolved |= eSymbolContextSymbol;
359 else
360 actual_resolve_scope |= eSymbolContextSymbol;
361 }
362 }
363
364 if (resolve_scope & eSymbolContextLineEntry) {
365 if (m_flags.IsClear(eSymbolContextLineEntry)) {
366 if (m_sc.line_entry.IsValid())
367 resolved |= eSymbolContextLineEntry;
368 else
369 actual_resolve_scope |= eSymbolContextLineEntry;
370 }
371 }
372
373 if (actual_resolve_scope) {
Adrian Prantl05097242018-04-30 16:49:04 +0000374 // We might be resolving less information than what is already in our
375 // current symbol context so resolve into a temporary symbol context
376 // "sc" so we don't clear out data we have already found in "m_sc"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 SymbolContext sc;
378 // Set flags that indicate what we have tried to resolve
379 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
380 lookup_addr, actual_resolve_scope, sc);
Adrian Prantl05097242018-04-30 16:49:04 +0000381 // Only replace what we didn't already have as we may have information
382 // for an inlined function scope that won't match what a standard
383 // lookup by address would match
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
385 m_sc.comp_unit = sc.comp_unit;
386 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
387 m_sc.function = sc.function;
388 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
389 m_sc.block = sc.block;
390 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
391 m_sc.symbol = sc.symbol;
392 if ((resolved & eSymbolContextLineEntry) &&
393 !m_sc.line_entry.IsValid()) {
394 m_sc.line_entry = sc.line_entry;
395 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
396 }
397 }
398 } else {
399 // If we don't have a module, then we can't have the compile unit,
400 // function, block, line entry or symbol, so we can safely call
401 // ResolveSymbolContextForAddress with our symbol context member m_sc.
402 if (m_sc.target_sp) {
403 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
404 lookup_addr, resolve_scope, m_sc);
405 }
406 }
407
408 // Update our internal flags so we remember what we have tried to locate so
409 // we don't have to keep trying when more calls to this function are made.
Adrian Prantl05097242018-04-30 16:49:04 +0000410 // We might have dug up more information that was requested (for example if
411 // we were asked to only get the block, we will have gotten the compile
412 // unit, and function) so set any additional bits that we resolved
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 m_flags.Set(resolve_scope | resolved);
414 }
415
416 // Return the symbol context with everything that was possible to resolve
417 // resolved.
418 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419}
420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421VariableList *StackFrame::GetVariableList(bool get_file_globals) {
422 std::lock_guard<std::recursive_mutex> guard(m_mutex);
423 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
424 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 Block *frame_block = GetFrameBlock();
427
428 if (frame_block) {
429 const bool get_child_variables = true;
430 const bool can_create = true;
431 const bool stop_if_child_block_is_inlined_function = true;
Jonas Devlieghere796ac802019-02-11 23:13:08 +0000432 m_variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 frame_block->AppendBlockVariables(can_create, get_child_variables,
434 stop_if_child_block_is_inlined_function,
Zachary Turner3bc714b2017-03-02 00:05:25 +0000435 [](Variable *v) { return true; },
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000437 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 }
439
440 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
441 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
442
443 if (m_flags.IsClear(eSymbolContextCompUnit))
444 GetSymbolContext(eSymbolContextCompUnit);
445
446 if (m_sc.comp_unit) {
447 VariableListSP global_variable_list_sp(
448 m_sc.comp_unit->GetVariableList(true));
449 if (m_variable_list_sp)
450 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
451 else
452 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454 }
455
456 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457}
458
Greg Claytond41f0322011-08-02 23:35:43 +0000459VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460StackFrame::GetInScopeVariableList(bool get_file_globals,
461 bool must_have_valid_location) {
462 std::lock_guard<std::recursive_mutex> guard(m_mutex);
463 // We can't fetch variable information for a history stack frame.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000464 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 VariableListSP var_list_sp(new VariableList);
468 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000469
Kate Stoneb9c1b512016-09-06 20:57:50 +0000470 if (m_sc.block) {
471 const bool can_create = true;
472 const bool get_parent_variables = true;
473 const bool stop_if_block_is_inlined_function = true;
474 m_sc.block->AppendVariables(
475 can_create, get_parent_variables, stop_if_block_is_inlined_function,
476 [this, must_have_valid_location](Variable *v) {
477 return v->IsInScope(this) && (!must_have_valid_location ||
478 v->LocationIsValidForFrame(this));
479 },
480 var_list_sp.get());
481 }
482
483 if (m_sc.comp_unit && get_file_globals) {
484 VariableListSP global_variable_list_sp(
485 m_sc.comp_unit->GetVariableList(true));
486 if (global_variable_list_sp)
487 var_list_sp->AddVariables(global_variable_list_sp.get());
488 }
489
490 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000491}
492
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner0eb31a12016-11-17 05:14:32 +0000494 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
Zachary Turner97206d52017-05-12 04:51:55 +0000495 VariableSP &var_sp, Status &error) {
Zachary Turner0eb31a12016-11-17 05:14:32 +0000496 llvm::StringRef original_var_expr = var_expr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497 // We can't fetch variable information for a history stack frame.
Vedant Kumar4b36f792018-10-05 23:23:15 +0000498 if (IsHistorical())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000499 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
Zachary Turner0eb31a12016-11-17 05:14:32 +0000501 if (var_expr.empty()) {
502 error.SetErrorStringWithFormat("invalid variable path '%s'",
503 var_expr.str().c_str());
Zachary Turner24bd3172016-11-17 01:37:52 +0000504 return ValueObjectSP();
505 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506
Zachary Turner24bd3172016-11-17 01:37:52 +0000507 const bool check_ptr_vs_member =
508 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
509 const bool no_fragile_ivar =
510 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
511 const bool no_synth_child =
512 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
513 // const bool no_synth_array = (options &
514 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
515 error.Clear();
516 bool deref = false;
517 bool address_of = false;
518 ValueObjectSP valobj_sp;
519 const bool get_file_globals = true;
520 // When looking up a variable for an expression, we need only consider the
521 // variables that are in scope.
522 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
523 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524
Zachary Turner24bd3172016-11-17 01:37:52 +0000525 if (!variable_list)
526 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527
Zachary Turner24bd3172016-11-17 01:37:52 +0000528 // If first character is a '*', then show pointer contents
Zachary Turner24bd3172016-11-17 01:37:52 +0000529 std::string var_expr_storage;
530 if (var_expr[0] == '*') {
531 deref = true;
532 var_expr = var_expr.drop_front(); // Skip the '*'
533 } else if (var_expr[0] == '&') {
534 address_of = true;
535 var_expr = var_expr.drop_front(); // Skip the '&'
536 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537
Zachary Turner24bd3172016-11-17 01:37:52 +0000538 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
539 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540
Zachary Turner24bd3172016-11-17 01:37:52 +0000541 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000542
Zachary Turner24bd3172016-11-17 01:37:52 +0000543 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000544
Zachary Turner24bd3172016-11-17 01:37:52 +0000545 bool synthetically_added_instance_object = false;
546
547 if (var_sp) {
548 var_expr = var_expr.drop_front(name_const_string.GetLength());
549 }
550
551 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000552 // Check for direct ivars access which helps us with implicit access to
553 // ivars with the "this->" or "self->"
Zachary Turner24bd3172016-11-17 01:37:52 +0000554 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
555 lldb::LanguageType method_language = eLanguageTypeUnknown;
556 bool is_instance_method = false;
557 ConstString method_object_name;
558 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
559 method_object_name)) {
560 if (is_instance_method && method_object_name) {
561 var_sp = variable_list->FindVariable(method_object_name);
562 if (var_sp) {
563 separator_idx = 0;
564 var_expr_storage = "->";
565 var_expr_storage += var_expr;
566 var_expr = var_expr_storage;
567 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000568 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000569 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000570 }
571 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000572
Zachary Turner24bd3172016-11-17 01:37:52 +0000573 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
574 // Check if any anonymous unions are there which contain a variable with
575 // the name we need
576 for (size_t i = 0; i < variable_list->GetSize(); i++) {
577 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
578 if (!variable_sp)
579 continue;
580 if (!variable_sp->GetName().IsEmpty())
581 continue;
582
583 Type *var_type = variable_sp->GetType();
584 if (!var_type)
585 continue;
586
587 if (!var_type->GetForwardCompilerType().IsAnonymousType())
588 continue;
589 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
590 if (!valobj_sp)
591 return valobj_sp;
592 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
593 if (valobj_sp)
594 break;
595 }
596 }
597
598 if (var_sp && !valobj_sp) {
599 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
600 if (!valobj_sp)
601 return valobj_sp;
602 }
603 if (!valobj_sp) {
604 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
605 name_const_string.GetCString());
606 return ValueObjectSP();
607 }
608
609 // We are dumping at least one child
610 while (separator_idx != std::string::npos) {
611 // Calculate the next separator index ahead of time
612 ValueObjectSP child_valobj_sp;
613 const char separator_type = var_expr[0];
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000614 bool expr_is_ptr = false;
Zachary Turner24bd3172016-11-17 01:37:52 +0000615 switch (separator_type) {
616 case '-':
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000617 expr_is_ptr = true;
Zachary Turner24bd3172016-11-17 01:37:52 +0000618 if (var_expr.size() >= 2 && var_expr[1] != '>')
619 return ValueObjectSP();
620
621 if (no_fragile_ivar) {
622 // Make sure we aren't trying to deref an objective
623 // C ivar if this is not allowed
624 const uint32_t pointer_type_flags =
625 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
626 if ((pointer_type_flags & eTypeIsObjC) &&
627 (pointer_type_flags & eTypeIsPointer)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000628 // This was an objective C object pointer and it was requested we
629 // skip any fragile ivars so return nothing here
Zachary Turner24bd3172016-11-17 01:37:52 +0000630 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631 }
632 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000633
634 // If we have a non pointer type with a sythetic value then lets check if
635 // we have an sythetic dereference specified.
636 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000637 Status deref_error;
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000638 if (valobj_sp->GetCompilerType().IsReferenceType()) {
639 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
640 if (error.Fail()) {
641 error.SetErrorStringWithFormatv(
642 "Failed to dereference reference type: %s", deref_error);
643 return ValueObjectSP();
644 }
645 }
646
647 valobj_sp = valobj_sp->Dereference(deref_error);
648 if (error.Fail()) {
649 error.SetErrorStringWithFormatv(
Greg Clayton0d6f6812019-03-11 18:16:20 +0000650 "Failed to dereference sythetic value: {0}", deref_error);
651 return ValueObjectSP();
652 }
653 // Some synthetic plug-ins fail to set the error in Dereference
654 if (!valobj_sp) {
655 error.SetErrorString("Failed to dereference sythetic value");
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000656 return ValueObjectSP();
657 }
658 expr_is_ptr = false;
659 }
660
Zachary Turner24bd3172016-11-17 01:37:52 +0000661 var_expr = var_expr.drop_front(); // Remove the '-'
662 LLVM_FALLTHROUGH;
663 case '.': {
Zachary Turner24bd3172016-11-17 01:37:52 +0000664 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
665 separator_idx = var_expr.find_first_of(".-[");
666 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
667
668 if (check_ptr_vs_member) {
Adrian Prantl05097242018-04-30 16:49:04 +0000669 // We either have a pointer type and need to verify valobj_sp is a
670 // pointer, or we have a member of a class/union/struct being accessed
671 // with the . syntax and need to verify we don't have a pointer.
Zachary Turner24bd3172016-11-17 01:37:52 +0000672 const bool actual_is_ptr = valobj_sp->IsPointerType();
673
674 if (actual_is_ptr != expr_is_ptr) {
Adrian Prantl05097242018-04-30 16:49:04 +0000675 // Incorrect use of "." with a pointer, or "->" with a
676 // class/union/struct instance or reference.
Zachary Turner24bd3172016-11-17 01:37:52 +0000677 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
678 if (actual_is_ptr)
679 error.SetErrorStringWithFormat(
680 "\"%s\" is a pointer and . was used to attempt to access "
681 "\"%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 else
685 error.SetErrorStringWithFormat(
686 "\"%s\" is not a pointer and -> was used to attempt to "
687 "access \"%s\". Did you mean \"%s.%s\"?",
688 var_expr_path_strm.GetData(), child_name.GetCString(),
689 var_expr_path_strm.GetData(), var_expr.str().c_str());
690 return ValueObjectSP();
691 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000693 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
694 if (!child_valobj_sp) {
695 if (!no_synth_child) {
696 child_valobj_sp = valobj_sp->GetSyntheticValue();
697 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000698 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000699 child_valobj_sp->GetChildMemberWithName(child_name, true);
700 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701
Zachary Turner24bd3172016-11-17 01:37:52 +0000702 if (no_synth_child || !child_valobj_sp) {
703 // No child member with name "child_name"
704 if (synthetically_added_instance_object) {
705 // We added a "this->" or "self->" to the beginning of the
Adrian Prantl05097242018-04-30 16:49:04 +0000706 // expression and this is the first pointer ivar access, so just
707 // return the normal error
Zachary Turner24bd3172016-11-17 01:37:52 +0000708 error.SetErrorStringWithFormat(
709 "no variable or instance variable named '%s' found in "
710 "this frame",
711 name_const_string.GetCString());
712 } else {
713 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
714 if (child_name) {
715 error.SetErrorStringWithFormat(
716 "\"%s\" is not a member of \"(%s) %s\"",
717 child_name.GetCString(),
718 valobj_sp->GetTypeName().AsCString("<invalid type>"),
719 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 } else {
721 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000722 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000723 var_expr_path_strm.GetData(),
724 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000725 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000726 }
727 return ValueObjectSP();
728 }
729 }
730 synthetically_added_instance_object = false;
731 // Remove the child name from the path
732 var_expr = var_expr.drop_front(child_name.GetLength());
733 if (use_dynamic != eNoDynamicValues) {
734 ValueObjectSP dynamic_value_sp(
735 child_valobj_sp->GetDynamicValue(use_dynamic));
736 if (dynamic_value_sp)
737 child_valobj_sp = dynamic_value_sp;
738 }
739 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000740
Zachary Turner24bd3172016-11-17 01:37:52 +0000741 case '[': {
Adrian Prantl05097242018-04-30 16:49:04 +0000742 // Array member access, or treating pointer as an array Need at least two
743 // brackets and a number
Zachary Turner24bd3172016-11-17 01:37:52 +0000744 if (var_expr.size() <= 2) {
745 error.SetErrorStringWithFormat(
746 "invalid square bracket encountered after \"%s\" in \"%s\"",
747 var_expr_path_strm.GetData(), var_expr.str().c_str());
748 return ValueObjectSP();
749 }
750
751 // Drop the open brace.
752 var_expr = var_expr.drop_front();
753 long child_index = 0;
754
755 // If there's no closing brace, this is an invalid expression.
756 size_t end_pos = var_expr.find_first_of(']');
757 if (end_pos == llvm::StringRef::npos) {
758 error.SetErrorStringWithFormat(
759 "missing closing square bracket in expression \"%s\"",
760 var_expr_path_strm.GetData());
761 return ValueObjectSP();
762 }
763 llvm::StringRef index_expr = var_expr.take_front(end_pos);
764 llvm::StringRef original_index_expr = index_expr;
765 // Drop all of "[index_expr]"
766 var_expr = var_expr.drop_front(end_pos + 1);
767
768 if (index_expr.consumeInteger(0, child_index)) {
769 // If there was no integer anywhere in the index expression, this is
770 // erroneous expression.
771 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
772 index_expr.str().c_str());
773 return ValueObjectSP();
774 }
775
776 if (index_expr.empty()) {
777 // The entire index expression was a single integer.
778
779 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
780 // what we have is *ptr[low]. the most similar C++ syntax is to deref
781 // ptr and extract bit low out of it. reading array item low would be
782 // done by saying ptr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000783 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000784 ValueObjectSP temp(valobj_sp->Dereference(error));
785 if (error.Fail()) {
786 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
787 error.SetErrorStringWithFormat(
788 "could not dereference \"(%s) %s\"",
789 valobj_sp->GetTypeName().AsCString("<invalid type>"),
790 var_expr_path_strm.GetData());
791 return ValueObjectSP();
792 }
793 valobj_sp = temp;
794 deref = false;
795 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
796 deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000797 // what we have is *arr[low]. the most similar C++ syntax is to get
798 // arr[0] (an operation that is equivalent to deref-ing arr) and
799 // extract bit low out of it. reading array item low would be done by
800 // saying arr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000801 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000802 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
803 if (error.Fail()) {
804 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
805 error.SetErrorStringWithFormat(
806 "could not get item 0 for \"(%s) %s\"",
807 valobj_sp->GetTypeName().AsCString("<invalid type>"),
808 var_expr_path_strm.GetData());
809 return ValueObjectSP();
810 }
811 valobj_sp = temp;
812 deref = false;
813 }
814
815 bool is_incomplete_array = false;
816 if (valobj_sp->IsPointerType()) {
817 bool is_objc_pointer = true;
818
819 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
820 eLanguageTypeObjC)
821 is_objc_pointer = false;
822 else if (!valobj_sp->GetCompilerType().IsPointerType())
823 is_objc_pointer = false;
824
825 if (no_synth_child && is_objc_pointer) {
826 error.SetErrorStringWithFormat(
827 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
828 "subscripted",
829 valobj_sp->GetTypeName().AsCString("<invalid type>"),
830 var_expr_path_strm.GetData());
831
832 return ValueObjectSP();
833 } else if (is_objc_pointer) {
Adrian Prantl05097242018-04-30 16:49:04 +0000834 // dereferencing ObjC variables is not valid.. so let's try and
835 // recur to synthetic children
Zachary Turner24bd3172016-11-17 01:37:52 +0000836 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
837 if (!synthetic /* no synthetic */
838 || synthetic == valobj_sp) /* synthetic is the same as
839 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000840 {
841 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
842 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000843 "\"(%s) %s\" is not an array type",
844 valobj_sp->GetTypeName().AsCString("<invalid type>"),
845 var_expr_path_strm.GetData());
846 } else if (
847 static_cast<uint32_t>(child_index) >=
848 synthetic
849 ->GetNumChildren() /* synthetic does not have that many values */) {
850 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
851 error.SetErrorStringWithFormat(
852 "array index %ld is not valid for \"(%s) %s\"", child_index,
853 valobj_sp->GetTypeName().AsCString("<invalid type>"),
854 var_expr_path_strm.GetData());
855 } else {
856 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
857 if (!child_valobj_sp) {
858 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
859 error.SetErrorStringWithFormat(
860 "array index %ld is not valid for \"(%s) %s\"", child_index,
861 valobj_sp->GetTypeName().AsCString("<invalid type>"),
862 var_expr_path_strm.GetData());
863 }
864 }
865 } else {
866 child_valobj_sp =
867 valobj_sp->GetSyntheticArrayMember(child_index, true);
868 if (!child_valobj_sp) {
869 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
870 error.SetErrorStringWithFormat(
871 "failed to use pointer as array for index %ld for "
872 "\"(%s) %s\"",
873 child_index,
874 valobj_sp->GetTypeName().AsCString("<invalid type>"),
875 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876 }
877 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000878 } else if (valobj_sp->GetCompilerType().IsArrayType(
879 nullptr, nullptr, &is_incomplete_array)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000880 // Pass false to dynamic_value here so we can tell the difference
881 // between no dynamic value and no member of this type...
Zachary Turner24bd3172016-11-17 01:37:52 +0000882 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
883 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
884 child_valobj_sp =
885 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000886
Zachary Turner24bd3172016-11-17 01:37:52 +0000887 if (!child_valobj_sp) {
888 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
889 error.SetErrorStringWithFormat(
890 "array index %ld is not valid for \"(%s) %s\"", child_index,
891 valobj_sp->GetTypeName().AsCString("<invalid type>"),
892 var_expr_path_strm.GetData());
893 }
894 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
895 // this is a bitfield asking to display just one bit
896 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
897 child_index, child_index, true);
898 if (!child_valobj_sp) {
899 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
900 error.SetErrorStringWithFormat(
901 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
902 child_index, child_index,
903 valobj_sp->GetTypeName().AsCString("<invalid type>"),
904 var_expr_path_strm.GetData());
905 }
906 } else {
907 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
908 if (no_synth_child /* synthetic is forbidden */ ||
909 !synthetic /* no synthetic */
910 || synthetic == valobj_sp) /* synthetic is the same as the
911 original object */
912 {
913 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
914 error.SetErrorStringWithFormat(
915 "\"(%s) %s\" is not an array type",
916 valobj_sp->GetTypeName().AsCString("<invalid type>"),
917 var_expr_path_strm.GetData());
918 } else if (
919 static_cast<uint32_t>(child_index) >=
920 synthetic
921 ->GetNumChildren() /* synthetic does not have that many values */) {
922 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
923 error.SetErrorStringWithFormat(
924 "array index %ld is not valid for \"(%s) %s\"", child_index,
925 valobj_sp->GetTypeName().AsCString("<invalid type>"),
926 var_expr_path_strm.GetData());
927 } else {
928 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
929 if (!child_valobj_sp) {
930 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
931 error.SetErrorStringWithFormat(
932 "array index %ld is not valid for \"(%s) %s\"", child_index,
933 valobj_sp->GetTypeName().AsCString("<invalid type>"),
934 var_expr_path_strm.GetData());
935 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936 }
937 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000938
939 if (!child_valobj_sp) {
940 // Invalid array index...
941 return ValueObjectSP();
942 }
943
944 separator_idx = var_expr.find_first_of(".-[");
945 if (use_dynamic != eNoDynamicValues) {
946 ValueObjectSP dynamic_value_sp(
947 child_valobj_sp->GetDynamicValue(use_dynamic));
948 if (dynamic_value_sp)
949 child_valobj_sp = dynamic_value_sp;
950 }
951 // Break out early from the switch since we were able to find the child
952 // member
953 break;
954 }
955
956 // this is most probably a BitField, let's take a look
957 if (index_expr.front() != '-') {
958 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
959 original_index_expr.str().c_str());
960 return ValueObjectSP();
961 }
962
Zachary Turner7edc3a62016-11-21 23:18:13 +0000963 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000964 long final_index = 0;
965 if (index_expr.getAsInteger(0, final_index)) {
966 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
967 original_index_expr.str().c_str());
968 return ValueObjectSP();
969 }
970
971 // if the format given is [high-low], swap range
972 if (child_index > final_index) {
973 long temp = child_index;
974 child_index = final_index;
975 final_index = temp;
976 }
977
978 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
979 // what we have is *ptr[low-high]. the most similar C++ syntax is to
980 // deref ptr and extract bits low thru high out of it. reading array
Adrian Prantl05097242018-04-30 16:49:04 +0000981 // items low thru high would be done by saying ptr[low-high], without a
982 // deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000983 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000984 ValueObjectSP temp(valobj_sp->Dereference(error));
985 if (error.Fail()) {
986 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
987 error.SetErrorStringWithFormat(
988 "could not dereference \"(%s) %s\"",
989 valobj_sp->GetTypeName().AsCString("<invalid type>"),
990 var_expr_path_strm.GetData());
991 return ValueObjectSP();
992 }
993 valobj_sp = temp;
994 deref = false;
995 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000996 // what we have is *arr[low-high]. the most similar C++ syntax is to
997 // get arr[0] (an operation that is equivalent to deref-ing arr) and
998 // extract bits low thru high out of it. reading array items low thru
999 // high would be done by saying arr[low-high], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +00001000 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +00001001 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
1002 if (error.Fail()) {
1003 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1004 error.SetErrorStringWithFormat(
1005 "could not get item 0 for \"(%s) %s\"",
1006 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1007 var_expr_path_strm.GetData());
1008 return ValueObjectSP();
1009 }
1010 valobj_sp = temp;
1011 deref = false;
1012 }
1013
1014 child_valobj_sp =
1015 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1016 if (!child_valobj_sp) {
1017 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001018 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +00001019 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1020 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1021 var_expr_path_strm.GetData());
1022 }
1023
1024 if (!child_valobj_sp) {
1025 // Invalid bitfield range...
1026 return ValueObjectSP();
1027 }
1028
1029 separator_idx = var_expr.find_first_of(".-[");
1030 if (use_dynamic != eNoDynamicValues) {
1031 ValueObjectSP dynamic_value_sp(
1032 child_valobj_sp->GetDynamicValue(use_dynamic));
1033 if (dynamic_value_sp)
1034 child_valobj_sp = dynamic_value_sp;
1035 }
1036 // Break out early from the switch since we were able to find the child
1037 // member
1038 break;
1039 }
1040 default:
1041 // Failure...
1042 {
1043 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1044 error.SetErrorStringWithFormat(
1045 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1046 separator_type, var_expr_path_strm.GetData(),
1047 var_expr.str().c_str());
1048
1049 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001051 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001052
1053 if (child_valobj_sp)
1054 valobj_sp = child_valobj_sp;
1055
1056 if (var_expr.empty())
1057 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001058 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001059 if (valobj_sp) {
1060 if (deref) {
1061 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1062 valobj_sp = deref_valobj_sp;
1063 } else if (address_of) {
1064 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1065 valobj_sp = address_of_valobj_sp;
1066 }
1067 }
1068 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069}
1070
Zachary Turner97206d52017-05-12 04:51:55 +00001071bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001072 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1073 if (!m_cfa_is_valid) {
1074 m_frame_base_error.SetErrorString(
1075 "No frame base available for this historical stack frame.");
1076 return false;
1077 }
1078
1079 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1080 if (m_sc.function) {
1081 m_frame_base.Clear();
1082 m_frame_base_error.Clear();
1083
1084 m_flags.Set(GOT_FRAME_BASE);
1085 ExecutionContext exe_ctx(shared_from_this());
1086 Value expr_value;
1087 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1088 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1089 loclist_base_addr =
1090 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1091 exe_ctx.GetTargetPtr());
1092
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001093 if (!m_sc.function->GetFrameBaseExpression().Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001094 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
Jonas Devliegherea6682a42018-12-15 00:15:33 +00001095 expr_value, &m_frame_base_error)) {
Adrian Prantl05097242018-04-30 16:49:04 +00001096 // We should really have an error if evaluate returns, but in case we
1097 // don't, lets set the error to something at least.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 if (m_frame_base_error.Success())
1099 m_frame_base_error.SetErrorString(
1100 "Evaluation of the frame base expression failed.");
1101 } else {
1102 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1103 }
1104 } else {
1105 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001107 }
1108
1109 if (m_frame_base_error.Success())
1110 frame_base = m_frame_base;
1111
1112 if (error_ptr)
1113 *error_ptr = m_frame_base_error;
1114 return m_frame_base_error.Success();
1115}
1116
Zachary Turner97206d52017-05-12 04:51:55 +00001117DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 if (!m_sc.function) {
1119 if (error_ptr) {
1120 error_ptr->SetErrorString("No function in symbol context.");
1121 }
1122 return nullptr;
1123 }
1124
1125 return &m_sc.function->GetFrameBaseExpression();
1126}
1127
1128RegisterContextSP StackFrame::GetRegisterContext() {
1129 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1130 if (!m_reg_context_sp) {
1131 ThreadSP thread_sp(GetThread());
1132 if (thread_sp)
1133 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1134 }
1135 return m_reg_context_sp;
1136}
1137
1138bool StackFrame::HasDebugInformation() {
1139 GetSymbolContext(eSymbolContextLineEntry);
1140 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001141}
1142
1143ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001144StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1145 DynamicValueType use_dynamic) {
1146 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1147 ValueObjectSP valobj_sp;
Vedant Kumar4b36f792018-10-05 23:23:15 +00001148 if (IsHistorical()) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001149 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150 }
1151 VariableList *var_list = GetVariableList(true);
1152 if (var_list) {
1153 // Make sure the variable is a frame variable
1154 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1155 const uint32_t num_variables = var_list->GetSize();
1156 if (var_idx < num_variables) {
1157 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1158 if (!valobj_sp) {
1159 if (m_variable_list_value_objects.GetSize() < num_variables)
1160 m_variable_list_value_objects.Resize(num_variables);
1161 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1162 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1163 }
Enrico Granata592afe72016-03-15 21:50:51 +00001164 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165 }
1166 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1167 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1168 if (dynamic_sp)
1169 return dynamic_sp;
1170 }
1171 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001172}
1173
Kate Stoneb9c1b512016-09-06 20:57:50 +00001174ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1175 DynamicValueType use_dynamic) {
1176 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Vedant Kumar4b36f792018-10-05 23:23:15 +00001177 if (IsHistorical())
Kate Stoneb9c1b512016-09-06 20:57:50 +00001178 return ValueObjectSP();
1179
1180 // Check to make sure we aren't already tracking this variable?
1181 ValueObjectSP valobj_sp(
1182 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1183 if (!valobj_sp) {
1184 // We aren't already tracking this global
1185 VariableList *var_list = GetVariableList(true);
1186 // If this frame has no variables, create a new list
1187 if (var_list == nullptr)
Jonas Devlieghere796ac802019-02-11 23:13:08 +00001188 m_variable_list_sp = std::make_shared<VariableList>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189
1190 // Add the global/static variable to this frame
1191 m_variable_list_sp->AddVariable(variable_sp);
1192
1193 // Now make a value object for it so we can track its changes
1194 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1195 }
1196 return valobj_sp;
1197}
1198
1199bool StackFrame::IsInlined() {
1200 if (m_sc.block == nullptr)
1201 GetSymbolContext(eSymbolContextBlock);
1202 if (m_sc.block)
1203 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1204 return false;
1205}
1206
Vedant Kumar4b36f792018-10-05 23:23:15 +00001207bool StackFrame::IsHistorical() const {
1208 return m_stack_frame_kind == StackFrame::Kind::History;
1209}
1210
1211bool StackFrame::IsArtificial() const {
1212 return m_stack_frame_kind == StackFrame::Kind::Artificial;
1213}
1214
Kate Stoneb9c1b512016-09-06 20:57:50 +00001215lldb::LanguageType StackFrame::GetLanguage() {
1216 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1217 if (cu)
1218 return cu->GetLanguage();
1219 return lldb::eLanguageTypeUnknown;
1220}
1221
1222lldb::LanguageType StackFrame::GuessLanguage() {
1223 LanguageType lang_type = GetLanguage();
1224
1225 if (lang_type == eLanguageTypeUnknown) {
Jim Inghambdbdd222017-04-12 00:19:54 +00001226 SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1227 | eSymbolContextSymbol);
1228 if (sc.function) {
1229 lang_type = sc.function->GetMangled().GuessLanguage();
1230 }
1231 else if (sc.symbol)
1232 {
1233 lang_type = sc.symbol->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001234 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235 }
1236
1237 return lang_type;
1238}
1239
1240namespace {
1241std::pair<const Instruction::Operand *, int64_t>
1242GetBaseExplainingValue(const Instruction::Operand &operand,
1243 RegisterContext &register_context, lldb::addr_t value) {
1244 switch (operand.m_type) {
1245 case Instruction::Operand::Type::Dereference:
1246 case Instruction::Operand::Type::Immediate:
1247 case Instruction::Operand::Type::Invalid:
1248 case Instruction::Operand::Type::Product:
1249 // These are not currently interesting
1250 return std::make_pair(nullptr, 0);
1251 case Instruction::Operand::Type::Sum: {
1252 const Instruction::Operand *immediate_child = nullptr;
1253 const Instruction::Operand *variable_child = nullptr;
1254 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1255 immediate_child = &operand.m_children[0];
1256 variable_child = &operand.m_children[1];
1257 } else if (operand.m_children[1].m_type ==
1258 Instruction::Operand::Type::Immediate) {
1259 immediate_child = &operand.m_children[1];
1260 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001261 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262 if (!immediate_child) {
1263 return std::make_pair(nullptr, 0);
1264 }
1265 lldb::addr_t adjusted_value = value;
1266 if (immediate_child->m_negative) {
1267 adjusted_value += immediate_child->m_immediate;
1268 } else {
1269 adjusted_value -= immediate_child->m_immediate;
1270 }
1271 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1272 GetBaseExplainingValue(*variable_child, register_context,
1273 adjusted_value);
1274 if (!base_and_offset.first) {
1275 return std::make_pair(nullptr, 0);
1276 }
1277 if (immediate_child->m_negative) {
1278 base_and_offset.second -= immediate_child->m_immediate;
1279 } else {
1280 base_and_offset.second += immediate_child->m_immediate;
1281 }
1282 return base_and_offset;
1283 }
1284 case Instruction::Operand::Type::Register: {
1285 const RegisterInfo *info =
1286 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1287 if (!info) {
1288 return std::make_pair(nullptr, 0);
1289 }
1290 RegisterValue reg_value;
1291 if (!register_context.ReadRegister(info, reg_value)) {
1292 return std::make_pair(nullptr, 0);
1293 }
1294 if (reg_value.GetAsUInt64() == value) {
1295 return std::make_pair(&operand, 0);
1296 } else {
1297 return std::make_pair(nullptr, 0);
1298 }
1299 }
1300 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001301 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302}
1303
1304std::pair<const Instruction::Operand *, int64_t>
1305GetBaseExplainingDereference(const Instruction::Operand &operand,
1306 RegisterContext &register_context,
1307 lldb::addr_t addr) {
1308 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1309 return GetBaseExplainingValue(operand.m_children[0], register_context,
1310 addr);
1311 }
1312 return std::make_pair(nullptr, 0);
1313}
Ilia K4f730dc2016-09-12 05:25:33 +00001314}
Sean Callanan4740a732016-09-06 04:48:36 +00001315
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1317 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001318
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 const ArchSpec &target_arch = target_sp->GetArchitecture();
1320
1321 AddressRange pc_range;
1322 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1323 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1324
1325 ExecutionContext exe_ctx(shared_from_this());
1326
1327 const char *plugin_name = nullptr;
1328 const char *flavor = nullptr;
1329 const bool prefer_file_cache = false;
1330
1331 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1332 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1333
Jim Ingham99d1e282017-03-31 22:39:55 +00001334 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001335 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 }
Sean Callanan4740a732016-09-06 04:48:36 +00001337
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 InstructionSP instruction_sp =
1339 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1340
1341 llvm::SmallVector<Instruction::Operand, 3> operands;
1342
1343 if (!instruction_sp->ParseOperands(operands)) {
1344 return ValueObjectSP();
1345 }
1346
1347 RegisterContextSP register_context_sp = GetRegisterContext();
1348
1349 if (!register_context_sp) {
1350 return ValueObjectSP();
1351 }
1352
1353 for (const Instruction::Operand &operand : operands) {
1354 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1355 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1356
1357 if (!base_and_offset.first) {
1358 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001359 }
Sean Callanan4740a732016-09-06 04:48:36 +00001360
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 switch (base_and_offset.first->m_type) {
1362 case Instruction::Operand::Type::Immediate: {
1363 lldb_private::Address addr;
1364 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1365 base_and_offset.second,
1366 addr)) {
Alex Langford0e252e32019-07-30 22:12:34 +00001367 auto c_type_system_or_err =
1368 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
1369 if (auto err = c_type_system_or_err.takeError()) {
1370 LLDB_LOG_ERROR(
1371 lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD),
1372 std::move(err), "Unable to guess value for given address");
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 return ValueObjectSP();
1374 } else {
1375 CompilerType void_ptr_type =
Alex Langford0e252e32019-07-30 22:12:34 +00001376 c_type_system_or_err
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1378 .GetPointerType();
1379 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001380 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001382 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383 }
1384 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001385 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 case Instruction::Operand::Type::Register: {
1387 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1388 base_and_offset.second);
1389 }
1390 default:
1391 return ValueObjectSP();
1392 }
1393 }
1394
1395 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001396}
1397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398namespace {
1399ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1400 int64_t offset) {
1401 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1402 return ValueObjectSP();
1403 }
Sean Callanan4740a732016-09-06 04:48:36 +00001404
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405 if (parent->IsPointerOrReferenceType()) {
1406 return parent;
1407 }
1408
1409 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1410 const bool can_create = true;
1411 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1412
1413 if (!child_sp) {
1414 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001415 }
1416
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417 int64_t child_offset = child_sp->GetByteOffset();
1418 int64_t child_size = child_sp->GetByteSize();
1419
1420 if (offset >= child_offset && offset < (child_offset + child_size)) {
1421 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001422 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 }
Sean Callanan4740a732016-09-06 04:48:36 +00001424
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425 if (offset == 0) {
1426 return parent;
1427 } else {
1428 return ValueObjectSP();
1429 }
1430}
1431
1432ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1433 ValueObjectSP &base,
1434 int64_t offset) {
1435 // base is a pointer to something
Adrian Prantl05097242018-04-30 16:49:04 +00001436 // offset is the thing to add to the pointer We return the most sensible
1437 // ValueObject for the result of *(base+offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001438
1439 if (!base->IsPointerOrReferenceType()) {
1440 return ValueObjectSP();
1441 }
1442
Zachary Turner97206d52017-05-12 04:51:55 +00001443 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001445
1446 if (!pointee) {
1447 return ValueObjectSP();
1448 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001449
Ilia K4f730dc2016-09-12 05:25:33 +00001450 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001451 int64_t index = offset / pointee->GetByteSize();
1452 offset = offset % pointee->GetByteSize();
1453 const bool can_create = true;
1454 pointee = base->GetSyntheticArrayMember(index, can_create);
1455 }
1456
1457 if (!pointee || error.Fail()) {
1458 return ValueObjectSP();
1459 }
1460
1461 return GetValueForOffset(frame, pointee, offset);
1462}
1463
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464/// Attempt to reconstruct the ValueObject for the address contained in a
1465/// given register plus an offset.
1466///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001467/// \params [in] frame
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468/// The current stack frame.
1469///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001470/// \params [in] reg
Kate Stoneb9c1b512016-09-06 20:57:50 +00001471/// The register.
1472///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001473/// \params [in] offset
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474/// The offset from the register.
1475///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001476/// \param [in] disassembler
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477/// A disassembler containing instructions valid up to the current PC.
1478///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001479/// \param [in] variables
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480/// The variable list from the current frame,
1481///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001482/// \param [in] pc
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483/// The program counter for the instruction considered the 'user'.
1484///
Adrian Prantlf05b42e2019-03-11 17:09:29 +00001485/// \return
Kate Stoneb9c1b512016-09-06 20:57:50 +00001486/// A string describing the base for the ExpressionPath. This could be a
1487/// variable, a register value, an argument, or a function return value.
1488/// The ValueObject if found. If valid, it has a valid ExpressionPath.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1490 int64_t offset, Disassembler &disassembler,
1491 VariableList &variables, const Address &pc) {
1492 // Example of operation for Intel:
1493 //
1494 // +14: movq -0x8(%rbp), %rdi
1495 // +18: movq 0x8(%rdi), %rdi
1496 // +22: addl 0x4(%rdi), %eax
1497 //
1498 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1499 //
Adrian Prantl05097242018-04-30 16:49:04 +00001500 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1501 // +18 that assigns to rdi, and calls itself recursively for that dereference
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1503 // +14 that assigns to rdi, and calls itself recursively for that
1504 // derefernece
1505 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1506 // variable list.
1507 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1508 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1509 // at +18)
1510 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1511 // rdi+4 at +22)
1512
1513 // First, check the variable list to see if anything is at the specified
1514 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001515
Sean Callanan50857102016-09-14 00:48:19 +00001516 using namespace OperandMatchers;
1517
Sean Callanan0ac172d2016-09-14 20:29:57 +00001518 const RegisterInfo *reg_info =
1519 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1520 if (!reg_info) {
1521 return ValueObjectSP();
1522 }
1523
Sean Callanan807ee2f2016-09-13 21:18:27 +00001524 Instruction::Operand op =
1525 offset ? Instruction::Operand::BuildDereference(
1526 Instruction::Operand::BuildSum(
1527 Instruction::Operand::BuildRegister(reg),
1528 Instruction::Operand::BuildImmediate(offset)))
1529 : Instruction::Operand::BuildDereference(
1530 Instruction::Operand::BuildRegister(reg));
1531
Kate Stoneb9c1b512016-09-06 20:57:50 +00001532 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1533 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001534 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1535 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001536 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537 }
1538
Kate Stoneb9c1b512016-09-06 20:57:50 +00001539 const uint32_t current_inst =
1540 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1541 if (current_inst == UINT32_MAX) {
1542 return ValueObjectSP();
1543 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1546 // This is not an exact algorithm, and it sacrifices accuracy for
Adrian Prantl05097242018-04-30 16:49:04 +00001547 // generality. Recognizing "mov" and "ld" instructions –– and which
1548 // are their source and destination operands -- is something the
1549 // disassembler should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001550 InstructionSP instruction_sp =
1551 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552
Sean Callanan50857102016-09-14 00:48:19 +00001553 if (instruction_sp->IsCall()) {
1554 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1555 if (!abi_sp) {
1556 continue;
1557 }
1558
1559 const char *return_register_name;
1560 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1561 continue;
1562 }
1563
1564 const RegisterInfo *return_register_info =
1565 frame.GetRegisterContext()->GetRegisterInfoByName(
1566 return_register_name);
1567 if (!return_register_info) {
1568 continue;
1569 }
1570
1571 int64_t offset = 0;
1572
1573 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1574 MatchRegOp(*return_register_info))(op) &&
1575 !MatchUnaryOp(
1576 MatchOpType(Instruction::Operand::Type::Dereference),
1577 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1578 MatchRegOp(*return_register_info),
1579 FetchImmOp(offset)))(op)) {
1580 continue;
1581 }
1582
Kate Stoneb9c1b512016-09-06 20:57:50 +00001583 llvm::SmallVector<Instruction::Operand, 1> operands;
1584 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1585 continue;
1586 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 switch (operands[0].m_type) {
1589 default:
1590 break;
1591 case Instruction::Operand::Type::Immediate: {
1592 SymbolContext sc;
1593 Address load_address;
1594 if (!frame.CalculateTarget()->ResolveLoadAddress(
1595 operands[0].m_immediate, load_address)) {
1596 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001597 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1599 load_address, eSymbolContextFunction, sc);
1600 if (!sc.function) {
1601 break;
1602 }
1603 CompilerType function_type = sc.function->GetCompilerType();
1604 if (!function_type.IsFunctionType()) {
1605 break;
1606 }
1607 CompilerType return_type = function_type.GetFunctionReturnType();
1608 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001609 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001610 return_value)) {
1611 break;
1612 }
1613 std::string name_str(
1614 sc.function->GetName().AsCString("<unknown function>"));
1615 name_str.append("()");
1616 Address return_value_address(return_value.GetAsUInt64());
1617 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001618 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1620 }
1621 }
1622
1623 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001624 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625
1626 llvm::SmallVector<Instruction::Operand, 2> operands;
1627 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1628 continue;
1629 }
1630
Kate Stoneb9c1b512016-09-06 20:57:50 +00001631 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001632 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1633 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1634 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001635
1636 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001637 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001638 }
1639 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001641 }
1642 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643 continue;
1644 }
1645
1646 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001647 ValueObjectSP source_path;
1648 ConstString origin_register;
1649 int64_t origin_offset = 0;
1650
1651 if (FetchRegOp(origin_register)(*origin_operand)) {
1652 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1653 variables, instruction_sp->GetAddress());
1654 } else if (MatchUnaryOp(
1655 MatchOpType(Instruction::Operand::Type::Dereference),
1656 FetchRegOp(origin_register))(*origin_operand) ||
1657 MatchUnaryOp(
1658 MatchOpType(Instruction::Operand::Type::Dereference),
1659 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1660 FetchRegOp(origin_register),
1661 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001662 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001663 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001664 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001665 if (!source_path) {
1666 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001668 source_path =
1669 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670 }
1671
1672 if (source_path) {
1673 return source_path;
1674 }
1675 }
1676
1677 return ValueObjectSP();
1678}
1679}
1680
1681lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1682 int64_t offset) {
1683 TargetSP target_sp = CalculateTarget();
1684
1685 const ArchSpec &target_arch = target_sp->GetArchitecture();
1686
1687 Block *frame_block = GetFrameBlock();
1688
1689 if (!frame_block) {
1690 return ValueObjectSP();
1691 }
1692
1693 Function *function = frame_block->CalculateSymbolContextFunction();
1694 if (!function) {
1695 return ValueObjectSP();
1696 }
1697
1698 AddressRange pc_range = function->GetAddressRange();
1699
1700 if (GetFrameCodeAddress().GetFileAddress() <
1701 pc_range.GetBaseAddress().GetFileAddress() ||
1702 GetFrameCodeAddress().GetFileAddress() -
1703 pc_range.GetBaseAddress().GetFileAddress() >=
1704 pc_range.GetByteSize()) {
1705 return ValueObjectSP();
1706 }
1707
1708 ExecutionContext exe_ctx(shared_from_this());
1709
1710 const char *plugin_name = nullptr;
1711 const char *flavor = nullptr;
1712 const bool prefer_file_cache = false;
1713 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1714 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1715
1716 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1717 return ValueObjectSP();
1718 }
1719
1720 const bool get_file_globals = false;
1721 VariableList *variables = GetVariableList(get_file_globals);
1722
1723 if (!variables) {
1724 return ValueObjectSP();
1725 }
1726
1727 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1728 GetFrameCodeAddress());
1729}
1730
Shafik Yaghmoure23d0b62018-09-20 17:06:34 +00001731lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1732 ValueObjectSP value_sp;
1733
1734 if (!name)
1735 return value_sp;
1736
1737 TargetSP target_sp = CalculateTarget();
1738 ProcessSP process_sp = CalculateProcess();
1739
1740 if (!target_sp && !process_sp)
1741 return value_sp;
1742
1743 VariableList variable_list;
1744 VariableSP var_sp;
1745 SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1746
1747 if (sc.block) {
1748 const bool can_create = true;
1749 const bool get_parent_variables = true;
1750 const bool stop_if_block_is_inlined_function = true;
1751
1752 if (sc.block->AppendVariables(
1753 can_create, get_parent_variables, stop_if_block_is_inlined_function,
1754 [this](Variable *v) { return v->IsInScope(this); },
1755 &variable_list)) {
1756 var_sp = variable_list.FindVariable(name);
1757 }
1758
1759 if (var_sp)
1760 value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1761 }
1762
1763 return value_sp;
1764}
1765
Kate Stoneb9c1b512016-09-06 20:57:50 +00001766TargetSP StackFrame::CalculateTarget() {
1767 TargetSP target_sp;
1768 ThreadSP thread_sp(GetThread());
1769 if (thread_sp) {
1770 ProcessSP process_sp(thread_sp->CalculateProcess());
1771 if (process_sp)
1772 target_sp = process_sp->CalculateTarget();
1773 }
1774 return target_sp;
1775}
1776
1777ProcessSP StackFrame::CalculateProcess() {
1778 ProcessSP process_sp;
1779 ThreadSP thread_sp(GetThread());
1780 if (thread_sp)
1781 process_sp = thread_sp->CalculateProcess();
1782 return process_sp;
1783}
1784
1785ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1786
1787StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1788
1789void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1790 exe_ctx.SetContext(shared_from_this());
1791}
1792
Pavel Labath7f1c1212017-06-12 16:25:24 +00001793void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001794 const char *frame_marker) {
1795 if (strm == nullptr)
1796 return;
1797
1798 GetSymbolContext(eSymbolContextEverything);
1799 ExecutionContext exe_ctx(shared_from_this());
1800 StreamString s;
1801
1802 if (frame_marker)
1803 s.PutCString(frame_marker);
1804
1805 const FormatEntity::Entry *frame_format = nullptr;
1806 Target *target = exe_ctx.GetTargetPtr();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001807 if (target) {
1808 if (show_unique) {
1809 frame_format = target->GetDebugger().GetFrameFormatUnique();
1810 } else {
1811 frame_format = target->GetDebugger().GetFrameFormat();
1812 }
1813 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1815 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001816 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817 } else {
1818 Dump(strm, true, false);
1819 strm->EOL();
1820 }
1821}
1822
1823void StackFrame::Dump(Stream *strm, bool show_frame_index,
1824 bool show_fullpaths) {
1825 if (strm == nullptr)
1826 return;
1827
1828 if (show_frame_index)
1829 strm->Printf("frame #%u: ", m_frame_index);
1830 ExecutionContext exe_ctx(shared_from_this());
1831 Target *target = exe_ctx.GetTargetPtr();
1832 strm->Printf("0x%0*" PRIx64 " ",
1833 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1834 : 16,
1835 GetFrameCodeAddress().GetLoadAddress(target));
1836 GetSymbolContext(eSymbolContextEverything);
1837 const bool show_module = true;
1838 const bool show_inline = true;
1839 const bool show_function_arguments = true;
1840 const bool show_function_name = true;
1841 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1842 GetFrameCodeAddress(), show_fullpaths, show_module,
1843 show_inline, show_function_arguments,
1844 show_function_name);
1845}
1846
1847void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1848 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1849 assert(GetStackID() ==
1850 prev_frame.GetStackID()); // TODO: remove this after some testing
1851 m_variable_list_sp = prev_frame.m_variable_list_sp;
1852 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001853 if (!m_disassembly.GetString().empty()) {
1854 m_disassembly.Clear();
1855 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1856 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001857}
1858
1859void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1860 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1861 assert(GetStackID() ==
1862 curr_frame.GetStackID()); // TODO: remove this after some testing
1863 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1864 assert(GetThread() == curr_frame.GetThread());
1865 m_frame_index = curr_frame.m_frame_index;
1866 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1867 m_reg_context_sp = curr_frame.m_reg_context_sp;
1868 m_frame_code_addr = curr_frame.m_frame_code_addr;
1869 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1870 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1871 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1872 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1873 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1874 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1875 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1876 m_sc.function == curr_frame.m_sc.function);
1877 m_sc = curr_frame.m_sc;
1878 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1879 m_flags.Set(m_sc.GetResolvedMask());
1880 m_frame_base.Clear();
1881 m_frame_base_error.Clear();
1882}
1883
1884bool StackFrame::HasCachedData() const {
1885 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001886 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001887 if (m_variable_list_value_objects.GetSize() > 0)
1888 return true;
1889 if (!m_disassembly.GetString().empty())
1890 return true;
1891 return false;
1892}
1893
1894bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
Pavel Labath7f1c1212017-06-12 16:25:24 +00001895 bool show_unique, const char *frame_marker) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001896 if (show_frame_info) {
1897 strm.Indent();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001898 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001899 }
1900
1901 if (show_source) {
1902 ExecutionContext exe_ctx(shared_from_this());
1903 bool have_source = false, have_debuginfo = false;
1904 Debugger::StopDisassemblyType disasm_display =
1905 Debugger::eStopDisassemblyTypeNever;
1906 Target *target = exe_ctx.GetTargetPtr();
1907 if (target) {
1908 Debugger &debugger = target->GetDebugger();
1909 const uint32_t source_lines_before =
1910 debugger.GetStopSourceLineCount(true);
1911 const uint32_t source_lines_after =
1912 debugger.GetStopSourceLineCount(false);
1913 disasm_display = debugger.GetStopDisassemblyDisplay();
1914
1915 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1916 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1917 have_debuginfo = true;
1918 if (source_lines_before > 0 || source_lines_after > 0) {
1919 size_t num_lines =
1920 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1921 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001922 m_sc.line_entry.column, source_lines_before,
1923 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001924 if (num_lines != 0)
1925 have_source = true;
1926 // TODO: Give here a one time warning if source file is missing.
1927 }
1928 }
1929 switch (disasm_display) {
1930 case Debugger::eStopDisassemblyTypeNever:
1931 break;
1932
1933 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1934 if (have_debuginfo)
1935 break;
1936 LLVM_FALLTHROUGH;
1937
1938 case Debugger::eStopDisassemblyTypeNoSource:
1939 if (have_source)
1940 break;
1941 LLVM_FALLTHROUGH;
1942
1943 case Debugger::eStopDisassemblyTypeAlways:
1944 if (target) {
1945 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1946 if (disasm_lines > 0) {
1947 const ArchSpec &target_arch = target->GetArchitecture();
1948 AddressRange pc_range;
1949 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1950 pc_range.SetByteSize(disasm_lines *
1951 target_arch.GetMaximumOpcodeByteSize());
1952 const char *plugin_name = nullptr;
1953 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001954 const bool mixed_source_and_assembly = false;
1955 Disassembler::Disassemble(
1956 target->GetDebugger(), target_arch, plugin_name, flavor,
1957 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1958 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001959 }
1960 }
1961 break;
1962 }
1963 }
1964 }
1965 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001966}
Kuba Mracek41ae8e72018-10-31 04:00:22 +00001967
1968RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
1969 if (!m_recognized_frame_sp) {
1970 m_recognized_frame_sp =
1971 StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame());
1972 }
1973 return m_recognized_frame_sp;
1974}