blob: e491c594a1f31e41e3dfb3aafd37d8c557d6f16c [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,
232 exe_ctx, 0, 0, 0, m_disassembly);
233 }
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\"?",
642 var_expr_path_strm.GetString().c_str(),
643 child_name.GetCString(),
644 var_expr_path_strm.GetString().c_str(), var_path.c_str());
645 else
646 error.SetErrorStringWithFormat(
647 "\"%s\" is not a pointer and -> was used to attempt to "
648 "access \"%s\". Did you mean \"%s.%s\"?",
649 var_expr_path_strm.GetString().c_str(),
650 child_name.GetCString(),
651 var_expr_path_strm.GetString().c_str(), var_path.c_str());
652 return ValueObjectSP();
653 }
654 }
655 child_valobj_sp =
656 valobj_sp->GetChildMemberWithName(child_name, true);
657 if (!child_valobj_sp) {
658 if (!no_synth_child) {
659 child_valobj_sp = valobj_sp->GetSyntheticValue();
660 if (child_valobj_sp)
661 child_valobj_sp =
662 child_valobj_sp->GetChildMemberWithName(child_name, true);
663 }
664
665 if (no_synth_child || !child_valobj_sp) {
666 // No child member with name "child_name"
667 if (synthetically_added_instance_object) {
668 // We added a "this->" or "self->" to the beginning of the
669 // expression
670 // and this is the first pointer ivar access, so just return
671 // the normal
672 // error
673 error.SetErrorStringWithFormat(
674 "no variable or instance variable named '%s' found in "
675 "this frame",
676 name_const_string.GetCString());
677 } else {
678 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
679 if (child_name) {
680 error.SetErrorStringWithFormat(
681 "\"%s\" is not a member of \"(%s) %s\"",
682 child_name.GetCString(),
683 valobj_sp->GetTypeName().AsCString("<invalid type>"),
684 var_expr_path_strm.GetString().c_str());
685 } else {
686 error.SetErrorStringWithFormat(
687 "incomplete expression path after \"%s\" in \"%s\"",
688 var_expr_path_strm.GetString().c_str(), var_expr_cstr);
689 }
690 }
691 return ValueObjectSP();
692 }
693 }
694 synthetically_added_instance_object = false;
695 // Remove the child name from the path
696 var_path.erase(0, child_name.GetLength());
697 if (use_dynamic != eNoDynamicValues) {
698 ValueObjectSP dynamic_value_sp(
699 child_valobj_sp->GetDynamicValue(use_dynamic));
700 if (dynamic_value_sp)
701 child_valobj_sp = dynamic_value_sp;
702 }
703 } break;
704
705 case '[':
706 // Array member access, or treating pointer as an array
707 if (var_path.size() > 2) // Need at least two brackets and a number
708 {
709 char *end = nullptr;
710 long child_index = ::strtol(&var_path[1], &end, 0);
711 if (end && *end == ']' &&
712 *(end - 1) != '[') // this code forces an error in the case of
713 // arr[]. as bitfield[] is not a good
714 // syntax we're good to go
715 {
716 if (valobj_sp->GetCompilerType().IsPointerToScalarType() &&
717 deref) {
718 // what we have is *ptr[low]. the most similar C++ syntax is
719 // to deref ptr
720 // and extract bit low out of it. reading array item low
721 // would be done by saying ptr[low], without a deref * sign
722 Error error;
723 ValueObjectSP temp(valobj_sp->Dereference(error));
724 if (error.Fail()) {
725 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
726 error.SetErrorStringWithFormat(
727 "could not dereference \"(%s) %s\"",
728 valobj_sp->GetTypeName().AsCString("<invalid type>"),
729 var_expr_path_strm.GetString().c_str());
730 return ValueObjectSP();
731 }
732 valobj_sp = temp;
733 deref = false;
734 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
735 deref) {
736 // what we have is *arr[low]. the most similar C++ syntax is
737 // to get arr[0]
738 // (an operation that is equivalent to deref-ing arr)
739 // and extract bit low out of it. reading array item low
740 // would be done by saying arr[low], without a deref * sign
741 Error error;
742 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
743 if (error.Fail()) {
744 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
745 error.SetErrorStringWithFormat(
746 "could not get item 0 for \"(%s) %s\"",
747 valobj_sp->GetTypeName().AsCString("<invalid type>"),
748 var_expr_path_strm.GetString().c_str());
749 return ValueObjectSP();
750 }
751 valobj_sp = temp;
752 deref = false;
753 }
754
755 bool is_incomplete_array = false;
756 if (valobj_sp->IsPointerType()) {
757 bool is_objc_pointer = true;
758
759 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
760 eLanguageTypeObjC)
761 is_objc_pointer = false;
762 else if (!valobj_sp->GetCompilerType().IsPointerType())
763 is_objc_pointer = false;
764
765 if (no_synth_child && is_objc_pointer) {
766 error.SetErrorStringWithFormat(
767 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
768 "subscripted",
769 valobj_sp->GetTypeName().AsCString("<invalid type>"),
770 var_expr_path_strm.GetString().c_str());
771
772 return ValueObjectSP();
773 } else if (is_objc_pointer) {
774 // dereferencing ObjC variables is not valid.. so let's try
775 // and recur to synthetic children
776 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
777 if (!synthetic /* no synthetic */
778 || synthetic == valobj_sp) /* synthetic is the same as
779 the original object */
780 {
781 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
782 error.SetErrorStringWithFormat(
783 "\"(%s) %s\" is not an array type",
784 valobj_sp->GetTypeName().AsCString("<invalid type>"),
785 var_expr_path_strm.GetString().c_str());
786 } else if (
787 static_cast<uint32_t>(child_index) >=
788 synthetic
789 ->GetNumChildren() /* synthetic does not have that many values */) {
790 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
791 error.SetErrorStringWithFormat(
792 "array index %ld is not valid for \"(%s) %s\"",
793 child_index,
794 valobj_sp->GetTypeName().AsCString("<invalid type>"),
795 var_expr_path_strm.GetString().c_str());
796 } else {
797 child_valobj_sp =
798 synthetic->GetChildAtIndex(child_index, true);
799 if (!child_valobj_sp) {
800 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
801 error.SetErrorStringWithFormat(
802 "array index %ld is not valid for \"(%s) %s\"",
803 child_index, valobj_sp->GetTypeName().AsCString(
804 "<invalid type>"),
805 var_expr_path_strm.GetString().c_str());
806 }
807 }
808 } else {
809 child_valobj_sp =
810 valobj_sp->GetSyntheticArrayMember(child_index, true);
811 if (!child_valobj_sp) {
812 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
813 error.SetErrorStringWithFormat(
814 "failed to use pointer as array for index %ld for "
815 "\"(%s) %s\"",
816 child_index,
817 valobj_sp->GetTypeName().AsCString("<invalid type>"),
818 var_expr_path_strm.GetString().c_str());
819 }
820 }
821 } else if (valobj_sp->GetCompilerType().IsArrayType(
822 nullptr, nullptr, &is_incomplete_array)) {
823 // Pass false to dynamic_value here so we can tell the
824 // difference between
825 // no dynamic value and no member of this type...
826 child_valobj_sp =
827 valobj_sp->GetChildAtIndex(child_index, true);
828 if (!child_valobj_sp &&
829 (is_incomplete_array || !no_synth_child))
830 child_valobj_sp =
831 valobj_sp->GetSyntheticArrayMember(child_index, true);
832
833 if (!child_valobj_sp) {
834 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
835 error.SetErrorStringWithFormat(
836 "array index %ld is not valid for \"(%s) %s\"",
837 child_index,
838 valobj_sp->GetTypeName().AsCString("<invalid type>"),
839 var_expr_path_strm.GetString().c_str());
840 }
841 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
842 // this is a bitfield asking to display just one bit
843 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
844 child_index, child_index, true);
845 if (!child_valobj_sp) {
846 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
847 error.SetErrorStringWithFormat(
848 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
849 child_index, child_index,
850 valobj_sp->GetTypeName().AsCString("<invalid type>"),
851 var_expr_path_strm.GetString().c_str());
852 }
853 } else {
854 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
855 if (no_synth_child /* synthetic is forbidden */ ||
856 !synthetic /* no synthetic */
857 ||
858 synthetic == valobj_sp) /* synthetic is the same as the
859 original object */
860 {
861 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
862 error.SetErrorStringWithFormat(
863 "\"(%s) %s\" is not an array type",
864 valobj_sp->GetTypeName().AsCString("<invalid type>"),
865 var_expr_path_strm.GetString().c_str());
866 } else if (
867 static_cast<uint32_t>(child_index) >=
868 synthetic
869 ->GetNumChildren() /* synthetic does not have that many values */) {
870 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
871 error.SetErrorStringWithFormat(
872 "array index %ld is not valid for \"(%s) %s\"",
873 child_index,
874 valobj_sp->GetTypeName().AsCString("<invalid type>"),
875 var_expr_path_strm.GetString().c_str());
876 } else {
877 child_valobj_sp =
878 synthetic->GetChildAtIndex(child_index, true);
879 if (!child_valobj_sp) {
880 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
881 error.SetErrorStringWithFormat(
882 "array index %ld is not valid for \"(%s) %s\"",
883 child_index,
884 valobj_sp->GetTypeName().AsCString("<invalid type>"),
885 var_expr_path_strm.GetString().c_str());
886 }
887 }
888 }
889
890 if (!child_valobj_sp) {
891 // Invalid array index...
892 return ValueObjectSP();
893 }
894
895 // Erase the array member specification '[%i]' where
896 // %i is the array index
897 var_path.erase(0, (end - var_path.c_str()) + 1);
898 separator_idx = var_path.find_first_of(".-[");
899 if (use_dynamic != eNoDynamicValues) {
900 ValueObjectSP dynamic_value_sp(
901 child_valobj_sp->GetDynamicValue(use_dynamic));
902 if (dynamic_value_sp)
903 child_valobj_sp = dynamic_value_sp;
904 }
905 // Break out early from the switch since we were
906 // able to find the child member
907 break;
908 } else if (end && *end == '-') {
909 // this is most probably a BitField, let's take a look
910 char *real_end = nullptr;
911 long final_index = ::strtol(end + 1, &real_end, 0);
912 bool expand_bitfield = true;
913 if (real_end && *real_end == ']') {
914 // if the format given is [high-low], swap range
915 if (child_index > final_index) {
916 long temp = child_index;
917 child_index = final_index;
918 final_index = temp;
919 }
920
921 if (valobj_sp->GetCompilerType().IsPointerToScalarType() &&
922 deref) {
923 // what we have is *ptr[low-high]. the most similar C++
924 // syntax is to deref ptr
925 // and extract bits low thru high out of it. reading array
926 // items low thru high
927 // would be done by saying ptr[low-high], without a deref *
928 // sign
929 Error error;
930 ValueObjectSP temp(valobj_sp->Dereference(error));
931 if (error.Fail()) {
932 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
933 error.SetErrorStringWithFormat(
934 "could not dereference \"(%s) %s\"",
935 valobj_sp->GetTypeName().AsCString("<invalid type>"),
936 var_expr_path_strm.GetString().c_str());
937 return ValueObjectSP();
938 }
939 valobj_sp = temp;
940 deref = false;
941 } else if (valobj_sp->GetCompilerType()
942 .IsArrayOfScalarType() &&
943 deref) {
944 // what we have is *arr[low-high]. the most similar C++
945 // syntax is to get arr[0]
946 // (an operation that is equivalent to deref-ing arr)
947 // and extract bits low thru high out of it. reading array
948 // items low thru high
949 // would be done by saying arr[low-high], without a deref *
950 // sign
951 Error error;
952 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
953 if (error.Fail()) {
954 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
955 error.SetErrorStringWithFormat(
956 "could not get item 0 for \"(%s) %s\"",
957 valobj_sp->GetTypeName().AsCString("<invalid type>"),
958 var_expr_path_strm.GetString().c_str());
959 return ValueObjectSP();
960 }
961 valobj_sp = temp;
962 deref = false;
963 }
964 /*else if (valobj_sp->IsArrayType() ||
965 valobj_sp->IsPointerType())
966 {
967 child_valobj_sp =
968 valobj_sp->GetSyntheticArrayRangeChild(child_index,
969 final_index, true);
970 expand_bitfield = false;
971 if (!child_valobj_sp)
972 {
973 valobj_sp->GetExpressionPath (var_expr_path_strm,
974 false);
975 error.SetErrorStringWithFormat ("array range %i-%i is
976 not valid for \"(%s) %s\"",
977 child_index,
978 final_index,
979 valobj_sp->GetTypeName().AsCString("<invalid
980 type>"),
981 var_expr_path_strm.GetString().c_str());
982 }
983 }*/
984
985 if (expand_bitfield) {
986 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
987 child_index, final_index, true);
988 if (!child_valobj_sp) {
989 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
990 error.SetErrorStringWithFormat(
991 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
992 child_index, final_index,
993 valobj_sp->GetTypeName().AsCString("<invalid type>"),
994 var_expr_path_strm.GetString().c_str());
995 }
996 }
997 }
998
999 if (!child_valobj_sp) {
1000 // Invalid bitfield range...
1001 return ValueObjectSP();
1002 }
1003
1004 // Erase the bitfield member specification '[%i-%i]' where
1005 // %i is the index
1006 var_path.erase(0, (real_end - var_path.c_str()) + 1);
1007 separator_idx = var_path.find_first_of(".-[");
1008 if (use_dynamic != eNoDynamicValues) {
1009 ValueObjectSP dynamic_value_sp(
1010 child_valobj_sp->GetDynamicValue(use_dynamic));
1011 if (dynamic_value_sp)
1012 child_valobj_sp = dynamic_value_sp;
1013 }
1014 // Break out early from the switch since we were
1015 // able to find the child member
1016 break;
1017 }
1018 } else {
1019 error.SetErrorStringWithFormat(
1020 "invalid square bracket encountered after \"%s\" in \"%s\"",
1021 var_expr_path_strm.GetString().c_str(), var_path.c_str());
1022 }
1023 return ValueObjectSP();
1024
1025 default:
1026 // Failure...
1027 {
1028 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1029 error.SetErrorStringWithFormat(
1030 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1031 separator_type, var_expr_path_strm.GetString().c_str(),
1032 var_path.c_str());
1033
1034 return ValueObjectSP();
1035 }
1036 }
1037
1038 if (child_valobj_sp)
1039 valobj_sp = child_valobj_sp;
1040
1041 if (var_path.empty())
1042 break;
1043 }
1044 if (valobj_sp) {
1045 if (deref) {
1046 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1047 valobj_sp = deref_valobj_sp;
1048 } else if (address_of) {
1049 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1050 valobj_sp = address_of_valobj_sp;
1051 }
1052 }
1053 return valobj_sp;
1054 } else {
1055 error.SetErrorStringWithFormat(
1056 "no variable named '%s' found in this frame",
1057 name_const_string.GetCString());
1058 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 } else {
1061 error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1062 }
1063 return ValueObjectSP();
1064}
1065
1066bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Error *error_ptr) {
1067 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1068 if (!m_cfa_is_valid) {
1069 m_frame_base_error.SetErrorString(
1070 "No frame base available for this historical stack frame.");
1071 return false;
1072 }
1073
1074 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1075 if (m_sc.function) {
1076 m_frame_base.Clear();
1077 m_frame_base_error.Clear();
1078
1079 m_flags.Set(GOT_FRAME_BASE);
1080 ExecutionContext exe_ctx(shared_from_this());
1081 Value expr_value;
1082 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1083 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1084 loclist_base_addr =
1085 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1086 exe_ctx.GetTargetPtr());
1087
1088 if (m_sc.function->GetFrameBaseExpression().Evaluate(
1089 &exe_ctx, nullptr, nullptr, nullptr, loclist_base_addr, nullptr,
1090 nullptr, expr_value, &m_frame_base_error) == false) {
1091 // We should really have an error if evaluate returns, but in case
1092 // we don't, lets set the error to something at least.
1093 if (m_frame_base_error.Success())
1094 m_frame_base_error.SetErrorString(
1095 "Evaluation of the frame base expression failed.");
1096 } else {
1097 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1098 }
1099 } else {
1100 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001101 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001102 }
1103
1104 if (m_frame_base_error.Success())
1105 frame_base = m_frame_base;
1106
1107 if (error_ptr)
1108 *error_ptr = m_frame_base_error;
1109 return m_frame_base_error.Success();
1110}
1111
1112DWARFExpression *StackFrame::GetFrameBaseExpression(Error *error_ptr) {
1113 if (!m_sc.function) {
1114 if (error_ptr) {
1115 error_ptr->SetErrorString("No function in symbol context.");
1116 }
1117 return nullptr;
1118 }
1119
1120 return &m_sc.function->GetFrameBaseExpression();
1121}
1122
1123RegisterContextSP StackFrame::GetRegisterContext() {
1124 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1125 if (!m_reg_context_sp) {
1126 ThreadSP thread_sp(GetThread());
1127 if (thread_sp)
1128 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1129 }
1130 return m_reg_context_sp;
1131}
1132
1133bool StackFrame::HasDebugInformation() {
1134 GetSymbolContext(eSymbolContextLineEntry);
1135 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001136}
1137
1138ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001139StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1140 DynamicValueType use_dynamic) {
1141 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1142 ValueObjectSP valobj_sp;
1143 if (m_is_history_frame) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001144 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001145 }
1146 VariableList *var_list = GetVariableList(true);
1147 if (var_list) {
1148 // Make sure the variable is a frame variable
1149 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1150 const uint32_t num_variables = var_list->GetSize();
1151 if (var_idx < num_variables) {
1152 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1153 if (!valobj_sp) {
1154 if (m_variable_list_value_objects.GetSize() < num_variables)
1155 m_variable_list_value_objects.Resize(num_variables);
1156 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1157 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1158 }
Enrico Granata592afe72016-03-15 21:50:51 +00001159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 }
1161 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1162 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1163 if (dynamic_sp)
1164 return dynamic_sp;
1165 }
1166 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001167}
1168
Kate Stoneb9c1b512016-09-06 20:57:50 +00001169ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1170 DynamicValueType use_dynamic) {
1171 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1172 if (m_is_history_frame)
1173 return ValueObjectSP();
1174
1175 // Check to make sure we aren't already tracking this variable?
1176 ValueObjectSP valobj_sp(
1177 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1178 if (!valobj_sp) {
1179 // We aren't already tracking this global
1180 VariableList *var_list = GetVariableList(true);
1181 // If this frame has no variables, create a new list
1182 if (var_list == nullptr)
1183 m_variable_list_sp.reset(new VariableList());
1184
1185 // Add the global/static variable to this frame
1186 m_variable_list_sp->AddVariable(variable_sp);
1187
1188 // Now make a value object for it so we can track its changes
1189 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1190 }
1191 return valobj_sp;
1192}
1193
1194bool StackFrame::IsInlined() {
1195 if (m_sc.block == nullptr)
1196 GetSymbolContext(eSymbolContextBlock);
1197 if (m_sc.block)
1198 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1199 return false;
1200}
1201
1202lldb::LanguageType StackFrame::GetLanguage() {
1203 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1204 if (cu)
1205 return cu->GetLanguage();
1206 return lldb::eLanguageTypeUnknown;
1207}
1208
1209lldb::LanguageType StackFrame::GuessLanguage() {
1210 LanguageType lang_type = GetLanguage();
1211
1212 if (lang_type == eLanguageTypeUnknown) {
1213 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1214 if (f) {
1215 lang_type = f->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 }
1283}
1284
1285std::pair<const Instruction::Operand *, int64_t>
1286GetBaseExplainingDereference(const Instruction::Operand &operand,
1287 RegisterContext &register_context,
1288 lldb::addr_t addr) {
1289 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1290 return GetBaseExplainingValue(operand.m_children[0], register_context,
1291 addr);
1292 }
1293 return std::make_pair(nullptr, 0);
1294}
Sean Callanan4740a732016-09-06 04:48:36 +00001295};
1296
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1298 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001299
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300 const ArchSpec &target_arch = target_sp->GetArchitecture();
1301
1302 AddressRange pc_range;
1303 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1304 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1305
1306 ExecutionContext exe_ctx(shared_from_this());
1307
1308 const char *plugin_name = nullptr;
1309 const char *flavor = nullptr;
1310 const bool prefer_file_cache = false;
1311
1312 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1313 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1314
1315 if (!disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001316 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 }
Sean Callanan4740a732016-09-06 04:48:36 +00001318
Kate Stoneb9c1b512016-09-06 20:57:50 +00001319 InstructionSP instruction_sp =
1320 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1321
1322 llvm::SmallVector<Instruction::Operand, 3> operands;
1323
1324 if (!instruction_sp->ParseOperands(operands)) {
1325 return ValueObjectSP();
1326 }
1327
1328 RegisterContextSP register_context_sp = GetRegisterContext();
1329
1330 if (!register_context_sp) {
1331 return ValueObjectSP();
1332 }
1333
1334 for (const Instruction::Operand &operand : operands) {
1335 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1336 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1337
1338 if (!base_and_offset.first) {
1339 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001340 }
Sean Callanan4740a732016-09-06 04:48:36 +00001341
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 switch (base_and_offset.first->m_type) {
1343 case Instruction::Operand::Type::Immediate: {
1344 lldb_private::Address addr;
1345 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1346 base_and_offset.second,
1347 addr)) {
1348 TypeSystem *c_type_system =
1349 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1350 if (!c_type_system) {
1351 return ValueObjectSP();
1352 } else {
1353 CompilerType void_ptr_type =
1354 c_type_system
1355 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1356 .GetPointerType();
1357 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001358 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001359 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001360 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361 }
1362 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001363 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364 case Instruction::Operand::Type::Register: {
1365 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1366 base_and_offset.second);
1367 }
1368 default:
1369 return ValueObjectSP();
1370 }
1371 }
1372
1373 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001374}
1375
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376namespace {
1377ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1378 int64_t offset) {
1379 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1380 return ValueObjectSP();
1381 }
Sean Callanan4740a732016-09-06 04:48:36 +00001382
Kate Stoneb9c1b512016-09-06 20:57:50 +00001383 if (parent->IsPointerOrReferenceType()) {
1384 return parent;
1385 }
1386
1387 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1388 const bool can_create = true;
1389 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1390
1391 if (!child_sp) {
1392 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001393 }
1394
Kate Stoneb9c1b512016-09-06 20:57:50 +00001395 int64_t child_offset = child_sp->GetByteOffset();
1396 int64_t child_size = child_sp->GetByteSize();
1397
1398 if (offset >= child_offset && offset < (child_offset + child_size)) {
1399 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001401 }
Sean Callanan4740a732016-09-06 04:48:36 +00001402
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 if (offset == 0) {
1404 return parent;
1405 } else {
1406 return ValueObjectSP();
1407 }
1408}
1409
1410ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1411 ValueObjectSP &base,
1412 int64_t offset) {
1413 // base is a pointer to something
1414 // offset is the thing to add to the pointer
1415 // We return the most sensible ValueObject for the result of *(base+offset)
1416
1417 if (!base->IsPointerOrReferenceType()) {
1418 return ValueObjectSP();
1419 }
1420
1421 Error error;
1422 ValueObjectSP pointee = base->Dereference(error);
1423
1424 if (offset >= pointee->GetByteSize()) {
1425 int64_t index = offset / pointee->GetByteSize();
1426 offset = offset % pointee->GetByteSize();
1427 const bool can_create = true;
1428 pointee = base->GetSyntheticArrayMember(index, can_create);
1429 }
1430
1431 if (!pointee || error.Fail()) {
1432 return ValueObjectSP();
1433 }
1434
1435 return GetValueForOffset(frame, pointee, offset);
1436}
1437
1438//------------------------------------------------------------------
1439/// Attempt to reconstruct the ValueObject for the address contained in a
1440/// given register plus an offset.
1441///
1442/// @params [in] frame
1443/// The current stack frame.
1444///
1445/// @params [in] reg
1446/// The register.
1447///
1448/// @params [in] offset
1449/// The offset from the register.
1450///
1451/// @param [in] disassembler
1452/// A disassembler containing instructions valid up to the current PC.
1453///
1454/// @param [in] variables
1455/// The variable list from the current frame,
1456///
1457/// @param [in] pc
1458/// The program counter for the instruction considered the 'user'.
1459///
1460/// @return
1461/// A string describing the base for the ExpressionPath. This could be a
1462/// variable, a register value, an argument, or a function return value.
1463/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1464//------------------------------------------------------------------
1465lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1466 int64_t offset, Disassembler &disassembler,
1467 VariableList &variables, const Address &pc) {
1468 // Example of operation for Intel:
1469 //
1470 // +14: movq -0x8(%rbp), %rdi
1471 // +18: movq 0x8(%rdi), %rdi
1472 // +22: addl 0x4(%rdi), %eax
1473 //
1474 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1475 //
1476 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18
1477 // that assigns to rdi, and calls itself recursively for that dereference
1478 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1479 // +14 that assigns to rdi, and calls itself recursively for that
1480 // derefernece
1481 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1482 // variable list.
1483 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1484 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1485 // at +18)
1486 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1487 // rdi+4 at +22)
1488
1489 // First, check the variable list to see if anything is at the specified
1490 // location.
1491 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1492 VariableSP var_sp = variables.GetVariableAtIndex(vi);
1493 DWARFExpression &dwarf_expression = var_sp->LocationExpression();
1494
1495 const RegisterInfo *expression_reg;
1496 int64_t expression_offset;
1497 ExecutionContext exe_ctx;
1498
1499 if (dwarf_expression.IsDereferenceOfRegister(frame, expression_reg,
1500 expression_offset)) {
1501 if ((reg == ConstString(expression_reg->name) ||
1502 reg == ConstString(expression_reg->alt_name)) &&
1503 expression_offset == offset) {
1504 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1505 }
Sean Callanan4740a732016-09-06 04:48:36 +00001506 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 }
1508
1509 bool is_in_return_register = false;
1510 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1511 RegisterInfo return_register_info;
1512
1513 if (abi_sp) {
1514 const char *return_register_name;
1515 const RegisterInfo *reg_info = nullptr;
1516 if (abi_sp->GetPointerReturnRegister(return_register_name) &&
1517 reg == ConstString(return_register_name) &&
1518 (reg_info = frame.GetRegisterContext()->GetRegisterInfoByName(
1519 return_register_name))) {
1520 is_in_return_register = true;
1521 return_register_info = *reg_info;
Sean Callanan4740a732016-09-06 04:48:36 +00001522 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 }
Sean Callanan4740a732016-09-06 04:48:36 +00001524
Kate Stoneb9c1b512016-09-06 20:57:50 +00001525 const uint32_t current_inst =
1526 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1527 if (current_inst == UINT32_MAX) {
1528 return ValueObjectSP();
1529 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001530
Kate Stoneb9c1b512016-09-06 20:57:50 +00001531 ValueObjectSP source_path;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1534 // This is not an exact algorithm, and it sacrifices accuracy for
1535 // generality.
1536 // Recognizing "mov" and "ld" instructions –– and which are their source and
1537 // destination operands -- is something the disassembler should do for us.
1538 InstructionSP instruction_sp =
1539 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540
Kate Stoneb9c1b512016-09-06 20:57:50 +00001541 if (is_in_return_register && instruction_sp->IsCall()) {
1542 llvm::SmallVector<Instruction::Operand, 1> operands;
1543 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1544 continue;
1545 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546
Kate Stoneb9c1b512016-09-06 20:57:50 +00001547 switch (operands[0].m_type) {
1548 default:
1549 break;
1550 case Instruction::Operand::Type::Immediate: {
1551 SymbolContext sc;
1552 Address load_address;
1553 if (!frame.CalculateTarget()->ResolveLoadAddress(
1554 operands[0].m_immediate, load_address)) {
1555 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001556 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001557 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1558 load_address, eSymbolContextFunction, sc);
1559 if (!sc.function) {
1560 break;
1561 }
1562 CompilerType function_type = sc.function->GetCompilerType();
1563 if (!function_type.IsFunctionType()) {
1564 break;
1565 }
1566 CompilerType return_type = function_type.GetFunctionReturnType();
1567 RegisterValue return_value;
1568 if (!frame.GetRegisterContext()->ReadRegister(&return_register_info,
1569 return_value)) {
1570 break;
1571 }
1572 std::string name_str(
1573 sc.function->GetName().AsCString("<unknown function>"));
1574 name_str.append("()");
1575 Address return_value_address(return_value.GetAsUInt64());
1576 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
1577 &frame, name_str.c_str(), return_value_address, return_type);
1578 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1579 }
1580 }
1581
1582 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001583 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001584
1585 llvm::SmallVector<Instruction::Operand, 2> operands;
1586 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1587 continue;
1588 }
1589
1590 Instruction::Operand *register_operand = nullptr;
1591 Instruction::Operand *origin_operand = nullptr;
1592 if (operands[0].m_type == Instruction::Operand::Type::Register &&
1593 operands[0].m_clobbered == true && operands[0].m_register == reg) {
1594 register_operand = &operands[0];
1595 origin_operand = &operands[1];
1596 } else if (operands[1].m_type == Instruction::Operand::Type::Register &&
1597 operands[1].m_clobbered == true &&
1598 operands[1].m_register == reg) {
1599 register_operand = &operands[1];
1600 origin_operand = &operands[0];
1601 } else {
1602 continue;
1603 }
1604
1605 // We have an origin operand. Can we track its value down?
1606 switch (origin_operand->m_type) {
1607 default:
1608 break;
1609 case Instruction::Operand::Type::Register:
1610 source_path =
1611 DoGuessValueAt(frame, origin_operand->m_register, 0, disassembler,
1612 variables, instruction_sp->GetAddress());
1613 break;
1614 case Instruction::Operand::Type::Dereference: {
1615 const Instruction::Operand &pointer = origin_operand->m_children[0];
1616 switch (pointer.m_type) {
1617 default:
1618 break;
1619 case Instruction::Operand::Type::Register:
1620 source_path = DoGuessValueAt(frame, pointer.m_register, 0, disassembler,
1621 variables, instruction_sp->GetAddress());
1622 if (source_path) {
1623 Error err;
1624 source_path = source_path->Dereference(err);
1625 if (!err.Success()) {
1626 source_path.reset();
1627 }
1628 }
1629 break;
1630 case Instruction::Operand::Type::Sum: {
1631 const Instruction::Operand *origin_register = nullptr;
1632 const Instruction::Operand *origin_offset = nullptr;
1633 if (pointer.m_children.size() != 2) {
1634 break;
1635 }
1636 if (pointer.m_children[0].m_type ==
1637 Instruction::Operand::Type::Register &&
1638 pointer.m_children[1].m_type ==
1639 Instruction::Operand::Type::Immediate) {
1640 origin_register = &pointer.m_children[0];
1641 origin_offset = &pointer.m_children[1];
1642 } else if (pointer.m_children[1].m_type ==
1643 Instruction::Operand::Type::Register &&
1644 pointer.m_children[0].m_type ==
1645 Instruction::Operand::Type::Immediate) {
1646 origin_register = &pointer.m_children[1];
1647 origin_offset = &pointer.m_children[0];
1648 }
1649 if (!origin_register) {
1650 break;
1651 }
1652 int64_t signed_origin_offset =
1653 origin_offset->m_negative ? -((int64_t)origin_offset->m_immediate)
1654 : origin_offset->m_immediate;
1655 source_path = DoGuessValueAt(frame, origin_register->m_register,
1656 signed_origin_offset, disassembler,
1657 variables, instruction_sp->GetAddress());
1658 if (!source_path) {
1659 break;
1660 }
1661 source_path =
1662 GetValueForDereferincingOffset(frame, source_path, offset);
1663 break;
1664 }
1665 }
1666 }
1667 }
1668
1669 if (source_path) {
1670 return source_path;
1671 }
1672 }
1673
1674 return ValueObjectSP();
1675}
1676}
1677
1678lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1679 int64_t offset) {
1680 TargetSP target_sp = CalculateTarget();
1681
1682 const ArchSpec &target_arch = target_sp->GetArchitecture();
1683
1684 Block *frame_block = GetFrameBlock();
1685
1686 if (!frame_block) {
1687 return ValueObjectSP();
1688 }
1689
1690 Function *function = frame_block->CalculateSymbolContextFunction();
1691 if (!function) {
1692 return ValueObjectSP();
1693 }
1694
1695 AddressRange pc_range = function->GetAddressRange();
1696
1697 if (GetFrameCodeAddress().GetFileAddress() <
1698 pc_range.GetBaseAddress().GetFileAddress() ||
1699 GetFrameCodeAddress().GetFileAddress() -
1700 pc_range.GetBaseAddress().GetFileAddress() >=
1701 pc_range.GetByteSize()) {
1702 return ValueObjectSP();
1703 }
1704
1705 ExecutionContext exe_ctx(shared_from_this());
1706
1707 const char *plugin_name = nullptr;
1708 const char *flavor = nullptr;
1709 const bool prefer_file_cache = false;
1710 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1711 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1712
1713 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1714 return ValueObjectSP();
1715 }
1716
1717 const bool get_file_globals = false;
1718 VariableList *variables = GetVariableList(get_file_globals);
1719
1720 if (!variables) {
1721 return ValueObjectSP();
1722 }
1723
1724 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1725 GetFrameCodeAddress());
1726}
1727
1728TargetSP StackFrame::CalculateTarget() {
1729 TargetSP target_sp;
1730 ThreadSP thread_sp(GetThread());
1731 if (thread_sp) {
1732 ProcessSP process_sp(thread_sp->CalculateProcess());
1733 if (process_sp)
1734 target_sp = process_sp->CalculateTarget();
1735 }
1736 return target_sp;
1737}
1738
1739ProcessSP StackFrame::CalculateProcess() {
1740 ProcessSP process_sp;
1741 ThreadSP thread_sp(GetThread());
1742 if (thread_sp)
1743 process_sp = thread_sp->CalculateProcess();
1744 return process_sp;
1745}
1746
1747ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1748
1749StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1750
1751void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1752 exe_ctx.SetContext(shared_from_this());
1753}
1754
1755void StackFrame::DumpUsingSettingsFormat(Stream *strm,
1756 const char *frame_marker) {
1757 if (strm == nullptr)
1758 return;
1759
1760 GetSymbolContext(eSymbolContextEverything);
1761 ExecutionContext exe_ctx(shared_from_this());
1762 StreamString s;
1763
1764 if (frame_marker)
1765 s.PutCString(frame_marker);
1766
1767 const FormatEntity::Entry *frame_format = nullptr;
1768 Target *target = exe_ctx.GetTargetPtr();
1769 if (target)
1770 frame_format = target->GetDebugger().GetFrameFormat();
1771 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1772 nullptr, nullptr, false, false)) {
1773 strm->Write(s.GetData(), s.GetSize());
1774 } else {
1775 Dump(strm, true, false);
1776 strm->EOL();
1777 }
1778}
1779
1780void StackFrame::Dump(Stream *strm, bool show_frame_index,
1781 bool show_fullpaths) {
1782 if (strm == nullptr)
1783 return;
1784
1785 if (show_frame_index)
1786 strm->Printf("frame #%u: ", m_frame_index);
1787 ExecutionContext exe_ctx(shared_from_this());
1788 Target *target = exe_ctx.GetTargetPtr();
1789 strm->Printf("0x%0*" PRIx64 " ",
1790 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1791 : 16,
1792 GetFrameCodeAddress().GetLoadAddress(target));
1793 GetSymbolContext(eSymbolContextEverything);
1794 const bool show_module = true;
1795 const bool show_inline = true;
1796 const bool show_function_arguments = true;
1797 const bool show_function_name = true;
1798 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1799 GetFrameCodeAddress(), show_fullpaths, show_module,
1800 show_inline, show_function_arguments,
1801 show_function_name);
1802}
1803
1804void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1805 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1806 assert(GetStackID() ==
1807 prev_frame.GetStackID()); // TODO: remove this after some testing
1808 m_variable_list_sp = prev_frame.m_variable_list_sp;
1809 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
1810 if (!m_disassembly.GetString().empty())
1811 m_disassembly.GetString().swap(m_disassembly.GetString());
1812}
1813
1814void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1815 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1816 assert(GetStackID() ==
1817 curr_frame.GetStackID()); // TODO: remove this after some testing
1818 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1819 assert(GetThread() == curr_frame.GetThread());
1820 m_frame_index = curr_frame.m_frame_index;
1821 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1822 m_reg_context_sp = curr_frame.m_reg_context_sp;
1823 m_frame_code_addr = curr_frame.m_frame_code_addr;
1824 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1825 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1826 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1827 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1828 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1829 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1830 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1831 m_sc.function == curr_frame.m_sc.function);
1832 m_sc = curr_frame.m_sc;
1833 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1834 m_flags.Set(m_sc.GetResolvedMask());
1835 m_frame_base.Clear();
1836 m_frame_base_error.Clear();
1837}
1838
1839bool StackFrame::HasCachedData() const {
1840 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001841 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001842 if (m_variable_list_value_objects.GetSize() > 0)
1843 return true;
1844 if (!m_disassembly.GetString().empty())
1845 return true;
1846 return false;
1847}
1848
1849bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1850 const char *frame_marker) {
1851
1852 if (show_frame_info) {
1853 strm.Indent();
1854 DumpUsingSettingsFormat(&strm, frame_marker);
1855 }
1856
1857 if (show_source) {
1858 ExecutionContext exe_ctx(shared_from_this());
1859 bool have_source = false, have_debuginfo = false;
1860 Debugger::StopDisassemblyType disasm_display =
1861 Debugger::eStopDisassemblyTypeNever;
1862 Target *target = exe_ctx.GetTargetPtr();
1863 if (target) {
1864 Debugger &debugger = target->GetDebugger();
1865 const uint32_t source_lines_before =
1866 debugger.GetStopSourceLineCount(true);
1867 const uint32_t source_lines_after =
1868 debugger.GetStopSourceLineCount(false);
1869 disasm_display = debugger.GetStopDisassemblyDisplay();
1870
1871 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1872 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1873 have_debuginfo = true;
1874 if (source_lines_before > 0 || source_lines_after > 0) {
1875 size_t num_lines =
1876 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1877 m_sc.line_entry.file, m_sc.line_entry.line,
1878 source_lines_before, source_lines_after, "->", &strm);
1879 if (num_lines != 0)
1880 have_source = true;
1881 // TODO: Give here a one time warning if source file is missing.
1882 }
1883 }
1884 switch (disasm_display) {
1885 case Debugger::eStopDisassemblyTypeNever:
1886 break;
1887
1888 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1889 if (have_debuginfo)
1890 break;
1891 LLVM_FALLTHROUGH;
1892
1893 case Debugger::eStopDisassemblyTypeNoSource:
1894 if (have_source)
1895 break;
1896 LLVM_FALLTHROUGH;
1897
1898 case Debugger::eStopDisassemblyTypeAlways:
1899 if (target) {
1900 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1901 if (disasm_lines > 0) {
1902 const ArchSpec &target_arch = target->GetArchitecture();
1903 AddressRange pc_range;
1904 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1905 pc_range.SetByteSize(disasm_lines *
1906 target_arch.GetMaximumOpcodeByteSize());
1907 const char *plugin_name = nullptr;
1908 const char *flavor = nullptr;
1909 Disassembler::Disassemble(target->GetDebugger(), target_arch,
1910 plugin_name, flavor, exe_ctx, pc_range,
1911 disasm_lines, 0,
1912 Disassembler::eOptionMarkPCAddress, strm);
1913 }
1914 }
1915 break;
1916 }
1917 }
1918 }
1919 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001920}