blob: ce7aa0e68483ab44f3d0ac88c5e2aadafdb68ed0 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- StackFrame.cpp ------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkod70a6e72016-02-18 18:52:47 +000014#include "lldb/Target/StackFrame.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000015#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Disassembler.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000017#include "lldb/Core/FormatEntity.h"
Enrico Granata592afe72016-03-15 21:50:51 +000018#include "lldb/Core/Mangled.h"
19#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Value.h"
Greg Clayton54979cd2010-12-15 05:08:08 +000021#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan4740a732016-09-06 04:48:36 +000022#include "lldb/Core/ValueObjectMemory.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000023#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000026#include "lldb/Symbol/Symbol.h"
27#include "lldb/Symbol/SymbolContextScope.h"
Enrico Granata46252392015-11-19 22:28:58 +000028#include "lldb/Symbol/Type.h"
Greg Clayton288bdf92010-09-02 02:59:18 +000029#include "lldb/Symbol/VariableList.h"
Sean Callanan4740a732016-09-06 04:48:36 +000030#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
34#include "lldb/Target/Target.h"
35#include "lldb/Target/Thread.h"
Pavel Labathd821c992018-08-07 11:07:21 +000036#include "lldb/Utility/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41// The first bits in the flags are reserved for the SymbolContext::Scope bits
42// so we know if we have tried to look up information in our internal symbol
43// context (m_sc) already.
Kate Stoneb9c1b512016-09-06 20:57:50 +000044#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
45#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
46#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
47#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
48#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
51 user_id_t unwind_frame_index, addr_t cfa,
52 bool cfa_is_valid, addr_t pc, uint32_t stop_id,
53 bool stop_id_is_valid, bool is_history_frame,
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),
59 m_stop_id(stop_id), m_stop_id_is_valid(stop_id_is_valid),
60 m_is_history_frame(is_history_frame), m_variable_list_sp(),
61 m_variable_list_value_objects(), m_disassembly(), m_mutex() {
62 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 if (m_is_history_frame && !m_cfa_is_valid) {
66 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(),
83 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
84 m_stop_id_is_valid(false), m_is_history_frame(false),
85 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
86 m_mutex() {
87 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(),
109 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
110 m_stop_id_is_valid(false), m_is_history_frame(false),
111 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
112 m_mutex() {
113 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.
213 if (m_is_history_frame)
214 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//----------------------------------------------------------------------
264// Get the symbol context if we already haven't done so by resolving the
265// PC address as much as possible. This way when we pass around a
Adrian Prantl05097242018-04-30 16:49:04 +0000266// StackFrame object, everyone will have as much information as possible and no
267// one will ever have to look things up manually.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269const SymbolContext &StackFrame::GetSymbolContext(uint32_t resolve_scope) {
270 std::lock_guard<std::recursive_mutex> guard(m_mutex);
271 // Copy our internal symbol context into "sc".
272 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
273 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 // If the target was requested add that:
276 if (!m_sc.target_sp) {
277 m_sc.target_sp = CalculateTarget();
278 if (m_sc.target_sp)
279 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 }
281
Adrian Prantl05097242018-04-30 16:49:04 +0000282 // Resolve our PC to section offset if we haven't already done so and if we
283 // don't have a module. The resolved address section will contain the
284 // module to which it belongs
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
286 GetFrameCodeAddress();
287
Adrian Prantl05097242018-04-30 16:49:04 +0000288 // If this is not frame zero, then we need to subtract 1 from the PC value
289 // when doing address lookups since the PC will be on the instruction
290 // following the function call instruction...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291
292 Address lookup_addr(GetFrameCodeAddress());
293 if (m_frame_index > 0 && lookup_addr.IsValid()) {
294 addr_t offset = lookup_addr.GetOffset();
295 if (offset > 0) {
296 lookup_addr.SetOffset(offset - 1);
297
298 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000299 // lookup_addr is the start of a section. We need do the math on the
300 // actual load address and re-compute the section. We're working with
301 // a 'noreturn' function at the end of a section.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 ThreadSP thread_sp(GetThread());
303 if (thread_sp) {
304 TargetSP target_sp(thread_sp->CalculateTarget());
305 if (target_sp) {
306 addr_t addr_minus_one =
307 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
308 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
309 } else {
310 lookup_addr.SetOffset(offset - 1);
311 }
312 }
313 }
314 }
315
316 if (m_sc.module_sp) {
Adrian Prantl05097242018-04-30 16:49:04 +0000317 // We have something in our stack frame symbol context, lets check if we
318 // haven't already tried to lookup one of those things. If we haven't
319 // then we will do the query.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320
321 uint32_t actual_resolve_scope = 0;
322
323 if (resolve_scope & eSymbolContextCompUnit) {
324 if (m_flags.IsClear(eSymbolContextCompUnit)) {
325 if (m_sc.comp_unit)
326 resolved |= eSymbolContextCompUnit;
327 else
328 actual_resolve_scope |= eSymbolContextCompUnit;
329 }
330 }
331
332 if (resolve_scope & eSymbolContextFunction) {
333 if (m_flags.IsClear(eSymbolContextFunction)) {
334 if (m_sc.function)
335 resolved |= eSymbolContextFunction;
336 else
337 actual_resolve_scope |= eSymbolContextFunction;
338 }
339 }
340
341 if (resolve_scope & eSymbolContextBlock) {
342 if (m_flags.IsClear(eSymbolContextBlock)) {
343 if (m_sc.block)
344 resolved |= eSymbolContextBlock;
345 else
346 actual_resolve_scope |= eSymbolContextBlock;
347 }
348 }
349
350 if (resolve_scope & eSymbolContextSymbol) {
351 if (m_flags.IsClear(eSymbolContextSymbol)) {
352 if (m_sc.symbol)
353 resolved |= eSymbolContextSymbol;
354 else
355 actual_resolve_scope |= eSymbolContextSymbol;
356 }
357 }
358
359 if (resolve_scope & eSymbolContextLineEntry) {
360 if (m_flags.IsClear(eSymbolContextLineEntry)) {
361 if (m_sc.line_entry.IsValid())
362 resolved |= eSymbolContextLineEntry;
363 else
364 actual_resolve_scope |= eSymbolContextLineEntry;
365 }
366 }
367
368 if (actual_resolve_scope) {
Adrian Prantl05097242018-04-30 16:49:04 +0000369 // We might be resolving less information than what is already in our
370 // current symbol context so resolve into a temporary symbol context
371 // "sc" so we don't clear out data we have already found in "m_sc"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 SymbolContext sc;
373 // Set flags that indicate what we have tried to resolve
374 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
375 lookup_addr, actual_resolve_scope, sc);
Adrian Prantl05097242018-04-30 16:49:04 +0000376 // Only replace what we didn't already have as we may have information
377 // for an inlined function scope that won't match what a standard
378 // lookup by address would match
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
380 m_sc.comp_unit = sc.comp_unit;
381 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
382 m_sc.function = sc.function;
383 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
384 m_sc.block = sc.block;
385 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
386 m_sc.symbol = sc.symbol;
387 if ((resolved & eSymbolContextLineEntry) &&
388 !m_sc.line_entry.IsValid()) {
389 m_sc.line_entry = sc.line_entry;
390 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
391 }
392 }
393 } else {
394 // If we don't have a module, then we can't have the compile unit,
395 // function, block, line entry or symbol, so we can safely call
396 // ResolveSymbolContextForAddress with our symbol context member m_sc.
397 if (m_sc.target_sp) {
398 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
399 lookup_addr, resolve_scope, m_sc);
400 }
401 }
402
403 // Update our internal flags so we remember what we have tried to locate so
404 // we don't have to keep trying when more calls to this function are made.
Adrian Prantl05097242018-04-30 16:49:04 +0000405 // We might have dug up more information that was requested (for example if
406 // we were asked to only get the block, we will have gotten the compile
407 // unit, and function) so set any additional bits that we resolved
Kate Stoneb9c1b512016-09-06 20:57:50 +0000408 m_flags.Set(resolve_scope | resolved);
409 }
410
411 // Return the symbol context with everything that was possible to resolve
412 // resolved.
413 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416VariableList *StackFrame::GetVariableList(bool get_file_globals) {
417 std::lock_guard<std::recursive_mutex> guard(m_mutex);
418 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
419 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 Block *frame_block = GetFrameBlock();
422
423 if (frame_block) {
424 const bool get_child_variables = true;
425 const bool can_create = true;
426 const bool stop_if_child_block_is_inlined_function = true;
427 m_variable_list_sp.reset(new VariableList());
428 frame_block->AppendBlockVariables(can_create, get_child_variables,
429 stop_if_child_block_is_inlined_function,
Zachary Turner3bc714b2017-03-02 00:05:25 +0000430 [](Variable *v) { return true; },
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000432 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 }
434
435 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
436 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
437
438 if (m_flags.IsClear(eSymbolContextCompUnit))
439 GetSymbolContext(eSymbolContextCompUnit);
440
441 if (m_sc.comp_unit) {
442 VariableListSP global_variable_list_sp(
443 m_sc.comp_unit->GetVariableList(true));
444 if (m_variable_list_sp)
445 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
446 else
447 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 }
450
451 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
Greg Claytond41f0322011-08-02 23:35:43 +0000454VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455StackFrame::GetInScopeVariableList(bool get_file_globals,
456 bool must_have_valid_location) {
457 std::lock_guard<std::recursive_mutex> guard(m_mutex);
458 // We can't fetch variable information for a history stack frame.
459 if (m_is_history_frame)
460 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 VariableListSP var_list_sp(new VariableList);
463 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 if (m_sc.block) {
466 const bool can_create = true;
467 const bool get_parent_variables = true;
468 const bool stop_if_block_is_inlined_function = true;
469 m_sc.block->AppendVariables(
470 can_create, get_parent_variables, stop_if_block_is_inlined_function,
471 [this, must_have_valid_location](Variable *v) {
472 return v->IsInScope(this) && (!must_have_valid_location ||
473 v->LocationIsValidForFrame(this));
474 },
475 var_list_sp.get());
476 }
477
478 if (m_sc.comp_unit && get_file_globals) {
479 VariableListSP global_variable_list_sp(
480 m_sc.comp_unit->GetVariableList(true));
481 if (global_variable_list_sp)
482 var_list_sp->AddVariables(global_variable_list_sp.get());
483 }
484
485 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000486}
487
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner0eb31a12016-11-17 05:14:32 +0000489 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
Zachary Turner97206d52017-05-12 04:51:55 +0000490 VariableSP &var_sp, Status &error) {
Zachary Turner0eb31a12016-11-17 05:14:32 +0000491 llvm::StringRef original_var_expr = var_expr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 // We can't fetch variable information for a history stack frame.
493 if (m_is_history_frame)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000494 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495
Zachary Turner0eb31a12016-11-17 05:14:32 +0000496 if (var_expr.empty()) {
497 error.SetErrorStringWithFormat("invalid variable path '%s'",
498 var_expr.str().c_str());
Zachary Turner24bd3172016-11-17 01:37:52 +0000499 return ValueObjectSP();
500 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501
Zachary Turner24bd3172016-11-17 01:37:52 +0000502 const bool check_ptr_vs_member =
503 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
504 const bool no_fragile_ivar =
505 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
506 const bool no_synth_child =
507 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
508 // const bool no_synth_array = (options &
509 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
510 error.Clear();
511 bool deref = false;
512 bool address_of = false;
513 ValueObjectSP valobj_sp;
514 const bool get_file_globals = true;
515 // When looking up a variable for an expression, we need only consider the
516 // variables that are in scope.
517 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
518 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519
Zachary Turner24bd3172016-11-17 01:37:52 +0000520 if (!variable_list)
521 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522
Zachary Turner24bd3172016-11-17 01:37:52 +0000523 // If first character is a '*', then show pointer contents
Zachary Turner24bd3172016-11-17 01:37:52 +0000524 std::string var_expr_storage;
525 if (var_expr[0] == '*') {
526 deref = true;
527 var_expr = var_expr.drop_front(); // Skip the '*'
528 } else if (var_expr[0] == '&') {
529 address_of = true;
530 var_expr = var_expr.drop_front(); // Skip the '&'
531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532
Zachary Turner24bd3172016-11-17 01:37:52 +0000533 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
534 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535
Zachary Turner24bd3172016-11-17 01:37:52 +0000536 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537
Zachary Turner24bd3172016-11-17 01:37:52 +0000538 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539
Zachary Turner24bd3172016-11-17 01:37:52 +0000540 bool synthetically_added_instance_object = false;
541
542 if (var_sp) {
543 var_expr = var_expr.drop_front(name_const_string.GetLength());
544 }
545
546 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000547 // Check for direct ivars access which helps us with implicit access to
548 // ivars with the "this->" or "self->"
Zachary Turner24bd3172016-11-17 01:37:52 +0000549 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
550 lldb::LanguageType method_language = eLanguageTypeUnknown;
551 bool is_instance_method = false;
552 ConstString method_object_name;
553 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
554 method_object_name)) {
555 if (is_instance_method && method_object_name) {
556 var_sp = variable_list->FindVariable(method_object_name);
557 if (var_sp) {
558 separator_idx = 0;
559 var_expr_storage = "->";
560 var_expr_storage += var_expr;
561 var_expr = var_expr_storage;
562 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000565 }
566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567
Zachary Turner24bd3172016-11-17 01:37:52 +0000568 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
569 // Check if any anonymous unions are there which contain a variable with
570 // the name we need
571 for (size_t i = 0; i < variable_list->GetSize(); i++) {
572 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
573 if (!variable_sp)
574 continue;
575 if (!variable_sp->GetName().IsEmpty())
576 continue;
577
578 Type *var_type = variable_sp->GetType();
579 if (!var_type)
580 continue;
581
582 if (!var_type->GetForwardCompilerType().IsAnonymousType())
583 continue;
584 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
585 if (!valobj_sp)
586 return valobj_sp;
587 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
588 if (valobj_sp)
589 break;
590 }
591 }
592
593 if (var_sp && !valobj_sp) {
594 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
595 if (!valobj_sp)
596 return valobj_sp;
597 }
598 if (!valobj_sp) {
599 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
600 name_const_string.GetCString());
601 return ValueObjectSP();
602 }
603
604 // We are dumping at least one child
605 while (separator_idx != std::string::npos) {
606 // Calculate the next separator index ahead of time
607 ValueObjectSP child_valobj_sp;
608 const char separator_type = var_expr[0];
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000609 bool expr_is_ptr = false;
Zachary Turner24bd3172016-11-17 01:37:52 +0000610 switch (separator_type) {
611 case '-':
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000612 expr_is_ptr = true;
Zachary Turner24bd3172016-11-17 01:37:52 +0000613 if (var_expr.size() >= 2 && var_expr[1] != '>')
614 return ValueObjectSP();
615
616 if (no_fragile_ivar) {
617 // Make sure we aren't trying to deref an objective
618 // C ivar if this is not allowed
619 const uint32_t pointer_type_flags =
620 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
621 if ((pointer_type_flags & eTypeIsObjC) &&
622 (pointer_type_flags & eTypeIsPointer)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000623 // This was an objective C object pointer and it was requested we
624 // skip any fragile ivars so return nothing here
Zachary Turner24bd3172016-11-17 01:37:52 +0000625 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000626 }
627 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000628
629 // If we have a non pointer type with a sythetic value then lets check if
630 // we have an sythetic dereference specified.
631 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
Zachary Turner97206d52017-05-12 04:51:55 +0000632 Status deref_error;
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000633 if (valobj_sp->GetCompilerType().IsReferenceType()) {
634 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
635 if (error.Fail()) {
636 error.SetErrorStringWithFormatv(
637 "Failed to dereference reference type: %s", deref_error);
638 return ValueObjectSP();
639 }
640 }
641
642 valobj_sp = valobj_sp->Dereference(deref_error);
643 if (error.Fail()) {
644 error.SetErrorStringWithFormatv(
645 "Failed to dereference sythetic value: %s", deref_error);
646 return ValueObjectSP();
647 }
648 expr_is_ptr = false;
649 }
650
Zachary Turner24bd3172016-11-17 01:37:52 +0000651 var_expr = var_expr.drop_front(); // Remove the '-'
652 LLVM_FALLTHROUGH;
653 case '.': {
Zachary Turner24bd3172016-11-17 01:37:52 +0000654 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
655 separator_idx = var_expr.find_first_of(".-[");
656 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
657
658 if (check_ptr_vs_member) {
Adrian Prantl05097242018-04-30 16:49:04 +0000659 // We either have a pointer type and need to verify valobj_sp is a
660 // pointer, or we have a member of a class/union/struct being accessed
661 // with the . syntax and need to verify we don't have a pointer.
Zachary Turner24bd3172016-11-17 01:37:52 +0000662 const bool actual_is_ptr = valobj_sp->IsPointerType();
663
664 if (actual_is_ptr != expr_is_ptr) {
Adrian Prantl05097242018-04-30 16:49:04 +0000665 // Incorrect use of "." with a pointer, or "->" with a
666 // class/union/struct instance or reference.
Zachary Turner24bd3172016-11-17 01:37:52 +0000667 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
668 if (actual_is_ptr)
669 error.SetErrorStringWithFormat(
670 "\"%s\" is a pointer and . was used to attempt to access "
671 "\"%s\". Did you mean \"%s->%s\"?",
672 var_expr_path_strm.GetData(), child_name.GetCString(),
673 var_expr_path_strm.GetData(), var_expr.str().c_str());
674 else
675 error.SetErrorStringWithFormat(
676 "\"%s\" is not a pointer and -> was used to attempt to "
677 "access \"%s\". Did you mean \"%s.%s\"?",
678 var_expr_path_strm.GetData(), child_name.GetCString(),
679 var_expr_path_strm.GetData(), var_expr.str().c_str());
680 return ValueObjectSP();
681 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000682 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000683 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
684 if (!child_valobj_sp) {
685 if (!no_synth_child) {
686 child_valobj_sp = valobj_sp->GetSyntheticValue();
687 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000689 child_valobj_sp->GetChildMemberWithName(child_name, true);
690 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691
Zachary Turner24bd3172016-11-17 01:37:52 +0000692 if (no_synth_child || !child_valobj_sp) {
693 // No child member with name "child_name"
694 if (synthetically_added_instance_object) {
695 // We added a "this->" or "self->" to the beginning of the
Adrian Prantl05097242018-04-30 16:49:04 +0000696 // expression and this is the first pointer ivar access, so just
697 // return the normal error
Zachary Turner24bd3172016-11-17 01:37:52 +0000698 error.SetErrorStringWithFormat(
699 "no variable or instance variable named '%s' found in "
700 "this frame",
701 name_const_string.GetCString());
702 } else {
703 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
704 if (child_name) {
705 error.SetErrorStringWithFormat(
706 "\"%s\" is not a member of \"(%s) %s\"",
707 child_name.GetCString(),
708 valobj_sp->GetTypeName().AsCString("<invalid type>"),
709 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 } else {
711 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000712 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000713 var_expr_path_strm.GetData(),
714 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000716 }
717 return ValueObjectSP();
718 }
719 }
720 synthetically_added_instance_object = false;
721 // Remove the child name from the path
722 var_expr = var_expr.drop_front(child_name.GetLength());
723 if (use_dynamic != eNoDynamicValues) {
724 ValueObjectSP dynamic_value_sp(
725 child_valobj_sp->GetDynamicValue(use_dynamic));
726 if (dynamic_value_sp)
727 child_valobj_sp = dynamic_value_sp;
728 }
729 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730
Zachary Turner24bd3172016-11-17 01:37:52 +0000731 case '[': {
Adrian Prantl05097242018-04-30 16:49:04 +0000732 // Array member access, or treating pointer as an array Need at least two
733 // brackets and a number
Zachary Turner24bd3172016-11-17 01:37:52 +0000734 if (var_expr.size() <= 2) {
735 error.SetErrorStringWithFormat(
736 "invalid square bracket encountered after \"%s\" in \"%s\"",
737 var_expr_path_strm.GetData(), var_expr.str().c_str());
738 return ValueObjectSP();
739 }
740
741 // Drop the open brace.
742 var_expr = var_expr.drop_front();
743 long child_index = 0;
744
745 // If there's no closing brace, this is an invalid expression.
746 size_t end_pos = var_expr.find_first_of(']');
747 if (end_pos == llvm::StringRef::npos) {
748 error.SetErrorStringWithFormat(
749 "missing closing square bracket in expression \"%s\"",
750 var_expr_path_strm.GetData());
751 return ValueObjectSP();
752 }
753 llvm::StringRef index_expr = var_expr.take_front(end_pos);
754 llvm::StringRef original_index_expr = index_expr;
755 // Drop all of "[index_expr]"
756 var_expr = var_expr.drop_front(end_pos + 1);
757
758 if (index_expr.consumeInteger(0, child_index)) {
759 // If there was no integer anywhere in the index expression, this is
760 // erroneous expression.
761 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
762 index_expr.str().c_str());
763 return ValueObjectSP();
764 }
765
766 if (index_expr.empty()) {
767 // The entire index expression was a single integer.
768
769 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
770 // what we have is *ptr[low]. the most similar C++ syntax is to deref
771 // ptr and extract bit low out of it. reading array item low would be
772 // done by saying ptr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000773 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000774 ValueObjectSP temp(valobj_sp->Dereference(error));
775 if (error.Fail()) {
776 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
777 error.SetErrorStringWithFormat(
778 "could not dereference \"(%s) %s\"",
779 valobj_sp->GetTypeName().AsCString("<invalid type>"),
780 var_expr_path_strm.GetData());
781 return ValueObjectSP();
782 }
783 valobj_sp = temp;
784 deref = false;
785 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
786 deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000787 // what we have is *arr[low]. the most similar C++ syntax is to get
788 // arr[0] (an operation that is equivalent to deref-ing arr) and
789 // extract bit low out of it. reading array item low would be done by
790 // saying arr[low], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000791 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000792 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
793 if (error.Fail()) {
794 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
795 error.SetErrorStringWithFormat(
796 "could not get item 0 for \"(%s) %s\"",
797 valobj_sp->GetTypeName().AsCString("<invalid type>"),
798 var_expr_path_strm.GetData());
799 return ValueObjectSP();
800 }
801 valobj_sp = temp;
802 deref = false;
803 }
804
805 bool is_incomplete_array = false;
806 if (valobj_sp->IsPointerType()) {
807 bool is_objc_pointer = true;
808
809 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
810 eLanguageTypeObjC)
811 is_objc_pointer = false;
812 else if (!valobj_sp->GetCompilerType().IsPointerType())
813 is_objc_pointer = false;
814
815 if (no_synth_child && is_objc_pointer) {
816 error.SetErrorStringWithFormat(
817 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
818 "subscripted",
819 valobj_sp->GetTypeName().AsCString("<invalid type>"),
820 var_expr_path_strm.GetData());
821
822 return ValueObjectSP();
823 } else if (is_objc_pointer) {
Adrian Prantl05097242018-04-30 16:49:04 +0000824 // dereferencing ObjC variables is not valid.. so let's try and
825 // recur to synthetic children
Zachary Turner24bd3172016-11-17 01:37:52 +0000826 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
827 if (!synthetic /* no synthetic */
828 || synthetic == valobj_sp) /* synthetic is the same as
829 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000830 {
831 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
832 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000833 "\"(%s) %s\" is not an array type",
834 valobj_sp->GetTypeName().AsCString("<invalid type>"),
835 var_expr_path_strm.GetData());
836 } else if (
837 static_cast<uint32_t>(child_index) >=
838 synthetic
839 ->GetNumChildren() /* synthetic does not have that many values */) {
840 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
841 error.SetErrorStringWithFormat(
842 "array index %ld is not valid for \"(%s) %s\"", child_index,
843 valobj_sp->GetTypeName().AsCString("<invalid type>"),
844 var_expr_path_strm.GetData());
845 } else {
846 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
847 if (!child_valobj_sp) {
848 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
849 error.SetErrorStringWithFormat(
850 "array index %ld is not valid for \"(%s) %s\"", child_index,
851 valobj_sp->GetTypeName().AsCString("<invalid type>"),
852 var_expr_path_strm.GetData());
853 }
854 }
855 } else {
856 child_valobj_sp =
857 valobj_sp->GetSyntheticArrayMember(child_index, true);
858 if (!child_valobj_sp) {
859 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
860 error.SetErrorStringWithFormat(
861 "failed to use pointer as array for index %ld for "
862 "\"(%s) %s\"",
863 child_index,
864 valobj_sp->GetTypeName().AsCString("<invalid type>"),
865 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000866 }
867 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000868 } else if (valobj_sp->GetCompilerType().IsArrayType(
869 nullptr, nullptr, &is_incomplete_array)) {
Adrian Prantl05097242018-04-30 16:49:04 +0000870 // Pass false to dynamic_value here so we can tell the difference
871 // between no dynamic value and no member of this type...
Zachary Turner24bd3172016-11-17 01:37:52 +0000872 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
873 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
874 child_valobj_sp =
875 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000876
Zachary Turner24bd3172016-11-17 01:37:52 +0000877 if (!child_valobj_sp) {
878 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
879 error.SetErrorStringWithFormat(
880 "array index %ld is not valid for \"(%s) %s\"", child_index,
881 valobj_sp->GetTypeName().AsCString("<invalid type>"),
882 var_expr_path_strm.GetData());
883 }
884 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
885 // this is a bitfield asking to display just one bit
886 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
887 child_index, child_index, true);
888 if (!child_valobj_sp) {
889 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
890 error.SetErrorStringWithFormat(
891 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
892 child_index, child_index,
893 valobj_sp->GetTypeName().AsCString("<invalid type>"),
894 var_expr_path_strm.GetData());
895 }
896 } else {
897 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
898 if (no_synth_child /* synthetic is forbidden */ ||
899 !synthetic /* no synthetic */
900 || synthetic == valobj_sp) /* synthetic is the same as the
901 original object */
902 {
903 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
904 error.SetErrorStringWithFormat(
905 "\"(%s) %s\" is not an array type",
906 valobj_sp->GetTypeName().AsCString("<invalid type>"),
907 var_expr_path_strm.GetData());
908 } else if (
909 static_cast<uint32_t>(child_index) >=
910 synthetic
911 ->GetNumChildren() /* synthetic does not have that many values */) {
912 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
913 error.SetErrorStringWithFormat(
914 "array index %ld is not valid for \"(%s) %s\"", child_index,
915 valobj_sp->GetTypeName().AsCString("<invalid type>"),
916 var_expr_path_strm.GetData());
917 } else {
918 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
919 if (!child_valobj_sp) {
920 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
921 error.SetErrorStringWithFormat(
922 "array index %ld is not valid for \"(%s) %s\"", child_index,
923 valobj_sp->GetTypeName().AsCString("<invalid type>"),
924 var_expr_path_strm.GetData());
925 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000926 }
927 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000928
929 if (!child_valobj_sp) {
930 // Invalid array index...
931 return ValueObjectSP();
932 }
933
934 separator_idx = var_expr.find_first_of(".-[");
935 if (use_dynamic != eNoDynamicValues) {
936 ValueObjectSP dynamic_value_sp(
937 child_valobj_sp->GetDynamicValue(use_dynamic));
938 if (dynamic_value_sp)
939 child_valobj_sp = dynamic_value_sp;
940 }
941 // Break out early from the switch since we were able to find the child
942 // member
943 break;
944 }
945
946 // this is most probably a BitField, let's take a look
947 if (index_expr.front() != '-') {
948 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
949 original_index_expr.str().c_str());
950 return ValueObjectSP();
951 }
952
Zachary Turner7edc3a62016-11-21 23:18:13 +0000953 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000954 long final_index = 0;
955 if (index_expr.getAsInteger(0, final_index)) {
956 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
957 original_index_expr.str().c_str());
958 return ValueObjectSP();
959 }
960
961 // if the format given is [high-low], swap range
962 if (child_index > final_index) {
963 long temp = child_index;
964 child_index = final_index;
965 final_index = temp;
966 }
967
968 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
969 // what we have is *ptr[low-high]. the most similar C++ syntax is to
970 // deref ptr and extract bits low thru high out of it. reading array
Adrian Prantl05097242018-04-30 16:49:04 +0000971 // items low thru high would be done by saying ptr[low-high], without a
972 // deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000973 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000974 ValueObjectSP temp(valobj_sp->Dereference(error));
975 if (error.Fail()) {
976 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
977 error.SetErrorStringWithFormat(
978 "could not dereference \"(%s) %s\"",
979 valobj_sp->GetTypeName().AsCString("<invalid type>"),
980 var_expr_path_strm.GetData());
981 return ValueObjectSP();
982 }
983 valobj_sp = temp;
984 deref = false;
985 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
Adrian Prantl05097242018-04-30 16:49:04 +0000986 // what we have is *arr[low-high]. the most similar C++ syntax is to
987 // get arr[0] (an operation that is equivalent to deref-ing arr) and
988 // extract bits low thru high out of it. reading array items low thru
989 // high would be done by saying arr[low-high], without a deref * sign
Zachary Turner97206d52017-05-12 04:51:55 +0000990 Status error;
Zachary Turner24bd3172016-11-17 01:37:52 +0000991 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
992 if (error.Fail()) {
993 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
994 error.SetErrorStringWithFormat(
995 "could not get item 0 for \"(%s) %s\"",
996 valobj_sp->GetTypeName().AsCString("<invalid type>"),
997 var_expr_path_strm.GetData());
998 return ValueObjectSP();
999 }
1000 valobj_sp = temp;
1001 deref = false;
1002 }
1003
1004 child_valobj_sp =
1005 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1006 if (!child_valobj_sp) {
1007 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001008 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +00001009 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1010 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1011 var_expr_path_strm.GetData());
1012 }
1013
1014 if (!child_valobj_sp) {
1015 // Invalid bitfield range...
1016 return ValueObjectSP();
1017 }
1018
1019 separator_idx = var_expr.find_first_of(".-[");
1020 if (use_dynamic != eNoDynamicValues) {
1021 ValueObjectSP dynamic_value_sp(
1022 child_valobj_sp->GetDynamicValue(use_dynamic));
1023 if (dynamic_value_sp)
1024 child_valobj_sp = dynamic_value_sp;
1025 }
1026 // Break out early from the switch since we were able to find the child
1027 // member
1028 break;
1029 }
1030 default:
1031 // Failure...
1032 {
1033 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1034 error.SetErrorStringWithFormat(
1035 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1036 separator_type, var_expr_path_strm.GetData(),
1037 var_expr.str().c_str());
1038
1039 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001041 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001042
1043 if (child_valobj_sp)
1044 valobj_sp = child_valobj_sp;
1045
1046 if (var_expr.empty())
1047 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001049 if (valobj_sp) {
1050 if (deref) {
1051 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1052 valobj_sp = deref_valobj_sp;
1053 } else if (address_of) {
1054 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1055 valobj_sp = address_of_valobj_sp;
1056 }
1057 }
1058 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059}
1060
Zachary Turner97206d52017-05-12 04:51:55 +00001061bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1063 if (!m_cfa_is_valid) {
1064 m_frame_base_error.SetErrorString(
1065 "No frame base available for this historical stack frame.");
1066 return false;
1067 }
1068
1069 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1070 if (m_sc.function) {
1071 m_frame_base.Clear();
1072 m_frame_base_error.Clear();
1073
1074 m_flags.Set(GOT_FRAME_BASE);
1075 ExecutionContext exe_ctx(shared_from_this());
1076 Value expr_value;
1077 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1078 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1079 loclist_base_addr =
1080 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1081 exe_ctx.GetTargetPtr());
1082
1083 if (m_sc.function->GetFrameBaseExpression().Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001084 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
1085 expr_value, &m_frame_base_error) == false) {
Adrian Prantl05097242018-04-30 16:49:04 +00001086 // We should really have an error if evaluate returns, but in case we
1087 // don't, lets set the error to something at least.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001088 if (m_frame_base_error.Success())
1089 m_frame_base_error.SetErrorString(
1090 "Evaluation of the frame base expression failed.");
1091 } else {
1092 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1093 }
1094 } else {
1095 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001096 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 }
1098
1099 if (m_frame_base_error.Success())
1100 frame_base = m_frame_base;
1101
1102 if (error_ptr)
1103 *error_ptr = m_frame_base_error;
1104 return m_frame_base_error.Success();
1105}
1106
Zachary Turner97206d52017-05-12 04:51:55 +00001107DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001108 if (!m_sc.function) {
1109 if (error_ptr) {
1110 error_ptr->SetErrorString("No function in symbol context.");
1111 }
1112 return nullptr;
1113 }
1114
1115 return &m_sc.function->GetFrameBaseExpression();
1116}
1117
1118RegisterContextSP StackFrame::GetRegisterContext() {
1119 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1120 if (!m_reg_context_sp) {
1121 ThreadSP thread_sp(GetThread());
1122 if (thread_sp)
1123 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1124 }
1125 return m_reg_context_sp;
1126}
1127
1128bool StackFrame::HasDebugInformation() {
1129 GetSymbolContext(eSymbolContextLineEntry);
1130 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001131}
1132
1133ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1135 DynamicValueType use_dynamic) {
1136 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1137 ValueObjectSP valobj_sp;
1138 if (m_is_history_frame) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001139 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 }
1141 VariableList *var_list = GetVariableList(true);
1142 if (var_list) {
1143 // Make sure the variable is a frame variable
1144 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1145 const uint32_t num_variables = var_list->GetSize();
1146 if (var_idx < num_variables) {
1147 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1148 if (!valobj_sp) {
1149 if (m_variable_list_value_objects.GetSize() < num_variables)
1150 m_variable_list_value_objects.Resize(num_variables);
1151 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1152 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1153 }
Enrico Granata592afe72016-03-15 21:50:51 +00001154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001155 }
1156 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1157 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1158 if (dynamic_sp)
1159 return dynamic_sp;
1160 }
1161 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001162}
1163
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1165 DynamicValueType use_dynamic) {
1166 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1167 if (m_is_history_frame)
1168 return ValueObjectSP();
1169
1170 // Check to make sure we aren't already tracking this variable?
1171 ValueObjectSP valobj_sp(
1172 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1173 if (!valobj_sp) {
1174 // We aren't already tracking this global
1175 VariableList *var_list = GetVariableList(true);
1176 // If this frame has no variables, create a new list
1177 if (var_list == nullptr)
1178 m_variable_list_sp.reset(new VariableList());
1179
1180 // Add the global/static variable to this frame
1181 m_variable_list_sp->AddVariable(variable_sp);
1182
1183 // Now make a value object for it so we can track its changes
1184 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1185 }
1186 return valobj_sp;
1187}
1188
1189bool StackFrame::IsInlined() {
1190 if (m_sc.block == nullptr)
1191 GetSymbolContext(eSymbolContextBlock);
1192 if (m_sc.block)
1193 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1194 return false;
1195}
1196
1197lldb::LanguageType StackFrame::GetLanguage() {
1198 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1199 if (cu)
1200 return cu->GetLanguage();
1201 return lldb::eLanguageTypeUnknown;
1202}
1203
1204lldb::LanguageType StackFrame::GuessLanguage() {
1205 LanguageType lang_type = GetLanguage();
1206
1207 if (lang_type == eLanguageTypeUnknown) {
Jim Inghambdbdd222017-04-12 00:19:54 +00001208 SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1209 | eSymbolContextSymbol);
1210 if (sc.function) {
1211 lang_type = sc.function->GetMangled().GuessLanguage();
1212 }
1213 else if (sc.symbol)
1214 {
1215 lang_type = sc.symbol->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001216 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001217 }
1218
1219 return lang_type;
1220}
1221
1222namespace {
1223std::pair<const Instruction::Operand *, int64_t>
1224GetBaseExplainingValue(const Instruction::Operand &operand,
1225 RegisterContext &register_context, lldb::addr_t value) {
1226 switch (operand.m_type) {
1227 case Instruction::Operand::Type::Dereference:
1228 case Instruction::Operand::Type::Immediate:
1229 case Instruction::Operand::Type::Invalid:
1230 case Instruction::Operand::Type::Product:
1231 // These are not currently interesting
1232 return std::make_pair(nullptr, 0);
1233 case Instruction::Operand::Type::Sum: {
1234 const Instruction::Operand *immediate_child = nullptr;
1235 const Instruction::Operand *variable_child = nullptr;
1236 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1237 immediate_child = &operand.m_children[0];
1238 variable_child = &operand.m_children[1];
1239 } else if (operand.m_children[1].m_type ==
1240 Instruction::Operand::Type::Immediate) {
1241 immediate_child = &operand.m_children[1];
1242 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001243 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 if (!immediate_child) {
1245 return std::make_pair(nullptr, 0);
1246 }
1247 lldb::addr_t adjusted_value = value;
1248 if (immediate_child->m_negative) {
1249 adjusted_value += immediate_child->m_immediate;
1250 } else {
1251 adjusted_value -= immediate_child->m_immediate;
1252 }
1253 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1254 GetBaseExplainingValue(*variable_child, register_context,
1255 adjusted_value);
1256 if (!base_and_offset.first) {
1257 return std::make_pair(nullptr, 0);
1258 }
1259 if (immediate_child->m_negative) {
1260 base_and_offset.second -= immediate_child->m_immediate;
1261 } else {
1262 base_and_offset.second += immediate_child->m_immediate;
1263 }
1264 return base_and_offset;
1265 }
1266 case Instruction::Operand::Type::Register: {
1267 const RegisterInfo *info =
1268 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1269 if (!info) {
1270 return std::make_pair(nullptr, 0);
1271 }
1272 RegisterValue reg_value;
1273 if (!register_context.ReadRegister(info, reg_value)) {
1274 return std::make_pair(nullptr, 0);
1275 }
1276 if (reg_value.GetAsUInt64() == value) {
1277 return std::make_pair(&operand, 0);
1278 } else {
1279 return std::make_pair(nullptr, 0);
1280 }
1281 }
1282 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001283 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284}
1285
1286std::pair<const Instruction::Operand *, int64_t>
1287GetBaseExplainingDereference(const Instruction::Operand &operand,
1288 RegisterContext &register_context,
1289 lldb::addr_t addr) {
1290 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1291 return GetBaseExplainingValue(operand.m_children[0], register_context,
1292 addr);
1293 }
1294 return std::make_pair(nullptr, 0);
1295}
Ilia K4f730dc2016-09-12 05:25:33 +00001296}
Sean Callanan4740a732016-09-06 04:48:36 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1299 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 const ArchSpec &target_arch = target_sp->GetArchitecture();
1302
1303 AddressRange pc_range;
1304 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1305 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1306
1307 ExecutionContext exe_ctx(shared_from_this());
1308
1309 const char *plugin_name = nullptr;
1310 const char *flavor = nullptr;
1311 const bool prefer_file_cache = false;
1312
1313 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1314 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1315
Jim Ingham99d1e282017-03-31 22:39:55 +00001316 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001317 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318 }
Sean Callanan4740a732016-09-06 04:48:36 +00001319
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 InstructionSP instruction_sp =
1321 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1322
1323 llvm::SmallVector<Instruction::Operand, 3> operands;
1324
1325 if (!instruction_sp->ParseOperands(operands)) {
1326 return ValueObjectSP();
1327 }
1328
1329 RegisterContextSP register_context_sp = GetRegisterContext();
1330
1331 if (!register_context_sp) {
1332 return ValueObjectSP();
1333 }
1334
1335 for (const Instruction::Operand &operand : operands) {
1336 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1337 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1338
1339 if (!base_and_offset.first) {
1340 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001341 }
Sean Callanan4740a732016-09-06 04:48:36 +00001342
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 switch (base_and_offset.first->m_type) {
1344 case Instruction::Operand::Type::Immediate: {
1345 lldb_private::Address addr;
1346 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1347 base_and_offset.second,
1348 addr)) {
1349 TypeSystem *c_type_system =
1350 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1351 if (!c_type_system) {
1352 return ValueObjectSP();
1353 } else {
1354 CompilerType void_ptr_type =
1355 c_type_system
1356 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1357 .GetPointerType();
1358 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001359 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001361 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 }
1363 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001364 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 case Instruction::Operand::Type::Register: {
1366 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1367 base_and_offset.second);
1368 }
1369 default:
1370 return ValueObjectSP();
1371 }
1372 }
1373
1374 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001375}
1376
Kate Stoneb9c1b512016-09-06 20:57:50 +00001377namespace {
1378ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1379 int64_t offset) {
1380 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1381 return ValueObjectSP();
1382 }
Sean Callanan4740a732016-09-06 04:48:36 +00001383
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 if (parent->IsPointerOrReferenceType()) {
1385 return parent;
1386 }
1387
1388 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1389 const bool can_create = true;
1390 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1391
1392 if (!child_sp) {
1393 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001394 }
1395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396 int64_t child_offset = child_sp->GetByteOffset();
1397 int64_t child_size = child_sp->GetByteSize();
1398
1399 if (offset >= child_offset && offset < (child_offset + child_size)) {
1400 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402 }
Sean Callanan4740a732016-09-06 04:48:36 +00001403
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404 if (offset == 0) {
1405 return parent;
1406 } else {
1407 return ValueObjectSP();
1408 }
1409}
1410
1411ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1412 ValueObjectSP &base,
1413 int64_t offset) {
1414 // base is a pointer to something
Adrian Prantl05097242018-04-30 16:49:04 +00001415 // offset is the thing to add to the pointer We return the most sensible
1416 // ValueObject for the result of *(base+offset)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001417
1418 if (!base->IsPointerOrReferenceType()) {
1419 return ValueObjectSP();
1420 }
1421
Zachary Turner97206d52017-05-12 04:51:55 +00001422 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001424
1425 if (!pointee) {
1426 return ValueObjectSP();
1427 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001428
Ilia K4f730dc2016-09-12 05:25:33 +00001429 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 int64_t index = offset / pointee->GetByteSize();
1431 offset = offset % pointee->GetByteSize();
1432 const bool can_create = true;
1433 pointee = base->GetSyntheticArrayMember(index, can_create);
1434 }
1435
1436 if (!pointee || error.Fail()) {
1437 return ValueObjectSP();
1438 }
1439
1440 return GetValueForOffset(frame, pointee, offset);
1441}
1442
1443//------------------------------------------------------------------
1444/// Attempt to reconstruct the ValueObject for the address contained in a
1445/// given register plus an offset.
1446///
1447/// @params [in] frame
1448/// The current stack frame.
1449///
1450/// @params [in] reg
1451/// The register.
1452///
1453/// @params [in] offset
1454/// The offset from the register.
1455///
1456/// @param [in] disassembler
1457/// A disassembler containing instructions valid up to the current PC.
1458///
1459/// @param [in] variables
1460/// The variable list from the current frame,
1461///
1462/// @param [in] pc
1463/// The program counter for the instruction considered the 'user'.
1464///
1465/// @return
1466/// A string describing the base for the ExpressionPath. This could be a
1467/// variable, a register value, an argument, or a function return value.
1468/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1469//------------------------------------------------------------------
1470lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1471 int64_t offset, Disassembler &disassembler,
1472 VariableList &variables, const Address &pc) {
1473 // Example of operation for Intel:
1474 //
1475 // +14: movq -0x8(%rbp), %rdi
1476 // +18: movq 0x8(%rdi), %rdi
1477 // +22: addl 0x4(%rdi), %eax
1478 //
1479 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1480 //
Adrian Prantl05097242018-04-30 16:49:04 +00001481 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1482 // +18 that assigns to rdi, and calls itself recursively for that dereference
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1484 // +14 that assigns to rdi, and calls itself recursively for that
1485 // derefernece
1486 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1487 // variable list.
1488 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1489 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1490 // at +18)
1491 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1492 // rdi+4 at +22)
1493
1494 // First, check the variable list to see if anything is at the specified
1495 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001496
Sean Callanan50857102016-09-14 00:48:19 +00001497 using namespace OperandMatchers;
1498
Sean Callanan0ac172d2016-09-14 20:29:57 +00001499 const RegisterInfo *reg_info =
1500 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1501 if (!reg_info) {
1502 return ValueObjectSP();
1503 }
1504
Sean Callanan807ee2f2016-09-13 21:18:27 +00001505 Instruction::Operand op =
1506 offset ? Instruction::Operand::BuildDereference(
1507 Instruction::Operand::BuildSum(
1508 Instruction::Operand::BuildRegister(reg),
1509 Instruction::Operand::BuildImmediate(offset)))
1510 : Instruction::Operand::BuildDereference(
1511 Instruction::Operand::BuildRegister(reg));
1512
Kate Stoneb9c1b512016-09-06 20:57:50 +00001513 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1514 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001515 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1516 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001517 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518 }
1519
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 const uint32_t current_inst =
1521 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1522 if (current_inst == UINT32_MAX) {
1523 return ValueObjectSP();
1524 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1527 // This is not an exact algorithm, and it sacrifices accuracy for
Adrian Prantl05097242018-04-30 16:49:04 +00001528 // generality. Recognizing "mov" and "ld" instructions –– and which
1529 // are their source and destination operands -- is something the
1530 // disassembler should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 InstructionSP instruction_sp =
1532 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533
Sean Callanan50857102016-09-14 00:48:19 +00001534 if (instruction_sp->IsCall()) {
1535 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1536 if (!abi_sp) {
1537 continue;
1538 }
1539
1540 const char *return_register_name;
1541 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1542 continue;
1543 }
1544
1545 const RegisterInfo *return_register_info =
1546 frame.GetRegisterContext()->GetRegisterInfoByName(
1547 return_register_name);
1548 if (!return_register_info) {
1549 continue;
1550 }
1551
1552 int64_t offset = 0;
1553
1554 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1555 MatchRegOp(*return_register_info))(op) &&
1556 !MatchUnaryOp(
1557 MatchOpType(Instruction::Operand::Type::Dereference),
1558 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1559 MatchRegOp(*return_register_info),
1560 FetchImmOp(offset)))(op)) {
1561 continue;
1562 }
1563
Kate Stoneb9c1b512016-09-06 20:57:50 +00001564 llvm::SmallVector<Instruction::Operand, 1> operands;
1565 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1566 continue;
1567 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 switch (operands[0].m_type) {
1570 default:
1571 break;
1572 case Instruction::Operand::Type::Immediate: {
1573 SymbolContext sc;
1574 Address load_address;
1575 if (!frame.CalculateTarget()->ResolveLoadAddress(
1576 operands[0].m_immediate, load_address)) {
1577 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1580 load_address, eSymbolContextFunction, sc);
1581 if (!sc.function) {
1582 break;
1583 }
1584 CompilerType function_type = sc.function->GetCompilerType();
1585 if (!function_type.IsFunctionType()) {
1586 break;
1587 }
1588 CompilerType return_type = function_type.GetFunctionReturnType();
1589 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001590 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591 return_value)) {
1592 break;
1593 }
1594 std::string name_str(
1595 sc.function->GetName().AsCString("<unknown function>"));
1596 name_str.append("()");
1597 Address return_value_address(return_value.GetAsUInt64());
1598 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001599 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1601 }
1602 }
1603
1604 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001605 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606
1607 llvm::SmallVector<Instruction::Operand, 2> operands;
1608 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1609 continue;
1610 }
1611
Kate Stoneb9c1b512016-09-06 20:57:50 +00001612 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001613 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1614 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1615 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001616
1617 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001618 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001619 }
1620 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001622 }
1623 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624 continue;
1625 }
1626
1627 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001628 ValueObjectSP source_path;
1629 ConstString origin_register;
1630 int64_t origin_offset = 0;
1631
1632 if (FetchRegOp(origin_register)(*origin_operand)) {
1633 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1634 variables, instruction_sp->GetAddress());
1635 } else if (MatchUnaryOp(
1636 MatchOpType(Instruction::Operand::Type::Dereference),
1637 FetchRegOp(origin_register))(*origin_operand) ||
1638 MatchUnaryOp(
1639 MatchOpType(Instruction::Operand::Type::Dereference),
1640 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1641 FetchRegOp(origin_register),
1642 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001643 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001644 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001646 if (!source_path) {
1647 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001649 source_path =
1650 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001651 }
1652
1653 if (source_path) {
1654 return source_path;
1655 }
1656 }
1657
1658 return ValueObjectSP();
1659}
1660}
1661
1662lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1663 int64_t offset) {
1664 TargetSP target_sp = CalculateTarget();
1665
1666 const ArchSpec &target_arch = target_sp->GetArchitecture();
1667
1668 Block *frame_block = GetFrameBlock();
1669
1670 if (!frame_block) {
1671 return ValueObjectSP();
1672 }
1673
1674 Function *function = frame_block->CalculateSymbolContextFunction();
1675 if (!function) {
1676 return ValueObjectSP();
1677 }
1678
1679 AddressRange pc_range = function->GetAddressRange();
1680
1681 if (GetFrameCodeAddress().GetFileAddress() <
1682 pc_range.GetBaseAddress().GetFileAddress() ||
1683 GetFrameCodeAddress().GetFileAddress() -
1684 pc_range.GetBaseAddress().GetFileAddress() >=
1685 pc_range.GetByteSize()) {
1686 return ValueObjectSP();
1687 }
1688
1689 ExecutionContext exe_ctx(shared_from_this());
1690
1691 const char *plugin_name = nullptr;
1692 const char *flavor = nullptr;
1693 const bool prefer_file_cache = false;
1694 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1695 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1696
1697 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1698 return ValueObjectSP();
1699 }
1700
1701 const bool get_file_globals = false;
1702 VariableList *variables = GetVariableList(get_file_globals);
1703
1704 if (!variables) {
1705 return ValueObjectSP();
1706 }
1707
1708 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1709 GetFrameCodeAddress());
1710}
1711
1712TargetSP StackFrame::CalculateTarget() {
1713 TargetSP target_sp;
1714 ThreadSP thread_sp(GetThread());
1715 if (thread_sp) {
1716 ProcessSP process_sp(thread_sp->CalculateProcess());
1717 if (process_sp)
1718 target_sp = process_sp->CalculateTarget();
1719 }
1720 return target_sp;
1721}
1722
1723ProcessSP StackFrame::CalculateProcess() {
1724 ProcessSP process_sp;
1725 ThreadSP thread_sp(GetThread());
1726 if (thread_sp)
1727 process_sp = thread_sp->CalculateProcess();
1728 return process_sp;
1729}
1730
1731ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1732
1733StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1734
1735void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1736 exe_ctx.SetContext(shared_from_this());
1737}
1738
Pavel Labath7f1c1212017-06-12 16:25:24 +00001739void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 const char *frame_marker) {
1741 if (strm == nullptr)
1742 return;
1743
1744 GetSymbolContext(eSymbolContextEverything);
1745 ExecutionContext exe_ctx(shared_from_this());
1746 StreamString s;
1747
1748 if (frame_marker)
1749 s.PutCString(frame_marker);
1750
1751 const FormatEntity::Entry *frame_format = nullptr;
1752 Target *target = exe_ctx.GetTargetPtr();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001753 if (target) {
1754 if (show_unique) {
1755 frame_format = target->GetDebugger().GetFrameFormatUnique();
1756 } else {
1757 frame_format = target->GetDebugger().GetFrameFormat();
1758 }
1759 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1761 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001762 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001763 } else {
1764 Dump(strm, true, false);
1765 strm->EOL();
1766 }
1767}
1768
1769void StackFrame::Dump(Stream *strm, bool show_frame_index,
1770 bool show_fullpaths) {
1771 if (strm == nullptr)
1772 return;
1773
1774 if (show_frame_index)
1775 strm->Printf("frame #%u: ", m_frame_index);
1776 ExecutionContext exe_ctx(shared_from_this());
1777 Target *target = exe_ctx.GetTargetPtr();
1778 strm->Printf("0x%0*" PRIx64 " ",
1779 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1780 : 16,
1781 GetFrameCodeAddress().GetLoadAddress(target));
1782 GetSymbolContext(eSymbolContextEverything);
1783 const bool show_module = true;
1784 const bool show_inline = true;
1785 const bool show_function_arguments = true;
1786 const bool show_function_name = true;
1787 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1788 GetFrameCodeAddress(), show_fullpaths, show_module,
1789 show_inline, show_function_arguments,
1790 show_function_name);
1791}
1792
1793void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1794 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1795 assert(GetStackID() ==
1796 prev_frame.GetStackID()); // TODO: remove this after some testing
1797 m_variable_list_sp = prev_frame.m_variable_list_sp;
1798 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001799 if (!m_disassembly.GetString().empty()) {
1800 m_disassembly.Clear();
1801 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1802 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001803}
1804
1805void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1806 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1807 assert(GetStackID() ==
1808 curr_frame.GetStackID()); // TODO: remove this after some testing
1809 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1810 assert(GetThread() == curr_frame.GetThread());
1811 m_frame_index = curr_frame.m_frame_index;
1812 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1813 m_reg_context_sp = curr_frame.m_reg_context_sp;
1814 m_frame_code_addr = curr_frame.m_frame_code_addr;
1815 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1816 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1817 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1818 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1819 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1820 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1821 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1822 m_sc.function == curr_frame.m_sc.function);
1823 m_sc = curr_frame.m_sc;
1824 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1825 m_flags.Set(m_sc.GetResolvedMask());
1826 m_frame_base.Clear();
1827 m_frame_base_error.Clear();
1828}
1829
1830bool StackFrame::HasCachedData() const {
1831 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001832 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001833 if (m_variable_list_value_objects.GetSize() > 0)
1834 return true;
1835 if (!m_disassembly.GetString().empty())
1836 return true;
1837 return false;
1838}
1839
1840bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
Pavel Labath7f1c1212017-06-12 16:25:24 +00001841 bool show_unique, const char *frame_marker) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001842 if (show_frame_info) {
1843 strm.Indent();
Pavel Labath7f1c1212017-06-12 16:25:24 +00001844 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001845 }
1846
1847 if (show_source) {
1848 ExecutionContext exe_ctx(shared_from_this());
1849 bool have_source = false, have_debuginfo = false;
1850 Debugger::StopDisassemblyType disasm_display =
1851 Debugger::eStopDisassemblyTypeNever;
1852 Target *target = exe_ctx.GetTargetPtr();
1853 if (target) {
1854 Debugger &debugger = target->GetDebugger();
1855 const uint32_t source_lines_before =
1856 debugger.GetStopSourceLineCount(true);
1857 const uint32_t source_lines_after =
1858 debugger.GetStopSourceLineCount(false);
1859 disasm_display = debugger.GetStopDisassemblyDisplay();
1860
1861 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1862 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1863 have_debuginfo = true;
1864 if (source_lines_before > 0 || source_lines_after > 0) {
1865 size_t num_lines =
1866 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1867 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001868 m_sc.line_entry.column, source_lines_before,
1869 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001870 if (num_lines != 0)
1871 have_source = true;
1872 // TODO: Give here a one time warning if source file is missing.
1873 }
1874 }
1875 switch (disasm_display) {
1876 case Debugger::eStopDisassemblyTypeNever:
1877 break;
1878
1879 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1880 if (have_debuginfo)
1881 break;
1882 LLVM_FALLTHROUGH;
1883
1884 case Debugger::eStopDisassemblyTypeNoSource:
1885 if (have_source)
1886 break;
1887 LLVM_FALLTHROUGH;
1888
1889 case Debugger::eStopDisassemblyTypeAlways:
1890 if (target) {
1891 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1892 if (disasm_lines > 0) {
1893 const ArchSpec &target_arch = target->GetArchitecture();
1894 AddressRange pc_range;
1895 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1896 pc_range.SetByteSize(disasm_lines *
1897 target_arch.GetMaximumOpcodeByteSize());
1898 const char *plugin_name = nullptr;
1899 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001900 const bool mixed_source_and_assembly = false;
1901 Disassembler::Disassemble(
1902 target->GetDebugger(), target_arch, plugin_name, flavor,
1903 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1904 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001905 }
1906 }
1907 break;
1908 }
1909 }
1910 }
1911 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001912}