blob: 7ca8e8ea42adcc3bcb12cc4dd3d3db6f45c3f36d [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);
Zachary Turner8b3f2162017-02-28 17:59:59 +0000224 if (m_disassembly.Empty()) {
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, false, 0, 0, m_disassembly);
233 }
234 if (m_disassembly.Empty())
235 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 }
Zachary Turner8b3f2162017-02-28 17:59:59 +0000237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239}
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241Block *StackFrame::GetFrameBlock() {
242 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
243 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000244
Kate Stoneb9c1b512016-09-06 20:57:50 +0000245 if (m_sc.block) {
246 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
247 if (inline_block) {
248 // Use the block with the inlined function info
249 // as the frame block we want this frame to have only the variables
250 // for the inlined function and its non-inlined block child blocks.
251 return inline_block;
252 } else {
253 // This block is not contained within any inlined function blocks
254 // with so we want to use the top most function block.
255 return &m_sc.function->GetBlock(false);
256 }
257 }
258 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000259}
260
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261//----------------------------------------------------------------------
262// Get the symbol context if we already haven't done so by resolving the
263// PC address as much as possible. This way when we pass around a
264// StackFrame object, everyone will have as much information as
265// possible and no one will ever have to look things up manually.
266//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267const SymbolContext &StackFrame::GetSymbolContext(uint32_t resolve_scope) {
268 std::lock_guard<std::recursive_mutex> guard(m_mutex);
269 // Copy our internal symbol context into "sc".
270 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
271 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000272
Kate Stoneb9c1b512016-09-06 20:57:50 +0000273 // If the target was requested add that:
274 if (!m_sc.target_sp) {
275 m_sc.target_sp = CalculateTarget();
276 if (m_sc.target_sp)
277 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 }
279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 // Resolve our PC to section offset if we haven't already done so
281 // and if we don't have a module. The resolved address section will
282 // contain the module to which it belongs
283 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
284 GetFrameCodeAddress();
285
286 // If this is not frame zero, then we need to subtract 1 from the PC
287 // value when doing address lookups since the PC will be on the
288 // instruction following the function call instruction...
289
290 Address lookup_addr(GetFrameCodeAddress());
291 if (m_frame_index > 0 && lookup_addr.IsValid()) {
292 addr_t offset = lookup_addr.GetOffset();
293 if (offset > 0) {
294 lookup_addr.SetOffset(offset - 1);
295
296 } else {
297 // lookup_addr is the start of a section. We need
298 // do the math on the actual load address and re-compute
299 // the section. We're working with a 'noreturn' function
300 // at the end of a section.
301 ThreadSP thread_sp(GetThread());
302 if (thread_sp) {
303 TargetSP target_sp(thread_sp->CalculateTarget());
304 if (target_sp) {
305 addr_t addr_minus_one =
306 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
307 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
308 } else {
309 lookup_addr.SetOffset(offset - 1);
310 }
311 }
312 }
313 }
314
315 if (m_sc.module_sp) {
316 // We have something in our stack frame symbol context, lets check
317 // if we haven't already tried to lookup one of those things. If we
318 // haven't then we will do the query.
319
320 uint32_t actual_resolve_scope = 0;
321
322 if (resolve_scope & eSymbolContextCompUnit) {
323 if (m_flags.IsClear(eSymbolContextCompUnit)) {
324 if (m_sc.comp_unit)
325 resolved |= eSymbolContextCompUnit;
326 else
327 actual_resolve_scope |= eSymbolContextCompUnit;
328 }
329 }
330
331 if (resolve_scope & eSymbolContextFunction) {
332 if (m_flags.IsClear(eSymbolContextFunction)) {
333 if (m_sc.function)
334 resolved |= eSymbolContextFunction;
335 else
336 actual_resolve_scope |= eSymbolContextFunction;
337 }
338 }
339
340 if (resolve_scope & eSymbolContextBlock) {
341 if (m_flags.IsClear(eSymbolContextBlock)) {
342 if (m_sc.block)
343 resolved |= eSymbolContextBlock;
344 else
345 actual_resolve_scope |= eSymbolContextBlock;
346 }
347 }
348
349 if (resolve_scope & eSymbolContextSymbol) {
350 if (m_flags.IsClear(eSymbolContextSymbol)) {
351 if (m_sc.symbol)
352 resolved |= eSymbolContextSymbol;
353 else
354 actual_resolve_scope |= eSymbolContextSymbol;
355 }
356 }
357
358 if (resolve_scope & eSymbolContextLineEntry) {
359 if (m_flags.IsClear(eSymbolContextLineEntry)) {
360 if (m_sc.line_entry.IsValid())
361 resolved |= eSymbolContextLineEntry;
362 else
363 actual_resolve_scope |= eSymbolContextLineEntry;
364 }
365 }
366
367 if (actual_resolve_scope) {
368 // We might be resolving less information than what is already
369 // in our current symbol context so resolve into a temporary
370 // symbol context "sc" so we don't clear out data we have
371 // already found in "m_sc"
372 SymbolContext sc;
373 // Set flags that indicate what we have tried to resolve
374 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
375 lookup_addr, actual_resolve_scope, sc);
376 // Only replace what we didn't already have as we may have
377 // information for an inlined function scope that won't match
378 // what a standard lookup by address would match
379 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
380 m_sc.comp_unit = sc.comp_unit;
381 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
382 m_sc.function = sc.function;
383 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
384 m_sc.block = sc.block;
385 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
386 m_sc.symbol = sc.symbol;
387 if ((resolved & eSymbolContextLineEntry) &&
388 !m_sc.line_entry.IsValid()) {
389 m_sc.line_entry = sc.line_entry;
390 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
391 }
392 }
393 } else {
394 // If we don't have a module, then we can't have the compile unit,
395 // function, block, line entry or symbol, so we can safely call
396 // ResolveSymbolContextForAddress with our symbol context member m_sc.
397 if (m_sc.target_sp) {
398 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
399 lookup_addr, resolve_scope, m_sc);
400 }
401 }
402
403 // Update our internal flags so we remember what we have tried to locate so
404 // we don't have to keep trying when more calls to this function are made.
405 // We might have dug up more information that was requested (for example
406 // if we were asked to only get the block, we will have gotten the
407 // compile unit, and function) so set any additional bits that we resolved
408 m_flags.Set(resolve_scope | resolved);
409 }
410
411 // Return the symbol context with everything that was possible to resolve
412 // resolved.
413 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414}
415
Kate Stoneb9c1b512016-09-06 20:57:50 +0000416VariableList *StackFrame::GetVariableList(bool get_file_globals) {
417 std::lock_guard<std::recursive_mutex> guard(m_mutex);
418 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
419 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
Kate Stoneb9c1b512016-09-06 20:57:50 +0000421 Block *frame_block = GetFrameBlock();
422
423 if (frame_block) {
424 const bool get_child_variables = true;
425 const bool can_create = true;
426 const bool stop_if_child_block_is_inlined_function = true;
427 m_variable_list_sp.reset(new VariableList());
428 frame_block->AppendBlockVariables(can_create, get_child_variables,
429 stop_if_child_block_is_inlined_function,
430 [this](Variable *v) { return true; },
431 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000432 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 }
434
435 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
436 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
437
438 if (m_flags.IsClear(eSymbolContextCompUnit))
439 GetSymbolContext(eSymbolContextCompUnit);
440
441 if (m_sc.comp_unit) {
442 VariableListSP global_variable_list_sp(
443 m_sc.comp_unit->GetVariableList(true));
444 if (m_variable_list_sp)
445 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
446 else
447 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000449 }
450
451 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452}
453
Greg Claytond41f0322011-08-02 23:35:43 +0000454VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000455StackFrame::GetInScopeVariableList(bool get_file_globals,
456 bool must_have_valid_location) {
457 std::lock_guard<std::recursive_mutex> guard(m_mutex);
458 // We can't fetch variable information for a history stack frame.
459 if (m_is_history_frame)
460 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000461
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 VariableListSP var_list_sp(new VariableList);
463 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000464
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 if (m_sc.block) {
466 const bool can_create = true;
467 const bool get_parent_variables = true;
468 const bool stop_if_block_is_inlined_function = true;
469 m_sc.block->AppendVariables(
470 can_create, get_parent_variables, stop_if_block_is_inlined_function,
471 [this, must_have_valid_location](Variable *v) {
472 return v->IsInScope(this) && (!must_have_valid_location ||
473 v->LocationIsValidForFrame(this));
474 },
475 var_list_sp.get());
476 }
477
478 if (m_sc.comp_unit && get_file_globals) {
479 VariableListSP global_variable_list_sp(
480 m_sc.comp_unit->GetVariableList(true));
481 if (global_variable_list_sp)
482 var_list_sp->AddVariables(global_variable_list_sp.get());
483 }
484
485 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000486}
487
Kate Stoneb9c1b512016-09-06 20:57:50 +0000488ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner0eb31a12016-11-17 05:14:32 +0000489 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
490 VariableSP &var_sp, Error &error) {
491 llvm::StringRef original_var_expr = var_expr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000492 // We can't fetch variable information for a history stack frame.
493 if (m_is_history_frame)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000494 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495
Zachary Turner0eb31a12016-11-17 05:14:32 +0000496 if (var_expr.empty()) {
497 error.SetErrorStringWithFormat("invalid variable path '%s'",
498 var_expr.str().c_str());
Zachary Turner24bd3172016-11-17 01:37:52 +0000499 return ValueObjectSP();
500 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501
Zachary Turner24bd3172016-11-17 01:37:52 +0000502 const bool check_ptr_vs_member =
503 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
504 const bool no_fragile_ivar =
505 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
506 const bool no_synth_child =
507 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
508 // const bool no_synth_array = (options &
509 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
510 error.Clear();
511 bool deref = false;
512 bool address_of = false;
513 ValueObjectSP valobj_sp;
514 const bool get_file_globals = true;
515 // When looking up a variable for an expression, we need only consider the
516 // variables that are in scope.
517 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
518 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519
Zachary Turner24bd3172016-11-17 01:37:52 +0000520 if (!variable_list)
521 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522
Zachary Turner24bd3172016-11-17 01:37:52 +0000523 // If first character is a '*', then show pointer contents
Zachary Turner24bd3172016-11-17 01:37:52 +0000524 std::string var_expr_storage;
525 if (var_expr[0] == '*') {
526 deref = true;
527 var_expr = var_expr.drop_front(); // Skip the '*'
528 } else if (var_expr[0] == '&') {
529 address_of = true;
530 var_expr = var_expr.drop_front(); // Skip the '&'
531 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532
Zachary Turner24bd3172016-11-17 01:37:52 +0000533 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
534 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535
Zachary Turner24bd3172016-11-17 01:37:52 +0000536 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000537
Zachary Turner24bd3172016-11-17 01:37:52 +0000538 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000539
Zachary Turner24bd3172016-11-17 01:37:52 +0000540 bool synthetically_added_instance_object = false;
541
542 if (var_sp) {
543 var_expr = var_expr.drop_front(name_const_string.GetLength());
544 }
545
546 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
547 // Check for direct ivars access which helps us with implicit
548 // access to ivars with the "this->" or "self->"
549 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
550 lldb::LanguageType method_language = eLanguageTypeUnknown;
551 bool is_instance_method = false;
552 ConstString method_object_name;
553 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
554 method_object_name)) {
555 if (is_instance_method && method_object_name) {
556 var_sp = variable_list->FindVariable(method_object_name);
557 if (var_sp) {
558 separator_idx = 0;
559 var_expr_storage = "->";
560 var_expr_storage += var_expr;
561 var_expr = var_expr_storage;
562 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000565 }
566 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000567
Zachary Turner24bd3172016-11-17 01:37:52 +0000568 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
569 // Check if any anonymous unions are there which contain a variable with
570 // the name we need
571 for (size_t i = 0; i < variable_list->GetSize(); i++) {
572 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
573 if (!variable_sp)
574 continue;
575 if (!variable_sp->GetName().IsEmpty())
576 continue;
577
578 Type *var_type = variable_sp->GetType();
579 if (!var_type)
580 continue;
581
582 if (!var_type->GetForwardCompilerType().IsAnonymousType())
583 continue;
584 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
585 if (!valobj_sp)
586 return valobj_sp;
587 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
588 if (valobj_sp)
589 break;
590 }
591 }
592
593 if (var_sp && !valobj_sp) {
594 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
595 if (!valobj_sp)
596 return valobj_sp;
597 }
598 if (!valobj_sp) {
599 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
600 name_const_string.GetCString());
601 return ValueObjectSP();
602 }
603
604 // We are dumping at least one child
605 while (separator_idx != std::string::npos) {
606 // Calculate the next separator index ahead of time
607 ValueObjectSP child_valobj_sp;
608 const char separator_type = var_expr[0];
609 switch (separator_type) {
610 case '-':
611 if (var_expr.size() >= 2 && var_expr[1] != '>')
612 return ValueObjectSP();
613
614 if (no_fragile_ivar) {
615 // Make sure we aren't trying to deref an objective
616 // C ivar if this is not allowed
617 const uint32_t pointer_type_flags =
618 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
619 if ((pointer_type_flags & eTypeIsObjC) &&
620 (pointer_type_flags & eTypeIsPointer)) {
621 // This was an objective C object pointer and
622 // it was requested we skip any fragile ivars
623 // so return nothing here
624 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625 }
626 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000627 var_expr = var_expr.drop_front(); // Remove the '-'
628 LLVM_FALLTHROUGH;
629 case '.': {
630 const bool expr_is_ptr = var_expr[0] == '>';
Kate Stoneb9c1b512016-09-06 20:57:50 +0000631
Zachary Turner24bd3172016-11-17 01:37:52 +0000632 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
633 separator_idx = var_expr.find_first_of(".-[");
634 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
635
636 if (check_ptr_vs_member) {
637 // We either have a pointer type and need to verify
638 // valobj_sp is a pointer, or we have a member of a
639 // class/union/struct being accessed with the . syntax
640 // and need to verify we don't have a pointer.
641 const bool actual_is_ptr = valobj_sp->IsPointerType();
642
643 if (actual_is_ptr != expr_is_ptr) {
644 // Incorrect use of "." with a pointer, or "->" with
645 // a class/union/struct instance or reference.
646 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
647 if (actual_is_ptr)
648 error.SetErrorStringWithFormat(
649 "\"%s\" is a pointer and . was used to attempt to access "
650 "\"%s\". Did you mean \"%s->%s\"?",
651 var_expr_path_strm.GetData(), child_name.GetCString(),
652 var_expr_path_strm.GetData(), var_expr.str().c_str());
653 else
654 error.SetErrorStringWithFormat(
655 "\"%s\" is not a pointer and -> was used to attempt to "
656 "access \"%s\". Did you mean \"%s.%s\"?",
657 var_expr_path_strm.GetData(), child_name.GetCString(),
658 var_expr_path_strm.GetData(), var_expr.str().c_str());
659 return ValueObjectSP();
660 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000662 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
663 if (!child_valobj_sp) {
664 if (!no_synth_child) {
665 child_valobj_sp = valobj_sp->GetSyntheticValue();
666 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000668 child_valobj_sp->GetChildMemberWithName(child_name, true);
669 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000670
Zachary Turner24bd3172016-11-17 01:37:52 +0000671 if (no_synth_child || !child_valobj_sp) {
672 // No child member with name "child_name"
673 if (synthetically_added_instance_object) {
674 // We added a "this->" or "self->" to the beginning of the
675 // expression
676 // and this is the first pointer ivar access, so just return
677 // the normal
678 // error
679 error.SetErrorStringWithFormat(
680 "no variable or instance variable named '%s' found in "
681 "this frame",
682 name_const_string.GetCString());
683 } else {
684 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
685 if (child_name) {
686 error.SetErrorStringWithFormat(
687 "\"%s\" is not a member of \"(%s) %s\"",
688 child_name.GetCString(),
689 valobj_sp->GetTypeName().AsCString("<invalid type>"),
690 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 } else {
692 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000693 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000694 var_expr_path_strm.GetData(),
695 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000697 }
698 return ValueObjectSP();
699 }
700 }
701 synthetically_added_instance_object = false;
702 // Remove the child name from the path
703 var_expr = var_expr.drop_front(child_name.GetLength());
704 if (use_dynamic != eNoDynamicValues) {
705 ValueObjectSP dynamic_value_sp(
706 child_valobj_sp->GetDynamicValue(use_dynamic));
707 if (dynamic_value_sp)
708 child_valobj_sp = dynamic_value_sp;
709 }
710 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711
Zachary Turner24bd3172016-11-17 01:37:52 +0000712 case '[': {
713 // Array member access, or treating pointer as an array
714 // Need at least two brackets and a number
715 if (var_expr.size() <= 2) {
716 error.SetErrorStringWithFormat(
717 "invalid square bracket encountered after \"%s\" in \"%s\"",
718 var_expr_path_strm.GetData(), var_expr.str().c_str());
719 return ValueObjectSP();
720 }
721
722 // Drop the open brace.
723 var_expr = var_expr.drop_front();
724 long child_index = 0;
725
726 // If there's no closing brace, this is an invalid expression.
727 size_t end_pos = var_expr.find_first_of(']');
728 if (end_pos == llvm::StringRef::npos) {
729 error.SetErrorStringWithFormat(
730 "missing closing square bracket in expression \"%s\"",
731 var_expr_path_strm.GetData());
732 return ValueObjectSP();
733 }
734 llvm::StringRef index_expr = var_expr.take_front(end_pos);
735 llvm::StringRef original_index_expr = index_expr;
736 // Drop all of "[index_expr]"
737 var_expr = var_expr.drop_front(end_pos + 1);
738
739 if (index_expr.consumeInteger(0, child_index)) {
740 // If there was no integer anywhere in the index expression, this is
741 // erroneous expression.
742 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
743 index_expr.str().c_str());
744 return ValueObjectSP();
745 }
746
747 if (index_expr.empty()) {
748 // The entire index expression was a single integer.
749
750 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
751 // what we have is *ptr[low]. the most similar C++ syntax is to deref
752 // ptr and extract bit low out of it. reading array item low would be
753 // done by saying ptr[low], without a deref * sign
754 Error error;
755 ValueObjectSP temp(valobj_sp->Dereference(error));
756 if (error.Fail()) {
757 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
758 error.SetErrorStringWithFormat(
759 "could not dereference \"(%s) %s\"",
760 valobj_sp->GetTypeName().AsCString("<invalid type>"),
761 var_expr_path_strm.GetData());
762 return ValueObjectSP();
763 }
764 valobj_sp = temp;
765 deref = false;
766 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
767 deref) {
768 // what we have is *arr[low]. the most similar C++ syntax is
769 // to get arr[0]
770 // (an operation that is equivalent to deref-ing arr)
771 // and extract bit low out of it. reading array item low
772 // would be done by saying arr[low], without a deref * sign
773 Error error;
774 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
775 if (error.Fail()) {
776 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
777 error.SetErrorStringWithFormat(
778 "could not get item 0 for \"(%s) %s\"",
779 valobj_sp->GetTypeName().AsCString("<invalid type>"),
780 var_expr_path_strm.GetData());
781 return ValueObjectSP();
782 }
783 valobj_sp = temp;
784 deref = false;
785 }
786
787 bool is_incomplete_array = false;
788 if (valobj_sp->IsPointerType()) {
789 bool is_objc_pointer = true;
790
791 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
792 eLanguageTypeObjC)
793 is_objc_pointer = false;
794 else if (!valobj_sp->GetCompilerType().IsPointerType())
795 is_objc_pointer = false;
796
797 if (no_synth_child && is_objc_pointer) {
798 error.SetErrorStringWithFormat(
799 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
800 "subscripted",
801 valobj_sp->GetTypeName().AsCString("<invalid type>"),
802 var_expr_path_strm.GetData());
803
804 return ValueObjectSP();
805 } else if (is_objc_pointer) {
806 // dereferencing ObjC variables is not valid.. so let's try
807 // and recur to synthetic children
808 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
809 if (!synthetic /* no synthetic */
810 || synthetic == valobj_sp) /* synthetic is the same as
811 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000812 {
813 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
814 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000815 "\"(%s) %s\" is not an array type",
816 valobj_sp->GetTypeName().AsCString("<invalid type>"),
817 var_expr_path_strm.GetData());
818 } else if (
819 static_cast<uint32_t>(child_index) >=
820 synthetic
821 ->GetNumChildren() /* synthetic does not have that many values */) {
822 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
823 error.SetErrorStringWithFormat(
824 "array index %ld is not valid for \"(%s) %s\"", child_index,
825 valobj_sp->GetTypeName().AsCString("<invalid type>"),
826 var_expr_path_strm.GetData());
827 } else {
828 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
829 if (!child_valobj_sp) {
830 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
831 error.SetErrorStringWithFormat(
832 "array index %ld is not valid for \"(%s) %s\"", child_index,
833 valobj_sp->GetTypeName().AsCString("<invalid type>"),
834 var_expr_path_strm.GetData());
835 }
836 }
837 } else {
838 child_valobj_sp =
839 valobj_sp->GetSyntheticArrayMember(child_index, true);
840 if (!child_valobj_sp) {
841 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
842 error.SetErrorStringWithFormat(
843 "failed to use pointer as array for index %ld for "
844 "\"(%s) %s\"",
845 child_index,
846 valobj_sp->GetTypeName().AsCString("<invalid type>"),
847 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 }
849 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000850 } else if (valobj_sp->GetCompilerType().IsArrayType(
851 nullptr, nullptr, &is_incomplete_array)) {
852 // Pass false to dynamic_value here so we can tell the
853 // difference between
854 // no dynamic value and no member of this type...
855 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
856 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
857 child_valobj_sp =
858 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000859
Zachary Turner24bd3172016-11-17 01:37:52 +0000860 if (!child_valobj_sp) {
861 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
862 error.SetErrorStringWithFormat(
863 "array index %ld is not valid for \"(%s) %s\"", child_index,
864 valobj_sp->GetTypeName().AsCString("<invalid type>"),
865 var_expr_path_strm.GetData());
866 }
867 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
868 // this is a bitfield asking to display just one bit
869 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
870 child_index, child_index, true);
871 if (!child_valobj_sp) {
872 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
873 error.SetErrorStringWithFormat(
874 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
875 child_index, child_index,
876 valobj_sp->GetTypeName().AsCString("<invalid type>"),
877 var_expr_path_strm.GetData());
878 }
879 } else {
880 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
881 if (no_synth_child /* synthetic is forbidden */ ||
882 !synthetic /* no synthetic */
883 || synthetic == valobj_sp) /* synthetic is the same as the
884 original object */
885 {
886 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
887 error.SetErrorStringWithFormat(
888 "\"(%s) %s\" is not an array type",
889 valobj_sp->GetTypeName().AsCString("<invalid type>"),
890 var_expr_path_strm.GetData());
891 } else if (
892 static_cast<uint32_t>(child_index) >=
893 synthetic
894 ->GetNumChildren() /* synthetic does not have that many values */) {
895 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
896 error.SetErrorStringWithFormat(
897 "array index %ld is not valid for \"(%s) %s\"", child_index,
898 valobj_sp->GetTypeName().AsCString("<invalid type>"),
899 var_expr_path_strm.GetData());
900 } else {
901 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
902 if (!child_valobj_sp) {
903 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
904 error.SetErrorStringWithFormat(
905 "array index %ld is not valid for \"(%s) %s\"", child_index,
906 valobj_sp->GetTypeName().AsCString("<invalid type>"),
907 var_expr_path_strm.GetData());
908 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000909 }
910 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000911
912 if (!child_valobj_sp) {
913 // Invalid array index...
914 return ValueObjectSP();
915 }
916
917 separator_idx = var_expr.find_first_of(".-[");
918 if (use_dynamic != eNoDynamicValues) {
919 ValueObjectSP dynamic_value_sp(
920 child_valobj_sp->GetDynamicValue(use_dynamic));
921 if (dynamic_value_sp)
922 child_valobj_sp = dynamic_value_sp;
923 }
924 // Break out early from the switch since we were able to find the child
925 // member
926 break;
927 }
928
929 // this is most probably a BitField, let's take a look
930 if (index_expr.front() != '-') {
931 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
932 original_index_expr.str().c_str());
933 return ValueObjectSP();
934 }
935
Zachary Turner7edc3a62016-11-21 23:18:13 +0000936 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000937 long final_index = 0;
938 if (index_expr.getAsInteger(0, final_index)) {
939 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
940 original_index_expr.str().c_str());
941 return ValueObjectSP();
942 }
943
944 // if the format given is [high-low], swap range
945 if (child_index > final_index) {
946 long temp = child_index;
947 child_index = final_index;
948 final_index = temp;
949 }
950
951 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
952 // what we have is *ptr[low-high]. the most similar C++ syntax is to
953 // deref ptr and extract bits low thru high out of it. reading array
954 // items low thru high would be done by saying ptr[low-high], without
955 // a deref * sign
956 Error error;
957 ValueObjectSP temp(valobj_sp->Dereference(error));
958 if (error.Fail()) {
959 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
960 error.SetErrorStringWithFormat(
961 "could not dereference \"(%s) %s\"",
962 valobj_sp->GetTypeName().AsCString("<invalid type>"),
963 var_expr_path_strm.GetData());
964 return ValueObjectSP();
965 }
966 valobj_sp = temp;
967 deref = false;
968 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
969 // what we have is *arr[low-high]. the most similar C++ syntax is to get
970 // arr[0] (an operation that is equivalent to deref-ing arr) and extract
971 // bits low thru high out of it. reading array items low thru high would
972 // be done by saying arr[low-high], without a deref * sign
973 Error error;
974 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
975 if (error.Fail()) {
976 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
977 error.SetErrorStringWithFormat(
978 "could not get item 0 for \"(%s) %s\"",
979 valobj_sp->GetTypeName().AsCString("<invalid type>"),
980 var_expr_path_strm.GetData());
981 return ValueObjectSP();
982 }
983 valobj_sp = temp;
984 deref = false;
985 }
986
987 child_valobj_sp =
988 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
989 if (!child_valobj_sp) {
990 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000992 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
993 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
994 var_expr_path_strm.GetData());
995 }
996
997 if (!child_valobj_sp) {
998 // Invalid bitfield range...
999 return ValueObjectSP();
1000 }
1001
1002 separator_idx = var_expr.find_first_of(".-[");
1003 if (use_dynamic != eNoDynamicValues) {
1004 ValueObjectSP dynamic_value_sp(
1005 child_valobj_sp->GetDynamicValue(use_dynamic));
1006 if (dynamic_value_sp)
1007 child_valobj_sp = dynamic_value_sp;
1008 }
1009 // Break out early from the switch since we were able to find the child
1010 // member
1011 break;
1012 }
1013 default:
1014 // Failure...
1015 {
1016 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1017 error.SetErrorStringWithFormat(
1018 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1019 separator_type, var_expr_path_strm.GetData(),
1020 var_expr.str().c_str());
1021
1022 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001024 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001025
1026 if (child_valobj_sp)
1027 valobj_sp = child_valobj_sp;
1028
1029 if (var_expr.empty())
1030 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001031 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001032 if (valobj_sp) {
1033 if (deref) {
1034 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1035 valobj_sp = deref_valobj_sp;
1036 } else if (address_of) {
1037 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1038 valobj_sp = address_of_valobj_sp;
1039 }
1040 }
1041 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042}
1043
1044bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Error *error_ptr) {
1045 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1046 if (!m_cfa_is_valid) {
1047 m_frame_base_error.SetErrorString(
1048 "No frame base available for this historical stack frame.");
1049 return false;
1050 }
1051
1052 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1053 if (m_sc.function) {
1054 m_frame_base.Clear();
1055 m_frame_base_error.Clear();
1056
1057 m_flags.Set(GOT_FRAME_BASE);
1058 ExecutionContext exe_ctx(shared_from_this());
1059 Value expr_value;
1060 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1061 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1062 loclist_base_addr =
1063 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1064 exe_ctx.GetTargetPtr());
1065
1066 if (m_sc.function->GetFrameBaseExpression().Evaluate(
1067 &exe_ctx, nullptr, nullptr, nullptr, loclist_base_addr, nullptr,
1068 nullptr, expr_value, &m_frame_base_error) == false) {
1069 // We should really have an error if evaluate returns, but in case
1070 // we don't, lets set the error to something at least.
1071 if (m_frame_base_error.Success())
1072 m_frame_base_error.SetErrorString(
1073 "Evaluation of the frame base expression failed.");
1074 } else {
1075 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1076 }
1077 } else {
1078 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001079 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 }
1081
1082 if (m_frame_base_error.Success())
1083 frame_base = m_frame_base;
1084
1085 if (error_ptr)
1086 *error_ptr = m_frame_base_error;
1087 return m_frame_base_error.Success();
1088}
1089
1090DWARFExpression *StackFrame::GetFrameBaseExpression(Error *error_ptr) {
1091 if (!m_sc.function) {
1092 if (error_ptr) {
1093 error_ptr->SetErrorString("No function in symbol context.");
1094 }
1095 return nullptr;
1096 }
1097
1098 return &m_sc.function->GetFrameBaseExpression();
1099}
1100
1101RegisterContextSP StackFrame::GetRegisterContext() {
1102 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1103 if (!m_reg_context_sp) {
1104 ThreadSP thread_sp(GetThread());
1105 if (thread_sp)
1106 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1107 }
1108 return m_reg_context_sp;
1109}
1110
1111bool StackFrame::HasDebugInformation() {
1112 GetSymbolContext(eSymbolContextLineEntry);
1113 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001114}
1115
1116ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001117StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1118 DynamicValueType use_dynamic) {
1119 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1120 ValueObjectSP valobj_sp;
1121 if (m_is_history_frame) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001122 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001123 }
1124 VariableList *var_list = GetVariableList(true);
1125 if (var_list) {
1126 // Make sure the variable is a frame variable
1127 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1128 const uint32_t num_variables = var_list->GetSize();
1129 if (var_idx < num_variables) {
1130 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1131 if (!valobj_sp) {
1132 if (m_variable_list_value_objects.GetSize() < num_variables)
1133 m_variable_list_value_objects.Resize(num_variables);
1134 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1135 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1136 }
Enrico Granata592afe72016-03-15 21:50:51 +00001137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 }
1139 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1140 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1141 if (dynamic_sp)
1142 return dynamic_sp;
1143 }
1144 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001145}
1146
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1148 DynamicValueType use_dynamic) {
1149 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1150 if (m_is_history_frame)
1151 return ValueObjectSP();
1152
1153 // Check to make sure we aren't already tracking this variable?
1154 ValueObjectSP valobj_sp(
1155 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1156 if (!valobj_sp) {
1157 // We aren't already tracking this global
1158 VariableList *var_list = GetVariableList(true);
1159 // If this frame has no variables, create a new list
1160 if (var_list == nullptr)
1161 m_variable_list_sp.reset(new VariableList());
1162
1163 // Add the global/static variable to this frame
1164 m_variable_list_sp->AddVariable(variable_sp);
1165
1166 // Now make a value object for it so we can track its changes
1167 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1168 }
1169 return valobj_sp;
1170}
1171
1172bool StackFrame::IsInlined() {
1173 if (m_sc.block == nullptr)
1174 GetSymbolContext(eSymbolContextBlock);
1175 if (m_sc.block)
1176 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1177 return false;
1178}
1179
1180lldb::LanguageType StackFrame::GetLanguage() {
1181 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1182 if (cu)
1183 return cu->GetLanguage();
1184 return lldb::eLanguageTypeUnknown;
1185}
1186
1187lldb::LanguageType StackFrame::GuessLanguage() {
1188 LanguageType lang_type = GetLanguage();
1189
1190 if (lang_type == eLanguageTypeUnknown) {
1191 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1192 if (f) {
1193 lang_type = f->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001194 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 }
1196
1197 return lang_type;
1198}
1199
1200namespace {
1201std::pair<const Instruction::Operand *, int64_t>
1202GetBaseExplainingValue(const Instruction::Operand &operand,
1203 RegisterContext &register_context, lldb::addr_t value) {
1204 switch (operand.m_type) {
1205 case Instruction::Operand::Type::Dereference:
1206 case Instruction::Operand::Type::Immediate:
1207 case Instruction::Operand::Type::Invalid:
1208 case Instruction::Operand::Type::Product:
1209 // These are not currently interesting
1210 return std::make_pair(nullptr, 0);
1211 case Instruction::Operand::Type::Sum: {
1212 const Instruction::Operand *immediate_child = nullptr;
1213 const Instruction::Operand *variable_child = nullptr;
1214 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1215 immediate_child = &operand.m_children[0];
1216 variable_child = &operand.m_children[1];
1217 } else if (operand.m_children[1].m_type ==
1218 Instruction::Operand::Type::Immediate) {
1219 immediate_child = &operand.m_children[1];
1220 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001221 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222 if (!immediate_child) {
1223 return std::make_pair(nullptr, 0);
1224 }
1225 lldb::addr_t adjusted_value = value;
1226 if (immediate_child->m_negative) {
1227 adjusted_value += immediate_child->m_immediate;
1228 } else {
1229 adjusted_value -= immediate_child->m_immediate;
1230 }
1231 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1232 GetBaseExplainingValue(*variable_child, register_context,
1233 adjusted_value);
1234 if (!base_and_offset.first) {
1235 return std::make_pair(nullptr, 0);
1236 }
1237 if (immediate_child->m_negative) {
1238 base_and_offset.second -= immediate_child->m_immediate;
1239 } else {
1240 base_and_offset.second += immediate_child->m_immediate;
1241 }
1242 return base_and_offset;
1243 }
1244 case Instruction::Operand::Type::Register: {
1245 const RegisterInfo *info =
1246 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1247 if (!info) {
1248 return std::make_pair(nullptr, 0);
1249 }
1250 RegisterValue reg_value;
1251 if (!register_context.ReadRegister(info, reg_value)) {
1252 return std::make_pair(nullptr, 0);
1253 }
1254 if (reg_value.GetAsUInt64() == value) {
1255 return std::make_pair(&operand, 0);
1256 } else {
1257 return std::make_pair(nullptr, 0);
1258 }
1259 }
1260 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001261 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262}
1263
1264std::pair<const Instruction::Operand *, int64_t>
1265GetBaseExplainingDereference(const Instruction::Operand &operand,
1266 RegisterContext &register_context,
1267 lldb::addr_t addr) {
1268 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1269 return GetBaseExplainingValue(operand.m_children[0], register_context,
1270 addr);
1271 }
1272 return std::make_pair(nullptr, 0);
1273}
Ilia K4f730dc2016-09-12 05:25:33 +00001274}
Sean Callanan4740a732016-09-06 04:48:36 +00001275
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1277 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001278
Kate Stoneb9c1b512016-09-06 20:57:50 +00001279 const ArchSpec &target_arch = target_sp->GetArchitecture();
1280
1281 AddressRange pc_range;
1282 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1283 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1284
1285 ExecutionContext exe_ctx(shared_from_this());
1286
1287 const char *plugin_name = nullptr;
1288 const char *flavor = nullptr;
1289 const bool prefer_file_cache = false;
1290
1291 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1292 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1293
1294 if (!disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001295 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 }
Sean Callanan4740a732016-09-06 04:48:36 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 InstructionSP instruction_sp =
1299 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1300
1301 llvm::SmallVector<Instruction::Operand, 3> operands;
1302
1303 if (!instruction_sp->ParseOperands(operands)) {
1304 return ValueObjectSP();
1305 }
1306
1307 RegisterContextSP register_context_sp = GetRegisterContext();
1308
1309 if (!register_context_sp) {
1310 return ValueObjectSP();
1311 }
1312
1313 for (const Instruction::Operand &operand : operands) {
1314 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1315 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1316
1317 if (!base_and_offset.first) {
1318 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001319 }
Sean Callanan4740a732016-09-06 04:48:36 +00001320
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321 switch (base_and_offset.first->m_type) {
1322 case Instruction::Operand::Type::Immediate: {
1323 lldb_private::Address addr;
1324 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1325 base_and_offset.second,
1326 addr)) {
1327 TypeSystem *c_type_system =
1328 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1329 if (!c_type_system) {
1330 return ValueObjectSP();
1331 } else {
1332 CompilerType void_ptr_type =
1333 c_type_system
1334 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1335 .GetPointerType();
1336 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001337 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001339 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 }
1341 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001342 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001343 case Instruction::Operand::Type::Register: {
1344 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1345 base_and_offset.second);
1346 }
1347 default:
1348 return ValueObjectSP();
1349 }
1350 }
1351
1352 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001353}
1354
Kate Stoneb9c1b512016-09-06 20:57:50 +00001355namespace {
1356ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1357 int64_t offset) {
1358 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1359 return ValueObjectSP();
1360 }
Sean Callanan4740a732016-09-06 04:48:36 +00001361
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 if (parent->IsPointerOrReferenceType()) {
1363 return parent;
1364 }
1365
1366 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1367 const bool can_create = true;
1368 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1369
1370 if (!child_sp) {
1371 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001372 }
1373
Kate Stoneb9c1b512016-09-06 20:57:50 +00001374 int64_t child_offset = child_sp->GetByteOffset();
1375 int64_t child_size = child_sp->GetByteSize();
1376
1377 if (offset >= child_offset && offset < (child_offset + child_size)) {
1378 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001379 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001380 }
Sean Callanan4740a732016-09-06 04:48:36 +00001381
Kate Stoneb9c1b512016-09-06 20:57:50 +00001382 if (offset == 0) {
1383 return parent;
1384 } else {
1385 return ValueObjectSP();
1386 }
1387}
1388
1389ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1390 ValueObjectSP &base,
1391 int64_t offset) {
1392 // base is a pointer to something
1393 // offset is the thing to add to the pointer
1394 // We return the most sensible ValueObject for the result of *(base+offset)
1395
1396 if (!base->IsPointerOrReferenceType()) {
1397 return ValueObjectSP();
1398 }
1399
1400 Error error;
1401 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001402
1403 if (!pointee) {
1404 return ValueObjectSP();
1405 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406
Ilia K4f730dc2016-09-12 05:25:33 +00001407 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 int64_t index = offset / pointee->GetByteSize();
1409 offset = offset % pointee->GetByteSize();
1410 const bool can_create = true;
1411 pointee = base->GetSyntheticArrayMember(index, can_create);
1412 }
1413
1414 if (!pointee || error.Fail()) {
1415 return ValueObjectSP();
1416 }
1417
1418 return GetValueForOffset(frame, pointee, offset);
1419}
1420
1421//------------------------------------------------------------------
1422/// Attempt to reconstruct the ValueObject for the address contained in a
1423/// given register plus an offset.
1424///
1425/// @params [in] frame
1426/// The current stack frame.
1427///
1428/// @params [in] reg
1429/// The register.
1430///
1431/// @params [in] offset
1432/// The offset from the register.
1433///
1434/// @param [in] disassembler
1435/// A disassembler containing instructions valid up to the current PC.
1436///
1437/// @param [in] variables
1438/// The variable list from the current frame,
1439///
1440/// @param [in] pc
1441/// The program counter for the instruction considered the 'user'.
1442///
1443/// @return
1444/// A string describing the base for the ExpressionPath. This could be a
1445/// variable, a register value, an argument, or a function return value.
1446/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1447//------------------------------------------------------------------
1448lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1449 int64_t offset, Disassembler &disassembler,
1450 VariableList &variables, const Address &pc) {
1451 // Example of operation for Intel:
1452 //
1453 // +14: movq -0x8(%rbp), %rdi
1454 // +18: movq 0x8(%rdi), %rdi
1455 // +22: addl 0x4(%rdi), %eax
1456 //
1457 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1458 //
1459 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18
1460 // that assigns to rdi, and calls itself recursively for that dereference
1461 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1462 // +14 that assigns to rdi, and calls itself recursively for that
1463 // derefernece
1464 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1465 // variable list.
1466 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1467 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1468 // at +18)
1469 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1470 // rdi+4 at +22)
1471
1472 // First, check the variable list to see if anything is at the specified
1473 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001474
Sean Callanan50857102016-09-14 00:48:19 +00001475 using namespace OperandMatchers;
1476
Sean Callanan0ac172d2016-09-14 20:29:57 +00001477 const RegisterInfo *reg_info =
1478 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1479 if (!reg_info) {
1480 return ValueObjectSP();
1481 }
1482
Sean Callanan807ee2f2016-09-13 21:18:27 +00001483 Instruction::Operand op =
1484 offset ? Instruction::Operand::BuildDereference(
1485 Instruction::Operand::BuildSum(
1486 Instruction::Operand::BuildRegister(reg),
1487 Instruction::Operand::BuildImmediate(offset)))
1488 : Instruction::Operand::BuildDereference(
1489 Instruction::Operand::BuildRegister(reg));
1490
Kate Stoneb9c1b512016-09-06 20:57:50 +00001491 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1492 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001493 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1494 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001495 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001496 }
1497
Kate Stoneb9c1b512016-09-06 20:57:50 +00001498 const uint32_t current_inst =
1499 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1500 if (current_inst == UINT32_MAX) {
1501 return ValueObjectSP();
1502 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503
Kate Stoneb9c1b512016-09-06 20:57:50 +00001504 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1505 // This is not an exact algorithm, and it sacrifices accuracy for
Sean Callanan0ac172d2016-09-14 20:29:57 +00001506 // generality. Recognizing "mov" and "ld" instructions –– and which are
1507 // their source and destination operands -- is something the disassembler
1508 // should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001509 InstructionSP instruction_sp =
1510 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511
Sean Callanan50857102016-09-14 00:48:19 +00001512 if (instruction_sp->IsCall()) {
1513 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1514 if (!abi_sp) {
1515 continue;
1516 }
1517
1518 const char *return_register_name;
1519 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1520 continue;
1521 }
1522
1523 const RegisterInfo *return_register_info =
1524 frame.GetRegisterContext()->GetRegisterInfoByName(
1525 return_register_name);
1526 if (!return_register_info) {
1527 continue;
1528 }
1529
1530 int64_t offset = 0;
1531
1532 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1533 MatchRegOp(*return_register_info))(op) &&
1534 !MatchUnaryOp(
1535 MatchOpType(Instruction::Operand::Type::Dereference),
1536 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1537 MatchRegOp(*return_register_info),
1538 FetchImmOp(offset)))(op)) {
1539 continue;
1540 }
1541
Kate Stoneb9c1b512016-09-06 20:57:50 +00001542 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;
Sean Callanan50857102016-09-14 00:48:19 +00001568 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 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(
Zachary Turner22a26282016-11-12 18:17:36 +00001577 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001578 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
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001591 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1592 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1593 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001594
1595 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001597 }
1598 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001600 }
1601 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 continue;
1603 }
1604
1605 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001606 ValueObjectSP source_path;
1607 ConstString origin_register;
1608 int64_t origin_offset = 0;
1609
1610 if (FetchRegOp(origin_register)(*origin_operand)) {
1611 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1612 variables, instruction_sp->GetAddress());
1613 } else if (MatchUnaryOp(
1614 MatchOpType(Instruction::Operand::Type::Dereference),
1615 FetchRegOp(origin_register))(*origin_operand) ||
1616 MatchUnaryOp(
1617 MatchOpType(Instruction::Operand::Type::Dereference),
1618 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1619 FetchRegOp(origin_register),
1620 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001621 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001622 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001624 if (!source_path) {
1625 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001627 source_path =
1628 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 }
1630
1631 if (source_path) {
1632 return source_path;
1633 }
1634 }
1635
1636 return ValueObjectSP();
1637}
1638}
1639
1640lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1641 int64_t offset) {
1642 TargetSP target_sp = CalculateTarget();
1643
1644 const ArchSpec &target_arch = target_sp->GetArchitecture();
1645
1646 Block *frame_block = GetFrameBlock();
1647
1648 if (!frame_block) {
1649 return ValueObjectSP();
1650 }
1651
1652 Function *function = frame_block->CalculateSymbolContextFunction();
1653 if (!function) {
1654 return ValueObjectSP();
1655 }
1656
1657 AddressRange pc_range = function->GetAddressRange();
1658
1659 if (GetFrameCodeAddress().GetFileAddress() <
1660 pc_range.GetBaseAddress().GetFileAddress() ||
1661 GetFrameCodeAddress().GetFileAddress() -
1662 pc_range.GetBaseAddress().GetFileAddress() >=
1663 pc_range.GetByteSize()) {
1664 return ValueObjectSP();
1665 }
1666
1667 ExecutionContext exe_ctx(shared_from_this());
1668
1669 const char *plugin_name = nullptr;
1670 const char *flavor = nullptr;
1671 const bool prefer_file_cache = false;
1672 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1673 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1674
1675 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1676 return ValueObjectSP();
1677 }
1678
1679 const bool get_file_globals = false;
1680 VariableList *variables = GetVariableList(get_file_globals);
1681
1682 if (!variables) {
1683 return ValueObjectSP();
1684 }
1685
1686 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1687 GetFrameCodeAddress());
1688}
1689
1690TargetSP StackFrame::CalculateTarget() {
1691 TargetSP target_sp;
1692 ThreadSP thread_sp(GetThread());
1693 if (thread_sp) {
1694 ProcessSP process_sp(thread_sp->CalculateProcess());
1695 if (process_sp)
1696 target_sp = process_sp->CalculateTarget();
1697 }
1698 return target_sp;
1699}
1700
1701ProcessSP StackFrame::CalculateProcess() {
1702 ProcessSP process_sp;
1703 ThreadSP thread_sp(GetThread());
1704 if (thread_sp)
1705 process_sp = thread_sp->CalculateProcess();
1706 return process_sp;
1707}
1708
1709ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1710
1711StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1712
1713void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1714 exe_ctx.SetContext(shared_from_this());
1715}
1716
1717void StackFrame::DumpUsingSettingsFormat(Stream *strm,
1718 const char *frame_marker) {
1719 if (strm == nullptr)
1720 return;
1721
1722 GetSymbolContext(eSymbolContextEverything);
1723 ExecutionContext exe_ctx(shared_from_this());
1724 StreamString s;
1725
1726 if (frame_marker)
1727 s.PutCString(frame_marker);
1728
1729 const FormatEntity::Entry *frame_format = nullptr;
1730 Target *target = exe_ctx.GetTargetPtr();
1731 if (target)
1732 frame_format = target->GetDebugger().GetFrameFormat();
1733 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1734 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001735 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 } else {
1737 Dump(strm, true, false);
1738 strm->EOL();
1739 }
1740}
1741
1742void StackFrame::Dump(Stream *strm, bool show_frame_index,
1743 bool show_fullpaths) {
1744 if (strm == nullptr)
1745 return;
1746
1747 if (show_frame_index)
1748 strm->Printf("frame #%u: ", m_frame_index);
1749 ExecutionContext exe_ctx(shared_from_this());
1750 Target *target = exe_ctx.GetTargetPtr();
1751 strm->Printf("0x%0*" PRIx64 " ",
1752 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1753 : 16,
1754 GetFrameCodeAddress().GetLoadAddress(target));
1755 GetSymbolContext(eSymbolContextEverything);
1756 const bool show_module = true;
1757 const bool show_inline = true;
1758 const bool show_function_arguments = true;
1759 const bool show_function_name = true;
1760 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1761 GetFrameCodeAddress(), show_fullpaths, show_module,
1762 show_inline, show_function_arguments,
1763 show_function_name);
1764}
1765
1766void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1767 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1768 assert(GetStackID() ==
1769 prev_frame.GetStackID()); // TODO: remove this after some testing
1770 m_variable_list_sp = prev_frame.m_variable_list_sp;
1771 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001772 if (!m_disassembly.GetString().empty()) {
1773 m_disassembly.Clear();
1774 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001776}
1777
1778void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1779 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1780 assert(GetStackID() ==
1781 curr_frame.GetStackID()); // TODO: remove this after some testing
1782 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1783 assert(GetThread() == curr_frame.GetThread());
1784 m_frame_index = curr_frame.m_frame_index;
1785 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1786 m_reg_context_sp = curr_frame.m_reg_context_sp;
1787 m_frame_code_addr = curr_frame.m_frame_code_addr;
1788 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1789 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1790 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1791 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1792 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1793 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1794 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1795 m_sc.function == curr_frame.m_sc.function);
1796 m_sc = curr_frame.m_sc;
1797 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1798 m_flags.Set(m_sc.GetResolvedMask());
1799 m_frame_base.Clear();
1800 m_frame_base_error.Clear();
1801}
1802
1803bool StackFrame::HasCachedData() const {
1804 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001805 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001806 if (m_variable_list_value_objects.GetSize() > 0)
1807 return true;
1808 if (!m_disassembly.GetString().empty())
1809 return true;
1810 return false;
1811}
1812
1813bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1814 const char *frame_marker) {
1815
1816 if (show_frame_info) {
1817 strm.Indent();
1818 DumpUsingSettingsFormat(&strm, frame_marker);
1819 }
1820
1821 if (show_source) {
1822 ExecutionContext exe_ctx(shared_from_this());
1823 bool have_source = false, have_debuginfo = false;
1824 Debugger::StopDisassemblyType disasm_display =
1825 Debugger::eStopDisassemblyTypeNever;
1826 Target *target = exe_ctx.GetTargetPtr();
1827 if (target) {
1828 Debugger &debugger = target->GetDebugger();
1829 const uint32_t source_lines_before =
1830 debugger.GetStopSourceLineCount(true);
1831 const uint32_t source_lines_after =
1832 debugger.GetStopSourceLineCount(false);
1833 disasm_display = debugger.GetStopDisassemblyDisplay();
1834
1835 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1836 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1837 have_debuginfo = true;
1838 if (source_lines_before > 0 || source_lines_after > 0) {
1839 size_t num_lines =
1840 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1841 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001842 m_sc.line_entry.column, source_lines_before,
1843 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001844 if (num_lines != 0)
1845 have_source = true;
1846 // TODO: Give here a one time warning if source file is missing.
1847 }
1848 }
1849 switch (disasm_display) {
1850 case Debugger::eStopDisassemblyTypeNever:
1851 break;
1852
1853 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1854 if (have_debuginfo)
1855 break;
1856 LLVM_FALLTHROUGH;
1857
1858 case Debugger::eStopDisassemblyTypeNoSource:
1859 if (have_source)
1860 break;
1861 LLVM_FALLTHROUGH;
1862
1863 case Debugger::eStopDisassemblyTypeAlways:
1864 if (target) {
1865 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1866 if (disasm_lines > 0) {
1867 const ArchSpec &target_arch = target->GetArchitecture();
1868 AddressRange pc_range;
1869 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1870 pc_range.SetByteSize(disasm_lines *
1871 target_arch.GetMaximumOpcodeByteSize());
1872 const char *plugin_name = nullptr;
1873 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001874 const bool mixed_source_and_assembly = false;
1875 Disassembler::Disassemble(
1876 target->GetDebugger(), target_arch, plugin_name, flavor,
1877 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1878 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001879 }
1880 }
1881 break;
1882 }
1883 }
1884 }
1885 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001886}