blob: 2676056c95a0a9d047025b155dbe7852f87328a6 [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"
36
37using namespace lldb;
38using namespace lldb_private;
39
40// The first bits in the flags are reserved for the SymbolContext::Scope bits
41// so we know if we have tried to look up information in our internal symbol
42// context (m_sc) already.
Kate Stoneb9c1b512016-09-06 20:57:50 +000043#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
44#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
45#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
46#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
47#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
50 user_id_t unwind_frame_index, addr_t cfa,
51 bool cfa_is_valid, addr_t pc, uint32_t stop_id,
52 bool stop_id_is_valid, bool is_history_frame,
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000053 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
55 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
56 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
57 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
58 m_stop_id(stop_id), m_stop_id_is_valid(stop_id_is_valid),
59 m_is_history_frame(is_history_frame), m_variable_list_sp(),
60 m_variable_list_value_objects(), m_disassembly(), m_mutex() {
61 // If we don't have a CFA value, use the frame index for our StackID so that
62 // recursive
63 // functions properly aren't confused with one another on a history stack.
64 if (m_is_history_frame && !m_cfa_is_valid) {
65 m_id.SetCFA(m_frame_index);
66 }
Jason Molenda99618472013-11-04 11:02:52 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 if (sc_ptr != nullptr) {
69 m_sc = *sc_ptr;
70 m_flags.Set(m_sc.GetResolvedMask());
71 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
75 user_id_t unwind_frame_index,
76 const RegisterContextSP &reg_context_sp, addr_t cfa,
77 addr_t pc, const SymbolContext *sc_ptr)
78 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000079 m_concrete_frame_index(unwind_frame_index),
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
81 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
82 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
83 m_stop_id_is_valid(false), m_is_history_frame(false),
84 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
85 m_mutex() {
86 if (sc_ptr != nullptr) {
87 m_sc = *sc_ptr;
88 m_flags.Set(m_sc.GetResolvedMask());
89 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 if (reg_context_sp && !m_sc.target_sp) {
92 m_sc.target_sp = reg_context_sp->CalculateTarget();
93 if (m_sc.target_sp)
94 m_flags.Set(eSymbolContextTarget);
95 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +000096}
97
Kate Stoneb9c1b512016-09-06 20:57:50 +000098StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
99 user_id_t unwind_frame_index,
100 const RegisterContextSP &reg_context_sp, addr_t cfa,
101 const Address &pc_addr, const SymbolContext *sc_ptr)
102 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000103 m_concrete_frame_index(unwind_frame_index),
104 m_reg_context_sp(reg_context_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
106 nullptr),
107 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
108 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
109 m_stop_id_is_valid(false), m_is_history_frame(false),
110 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
111 m_mutex() {
112 if (sc_ptr != nullptr) {
113 m_sc = *sc_ptr;
114 m_flags.Set(m_sc.GetResolvedMask());
115 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 if (!m_sc.target_sp && reg_context_sp) {
118 m_sc.target_sp = reg_context_sp->CalculateTarget();
119 if (m_sc.target_sp)
120 m_flags.Set(eSymbolContextTarget);
121 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 ModuleSP pc_module_sp(pc_addr.GetModule());
124 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
125 if (pc_module_sp) {
126 m_sc.module_sp = pc_module_sp;
127 m_flags.Set(eSymbolContextModule);
128 } else {
129 m_sc.module_sp.reset();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000130 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132}
133
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000134StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136StackID &StackFrame::GetStackID() {
137 std::lock_guard<std::recursive_mutex> guard(m_mutex);
138 // Make sure we have resolved the StackID object's symbol context scope if
139 // we already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
142 if (m_id.GetSymbolContextScope()) {
143 // We already have a symbol context scope, we just don't have our
144 // flag bit set.
145 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
146 } else {
147 // Calculate the frame block and use this for the stack ID symbol
148 // context scope if we have one.
149 SymbolContextScope *scope = GetFrameBlock();
150 if (scope == nullptr) {
151 // We don't have a block, so use the symbol
152 if (m_flags.IsClear(eSymbolContextSymbol))
153 GetSymbolContext(eSymbolContextSymbol);
154
155 // It is ok if m_sc.symbol is nullptr here
156 scope = m_sc.symbol;
157 }
158 // Set the symbol context scope (the accessor will set the
159 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
160 SetSymbolContextScope(scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
163 return m_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164}
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166uint32_t StackFrame::GetFrameIndex() const {
167 ThreadSP thread_sp = GetThread();
168 if (thread_sp)
169 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
170 m_frame_index);
171 else
172 return m_frame_index;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000173}
174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
176 std::lock_guard<std::recursive_mutex> guard(m_mutex);
177 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
178 m_id.SetSymbolContextScope(symbol_scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181const Address &StackFrame::GetFrameCodeAddress() {
182 std::lock_guard<std::recursive_mutex> guard(m_mutex);
183 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
184 !m_frame_code_addr.IsSectionOffset()) {
185 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 // Resolve the PC into a temporary address because if ResolveLoadAddress
188 // fails to resolve the address, it will clear the address object...
189 ThreadSP thread_sp(GetThread());
190 if (thread_sp) {
191 TargetSP target_sp(thread_sp->CalculateTarget());
192 if (target_sp) {
193 if (m_frame_code_addr.SetOpcodeLoadAddress(
194 m_frame_code_addr.GetOffset(), target_sp.get(),
195 eAddressClassCode)) {
196 ModuleSP module_sp(m_frame_code_addr.GetModule());
197 if (module_sp) {
198 m_sc.module_sp = module_sp;
199 m_flags.Set(eSymbolContextModule);
200 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 }
205 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208bool StackFrame::ChangePC(addr_t pc) {
209 std::lock_guard<std::recursive_mutex> guard(m_mutex);
210 // We can't change the pc value of a history stack frame - it is immutable.
211 if (m_is_history_frame)
212 return false;
213 m_frame_code_addr.SetRawAddress(pc);
214 m_sc.Clear(false);
215 m_flags.Reset(0);
216 ThreadSP thread_sp(GetThread());
217 if (thread_sp)
218 thread_sp->ClearStackFrames();
219 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220}
221
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222const char *StackFrame::Disassemble() {
223 std::lock_guard<std::recursive_mutex> guard(m_mutex);
224 if (m_disassembly.GetSize() == 0) {
225 ExecutionContext exe_ctx(shared_from_this());
226 Target *target = exe_ctx.GetTargetPtr();
227 if (target) {
228 const char *plugin_name = nullptr;
229 const char *flavor = nullptr;
230 Disassembler::Disassemble(target->GetDebugger(),
231 target->GetArchitecture(), plugin_name, flavor,
Jason Molenda0b4c26b2016-09-08 05:12:41 +0000232 exe_ctx, 0, false, 0, 0, m_disassembly);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 if (m_disassembly.GetSize() == 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 return nullptr;
236 }
237 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240Block *StackFrame::GetFrameBlock() {
241 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
242 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 if (m_sc.block) {
245 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
246 if (inline_block) {
247 // Use the block with the inlined function info
248 // as the frame block we want this frame to have only the variables
249 // for the inlined function and its non-inlined block child blocks.
250 return inline_block;
251 } else {
252 // This block is not contained within any inlined function blocks
253 // with so we want to use the top most function block.
254 return &m_sc.function->GetBlock(false);
255 }
256 }
257 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000258}
259
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260//----------------------------------------------------------------------
261// Get the symbol context if we already haven't done so by resolving the
262// PC address as much as possible. This way when we pass around a
263// StackFrame object, everyone will have as much information as
264// possible and no one will ever have to look things up manually.
265//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266const SymbolContext &StackFrame::GetSymbolContext(uint32_t resolve_scope) {
267 std::lock_guard<std::recursive_mutex> guard(m_mutex);
268 // Copy our internal symbol context into "sc".
269 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
270 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 // If the target was requested add that:
273 if (!m_sc.target_sp) {
274 m_sc.target_sp = CalculateTarget();
275 if (m_sc.target_sp)
276 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 }
278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 // Resolve our PC to section offset if we haven't already done so
280 // and if we don't have a module. The resolved address section will
281 // contain the module to which it belongs
282 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
283 GetFrameCodeAddress();
284
285 // If this is not frame zero, then we need to subtract 1 from the PC
286 // value when doing address lookups since the PC will be on the
287 // instruction following the function call instruction...
288
289 Address lookup_addr(GetFrameCodeAddress());
290 if (m_frame_index > 0 && lookup_addr.IsValid()) {
291 addr_t offset = lookup_addr.GetOffset();
292 if (offset > 0) {
293 lookup_addr.SetOffset(offset - 1);
294
295 } else {
296 // lookup_addr is the start of a section. We need
297 // do the math on the actual load address and re-compute
298 // the section. We're working with a 'noreturn' function
299 // at the end of a section.
300 ThreadSP thread_sp(GetThread());
301 if (thread_sp) {
302 TargetSP target_sp(thread_sp->CalculateTarget());
303 if (target_sp) {
304 addr_t addr_minus_one =
305 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
306 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
307 } else {
308 lookup_addr.SetOffset(offset - 1);
309 }
310 }
311 }
312 }
313
314 if (m_sc.module_sp) {
315 // We have something in our stack frame symbol context, lets check
316 // if we haven't already tried to lookup one of those things. If we
317 // haven't then we will do the query.
318
319 uint32_t actual_resolve_scope = 0;
320
321 if (resolve_scope & eSymbolContextCompUnit) {
322 if (m_flags.IsClear(eSymbolContextCompUnit)) {
323 if (m_sc.comp_unit)
324 resolved |= eSymbolContextCompUnit;
325 else
326 actual_resolve_scope |= eSymbolContextCompUnit;
327 }
328 }
329
330 if (resolve_scope & eSymbolContextFunction) {
331 if (m_flags.IsClear(eSymbolContextFunction)) {
332 if (m_sc.function)
333 resolved |= eSymbolContextFunction;
334 else
335 actual_resolve_scope |= eSymbolContextFunction;
336 }
337 }
338
339 if (resolve_scope & eSymbolContextBlock) {
340 if (m_flags.IsClear(eSymbolContextBlock)) {
341 if (m_sc.block)
342 resolved |= eSymbolContextBlock;
343 else
344 actual_resolve_scope |= eSymbolContextBlock;
345 }
346 }
347
348 if (resolve_scope & eSymbolContextSymbol) {
349 if (m_flags.IsClear(eSymbolContextSymbol)) {
350 if (m_sc.symbol)
351 resolved |= eSymbolContextSymbol;
352 else
353 actual_resolve_scope |= eSymbolContextSymbol;
354 }
355 }
356
357 if (resolve_scope & eSymbolContextLineEntry) {
358 if (m_flags.IsClear(eSymbolContextLineEntry)) {
359 if (m_sc.line_entry.IsValid())
360 resolved |= eSymbolContextLineEntry;
361 else
362 actual_resolve_scope |= eSymbolContextLineEntry;
363 }
364 }
365
366 if (actual_resolve_scope) {
367 // We might be resolving less information than what is already
368 // in our current symbol context so resolve into a temporary
369 // symbol context "sc" so we don't clear out data we have
370 // already found in "m_sc"
371 SymbolContext sc;
372 // Set flags that indicate what we have tried to resolve
373 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
374 lookup_addr, actual_resolve_scope, sc);
375 // Only replace what we didn't already have as we may have
376 // information for an inlined function scope that won't match
377 // what a standard lookup by address would match
378 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
379 m_sc.comp_unit = sc.comp_unit;
380 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
381 m_sc.function = sc.function;
382 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
383 m_sc.block = sc.block;
384 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
385 m_sc.symbol = sc.symbol;
386 if ((resolved & eSymbolContextLineEntry) &&
387 !m_sc.line_entry.IsValid()) {
388 m_sc.line_entry = sc.line_entry;
389 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
390 }
391 }
392 } else {
393 // If we don't have a module, then we can't have the compile unit,
394 // function, block, line entry or symbol, so we can safely call
395 // ResolveSymbolContextForAddress with our symbol context member m_sc.
396 if (m_sc.target_sp) {
397 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
398 lookup_addr, resolve_scope, m_sc);
399 }
400 }
401
402 // Update our internal flags so we remember what we have tried to locate so
403 // we don't have to keep trying when more calls to this function are made.
404 // We might have dug up more information that was requested (for example
405 // if we were asked to only get the block, we will have gotten the
406 // compile unit, and function) so set any additional bits that we resolved
407 m_flags.Set(resolve_scope | resolved);
408 }
409
410 // Return the symbol context with everything that was possible to resolve
411 // resolved.
412 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413}
414
Kate Stoneb9c1b512016-09-06 20:57:50 +0000415VariableList *StackFrame::GetVariableList(bool get_file_globals) {
416 std::lock_guard<std::recursive_mutex> guard(m_mutex);
417 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
418 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 Block *frame_block = GetFrameBlock();
421
422 if (frame_block) {
423 const bool get_child_variables = true;
424 const bool can_create = true;
425 const bool stop_if_child_block_is_inlined_function = true;
426 m_variable_list_sp.reset(new VariableList());
427 frame_block->AppendBlockVariables(can_create, get_child_variables,
428 stop_if_child_block_is_inlined_function,
429 [this](Variable *v) { return true; },
430 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000431 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 }
433
434 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
435 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
436
437 if (m_flags.IsClear(eSymbolContextCompUnit))
438 GetSymbolContext(eSymbolContextCompUnit);
439
440 if (m_sc.comp_unit) {
441 VariableListSP global_variable_list_sp(
442 m_sc.comp_unit->GetVariableList(true));
443 if (m_variable_list_sp)
444 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
445 else
446 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000448 }
449
450 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451}
452
Greg Claytond41f0322011-08-02 23:35:43 +0000453VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000454StackFrame::GetInScopeVariableList(bool get_file_globals,
455 bool must_have_valid_location) {
456 std::lock_guard<std::recursive_mutex> guard(m_mutex);
457 // We can't fetch variable information for a history stack frame.
458 if (m_is_history_frame)
459 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000460
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 VariableListSP var_list_sp(new VariableList);
462 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000463
Kate Stoneb9c1b512016-09-06 20:57:50 +0000464 if (m_sc.block) {
465 const bool can_create = true;
466 const bool get_parent_variables = true;
467 const bool stop_if_block_is_inlined_function = true;
468 m_sc.block->AppendVariables(
469 can_create, get_parent_variables, stop_if_block_is_inlined_function,
470 [this, must_have_valid_location](Variable *v) {
471 return v->IsInScope(this) && (!must_have_valid_location ||
472 v->LocationIsValidForFrame(this));
473 },
474 var_list_sp.get());
475 }
476
477 if (m_sc.comp_unit && get_file_globals) {
478 VariableListSP global_variable_list_sp(
479 m_sc.comp_unit->GetVariableList(true));
480 if (global_variable_list_sp)
481 var_list_sp->AddVariables(global_variable_list_sp.get());
482 }
483
484 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
488 const char *var_expr_cstr, DynamicValueType use_dynamic, uint32_t options,
489 VariableSP &var_sp, Error &error) {
490 // We can't fetch variable information for a history stack frame.
491 if (m_is_history_frame)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000492 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494 if (var_expr_cstr && var_expr_cstr[0]) {
495 const bool check_ptr_vs_member =
496 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
497 const bool no_fragile_ivar =
498 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
499 const bool no_synth_child =
500 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
501 // const bool no_synth_array = (options &
502 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
503 error.Clear();
504 bool deref = false;
505 bool address_of = false;
Greg Clayton288bdf92010-09-02 02:59:18 +0000506 ValueObjectSP valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000507 const bool get_file_globals = true;
508 // When looking up a variable for an expression, we need only consider the
509 // variables that are in scope.
510 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
511 VariableList *variable_list = var_list_sp.get();
512
513 if (variable_list) {
514 // If first character is a '*', then show pointer contents
515 const char *var_expr = var_expr_cstr;
516 if (var_expr[0] == '*') {
517 deref = true;
518 var_expr++; // Skip the '*'
519 } else if (var_expr[0] == '&') {
520 address_of = true;
521 var_expr++; // Skip the '&'
522 }
523
524 std::string var_path(var_expr);
525 size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
526 StreamString var_expr_path_strm;
527
528 ConstString name_const_string;
529 if (separator_idx == std::string::npos)
530 name_const_string.SetCString(var_path.c_str());
531 else
532 name_const_string.SetCStringWithLength(var_path.c_str(), separator_idx);
533
534 var_sp = variable_list->FindVariable(name_const_string, false);
535
536 bool synthetically_added_instance_object = false;
537
538 if (var_sp) {
539 var_path.erase(0, name_const_string.GetLength());
540 }
541
542 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
543 // Check for direct ivars access which helps us with implicit
544 // access to ivars with the "this->" or "self->"
545 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
546 lldb::LanguageType method_language = eLanguageTypeUnknown;
547 bool is_instance_method = false;
548 ConstString method_object_name;
549 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
550 method_object_name)) {
551 if (is_instance_method && method_object_name) {
552 var_sp = variable_list->FindVariable(method_object_name);
553 if (var_sp) {
554 separator_idx = 0;
555 var_path.insert(0, "->");
556 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000557 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000558 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000559 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 }
561
562 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
563 // Check if any anonymous unions are there which contain a variable with
564 // the name we need
565 for (size_t i = 0; i < variable_list->GetSize(); i++) {
566 if (VariableSP variable_sp = variable_list->GetVariableAtIndex(i)) {
567 if (variable_sp->GetName().IsEmpty()) {
568 if (Type *var_type = variable_sp->GetType()) {
569 if (var_type->GetForwardCompilerType().IsAnonymousType()) {
570 valobj_sp =
571 GetValueObjectForFrameVariable(variable_sp, use_dynamic);
572 if (!valobj_sp)
573 return valobj_sp;
574 valobj_sp = valobj_sp->GetChildMemberWithName(
575 name_const_string, true);
576 if (valobj_sp)
577 break;
578 }
579 }
580 }
581 }
582 }
583 }
584
585 if (var_sp && !valobj_sp) {
586 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
587 if (!valobj_sp)
588 return valobj_sp;
589 }
590 if (valobj_sp) {
591 // We are dumping at least one child
592 while (separator_idx != std::string::npos) {
593 // Calculate the next separator index ahead of time
594 ValueObjectSP child_valobj_sp;
595 const char separator_type = var_path[0];
596 switch (separator_type) {
597 case '-':
598 if (var_path.size() >= 2 && var_path[1] != '>')
599 return ValueObjectSP();
600
601 if (no_fragile_ivar) {
602 // Make sure we aren't trying to deref an objective
603 // C ivar if this is not allowed
604 const uint32_t pointer_type_flags =
605 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
606 if ((pointer_type_flags & eTypeIsObjC) &&
607 (pointer_type_flags & eTypeIsPointer)) {
608 // This was an objective C object pointer and
609 // it was requested we skip any fragile ivars
610 // so return nothing here
611 return ValueObjectSP();
612 }
613 }
614 var_path.erase(0, 1); // Remove the '-'
615 LLVM_FALLTHROUGH;
616 case '.': {
617 const bool expr_is_ptr = var_path[0] == '>';
618
619 var_path.erase(0, 1); // Remove the '.' or '>'
620 separator_idx = var_path.find_first_of(".-[");
621 ConstString child_name;
622 if (separator_idx == std::string::npos)
623 child_name.SetCString(var_path.c_str());
624 else
625 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
626
627 if (check_ptr_vs_member) {
628 // We either have a pointer type and need to verify
629 // valobj_sp is a pointer, or we have a member of a
630 // class/union/struct being accessed with the . syntax
631 // and need to verify we don't have a pointer.
632 const bool actual_is_ptr = valobj_sp->IsPointerType();
633
634 if (actual_is_ptr != expr_is_ptr) {
635 // Incorrect use of "." with a pointer, or "->" with
636 // a class/union/struct instance or reference.
637 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
638 if (actual_is_ptr)
639 error.SetErrorStringWithFormat(
640 "\"%s\" is a pointer and . was used to attempt to access "
641 "\"%s\". Did you mean \"%s->%s\"?",
Zachary Turnerc1564272016-11-16 21:15:24 +0000642 var_expr_path_strm.GetData(), child_name.GetCString(),
643 var_expr_path_strm.GetData(), var_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644 else
645 error.SetErrorStringWithFormat(
646 "\"%s\" is not a pointer and -> was used to attempt to "
647 "access \"%s\". Did you mean \"%s.%s\"?",
Zachary Turnerc1564272016-11-16 21:15:24 +0000648 var_expr_path_strm.GetData(), child_name.GetCString(),
649 var_expr_path_strm.GetData(), var_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000650 return ValueObjectSP();
651 }
652 }
653 child_valobj_sp =
654 valobj_sp->GetChildMemberWithName(child_name, true);
655 if (!child_valobj_sp) {
656 if (!no_synth_child) {
657 child_valobj_sp = valobj_sp->GetSyntheticValue();
658 if (child_valobj_sp)
659 child_valobj_sp =
660 child_valobj_sp->GetChildMemberWithName(child_name, true);
661 }
662
663 if (no_synth_child || !child_valobj_sp) {
664 // No child member with name "child_name"
665 if (synthetically_added_instance_object) {
666 // We added a "this->" or "self->" to the beginning of the
667 // expression
668 // and this is the first pointer ivar access, so just return
669 // the normal
670 // error
671 error.SetErrorStringWithFormat(
672 "no variable or instance variable named '%s' found in "
673 "this frame",
674 name_const_string.GetCString());
675 } else {
676 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
677 if (child_name) {
678 error.SetErrorStringWithFormat(
679 "\"%s\" is not a member of \"(%s) %s\"",
680 child_name.GetCString(),
681 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000682 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 } else {
684 error.SetErrorStringWithFormat(
685 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turnerc1564272016-11-16 21:15:24 +0000686 var_expr_path_strm.GetData(), var_expr_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000687 }
688 }
689 return ValueObjectSP();
690 }
691 }
692 synthetically_added_instance_object = false;
693 // Remove the child name from the path
694 var_path.erase(0, child_name.GetLength());
695 if (use_dynamic != eNoDynamicValues) {
696 ValueObjectSP dynamic_value_sp(
697 child_valobj_sp->GetDynamicValue(use_dynamic));
698 if (dynamic_value_sp)
699 child_valobj_sp = dynamic_value_sp;
700 }
701 } break;
702
703 case '[':
704 // Array member access, or treating pointer as an array
705 if (var_path.size() > 2) // Need at least two brackets and a number
706 {
707 char *end = nullptr;
708 long child_index = ::strtol(&var_path[1], &end, 0);
709 if (end && *end == ']' &&
710 *(end - 1) != '[') // this code forces an error in the case of
711 // arr[]. as bitfield[] is not a good
712 // syntax we're good to go
713 {
714 if (valobj_sp->GetCompilerType().IsPointerToScalarType() &&
715 deref) {
716 // what we have is *ptr[low]. the most similar C++ syntax is
717 // to deref ptr
718 // and extract bit low out of it. reading array item low
719 // would be done by saying ptr[low], without a deref * sign
720 Error error;
721 ValueObjectSP temp(valobj_sp->Dereference(error));
722 if (error.Fail()) {
723 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
724 error.SetErrorStringWithFormat(
725 "could not dereference \"(%s) %s\"",
726 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000727 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 return ValueObjectSP();
729 }
730 valobj_sp = temp;
731 deref = false;
732 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
733 deref) {
734 // what we have is *arr[low]. the most similar C++ syntax is
735 // to get arr[0]
736 // (an operation that is equivalent to deref-ing arr)
737 // and extract bit low out of it. reading array item low
738 // would be done by saying arr[low], without a deref * sign
739 Error error;
740 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
741 if (error.Fail()) {
742 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
743 error.SetErrorStringWithFormat(
744 "could not get item 0 for \"(%s) %s\"",
745 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000746 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000747 return ValueObjectSP();
748 }
749 valobj_sp = temp;
750 deref = false;
751 }
752
753 bool is_incomplete_array = false;
754 if (valobj_sp->IsPointerType()) {
755 bool is_objc_pointer = true;
756
757 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
758 eLanguageTypeObjC)
759 is_objc_pointer = false;
760 else if (!valobj_sp->GetCompilerType().IsPointerType())
761 is_objc_pointer = false;
762
763 if (no_synth_child && is_objc_pointer) {
764 error.SetErrorStringWithFormat(
765 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
766 "subscripted",
767 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000768 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769
770 return ValueObjectSP();
771 } else if (is_objc_pointer) {
772 // dereferencing ObjC variables is not valid.. so let's try
773 // and recur to synthetic children
774 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
775 if (!synthetic /* no synthetic */
776 || synthetic == valobj_sp) /* synthetic is the same as
777 the original object */
778 {
779 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
780 error.SetErrorStringWithFormat(
781 "\"(%s) %s\" is not an array type",
782 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000783 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000784 } else if (
785 static_cast<uint32_t>(child_index) >=
786 synthetic
787 ->GetNumChildren() /* synthetic does not have that many values */) {
788 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
789 error.SetErrorStringWithFormat(
790 "array index %ld is not valid for \"(%s) %s\"",
791 child_index,
792 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000793 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 } else {
795 child_valobj_sp =
796 synthetic->GetChildAtIndex(child_index, true);
797 if (!child_valobj_sp) {
798 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
799 error.SetErrorStringWithFormat(
800 "array index %ld is not valid for \"(%s) %s\"",
801 child_index, valobj_sp->GetTypeName().AsCString(
802 "<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000803 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000804 }
805 }
806 } else {
807 child_valobj_sp =
808 valobj_sp->GetSyntheticArrayMember(child_index, true);
809 if (!child_valobj_sp) {
810 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
811 error.SetErrorStringWithFormat(
812 "failed to use pointer as array for index %ld for "
813 "\"(%s) %s\"",
814 child_index,
815 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000816 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000817 }
818 }
819 } else if (valobj_sp->GetCompilerType().IsArrayType(
820 nullptr, nullptr, &is_incomplete_array)) {
821 // Pass false to dynamic_value here so we can tell the
822 // difference between
823 // no dynamic value and no member of this type...
824 child_valobj_sp =
825 valobj_sp->GetChildAtIndex(child_index, true);
826 if (!child_valobj_sp &&
827 (is_incomplete_array || !no_synth_child))
828 child_valobj_sp =
829 valobj_sp->GetSyntheticArrayMember(child_index, true);
830
831 if (!child_valobj_sp) {
832 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
833 error.SetErrorStringWithFormat(
834 "array index %ld is not valid for \"(%s) %s\"",
835 child_index,
836 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000837 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000838 }
839 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
840 // this is a bitfield asking to display just one bit
841 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
842 child_index, child_index, true);
843 if (!child_valobj_sp) {
844 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
845 error.SetErrorStringWithFormat(
846 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
847 child_index, child_index,
848 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000849 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 }
851 } else {
852 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
853 if (no_synth_child /* synthetic is forbidden */ ||
Jason Molenda0b4c26b2016-09-08 05:12:41 +0000854 !synthetic /* no synthetic */
855 || synthetic == valobj_sp) /* synthetic is the same as the
856 original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000857 {
858 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
859 error.SetErrorStringWithFormat(
860 "\"(%s) %s\" is not an array type",
861 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000862 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000863 } else if (
864 static_cast<uint32_t>(child_index) >=
865 synthetic
866 ->GetNumChildren() /* synthetic does not have that many values */) {
867 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
868 error.SetErrorStringWithFormat(
869 "array index %ld is not valid for \"(%s) %s\"",
870 child_index,
871 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000872 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000873 } else {
874 child_valobj_sp =
875 synthetic->GetChildAtIndex(child_index, true);
876 if (!child_valobj_sp) {
877 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
878 error.SetErrorStringWithFormat(
879 "array index %ld is not valid for \"(%s) %s\"",
880 child_index,
881 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000882 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883 }
884 }
885 }
886
887 if (!child_valobj_sp) {
888 // Invalid array index...
889 return ValueObjectSP();
890 }
891
892 // Erase the array member specification '[%i]' where
893 // %i is the array index
894 var_path.erase(0, (end - var_path.c_str()) + 1);
895 separator_idx = var_path.find_first_of(".-[");
896 if (use_dynamic != eNoDynamicValues) {
897 ValueObjectSP dynamic_value_sp(
898 child_valobj_sp->GetDynamicValue(use_dynamic));
899 if (dynamic_value_sp)
900 child_valobj_sp = dynamic_value_sp;
901 }
902 // Break out early from the switch since we were
903 // able to find the child member
904 break;
905 } else if (end && *end == '-') {
906 // this is most probably a BitField, let's take a look
907 char *real_end = nullptr;
908 long final_index = ::strtol(end + 1, &real_end, 0);
909 bool expand_bitfield = true;
910 if (real_end && *real_end == ']') {
911 // if the format given is [high-low], swap range
912 if (child_index > final_index) {
913 long temp = child_index;
914 child_index = final_index;
915 final_index = temp;
916 }
917
918 if (valobj_sp->GetCompilerType().IsPointerToScalarType() &&
919 deref) {
920 // what we have is *ptr[low-high]. the most similar C++
921 // syntax is to deref ptr
922 // and extract bits low thru high out of it. reading array
923 // items low thru high
924 // would be done by saying ptr[low-high], without a deref *
925 // sign
926 Error error;
927 ValueObjectSP temp(valobj_sp->Dereference(error));
928 if (error.Fail()) {
929 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
930 error.SetErrorStringWithFormat(
931 "could not dereference \"(%s) %s\"",
932 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000933 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934 return ValueObjectSP();
935 }
936 valobj_sp = temp;
937 deref = false;
938 } else if (valobj_sp->GetCompilerType()
939 .IsArrayOfScalarType() &&
940 deref) {
941 // what we have is *arr[low-high]. the most similar C++
942 // syntax is to get arr[0]
943 // (an operation that is equivalent to deref-ing arr)
944 // and extract bits low thru high out of it. reading array
945 // items low thru high
946 // would be done by saying arr[low-high], without a deref *
947 // sign
948 Error error;
949 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
950 if (error.Fail()) {
951 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
952 error.SetErrorStringWithFormat(
953 "could not get item 0 for \"(%s) %s\"",
954 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000955 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 return ValueObjectSP();
957 }
958 valobj_sp = temp;
959 deref = false;
960 }
961 /*else if (valobj_sp->IsArrayType() ||
962 valobj_sp->IsPointerType())
963 {
964 child_valobj_sp =
965 valobj_sp->GetSyntheticArrayRangeChild(child_index,
966 final_index, true);
967 expand_bitfield = false;
968 if (!child_valobj_sp)
969 {
970 valobj_sp->GetExpressionPath (var_expr_path_strm,
971 false);
972 error.SetErrorStringWithFormat ("array range %i-%i is
973 not valid for \"(%s) %s\"",
974 child_index,
975 final_index,
976 valobj_sp->GetTypeName().AsCString("<invalid
977 type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000978 var_expr_path_strm.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979 }
980 }*/
981
982 if (expand_bitfield) {
983 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
984 child_index, final_index, true);
985 if (!child_valobj_sp) {
986 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
987 error.SetErrorStringWithFormat(
988 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
989 child_index, final_index,
990 valobj_sp->GetTypeName().AsCString("<invalid type>"),
Zachary Turnerc1564272016-11-16 21:15:24 +0000991 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000992 }
993 }
994 }
995
996 if (!child_valobj_sp) {
997 // Invalid bitfield range...
998 return ValueObjectSP();
999 }
1000
1001 // Erase the bitfield member specification '[%i-%i]' where
1002 // %i is the index
1003 var_path.erase(0, (real_end - var_path.c_str()) + 1);
1004 separator_idx = var_path.find_first_of(".-[");
1005 if (use_dynamic != eNoDynamicValues) {
1006 ValueObjectSP dynamic_value_sp(
1007 child_valobj_sp->GetDynamicValue(use_dynamic));
1008 if (dynamic_value_sp)
1009 child_valobj_sp = dynamic_value_sp;
1010 }
1011 // Break out early from the switch since we were
1012 // able to find the child member
1013 break;
1014 }
1015 } else {
1016 error.SetErrorStringWithFormat(
1017 "invalid square bracket encountered after \"%s\" in \"%s\"",
Zachary Turnerc1564272016-11-16 21:15:24 +00001018 var_expr_path_strm.GetData(), var_path.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 }
1020 return ValueObjectSP();
1021
1022 default:
1023 // Failure...
1024 {
1025 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1026 error.SetErrorStringWithFormat(
1027 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
Zachary Turnerc1564272016-11-16 21:15:24 +00001028 separator_type, var_expr_path_strm.GetData(),
Kate Stoneb9c1b512016-09-06 20:57:50 +00001029 var_path.c_str());
1030
1031 return ValueObjectSP();
1032 }
1033 }
1034
1035 if (child_valobj_sp)
1036 valobj_sp = child_valobj_sp;
1037
1038 if (var_path.empty())
1039 break;
1040 }
1041 if (valobj_sp) {
1042 if (deref) {
1043 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1044 valobj_sp = deref_valobj_sp;
1045 } else if (address_of) {
1046 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1047 valobj_sp = address_of_valobj_sp;
1048 }
1049 }
1050 return valobj_sp;
1051 } else {
1052 error.SetErrorStringWithFormat(
1053 "no variable named '%s' found in this frame",
1054 name_const_string.GetCString());
1055 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001056 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001057 } else {
1058 error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1059 }
1060 return ValueObjectSP();
1061}
1062
1063bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Error *error_ptr) {
1064 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1065 if (!m_cfa_is_valid) {
1066 m_frame_base_error.SetErrorString(
1067 "No frame base available for this historical stack frame.");
1068 return false;
1069 }
1070
1071 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1072 if (m_sc.function) {
1073 m_frame_base.Clear();
1074 m_frame_base_error.Clear();
1075
1076 m_flags.Set(GOT_FRAME_BASE);
1077 ExecutionContext exe_ctx(shared_from_this());
1078 Value expr_value;
1079 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1080 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1081 loclist_base_addr =
1082 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1083 exe_ctx.GetTargetPtr());
1084
1085 if (m_sc.function->GetFrameBaseExpression().Evaluate(
1086 &exe_ctx, nullptr, nullptr, nullptr, loclist_base_addr, nullptr,
1087 nullptr, expr_value, &m_frame_base_error) == false) {
1088 // We should really have an error if evaluate returns, but in case
1089 // we don't, lets set the error to something at least.
1090 if (m_frame_base_error.Success())
1091 m_frame_base_error.SetErrorString(
1092 "Evaluation of the frame base expression failed.");
1093 } else {
1094 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1095 }
1096 } else {
1097 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001098 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 }
1100
1101 if (m_frame_base_error.Success())
1102 frame_base = m_frame_base;
1103
1104 if (error_ptr)
1105 *error_ptr = m_frame_base_error;
1106 return m_frame_base_error.Success();
1107}
1108
1109DWARFExpression *StackFrame::GetFrameBaseExpression(Error *error_ptr) {
1110 if (!m_sc.function) {
1111 if (error_ptr) {
1112 error_ptr->SetErrorString("No function in symbol context.");
1113 }
1114 return nullptr;
1115 }
1116
1117 return &m_sc.function->GetFrameBaseExpression();
1118}
1119
1120RegisterContextSP StackFrame::GetRegisterContext() {
1121 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1122 if (!m_reg_context_sp) {
1123 ThreadSP thread_sp(GetThread());
1124 if (thread_sp)
1125 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1126 }
1127 return m_reg_context_sp;
1128}
1129
1130bool StackFrame::HasDebugInformation() {
1131 GetSymbolContext(eSymbolContextLineEntry);
1132 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001133}
1134
1135ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001136StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1137 DynamicValueType use_dynamic) {
1138 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1139 ValueObjectSP valobj_sp;
1140 if (m_is_history_frame) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001141 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001142 }
1143 VariableList *var_list = GetVariableList(true);
1144 if (var_list) {
1145 // Make sure the variable is a frame variable
1146 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1147 const uint32_t num_variables = var_list->GetSize();
1148 if (var_idx < num_variables) {
1149 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1150 if (!valobj_sp) {
1151 if (m_variable_list_value_objects.GetSize() < num_variables)
1152 m_variable_list_value_objects.Resize(num_variables);
1153 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1154 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1155 }
Enrico Granata592afe72016-03-15 21:50:51 +00001156 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001157 }
1158 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1159 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1160 if (dynamic_sp)
1161 return dynamic_sp;
1162 }
1163 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001164}
1165
Kate Stoneb9c1b512016-09-06 20:57:50 +00001166ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1167 DynamicValueType use_dynamic) {
1168 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1169 if (m_is_history_frame)
1170 return ValueObjectSP();
1171
1172 // Check to make sure we aren't already tracking this variable?
1173 ValueObjectSP valobj_sp(
1174 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1175 if (!valobj_sp) {
1176 // We aren't already tracking this global
1177 VariableList *var_list = GetVariableList(true);
1178 // If this frame has no variables, create a new list
1179 if (var_list == nullptr)
1180 m_variable_list_sp.reset(new VariableList());
1181
1182 // Add the global/static variable to this frame
1183 m_variable_list_sp->AddVariable(variable_sp);
1184
1185 // Now make a value object for it so we can track its changes
1186 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1187 }
1188 return valobj_sp;
1189}
1190
1191bool StackFrame::IsInlined() {
1192 if (m_sc.block == nullptr)
1193 GetSymbolContext(eSymbolContextBlock);
1194 if (m_sc.block)
1195 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1196 return false;
1197}
1198
1199lldb::LanguageType StackFrame::GetLanguage() {
1200 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1201 if (cu)
1202 return cu->GetLanguage();
1203 return lldb::eLanguageTypeUnknown;
1204}
1205
1206lldb::LanguageType StackFrame::GuessLanguage() {
1207 LanguageType lang_type = GetLanguage();
1208
1209 if (lang_type == eLanguageTypeUnknown) {
1210 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1211 if (f) {
1212 lang_type = f->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001213 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001214 }
1215
1216 return lang_type;
1217}
1218
1219namespace {
1220std::pair<const Instruction::Operand *, int64_t>
1221GetBaseExplainingValue(const Instruction::Operand &operand,
1222 RegisterContext &register_context, lldb::addr_t value) {
1223 switch (operand.m_type) {
1224 case Instruction::Operand::Type::Dereference:
1225 case Instruction::Operand::Type::Immediate:
1226 case Instruction::Operand::Type::Invalid:
1227 case Instruction::Operand::Type::Product:
1228 // These are not currently interesting
1229 return std::make_pair(nullptr, 0);
1230 case Instruction::Operand::Type::Sum: {
1231 const Instruction::Operand *immediate_child = nullptr;
1232 const Instruction::Operand *variable_child = nullptr;
1233 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1234 immediate_child = &operand.m_children[0];
1235 variable_child = &operand.m_children[1];
1236 } else if (operand.m_children[1].m_type ==
1237 Instruction::Operand::Type::Immediate) {
1238 immediate_child = &operand.m_children[1];
1239 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001240 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001241 if (!immediate_child) {
1242 return std::make_pair(nullptr, 0);
1243 }
1244 lldb::addr_t adjusted_value = value;
1245 if (immediate_child->m_negative) {
1246 adjusted_value += immediate_child->m_immediate;
1247 } else {
1248 adjusted_value -= immediate_child->m_immediate;
1249 }
1250 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1251 GetBaseExplainingValue(*variable_child, register_context,
1252 adjusted_value);
1253 if (!base_and_offset.first) {
1254 return std::make_pair(nullptr, 0);
1255 }
1256 if (immediate_child->m_negative) {
1257 base_and_offset.second -= immediate_child->m_immediate;
1258 } else {
1259 base_and_offset.second += immediate_child->m_immediate;
1260 }
1261 return base_and_offset;
1262 }
1263 case Instruction::Operand::Type::Register: {
1264 const RegisterInfo *info =
1265 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1266 if (!info) {
1267 return std::make_pair(nullptr, 0);
1268 }
1269 RegisterValue reg_value;
1270 if (!register_context.ReadRegister(info, reg_value)) {
1271 return std::make_pair(nullptr, 0);
1272 }
1273 if (reg_value.GetAsUInt64() == value) {
1274 return std::make_pair(&operand, 0);
1275 } else {
1276 return std::make_pair(nullptr, 0);
1277 }
1278 }
1279 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001280 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001281}
1282
1283std::pair<const Instruction::Operand *, int64_t>
1284GetBaseExplainingDereference(const Instruction::Operand &operand,
1285 RegisterContext &register_context,
1286 lldb::addr_t addr) {
1287 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1288 return GetBaseExplainingValue(operand.m_children[0], register_context,
1289 addr);
1290 }
1291 return std::make_pair(nullptr, 0);
1292}
Ilia K4f730dc2016-09-12 05:25:33 +00001293}
Sean Callanan4740a732016-09-06 04:48:36 +00001294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1296 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 const ArchSpec &target_arch = target_sp->GetArchitecture();
1299
1300 AddressRange pc_range;
1301 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1302 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1303
1304 ExecutionContext exe_ctx(shared_from_this());
1305
1306 const char *plugin_name = nullptr;
1307 const char *flavor = nullptr;
1308 const bool prefer_file_cache = false;
1309
1310 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1311 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1312
1313 if (!disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001314 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 }
Sean Callanan4740a732016-09-06 04:48:36 +00001316
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 InstructionSP instruction_sp =
1318 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1319
1320 llvm::SmallVector<Instruction::Operand, 3> operands;
1321
1322 if (!instruction_sp->ParseOperands(operands)) {
1323 return ValueObjectSP();
1324 }
1325
1326 RegisterContextSP register_context_sp = GetRegisterContext();
1327
1328 if (!register_context_sp) {
1329 return ValueObjectSP();
1330 }
1331
1332 for (const Instruction::Operand &operand : operands) {
1333 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1334 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1335
1336 if (!base_and_offset.first) {
1337 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001338 }
Sean Callanan4740a732016-09-06 04:48:36 +00001339
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 switch (base_and_offset.first->m_type) {
1341 case Instruction::Operand::Type::Immediate: {
1342 lldb_private::Address addr;
1343 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1344 base_and_offset.second,
1345 addr)) {
1346 TypeSystem *c_type_system =
1347 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1348 if (!c_type_system) {
1349 return ValueObjectSP();
1350 } else {
1351 CompilerType void_ptr_type =
1352 c_type_system
1353 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1354 .GetPointerType();
1355 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001356 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001358 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 }
1360 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001361 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 case Instruction::Operand::Type::Register: {
1363 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1364 base_and_offset.second);
1365 }
1366 default:
1367 return ValueObjectSP();
1368 }
1369 }
1370
1371 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001372}
1373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374namespace {
1375ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1376 int64_t offset) {
1377 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1378 return ValueObjectSP();
1379 }
Sean Callanan4740a732016-09-06 04:48:36 +00001380
Kate Stoneb9c1b512016-09-06 20:57:50 +00001381 if (parent->IsPointerOrReferenceType()) {
1382 return parent;
1383 }
1384
1385 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1386 const bool can_create = true;
1387 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1388
1389 if (!child_sp) {
1390 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001391 }
1392
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393 int64_t child_offset = child_sp->GetByteOffset();
1394 int64_t child_size = child_sp->GetByteSize();
1395
1396 if (offset >= child_offset && offset < (child_offset + child_size)) {
1397 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001398 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 }
Sean Callanan4740a732016-09-06 04:48:36 +00001400
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 if (offset == 0) {
1402 return parent;
1403 } else {
1404 return ValueObjectSP();
1405 }
1406}
1407
1408ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1409 ValueObjectSP &base,
1410 int64_t offset) {
1411 // base is a pointer to something
1412 // offset is the thing to add to the pointer
1413 // We return the most sensible ValueObject for the result of *(base+offset)
1414
1415 if (!base->IsPointerOrReferenceType()) {
1416 return ValueObjectSP();
1417 }
1418
1419 Error error;
1420 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001421
1422 if (!pointee) {
1423 return ValueObjectSP();
1424 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001425
Ilia K4f730dc2016-09-12 05:25:33 +00001426 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 int64_t index = offset / pointee->GetByteSize();
1428 offset = offset % pointee->GetByteSize();
1429 const bool can_create = true;
1430 pointee = base->GetSyntheticArrayMember(index, can_create);
1431 }
1432
1433 if (!pointee || error.Fail()) {
1434 return ValueObjectSP();
1435 }
1436
1437 return GetValueForOffset(frame, pointee, offset);
1438}
1439
1440//------------------------------------------------------------------
1441/// Attempt to reconstruct the ValueObject for the address contained in a
1442/// given register plus an offset.
1443///
1444/// @params [in] frame
1445/// The current stack frame.
1446///
1447/// @params [in] reg
1448/// The register.
1449///
1450/// @params [in] offset
1451/// The offset from the register.
1452///
1453/// @param [in] disassembler
1454/// A disassembler containing instructions valid up to the current PC.
1455///
1456/// @param [in] variables
1457/// The variable list from the current frame,
1458///
1459/// @param [in] pc
1460/// The program counter for the instruction considered the 'user'.
1461///
1462/// @return
1463/// A string describing the base for the ExpressionPath. This could be a
1464/// variable, a register value, an argument, or a function return value.
1465/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1466//------------------------------------------------------------------
1467lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1468 int64_t offset, Disassembler &disassembler,
1469 VariableList &variables, const Address &pc) {
1470 // Example of operation for Intel:
1471 //
1472 // +14: movq -0x8(%rbp), %rdi
1473 // +18: movq 0x8(%rdi), %rdi
1474 // +22: addl 0x4(%rdi), %eax
1475 //
1476 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1477 //
1478 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18
1479 // that assigns to rdi, and calls itself recursively for that dereference
1480 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1481 // +14 that assigns to rdi, and calls itself recursively for that
1482 // derefernece
1483 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1484 // variable list.
1485 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1486 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1487 // at +18)
1488 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1489 // rdi+4 at +22)
1490
1491 // First, check the variable list to see if anything is at the specified
1492 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001493
Sean Callanan50857102016-09-14 00:48:19 +00001494 using namespace OperandMatchers;
1495
Sean Callanan0ac172d2016-09-14 20:29:57 +00001496 const RegisterInfo *reg_info =
1497 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1498 if (!reg_info) {
1499 return ValueObjectSP();
1500 }
1501
Sean Callanan807ee2f2016-09-13 21:18:27 +00001502 Instruction::Operand op =
1503 offset ? Instruction::Operand::BuildDereference(
1504 Instruction::Operand::BuildSum(
1505 Instruction::Operand::BuildRegister(reg),
1506 Instruction::Operand::BuildImmediate(offset)))
1507 : Instruction::Operand::BuildDereference(
1508 Instruction::Operand::BuildRegister(reg));
1509
Kate Stoneb9c1b512016-09-06 20:57:50 +00001510 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1511 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001512 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1513 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001514 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515 }
1516
Kate Stoneb9c1b512016-09-06 20:57:50 +00001517 const uint32_t current_inst =
1518 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1519 if (current_inst == UINT32_MAX) {
1520 return ValueObjectSP();
1521 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1524 // This is not an exact algorithm, and it sacrifices accuracy for
Sean Callanan0ac172d2016-09-14 20:29:57 +00001525 // generality. Recognizing "mov" and "ld" instructions –– and which are
1526 // their source and destination operands -- is something the disassembler
1527 // should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 InstructionSP instruction_sp =
1529 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001530
Sean Callanan50857102016-09-14 00:48:19 +00001531 if (instruction_sp->IsCall()) {
1532 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1533 if (!abi_sp) {
1534 continue;
1535 }
1536
1537 const char *return_register_name;
1538 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1539 continue;
1540 }
1541
1542 const RegisterInfo *return_register_info =
1543 frame.GetRegisterContext()->GetRegisterInfoByName(
1544 return_register_name);
1545 if (!return_register_info) {
1546 continue;
1547 }
1548
1549 int64_t offset = 0;
1550
1551 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1552 MatchRegOp(*return_register_info))(op) &&
1553 !MatchUnaryOp(
1554 MatchOpType(Instruction::Operand::Type::Dereference),
1555 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1556 MatchRegOp(*return_register_info),
1557 FetchImmOp(offset)))(op)) {
1558 continue;
1559 }
1560
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561 llvm::SmallVector<Instruction::Operand, 1> operands;
1562 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1563 continue;
1564 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 switch (operands[0].m_type) {
1567 default:
1568 break;
1569 case Instruction::Operand::Type::Immediate: {
1570 SymbolContext sc;
1571 Address load_address;
1572 if (!frame.CalculateTarget()->ResolveLoadAddress(
1573 operands[0].m_immediate, load_address)) {
1574 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001575 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001576 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1577 load_address, eSymbolContextFunction, sc);
1578 if (!sc.function) {
1579 break;
1580 }
1581 CompilerType function_type = sc.function->GetCompilerType();
1582 if (!function_type.IsFunctionType()) {
1583 break;
1584 }
1585 CompilerType return_type = function_type.GetFunctionReturnType();
1586 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001587 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588 return_value)) {
1589 break;
1590 }
1591 std::string name_str(
1592 sc.function->GetName().AsCString("<unknown function>"));
1593 name_str.append("()");
1594 Address return_value_address(return_value.GetAsUInt64());
1595 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001596 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001597 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1598 }
1599 }
1600
1601 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001602 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001603
1604 llvm::SmallVector<Instruction::Operand, 2> operands;
1605 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1606 continue;
1607 }
1608
Kate Stoneb9c1b512016-09-06 20:57:50 +00001609 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001610 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1611 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1612 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001613
1614 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001615 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001616 }
1617 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001618 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001619 }
1620 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 continue;
1622 }
1623
1624 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001625 ValueObjectSP source_path;
1626 ConstString origin_register;
1627 int64_t origin_offset = 0;
1628
1629 if (FetchRegOp(origin_register)(*origin_operand)) {
1630 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1631 variables, instruction_sp->GetAddress());
1632 } else if (MatchUnaryOp(
1633 MatchOpType(Instruction::Operand::Type::Dereference),
1634 FetchRegOp(origin_register))(*origin_operand) ||
1635 MatchUnaryOp(
1636 MatchOpType(Instruction::Operand::Type::Dereference),
1637 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1638 FetchRegOp(origin_register),
1639 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001640 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001641 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001642 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001643 if (!source_path) {
1644 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001646 source_path =
1647 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 }
1649
1650 if (source_path) {
1651 return source_path;
1652 }
1653 }
1654
1655 return ValueObjectSP();
1656}
1657}
1658
1659lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1660 int64_t offset) {
1661 TargetSP target_sp = CalculateTarget();
1662
1663 const ArchSpec &target_arch = target_sp->GetArchitecture();
1664
1665 Block *frame_block = GetFrameBlock();
1666
1667 if (!frame_block) {
1668 return ValueObjectSP();
1669 }
1670
1671 Function *function = frame_block->CalculateSymbolContextFunction();
1672 if (!function) {
1673 return ValueObjectSP();
1674 }
1675
1676 AddressRange pc_range = function->GetAddressRange();
1677
1678 if (GetFrameCodeAddress().GetFileAddress() <
1679 pc_range.GetBaseAddress().GetFileAddress() ||
1680 GetFrameCodeAddress().GetFileAddress() -
1681 pc_range.GetBaseAddress().GetFileAddress() >=
1682 pc_range.GetByteSize()) {
1683 return ValueObjectSP();
1684 }
1685
1686 ExecutionContext exe_ctx(shared_from_this());
1687
1688 const char *plugin_name = nullptr;
1689 const char *flavor = nullptr;
1690 const bool prefer_file_cache = false;
1691 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1692 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1693
1694 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1695 return ValueObjectSP();
1696 }
1697
1698 const bool get_file_globals = false;
1699 VariableList *variables = GetVariableList(get_file_globals);
1700
1701 if (!variables) {
1702 return ValueObjectSP();
1703 }
1704
1705 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1706 GetFrameCodeAddress());
1707}
1708
1709TargetSP StackFrame::CalculateTarget() {
1710 TargetSP target_sp;
1711 ThreadSP thread_sp(GetThread());
1712 if (thread_sp) {
1713 ProcessSP process_sp(thread_sp->CalculateProcess());
1714 if (process_sp)
1715 target_sp = process_sp->CalculateTarget();
1716 }
1717 return target_sp;
1718}
1719
1720ProcessSP StackFrame::CalculateProcess() {
1721 ProcessSP process_sp;
1722 ThreadSP thread_sp(GetThread());
1723 if (thread_sp)
1724 process_sp = thread_sp->CalculateProcess();
1725 return process_sp;
1726}
1727
1728ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1729
1730StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1731
1732void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1733 exe_ctx.SetContext(shared_from_this());
1734}
1735
1736void StackFrame::DumpUsingSettingsFormat(Stream *strm,
1737 const char *frame_marker) {
1738 if (strm == nullptr)
1739 return;
1740
1741 GetSymbolContext(eSymbolContextEverything);
1742 ExecutionContext exe_ctx(shared_from_this());
1743 StreamString s;
1744
1745 if (frame_marker)
1746 s.PutCString(frame_marker);
1747
1748 const FormatEntity::Entry *frame_format = nullptr;
1749 Target *target = exe_ctx.GetTargetPtr();
1750 if (target)
1751 frame_format = target->GetDebugger().GetFrameFormat();
1752 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1753 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001754 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001755 } else {
1756 Dump(strm, true, false);
1757 strm->EOL();
1758 }
1759}
1760
1761void StackFrame::Dump(Stream *strm, bool show_frame_index,
1762 bool show_fullpaths) {
1763 if (strm == nullptr)
1764 return;
1765
1766 if (show_frame_index)
1767 strm->Printf("frame #%u: ", m_frame_index);
1768 ExecutionContext exe_ctx(shared_from_this());
1769 Target *target = exe_ctx.GetTargetPtr();
1770 strm->Printf("0x%0*" PRIx64 " ",
1771 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1772 : 16,
1773 GetFrameCodeAddress().GetLoadAddress(target));
1774 GetSymbolContext(eSymbolContextEverything);
1775 const bool show_module = true;
1776 const bool show_inline = true;
1777 const bool show_function_arguments = true;
1778 const bool show_function_name = true;
1779 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1780 GetFrameCodeAddress(), show_fullpaths, show_module,
1781 show_inline, show_function_arguments,
1782 show_function_name);
1783}
1784
1785void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1786 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1787 assert(GetStackID() ==
1788 prev_frame.GetStackID()); // TODO: remove this after some testing
1789 m_variable_list_sp = prev_frame.m_variable_list_sp;
1790 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001791 if (!m_disassembly.GetString().empty()) {
1792 m_disassembly.Clear();
1793 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1794 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001795}
1796
1797void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1798 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1799 assert(GetStackID() ==
1800 curr_frame.GetStackID()); // TODO: remove this after some testing
1801 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1802 assert(GetThread() == curr_frame.GetThread());
1803 m_frame_index = curr_frame.m_frame_index;
1804 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1805 m_reg_context_sp = curr_frame.m_reg_context_sp;
1806 m_frame_code_addr = curr_frame.m_frame_code_addr;
1807 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1808 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1809 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1810 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1811 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1812 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1813 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1814 m_sc.function == curr_frame.m_sc.function);
1815 m_sc = curr_frame.m_sc;
1816 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1817 m_flags.Set(m_sc.GetResolvedMask());
1818 m_frame_base.Clear();
1819 m_frame_base_error.Clear();
1820}
1821
1822bool StackFrame::HasCachedData() const {
1823 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001824 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001825 if (m_variable_list_value_objects.GetSize() > 0)
1826 return true;
1827 if (!m_disassembly.GetString().empty())
1828 return true;
1829 return false;
1830}
1831
1832bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1833 const char *frame_marker) {
1834
1835 if (show_frame_info) {
1836 strm.Indent();
1837 DumpUsingSettingsFormat(&strm, frame_marker);
1838 }
1839
1840 if (show_source) {
1841 ExecutionContext exe_ctx(shared_from_this());
1842 bool have_source = false, have_debuginfo = false;
1843 Debugger::StopDisassemblyType disasm_display =
1844 Debugger::eStopDisassemblyTypeNever;
1845 Target *target = exe_ctx.GetTargetPtr();
1846 if (target) {
1847 Debugger &debugger = target->GetDebugger();
1848 const uint32_t source_lines_before =
1849 debugger.GetStopSourceLineCount(true);
1850 const uint32_t source_lines_after =
1851 debugger.GetStopSourceLineCount(false);
1852 disasm_display = debugger.GetStopDisassemblyDisplay();
1853
1854 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1855 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1856 have_debuginfo = true;
1857 if (source_lines_before > 0 || source_lines_after > 0) {
1858 size_t num_lines =
1859 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1860 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001861 m_sc.line_entry.column, source_lines_before,
1862 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001863 if (num_lines != 0)
1864 have_source = true;
1865 // TODO: Give here a one time warning if source file is missing.
1866 }
1867 }
1868 switch (disasm_display) {
1869 case Debugger::eStopDisassemblyTypeNever:
1870 break;
1871
1872 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1873 if (have_debuginfo)
1874 break;
1875 LLVM_FALLTHROUGH;
1876
1877 case Debugger::eStopDisassemblyTypeNoSource:
1878 if (have_source)
1879 break;
1880 LLVM_FALLTHROUGH;
1881
1882 case Debugger::eStopDisassemblyTypeAlways:
1883 if (target) {
1884 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1885 if (disasm_lines > 0) {
1886 const ArchSpec &target_arch = target->GetArchitecture();
1887 AddressRange pc_range;
1888 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1889 pc_range.SetByteSize(disasm_lines *
1890 target_arch.GetMaximumOpcodeByteSize());
1891 const char *plugin_name = nullptr;
1892 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001893 const bool mixed_source_and_assembly = false;
1894 Disassembler::Disassemble(
1895 target->GetDebugger(), target_arch, plugin_name, flavor,
1896 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1897 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001898 }
1899 }
1900 break;
1901 }
1902 }
1903 }
1904 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001905}