blob: 3a25210ea93f65b5e762c1dc1628e332ae887cdf [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 Turner24bd3172016-11-17 01:37:52 +0000224 if (m_disassembly.Empty())
225 return nullptr;
226
227 ExecutionContext exe_ctx(shared_from_this());
228 Target *target = exe_ctx.GetTargetPtr();
229 if (target) {
230 const char *plugin_name = nullptr;
231 const char *flavor = nullptr;
232 Disassembler::Disassemble(target->GetDebugger(), target->GetArchitecture(),
233 plugin_name, flavor, exe_ctx, 0, false, 0, 0,
234 m_disassembly);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 }
236 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237}
238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239Block *StackFrame::GetFrameBlock() {
240 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
241 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 if (m_sc.block) {
244 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
245 if (inline_block) {
246 // Use the block with the inlined function info
247 // as the frame block we want this frame to have only the variables
248 // for the inlined function and its non-inlined block child blocks.
249 return inline_block;
250 } else {
251 // This block is not contained within any inlined function blocks
252 // with so we want to use the top most function block.
253 return &m_sc.function->GetBlock(false);
254 }
255 }
256 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000257}
258
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259//----------------------------------------------------------------------
260// Get the symbol context if we already haven't done so by resolving the
261// PC address as much as possible. This way when we pass around a
262// StackFrame object, everyone will have as much information as
263// possible and no one will ever have to look things up manually.
264//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265const SymbolContext &StackFrame::GetSymbolContext(uint32_t resolve_scope) {
266 std::lock_guard<std::recursive_mutex> guard(m_mutex);
267 // Copy our internal symbol context into "sc".
268 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
269 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000270
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271 // If the target was requested add that:
272 if (!m_sc.target_sp) {
273 m_sc.target_sp = CalculateTarget();
274 if (m_sc.target_sp)
275 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 }
277
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 // Resolve our PC to section offset if we haven't already done so
279 // and if we don't have a module. The resolved address section will
280 // contain the module to which it belongs
281 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
282 GetFrameCodeAddress();
283
284 // If this is not frame zero, then we need to subtract 1 from the PC
285 // value when doing address lookups since the PC will be on the
286 // instruction following the function call instruction...
287
288 Address lookup_addr(GetFrameCodeAddress());
289 if (m_frame_index > 0 && lookup_addr.IsValid()) {
290 addr_t offset = lookup_addr.GetOffset();
291 if (offset > 0) {
292 lookup_addr.SetOffset(offset - 1);
293
294 } else {
295 // lookup_addr is the start of a section. We need
296 // do the math on the actual load address and re-compute
297 // the section. We're working with a 'noreturn' function
298 // at the end of a section.
299 ThreadSP thread_sp(GetThread());
300 if (thread_sp) {
301 TargetSP target_sp(thread_sp->CalculateTarget());
302 if (target_sp) {
303 addr_t addr_minus_one =
304 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
305 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
306 } else {
307 lookup_addr.SetOffset(offset - 1);
308 }
309 }
310 }
311 }
312
313 if (m_sc.module_sp) {
314 // We have something in our stack frame symbol context, lets check
315 // if we haven't already tried to lookup one of those things. If we
316 // haven't then we will do the query.
317
318 uint32_t actual_resolve_scope = 0;
319
320 if (resolve_scope & eSymbolContextCompUnit) {
321 if (m_flags.IsClear(eSymbolContextCompUnit)) {
322 if (m_sc.comp_unit)
323 resolved |= eSymbolContextCompUnit;
324 else
325 actual_resolve_scope |= eSymbolContextCompUnit;
326 }
327 }
328
329 if (resolve_scope & eSymbolContextFunction) {
330 if (m_flags.IsClear(eSymbolContextFunction)) {
331 if (m_sc.function)
332 resolved |= eSymbolContextFunction;
333 else
334 actual_resolve_scope |= eSymbolContextFunction;
335 }
336 }
337
338 if (resolve_scope & eSymbolContextBlock) {
339 if (m_flags.IsClear(eSymbolContextBlock)) {
340 if (m_sc.block)
341 resolved |= eSymbolContextBlock;
342 else
343 actual_resolve_scope |= eSymbolContextBlock;
344 }
345 }
346
347 if (resolve_scope & eSymbolContextSymbol) {
348 if (m_flags.IsClear(eSymbolContextSymbol)) {
349 if (m_sc.symbol)
350 resolved |= eSymbolContextSymbol;
351 else
352 actual_resolve_scope |= eSymbolContextSymbol;
353 }
354 }
355
356 if (resolve_scope & eSymbolContextLineEntry) {
357 if (m_flags.IsClear(eSymbolContextLineEntry)) {
358 if (m_sc.line_entry.IsValid())
359 resolved |= eSymbolContextLineEntry;
360 else
361 actual_resolve_scope |= eSymbolContextLineEntry;
362 }
363 }
364
365 if (actual_resolve_scope) {
366 // We might be resolving less information than what is already
367 // in our current symbol context so resolve into a temporary
368 // symbol context "sc" so we don't clear out data we have
369 // already found in "m_sc"
370 SymbolContext sc;
371 // Set flags that indicate what we have tried to resolve
372 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
373 lookup_addr, actual_resolve_scope, sc);
374 // Only replace what we didn't already have as we may have
375 // information for an inlined function scope that won't match
376 // what a standard lookup by address would match
377 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
378 m_sc.comp_unit = sc.comp_unit;
379 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
380 m_sc.function = sc.function;
381 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
382 m_sc.block = sc.block;
383 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
384 m_sc.symbol = sc.symbol;
385 if ((resolved & eSymbolContextLineEntry) &&
386 !m_sc.line_entry.IsValid()) {
387 m_sc.line_entry = sc.line_entry;
388 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
389 }
390 }
391 } else {
392 // If we don't have a module, then we can't have the compile unit,
393 // function, block, line entry or symbol, so we can safely call
394 // ResolveSymbolContextForAddress with our symbol context member m_sc.
395 if (m_sc.target_sp) {
396 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
397 lookup_addr, resolve_scope, m_sc);
398 }
399 }
400
401 // Update our internal flags so we remember what we have tried to locate so
402 // we don't have to keep trying when more calls to this function are made.
403 // We might have dug up more information that was requested (for example
404 // if we were asked to only get the block, we will have gotten the
405 // compile unit, and function) so set any additional bits that we resolved
406 m_flags.Set(resolve_scope | resolved);
407 }
408
409 // Return the symbol context with everything that was possible to resolve
410 // resolved.
411 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412}
413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414VariableList *StackFrame::GetVariableList(bool get_file_globals) {
415 std::lock_guard<std::recursive_mutex> guard(m_mutex);
416 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
417 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Kate Stoneb9c1b512016-09-06 20:57:50 +0000419 Block *frame_block = GetFrameBlock();
420
421 if (frame_block) {
422 const bool get_child_variables = true;
423 const bool can_create = true;
424 const bool stop_if_child_block_is_inlined_function = true;
425 m_variable_list_sp.reset(new VariableList());
426 frame_block->AppendBlockVariables(can_create, get_child_variables,
427 stop_if_child_block_is_inlined_function,
428 [this](Variable *v) { return true; },
429 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000430 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000431 }
432
433 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
434 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
435
436 if (m_flags.IsClear(eSymbolContextCompUnit))
437 GetSymbolContext(eSymbolContextCompUnit);
438
439 if (m_sc.comp_unit) {
440 VariableListSP global_variable_list_sp(
441 m_sc.comp_unit->GetVariableList(true));
442 if (m_variable_list_sp)
443 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
444 else
445 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 }
448
449 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450}
451
Greg Claytond41f0322011-08-02 23:35:43 +0000452VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453StackFrame::GetInScopeVariableList(bool get_file_globals,
454 bool must_have_valid_location) {
455 std::lock_guard<std::recursive_mutex> guard(m_mutex);
456 // We can't fetch variable information for a history stack frame.
457 if (m_is_history_frame)
458 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000459
Kate Stoneb9c1b512016-09-06 20:57:50 +0000460 VariableListSP var_list_sp(new VariableList);
461 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 if (m_sc.block) {
464 const bool can_create = true;
465 const bool get_parent_variables = true;
466 const bool stop_if_block_is_inlined_function = true;
467 m_sc.block->AppendVariables(
468 can_create, get_parent_variables, stop_if_block_is_inlined_function,
469 [this, must_have_valid_location](Variable *v) {
470 return v->IsInScope(this) && (!must_have_valid_location ||
471 v->LocationIsValidForFrame(this));
472 },
473 var_list_sp.get());
474 }
475
476 if (m_sc.comp_unit && get_file_globals) {
477 VariableListSP global_variable_list_sp(
478 m_sc.comp_unit->GetVariableList(true));
479 if (global_variable_list_sp)
480 var_list_sp->AddVariables(global_variable_list_sp.get());
481 }
482
483 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000484}
485
Kate Stoneb9c1b512016-09-06 20:57:50 +0000486ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner24bd3172016-11-17 01:37:52 +0000487 llvm::StringRef var_expr_cstr, DynamicValueType use_dynamic,
488 uint32_t options, VariableSP &var_sp, Error &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 // We can't fetch variable information for a history stack frame.
490 if (m_is_history_frame)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000491 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492
Zachary Turner24bd3172016-11-17 01:37:52 +0000493 if (var_expr_cstr.empty()) {
494 error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
495 return ValueObjectSP();
496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000497
Zachary Turner24bd3172016-11-17 01:37:52 +0000498 const bool check_ptr_vs_member =
499 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
500 const bool no_fragile_ivar =
501 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
502 const bool no_synth_child =
503 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
504 // const bool no_synth_array = (options &
505 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
506 error.Clear();
507 bool deref = false;
508 bool address_of = false;
509 ValueObjectSP valobj_sp;
510 const bool get_file_globals = true;
511 // When looking up a variable for an expression, we need only consider the
512 // variables that are in scope.
513 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
514 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515
Zachary Turner24bd3172016-11-17 01:37:52 +0000516 if (!variable_list)
517 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518
Zachary Turner24bd3172016-11-17 01:37:52 +0000519 // If first character is a '*', then show pointer contents
520 llvm::StringRef var_expr = var_expr_cstr;
521 std::string var_expr_storage;
522 if (var_expr[0] == '*') {
523 deref = true;
524 var_expr = var_expr.drop_front(); // Skip the '*'
525 } else if (var_expr[0] == '&') {
526 address_of = true;
527 var_expr = var_expr.drop_front(); // Skip the '&'
528 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529
Zachary Turner24bd3172016-11-17 01:37:52 +0000530 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
531 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000532
Zachary Turner24bd3172016-11-17 01:37:52 +0000533 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534
Zachary Turner24bd3172016-11-17 01:37:52 +0000535 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536
Zachary Turner24bd3172016-11-17 01:37:52 +0000537 bool synthetically_added_instance_object = false;
538
539 if (var_sp) {
540 var_expr = var_expr.drop_front(name_const_string.GetLength());
541 }
542
543 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
544 // Check for direct ivars access which helps us with implicit
545 // access to ivars with the "this->" or "self->"
546 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
547 lldb::LanguageType method_language = eLanguageTypeUnknown;
548 bool is_instance_method = false;
549 ConstString method_object_name;
550 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
551 method_object_name)) {
552 if (is_instance_method && method_object_name) {
553 var_sp = variable_list->FindVariable(method_object_name);
554 if (var_sp) {
555 separator_idx = 0;
556 var_expr_storage = "->";
557 var_expr_storage += var_expr;
558 var_expr = var_expr_storage;
559 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000560 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000562 }
563 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564
Zachary Turner24bd3172016-11-17 01:37:52 +0000565 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
566 // Check if any anonymous unions are there which contain a variable with
567 // the name we need
568 for (size_t i = 0; i < variable_list->GetSize(); i++) {
569 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
570 if (!variable_sp)
571 continue;
572 if (!variable_sp->GetName().IsEmpty())
573 continue;
574
575 Type *var_type = variable_sp->GetType();
576 if (!var_type)
577 continue;
578
579 if (!var_type->GetForwardCompilerType().IsAnonymousType())
580 continue;
581 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
582 if (!valobj_sp)
583 return valobj_sp;
584 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
585 if (valobj_sp)
586 break;
587 }
588 }
589
590 if (var_sp && !valobj_sp) {
591 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
592 if (!valobj_sp)
593 return valobj_sp;
594 }
595 if (!valobj_sp) {
596 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
597 name_const_string.GetCString());
598 return ValueObjectSP();
599 }
600
601 // We are dumping at least one child
602 while (separator_idx != std::string::npos) {
603 // Calculate the next separator index ahead of time
604 ValueObjectSP child_valobj_sp;
605 const char separator_type = var_expr[0];
606 switch (separator_type) {
607 case '-':
608 if (var_expr.size() >= 2 && var_expr[1] != '>')
609 return ValueObjectSP();
610
611 if (no_fragile_ivar) {
612 // Make sure we aren't trying to deref an objective
613 // C ivar if this is not allowed
614 const uint32_t pointer_type_flags =
615 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
616 if ((pointer_type_flags & eTypeIsObjC) &&
617 (pointer_type_flags & eTypeIsPointer)) {
618 // This was an objective C object pointer and
619 // it was requested we skip any fragile ivars
620 // so return nothing here
621 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000622 }
623 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000624 var_expr = var_expr.drop_front(); // Remove the '-'
625 LLVM_FALLTHROUGH;
626 case '.': {
627 const bool expr_is_ptr = var_expr[0] == '>';
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628
Zachary Turner24bd3172016-11-17 01:37:52 +0000629 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
630 separator_idx = var_expr.find_first_of(".-[");
631 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
632
633 if (check_ptr_vs_member) {
634 // We either have a pointer type and need to verify
635 // valobj_sp is a pointer, or we have a member of a
636 // class/union/struct being accessed with the . syntax
637 // and need to verify we don't have a pointer.
638 const bool actual_is_ptr = valobj_sp->IsPointerType();
639
640 if (actual_is_ptr != expr_is_ptr) {
641 // Incorrect use of "." with a pointer, or "->" with
642 // a class/union/struct instance or reference.
643 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
644 if (actual_is_ptr)
645 error.SetErrorStringWithFormat(
646 "\"%s\" is a pointer and . was used to attempt to access "
647 "\"%s\". Did you mean \"%s->%s\"?",
648 var_expr_path_strm.GetData(), child_name.GetCString(),
649 var_expr_path_strm.GetData(), var_expr.str().c_str());
650 else
651 error.SetErrorStringWithFormat(
652 "\"%s\" is not a pointer and -> was used to attempt to "
653 "access \"%s\". Did you mean \"%s.%s\"?",
654 var_expr_path_strm.GetData(), child_name.GetCString(),
655 var_expr_path_strm.GetData(), var_expr.str().c_str());
656 return ValueObjectSP();
657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000659 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
660 if (!child_valobj_sp) {
661 if (!no_synth_child) {
662 child_valobj_sp = valobj_sp->GetSyntheticValue();
663 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000664 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000665 child_valobj_sp->GetChildMemberWithName(child_name, true);
666 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000667
Zachary Turner24bd3172016-11-17 01:37:52 +0000668 if (no_synth_child || !child_valobj_sp) {
669 // No child member with name "child_name"
670 if (synthetically_added_instance_object) {
671 // We added a "this->" or "self->" to the beginning of the
672 // expression
673 // and this is the first pointer ivar access, so just return
674 // the normal
675 // error
676 error.SetErrorStringWithFormat(
677 "no variable or instance variable named '%s' found in "
678 "this frame",
679 name_const_string.GetCString());
680 } else {
681 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
682 if (child_name) {
683 error.SetErrorStringWithFormat(
684 "\"%s\" is not a member of \"(%s) %s\"",
685 child_name.GetCString(),
686 valobj_sp->GetTypeName().AsCString("<invalid type>"),
687 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 } else {
689 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000690 "incomplete expression path after \"%s\" in \"%s\"",
691 var_expr_path_strm.GetData(), var_expr_cstr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000692 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000693 }
694 return ValueObjectSP();
695 }
696 }
697 synthetically_added_instance_object = false;
698 // Remove the child name from the path
699 var_expr = var_expr.drop_front(child_name.GetLength());
700 if (use_dynamic != eNoDynamicValues) {
701 ValueObjectSP dynamic_value_sp(
702 child_valobj_sp->GetDynamicValue(use_dynamic));
703 if (dynamic_value_sp)
704 child_valobj_sp = dynamic_value_sp;
705 }
706 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707
Zachary Turner24bd3172016-11-17 01:37:52 +0000708 case '[': {
709 // Array member access, or treating pointer as an array
710 // Need at least two brackets and a number
711 if (var_expr.size() <= 2) {
712 error.SetErrorStringWithFormat(
713 "invalid square bracket encountered after \"%s\" in \"%s\"",
714 var_expr_path_strm.GetData(), var_expr.str().c_str());
715 return ValueObjectSP();
716 }
717
718 // Drop the open brace.
719 var_expr = var_expr.drop_front();
720 long child_index = 0;
721
722 // If there's no closing brace, this is an invalid expression.
723 size_t end_pos = var_expr.find_first_of(']');
724 if (end_pos == llvm::StringRef::npos) {
725 error.SetErrorStringWithFormat(
726 "missing closing square bracket in expression \"%s\"",
727 var_expr_path_strm.GetData());
728 return ValueObjectSP();
729 }
730 llvm::StringRef index_expr = var_expr.take_front(end_pos);
731 llvm::StringRef original_index_expr = index_expr;
732 // Drop all of "[index_expr]"
733 var_expr = var_expr.drop_front(end_pos + 1);
734
735 if (index_expr.consumeInteger(0, child_index)) {
736 // If there was no integer anywhere in the index expression, this is
737 // erroneous expression.
738 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
739 index_expr.str().c_str());
740 return ValueObjectSP();
741 }
742
743 if (index_expr.empty()) {
744 // The entire index expression was a single integer.
745
746 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
747 // what we have is *ptr[low]. the most similar C++ syntax is to deref
748 // ptr and extract bit low out of it. reading array item low would be
749 // done by saying ptr[low], without a deref * sign
750 Error error;
751 ValueObjectSP temp(valobj_sp->Dereference(error));
752 if (error.Fail()) {
753 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
754 error.SetErrorStringWithFormat(
755 "could not dereference \"(%s) %s\"",
756 valobj_sp->GetTypeName().AsCString("<invalid type>"),
757 var_expr_path_strm.GetData());
758 return ValueObjectSP();
759 }
760 valobj_sp = temp;
761 deref = false;
762 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
763 deref) {
764 // what we have is *arr[low]. the most similar C++ syntax is
765 // to get arr[0]
766 // (an operation that is equivalent to deref-ing arr)
767 // and extract bit low out of it. reading array item low
768 // would be done by saying arr[low], without a deref * sign
769 Error error;
770 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
771 if (error.Fail()) {
772 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
773 error.SetErrorStringWithFormat(
774 "could not get item 0 for \"(%s) %s\"",
775 valobj_sp->GetTypeName().AsCString("<invalid type>"),
776 var_expr_path_strm.GetData());
777 return ValueObjectSP();
778 }
779 valobj_sp = temp;
780 deref = false;
781 }
782
783 bool is_incomplete_array = false;
784 if (valobj_sp->IsPointerType()) {
785 bool is_objc_pointer = true;
786
787 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
788 eLanguageTypeObjC)
789 is_objc_pointer = false;
790 else if (!valobj_sp->GetCompilerType().IsPointerType())
791 is_objc_pointer = false;
792
793 if (no_synth_child && is_objc_pointer) {
794 error.SetErrorStringWithFormat(
795 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
796 "subscripted",
797 valobj_sp->GetTypeName().AsCString("<invalid type>"),
798 var_expr_path_strm.GetData());
799
800 return ValueObjectSP();
801 } else if (is_objc_pointer) {
802 // dereferencing ObjC variables is not valid.. so let's try
803 // and recur to synthetic children
804 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
805 if (!synthetic /* no synthetic */
806 || synthetic == valobj_sp) /* synthetic is the same as
807 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808 {
809 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
810 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000811 "\"(%s) %s\" is not an array type",
812 valobj_sp->GetTypeName().AsCString("<invalid type>"),
813 var_expr_path_strm.GetData());
814 } else if (
815 static_cast<uint32_t>(child_index) >=
816 synthetic
817 ->GetNumChildren() /* synthetic does not have that many values */) {
818 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
819 error.SetErrorStringWithFormat(
820 "array index %ld is not valid for \"(%s) %s\"", child_index,
821 valobj_sp->GetTypeName().AsCString("<invalid type>"),
822 var_expr_path_strm.GetData());
823 } else {
824 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
825 if (!child_valobj_sp) {
826 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
827 error.SetErrorStringWithFormat(
828 "array index %ld is not valid for \"(%s) %s\"", child_index,
829 valobj_sp->GetTypeName().AsCString("<invalid type>"),
830 var_expr_path_strm.GetData());
831 }
832 }
833 } else {
834 child_valobj_sp =
835 valobj_sp->GetSyntheticArrayMember(child_index, true);
836 if (!child_valobj_sp) {
837 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
838 error.SetErrorStringWithFormat(
839 "failed to use pointer as array for index %ld for "
840 "\"(%s) %s\"",
841 child_index,
842 valobj_sp->GetTypeName().AsCString("<invalid type>"),
843 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 }
845 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000846 } else if (valobj_sp->GetCompilerType().IsArrayType(
847 nullptr, nullptr, &is_incomplete_array)) {
848 // Pass false to dynamic_value here so we can tell the
849 // difference between
850 // no dynamic value and no member of this type...
851 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
852 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
853 child_valobj_sp =
854 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000855
Zachary Turner24bd3172016-11-17 01:37:52 +0000856 if (!child_valobj_sp) {
857 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
858 error.SetErrorStringWithFormat(
859 "array index %ld is not valid for \"(%s) %s\"", child_index,
860 valobj_sp->GetTypeName().AsCString("<invalid type>"),
861 var_expr_path_strm.GetData());
862 }
863 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
864 // this is a bitfield asking to display just one bit
865 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
866 child_index, child_index, true);
867 if (!child_valobj_sp) {
868 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
869 error.SetErrorStringWithFormat(
870 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
871 child_index, child_index,
872 valobj_sp->GetTypeName().AsCString("<invalid type>"),
873 var_expr_path_strm.GetData());
874 }
875 } else {
876 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
877 if (no_synth_child /* synthetic is forbidden */ ||
878 !synthetic /* no synthetic */
879 || synthetic == valobj_sp) /* synthetic is the same as the
880 original object */
881 {
882 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
883 error.SetErrorStringWithFormat(
884 "\"(%s) %s\" is not an array type",
885 valobj_sp->GetTypeName().AsCString("<invalid type>"),
886 var_expr_path_strm.GetData());
887 } else if (
888 static_cast<uint32_t>(child_index) >=
889 synthetic
890 ->GetNumChildren() /* synthetic does not have that many values */) {
891 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
892 error.SetErrorStringWithFormat(
893 "array index %ld is not valid for \"(%s) %s\"", child_index,
894 valobj_sp->GetTypeName().AsCString("<invalid type>"),
895 var_expr_path_strm.GetData());
896 } else {
897 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
898 if (!child_valobj_sp) {
899 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
900 error.SetErrorStringWithFormat(
901 "array index %ld is not valid for \"(%s) %s\"", child_index,
902 valobj_sp->GetTypeName().AsCString("<invalid type>"),
903 var_expr_path_strm.GetData());
904 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000905 }
906 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000907
908 if (!child_valobj_sp) {
909 // Invalid array index...
910 return ValueObjectSP();
911 }
912
913 separator_idx = var_expr.find_first_of(".-[");
914 if (use_dynamic != eNoDynamicValues) {
915 ValueObjectSP dynamic_value_sp(
916 child_valobj_sp->GetDynamicValue(use_dynamic));
917 if (dynamic_value_sp)
918 child_valobj_sp = dynamic_value_sp;
919 }
920 // Break out early from the switch since we were able to find the child
921 // member
922 break;
923 }
924
925 // this is most probably a BitField, let's take a look
926 if (index_expr.front() != '-') {
927 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
928 original_index_expr.str().c_str());
929 return ValueObjectSP();
930 }
931
932 index_expr.drop_front();
933 long final_index = 0;
934 if (index_expr.getAsInteger(0, final_index)) {
935 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
936 original_index_expr.str().c_str());
937 return ValueObjectSP();
938 }
939
940 // if the format given is [high-low], swap range
941 if (child_index > final_index) {
942 long temp = child_index;
943 child_index = final_index;
944 final_index = temp;
945 }
946
947 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
948 // what we have is *ptr[low-high]. the most similar C++ syntax is to
949 // deref ptr and extract bits low thru high out of it. reading array
950 // items low thru high would be done by saying ptr[low-high], without
951 // a deref * sign
952 Error error;
953 ValueObjectSP temp(valobj_sp->Dereference(error));
954 if (error.Fail()) {
955 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
956 error.SetErrorStringWithFormat(
957 "could not dereference \"(%s) %s\"",
958 valobj_sp->GetTypeName().AsCString("<invalid type>"),
959 var_expr_path_strm.GetData());
960 return ValueObjectSP();
961 }
962 valobj_sp = temp;
963 deref = false;
964 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
965 // what we have is *arr[low-high]. the most similar C++ syntax is to get
966 // arr[0] (an operation that is equivalent to deref-ing arr) and extract
967 // bits low thru high out of it. reading array items low thru high would
968 // be done by saying arr[low-high], without a deref * sign
969 Error error;
970 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
971 if (error.Fail()) {
972 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
973 error.SetErrorStringWithFormat(
974 "could not get item 0 for \"(%s) %s\"",
975 valobj_sp->GetTypeName().AsCString("<invalid type>"),
976 var_expr_path_strm.GetData());
977 return ValueObjectSP();
978 }
979 valobj_sp = temp;
980 deref = false;
981 }
982
983 child_valobj_sp =
984 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
985 if (!child_valobj_sp) {
986 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000988 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
989 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
990 var_expr_path_strm.GetData());
991 }
992
993 if (!child_valobj_sp) {
994 // Invalid bitfield range...
995 return ValueObjectSP();
996 }
997
998 separator_idx = var_expr.find_first_of(".-[");
999 if (use_dynamic != eNoDynamicValues) {
1000 ValueObjectSP dynamic_value_sp(
1001 child_valobj_sp->GetDynamicValue(use_dynamic));
1002 if (dynamic_value_sp)
1003 child_valobj_sp = dynamic_value_sp;
1004 }
1005 // Break out early from the switch since we were able to find the child
1006 // member
1007 break;
1008 }
1009 default:
1010 // Failure...
1011 {
1012 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1013 error.SetErrorStringWithFormat(
1014 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1015 separator_type, var_expr_path_strm.GetData(),
1016 var_expr.str().c_str());
1017
1018 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001020 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001021
1022 if (child_valobj_sp)
1023 valobj_sp = child_valobj_sp;
1024
1025 if (var_expr.empty())
1026 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001027 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001028 if (valobj_sp) {
1029 if (deref) {
1030 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1031 valobj_sp = deref_valobj_sp;
1032 } else if (address_of) {
1033 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1034 valobj_sp = address_of_valobj_sp;
1035 }
1036 }
1037 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001038}
1039
1040bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Error *error_ptr) {
1041 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1042 if (!m_cfa_is_valid) {
1043 m_frame_base_error.SetErrorString(
1044 "No frame base available for this historical stack frame.");
1045 return false;
1046 }
1047
1048 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1049 if (m_sc.function) {
1050 m_frame_base.Clear();
1051 m_frame_base_error.Clear();
1052
1053 m_flags.Set(GOT_FRAME_BASE);
1054 ExecutionContext exe_ctx(shared_from_this());
1055 Value expr_value;
1056 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1057 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1058 loclist_base_addr =
1059 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1060 exe_ctx.GetTargetPtr());
1061
1062 if (m_sc.function->GetFrameBaseExpression().Evaluate(
1063 &exe_ctx, nullptr, nullptr, nullptr, loclist_base_addr, nullptr,
1064 nullptr, expr_value, &m_frame_base_error) == false) {
1065 // We should really have an error if evaluate returns, but in case
1066 // we don't, lets set the error to something at least.
1067 if (m_frame_base_error.Success())
1068 m_frame_base_error.SetErrorString(
1069 "Evaluation of the frame base expression failed.");
1070 } else {
1071 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1072 }
1073 } else {
1074 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001075 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 }
1077
1078 if (m_frame_base_error.Success())
1079 frame_base = m_frame_base;
1080
1081 if (error_ptr)
1082 *error_ptr = m_frame_base_error;
1083 return m_frame_base_error.Success();
1084}
1085
1086DWARFExpression *StackFrame::GetFrameBaseExpression(Error *error_ptr) {
1087 if (!m_sc.function) {
1088 if (error_ptr) {
1089 error_ptr->SetErrorString("No function in symbol context.");
1090 }
1091 return nullptr;
1092 }
1093
1094 return &m_sc.function->GetFrameBaseExpression();
1095}
1096
1097RegisterContextSP StackFrame::GetRegisterContext() {
1098 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1099 if (!m_reg_context_sp) {
1100 ThreadSP thread_sp(GetThread());
1101 if (thread_sp)
1102 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1103 }
1104 return m_reg_context_sp;
1105}
1106
1107bool StackFrame::HasDebugInformation() {
1108 GetSymbolContext(eSymbolContextLineEntry);
1109 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001110}
1111
1112ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001113StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1114 DynamicValueType use_dynamic) {
1115 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1116 ValueObjectSP valobj_sp;
1117 if (m_is_history_frame) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001118 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001119 }
1120 VariableList *var_list = GetVariableList(true);
1121 if (var_list) {
1122 // Make sure the variable is a frame variable
1123 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1124 const uint32_t num_variables = var_list->GetSize();
1125 if (var_idx < num_variables) {
1126 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1127 if (!valobj_sp) {
1128 if (m_variable_list_value_objects.GetSize() < num_variables)
1129 m_variable_list_value_objects.Resize(num_variables);
1130 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1131 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1132 }
Enrico Granata592afe72016-03-15 21:50:51 +00001133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001134 }
1135 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1136 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1137 if (dynamic_sp)
1138 return dynamic_sp;
1139 }
1140 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001141}
1142
Kate Stoneb9c1b512016-09-06 20:57:50 +00001143ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1144 DynamicValueType use_dynamic) {
1145 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1146 if (m_is_history_frame)
1147 return ValueObjectSP();
1148
1149 // Check to make sure we aren't already tracking this variable?
1150 ValueObjectSP valobj_sp(
1151 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1152 if (!valobj_sp) {
1153 // We aren't already tracking this global
1154 VariableList *var_list = GetVariableList(true);
1155 // If this frame has no variables, create a new list
1156 if (var_list == nullptr)
1157 m_variable_list_sp.reset(new VariableList());
1158
1159 // Add the global/static variable to this frame
1160 m_variable_list_sp->AddVariable(variable_sp);
1161
1162 // Now make a value object for it so we can track its changes
1163 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1164 }
1165 return valobj_sp;
1166}
1167
1168bool StackFrame::IsInlined() {
1169 if (m_sc.block == nullptr)
1170 GetSymbolContext(eSymbolContextBlock);
1171 if (m_sc.block)
1172 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1173 return false;
1174}
1175
1176lldb::LanguageType StackFrame::GetLanguage() {
1177 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1178 if (cu)
1179 return cu->GetLanguage();
1180 return lldb::eLanguageTypeUnknown;
1181}
1182
1183lldb::LanguageType StackFrame::GuessLanguage() {
1184 LanguageType lang_type = GetLanguage();
1185
1186 if (lang_type == eLanguageTypeUnknown) {
1187 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1188 if (f) {
1189 lang_type = f->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001190 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001191 }
1192
1193 return lang_type;
1194}
1195
1196namespace {
1197std::pair<const Instruction::Operand *, int64_t>
1198GetBaseExplainingValue(const Instruction::Operand &operand,
1199 RegisterContext &register_context, lldb::addr_t value) {
1200 switch (operand.m_type) {
1201 case Instruction::Operand::Type::Dereference:
1202 case Instruction::Operand::Type::Immediate:
1203 case Instruction::Operand::Type::Invalid:
1204 case Instruction::Operand::Type::Product:
1205 // These are not currently interesting
1206 return std::make_pair(nullptr, 0);
1207 case Instruction::Operand::Type::Sum: {
1208 const Instruction::Operand *immediate_child = nullptr;
1209 const Instruction::Operand *variable_child = nullptr;
1210 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1211 immediate_child = &operand.m_children[0];
1212 variable_child = &operand.m_children[1];
1213 } else if (operand.m_children[1].m_type ==
1214 Instruction::Operand::Type::Immediate) {
1215 immediate_child = &operand.m_children[1];
1216 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 if (!immediate_child) {
1219 return std::make_pair(nullptr, 0);
1220 }
1221 lldb::addr_t adjusted_value = value;
1222 if (immediate_child->m_negative) {
1223 adjusted_value += immediate_child->m_immediate;
1224 } else {
1225 adjusted_value -= immediate_child->m_immediate;
1226 }
1227 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1228 GetBaseExplainingValue(*variable_child, register_context,
1229 adjusted_value);
1230 if (!base_and_offset.first) {
1231 return std::make_pair(nullptr, 0);
1232 }
1233 if (immediate_child->m_negative) {
1234 base_and_offset.second -= immediate_child->m_immediate;
1235 } else {
1236 base_and_offset.second += immediate_child->m_immediate;
1237 }
1238 return base_and_offset;
1239 }
1240 case Instruction::Operand::Type::Register: {
1241 const RegisterInfo *info =
1242 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1243 if (!info) {
1244 return std::make_pair(nullptr, 0);
1245 }
1246 RegisterValue reg_value;
1247 if (!register_context.ReadRegister(info, reg_value)) {
1248 return std::make_pair(nullptr, 0);
1249 }
1250 if (reg_value.GetAsUInt64() == value) {
1251 return std::make_pair(&operand, 0);
1252 } else {
1253 return std::make_pair(nullptr, 0);
1254 }
1255 }
1256 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001257 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001258}
1259
1260std::pair<const Instruction::Operand *, int64_t>
1261GetBaseExplainingDereference(const Instruction::Operand &operand,
1262 RegisterContext &register_context,
1263 lldb::addr_t addr) {
1264 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1265 return GetBaseExplainingValue(operand.m_children[0], register_context,
1266 addr);
1267 }
1268 return std::make_pair(nullptr, 0);
1269}
Ilia K4f730dc2016-09-12 05:25:33 +00001270}
Sean Callanan4740a732016-09-06 04:48:36 +00001271
Kate Stoneb9c1b512016-09-06 20:57:50 +00001272lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1273 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001274
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275 const ArchSpec &target_arch = target_sp->GetArchitecture();
1276
1277 AddressRange pc_range;
1278 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1279 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1280
1281 ExecutionContext exe_ctx(shared_from_this());
1282
1283 const char *plugin_name = nullptr;
1284 const char *flavor = nullptr;
1285 const bool prefer_file_cache = false;
1286
1287 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1288 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1289
1290 if (!disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001291 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 }
Sean Callanan4740a732016-09-06 04:48:36 +00001293
Kate Stoneb9c1b512016-09-06 20:57:50 +00001294 InstructionSP instruction_sp =
1295 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1296
1297 llvm::SmallVector<Instruction::Operand, 3> operands;
1298
1299 if (!instruction_sp->ParseOperands(operands)) {
1300 return ValueObjectSP();
1301 }
1302
1303 RegisterContextSP register_context_sp = GetRegisterContext();
1304
1305 if (!register_context_sp) {
1306 return ValueObjectSP();
1307 }
1308
1309 for (const Instruction::Operand &operand : operands) {
1310 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1311 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1312
1313 if (!base_and_offset.first) {
1314 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001315 }
Sean Callanan4740a732016-09-06 04:48:36 +00001316
Kate Stoneb9c1b512016-09-06 20:57:50 +00001317 switch (base_and_offset.first->m_type) {
1318 case Instruction::Operand::Type::Immediate: {
1319 lldb_private::Address addr;
1320 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1321 base_and_offset.second,
1322 addr)) {
1323 TypeSystem *c_type_system =
1324 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1325 if (!c_type_system) {
1326 return ValueObjectSP();
1327 } else {
1328 CompilerType void_ptr_type =
1329 c_type_system
1330 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1331 .GetPointerType();
1332 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001333 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001335 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001336 }
1337 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001338 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 case Instruction::Operand::Type::Register: {
1340 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1341 base_and_offset.second);
1342 }
1343 default:
1344 return ValueObjectSP();
1345 }
1346 }
1347
1348 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001349}
1350
Kate Stoneb9c1b512016-09-06 20:57:50 +00001351namespace {
1352ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1353 int64_t offset) {
1354 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1355 return ValueObjectSP();
1356 }
Sean Callanan4740a732016-09-06 04:48:36 +00001357
Kate Stoneb9c1b512016-09-06 20:57:50 +00001358 if (parent->IsPointerOrReferenceType()) {
1359 return parent;
1360 }
1361
1362 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1363 const bool can_create = true;
1364 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1365
1366 if (!child_sp) {
1367 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001368 }
1369
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 int64_t child_offset = child_sp->GetByteOffset();
1371 int64_t child_size = child_sp->GetByteSize();
1372
1373 if (offset >= child_offset && offset < (child_offset + child_size)) {
1374 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001375 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 }
Sean Callanan4740a732016-09-06 04:48:36 +00001377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 if (offset == 0) {
1379 return parent;
1380 } else {
1381 return ValueObjectSP();
1382 }
1383}
1384
1385ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1386 ValueObjectSP &base,
1387 int64_t offset) {
1388 // base is a pointer to something
1389 // offset is the thing to add to the pointer
1390 // We return the most sensible ValueObject for the result of *(base+offset)
1391
1392 if (!base->IsPointerOrReferenceType()) {
1393 return ValueObjectSP();
1394 }
1395
1396 Error error;
1397 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001398
1399 if (!pointee) {
1400 return ValueObjectSP();
1401 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402
Ilia K4f730dc2016-09-12 05:25:33 +00001403 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404 int64_t index = offset / pointee->GetByteSize();
1405 offset = offset % pointee->GetByteSize();
1406 const bool can_create = true;
1407 pointee = base->GetSyntheticArrayMember(index, can_create);
1408 }
1409
1410 if (!pointee || error.Fail()) {
1411 return ValueObjectSP();
1412 }
1413
1414 return GetValueForOffset(frame, pointee, offset);
1415}
1416
1417//------------------------------------------------------------------
1418/// Attempt to reconstruct the ValueObject for the address contained in a
1419/// given register plus an offset.
1420///
1421/// @params [in] frame
1422/// The current stack frame.
1423///
1424/// @params [in] reg
1425/// The register.
1426///
1427/// @params [in] offset
1428/// The offset from the register.
1429///
1430/// @param [in] disassembler
1431/// A disassembler containing instructions valid up to the current PC.
1432///
1433/// @param [in] variables
1434/// The variable list from the current frame,
1435///
1436/// @param [in] pc
1437/// The program counter for the instruction considered the 'user'.
1438///
1439/// @return
1440/// A string describing the base for the ExpressionPath. This could be a
1441/// variable, a register value, an argument, or a function return value.
1442/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1443//------------------------------------------------------------------
1444lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1445 int64_t offset, Disassembler &disassembler,
1446 VariableList &variables, const Address &pc) {
1447 // Example of operation for Intel:
1448 //
1449 // +14: movq -0x8(%rbp), %rdi
1450 // +18: movq 0x8(%rdi), %rdi
1451 // +22: addl 0x4(%rdi), %eax
1452 //
1453 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1454 //
1455 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18
1456 // that assigns to rdi, and calls itself recursively for that dereference
1457 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1458 // +14 that assigns to rdi, and calls itself recursively for that
1459 // derefernece
1460 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1461 // variable list.
1462 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1463 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1464 // at +18)
1465 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1466 // rdi+4 at +22)
1467
1468 // First, check the variable list to see if anything is at the specified
1469 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001470
Sean Callanan50857102016-09-14 00:48:19 +00001471 using namespace OperandMatchers;
1472
Sean Callanan0ac172d2016-09-14 20:29:57 +00001473 const RegisterInfo *reg_info =
1474 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1475 if (!reg_info) {
1476 return ValueObjectSP();
1477 }
1478
Sean Callanan807ee2f2016-09-13 21:18:27 +00001479 Instruction::Operand op =
1480 offset ? Instruction::Operand::BuildDereference(
1481 Instruction::Operand::BuildSum(
1482 Instruction::Operand::BuildRegister(reg),
1483 Instruction::Operand::BuildImmediate(offset)))
1484 : Instruction::Operand::BuildDereference(
1485 Instruction::Operand::BuildRegister(reg));
1486
Kate Stoneb9c1b512016-09-06 20:57:50 +00001487 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1488 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001489 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1490 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001491 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 }
1493
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 const uint32_t current_inst =
1495 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1496 if (current_inst == UINT32_MAX) {
1497 return ValueObjectSP();
1498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1501 // This is not an exact algorithm, and it sacrifices accuracy for
Sean Callanan0ac172d2016-09-14 20:29:57 +00001502 // generality. Recognizing "mov" and "ld" instructions –– and which are
1503 // their source and destination operands -- is something the disassembler
1504 // should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001505 InstructionSP instruction_sp =
1506 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001507
Sean Callanan50857102016-09-14 00:48:19 +00001508 if (instruction_sp->IsCall()) {
1509 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1510 if (!abi_sp) {
1511 continue;
1512 }
1513
1514 const char *return_register_name;
1515 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1516 continue;
1517 }
1518
1519 const RegisterInfo *return_register_info =
1520 frame.GetRegisterContext()->GetRegisterInfoByName(
1521 return_register_name);
1522 if (!return_register_info) {
1523 continue;
1524 }
1525
1526 int64_t offset = 0;
1527
1528 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1529 MatchRegOp(*return_register_info))(op) &&
1530 !MatchUnaryOp(
1531 MatchOpType(Instruction::Operand::Type::Dereference),
1532 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1533 MatchRegOp(*return_register_info),
1534 FetchImmOp(offset)))(op)) {
1535 continue;
1536 }
1537
Kate Stoneb9c1b512016-09-06 20:57:50 +00001538 llvm::SmallVector<Instruction::Operand, 1> operands;
1539 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1540 continue;
1541 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001542
Kate Stoneb9c1b512016-09-06 20:57:50 +00001543 switch (operands[0].m_type) {
1544 default:
1545 break;
1546 case Instruction::Operand::Type::Immediate: {
1547 SymbolContext sc;
1548 Address load_address;
1549 if (!frame.CalculateTarget()->ResolveLoadAddress(
1550 operands[0].m_immediate, load_address)) {
1551 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001552 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001553 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1554 load_address, eSymbolContextFunction, sc);
1555 if (!sc.function) {
1556 break;
1557 }
1558 CompilerType function_type = sc.function->GetCompilerType();
1559 if (!function_type.IsFunctionType()) {
1560 break;
1561 }
1562 CompilerType return_type = function_type.GetFunctionReturnType();
1563 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001564 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 return_value)) {
1566 break;
1567 }
1568 std::string name_str(
1569 sc.function->GetName().AsCString("<unknown function>"));
1570 name_str.append("()");
1571 Address return_value_address(return_value.GetAsUInt64());
1572 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001573 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001574 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1575 }
1576 }
1577
1578 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001579 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580
1581 llvm::SmallVector<Instruction::Operand, 2> operands;
1582 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1583 continue;
1584 }
1585
Kate Stoneb9c1b512016-09-06 20:57:50 +00001586 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001587 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1588 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1589 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001590
1591 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001593 }
1594 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001595 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001596 }
1597 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001598 continue;
1599 }
1600
1601 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001602 ValueObjectSP source_path;
1603 ConstString origin_register;
1604 int64_t origin_offset = 0;
1605
1606 if (FetchRegOp(origin_register)(*origin_operand)) {
1607 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1608 variables, instruction_sp->GetAddress());
1609 } else if (MatchUnaryOp(
1610 MatchOpType(Instruction::Operand::Type::Dereference),
1611 FetchRegOp(origin_register))(*origin_operand) ||
1612 MatchUnaryOp(
1613 MatchOpType(Instruction::Operand::Type::Dereference),
1614 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1615 FetchRegOp(origin_register),
1616 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001618 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001620 if (!source_path) {
1621 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001622 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001623 source_path =
1624 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625 }
1626
1627 if (source_path) {
1628 return source_path;
1629 }
1630 }
1631
1632 return ValueObjectSP();
1633}
1634}
1635
1636lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1637 int64_t offset) {
1638 TargetSP target_sp = CalculateTarget();
1639
1640 const ArchSpec &target_arch = target_sp->GetArchitecture();
1641
1642 Block *frame_block = GetFrameBlock();
1643
1644 if (!frame_block) {
1645 return ValueObjectSP();
1646 }
1647
1648 Function *function = frame_block->CalculateSymbolContextFunction();
1649 if (!function) {
1650 return ValueObjectSP();
1651 }
1652
1653 AddressRange pc_range = function->GetAddressRange();
1654
1655 if (GetFrameCodeAddress().GetFileAddress() <
1656 pc_range.GetBaseAddress().GetFileAddress() ||
1657 GetFrameCodeAddress().GetFileAddress() -
1658 pc_range.GetBaseAddress().GetFileAddress() >=
1659 pc_range.GetByteSize()) {
1660 return ValueObjectSP();
1661 }
1662
1663 ExecutionContext exe_ctx(shared_from_this());
1664
1665 const char *plugin_name = nullptr;
1666 const char *flavor = nullptr;
1667 const bool prefer_file_cache = false;
1668 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1669 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1670
1671 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1672 return ValueObjectSP();
1673 }
1674
1675 const bool get_file_globals = false;
1676 VariableList *variables = GetVariableList(get_file_globals);
1677
1678 if (!variables) {
1679 return ValueObjectSP();
1680 }
1681
1682 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1683 GetFrameCodeAddress());
1684}
1685
1686TargetSP StackFrame::CalculateTarget() {
1687 TargetSP target_sp;
1688 ThreadSP thread_sp(GetThread());
1689 if (thread_sp) {
1690 ProcessSP process_sp(thread_sp->CalculateProcess());
1691 if (process_sp)
1692 target_sp = process_sp->CalculateTarget();
1693 }
1694 return target_sp;
1695}
1696
1697ProcessSP StackFrame::CalculateProcess() {
1698 ProcessSP process_sp;
1699 ThreadSP thread_sp(GetThread());
1700 if (thread_sp)
1701 process_sp = thread_sp->CalculateProcess();
1702 return process_sp;
1703}
1704
1705ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1706
1707StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1708
1709void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1710 exe_ctx.SetContext(shared_from_this());
1711}
1712
1713void StackFrame::DumpUsingSettingsFormat(Stream *strm,
1714 const char *frame_marker) {
1715 if (strm == nullptr)
1716 return;
1717
1718 GetSymbolContext(eSymbolContextEverything);
1719 ExecutionContext exe_ctx(shared_from_this());
1720 StreamString s;
1721
1722 if (frame_marker)
1723 s.PutCString(frame_marker);
1724
1725 const FormatEntity::Entry *frame_format = nullptr;
1726 Target *target = exe_ctx.GetTargetPtr();
1727 if (target)
1728 frame_format = target->GetDebugger().GetFrameFormat();
1729 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1730 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001731 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 } else {
1733 Dump(strm, true, false);
1734 strm->EOL();
1735 }
1736}
1737
1738void StackFrame::Dump(Stream *strm, bool show_frame_index,
1739 bool show_fullpaths) {
1740 if (strm == nullptr)
1741 return;
1742
1743 if (show_frame_index)
1744 strm->Printf("frame #%u: ", m_frame_index);
1745 ExecutionContext exe_ctx(shared_from_this());
1746 Target *target = exe_ctx.GetTargetPtr();
1747 strm->Printf("0x%0*" PRIx64 " ",
1748 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1749 : 16,
1750 GetFrameCodeAddress().GetLoadAddress(target));
1751 GetSymbolContext(eSymbolContextEverything);
1752 const bool show_module = true;
1753 const bool show_inline = true;
1754 const bool show_function_arguments = true;
1755 const bool show_function_name = true;
1756 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1757 GetFrameCodeAddress(), show_fullpaths, show_module,
1758 show_inline, show_function_arguments,
1759 show_function_name);
1760}
1761
1762void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1763 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1764 assert(GetStackID() ==
1765 prev_frame.GetStackID()); // TODO: remove this after some testing
1766 m_variable_list_sp = prev_frame.m_variable_list_sp;
1767 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001768 if (!m_disassembly.GetString().empty()) {
1769 m_disassembly.Clear();
1770 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772}
1773
1774void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1775 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1776 assert(GetStackID() ==
1777 curr_frame.GetStackID()); // TODO: remove this after some testing
1778 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1779 assert(GetThread() == curr_frame.GetThread());
1780 m_frame_index = curr_frame.m_frame_index;
1781 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1782 m_reg_context_sp = curr_frame.m_reg_context_sp;
1783 m_frame_code_addr = curr_frame.m_frame_code_addr;
1784 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1785 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1786 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1787 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1788 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1789 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1790 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1791 m_sc.function == curr_frame.m_sc.function);
1792 m_sc = curr_frame.m_sc;
1793 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1794 m_flags.Set(m_sc.GetResolvedMask());
1795 m_frame_base.Clear();
1796 m_frame_base_error.Clear();
1797}
1798
1799bool StackFrame::HasCachedData() const {
1800 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001801 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001802 if (m_variable_list_value_objects.GetSize() > 0)
1803 return true;
1804 if (!m_disassembly.GetString().empty())
1805 return true;
1806 return false;
1807}
1808
1809bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1810 const char *frame_marker) {
1811
1812 if (show_frame_info) {
1813 strm.Indent();
1814 DumpUsingSettingsFormat(&strm, frame_marker);
1815 }
1816
1817 if (show_source) {
1818 ExecutionContext exe_ctx(shared_from_this());
1819 bool have_source = false, have_debuginfo = false;
1820 Debugger::StopDisassemblyType disasm_display =
1821 Debugger::eStopDisassemblyTypeNever;
1822 Target *target = exe_ctx.GetTargetPtr();
1823 if (target) {
1824 Debugger &debugger = target->GetDebugger();
1825 const uint32_t source_lines_before =
1826 debugger.GetStopSourceLineCount(true);
1827 const uint32_t source_lines_after =
1828 debugger.GetStopSourceLineCount(false);
1829 disasm_display = debugger.GetStopDisassemblyDisplay();
1830
1831 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1832 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1833 have_debuginfo = true;
1834 if (source_lines_before > 0 || source_lines_after > 0) {
1835 size_t num_lines =
1836 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1837 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001838 m_sc.line_entry.column, source_lines_before,
1839 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001840 if (num_lines != 0)
1841 have_source = true;
1842 // TODO: Give here a one time warning if source file is missing.
1843 }
1844 }
1845 switch (disasm_display) {
1846 case Debugger::eStopDisassemblyTypeNever:
1847 break;
1848
1849 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1850 if (have_debuginfo)
1851 break;
1852 LLVM_FALLTHROUGH;
1853
1854 case Debugger::eStopDisassemblyTypeNoSource:
1855 if (have_source)
1856 break;
1857 LLVM_FALLTHROUGH;
1858
1859 case Debugger::eStopDisassemblyTypeAlways:
1860 if (target) {
1861 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1862 if (disasm_lines > 0) {
1863 const ArchSpec &target_arch = target->GetArchitecture();
1864 AddressRange pc_range;
1865 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1866 pc_range.SetByteSize(disasm_lines *
1867 target_arch.GetMaximumOpcodeByteSize());
1868 const char *plugin_name = nullptr;
1869 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001870 const bool mixed_source_and_assembly = false;
1871 Disassembler::Disassemble(
1872 target->GetDebugger(), target_arch, plugin_name, flavor,
1873 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1874 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001875 }
1876 }
1877 break;
1878 }
1879 }
1880 }
1881 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001882}