blob: 03151fbfc17f7ce0435aedec8085fc01b1ba6efd [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"
Zachary Turner2f3df612017-04-06 21:28:29 +000020#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Value.h"
Greg Clayton54979cd2010-12-15 05:08:08 +000022#include "lldb/Core/ValueObjectConstResult.h"
Sean Callanan4740a732016-09-06 04:48:36 +000023#include "lldb/Core/ValueObjectMemory.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000024#include "lldb/Core/ValueObjectVariable.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Symbol/Function.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Symbol/Symbol.h"
28#include "lldb/Symbol/SymbolContextScope.h"
Enrico Granata46252392015-11-19 22:28:58 +000029#include "lldb/Symbol/Type.h"
Greg Clayton288bdf92010-09-02 02:59:18 +000030#include "lldb/Symbol/VariableList.h"
Sean Callanan4740a732016-09-06 04:48:36 +000031#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/ExecutionContext.h"
33#include "lldb/Target/Process.h"
34#include "lldb/Target/RegisterContext.h"
35#include "lldb/Target/Target.h"
36#include "lldb/Target/Thread.h"
37
38using namespace lldb;
39using namespace lldb_private;
40
41// The first bits in the flags are reserved for the SymbolContext::Scope bits
42// so we know if we have tried to look up information in our internal symbol
43// context (m_sc) already.
Kate Stoneb9c1b512016-09-06 20:57:50 +000044#define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
45#define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
46#define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
47#define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
48#define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
51 user_id_t unwind_frame_index, addr_t cfa,
52 bool cfa_is_valid, addr_t pc, uint32_t stop_id,
53 bool stop_id_is_valid, bool is_history_frame,
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000054 const SymbolContext *sc_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
56 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
57 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
58 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
59 m_stop_id(stop_id), m_stop_id_is_valid(stop_id_is_valid),
60 m_is_history_frame(is_history_frame), m_variable_list_sp(),
61 m_variable_list_value_objects(), m_disassembly(), m_mutex() {
62 // If we don't have a CFA value, use the frame index for our StackID so that
63 // recursive
64 // functions properly aren't confused with one another on a history stack.
65 if (m_is_history_frame && !m_cfa_is_valid) {
66 m_id.SetCFA(m_frame_index);
67 }
Jason Molenda99618472013-11-04 11:02:52 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 if (sc_ptr != nullptr) {
70 m_sc = *sc_ptr;
71 m_flags.Set(m_sc.GetResolvedMask());
72 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
76 user_id_t unwind_frame_index,
77 const RegisterContextSP &reg_context_sp, addr_t cfa,
78 addr_t pc, const SymbolContext *sc_ptr)
79 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000080 m_concrete_frame_index(unwind_frame_index),
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
82 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
83 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
84 m_stop_id_is_valid(false), m_is_history_frame(false),
85 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
86 m_mutex() {
87 if (sc_ptr != nullptr) {
88 m_sc = *sc_ptr;
89 m_flags.Set(m_sc.GetResolvedMask());
90 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 if (reg_context_sp && !m_sc.target_sp) {
93 m_sc.target_sp = reg_context_sp->CalculateTarget();
94 if (m_sc.target_sp)
95 m_flags.Set(eSymbolContextTarget);
96 }
Greg Clayton1b72fcb2010-08-24 00:45:41 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
100 user_id_t unwind_frame_index,
101 const RegisterContextSP &reg_context_sp, addr_t cfa,
102 const Address &pc_addr, const SymbolContext *sc_ptr)
103 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000104 m_concrete_frame_index(unwind_frame_index),
105 m_reg_context_sp(reg_context_sp),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
107 nullptr),
108 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
109 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
110 m_stop_id_is_valid(false), m_is_history_frame(false),
111 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
112 m_mutex() {
113 if (sc_ptr != nullptr) {
114 m_sc = *sc_ptr;
115 m_flags.Set(m_sc.GetResolvedMask());
116 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 if (!m_sc.target_sp && reg_context_sp) {
119 m_sc.target_sp = reg_context_sp->CalculateTarget();
120 if (m_sc.target_sp)
121 m_flags.Set(eSymbolContextTarget);
122 }
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 ModuleSP pc_module_sp(pc_addr.GetModule());
125 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
126 if (pc_module_sp) {
127 m_sc.module_sp = pc_module_sp;
128 m_flags.Set(eSymbolContextModule);
129 } else {
130 m_sc.module_sp.reset();
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133}
134
Eugene Zelenkod70a6e72016-02-18 18:52:47 +0000135StackFrame::~StackFrame() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137StackID &StackFrame::GetStackID() {
138 std::lock_guard<std::recursive_mutex> guard(m_mutex);
139 // Make sure we have resolved the StackID object's symbol context scope if
140 // we already haven't looked it up.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
143 if (m_id.GetSymbolContextScope()) {
144 // We already have a symbol context scope, we just don't have our
145 // flag bit set.
146 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
147 } else {
148 // Calculate the frame block and use this for the stack ID symbol
149 // context scope if we have one.
150 SymbolContextScope *scope = GetFrameBlock();
151 if (scope == nullptr) {
152 // We don't have a block, so use the symbol
153 if (m_flags.IsClear(eSymbolContextSymbol))
154 GetSymbolContext(eSymbolContextSymbol);
155
156 // It is ok if m_sc.symbol is nullptr here
157 scope = m_sc.symbol;
158 }
159 // Set the symbol context scope (the accessor will set the
160 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
161 SetSymbolContextScope(scope);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 }
164 return m_id;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167uint32_t StackFrame::GetFrameIndex() const {
168 ThreadSP thread_sp = GetThread();
169 if (thread_sp)
170 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
171 m_frame_index);
172 else
173 return m_frame_index;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000174}
175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
177 std::lock_guard<std::recursive_mutex> guard(m_mutex);
178 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
179 m_id.SetSymbolContextScope(symbol_scope);
Greg Clayton59e8fc1c2010-08-30 18:11:35 +0000180}
181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182const Address &StackFrame::GetFrameCodeAddress() {
183 std::lock_guard<std::recursive_mutex> guard(m_mutex);
184 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
185 !m_frame_code_addr.IsSectionOffset()) {
186 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 // Resolve the PC into a temporary address because if ResolveLoadAddress
189 // fails to resolve the address, it will clear the address object...
190 ThreadSP thread_sp(GetThread());
191 if (thread_sp) {
192 TargetSP target_sp(thread_sp->CalculateTarget());
193 if (target_sp) {
194 if (m_frame_code_addr.SetOpcodeLoadAddress(
195 m_frame_code_addr.GetOffset(), target_sp.get(),
196 eAddressClassCode)) {
197 ModuleSP module_sp(m_frame_code_addr.GetModule());
198 if (module_sp) {
199 m_sc.module_sp = module_sp;
200 m_flags.Set(eSymbolContextModule);
201 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 }
206 return m_frame_code_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209bool StackFrame::ChangePC(addr_t pc) {
210 std::lock_guard<std::recursive_mutex> guard(m_mutex);
211 // We can't change the pc value of a history stack frame - it is immutable.
212 if (m_is_history_frame)
213 return false;
214 m_frame_code_addr.SetRawAddress(pc);
215 m_sc.Clear(false);
216 m_flags.Reset(0);
217 ThreadSP thread_sp(GetThread());
218 if (thread_sp)
219 thread_sp->ClearStackFrames();
220 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221}
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223const char *StackFrame::Disassemble() {
224 std::lock_guard<std::recursive_mutex> guard(m_mutex);
Zachary Turner8b3f2162017-02-28 17:59:59 +0000225 if (m_disassembly.Empty()) {
226 ExecutionContext exe_ctx(shared_from_this());
227 Target *target = exe_ctx.GetTargetPtr();
228 if (target) {
229 const char *plugin_name = nullptr;
230 const char *flavor = nullptr;
231 Disassembler::Disassemble(target->GetDebugger(),
232 target->GetArchitecture(), plugin_name, flavor,
233 exe_ctx, 0, false, 0, 0, m_disassembly);
234 }
235 if (m_disassembly.Empty())
236 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 }
Zachary Turner8b3f2162017-02-28 17:59:59 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 return m_disassembly.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242Block *StackFrame::GetFrameBlock() {
243 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
244 GetSymbolContext(eSymbolContextBlock);
Greg Clayton95897c62010-09-07 04:20:48 +0000245
Kate Stoneb9c1b512016-09-06 20:57:50 +0000246 if (m_sc.block) {
247 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
248 if (inline_block) {
249 // Use the block with the inlined function info
250 // as the frame block we want this frame to have only the variables
251 // for the inlined function and its non-inlined block child blocks.
252 return inline_block;
253 } else {
254 // This block is not contained within any inlined function blocks
255 // with so we want to use the top most function block.
256 return &m_sc.function->GetBlock(false);
257 }
258 }
259 return nullptr;
Greg Clayton95897c62010-09-07 04:20:48 +0000260}
261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262//----------------------------------------------------------------------
263// Get the symbol context if we already haven't done so by resolving the
264// PC address as much as possible. This way when we pass around a
265// StackFrame object, everyone will have as much information as
266// possible and no one will ever have to look things up manually.
267//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268const SymbolContext &StackFrame::GetSymbolContext(uint32_t resolve_scope) {
269 std::lock_guard<std::recursive_mutex> guard(m_mutex);
270 // Copy our internal symbol context into "sc".
271 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
272 uint32_t resolved = 0;
Greg Clayton75a03332012-11-29 00:53:06 +0000273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 // If the target was requested add that:
275 if (!m_sc.target_sp) {
276 m_sc.target_sp = CalculateTarget();
277 if (m_sc.target_sp)
278 resolved |= eSymbolContextTarget;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 }
280
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 // Resolve our PC to section offset if we haven't already done so
282 // and if we don't have a module. The resolved address section will
283 // contain the module to which it belongs
284 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
285 GetFrameCodeAddress();
286
287 // If this is not frame zero, then we need to subtract 1 from the PC
288 // value when doing address lookups since the PC will be on the
289 // instruction following the function call instruction...
290
291 Address lookup_addr(GetFrameCodeAddress());
292 if (m_frame_index > 0 && lookup_addr.IsValid()) {
293 addr_t offset = lookup_addr.GetOffset();
294 if (offset > 0) {
295 lookup_addr.SetOffset(offset - 1);
296
297 } else {
298 // lookup_addr is the start of a section. We need
299 // do the math on the actual load address and re-compute
300 // the section. We're working with a 'noreturn' function
301 // at the end of a section.
302 ThreadSP thread_sp(GetThread());
303 if (thread_sp) {
304 TargetSP target_sp(thread_sp->CalculateTarget());
305 if (target_sp) {
306 addr_t addr_minus_one =
307 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
308 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
309 } else {
310 lookup_addr.SetOffset(offset - 1);
311 }
312 }
313 }
314 }
315
316 if (m_sc.module_sp) {
317 // We have something in our stack frame symbol context, lets check
318 // if we haven't already tried to lookup one of those things. If we
319 // haven't then we will do the query.
320
321 uint32_t actual_resolve_scope = 0;
322
323 if (resolve_scope & eSymbolContextCompUnit) {
324 if (m_flags.IsClear(eSymbolContextCompUnit)) {
325 if (m_sc.comp_unit)
326 resolved |= eSymbolContextCompUnit;
327 else
328 actual_resolve_scope |= eSymbolContextCompUnit;
329 }
330 }
331
332 if (resolve_scope & eSymbolContextFunction) {
333 if (m_flags.IsClear(eSymbolContextFunction)) {
334 if (m_sc.function)
335 resolved |= eSymbolContextFunction;
336 else
337 actual_resolve_scope |= eSymbolContextFunction;
338 }
339 }
340
341 if (resolve_scope & eSymbolContextBlock) {
342 if (m_flags.IsClear(eSymbolContextBlock)) {
343 if (m_sc.block)
344 resolved |= eSymbolContextBlock;
345 else
346 actual_resolve_scope |= eSymbolContextBlock;
347 }
348 }
349
350 if (resolve_scope & eSymbolContextSymbol) {
351 if (m_flags.IsClear(eSymbolContextSymbol)) {
352 if (m_sc.symbol)
353 resolved |= eSymbolContextSymbol;
354 else
355 actual_resolve_scope |= eSymbolContextSymbol;
356 }
357 }
358
359 if (resolve_scope & eSymbolContextLineEntry) {
360 if (m_flags.IsClear(eSymbolContextLineEntry)) {
361 if (m_sc.line_entry.IsValid())
362 resolved |= eSymbolContextLineEntry;
363 else
364 actual_resolve_scope |= eSymbolContextLineEntry;
365 }
366 }
367
368 if (actual_resolve_scope) {
369 // We might be resolving less information than what is already
370 // in our current symbol context so resolve into a temporary
371 // symbol context "sc" so we don't clear out data we have
372 // already found in "m_sc"
373 SymbolContext sc;
374 // Set flags that indicate what we have tried to resolve
375 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
376 lookup_addr, actual_resolve_scope, sc);
377 // Only replace what we didn't already have as we may have
378 // information for an inlined function scope that won't match
379 // what a standard lookup by address would match
380 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
381 m_sc.comp_unit = sc.comp_unit;
382 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
383 m_sc.function = sc.function;
384 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
385 m_sc.block = sc.block;
386 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
387 m_sc.symbol = sc.symbol;
388 if ((resolved & eSymbolContextLineEntry) &&
389 !m_sc.line_entry.IsValid()) {
390 m_sc.line_entry = sc.line_entry;
391 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
392 }
393 }
394 } else {
395 // If we don't have a module, then we can't have the compile unit,
396 // function, block, line entry or symbol, so we can safely call
397 // ResolveSymbolContextForAddress with our symbol context member m_sc.
398 if (m_sc.target_sp) {
399 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
400 lookup_addr, resolve_scope, m_sc);
401 }
402 }
403
404 // Update our internal flags so we remember what we have tried to locate so
405 // we don't have to keep trying when more calls to this function are made.
406 // We might have dug up more information that was requested (for example
407 // if we were asked to only get the block, we will have gotten the
408 // compile unit, and function) so set any additional bits that we resolved
409 m_flags.Set(resolve_scope | resolved);
410 }
411
412 // Return the symbol context with everything that was possible to resolve
413 // resolved.
414 return m_sc;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
Kate Stoneb9c1b512016-09-06 20:57:50 +0000417VariableList *StackFrame::GetVariableList(bool get_file_globals) {
418 std::lock_guard<std::recursive_mutex> guard(m_mutex);
419 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
420 m_flags.Set(RESOLVED_VARIABLES);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 Block *frame_block = GetFrameBlock();
423
424 if (frame_block) {
425 const bool get_child_variables = true;
426 const bool can_create = true;
427 const bool stop_if_child_block_is_inlined_function = true;
428 m_variable_list_sp.reset(new VariableList());
429 frame_block->AppendBlockVariables(can_create, get_child_variables,
430 stop_if_child_block_is_inlined_function,
Zachary Turner3bc714b2017-03-02 00:05:25 +0000431 [](Variable *v) { return true; },
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432 m_variable_list_sp.get());
Sean Callanan7c0962d2010-11-01 04:38:59 +0000433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000434 }
435
436 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
437 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
438
439 if (m_flags.IsClear(eSymbolContextCompUnit))
440 GetSymbolContext(eSymbolContextCompUnit);
441
442 if (m_sc.comp_unit) {
443 VariableListSP global_variable_list_sp(
444 m_sc.comp_unit->GetVariableList(true));
445 if (m_variable_list_sp)
446 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
447 else
448 m_variable_list_sp = global_variable_list_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 }
451
452 return m_variable_list_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453}
454
Greg Claytond41f0322011-08-02 23:35:43 +0000455VariableListSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456StackFrame::GetInScopeVariableList(bool get_file_globals,
457 bool must_have_valid_location) {
458 std::lock_guard<std::recursive_mutex> guard(m_mutex);
459 // We can't fetch variable information for a history stack frame.
460 if (m_is_history_frame)
461 return VariableListSP();
Jason Molenda99618472013-11-04 11:02:52 +0000462
Kate Stoneb9c1b512016-09-06 20:57:50 +0000463 VariableListSP var_list_sp(new VariableList);
464 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
Greg Claytond41f0322011-08-02 23:35:43 +0000465
Kate Stoneb9c1b512016-09-06 20:57:50 +0000466 if (m_sc.block) {
467 const bool can_create = true;
468 const bool get_parent_variables = true;
469 const bool stop_if_block_is_inlined_function = true;
470 m_sc.block->AppendVariables(
471 can_create, get_parent_variables, stop_if_block_is_inlined_function,
472 [this, must_have_valid_location](Variable *v) {
473 return v->IsInScope(this) && (!must_have_valid_location ||
474 v->LocationIsValidForFrame(this));
475 },
476 var_list_sp.get());
477 }
478
479 if (m_sc.comp_unit && get_file_globals) {
480 VariableListSP global_variable_list_sp(
481 m_sc.comp_unit->GetVariableList(true));
482 if (global_variable_list_sp)
483 var_list_sp->AddVariables(global_variable_list_sp.get());
484 }
485
486 return var_list_sp;
Greg Claytond41f0322011-08-02 23:35:43 +0000487}
488
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
Zachary Turner0eb31a12016-11-17 05:14:32 +0000490 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
491 VariableSP &var_sp, Error &error) {
492 llvm::StringRef original_var_expr = var_expr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000493 // We can't fetch variable information for a history stack frame.
494 if (m_is_history_frame)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000495 return ValueObjectSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000496
Zachary Turner0eb31a12016-11-17 05:14:32 +0000497 if (var_expr.empty()) {
498 error.SetErrorStringWithFormat("invalid variable path '%s'",
499 var_expr.str().c_str());
Zachary Turner24bd3172016-11-17 01:37:52 +0000500 return ValueObjectSP();
501 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000502
Zachary Turner24bd3172016-11-17 01:37:52 +0000503 const bool check_ptr_vs_member =
504 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
505 const bool no_fragile_ivar =
506 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
507 const bool no_synth_child =
508 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
509 // const bool no_synth_array = (options &
510 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
511 error.Clear();
512 bool deref = false;
513 bool address_of = false;
514 ValueObjectSP valobj_sp;
515 const bool get_file_globals = true;
516 // When looking up a variable for an expression, we need only consider the
517 // variables that are in scope.
518 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
519 VariableList *variable_list = var_list_sp.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520
Zachary Turner24bd3172016-11-17 01:37:52 +0000521 if (!variable_list)
522 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523
Zachary Turner24bd3172016-11-17 01:37:52 +0000524 // If first character is a '*', then show pointer contents
Zachary Turner24bd3172016-11-17 01:37:52 +0000525 std::string var_expr_storage;
526 if (var_expr[0] == '*') {
527 deref = true;
528 var_expr = var_expr.drop_front(); // Skip the '*'
529 } else if (var_expr[0] == '&') {
530 address_of = true;
531 var_expr = var_expr.drop_front(); // Skip the '&'
532 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533
Zachary Turner24bd3172016-11-17 01:37:52 +0000534 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
535 StreamString var_expr_path_strm;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000536
Zachary Turner24bd3172016-11-17 01:37:52 +0000537 ConstString name_const_string(var_expr.substr(0, separator_idx));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538
Zachary Turner24bd3172016-11-17 01:37:52 +0000539 var_sp = variable_list->FindVariable(name_const_string, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540
Zachary Turner24bd3172016-11-17 01:37:52 +0000541 bool synthetically_added_instance_object = false;
542
543 if (var_sp) {
544 var_expr = var_expr.drop_front(name_const_string.GetLength());
545 }
546
547 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
548 // Check for direct ivars access which helps us with implicit
549 // access to ivars with the "this->" or "self->"
550 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
551 lldb::LanguageType method_language = eLanguageTypeUnknown;
552 bool is_instance_method = false;
553 ConstString method_object_name;
554 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
555 method_object_name)) {
556 if (is_instance_method && method_object_name) {
557 var_sp = variable_list->FindVariable(method_object_name);
558 if (var_sp) {
559 separator_idx = 0;
560 var_expr_storage = "->";
561 var_expr_storage += var_expr;
562 var_expr = var_expr_storage;
563 synthetically_added_instance_object = true;
Greg Clayton288bdf92010-09-02 02:59:18 +0000564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000566 }
567 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568
Zachary Turner24bd3172016-11-17 01:37:52 +0000569 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
570 // Check if any anonymous unions are there which contain a variable with
571 // the name we need
572 for (size_t i = 0; i < variable_list->GetSize(); i++) {
573 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
574 if (!variable_sp)
575 continue;
576 if (!variable_sp->GetName().IsEmpty())
577 continue;
578
579 Type *var_type = variable_sp->GetType();
580 if (!var_type)
581 continue;
582
583 if (!var_type->GetForwardCompilerType().IsAnonymousType())
584 continue;
585 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
586 if (!valobj_sp)
587 return valobj_sp;
588 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
589 if (valobj_sp)
590 break;
591 }
592 }
593
594 if (var_sp && !valobj_sp) {
595 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
596 if (!valobj_sp)
597 return valobj_sp;
598 }
599 if (!valobj_sp) {
600 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
601 name_const_string.GetCString());
602 return ValueObjectSP();
603 }
604
605 // We are dumping at least one child
606 while (separator_idx != std::string::npos) {
607 // Calculate the next separator index ahead of time
608 ValueObjectSP child_valobj_sp;
609 const char separator_type = var_expr[0];
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000610 bool expr_is_ptr = false;
Zachary Turner24bd3172016-11-17 01:37:52 +0000611 switch (separator_type) {
612 case '-':
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000613 expr_is_ptr = true;
Zachary Turner24bd3172016-11-17 01:37:52 +0000614 if (var_expr.size() >= 2 && var_expr[1] != '>')
615 return ValueObjectSP();
616
617 if (no_fragile_ivar) {
618 // Make sure we aren't trying to deref an objective
619 // C ivar if this is not allowed
620 const uint32_t pointer_type_flags =
621 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
622 if ((pointer_type_flags & eTypeIsObjC) &&
623 (pointer_type_flags & eTypeIsPointer)) {
624 // This was an objective C object pointer and
625 // it was requested we skip any fragile ivars
626 // so return nothing here
627 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 }
629 }
Tamas Berghammer4c08fe22017-03-31 20:23:22 +0000630
631 // If we have a non pointer type with a sythetic value then lets check if
632 // we have an sythetic dereference specified.
633 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
634 Error deref_error;
635 if (valobj_sp->GetCompilerType().IsReferenceType()) {
636 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
637 if (error.Fail()) {
638 error.SetErrorStringWithFormatv(
639 "Failed to dereference reference type: %s", deref_error);
640 return ValueObjectSP();
641 }
642 }
643
644 valobj_sp = valobj_sp->Dereference(deref_error);
645 if (error.Fail()) {
646 error.SetErrorStringWithFormatv(
647 "Failed to dereference sythetic value: %s", deref_error);
648 return ValueObjectSP();
649 }
650 expr_is_ptr = false;
651 }
652
Zachary Turner24bd3172016-11-17 01:37:52 +0000653 var_expr = var_expr.drop_front(); // Remove the '-'
654 LLVM_FALLTHROUGH;
655 case '.': {
Zachary Turner24bd3172016-11-17 01:37:52 +0000656 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
657 separator_idx = var_expr.find_first_of(".-[");
658 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
659
660 if (check_ptr_vs_member) {
661 // We either have a pointer type and need to verify
662 // valobj_sp is a pointer, or we have a member of a
663 // class/union/struct being accessed with the . syntax
664 // and need to verify we don't have a pointer.
665 const bool actual_is_ptr = valobj_sp->IsPointerType();
666
667 if (actual_is_ptr != expr_is_ptr) {
668 // Incorrect use of "." with a pointer, or "->" with
669 // a class/union/struct instance or reference.
670 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
671 if (actual_is_ptr)
672 error.SetErrorStringWithFormat(
673 "\"%s\" is a pointer and . was used to attempt to access "
674 "\"%s\". Did you mean \"%s->%s\"?",
675 var_expr_path_strm.GetData(), child_name.GetCString(),
676 var_expr_path_strm.GetData(), var_expr.str().c_str());
677 else
678 error.SetErrorStringWithFormat(
679 "\"%s\" is not a pointer and -> was used to attempt to "
680 "access \"%s\". Did you mean \"%s.%s\"?",
681 var_expr_path_strm.GetData(), child_name.GetCString(),
682 var_expr_path_strm.GetData(), var_expr.str().c_str());
683 return ValueObjectSP();
684 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000685 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000686 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
687 if (!child_valobj_sp) {
688 if (!no_synth_child) {
689 child_valobj_sp = valobj_sp->GetSyntheticValue();
690 if (child_valobj_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 child_valobj_sp =
Zachary Turner24bd3172016-11-17 01:37:52 +0000692 child_valobj_sp->GetChildMemberWithName(child_name, true);
693 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694
Zachary Turner24bd3172016-11-17 01:37:52 +0000695 if (no_synth_child || !child_valobj_sp) {
696 // No child member with name "child_name"
697 if (synthetically_added_instance_object) {
698 // We added a "this->" or "self->" to the beginning of the
699 // expression
700 // and this is the first pointer ivar access, so just return
701 // the normal
702 // error
703 error.SetErrorStringWithFormat(
704 "no variable or instance variable named '%s' found in "
705 "this frame",
706 name_const_string.GetCString());
707 } else {
708 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
709 if (child_name) {
710 error.SetErrorStringWithFormat(
711 "\"%s\" is not a member of \"(%s) %s\"",
712 child_name.GetCString(),
713 valobj_sp->GetTypeName().AsCString("<invalid type>"),
714 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715 } else {
716 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000717 "incomplete expression path after \"%s\" in \"%s\"",
Zachary Turner0eb31a12016-11-17 05:14:32 +0000718 var_expr_path_strm.GetData(),
719 original_var_expr.str().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000721 }
722 return ValueObjectSP();
723 }
724 }
725 synthetically_added_instance_object = false;
726 // Remove the child name from the path
727 var_expr = var_expr.drop_front(child_name.GetLength());
728 if (use_dynamic != eNoDynamicValues) {
729 ValueObjectSP dynamic_value_sp(
730 child_valobj_sp->GetDynamicValue(use_dynamic));
731 if (dynamic_value_sp)
732 child_valobj_sp = dynamic_value_sp;
733 }
734 } break;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735
Zachary Turner24bd3172016-11-17 01:37:52 +0000736 case '[': {
737 // Array member access, or treating pointer as an array
738 // Need at least two brackets and a number
739 if (var_expr.size() <= 2) {
740 error.SetErrorStringWithFormat(
741 "invalid square bracket encountered after \"%s\" in \"%s\"",
742 var_expr_path_strm.GetData(), var_expr.str().c_str());
743 return ValueObjectSP();
744 }
745
746 // Drop the open brace.
747 var_expr = var_expr.drop_front();
748 long child_index = 0;
749
750 // If there's no closing brace, this is an invalid expression.
751 size_t end_pos = var_expr.find_first_of(']');
752 if (end_pos == llvm::StringRef::npos) {
753 error.SetErrorStringWithFormat(
754 "missing closing square bracket in expression \"%s\"",
755 var_expr_path_strm.GetData());
756 return ValueObjectSP();
757 }
758 llvm::StringRef index_expr = var_expr.take_front(end_pos);
759 llvm::StringRef original_index_expr = index_expr;
760 // Drop all of "[index_expr]"
761 var_expr = var_expr.drop_front(end_pos + 1);
762
763 if (index_expr.consumeInteger(0, child_index)) {
764 // If there was no integer anywhere in the index expression, this is
765 // erroneous expression.
766 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
767 index_expr.str().c_str());
768 return ValueObjectSP();
769 }
770
771 if (index_expr.empty()) {
772 // The entire index expression was a single integer.
773
774 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
775 // what we have is *ptr[low]. the most similar C++ syntax is to deref
776 // ptr and extract bit low out of it. reading array item low would be
777 // done by saying ptr[low], without a deref * sign
778 Error error;
779 ValueObjectSP temp(valobj_sp->Dereference(error));
780 if (error.Fail()) {
781 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
782 error.SetErrorStringWithFormat(
783 "could not dereference \"(%s) %s\"",
784 valobj_sp->GetTypeName().AsCString("<invalid type>"),
785 var_expr_path_strm.GetData());
786 return ValueObjectSP();
787 }
788 valobj_sp = temp;
789 deref = false;
790 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
791 deref) {
792 // what we have is *arr[low]. the most similar C++ syntax is
793 // to get arr[0]
794 // (an operation that is equivalent to deref-ing arr)
795 // and extract bit low out of it. reading array item low
796 // would be done by saying arr[low], without a deref * sign
797 Error error;
798 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
799 if (error.Fail()) {
800 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
801 error.SetErrorStringWithFormat(
802 "could not get item 0 for \"(%s) %s\"",
803 valobj_sp->GetTypeName().AsCString("<invalid type>"),
804 var_expr_path_strm.GetData());
805 return ValueObjectSP();
806 }
807 valobj_sp = temp;
808 deref = false;
809 }
810
811 bool is_incomplete_array = false;
812 if (valobj_sp->IsPointerType()) {
813 bool is_objc_pointer = true;
814
815 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
816 eLanguageTypeObjC)
817 is_objc_pointer = false;
818 else if (!valobj_sp->GetCompilerType().IsPointerType())
819 is_objc_pointer = false;
820
821 if (no_synth_child && is_objc_pointer) {
822 error.SetErrorStringWithFormat(
823 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
824 "subscripted",
825 valobj_sp->GetTypeName().AsCString("<invalid type>"),
826 var_expr_path_strm.GetData());
827
828 return ValueObjectSP();
829 } else if (is_objc_pointer) {
830 // dereferencing ObjC variables is not valid.. so let's try
831 // and recur to synthetic children
832 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
833 if (!synthetic /* no synthetic */
834 || synthetic == valobj_sp) /* synthetic is the same as
835 the original object */
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 {
837 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
838 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +0000839 "\"(%s) %s\" is not an array type",
840 valobj_sp->GetTypeName().AsCString("<invalid type>"),
841 var_expr_path_strm.GetData());
842 } else if (
843 static_cast<uint32_t>(child_index) >=
844 synthetic
845 ->GetNumChildren() /* synthetic does not have that many values */) {
846 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
847 error.SetErrorStringWithFormat(
848 "array index %ld is not valid for \"(%s) %s\"", child_index,
849 valobj_sp->GetTypeName().AsCString("<invalid type>"),
850 var_expr_path_strm.GetData());
851 } else {
852 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
853 if (!child_valobj_sp) {
854 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
855 error.SetErrorStringWithFormat(
856 "array index %ld is not valid for \"(%s) %s\"", child_index,
857 valobj_sp->GetTypeName().AsCString("<invalid type>"),
858 var_expr_path_strm.GetData());
859 }
860 }
861 } else {
862 child_valobj_sp =
863 valobj_sp->GetSyntheticArrayMember(child_index, true);
864 if (!child_valobj_sp) {
865 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
866 error.SetErrorStringWithFormat(
867 "failed to use pointer as array for index %ld for "
868 "\"(%s) %s\"",
869 child_index,
870 valobj_sp->GetTypeName().AsCString("<invalid type>"),
871 var_expr_path_strm.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872 }
873 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000874 } else if (valobj_sp->GetCompilerType().IsArrayType(
875 nullptr, nullptr, &is_incomplete_array)) {
876 // Pass false to dynamic_value here so we can tell the
877 // difference between
878 // no dynamic value and no member of this type...
879 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
880 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
881 child_valobj_sp =
882 valobj_sp->GetSyntheticArrayMember(child_index, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000883
Zachary Turner24bd3172016-11-17 01:37:52 +0000884 if (!child_valobj_sp) {
885 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
886 error.SetErrorStringWithFormat(
887 "array index %ld is not valid for \"(%s) %s\"", child_index,
888 valobj_sp->GetTypeName().AsCString("<invalid type>"),
889 var_expr_path_strm.GetData());
890 }
891 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
892 // this is a bitfield asking to display just one bit
893 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
894 child_index, child_index, true);
895 if (!child_valobj_sp) {
896 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
897 error.SetErrorStringWithFormat(
898 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
899 child_index, child_index,
900 valobj_sp->GetTypeName().AsCString("<invalid type>"),
901 var_expr_path_strm.GetData());
902 }
903 } else {
904 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
905 if (no_synth_child /* synthetic is forbidden */ ||
906 !synthetic /* no synthetic */
907 || synthetic == valobj_sp) /* synthetic is the same as the
908 original object */
909 {
910 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
911 error.SetErrorStringWithFormat(
912 "\"(%s) %s\" is not an array type",
913 valobj_sp->GetTypeName().AsCString("<invalid type>"),
914 var_expr_path_strm.GetData());
915 } else if (
916 static_cast<uint32_t>(child_index) >=
917 synthetic
918 ->GetNumChildren() /* synthetic does not have that many values */) {
919 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
920 error.SetErrorStringWithFormat(
921 "array index %ld is not valid for \"(%s) %s\"", child_index,
922 valobj_sp->GetTypeName().AsCString("<invalid type>"),
923 var_expr_path_strm.GetData());
924 } else {
925 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
926 if (!child_valobj_sp) {
927 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
928 error.SetErrorStringWithFormat(
929 "array index %ld is not valid for \"(%s) %s\"", child_index,
930 valobj_sp->GetTypeName().AsCString("<invalid type>"),
931 var_expr_path_strm.GetData());
932 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000933 }
934 }
Zachary Turner24bd3172016-11-17 01:37:52 +0000935
936 if (!child_valobj_sp) {
937 // Invalid array index...
938 return ValueObjectSP();
939 }
940
941 separator_idx = var_expr.find_first_of(".-[");
942 if (use_dynamic != eNoDynamicValues) {
943 ValueObjectSP dynamic_value_sp(
944 child_valobj_sp->GetDynamicValue(use_dynamic));
945 if (dynamic_value_sp)
946 child_valobj_sp = dynamic_value_sp;
947 }
948 // Break out early from the switch since we were able to find the child
949 // member
950 break;
951 }
952
953 // this is most probably a BitField, let's take a look
954 if (index_expr.front() != '-') {
955 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
956 original_index_expr.str().c_str());
957 return ValueObjectSP();
958 }
959
Zachary Turner7edc3a62016-11-21 23:18:13 +0000960 index_expr = index_expr.drop_front();
Zachary Turner24bd3172016-11-17 01:37:52 +0000961 long final_index = 0;
962 if (index_expr.getAsInteger(0, final_index)) {
963 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
964 original_index_expr.str().c_str());
965 return ValueObjectSP();
966 }
967
968 // if the format given is [high-low], swap range
969 if (child_index > final_index) {
970 long temp = child_index;
971 child_index = final_index;
972 final_index = temp;
973 }
974
975 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
976 // what we have is *ptr[low-high]. the most similar C++ syntax is to
977 // deref ptr and extract bits low thru high out of it. reading array
978 // items low thru high would be done by saying ptr[low-high], without
979 // a deref * sign
980 Error error;
981 ValueObjectSP temp(valobj_sp->Dereference(error));
982 if (error.Fail()) {
983 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
984 error.SetErrorStringWithFormat(
985 "could not dereference \"(%s) %s\"",
986 valobj_sp->GetTypeName().AsCString("<invalid type>"),
987 var_expr_path_strm.GetData());
988 return ValueObjectSP();
989 }
990 valobj_sp = temp;
991 deref = false;
992 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
993 // what we have is *arr[low-high]. the most similar C++ syntax is to get
994 // arr[0] (an operation that is equivalent to deref-ing arr) and extract
995 // bits low thru high out of it. reading array items low thru high would
996 // be done by saying arr[low-high], without a deref * sign
997 Error error;
998 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
999 if (error.Fail()) {
1000 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1001 error.SetErrorStringWithFormat(
1002 "could not get item 0 for \"(%s) %s\"",
1003 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1004 var_expr_path_strm.GetData());
1005 return ValueObjectSP();
1006 }
1007 valobj_sp = temp;
1008 deref = false;
1009 }
1010
1011 child_valobj_sp =
1012 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1013 if (!child_valobj_sp) {
1014 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 error.SetErrorStringWithFormat(
Zachary Turner24bd3172016-11-17 01:37:52 +00001016 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1017 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1018 var_expr_path_strm.GetData());
1019 }
1020
1021 if (!child_valobj_sp) {
1022 // Invalid bitfield range...
1023 return ValueObjectSP();
1024 }
1025
1026 separator_idx = var_expr.find_first_of(".-[");
1027 if (use_dynamic != eNoDynamicValues) {
1028 ValueObjectSP dynamic_value_sp(
1029 child_valobj_sp->GetDynamicValue(use_dynamic));
1030 if (dynamic_value_sp)
1031 child_valobj_sp = dynamic_value_sp;
1032 }
1033 // Break out early from the switch since we were able to find the child
1034 // member
1035 break;
1036 }
1037 default:
1038 // Failure...
1039 {
1040 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1041 error.SetErrorStringWithFormat(
1042 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1043 separator_type, var_expr_path_strm.GetData(),
1044 var_expr.str().c_str());
1045
1046 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047 }
Greg Clayton288bdf92010-09-02 02:59:18 +00001048 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001049
1050 if (child_valobj_sp)
1051 valobj_sp = child_valobj_sp;
1052
1053 if (var_expr.empty())
1054 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001055 }
Zachary Turner24bd3172016-11-17 01:37:52 +00001056 if (valobj_sp) {
1057 if (deref) {
1058 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1059 valobj_sp = deref_valobj_sp;
1060 } else if (address_of) {
1061 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1062 valobj_sp = address_of_valobj_sp;
1063 }
1064 }
1065 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001066}
1067
1068bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Error *error_ptr) {
1069 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1070 if (!m_cfa_is_valid) {
1071 m_frame_base_error.SetErrorString(
1072 "No frame base available for this historical stack frame.");
1073 return false;
1074 }
1075
1076 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1077 if (m_sc.function) {
1078 m_frame_base.Clear();
1079 m_frame_base_error.Clear();
1080
1081 m_flags.Set(GOT_FRAME_BASE);
1082 ExecutionContext exe_ctx(shared_from_this());
1083 Value expr_value;
1084 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1085 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1086 loclist_base_addr =
1087 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1088 exe_ctx.GetTargetPtr());
1089
1090 if (m_sc.function->GetFrameBaseExpression().Evaluate(
1091 &exe_ctx, nullptr, nullptr, nullptr, loclist_base_addr, nullptr,
1092 nullptr, expr_value, &m_frame_base_error) == false) {
1093 // We should really have an error if evaluate returns, but in case
1094 // we don't, lets set the error to something at least.
1095 if (m_frame_base_error.Success())
1096 m_frame_base_error.SetErrorString(
1097 "Evaluation of the frame base expression failed.");
1098 } else {
1099 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1100 }
1101 } else {
1102 m_frame_base_error.SetErrorString("No function in symbol context.");
Jim Ingham78a685a2011-04-16 00:01:13 +00001103 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001104 }
1105
1106 if (m_frame_base_error.Success())
1107 frame_base = m_frame_base;
1108
1109 if (error_ptr)
1110 *error_ptr = m_frame_base_error;
1111 return m_frame_base_error.Success();
1112}
1113
1114DWARFExpression *StackFrame::GetFrameBaseExpression(Error *error_ptr) {
1115 if (!m_sc.function) {
1116 if (error_ptr) {
1117 error_ptr->SetErrorString("No function in symbol context.");
1118 }
1119 return nullptr;
1120 }
1121
1122 return &m_sc.function->GetFrameBaseExpression();
1123}
1124
1125RegisterContextSP StackFrame::GetRegisterContext() {
1126 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1127 if (!m_reg_context_sp) {
1128 ThreadSP thread_sp(GetThread());
1129 if (thread_sp)
1130 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1131 }
1132 return m_reg_context_sp;
1133}
1134
1135bool StackFrame::HasDebugInformation() {
1136 GetSymbolContext(eSymbolContextLineEntry);
1137 return m_sc.line_entry.IsValid();
Greg Clayton288bdf92010-09-02 02:59:18 +00001138}
1139
1140ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00001141StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1142 DynamicValueType use_dynamic) {
1143 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1144 ValueObjectSP valobj_sp;
1145 if (m_is_history_frame) {
Greg Clayton288bdf92010-09-02 02:59:18 +00001146 return valobj_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001147 }
1148 VariableList *var_list = GetVariableList(true);
1149 if (var_list) {
1150 // Make sure the variable is a frame variable
1151 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1152 const uint32_t num_variables = var_list->GetSize();
1153 if (var_idx < num_variables) {
1154 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1155 if (!valobj_sp) {
1156 if (m_variable_list_value_objects.GetSize() < num_variables)
1157 m_variable_list_value_objects.Resize(num_variables);
1158 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1159 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1160 }
Enrico Granata592afe72016-03-15 21:50:51 +00001161 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001162 }
1163 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1164 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1165 if (dynamic_sp)
1166 return dynamic_sp;
1167 }
1168 return valobj_sp;
Enrico Granata592afe72016-03-15 21:50:51 +00001169}
1170
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1172 DynamicValueType use_dynamic) {
1173 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1174 if (m_is_history_frame)
1175 return ValueObjectSP();
1176
1177 // Check to make sure we aren't already tracking this variable?
1178 ValueObjectSP valobj_sp(
1179 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1180 if (!valobj_sp) {
1181 // We aren't already tracking this global
1182 VariableList *var_list = GetVariableList(true);
1183 // If this frame has no variables, create a new list
1184 if (var_list == nullptr)
1185 m_variable_list_sp.reset(new VariableList());
1186
1187 // Add the global/static variable to this frame
1188 m_variable_list_sp->AddVariable(variable_sp);
1189
1190 // Now make a value object for it so we can track its changes
1191 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1192 }
1193 return valobj_sp;
1194}
1195
1196bool StackFrame::IsInlined() {
1197 if (m_sc.block == nullptr)
1198 GetSymbolContext(eSymbolContextBlock);
1199 if (m_sc.block)
1200 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1201 return false;
1202}
1203
1204lldb::LanguageType StackFrame::GetLanguage() {
1205 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1206 if (cu)
1207 return cu->GetLanguage();
1208 return lldb::eLanguageTypeUnknown;
1209}
1210
1211lldb::LanguageType StackFrame::GuessLanguage() {
1212 LanguageType lang_type = GetLanguage();
1213
1214 if (lang_type == eLanguageTypeUnknown) {
1215 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1216 if (f) {
1217 lang_type = f->GetMangled().GuessLanguage();
Sean Callanan4740a732016-09-06 04:48:36 +00001218 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001219 }
1220
1221 return lang_type;
1222}
1223
1224namespace {
1225std::pair<const Instruction::Operand *, int64_t>
1226GetBaseExplainingValue(const Instruction::Operand &operand,
1227 RegisterContext &register_context, lldb::addr_t value) {
1228 switch (operand.m_type) {
1229 case Instruction::Operand::Type::Dereference:
1230 case Instruction::Operand::Type::Immediate:
1231 case Instruction::Operand::Type::Invalid:
1232 case Instruction::Operand::Type::Product:
1233 // These are not currently interesting
1234 return std::make_pair(nullptr, 0);
1235 case Instruction::Operand::Type::Sum: {
1236 const Instruction::Operand *immediate_child = nullptr;
1237 const Instruction::Operand *variable_child = nullptr;
1238 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1239 immediate_child = &operand.m_children[0];
1240 variable_child = &operand.m_children[1];
1241 } else if (operand.m_children[1].m_type ==
1242 Instruction::Operand::Type::Immediate) {
1243 immediate_child = &operand.m_children[1];
1244 variable_child = &operand.m_children[0];
Sean Callanan4740a732016-09-06 04:48:36 +00001245 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246 if (!immediate_child) {
1247 return std::make_pair(nullptr, 0);
1248 }
1249 lldb::addr_t adjusted_value = value;
1250 if (immediate_child->m_negative) {
1251 adjusted_value += immediate_child->m_immediate;
1252 } else {
1253 adjusted_value -= immediate_child->m_immediate;
1254 }
1255 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1256 GetBaseExplainingValue(*variable_child, register_context,
1257 adjusted_value);
1258 if (!base_and_offset.first) {
1259 return std::make_pair(nullptr, 0);
1260 }
1261 if (immediate_child->m_negative) {
1262 base_and_offset.second -= immediate_child->m_immediate;
1263 } else {
1264 base_and_offset.second += immediate_child->m_immediate;
1265 }
1266 return base_and_offset;
1267 }
1268 case Instruction::Operand::Type::Register: {
1269 const RegisterInfo *info =
1270 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1271 if (!info) {
1272 return std::make_pair(nullptr, 0);
1273 }
1274 RegisterValue reg_value;
1275 if (!register_context.ReadRegister(info, reg_value)) {
1276 return std::make_pair(nullptr, 0);
1277 }
1278 if (reg_value.GetAsUInt64() == value) {
1279 return std::make_pair(&operand, 0);
1280 } else {
1281 return std::make_pair(nullptr, 0);
1282 }
1283 }
1284 }
Zachary Turner5a8ad4592016-10-05 17:07:34 +00001285 return std::make_pair(nullptr, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286}
1287
1288std::pair<const Instruction::Operand *, int64_t>
1289GetBaseExplainingDereference(const Instruction::Operand &operand,
1290 RegisterContext &register_context,
1291 lldb::addr_t addr) {
1292 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1293 return GetBaseExplainingValue(operand.m_children[0], register_context,
1294 addr);
1295 }
1296 return std::make_pair(nullptr, 0);
1297}
Ilia K4f730dc2016-09-12 05:25:33 +00001298}
Sean Callanan4740a732016-09-06 04:48:36 +00001299
Kate Stoneb9c1b512016-09-06 20:57:50 +00001300lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1301 TargetSP target_sp = CalculateTarget();
Sean Callanan4740a732016-09-06 04:48:36 +00001302
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 const ArchSpec &target_arch = target_sp->GetArchitecture();
1304
1305 AddressRange pc_range;
1306 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1307 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1308
1309 ExecutionContext exe_ctx(shared_from_this());
1310
1311 const char *plugin_name = nullptr;
1312 const char *flavor = nullptr;
1313 const bool prefer_file_cache = false;
1314
1315 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1316 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1317
Jim Ingham99d1e282017-03-31 22:39:55 +00001318 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
Sean Callanan4740a732016-09-06 04:48:36 +00001319 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001320 }
Sean Callanan4740a732016-09-06 04:48:36 +00001321
Kate Stoneb9c1b512016-09-06 20:57:50 +00001322 InstructionSP instruction_sp =
1323 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1324
1325 llvm::SmallVector<Instruction::Operand, 3> operands;
1326
1327 if (!instruction_sp->ParseOperands(operands)) {
1328 return ValueObjectSP();
1329 }
1330
1331 RegisterContextSP register_context_sp = GetRegisterContext();
1332
1333 if (!register_context_sp) {
1334 return ValueObjectSP();
1335 }
1336
1337 for (const Instruction::Operand &operand : operands) {
1338 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1339 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1340
1341 if (!base_and_offset.first) {
1342 continue;
Sean Callanan4740a732016-09-06 04:48:36 +00001343 }
Sean Callanan4740a732016-09-06 04:48:36 +00001344
Kate Stoneb9c1b512016-09-06 20:57:50 +00001345 switch (base_and_offset.first->m_type) {
1346 case Instruction::Operand::Type::Immediate: {
1347 lldb_private::Address addr;
1348 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1349 base_and_offset.second,
1350 addr)) {
1351 TypeSystem *c_type_system =
1352 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1353 if (!c_type_system) {
1354 return ValueObjectSP();
1355 } else {
1356 CompilerType void_ptr_type =
1357 c_type_system
1358 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1359 .GetPointerType();
1360 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
Sean Callanan4740a732016-09-06 04:48:36 +00001361 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 } else {
Sean Callanan4740a732016-09-06 04:48:36 +00001363 return ValueObjectSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001364 }
1365 break;
Sean Callanan4740a732016-09-06 04:48:36 +00001366 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 case Instruction::Operand::Type::Register: {
1368 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1369 base_and_offset.second);
1370 }
1371 default:
1372 return ValueObjectSP();
1373 }
1374 }
1375
1376 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001377}
1378
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379namespace {
1380ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1381 int64_t offset) {
1382 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1383 return ValueObjectSP();
1384 }
Sean Callanan4740a732016-09-06 04:48:36 +00001385
Kate Stoneb9c1b512016-09-06 20:57:50 +00001386 if (parent->IsPointerOrReferenceType()) {
1387 return parent;
1388 }
1389
1390 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1391 const bool can_create = true;
1392 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1393
1394 if (!child_sp) {
1395 return ValueObjectSP();
Sean Callanan4740a732016-09-06 04:48:36 +00001396 }
1397
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 int64_t child_offset = child_sp->GetByteOffset();
1399 int64_t child_size = child_sp->GetByteSize();
1400
1401 if (offset >= child_offset && offset < (child_offset + child_size)) {
1402 return GetValueForOffset(frame, child_sp, offset - child_offset);
Sean Callanan4740a732016-09-06 04:48:36 +00001403 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001404 }
Sean Callanan4740a732016-09-06 04:48:36 +00001405
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406 if (offset == 0) {
1407 return parent;
1408 } else {
1409 return ValueObjectSP();
1410 }
1411}
1412
1413ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1414 ValueObjectSP &base,
1415 int64_t offset) {
1416 // base is a pointer to something
1417 // offset is the thing to add to the pointer
1418 // We return the most sensible ValueObject for the result of *(base+offset)
1419
1420 if (!base->IsPointerOrReferenceType()) {
1421 return ValueObjectSP();
1422 }
1423
1424 Error error;
1425 ValueObjectSP pointee = base->Dereference(error);
Sean Callanana0a1d2d2016-09-29 00:16:37 +00001426
1427 if (!pointee) {
1428 return ValueObjectSP();
1429 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430
Ilia K4f730dc2016-09-12 05:25:33 +00001431 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 int64_t index = offset / pointee->GetByteSize();
1433 offset = offset % pointee->GetByteSize();
1434 const bool can_create = true;
1435 pointee = base->GetSyntheticArrayMember(index, can_create);
1436 }
1437
1438 if (!pointee || error.Fail()) {
1439 return ValueObjectSP();
1440 }
1441
1442 return GetValueForOffset(frame, pointee, offset);
1443}
1444
1445//------------------------------------------------------------------
1446/// Attempt to reconstruct the ValueObject for the address contained in a
1447/// given register plus an offset.
1448///
1449/// @params [in] frame
1450/// The current stack frame.
1451///
1452/// @params [in] reg
1453/// The register.
1454///
1455/// @params [in] offset
1456/// The offset from the register.
1457///
1458/// @param [in] disassembler
1459/// A disassembler containing instructions valid up to the current PC.
1460///
1461/// @param [in] variables
1462/// The variable list from the current frame,
1463///
1464/// @param [in] pc
1465/// The program counter for the instruction considered the 'user'.
1466///
1467/// @return
1468/// A string describing the base for the ExpressionPath. This could be a
1469/// variable, a register value, an argument, or a function return value.
1470/// The ValueObject if found. If valid, it has a valid ExpressionPath.
1471//------------------------------------------------------------------
1472lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1473 int64_t offset, Disassembler &disassembler,
1474 VariableList &variables, const Address &pc) {
1475 // Example of operation for Intel:
1476 //
1477 // +14: movq -0x8(%rbp), %rdi
1478 // +18: movq 0x8(%rdi), %rdi
1479 // +22: addl 0x4(%rdi), %eax
1480 //
1481 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1482 //
1483 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18
1484 // that assigns to rdi, and calls itself recursively for that dereference
1485 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1486 // +14 that assigns to rdi, and calls itself recursively for that
1487 // derefernece
1488 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1489 // variable list.
1490 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1491 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1492 // at +18)
1493 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1494 // rdi+4 at +22)
1495
1496 // First, check the variable list to see if anything is at the specified
1497 // location.
Sean Callanan807ee2f2016-09-13 21:18:27 +00001498
Sean Callanan50857102016-09-14 00:48:19 +00001499 using namespace OperandMatchers;
1500
Sean Callanan0ac172d2016-09-14 20:29:57 +00001501 const RegisterInfo *reg_info =
1502 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1503 if (!reg_info) {
1504 return ValueObjectSP();
1505 }
1506
Sean Callanan807ee2f2016-09-13 21:18:27 +00001507 Instruction::Operand op =
1508 offset ? Instruction::Operand::BuildDereference(
1509 Instruction::Operand::BuildSum(
1510 Instruction::Operand::BuildRegister(reg),
1511 Instruction::Operand::BuildImmediate(offset)))
1512 : Instruction::Operand::BuildDereference(
1513 Instruction::Operand::BuildRegister(reg));
1514
Kate Stoneb9c1b512016-09-06 20:57:50 +00001515 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1516 VariableSP var_sp = variables.GetVariableAtIndex(vi);
Sean Callanan807ee2f2016-09-13 21:18:27 +00001517 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1518 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
Sean Callanan4740a732016-09-06 04:48:36 +00001519 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 }
1521
Kate Stoneb9c1b512016-09-06 20:57:50 +00001522 const uint32_t current_inst =
1523 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1524 if (current_inst == UINT32_MAX) {
1525 return ValueObjectSP();
1526 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001527
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1529 // This is not an exact algorithm, and it sacrifices accuracy for
Sean Callanan0ac172d2016-09-14 20:29:57 +00001530 // generality. Recognizing "mov" and "ld" instructions –– and which are
1531 // their source and destination operands -- is something the disassembler
1532 // should do for us.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001533 InstructionSP instruction_sp =
1534 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535
Sean Callanan50857102016-09-14 00:48:19 +00001536 if (instruction_sp->IsCall()) {
1537 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1538 if (!abi_sp) {
1539 continue;
1540 }
1541
1542 const char *return_register_name;
1543 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1544 continue;
1545 }
1546
1547 const RegisterInfo *return_register_info =
1548 frame.GetRegisterContext()->GetRegisterInfoByName(
1549 return_register_name);
1550 if (!return_register_info) {
1551 continue;
1552 }
1553
1554 int64_t offset = 0;
1555
1556 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1557 MatchRegOp(*return_register_info))(op) &&
1558 !MatchUnaryOp(
1559 MatchOpType(Instruction::Operand::Type::Dereference),
1560 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1561 MatchRegOp(*return_register_info),
1562 FetchImmOp(offset)))(op)) {
1563 continue;
1564 }
1565
Kate Stoneb9c1b512016-09-06 20:57:50 +00001566 llvm::SmallVector<Instruction::Operand, 1> operands;
1567 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1568 continue;
1569 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570
Kate Stoneb9c1b512016-09-06 20:57:50 +00001571 switch (operands[0].m_type) {
1572 default:
1573 break;
1574 case Instruction::Operand::Type::Immediate: {
1575 SymbolContext sc;
1576 Address load_address;
1577 if (!frame.CalculateTarget()->ResolveLoadAddress(
1578 operands[0].m_immediate, load_address)) {
1579 break;
Greg Clayton7260f622011-04-18 08:33:37 +00001580 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1582 load_address, eSymbolContextFunction, sc);
1583 if (!sc.function) {
1584 break;
1585 }
1586 CompilerType function_type = sc.function->GetCompilerType();
1587 if (!function_type.IsFunctionType()) {
1588 break;
1589 }
1590 CompilerType return_type = function_type.GetFunctionReturnType();
1591 RegisterValue return_value;
Sean Callanan50857102016-09-14 00:48:19 +00001592 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001593 return_value)) {
1594 break;
1595 }
1596 std::string name_str(
1597 sc.function->GetName().AsCString("<unknown function>"));
1598 name_str.append("()");
1599 Address return_value_address(return_value.GetAsUInt64());
1600 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
Zachary Turner22a26282016-11-12 18:17:36 +00001601 &frame, name_str, return_value_address, return_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001602 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1603 }
1604 }
1605
1606 continue;
Greg Clayton7260f622011-04-18 08:33:37 +00001607 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001608
1609 llvm::SmallVector<Instruction::Operand, 2> operands;
1610 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1611 continue;
1612 }
1613
Kate Stoneb9c1b512016-09-06 20:57:50 +00001614 Instruction::Operand *origin_operand = nullptr;
Sean Callananaa4b44c2016-09-14 20:58:31 +00001615 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1616 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1617 };
Sean Callanan0ac172d2016-09-14 20:29:57 +00001618
1619 if (clobbered_reg_matcher(operands[0])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001620 origin_operand = &operands[1];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001621 }
1622 else if (clobbered_reg_matcher(operands[1])) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001623 origin_operand = &operands[0];
Sean Callanan0ac172d2016-09-14 20:29:57 +00001624 }
1625 else {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626 continue;
1627 }
1628
1629 // We have an origin operand. Can we track its value down?
Sean Callanan561a9bb2016-09-14 21:54:28 +00001630 ValueObjectSP source_path;
1631 ConstString origin_register;
1632 int64_t origin_offset = 0;
1633
1634 if (FetchRegOp(origin_register)(*origin_operand)) {
1635 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1636 variables, instruction_sp->GetAddress());
1637 } else if (MatchUnaryOp(
1638 MatchOpType(Instruction::Operand::Type::Dereference),
1639 FetchRegOp(origin_register))(*origin_operand) ||
1640 MatchUnaryOp(
1641 MatchOpType(Instruction::Operand::Type::Dereference),
1642 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1643 FetchRegOp(origin_register),
1644 FetchImmOp(origin_offset)))(*origin_operand)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645 source_path =
Sean Callanan561a9bb2016-09-14 21:54:28 +00001646 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001647 variables, instruction_sp->GetAddress());
Sean Callanan561a9bb2016-09-14 21:54:28 +00001648 if (!source_path) {
1649 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001650 }
Sean Callanan561a9bb2016-09-14 21:54:28 +00001651 source_path =
1652 GetValueForDereferincingOffset(frame, source_path, offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653 }
1654
1655 if (source_path) {
1656 return source_path;
1657 }
1658 }
1659
1660 return ValueObjectSP();
1661}
1662}
1663
1664lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1665 int64_t offset) {
1666 TargetSP target_sp = CalculateTarget();
1667
1668 const ArchSpec &target_arch = target_sp->GetArchitecture();
1669
1670 Block *frame_block = GetFrameBlock();
1671
1672 if (!frame_block) {
1673 return ValueObjectSP();
1674 }
1675
1676 Function *function = frame_block->CalculateSymbolContextFunction();
1677 if (!function) {
1678 return ValueObjectSP();
1679 }
1680
1681 AddressRange pc_range = function->GetAddressRange();
1682
1683 if (GetFrameCodeAddress().GetFileAddress() <
1684 pc_range.GetBaseAddress().GetFileAddress() ||
1685 GetFrameCodeAddress().GetFileAddress() -
1686 pc_range.GetBaseAddress().GetFileAddress() >=
1687 pc_range.GetByteSize()) {
1688 return ValueObjectSP();
1689 }
1690
1691 ExecutionContext exe_ctx(shared_from_this());
1692
1693 const char *plugin_name = nullptr;
1694 const char *flavor = nullptr;
1695 const bool prefer_file_cache = false;
1696 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1697 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1698
1699 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1700 return ValueObjectSP();
1701 }
1702
1703 const bool get_file_globals = false;
1704 VariableList *variables = GetVariableList(get_file_globals);
1705
1706 if (!variables) {
1707 return ValueObjectSP();
1708 }
1709
1710 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1711 GetFrameCodeAddress());
1712}
1713
1714TargetSP StackFrame::CalculateTarget() {
1715 TargetSP target_sp;
1716 ThreadSP thread_sp(GetThread());
1717 if (thread_sp) {
1718 ProcessSP process_sp(thread_sp->CalculateProcess());
1719 if (process_sp)
1720 target_sp = process_sp->CalculateTarget();
1721 }
1722 return target_sp;
1723}
1724
1725ProcessSP StackFrame::CalculateProcess() {
1726 ProcessSP process_sp;
1727 ThreadSP thread_sp(GetThread());
1728 if (thread_sp)
1729 process_sp = thread_sp->CalculateProcess();
1730 return process_sp;
1731}
1732
1733ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1734
1735StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1736
1737void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1738 exe_ctx.SetContext(shared_from_this());
1739}
1740
1741void StackFrame::DumpUsingSettingsFormat(Stream *strm,
1742 const char *frame_marker) {
1743 if (strm == nullptr)
1744 return;
1745
1746 GetSymbolContext(eSymbolContextEverything);
1747 ExecutionContext exe_ctx(shared_from_this());
1748 StreamString s;
1749
1750 if (frame_marker)
1751 s.PutCString(frame_marker);
1752
1753 const FormatEntity::Entry *frame_format = nullptr;
1754 Target *target = exe_ctx.GetTargetPtr();
1755 if (target)
1756 frame_format = target->GetDebugger().GetFrameFormat();
1757 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1758 nullptr, nullptr, false, false)) {
Zachary Turnerc1564272016-11-16 21:15:24 +00001759 strm->PutCString(s.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001760 } else {
1761 Dump(strm, true, false);
1762 strm->EOL();
1763 }
1764}
1765
1766void StackFrame::Dump(Stream *strm, bool show_frame_index,
1767 bool show_fullpaths) {
1768 if (strm == nullptr)
1769 return;
1770
1771 if (show_frame_index)
1772 strm->Printf("frame #%u: ", m_frame_index);
1773 ExecutionContext exe_ctx(shared_from_this());
1774 Target *target = exe_ctx.GetTargetPtr();
1775 strm->Printf("0x%0*" PRIx64 " ",
1776 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1777 : 16,
1778 GetFrameCodeAddress().GetLoadAddress(target));
1779 GetSymbolContext(eSymbolContextEverything);
1780 const bool show_module = true;
1781 const bool show_inline = true;
1782 const bool show_function_arguments = true;
1783 const bool show_function_name = true;
1784 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1785 GetFrameCodeAddress(), show_fullpaths, show_module,
1786 show_inline, show_function_arguments,
1787 show_function_name);
1788}
1789
1790void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1791 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1792 assert(GetStackID() ==
1793 prev_frame.GetStackID()); // TODO: remove this after some testing
1794 m_variable_list_sp = prev_frame.m_variable_list_sp;
1795 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
Zachary Turnerc1564272016-11-16 21:15:24 +00001796 if (!m_disassembly.GetString().empty()) {
1797 m_disassembly.Clear();
1798 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1799 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800}
1801
1802void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1803 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1804 assert(GetStackID() ==
1805 curr_frame.GetStackID()); // TODO: remove this after some testing
1806 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1807 assert(GetThread() == curr_frame.GetThread());
1808 m_frame_index = curr_frame.m_frame_index;
1809 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1810 m_reg_context_sp = curr_frame.m_reg_context_sp;
1811 m_frame_code_addr = curr_frame.m_frame_code_addr;
1812 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1813 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1814 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1815 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1816 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1817 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1818 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1819 m_sc.function == curr_frame.m_sc.function);
1820 m_sc = curr_frame.m_sc;
1821 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1822 m_flags.Set(m_sc.GetResolvedMask());
1823 m_frame_base.Clear();
1824 m_frame_base_error.Clear();
1825}
1826
1827bool StackFrame::HasCachedData() const {
1828 if (m_variable_list_sp)
Greg Clayton7260f622011-04-18 08:33:37 +00001829 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 if (m_variable_list_value_objects.GetSize() > 0)
1831 return true;
1832 if (!m_disassembly.GetString().empty())
1833 return true;
1834 return false;
1835}
1836
1837bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1838 const char *frame_marker) {
1839
1840 if (show_frame_info) {
1841 strm.Indent();
1842 DumpUsingSettingsFormat(&strm, frame_marker);
1843 }
1844
1845 if (show_source) {
1846 ExecutionContext exe_ctx(shared_from_this());
1847 bool have_source = false, have_debuginfo = false;
1848 Debugger::StopDisassemblyType disasm_display =
1849 Debugger::eStopDisassemblyTypeNever;
1850 Target *target = exe_ctx.GetTargetPtr();
1851 if (target) {
1852 Debugger &debugger = target->GetDebugger();
1853 const uint32_t source_lines_before =
1854 debugger.GetStopSourceLineCount(true);
1855 const uint32_t source_lines_after =
1856 debugger.GetStopSourceLineCount(false);
1857 disasm_display = debugger.GetStopDisassemblyDisplay();
1858
1859 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1860 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1861 have_debuginfo = true;
1862 if (source_lines_before > 0 || source_lines_after > 0) {
1863 size_t num_lines =
1864 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1865 m_sc.line_entry.file, m_sc.line_entry.line,
Todd Fiala9666ba72016-09-21 20:13:14 +00001866 m_sc.line_entry.column, source_lines_before,
1867 source_lines_after, "->", &strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868 if (num_lines != 0)
1869 have_source = true;
1870 // TODO: Give here a one time warning if source file is missing.
1871 }
1872 }
1873 switch (disasm_display) {
1874 case Debugger::eStopDisassemblyTypeNever:
1875 break;
1876
1877 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1878 if (have_debuginfo)
1879 break;
1880 LLVM_FALLTHROUGH;
1881
1882 case Debugger::eStopDisassemblyTypeNoSource:
1883 if (have_source)
1884 break;
1885 LLVM_FALLTHROUGH;
1886
1887 case Debugger::eStopDisassemblyTypeAlways:
1888 if (target) {
1889 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1890 if (disasm_lines > 0) {
1891 const ArchSpec &target_arch = target->GetArchitecture();
1892 AddressRange pc_range;
1893 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1894 pc_range.SetByteSize(disasm_lines *
1895 target_arch.GetMaximumOpcodeByteSize());
1896 const char *plugin_name = nullptr;
1897 const char *flavor = nullptr;
Jason Molenda0b4c26b2016-09-08 05:12:41 +00001898 const bool mixed_source_and_assembly = false;
1899 Disassembler::Disassemble(
1900 target->GetDebugger(), target_arch, plugin_name, flavor,
1901 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1902 Disassembler::eOptionMarkPCAddress, strm);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001903 }
1904 }
1905 break;
1906 }
1907 }
1908 }
1909 return true;
Greg Clayton7260f622011-04-18 08:33:37 +00001910}