15ffd83dbSDimitry Andric //===-- StackFrame.cpp ----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Target/StackFrame.h"
100b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
110b57cec5SDimitry Andric #include "lldb/Core/Disassembler.h"
120b57cec5SDimitry Andric #include "lldb/Core/FormatEntity.h"
130b57cec5SDimitry Andric #include "lldb/Core/Mangled.h"
140b57cec5SDimitry Andric #include "lldb/Core/Module.h"
150b57cec5SDimitry Andric #include "lldb/Core/Value.h"
160b57cec5SDimitry Andric #include "lldb/Core/ValueObjectConstResult.h"
170b57cec5SDimitry Andric #include "lldb/Core/ValueObjectMemory.h"
180b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h"
190b57cec5SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
200b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
210b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
220b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContextScope.h"
230b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
250b57cec5SDimitry Andric #include "lldb/Target/ABI.h"
260b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
270b57cec5SDimitry Andric #include "lldb/Target/Process.h"
280b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
290b57cec5SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
300b57cec5SDimitry Andric #include "lldb/Target/Target.h"
310b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
329dba64beSDimitry Andric #include "lldb/Utility/Log.h"
330b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h"
340b57cec5SDimitry Andric
350b57cec5SDimitry Andric #include "lldb/lldb-enumerations.h"
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric #include <memory>
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric using namespace lldb;
400b57cec5SDimitry Andric using namespace lldb_private;
410b57cec5SDimitry Andric
420b57cec5SDimitry Andric // The first bits in the flags are reserved for the SymbolContext::Scope bits
430b57cec5SDimitry Andric // so we know if we have tried to look up information in our internal symbol
440b57cec5SDimitry Andric // context (m_sc) already.
450b57cec5SDimitry Andric #define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
460b57cec5SDimitry Andric #define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
470b57cec5SDimitry Andric #define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
480b57cec5SDimitry Andric #define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
490b57cec5SDimitry Andric #define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
500b57cec5SDimitry Andric
StackFrame(const ThreadSP & thread_sp,user_id_t frame_idx,user_id_t unwind_frame_index,addr_t cfa,bool cfa_is_valid,addr_t pc,StackFrame::Kind kind,bool behaves_like_zeroth_frame,const SymbolContext * sc_ptr)510b57cec5SDimitry Andric StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
520b57cec5SDimitry Andric user_id_t unwind_frame_index, addr_t cfa,
530b57cec5SDimitry Andric bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
549dba64beSDimitry Andric bool behaves_like_zeroth_frame,
550b57cec5SDimitry Andric const SymbolContext *sc_ptr)
560b57cec5SDimitry Andric : m_thread_wp(thread_sp), m_frame_index(frame_idx),
570b57cec5SDimitry Andric m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
580b57cec5SDimitry Andric m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
590b57cec5SDimitry Andric m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
609dba64beSDimitry Andric m_stack_frame_kind(kind),
619dba64beSDimitry Andric m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
629dba64beSDimitry Andric m_variable_list_sp(), m_variable_list_value_objects(),
639dba64beSDimitry Andric m_recognized_frame_sp(), m_disassembly(), m_mutex() {
640b57cec5SDimitry Andric // If we don't have a CFA value, use the frame index for our StackID so that
650b57cec5SDimitry Andric // recursive functions properly aren't confused with one another on a history
660b57cec5SDimitry Andric // stack.
670b57cec5SDimitry Andric if (IsHistorical() && !m_cfa_is_valid) {
680b57cec5SDimitry Andric m_id.SetCFA(m_frame_index);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric if (sc_ptr != nullptr) {
720b57cec5SDimitry Andric m_sc = *sc_ptr;
730b57cec5SDimitry Andric m_flags.Set(m_sc.GetResolvedMask());
740b57cec5SDimitry Andric }
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric
StackFrame(const ThreadSP & thread_sp,user_id_t frame_idx,user_id_t unwind_frame_index,const RegisterContextSP & reg_context_sp,addr_t cfa,addr_t pc,bool behaves_like_zeroth_frame,const SymbolContext * sc_ptr)770b57cec5SDimitry Andric StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
780b57cec5SDimitry Andric user_id_t unwind_frame_index,
790b57cec5SDimitry Andric const RegisterContextSP ®_context_sp, addr_t cfa,
809dba64beSDimitry Andric addr_t pc, bool behaves_like_zeroth_frame,
819dba64beSDimitry Andric const SymbolContext *sc_ptr)
820b57cec5SDimitry Andric : m_thread_wp(thread_sp), m_frame_index(frame_idx),
830b57cec5SDimitry Andric m_concrete_frame_index(unwind_frame_index),
840b57cec5SDimitry Andric m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
850b57cec5SDimitry Andric m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
860b57cec5SDimitry Andric m_frame_base_error(), m_cfa_is_valid(true),
879dba64beSDimitry Andric m_stack_frame_kind(StackFrame::Kind::Regular),
889dba64beSDimitry Andric m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
899dba64beSDimitry Andric m_variable_list_sp(), m_variable_list_value_objects(),
909dba64beSDimitry Andric m_recognized_frame_sp(), m_disassembly(), m_mutex() {
910b57cec5SDimitry Andric if (sc_ptr != nullptr) {
920b57cec5SDimitry Andric m_sc = *sc_ptr;
930b57cec5SDimitry Andric m_flags.Set(m_sc.GetResolvedMask());
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric if (reg_context_sp && !m_sc.target_sp) {
970b57cec5SDimitry Andric m_sc.target_sp = reg_context_sp->CalculateTarget();
980b57cec5SDimitry Andric if (m_sc.target_sp)
990b57cec5SDimitry Andric m_flags.Set(eSymbolContextTarget);
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric }
1020b57cec5SDimitry Andric
StackFrame(const ThreadSP & thread_sp,user_id_t frame_idx,user_id_t unwind_frame_index,const RegisterContextSP & reg_context_sp,addr_t cfa,const Address & pc_addr,bool behaves_like_zeroth_frame,const SymbolContext * sc_ptr)1030b57cec5SDimitry Andric StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
1040b57cec5SDimitry Andric user_id_t unwind_frame_index,
1050b57cec5SDimitry Andric const RegisterContextSP ®_context_sp, addr_t cfa,
1069dba64beSDimitry Andric const Address &pc_addr, bool behaves_like_zeroth_frame,
1079dba64beSDimitry Andric const SymbolContext *sc_ptr)
1080b57cec5SDimitry Andric : m_thread_wp(thread_sp), m_frame_index(frame_idx),
1090b57cec5SDimitry Andric m_concrete_frame_index(unwind_frame_index),
1100b57cec5SDimitry Andric m_reg_context_sp(reg_context_sp),
1110b57cec5SDimitry Andric m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
1120b57cec5SDimitry Andric nullptr),
1130b57cec5SDimitry Andric m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
1140b57cec5SDimitry Andric m_frame_base_error(), m_cfa_is_valid(true),
1159dba64beSDimitry Andric m_stack_frame_kind(StackFrame::Kind::Regular),
1169dba64beSDimitry Andric m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
1179dba64beSDimitry Andric m_variable_list_sp(), m_variable_list_value_objects(),
1189dba64beSDimitry Andric m_recognized_frame_sp(), m_disassembly(), m_mutex() {
1190b57cec5SDimitry Andric if (sc_ptr != nullptr) {
1200b57cec5SDimitry Andric m_sc = *sc_ptr;
1210b57cec5SDimitry Andric m_flags.Set(m_sc.GetResolvedMask());
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric if (!m_sc.target_sp && reg_context_sp) {
1250b57cec5SDimitry Andric m_sc.target_sp = reg_context_sp->CalculateTarget();
1260b57cec5SDimitry Andric if (m_sc.target_sp)
1270b57cec5SDimitry Andric m_flags.Set(eSymbolContextTarget);
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric
1300b57cec5SDimitry Andric ModuleSP pc_module_sp(pc_addr.GetModule());
1310b57cec5SDimitry Andric if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
1320b57cec5SDimitry Andric if (pc_module_sp) {
1330b57cec5SDimitry Andric m_sc.module_sp = pc_module_sp;
1340b57cec5SDimitry Andric m_flags.Set(eSymbolContextModule);
1350b57cec5SDimitry Andric } else {
1360b57cec5SDimitry Andric m_sc.module_sp.reset();
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric StackFrame::~StackFrame() = default;
1420b57cec5SDimitry Andric
GetStackID()1430b57cec5SDimitry Andric StackID &StackFrame::GetStackID() {
1440b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1450b57cec5SDimitry Andric // Make sure we have resolved the StackID object's symbol context scope if we
1460b57cec5SDimitry Andric // already haven't looked it up.
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
1490b57cec5SDimitry Andric if (m_id.GetSymbolContextScope()) {
1500b57cec5SDimitry Andric // We already have a symbol context scope, we just don't have our flag
1510b57cec5SDimitry Andric // bit set.
1520b57cec5SDimitry Andric m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
1530b57cec5SDimitry Andric } else {
1540b57cec5SDimitry Andric // Calculate the frame block and use this for the stack ID symbol context
1550b57cec5SDimitry Andric // scope if we have one.
1560b57cec5SDimitry Andric SymbolContextScope *scope = GetFrameBlock();
1570b57cec5SDimitry Andric if (scope == nullptr) {
1580b57cec5SDimitry Andric // We don't have a block, so use the symbol
1590b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextSymbol))
1600b57cec5SDimitry Andric GetSymbolContext(eSymbolContextSymbol);
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric // It is ok if m_sc.symbol is nullptr here
1630b57cec5SDimitry Andric scope = m_sc.symbol;
1640b57cec5SDimitry Andric }
1650b57cec5SDimitry Andric // Set the symbol context scope (the accessor will set the
1660b57cec5SDimitry Andric // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
1670b57cec5SDimitry Andric SetSymbolContextScope(scope);
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric return m_id;
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric
GetFrameIndex() const1730b57cec5SDimitry Andric uint32_t StackFrame::GetFrameIndex() const {
1740b57cec5SDimitry Andric ThreadSP thread_sp = GetThread();
1750b57cec5SDimitry Andric if (thread_sp)
1760b57cec5SDimitry Andric return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
1770b57cec5SDimitry Andric m_frame_index);
1780b57cec5SDimitry Andric else
1790b57cec5SDimitry Andric return m_frame_index;
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric
SetSymbolContextScope(SymbolContextScope * symbol_scope)1820b57cec5SDimitry Andric void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
1830b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1840b57cec5SDimitry Andric m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
1850b57cec5SDimitry Andric m_id.SetSymbolContextScope(symbol_scope);
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric
GetFrameCodeAddress()1880b57cec5SDimitry Andric const Address &StackFrame::GetFrameCodeAddress() {
1890b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
1900b57cec5SDimitry Andric if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
1910b57cec5SDimitry Andric !m_frame_code_addr.IsSectionOffset()) {
1920b57cec5SDimitry Andric m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric // Resolve the PC into a temporary address because if ResolveLoadAddress
1950b57cec5SDimitry Andric // fails to resolve the address, it will clear the address object...
1960b57cec5SDimitry Andric ThreadSP thread_sp(GetThread());
1970b57cec5SDimitry Andric if (thread_sp) {
1980b57cec5SDimitry Andric TargetSP target_sp(thread_sp->CalculateTarget());
1990b57cec5SDimitry Andric if (target_sp) {
2000b57cec5SDimitry Andric const bool allow_section_end = true;
2010b57cec5SDimitry Andric if (m_frame_code_addr.SetOpcodeLoadAddress(
2020b57cec5SDimitry Andric m_frame_code_addr.GetOffset(), target_sp.get(),
2030b57cec5SDimitry Andric AddressClass::eCode, allow_section_end)) {
2040b57cec5SDimitry Andric ModuleSP module_sp(m_frame_code_addr.GetModule());
2050b57cec5SDimitry Andric if (module_sp) {
2060b57cec5SDimitry Andric m_sc.module_sp = module_sp;
2070b57cec5SDimitry Andric m_flags.Set(eSymbolContextModule);
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric }
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric }
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric return m_frame_code_addr;
2140b57cec5SDimitry Andric }
2150b57cec5SDimitry Andric
216*5f7ddb14SDimitry Andric // This can't be rewritten into a call to
217*5f7ddb14SDimitry Andric // RegisterContext::GetPCForSymbolication because this
218*5f7ddb14SDimitry Andric // StackFrame may have been constructed with a special pc,
219*5f7ddb14SDimitry Andric // e.g. tail-call artificial frames.
GetFrameCodeAddressForSymbolication()220*5f7ddb14SDimitry Andric Address StackFrame::GetFrameCodeAddressForSymbolication() {
221*5f7ddb14SDimitry Andric Address lookup_addr(GetFrameCodeAddress());
222*5f7ddb14SDimitry Andric if (!lookup_addr.IsValid())
223*5f7ddb14SDimitry Andric return lookup_addr;
224*5f7ddb14SDimitry Andric if (m_behaves_like_zeroth_frame)
225*5f7ddb14SDimitry Andric return lookup_addr;
226*5f7ddb14SDimitry Andric
227*5f7ddb14SDimitry Andric addr_t offset = lookup_addr.GetOffset();
228*5f7ddb14SDimitry Andric if (offset > 0) {
229*5f7ddb14SDimitry Andric lookup_addr.SetOffset(offset - 1);
230*5f7ddb14SDimitry Andric } else {
231*5f7ddb14SDimitry Andric // lookup_addr is the start of a section. We need do the math on the
232*5f7ddb14SDimitry Andric // actual load address and re-compute the section. We're working with
233*5f7ddb14SDimitry Andric // a 'noreturn' function at the end of a section.
234*5f7ddb14SDimitry Andric TargetSP target_sp = CalculateTarget();
235*5f7ddb14SDimitry Andric if (target_sp) {
236*5f7ddb14SDimitry Andric addr_t addr_minus_one = lookup_addr.GetOpcodeLoadAddress(
237*5f7ddb14SDimitry Andric target_sp.get(), AddressClass::eCode) -
238*5f7ddb14SDimitry Andric 1;
239*5f7ddb14SDimitry Andric lookup_addr.SetOpcodeLoadAddress(addr_minus_one, target_sp.get());
240*5f7ddb14SDimitry Andric }
241*5f7ddb14SDimitry Andric }
242*5f7ddb14SDimitry Andric return lookup_addr;
243*5f7ddb14SDimitry Andric }
244*5f7ddb14SDimitry Andric
ChangePC(addr_t pc)2450b57cec5SDimitry Andric bool StackFrame::ChangePC(addr_t pc) {
2460b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
2470b57cec5SDimitry Andric // We can't change the pc value of a history stack frame - it is immutable.
2480b57cec5SDimitry Andric if (IsHistorical())
2490b57cec5SDimitry Andric return false;
2500b57cec5SDimitry Andric m_frame_code_addr.SetRawAddress(pc);
2510b57cec5SDimitry Andric m_sc.Clear(false);
2520b57cec5SDimitry Andric m_flags.Reset(0);
2530b57cec5SDimitry Andric ThreadSP thread_sp(GetThread());
2540b57cec5SDimitry Andric if (thread_sp)
2550b57cec5SDimitry Andric thread_sp->ClearStackFrames();
2560b57cec5SDimitry Andric return true;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric
Disassemble()2590b57cec5SDimitry Andric const char *StackFrame::Disassemble() {
2600b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
261af732203SDimitry Andric if (!m_disassembly.Empty())
262af732203SDimitry Andric return m_disassembly.GetData();
263af732203SDimitry Andric
2640b57cec5SDimitry Andric ExecutionContext exe_ctx(shared_from_this());
265af732203SDimitry Andric if (Target *target = exe_ctx.GetTargetPtr()) {
266af732203SDimitry Andric Disassembler::Disassemble(target->GetDebugger(), target->GetArchitecture(),
267af732203SDimitry Andric *this, m_disassembly);
2680b57cec5SDimitry Andric }
2690b57cec5SDimitry Andric
270af732203SDimitry Andric return m_disassembly.Empty() ? nullptr : m_disassembly.GetData();
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric
GetFrameBlock()2730b57cec5SDimitry Andric Block *StackFrame::GetFrameBlock() {
2740b57cec5SDimitry Andric if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
2750b57cec5SDimitry Andric GetSymbolContext(eSymbolContextBlock);
2760b57cec5SDimitry Andric
2770b57cec5SDimitry Andric if (m_sc.block) {
2780b57cec5SDimitry Andric Block *inline_block = m_sc.block->GetContainingInlinedBlock();
2790b57cec5SDimitry Andric if (inline_block) {
2800b57cec5SDimitry Andric // Use the block with the inlined function info as the frame block we
2810b57cec5SDimitry Andric // want this frame to have only the variables for the inlined function
2820b57cec5SDimitry Andric // and its non-inlined block child blocks.
2830b57cec5SDimitry Andric return inline_block;
2840b57cec5SDimitry Andric } else {
2850b57cec5SDimitry Andric // This block is not contained within any inlined function blocks with so
2860b57cec5SDimitry Andric // we want to use the top most function block.
2870b57cec5SDimitry Andric return &m_sc.function->GetBlock(false);
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric return nullptr;
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric // Get the symbol context if we already haven't done so by resolving the
2940b57cec5SDimitry Andric // PC address as much as possible. This way when we pass around a
2950b57cec5SDimitry Andric // StackFrame object, everyone will have as much information as possible and no
2960b57cec5SDimitry Andric // one will ever have to look things up manually.
2970b57cec5SDimitry Andric const SymbolContext &
GetSymbolContext(SymbolContextItem resolve_scope)2980b57cec5SDimitry Andric StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
2990b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
3000b57cec5SDimitry Andric // Copy our internal symbol context into "sc".
3010b57cec5SDimitry Andric if ((m_flags.Get() & resolve_scope) != resolve_scope) {
3020b57cec5SDimitry Andric uint32_t resolved = 0;
3030b57cec5SDimitry Andric
3040b57cec5SDimitry Andric // If the target was requested add that:
3050b57cec5SDimitry Andric if (!m_sc.target_sp) {
3060b57cec5SDimitry Andric m_sc.target_sp = CalculateTarget();
3070b57cec5SDimitry Andric if (m_sc.target_sp)
3080b57cec5SDimitry Andric resolved |= eSymbolContextTarget;
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric // Resolve our PC to section offset if we haven't already done so and if we
3120b57cec5SDimitry Andric // don't have a module. The resolved address section will contain the
3130b57cec5SDimitry Andric // module to which it belongs
3140b57cec5SDimitry Andric if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
3150b57cec5SDimitry Andric GetFrameCodeAddress();
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric // If this is not frame zero, then we need to subtract 1 from the PC value
3180b57cec5SDimitry Andric // when doing address lookups since the PC will be on the instruction
3190b57cec5SDimitry Andric // following the function call instruction...
320*5f7ddb14SDimitry Andric Address lookup_addr(GetFrameCodeAddressForSymbolication());
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andric if (m_sc.module_sp) {
3230b57cec5SDimitry Andric // We have something in our stack frame symbol context, lets check if we
3240b57cec5SDimitry Andric // haven't already tried to lookup one of those things. If we haven't
3250b57cec5SDimitry Andric // then we will do the query.
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andric SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
3280b57cec5SDimitry Andric
3290b57cec5SDimitry Andric if (resolve_scope & eSymbolContextCompUnit) {
3300b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextCompUnit)) {
3310b57cec5SDimitry Andric if (m_sc.comp_unit)
3320b57cec5SDimitry Andric resolved |= eSymbolContextCompUnit;
3330b57cec5SDimitry Andric else
3340b57cec5SDimitry Andric actual_resolve_scope |= eSymbolContextCompUnit;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric if (resolve_scope & eSymbolContextFunction) {
3390b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextFunction)) {
3400b57cec5SDimitry Andric if (m_sc.function)
3410b57cec5SDimitry Andric resolved |= eSymbolContextFunction;
3420b57cec5SDimitry Andric else
3430b57cec5SDimitry Andric actual_resolve_scope |= eSymbolContextFunction;
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric
3470b57cec5SDimitry Andric if (resolve_scope & eSymbolContextBlock) {
3480b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextBlock)) {
3490b57cec5SDimitry Andric if (m_sc.block)
3500b57cec5SDimitry Andric resolved |= eSymbolContextBlock;
3510b57cec5SDimitry Andric else
3520b57cec5SDimitry Andric actual_resolve_scope |= eSymbolContextBlock;
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric if (resolve_scope & eSymbolContextSymbol) {
3570b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextSymbol)) {
3580b57cec5SDimitry Andric if (m_sc.symbol)
3590b57cec5SDimitry Andric resolved |= eSymbolContextSymbol;
3600b57cec5SDimitry Andric else
3610b57cec5SDimitry Andric actual_resolve_scope |= eSymbolContextSymbol;
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric if (resolve_scope & eSymbolContextLineEntry) {
3660b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextLineEntry)) {
3670b57cec5SDimitry Andric if (m_sc.line_entry.IsValid())
3680b57cec5SDimitry Andric resolved |= eSymbolContextLineEntry;
3690b57cec5SDimitry Andric else
3700b57cec5SDimitry Andric actual_resolve_scope |= eSymbolContextLineEntry;
3710b57cec5SDimitry Andric }
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
3740b57cec5SDimitry Andric if (actual_resolve_scope) {
3750b57cec5SDimitry Andric // We might be resolving less information than what is already in our
3760b57cec5SDimitry Andric // current symbol context so resolve into a temporary symbol context
3770b57cec5SDimitry Andric // "sc" so we don't clear out data we have already found in "m_sc"
3780b57cec5SDimitry Andric SymbolContext sc;
3790b57cec5SDimitry Andric // Set flags that indicate what we have tried to resolve
3800b57cec5SDimitry Andric resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
3810b57cec5SDimitry Andric lookup_addr, actual_resolve_scope, sc);
3820b57cec5SDimitry Andric // Only replace what we didn't already have as we may have information
3830b57cec5SDimitry Andric // for an inlined function scope that won't match what a standard
3840b57cec5SDimitry Andric // lookup by address would match
3850b57cec5SDimitry Andric if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
3860b57cec5SDimitry Andric m_sc.comp_unit = sc.comp_unit;
3870b57cec5SDimitry Andric if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
3880b57cec5SDimitry Andric m_sc.function = sc.function;
3890b57cec5SDimitry Andric if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
3900b57cec5SDimitry Andric m_sc.block = sc.block;
3910b57cec5SDimitry Andric if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
3920b57cec5SDimitry Andric m_sc.symbol = sc.symbol;
3930b57cec5SDimitry Andric if ((resolved & eSymbolContextLineEntry) &&
3940b57cec5SDimitry Andric !m_sc.line_entry.IsValid()) {
3950b57cec5SDimitry Andric m_sc.line_entry = sc.line_entry;
3960b57cec5SDimitry Andric m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric } else {
4000b57cec5SDimitry Andric // If we don't have a module, then we can't have the compile unit,
4010b57cec5SDimitry Andric // function, block, line entry or symbol, so we can safely call
4020b57cec5SDimitry Andric // ResolveSymbolContextForAddress with our symbol context member m_sc.
4030b57cec5SDimitry Andric if (m_sc.target_sp) {
4040b57cec5SDimitry Andric resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
4050b57cec5SDimitry Andric lookup_addr, resolve_scope, m_sc);
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric }
4080b57cec5SDimitry Andric
4090b57cec5SDimitry Andric // Update our internal flags so we remember what we have tried to locate so
4100b57cec5SDimitry Andric // we don't have to keep trying when more calls to this function are made.
4110b57cec5SDimitry Andric // We might have dug up more information that was requested (for example if
4120b57cec5SDimitry Andric // we were asked to only get the block, we will have gotten the compile
4130b57cec5SDimitry Andric // unit, and function) so set any additional bits that we resolved
4140b57cec5SDimitry Andric m_flags.Set(resolve_scope | resolved);
4150b57cec5SDimitry Andric }
4160b57cec5SDimitry Andric
4170b57cec5SDimitry Andric // Return the symbol context with everything that was possible to resolve
4180b57cec5SDimitry Andric // resolved.
4190b57cec5SDimitry Andric return m_sc;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric
GetVariableList(bool get_file_globals)4220b57cec5SDimitry Andric VariableList *StackFrame::GetVariableList(bool get_file_globals) {
4230b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
4240b57cec5SDimitry Andric if (m_flags.IsClear(RESOLVED_VARIABLES)) {
4250b57cec5SDimitry Andric m_flags.Set(RESOLVED_VARIABLES);
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric Block *frame_block = GetFrameBlock();
4280b57cec5SDimitry Andric
4290b57cec5SDimitry Andric if (frame_block) {
4300b57cec5SDimitry Andric const bool get_child_variables = true;
4310b57cec5SDimitry Andric const bool can_create = true;
4320b57cec5SDimitry Andric const bool stop_if_child_block_is_inlined_function = true;
4330b57cec5SDimitry Andric m_variable_list_sp = std::make_shared<VariableList>();
4340b57cec5SDimitry Andric frame_block->AppendBlockVariables(can_create, get_child_variables,
4350b57cec5SDimitry Andric stop_if_child_block_is_inlined_function,
4360b57cec5SDimitry Andric [](Variable *v) { return true; },
4370b57cec5SDimitry Andric m_variable_list_sp.get());
4380b57cec5SDimitry Andric }
4390b57cec5SDimitry Andric }
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
4420b57cec5SDimitry Andric m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
4430b57cec5SDimitry Andric
4440b57cec5SDimitry Andric if (m_flags.IsClear(eSymbolContextCompUnit))
4450b57cec5SDimitry Andric GetSymbolContext(eSymbolContextCompUnit);
4460b57cec5SDimitry Andric
4470b57cec5SDimitry Andric if (m_sc.comp_unit) {
4480b57cec5SDimitry Andric VariableListSP global_variable_list_sp(
4490b57cec5SDimitry Andric m_sc.comp_unit->GetVariableList(true));
4500b57cec5SDimitry Andric if (m_variable_list_sp)
4510b57cec5SDimitry Andric m_variable_list_sp->AddVariables(global_variable_list_sp.get());
4520b57cec5SDimitry Andric else
4530b57cec5SDimitry Andric m_variable_list_sp = global_variable_list_sp;
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric }
4560b57cec5SDimitry Andric
4570b57cec5SDimitry Andric return m_variable_list_sp.get();
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric
4600b57cec5SDimitry Andric VariableListSP
GetInScopeVariableList(bool get_file_globals,bool must_have_valid_location)4610b57cec5SDimitry Andric StackFrame::GetInScopeVariableList(bool get_file_globals,
4620b57cec5SDimitry Andric bool must_have_valid_location) {
4630b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
4640b57cec5SDimitry Andric // We can't fetch variable information for a history stack frame.
4650b57cec5SDimitry Andric if (IsHistorical())
4660b57cec5SDimitry Andric return VariableListSP();
4670b57cec5SDimitry Andric
4680b57cec5SDimitry Andric VariableListSP var_list_sp(new VariableList);
4690b57cec5SDimitry Andric GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
4700b57cec5SDimitry Andric
4710b57cec5SDimitry Andric if (m_sc.block) {
4720b57cec5SDimitry Andric const bool can_create = true;
4730b57cec5SDimitry Andric const bool get_parent_variables = true;
4740b57cec5SDimitry Andric const bool stop_if_block_is_inlined_function = true;
4750b57cec5SDimitry Andric m_sc.block->AppendVariables(
4760b57cec5SDimitry Andric can_create, get_parent_variables, stop_if_block_is_inlined_function,
4770b57cec5SDimitry Andric [this, must_have_valid_location](Variable *v) {
4780b57cec5SDimitry Andric return v->IsInScope(this) && (!must_have_valid_location ||
4790b57cec5SDimitry Andric v->LocationIsValidForFrame(this));
4800b57cec5SDimitry Andric },
4810b57cec5SDimitry Andric var_list_sp.get());
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andric if (m_sc.comp_unit && get_file_globals) {
4850b57cec5SDimitry Andric VariableListSP global_variable_list_sp(
4860b57cec5SDimitry Andric m_sc.comp_unit->GetVariableList(true));
4870b57cec5SDimitry Andric if (global_variable_list_sp)
4880b57cec5SDimitry Andric var_list_sp->AddVariables(global_variable_list_sp.get());
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric
4910b57cec5SDimitry Andric return var_list_sp;
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric
GetValueForVariableExpressionPath(llvm::StringRef var_expr,DynamicValueType use_dynamic,uint32_t options,VariableSP & var_sp,Status & error)4940b57cec5SDimitry Andric ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
4950b57cec5SDimitry Andric llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
4960b57cec5SDimitry Andric VariableSP &var_sp, Status &error) {
4970b57cec5SDimitry Andric llvm::StringRef original_var_expr = var_expr;
4980b57cec5SDimitry Andric // We can't fetch variable information for a history stack frame.
4990b57cec5SDimitry Andric if (IsHistorical())
5000b57cec5SDimitry Andric return ValueObjectSP();
5010b57cec5SDimitry Andric
5020b57cec5SDimitry Andric if (var_expr.empty()) {
5030b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid variable path '%s'",
5040b57cec5SDimitry Andric var_expr.str().c_str());
5050b57cec5SDimitry Andric return ValueObjectSP();
5060b57cec5SDimitry Andric }
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andric const bool check_ptr_vs_member =
5090b57cec5SDimitry Andric (options & eExpressionPathOptionCheckPtrVsMember) != 0;
5100b57cec5SDimitry Andric const bool no_fragile_ivar =
5110b57cec5SDimitry Andric (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
5120b57cec5SDimitry Andric const bool no_synth_child =
5130b57cec5SDimitry Andric (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
5140b57cec5SDimitry Andric // const bool no_synth_array = (options &
5150b57cec5SDimitry Andric // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
5160b57cec5SDimitry Andric error.Clear();
5170b57cec5SDimitry Andric bool deref = false;
5180b57cec5SDimitry Andric bool address_of = false;
5190b57cec5SDimitry Andric ValueObjectSP valobj_sp;
5200b57cec5SDimitry Andric const bool get_file_globals = true;
5210b57cec5SDimitry Andric // When looking up a variable for an expression, we need only consider the
5220b57cec5SDimitry Andric // variables that are in scope.
5230b57cec5SDimitry Andric VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
5240b57cec5SDimitry Andric VariableList *variable_list = var_list_sp.get();
5250b57cec5SDimitry Andric
5260b57cec5SDimitry Andric if (!variable_list)
5270b57cec5SDimitry Andric return ValueObjectSP();
5280b57cec5SDimitry Andric
5290b57cec5SDimitry Andric // If first character is a '*', then show pointer contents
5300b57cec5SDimitry Andric std::string var_expr_storage;
5310b57cec5SDimitry Andric if (var_expr[0] == '*') {
5320b57cec5SDimitry Andric deref = true;
5330b57cec5SDimitry Andric var_expr = var_expr.drop_front(); // Skip the '*'
5340b57cec5SDimitry Andric } else if (var_expr[0] == '&') {
5350b57cec5SDimitry Andric address_of = true;
5360b57cec5SDimitry Andric var_expr = var_expr.drop_front(); // Skip the '&'
5370b57cec5SDimitry Andric }
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
5400b57cec5SDimitry Andric StreamString var_expr_path_strm;
5410b57cec5SDimitry Andric
5420b57cec5SDimitry Andric ConstString name_const_string(var_expr.substr(0, separator_idx));
5430b57cec5SDimitry Andric
5440b57cec5SDimitry Andric var_sp = variable_list->FindVariable(name_const_string, false);
5450b57cec5SDimitry Andric
5460b57cec5SDimitry Andric bool synthetically_added_instance_object = false;
5470b57cec5SDimitry Andric
5480b57cec5SDimitry Andric if (var_sp) {
5490b57cec5SDimitry Andric var_expr = var_expr.drop_front(name_const_string.GetLength());
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
5530b57cec5SDimitry Andric // Check for direct ivars access which helps us with implicit access to
5540b57cec5SDimitry Andric // ivars with the "this->" or "self->"
5550b57cec5SDimitry Andric GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
5560b57cec5SDimitry Andric lldb::LanguageType method_language = eLanguageTypeUnknown;
5570b57cec5SDimitry Andric bool is_instance_method = false;
5580b57cec5SDimitry Andric ConstString method_object_name;
5590b57cec5SDimitry Andric if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
5600b57cec5SDimitry Andric method_object_name)) {
5610b57cec5SDimitry Andric if (is_instance_method && method_object_name) {
5620b57cec5SDimitry Andric var_sp = variable_list->FindVariable(method_object_name);
5630b57cec5SDimitry Andric if (var_sp) {
5640b57cec5SDimitry Andric separator_idx = 0;
5650b57cec5SDimitry Andric var_expr_storage = "->";
5660b57cec5SDimitry Andric var_expr_storage += var_expr;
5670b57cec5SDimitry Andric var_expr = var_expr_storage;
5680b57cec5SDimitry Andric synthetically_added_instance_object = true;
5690b57cec5SDimitry Andric }
5700b57cec5SDimitry Andric }
5710b57cec5SDimitry Andric }
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andric if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
5750b57cec5SDimitry Andric // Check if any anonymous unions are there which contain a variable with
5760b57cec5SDimitry Andric // the name we need
577480093f4SDimitry Andric for (const VariableSP &variable_sp : *variable_list) {
5780b57cec5SDimitry Andric if (!variable_sp)
5790b57cec5SDimitry Andric continue;
5800b57cec5SDimitry Andric if (!variable_sp->GetName().IsEmpty())
5810b57cec5SDimitry Andric continue;
5820b57cec5SDimitry Andric
5830b57cec5SDimitry Andric Type *var_type = variable_sp->GetType();
5840b57cec5SDimitry Andric if (!var_type)
5850b57cec5SDimitry Andric continue;
5860b57cec5SDimitry Andric
5870b57cec5SDimitry Andric if (!var_type->GetForwardCompilerType().IsAnonymousType())
5880b57cec5SDimitry Andric continue;
5890b57cec5SDimitry Andric valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
5900b57cec5SDimitry Andric if (!valobj_sp)
5910b57cec5SDimitry Andric return valobj_sp;
5920b57cec5SDimitry Andric valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
5930b57cec5SDimitry Andric if (valobj_sp)
5940b57cec5SDimitry Andric break;
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric }
5970b57cec5SDimitry Andric
5980b57cec5SDimitry Andric if (var_sp && !valobj_sp) {
5990b57cec5SDimitry Andric valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
6000b57cec5SDimitry Andric if (!valobj_sp)
6010b57cec5SDimitry Andric return valobj_sp;
6020b57cec5SDimitry Andric }
6030b57cec5SDimitry Andric if (!valobj_sp) {
6040b57cec5SDimitry Andric error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
6050b57cec5SDimitry Andric name_const_string.GetCString());
6060b57cec5SDimitry Andric return ValueObjectSP();
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric // We are dumping at least one child
6105ffd83dbSDimitry Andric while (!var_expr.empty()) {
6110b57cec5SDimitry Andric // Calculate the next separator index ahead of time
6120b57cec5SDimitry Andric ValueObjectSP child_valobj_sp;
6130b57cec5SDimitry Andric const char separator_type = var_expr[0];
6140b57cec5SDimitry Andric bool expr_is_ptr = false;
6150b57cec5SDimitry Andric switch (separator_type) {
6160b57cec5SDimitry Andric case '-':
6170b57cec5SDimitry Andric expr_is_ptr = true;
6180b57cec5SDimitry Andric if (var_expr.size() >= 2 && var_expr[1] != '>')
6190b57cec5SDimitry Andric return ValueObjectSP();
6200b57cec5SDimitry Andric
6210b57cec5SDimitry Andric if (no_fragile_ivar) {
6220b57cec5SDimitry Andric // Make sure we aren't trying to deref an objective
6230b57cec5SDimitry Andric // C ivar if this is not allowed
6240b57cec5SDimitry Andric const uint32_t pointer_type_flags =
6250b57cec5SDimitry Andric valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
6260b57cec5SDimitry Andric if ((pointer_type_flags & eTypeIsObjC) &&
6270b57cec5SDimitry Andric (pointer_type_flags & eTypeIsPointer)) {
6280b57cec5SDimitry Andric // This was an objective C object pointer and it was requested we
6290b57cec5SDimitry Andric // skip any fragile ivars so return nothing here
6300b57cec5SDimitry Andric return ValueObjectSP();
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric }
6330b57cec5SDimitry Andric
6340b57cec5SDimitry Andric // If we have a non pointer type with a sythetic value then lets check if
6350b57cec5SDimitry Andric // we have an sythetic dereference specified.
6360b57cec5SDimitry Andric if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
6370b57cec5SDimitry Andric Status deref_error;
6380b57cec5SDimitry Andric if (valobj_sp->GetCompilerType().IsReferenceType()) {
6390b57cec5SDimitry Andric valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
6400b57cec5SDimitry Andric if (error.Fail()) {
6410b57cec5SDimitry Andric error.SetErrorStringWithFormatv(
6420b57cec5SDimitry Andric "Failed to dereference reference type: %s", deref_error);
6430b57cec5SDimitry Andric return ValueObjectSP();
6440b57cec5SDimitry Andric }
6450b57cec5SDimitry Andric }
6460b57cec5SDimitry Andric
6470b57cec5SDimitry Andric valobj_sp = valobj_sp->Dereference(deref_error);
6480b57cec5SDimitry Andric if (error.Fail()) {
6490b57cec5SDimitry Andric error.SetErrorStringWithFormatv(
6500b57cec5SDimitry Andric "Failed to dereference sythetic value: {0}", deref_error);
6510b57cec5SDimitry Andric return ValueObjectSP();
6520b57cec5SDimitry Andric }
6530b57cec5SDimitry Andric // Some synthetic plug-ins fail to set the error in Dereference
6540b57cec5SDimitry Andric if (!valobj_sp) {
6550b57cec5SDimitry Andric error.SetErrorString("Failed to dereference sythetic value");
6560b57cec5SDimitry Andric return ValueObjectSP();
6570b57cec5SDimitry Andric }
6580b57cec5SDimitry Andric expr_is_ptr = false;
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric var_expr = var_expr.drop_front(); // Remove the '-'
6620b57cec5SDimitry Andric LLVM_FALLTHROUGH;
6630b57cec5SDimitry Andric case '.': {
6640b57cec5SDimitry Andric var_expr = var_expr.drop_front(); // Remove the '.' or '>'
6650b57cec5SDimitry Andric separator_idx = var_expr.find_first_of(".-[");
6660b57cec5SDimitry Andric ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
6670b57cec5SDimitry Andric
6680b57cec5SDimitry Andric if (check_ptr_vs_member) {
6690b57cec5SDimitry Andric // We either have a pointer type and need to verify valobj_sp is a
6700b57cec5SDimitry Andric // pointer, or we have a member of a class/union/struct being accessed
6710b57cec5SDimitry Andric // with the . syntax and need to verify we don't have a pointer.
6720b57cec5SDimitry Andric const bool actual_is_ptr = valobj_sp->IsPointerType();
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andric if (actual_is_ptr != expr_is_ptr) {
6750b57cec5SDimitry Andric // Incorrect use of "." with a pointer, or "->" with a
6760b57cec5SDimitry Andric // class/union/struct instance or reference.
6775ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
6780b57cec5SDimitry Andric if (actual_is_ptr)
6790b57cec5SDimitry Andric error.SetErrorStringWithFormat(
6800b57cec5SDimitry Andric "\"%s\" is a pointer and . was used to attempt to access "
6810b57cec5SDimitry Andric "\"%s\". Did you mean \"%s->%s\"?",
6820b57cec5SDimitry Andric var_expr_path_strm.GetData(), child_name.GetCString(),
6830b57cec5SDimitry Andric var_expr_path_strm.GetData(), var_expr.str().c_str());
6840b57cec5SDimitry Andric else
6850b57cec5SDimitry Andric error.SetErrorStringWithFormat(
6860b57cec5SDimitry Andric "\"%s\" is not a pointer and -> was used to attempt to "
6870b57cec5SDimitry Andric "access \"%s\". Did you mean \"%s.%s\"?",
6880b57cec5SDimitry Andric var_expr_path_strm.GetData(), child_name.GetCString(),
6890b57cec5SDimitry Andric var_expr_path_strm.GetData(), var_expr.str().c_str());
6900b57cec5SDimitry Andric return ValueObjectSP();
6910b57cec5SDimitry Andric }
6920b57cec5SDimitry Andric }
6930b57cec5SDimitry Andric child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
6940b57cec5SDimitry Andric if (!child_valobj_sp) {
6950b57cec5SDimitry Andric if (!no_synth_child) {
6960b57cec5SDimitry Andric child_valobj_sp = valobj_sp->GetSyntheticValue();
6970b57cec5SDimitry Andric if (child_valobj_sp)
6980b57cec5SDimitry Andric child_valobj_sp =
6990b57cec5SDimitry Andric child_valobj_sp->GetChildMemberWithName(child_name, true);
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric
7020b57cec5SDimitry Andric if (no_synth_child || !child_valobj_sp) {
7030b57cec5SDimitry Andric // No child member with name "child_name"
7040b57cec5SDimitry Andric if (synthetically_added_instance_object) {
7050b57cec5SDimitry Andric // We added a "this->" or "self->" to the beginning of the
7060b57cec5SDimitry Andric // expression and this is the first pointer ivar access, so just
7070b57cec5SDimitry Andric // return the normal error
7080b57cec5SDimitry Andric error.SetErrorStringWithFormat(
7090b57cec5SDimitry Andric "no variable or instance variable named '%s' found in "
7100b57cec5SDimitry Andric "this frame",
7110b57cec5SDimitry Andric name_const_string.GetCString());
7120b57cec5SDimitry Andric } else {
7135ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
7140b57cec5SDimitry Andric if (child_name) {
7150b57cec5SDimitry Andric error.SetErrorStringWithFormat(
7160b57cec5SDimitry Andric "\"%s\" is not a member of \"(%s) %s\"",
7170b57cec5SDimitry Andric child_name.GetCString(),
7180b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
7190b57cec5SDimitry Andric var_expr_path_strm.GetData());
7200b57cec5SDimitry Andric } else {
7210b57cec5SDimitry Andric error.SetErrorStringWithFormat(
7220b57cec5SDimitry Andric "incomplete expression path after \"%s\" in \"%s\"",
7230b57cec5SDimitry Andric var_expr_path_strm.GetData(),
7240b57cec5SDimitry Andric original_var_expr.str().c_str());
7250b57cec5SDimitry Andric }
7260b57cec5SDimitry Andric }
7270b57cec5SDimitry Andric return ValueObjectSP();
7280b57cec5SDimitry Andric }
7290b57cec5SDimitry Andric }
7300b57cec5SDimitry Andric synthetically_added_instance_object = false;
7310b57cec5SDimitry Andric // Remove the child name from the path
7320b57cec5SDimitry Andric var_expr = var_expr.drop_front(child_name.GetLength());
7330b57cec5SDimitry Andric if (use_dynamic != eNoDynamicValues) {
7340b57cec5SDimitry Andric ValueObjectSP dynamic_value_sp(
7350b57cec5SDimitry Andric child_valobj_sp->GetDynamicValue(use_dynamic));
7360b57cec5SDimitry Andric if (dynamic_value_sp)
7370b57cec5SDimitry Andric child_valobj_sp = dynamic_value_sp;
7380b57cec5SDimitry Andric }
7390b57cec5SDimitry Andric } break;
7400b57cec5SDimitry Andric
7410b57cec5SDimitry Andric case '[': {
7420b57cec5SDimitry Andric // Array member access, or treating pointer as an array Need at least two
7430b57cec5SDimitry Andric // brackets and a number
7440b57cec5SDimitry Andric if (var_expr.size() <= 2) {
7450b57cec5SDimitry Andric error.SetErrorStringWithFormat(
7460b57cec5SDimitry Andric "invalid square bracket encountered after \"%s\" in \"%s\"",
7470b57cec5SDimitry Andric var_expr_path_strm.GetData(), var_expr.str().c_str());
7480b57cec5SDimitry Andric return ValueObjectSP();
7490b57cec5SDimitry Andric }
7500b57cec5SDimitry Andric
7510b57cec5SDimitry Andric // Drop the open brace.
7520b57cec5SDimitry Andric var_expr = var_expr.drop_front();
7530b57cec5SDimitry Andric long child_index = 0;
7540b57cec5SDimitry Andric
7550b57cec5SDimitry Andric // If there's no closing brace, this is an invalid expression.
7560b57cec5SDimitry Andric size_t end_pos = var_expr.find_first_of(']');
7570b57cec5SDimitry Andric if (end_pos == llvm::StringRef::npos) {
7580b57cec5SDimitry Andric error.SetErrorStringWithFormat(
7590b57cec5SDimitry Andric "missing closing square bracket in expression \"%s\"",
7600b57cec5SDimitry Andric var_expr_path_strm.GetData());
7610b57cec5SDimitry Andric return ValueObjectSP();
7620b57cec5SDimitry Andric }
7630b57cec5SDimitry Andric llvm::StringRef index_expr = var_expr.take_front(end_pos);
7640b57cec5SDimitry Andric llvm::StringRef original_index_expr = index_expr;
7650b57cec5SDimitry Andric // Drop all of "[index_expr]"
7660b57cec5SDimitry Andric var_expr = var_expr.drop_front(end_pos + 1);
7670b57cec5SDimitry Andric
7680b57cec5SDimitry Andric if (index_expr.consumeInteger(0, child_index)) {
7690b57cec5SDimitry Andric // If there was no integer anywhere in the index expression, this is
7700b57cec5SDimitry Andric // erroneous expression.
7710b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid index expression \"%s\"",
7720b57cec5SDimitry Andric index_expr.str().c_str());
7730b57cec5SDimitry Andric return ValueObjectSP();
7740b57cec5SDimitry Andric }
7750b57cec5SDimitry Andric
7760b57cec5SDimitry Andric if (index_expr.empty()) {
7770b57cec5SDimitry Andric // The entire index expression was a single integer.
7780b57cec5SDimitry Andric
7790b57cec5SDimitry Andric if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
7800b57cec5SDimitry Andric // what we have is *ptr[low]. the most similar C++ syntax is to deref
7810b57cec5SDimitry Andric // ptr and extract bit low out of it. reading array item low would be
7820b57cec5SDimitry Andric // done by saying ptr[low], without a deref * sign
7830b57cec5SDimitry Andric Status error;
7840b57cec5SDimitry Andric ValueObjectSP temp(valobj_sp->Dereference(error));
7850b57cec5SDimitry Andric if (error.Fail()) {
7865ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
7870b57cec5SDimitry Andric error.SetErrorStringWithFormat(
7880b57cec5SDimitry Andric "could not dereference \"(%s) %s\"",
7890b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
7900b57cec5SDimitry Andric var_expr_path_strm.GetData());
7910b57cec5SDimitry Andric return ValueObjectSP();
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric valobj_sp = temp;
7940b57cec5SDimitry Andric deref = false;
7950b57cec5SDimitry Andric } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
7960b57cec5SDimitry Andric deref) {
7970b57cec5SDimitry Andric // what we have is *arr[low]. the most similar C++ syntax is to get
7980b57cec5SDimitry Andric // arr[0] (an operation that is equivalent to deref-ing arr) and
7990b57cec5SDimitry Andric // extract bit low out of it. reading array item low would be done by
8000b57cec5SDimitry Andric // saying arr[low], without a deref * sign
8010b57cec5SDimitry Andric Status error;
8020b57cec5SDimitry Andric ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
8030b57cec5SDimitry Andric if (error.Fail()) {
8045ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
8050b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8060b57cec5SDimitry Andric "could not get item 0 for \"(%s) %s\"",
8070b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8080b57cec5SDimitry Andric var_expr_path_strm.GetData());
8090b57cec5SDimitry Andric return ValueObjectSP();
8100b57cec5SDimitry Andric }
8110b57cec5SDimitry Andric valobj_sp = temp;
8120b57cec5SDimitry Andric deref = false;
8130b57cec5SDimitry Andric }
8140b57cec5SDimitry Andric
8150b57cec5SDimitry Andric bool is_incomplete_array = false;
8160b57cec5SDimitry Andric if (valobj_sp->IsPointerType()) {
8170b57cec5SDimitry Andric bool is_objc_pointer = true;
8180b57cec5SDimitry Andric
8190b57cec5SDimitry Andric if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
8200b57cec5SDimitry Andric eLanguageTypeObjC)
8210b57cec5SDimitry Andric is_objc_pointer = false;
8220b57cec5SDimitry Andric else if (!valobj_sp->GetCompilerType().IsPointerType())
8230b57cec5SDimitry Andric is_objc_pointer = false;
8240b57cec5SDimitry Andric
8250b57cec5SDimitry Andric if (no_synth_child && is_objc_pointer) {
8260b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8270b57cec5SDimitry Andric "\"(%s) %s\" is an Objective-C pointer, and cannot be "
8280b57cec5SDimitry Andric "subscripted",
8290b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8300b57cec5SDimitry Andric var_expr_path_strm.GetData());
8310b57cec5SDimitry Andric
8320b57cec5SDimitry Andric return ValueObjectSP();
8330b57cec5SDimitry Andric } else if (is_objc_pointer) {
8340b57cec5SDimitry Andric // dereferencing ObjC variables is not valid.. so let's try and
8350b57cec5SDimitry Andric // recur to synthetic children
8360b57cec5SDimitry Andric ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
8370b57cec5SDimitry Andric if (!synthetic /* no synthetic */
8380b57cec5SDimitry Andric || synthetic == valobj_sp) /* synthetic is the same as
8390b57cec5SDimitry Andric the original object */
8400b57cec5SDimitry Andric {
8415ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
8420b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8430b57cec5SDimitry Andric "\"(%s) %s\" is not an array type",
8440b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8450b57cec5SDimitry Andric var_expr_path_strm.GetData());
8460b57cec5SDimitry Andric } else if (
8470b57cec5SDimitry Andric static_cast<uint32_t>(child_index) >=
8480b57cec5SDimitry Andric synthetic
8490b57cec5SDimitry Andric ->GetNumChildren() /* synthetic does not have that many values */) {
8505ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
8510b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8520b57cec5SDimitry Andric "array index %ld is not valid for \"(%s) %s\"", child_index,
8530b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8540b57cec5SDimitry Andric var_expr_path_strm.GetData());
8550b57cec5SDimitry Andric } else {
8560b57cec5SDimitry Andric child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
8570b57cec5SDimitry Andric if (!child_valobj_sp) {
8585ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
8590b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8600b57cec5SDimitry Andric "array index %ld is not valid for \"(%s) %s\"", child_index,
8610b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8620b57cec5SDimitry Andric var_expr_path_strm.GetData());
8630b57cec5SDimitry Andric }
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric } else {
8660b57cec5SDimitry Andric child_valobj_sp =
8670b57cec5SDimitry Andric valobj_sp->GetSyntheticArrayMember(child_index, true);
8680b57cec5SDimitry Andric if (!child_valobj_sp) {
8695ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
8700b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8710b57cec5SDimitry Andric "failed to use pointer as array for index %ld for "
8720b57cec5SDimitry Andric "\"(%s) %s\"",
8730b57cec5SDimitry Andric child_index,
8740b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8750b57cec5SDimitry Andric var_expr_path_strm.GetData());
8760b57cec5SDimitry Andric }
8770b57cec5SDimitry Andric }
8780b57cec5SDimitry Andric } else if (valobj_sp->GetCompilerType().IsArrayType(
8790b57cec5SDimitry Andric nullptr, nullptr, &is_incomplete_array)) {
8800b57cec5SDimitry Andric // Pass false to dynamic_value here so we can tell the difference
8810b57cec5SDimitry Andric // between no dynamic value and no member of this type...
8820b57cec5SDimitry Andric child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
8830b57cec5SDimitry Andric if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
8840b57cec5SDimitry Andric child_valobj_sp =
8850b57cec5SDimitry Andric valobj_sp->GetSyntheticArrayMember(child_index, true);
8860b57cec5SDimitry Andric
8870b57cec5SDimitry Andric if (!child_valobj_sp) {
8885ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
8890b57cec5SDimitry Andric error.SetErrorStringWithFormat(
8900b57cec5SDimitry Andric "array index %ld is not valid for \"(%s) %s\"", child_index,
8910b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
8920b57cec5SDimitry Andric var_expr_path_strm.GetData());
8930b57cec5SDimitry Andric }
8940b57cec5SDimitry Andric } else if (valobj_sp->GetCompilerType().IsScalarType()) {
8950b57cec5SDimitry Andric // this is a bitfield asking to display just one bit
8960b57cec5SDimitry Andric child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
8970b57cec5SDimitry Andric child_index, child_index, true);
8980b57cec5SDimitry Andric if (!child_valobj_sp) {
8995ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
9000b57cec5SDimitry Andric error.SetErrorStringWithFormat(
9010b57cec5SDimitry Andric "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
9020b57cec5SDimitry Andric child_index, child_index,
9030b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
9040b57cec5SDimitry Andric var_expr_path_strm.GetData());
9050b57cec5SDimitry Andric }
9060b57cec5SDimitry Andric } else {
9070b57cec5SDimitry Andric ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
9080b57cec5SDimitry Andric if (no_synth_child /* synthetic is forbidden */ ||
9090b57cec5SDimitry Andric !synthetic /* no synthetic */
9100b57cec5SDimitry Andric || synthetic == valobj_sp) /* synthetic is the same as the
9110b57cec5SDimitry Andric original object */
9120b57cec5SDimitry Andric {
9135ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
9140b57cec5SDimitry Andric error.SetErrorStringWithFormat(
9150b57cec5SDimitry Andric "\"(%s) %s\" is not an array type",
9160b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
9170b57cec5SDimitry Andric var_expr_path_strm.GetData());
9180b57cec5SDimitry Andric } else if (
9190b57cec5SDimitry Andric static_cast<uint32_t>(child_index) >=
9200b57cec5SDimitry Andric synthetic
9210b57cec5SDimitry Andric ->GetNumChildren() /* synthetic does not have that many values */) {
9225ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
9230b57cec5SDimitry Andric error.SetErrorStringWithFormat(
9240b57cec5SDimitry Andric "array index %ld is not valid for \"(%s) %s\"", child_index,
9250b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
9260b57cec5SDimitry Andric var_expr_path_strm.GetData());
9270b57cec5SDimitry Andric } else {
9280b57cec5SDimitry Andric child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
9290b57cec5SDimitry Andric if (!child_valobj_sp) {
9305ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
9310b57cec5SDimitry Andric error.SetErrorStringWithFormat(
9320b57cec5SDimitry Andric "array index %ld is not valid for \"(%s) %s\"", child_index,
9330b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
9340b57cec5SDimitry Andric var_expr_path_strm.GetData());
9350b57cec5SDimitry Andric }
9360b57cec5SDimitry Andric }
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric
9390b57cec5SDimitry Andric if (!child_valobj_sp) {
9400b57cec5SDimitry Andric // Invalid array index...
9410b57cec5SDimitry Andric return ValueObjectSP();
9420b57cec5SDimitry Andric }
9430b57cec5SDimitry Andric
9440b57cec5SDimitry Andric if (use_dynamic != eNoDynamicValues) {
9450b57cec5SDimitry Andric ValueObjectSP dynamic_value_sp(
9460b57cec5SDimitry Andric child_valobj_sp->GetDynamicValue(use_dynamic));
9470b57cec5SDimitry Andric if (dynamic_value_sp)
9480b57cec5SDimitry Andric child_valobj_sp = dynamic_value_sp;
9490b57cec5SDimitry Andric }
9500b57cec5SDimitry Andric // Break out early from the switch since we were able to find the child
9510b57cec5SDimitry Andric // member
9520b57cec5SDimitry Andric break;
9530b57cec5SDimitry Andric }
9540b57cec5SDimitry Andric
9550b57cec5SDimitry Andric // this is most probably a BitField, let's take a look
9560b57cec5SDimitry Andric if (index_expr.front() != '-') {
9570b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
9580b57cec5SDimitry Andric original_index_expr.str().c_str());
9590b57cec5SDimitry Andric return ValueObjectSP();
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric
9620b57cec5SDimitry Andric index_expr = index_expr.drop_front();
9630b57cec5SDimitry Andric long final_index = 0;
9640b57cec5SDimitry Andric if (index_expr.getAsInteger(0, final_index)) {
9650b57cec5SDimitry Andric error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
9660b57cec5SDimitry Andric original_index_expr.str().c_str());
9670b57cec5SDimitry Andric return ValueObjectSP();
9680b57cec5SDimitry Andric }
9690b57cec5SDimitry Andric
9700b57cec5SDimitry Andric // if the format given is [high-low], swap range
9710b57cec5SDimitry Andric if (child_index > final_index) {
9720b57cec5SDimitry Andric long temp = child_index;
9730b57cec5SDimitry Andric child_index = final_index;
9740b57cec5SDimitry Andric final_index = temp;
9750b57cec5SDimitry Andric }
9760b57cec5SDimitry Andric
9770b57cec5SDimitry Andric if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
9780b57cec5SDimitry Andric // what we have is *ptr[low-high]. the most similar C++ syntax is to
9790b57cec5SDimitry Andric // deref ptr and extract bits low thru high out of it. reading array
9800b57cec5SDimitry Andric // items low thru high would be done by saying ptr[low-high], without a
9810b57cec5SDimitry Andric // deref * sign
9820b57cec5SDimitry Andric Status error;
9830b57cec5SDimitry Andric ValueObjectSP temp(valobj_sp->Dereference(error));
9840b57cec5SDimitry Andric if (error.Fail()) {
9855ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
9860b57cec5SDimitry Andric error.SetErrorStringWithFormat(
9870b57cec5SDimitry Andric "could not dereference \"(%s) %s\"",
9880b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
9890b57cec5SDimitry Andric var_expr_path_strm.GetData());
9900b57cec5SDimitry Andric return ValueObjectSP();
9910b57cec5SDimitry Andric }
9920b57cec5SDimitry Andric valobj_sp = temp;
9930b57cec5SDimitry Andric deref = false;
9940b57cec5SDimitry Andric } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
9950b57cec5SDimitry Andric // what we have is *arr[low-high]. the most similar C++ syntax is to
9960b57cec5SDimitry Andric // get arr[0] (an operation that is equivalent to deref-ing arr) and
9970b57cec5SDimitry Andric // extract bits low thru high out of it. reading array items low thru
9980b57cec5SDimitry Andric // high would be done by saying arr[low-high], without a deref * sign
9990b57cec5SDimitry Andric Status error;
10000b57cec5SDimitry Andric ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
10010b57cec5SDimitry Andric if (error.Fail()) {
10025ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
10030b57cec5SDimitry Andric error.SetErrorStringWithFormat(
10040b57cec5SDimitry Andric "could not get item 0 for \"(%s) %s\"",
10050b57cec5SDimitry Andric valobj_sp->GetTypeName().AsCString("<invalid type>"),
10060b57cec5SDimitry Andric var_expr_path_strm.GetData());
10070b57cec5SDimitry Andric return ValueObjectSP();
10080b57cec5SDimitry Andric }
10090b57cec5SDimitry Andric valobj_sp = temp;
10100b57cec5SDimitry Andric deref = false;
10110b57cec5SDimitry Andric }
10120b57cec5SDimitry Andric
10130b57cec5SDimitry Andric child_valobj_sp =
10140b57cec5SDimitry Andric valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
10150b57cec5SDimitry Andric if (!child_valobj_sp) {
10165ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
10170b57cec5SDimitry Andric error.SetErrorStringWithFormat(
10180b57cec5SDimitry Andric "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
10190b57cec5SDimitry Andric final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
10200b57cec5SDimitry Andric var_expr_path_strm.GetData());
10210b57cec5SDimitry Andric }
10220b57cec5SDimitry Andric
10230b57cec5SDimitry Andric if (!child_valobj_sp) {
10240b57cec5SDimitry Andric // Invalid bitfield range...
10250b57cec5SDimitry Andric return ValueObjectSP();
10260b57cec5SDimitry Andric }
10270b57cec5SDimitry Andric
10280b57cec5SDimitry Andric if (use_dynamic != eNoDynamicValues) {
10290b57cec5SDimitry Andric ValueObjectSP dynamic_value_sp(
10300b57cec5SDimitry Andric child_valobj_sp->GetDynamicValue(use_dynamic));
10310b57cec5SDimitry Andric if (dynamic_value_sp)
10320b57cec5SDimitry Andric child_valobj_sp = dynamic_value_sp;
10330b57cec5SDimitry Andric }
10340b57cec5SDimitry Andric // Break out early from the switch since we were able to find the child
10350b57cec5SDimitry Andric // member
10360b57cec5SDimitry Andric break;
10370b57cec5SDimitry Andric }
10380b57cec5SDimitry Andric default:
10390b57cec5SDimitry Andric // Failure...
10400b57cec5SDimitry Andric {
10415ffd83dbSDimitry Andric valobj_sp->GetExpressionPath(var_expr_path_strm);
10420b57cec5SDimitry Andric error.SetErrorStringWithFormat(
10430b57cec5SDimitry Andric "unexpected char '%c' encountered after \"%s\" in \"%s\"",
10440b57cec5SDimitry Andric separator_type, var_expr_path_strm.GetData(),
10450b57cec5SDimitry Andric var_expr.str().c_str());
10460b57cec5SDimitry Andric
10470b57cec5SDimitry Andric return ValueObjectSP();
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric }
10500b57cec5SDimitry Andric
10510b57cec5SDimitry Andric if (child_valobj_sp)
10520b57cec5SDimitry Andric valobj_sp = child_valobj_sp;
10530b57cec5SDimitry Andric }
10540b57cec5SDimitry Andric if (valobj_sp) {
10550b57cec5SDimitry Andric if (deref) {
10560b57cec5SDimitry Andric ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
10570b57cec5SDimitry Andric valobj_sp = deref_valobj_sp;
10580b57cec5SDimitry Andric } else if (address_of) {
10590b57cec5SDimitry Andric ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
10600b57cec5SDimitry Andric valobj_sp = address_of_valobj_sp;
10610b57cec5SDimitry Andric }
10620b57cec5SDimitry Andric }
10630b57cec5SDimitry Andric return valobj_sp;
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric
GetFrameBaseValue(Scalar & frame_base,Status * error_ptr)10660b57cec5SDimitry Andric bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
10670b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
10680b57cec5SDimitry Andric if (!m_cfa_is_valid) {
10690b57cec5SDimitry Andric m_frame_base_error.SetErrorString(
10700b57cec5SDimitry Andric "No frame base available for this historical stack frame.");
10710b57cec5SDimitry Andric return false;
10720b57cec5SDimitry Andric }
10730b57cec5SDimitry Andric
10740b57cec5SDimitry Andric if (m_flags.IsClear(GOT_FRAME_BASE)) {
10750b57cec5SDimitry Andric if (m_sc.function) {
10760b57cec5SDimitry Andric m_frame_base.Clear();
10770b57cec5SDimitry Andric m_frame_base_error.Clear();
10780b57cec5SDimitry Andric
10790b57cec5SDimitry Andric m_flags.Set(GOT_FRAME_BASE);
10800b57cec5SDimitry Andric ExecutionContext exe_ctx(shared_from_this());
10810b57cec5SDimitry Andric Value expr_value;
10820b57cec5SDimitry Andric addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
10830b57cec5SDimitry Andric if (m_sc.function->GetFrameBaseExpression().IsLocationList())
10840b57cec5SDimitry Andric loclist_base_addr =
10850b57cec5SDimitry Andric m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
10860b57cec5SDimitry Andric exe_ctx.GetTargetPtr());
10870b57cec5SDimitry Andric
10880b57cec5SDimitry Andric if (!m_sc.function->GetFrameBaseExpression().Evaluate(
10890b57cec5SDimitry Andric &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
10900b57cec5SDimitry Andric expr_value, &m_frame_base_error)) {
10910b57cec5SDimitry Andric // We should really have an error if evaluate returns, but in case we
10920b57cec5SDimitry Andric // don't, lets set the error to something at least.
10930b57cec5SDimitry Andric if (m_frame_base_error.Success())
10940b57cec5SDimitry Andric m_frame_base_error.SetErrorString(
10950b57cec5SDimitry Andric "Evaluation of the frame base expression failed.");
10960b57cec5SDimitry Andric } else {
10970b57cec5SDimitry Andric m_frame_base = expr_value.ResolveValue(&exe_ctx);
10980b57cec5SDimitry Andric }
10990b57cec5SDimitry Andric } else {
11000b57cec5SDimitry Andric m_frame_base_error.SetErrorString("No function in symbol context.");
11010b57cec5SDimitry Andric }
11020b57cec5SDimitry Andric }
11030b57cec5SDimitry Andric
11040b57cec5SDimitry Andric if (m_frame_base_error.Success())
11050b57cec5SDimitry Andric frame_base = m_frame_base;
11060b57cec5SDimitry Andric
11070b57cec5SDimitry Andric if (error_ptr)
11080b57cec5SDimitry Andric *error_ptr = m_frame_base_error;
11090b57cec5SDimitry Andric return m_frame_base_error.Success();
11100b57cec5SDimitry Andric }
11110b57cec5SDimitry Andric
GetFrameBaseExpression(Status * error_ptr)11120b57cec5SDimitry Andric DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
11130b57cec5SDimitry Andric if (!m_sc.function) {
11140b57cec5SDimitry Andric if (error_ptr) {
11150b57cec5SDimitry Andric error_ptr->SetErrorString("No function in symbol context.");
11160b57cec5SDimitry Andric }
11170b57cec5SDimitry Andric return nullptr;
11180b57cec5SDimitry Andric }
11190b57cec5SDimitry Andric
11200b57cec5SDimitry Andric return &m_sc.function->GetFrameBaseExpression();
11210b57cec5SDimitry Andric }
11220b57cec5SDimitry Andric
GetRegisterContext()11230b57cec5SDimitry Andric RegisterContextSP StackFrame::GetRegisterContext() {
11240b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
11250b57cec5SDimitry Andric if (!m_reg_context_sp) {
11260b57cec5SDimitry Andric ThreadSP thread_sp(GetThread());
11270b57cec5SDimitry Andric if (thread_sp)
11280b57cec5SDimitry Andric m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
11290b57cec5SDimitry Andric }
11300b57cec5SDimitry Andric return m_reg_context_sp;
11310b57cec5SDimitry Andric }
11320b57cec5SDimitry Andric
HasDebugInformation()11330b57cec5SDimitry Andric bool StackFrame::HasDebugInformation() {
11340b57cec5SDimitry Andric GetSymbolContext(eSymbolContextLineEntry);
11350b57cec5SDimitry Andric return m_sc.line_entry.IsValid();
11360b57cec5SDimitry Andric }
11370b57cec5SDimitry Andric
11380b57cec5SDimitry Andric ValueObjectSP
GetValueObjectForFrameVariable(const VariableSP & variable_sp,DynamicValueType use_dynamic)11390b57cec5SDimitry Andric StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
11400b57cec5SDimitry Andric DynamicValueType use_dynamic) {
11410b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
11420b57cec5SDimitry Andric ValueObjectSP valobj_sp;
11430b57cec5SDimitry Andric if (IsHistorical()) {
11440b57cec5SDimitry Andric return valobj_sp;
11450b57cec5SDimitry Andric }
11460b57cec5SDimitry Andric VariableList *var_list = GetVariableList(true);
11470b57cec5SDimitry Andric if (var_list) {
11480b57cec5SDimitry Andric // Make sure the variable is a frame variable
11490b57cec5SDimitry Andric const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
11500b57cec5SDimitry Andric const uint32_t num_variables = var_list->GetSize();
11510b57cec5SDimitry Andric if (var_idx < num_variables) {
11520b57cec5SDimitry Andric valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
11530b57cec5SDimitry Andric if (!valobj_sp) {
11540b57cec5SDimitry Andric if (m_variable_list_value_objects.GetSize() < num_variables)
11550b57cec5SDimitry Andric m_variable_list_value_objects.Resize(num_variables);
11560b57cec5SDimitry Andric valobj_sp = ValueObjectVariable::Create(this, variable_sp);
11570b57cec5SDimitry Andric m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
11580b57cec5SDimitry Andric }
11590b57cec5SDimitry Andric }
11600b57cec5SDimitry Andric }
11610b57cec5SDimitry Andric if (use_dynamic != eNoDynamicValues && valobj_sp) {
11620b57cec5SDimitry Andric ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
11630b57cec5SDimitry Andric if (dynamic_sp)
11640b57cec5SDimitry Andric return dynamic_sp;
11650b57cec5SDimitry Andric }
11660b57cec5SDimitry Andric return valobj_sp;
11670b57cec5SDimitry Andric }
11680b57cec5SDimitry Andric
IsInlined()11690b57cec5SDimitry Andric bool StackFrame::IsInlined() {
11700b57cec5SDimitry Andric if (m_sc.block == nullptr)
11710b57cec5SDimitry Andric GetSymbolContext(eSymbolContextBlock);
11720b57cec5SDimitry Andric if (m_sc.block)
11730b57cec5SDimitry Andric return m_sc.block->GetContainingInlinedBlock() != nullptr;
11740b57cec5SDimitry Andric return false;
11750b57cec5SDimitry Andric }
11760b57cec5SDimitry Andric
IsHistorical() const11770b57cec5SDimitry Andric bool StackFrame::IsHistorical() const {
11780b57cec5SDimitry Andric return m_stack_frame_kind == StackFrame::Kind::History;
11790b57cec5SDimitry Andric }
11800b57cec5SDimitry Andric
IsArtificial() const11810b57cec5SDimitry Andric bool StackFrame::IsArtificial() const {
11820b57cec5SDimitry Andric return m_stack_frame_kind == StackFrame::Kind::Artificial;
11830b57cec5SDimitry Andric }
11840b57cec5SDimitry Andric
GetLanguage()11850b57cec5SDimitry Andric lldb::LanguageType StackFrame::GetLanguage() {
11860b57cec5SDimitry Andric CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
11870b57cec5SDimitry Andric if (cu)
11880b57cec5SDimitry Andric return cu->GetLanguage();
11890b57cec5SDimitry Andric return lldb::eLanguageTypeUnknown;
11900b57cec5SDimitry Andric }
11910b57cec5SDimitry Andric
GuessLanguage()11920b57cec5SDimitry Andric lldb::LanguageType StackFrame::GuessLanguage() {
11930b57cec5SDimitry Andric LanguageType lang_type = GetLanguage();
11940b57cec5SDimitry Andric
11950b57cec5SDimitry Andric if (lang_type == eLanguageTypeUnknown) {
11960b57cec5SDimitry Andric SymbolContext sc = GetSymbolContext(eSymbolContextFunction
11970b57cec5SDimitry Andric | eSymbolContextSymbol);
11980b57cec5SDimitry Andric if (sc.function) {
11990b57cec5SDimitry Andric lang_type = sc.function->GetMangled().GuessLanguage();
12000b57cec5SDimitry Andric }
12010b57cec5SDimitry Andric else if (sc.symbol)
12020b57cec5SDimitry Andric {
12030b57cec5SDimitry Andric lang_type = sc.symbol->GetMangled().GuessLanguage();
12040b57cec5SDimitry Andric }
12050b57cec5SDimitry Andric }
12060b57cec5SDimitry Andric
12070b57cec5SDimitry Andric return lang_type;
12080b57cec5SDimitry Andric }
12090b57cec5SDimitry Andric
12100b57cec5SDimitry Andric namespace {
12110b57cec5SDimitry Andric std::pair<const Instruction::Operand *, int64_t>
GetBaseExplainingValue(const Instruction::Operand & operand,RegisterContext & register_context,lldb::addr_t value)12120b57cec5SDimitry Andric GetBaseExplainingValue(const Instruction::Operand &operand,
12130b57cec5SDimitry Andric RegisterContext ®ister_context, lldb::addr_t value) {
12140b57cec5SDimitry Andric switch (operand.m_type) {
12150b57cec5SDimitry Andric case Instruction::Operand::Type::Dereference:
12160b57cec5SDimitry Andric case Instruction::Operand::Type::Immediate:
12170b57cec5SDimitry Andric case Instruction::Operand::Type::Invalid:
12180b57cec5SDimitry Andric case Instruction::Operand::Type::Product:
12190b57cec5SDimitry Andric // These are not currently interesting
12200b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12210b57cec5SDimitry Andric case Instruction::Operand::Type::Sum: {
12220b57cec5SDimitry Andric const Instruction::Operand *immediate_child = nullptr;
12230b57cec5SDimitry Andric const Instruction::Operand *variable_child = nullptr;
12240b57cec5SDimitry Andric if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
12250b57cec5SDimitry Andric immediate_child = &operand.m_children[0];
12260b57cec5SDimitry Andric variable_child = &operand.m_children[1];
12270b57cec5SDimitry Andric } else if (operand.m_children[1].m_type ==
12280b57cec5SDimitry Andric Instruction::Operand::Type::Immediate) {
12290b57cec5SDimitry Andric immediate_child = &operand.m_children[1];
12300b57cec5SDimitry Andric variable_child = &operand.m_children[0];
12310b57cec5SDimitry Andric }
12320b57cec5SDimitry Andric if (!immediate_child) {
12330b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12340b57cec5SDimitry Andric }
12350b57cec5SDimitry Andric lldb::addr_t adjusted_value = value;
12360b57cec5SDimitry Andric if (immediate_child->m_negative) {
12370b57cec5SDimitry Andric adjusted_value += immediate_child->m_immediate;
12380b57cec5SDimitry Andric } else {
12390b57cec5SDimitry Andric adjusted_value -= immediate_child->m_immediate;
12400b57cec5SDimitry Andric }
12410b57cec5SDimitry Andric std::pair<const Instruction::Operand *, int64_t> base_and_offset =
12420b57cec5SDimitry Andric GetBaseExplainingValue(*variable_child, register_context,
12430b57cec5SDimitry Andric adjusted_value);
12440b57cec5SDimitry Andric if (!base_and_offset.first) {
12450b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12460b57cec5SDimitry Andric }
12470b57cec5SDimitry Andric if (immediate_child->m_negative) {
12480b57cec5SDimitry Andric base_and_offset.second -= immediate_child->m_immediate;
12490b57cec5SDimitry Andric } else {
12500b57cec5SDimitry Andric base_and_offset.second += immediate_child->m_immediate;
12510b57cec5SDimitry Andric }
12520b57cec5SDimitry Andric return base_and_offset;
12530b57cec5SDimitry Andric }
12540b57cec5SDimitry Andric case Instruction::Operand::Type::Register: {
12550b57cec5SDimitry Andric const RegisterInfo *info =
12560b57cec5SDimitry Andric register_context.GetRegisterInfoByName(operand.m_register.AsCString());
12570b57cec5SDimitry Andric if (!info) {
12580b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12590b57cec5SDimitry Andric }
12600b57cec5SDimitry Andric RegisterValue reg_value;
12610b57cec5SDimitry Andric if (!register_context.ReadRegister(info, reg_value)) {
12620b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12630b57cec5SDimitry Andric }
12640b57cec5SDimitry Andric if (reg_value.GetAsUInt64() == value) {
12650b57cec5SDimitry Andric return std::make_pair(&operand, 0);
12660b57cec5SDimitry Andric } else {
12670b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12680b57cec5SDimitry Andric }
12690b57cec5SDimitry Andric }
12700b57cec5SDimitry Andric }
12710b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12720b57cec5SDimitry Andric }
12730b57cec5SDimitry Andric
12740b57cec5SDimitry Andric std::pair<const Instruction::Operand *, int64_t>
GetBaseExplainingDereference(const Instruction::Operand & operand,RegisterContext & register_context,lldb::addr_t addr)12750b57cec5SDimitry Andric GetBaseExplainingDereference(const Instruction::Operand &operand,
12760b57cec5SDimitry Andric RegisterContext ®ister_context,
12770b57cec5SDimitry Andric lldb::addr_t addr) {
12780b57cec5SDimitry Andric if (operand.m_type == Instruction::Operand::Type::Dereference) {
12790b57cec5SDimitry Andric return GetBaseExplainingValue(operand.m_children[0], register_context,
12800b57cec5SDimitry Andric addr);
12810b57cec5SDimitry Andric }
12820b57cec5SDimitry Andric return std::make_pair(nullptr, 0);
12830b57cec5SDimitry Andric }
12840b57cec5SDimitry Andric }
12850b57cec5SDimitry Andric
GuessValueForAddress(lldb::addr_t addr)12860b57cec5SDimitry Andric lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
12870b57cec5SDimitry Andric TargetSP target_sp = CalculateTarget();
12880b57cec5SDimitry Andric
12890b57cec5SDimitry Andric const ArchSpec &target_arch = target_sp->GetArchitecture();
12900b57cec5SDimitry Andric
12910b57cec5SDimitry Andric AddressRange pc_range;
12920b57cec5SDimitry Andric pc_range.GetBaseAddress() = GetFrameCodeAddress();
12930b57cec5SDimitry Andric pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
12940b57cec5SDimitry Andric
12950b57cec5SDimitry Andric const char *plugin_name = nullptr;
12960b57cec5SDimitry Andric const char *flavor = nullptr;
1297*5f7ddb14SDimitry Andric const bool force_live_memory = true;
12980b57cec5SDimitry Andric
12995ffd83dbSDimitry Andric DisassemblerSP disassembler_sp =
13005ffd83dbSDimitry Andric Disassembler::DisassembleRange(target_arch, plugin_name, flavor,
1301*5f7ddb14SDimitry Andric *target_sp, pc_range, force_live_memory);
13020b57cec5SDimitry Andric
13030b57cec5SDimitry Andric if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
13040b57cec5SDimitry Andric return ValueObjectSP();
13050b57cec5SDimitry Andric }
13060b57cec5SDimitry Andric
13070b57cec5SDimitry Andric InstructionSP instruction_sp =
13080b57cec5SDimitry Andric disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
13090b57cec5SDimitry Andric
13100b57cec5SDimitry Andric llvm::SmallVector<Instruction::Operand, 3> operands;
13110b57cec5SDimitry Andric
13120b57cec5SDimitry Andric if (!instruction_sp->ParseOperands(operands)) {
13130b57cec5SDimitry Andric return ValueObjectSP();
13140b57cec5SDimitry Andric }
13150b57cec5SDimitry Andric
13160b57cec5SDimitry Andric RegisterContextSP register_context_sp = GetRegisterContext();
13170b57cec5SDimitry Andric
13180b57cec5SDimitry Andric if (!register_context_sp) {
13190b57cec5SDimitry Andric return ValueObjectSP();
13200b57cec5SDimitry Andric }
13210b57cec5SDimitry Andric
13220b57cec5SDimitry Andric for (const Instruction::Operand &operand : operands) {
13230b57cec5SDimitry Andric std::pair<const Instruction::Operand *, int64_t> base_and_offset =
13240b57cec5SDimitry Andric GetBaseExplainingDereference(operand, *register_context_sp, addr);
13250b57cec5SDimitry Andric
13260b57cec5SDimitry Andric if (!base_and_offset.first) {
13270b57cec5SDimitry Andric continue;
13280b57cec5SDimitry Andric }
13290b57cec5SDimitry Andric
13300b57cec5SDimitry Andric switch (base_and_offset.first->m_type) {
13310b57cec5SDimitry Andric case Instruction::Operand::Type::Immediate: {
13320b57cec5SDimitry Andric lldb_private::Address addr;
13330b57cec5SDimitry Andric if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
13340b57cec5SDimitry Andric base_and_offset.second,
13350b57cec5SDimitry Andric addr)) {
13369dba64beSDimitry Andric auto c_type_system_or_err =
13379dba64beSDimitry Andric target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
13389dba64beSDimitry Andric if (auto err = c_type_system_or_err.takeError()) {
13399dba64beSDimitry Andric LLDB_LOG_ERROR(
13409dba64beSDimitry Andric lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD),
13419dba64beSDimitry Andric std::move(err), "Unable to guess value for given address");
13420b57cec5SDimitry Andric return ValueObjectSP();
13430b57cec5SDimitry Andric } else {
13440b57cec5SDimitry Andric CompilerType void_ptr_type =
13459dba64beSDimitry Andric c_type_system_or_err
13460b57cec5SDimitry Andric ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
13470b57cec5SDimitry Andric .GetPointerType();
13480b57cec5SDimitry Andric return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
13490b57cec5SDimitry Andric }
13500b57cec5SDimitry Andric } else {
13510b57cec5SDimitry Andric return ValueObjectSP();
13520b57cec5SDimitry Andric }
13530b57cec5SDimitry Andric break;
13540b57cec5SDimitry Andric }
13550b57cec5SDimitry Andric case Instruction::Operand::Type::Register: {
13560b57cec5SDimitry Andric return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
13570b57cec5SDimitry Andric base_and_offset.second);
13580b57cec5SDimitry Andric }
13590b57cec5SDimitry Andric default:
13600b57cec5SDimitry Andric return ValueObjectSP();
13610b57cec5SDimitry Andric }
13620b57cec5SDimitry Andric }
13630b57cec5SDimitry Andric
13640b57cec5SDimitry Andric return ValueObjectSP();
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric
13670b57cec5SDimitry Andric namespace {
GetValueForOffset(StackFrame & frame,ValueObjectSP & parent,int64_t offset)13680b57cec5SDimitry Andric ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
13690b57cec5SDimitry Andric int64_t offset) {
13700b57cec5SDimitry Andric if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
13710b57cec5SDimitry Andric return ValueObjectSP();
13720b57cec5SDimitry Andric }
13730b57cec5SDimitry Andric
13740b57cec5SDimitry Andric if (parent->IsPointerOrReferenceType()) {
13750b57cec5SDimitry Andric return parent;
13760b57cec5SDimitry Andric }
13770b57cec5SDimitry Andric
13780b57cec5SDimitry Andric for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
13790b57cec5SDimitry Andric const bool can_create = true;
13800b57cec5SDimitry Andric ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
13810b57cec5SDimitry Andric
13820b57cec5SDimitry Andric if (!child_sp) {
13830b57cec5SDimitry Andric return ValueObjectSP();
13840b57cec5SDimitry Andric }
13850b57cec5SDimitry Andric
13860b57cec5SDimitry Andric int64_t child_offset = child_sp->GetByteOffset();
1387af732203SDimitry Andric int64_t child_size = child_sp->GetByteSize().getValueOr(0);
13880b57cec5SDimitry Andric
13890b57cec5SDimitry Andric if (offset >= child_offset && offset < (child_offset + child_size)) {
13900b57cec5SDimitry Andric return GetValueForOffset(frame, child_sp, offset - child_offset);
13910b57cec5SDimitry Andric }
13920b57cec5SDimitry Andric }
13930b57cec5SDimitry Andric
13940b57cec5SDimitry Andric if (offset == 0) {
13950b57cec5SDimitry Andric return parent;
13960b57cec5SDimitry Andric } else {
13970b57cec5SDimitry Andric return ValueObjectSP();
13980b57cec5SDimitry Andric }
13990b57cec5SDimitry Andric }
14000b57cec5SDimitry Andric
GetValueForDereferincingOffset(StackFrame & frame,ValueObjectSP & base,int64_t offset)14010b57cec5SDimitry Andric ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
14020b57cec5SDimitry Andric ValueObjectSP &base,
14030b57cec5SDimitry Andric int64_t offset) {
14040b57cec5SDimitry Andric // base is a pointer to something
14050b57cec5SDimitry Andric // offset is the thing to add to the pointer We return the most sensible
14060b57cec5SDimitry Andric // ValueObject for the result of *(base+offset)
14070b57cec5SDimitry Andric
14080b57cec5SDimitry Andric if (!base->IsPointerOrReferenceType()) {
14090b57cec5SDimitry Andric return ValueObjectSP();
14100b57cec5SDimitry Andric }
14110b57cec5SDimitry Andric
14120b57cec5SDimitry Andric Status error;
14130b57cec5SDimitry Andric ValueObjectSP pointee = base->Dereference(error);
14140b57cec5SDimitry Andric
14150b57cec5SDimitry Andric if (!pointee) {
14160b57cec5SDimitry Andric return ValueObjectSP();
14170b57cec5SDimitry Andric }
14180b57cec5SDimitry Andric
14190b57cec5SDimitry Andric if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
1420af732203SDimitry Andric int64_t index = offset / pointee->GetByteSize().getValueOr(1);
1421af732203SDimitry Andric offset = offset % pointee->GetByteSize().getValueOr(1);
14220b57cec5SDimitry Andric const bool can_create = true;
14230b57cec5SDimitry Andric pointee = base->GetSyntheticArrayMember(index, can_create);
14240b57cec5SDimitry Andric }
14250b57cec5SDimitry Andric
14260b57cec5SDimitry Andric if (!pointee || error.Fail()) {
14270b57cec5SDimitry Andric return ValueObjectSP();
14280b57cec5SDimitry Andric }
14290b57cec5SDimitry Andric
14300b57cec5SDimitry Andric return GetValueForOffset(frame, pointee, offset);
14310b57cec5SDimitry Andric }
14320b57cec5SDimitry Andric
14330b57cec5SDimitry Andric /// Attempt to reconstruct the ValueObject for the address contained in a
14340b57cec5SDimitry Andric /// given register plus an offset.
14350b57cec5SDimitry Andric ///
14360b57cec5SDimitry Andric /// \params [in] frame
14370b57cec5SDimitry Andric /// The current stack frame.
14380b57cec5SDimitry Andric ///
14390b57cec5SDimitry Andric /// \params [in] reg
14400b57cec5SDimitry Andric /// The register.
14410b57cec5SDimitry Andric ///
14420b57cec5SDimitry Andric /// \params [in] offset
14430b57cec5SDimitry Andric /// The offset from the register.
14440b57cec5SDimitry Andric ///
14450b57cec5SDimitry Andric /// \param [in] disassembler
14460b57cec5SDimitry Andric /// A disassembler containing instructions valid up to the current PC.
14470b57cec5SDimitry Andric ///
14480b57cec5SDimitry Andric /// \param [in] variables
14490b57cec5SDimitry Andric /// The variable list from the current frame,
14500b57cec5SDimitry Andric ///
14510b57cec5SDimitry Andric /// \param [in] pc
14520b57cec5SDimitry Andric /// The program counter for the instruction considered the 'user'.
14530b57cec5SDimitry Andric ///
14540b57cec5SDimitry Andric /// \return
14550b57cec5SDimitry Andric /// A string describing the base for the ExpressionPath. This could be a
14560b57cec5SDimitry Andric /// variable, a register value, an argument, or a function return value.
14570b57cec5SDimitry Andric /// The ValueObject if found. If valid, it has a valid ExpressionPath.
DoGuessValueAt(StackFrame & frame,ConstString reg,int64_t offset,Disassembler & disassembler,VariableList & variables,const Address & pc)14580b57cec5SDimitry Andric lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
14590b57cec5SDimitry Andric int64_t offset, Disassembler &disassembler,
14600b57cec5SDimitry Andric VariableList &variables, const Address &pc) {
14610b57cec5SDimitry Andric // Example of operation for Intel:
14620b57cec5SDimitry Andric //
14630b57cec5SDimitry Andric // +14: movq -0x8(%rbp), %rdi
14640b57cec5SDimitry Andric // +18: movq 0x8(%rdi), %rdi
14650b57cec5SDimitry Andric // +22: addl 0x4(%rdi), %eax
14660b57cec5SDimitry Andric //
14670b57cec5SDimitry Andric // f, a pointer to a struct, is known to be at -0x8(%rbp).
14680b57cec5SDimitry Andric //
14690b57cec5SDimitry Andric // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
14700b57cec5SDimitry Andric // +18 that assigns to rdi, and calls itself recursively for that dereference
14710b57cec5SDimitry Andric // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
14720b57cec5SDimitry Andric // +14 that assigns to rdi, and calls itself recursively for that
14735ffd83dbSDimitry Andric // dereference
14740b57cec5SDimitry Andric // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
14750b57cec5SDimitry Andric // variable list.
14760b57cec5SDimitry Andric // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
14770b57cec5SDimitry Andric // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
14780b57cec5SDimitry Andric // at +18)
14790b57cec5SDimitry Andric // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
14800b57cec5SDimitry Andric // rdi+4 at +22)
14810b57cec5SDimitry Andric
14820b57cec5SDimitry Andric // First, check the variable list to see if anything is at the specified
14830b57cec5SDimitry Andric // location.
14840b57cec5SDimitry Andric
14850b57cec5SDimitry Andric using namespace OperandMatchers;
14860b57cec5SDimitry Andric
14870b57cec5SDimitry Andric const RegisterInfo *reg_info =
14880b57cec5SDimitry Andric frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
14890b57cec5SDimitry Andric if (!reg_info) {
14900b57cec5SDimitry Andric return ValueObjectSP();
14910b57cec5SDimitry Andric }
14920b57cec5SDimitry Andric
14930b57cec5SDimitry Andric Instruction::Operand op =
14940b57cec5SDimitry Andric offset ? Instruction::Operand::BuildDereference(
14950b57cec5SDimitry Andric Instruction::Operand::BuildSum(
14960b57cec5SDimitry Andric Instruction::Operand::BuildRegister(reg),
14970b57cec5SDimitry Andric Instruction::Operand::BuildImmediate(offset)))
14980b57cec5SDimitry Andric : Instruction::Operand::BuildDereference(
14990b57cec5SDimitry Andric Instruction::Operand::BuildRegister(reg));
15000b57cec5SDimitry Andric
1501480093f4SDimitry Andric for (VariableSP var_sp : variables) {
1502480093f4SDimitry Andric if (var_sp->LocationExpression().MatchesOperand(frame, op))
15030b57cec5SDimitry Andric return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
15040b57cec5SDimitry Andric }
15050b57cec5SDimitry Andric
15060b57cec5SDimitry Andric const uint32_t current_inst =
15070b57cec5SDimitry Andric disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
15080b57cec5SDimitry Andric if (current_inst == UINT32_MAX) {
15090b57cec5SDimitry Andric return ValueObjectSP();
15100b57cec5SDimitry Andric }
15110b57cec5SDimitry Andric
15120b57cec5SDimitry Andric for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
15130b57cec5SDimitry Andric // This is not an exact algorithm, and it sacrifices accuracy for
15140b57cec5SDimitry Andric // generality. Recognizing "mov" and "ld" instructions –– and which
15150b57cec5SDimitry Andric // are their source and destination operands -- is something the
15160b57cec5SDimitry Andric // disassembler should do for us.
15170b57cec5SDimitry Andric InstructionSP instruction_sp =
15180b57cec5SDimitry Andric disassembler.GetInstructionList().GetInstructionAtIndex(ii);
15190b57cec5SDimitry Andric
15200b57cec5SDimitry Andric if (instruction_sp->IsCall()) {
15210b57cec5SDimitry Andric ABISP abi_sp = frame.CalculateProcess()->GetABI();
15220b57cec5SDimitry Andric if (!abi_sp) {
15230b57cec5SDimitry Andric continue;
15240b57cec5SDimitry Andric }
15250b57cec5SDimitry Andric
15260b57cec5SDimitry Andric const char *return_register_name;
15270b57cec5SDimitry Andric if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
15280b57cec5SDimitry Andric continue;
15290b57cec5SDimitry Andric }
15300b57cec5SDimitry Andric
15310b57cec5SDimitry Andric const RegisterInfo *return_register_info =
15320b57cec5SDimitry Andric frame.GetRegisterContext()->GetRegisterInfoByName(
15330b57cec5SDimitry Andric return_register_name);
15340b57cec5SDimitry Andric if (!return_register_info) {
15350b57cec5SDimitry Andric continue;
15360b57cec5SDimitry Andric }
15370b57cec5SDimitry Andric
15380b57cec5SDimitry Andric int64_t offset = 0;
15390b57cec5SDimitry Andric
15400b57cec5SDimitry Andric if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
15410b57cec5SDimitry Andric MatchRegOp(*return_register_info))(op) &&
15420b57cec5SDimitry Andric !MatchUnaryOp(
15430b57cec5SDimitry Andric MatchOpType(Instruction::Operand::Type::Dereference),
15440b57cec5SDimitry Andric MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
15450b57cec5SDimitry Andric MatchRegOp(*return_register_info),
15460b57cec5SDimitry Andric FetchImmOp(offset)))(op)) {
15470b57cec5SDimitry Andric continue;
15480b57cec5SDimitry Andric }
15490b57cec5SDimitry Andric
15500b57cec5SDimitry Andric llvm::SmallVector<Instruction::Operand, 1> operands;
15510b57cec5SDimitry Andric if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
15520b57cec5SDimitry Andric continue;
15530b57cec5SDimitry Andric }
15540b57cec5SDimitry Andric
15550b57cec5SDimitry Andric switch (operands[0].m_type) {
15560b57cec5SDimitry Andric default:
15570b57cec5SDimitry Andric break;
15580b57cec5SDimitry Andric case Instruction::Operand::Type::Immediate: {
15590b57cec5SDimitry Andric SymbolContext sc;
15600b57cec5SDimitry Andric Address load_address;
15610b57cec5SDimitry Andric if (!frame.CalculateTarget()->ResolveLoadAddress(
15620b57cec5SDimitry Andric operands[0].m_immediate, load_address)) {
15630b57cec5SDimitry Andric break;
15640b57cec5SDimitry Andric }
15650b57cec5SDimitry Andric frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
15660b57cec5SDimitry Andric load_address, eSymbolContextFunction, sc);
15670b57cec5SDimitry Andric if (!sc.function) {
15680b57cec5SDimitry Andric break;
15690b57cec5SDimitry Andric }
15700b57cec5SDimitry Andric CompilerType function_type = sc.function->GetCompilerType();
15710b57cec5SDimitry Andric if (!function_type.IsFunctionType()) {
15720b57cec5SDimitry Andric break;
15730b57cec5SDimitry Andric }
15740b57cec5SDimitry Andric CompilerType return_type = function_type.GetFunctionReturnType();
15750b57cec5SDimitry Andric RegisterValue return_value;
15760b57cec5SDimitry Andric if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
15770b57cec5SDimitry Andric return_value)) {
15780b57cec5SDimitry Andric break;
15790b57cec5SDimitry Andric }
15800b57cec5SDimitry Andric std::string name_str(
15810b57cec5SDimitry Andric sc.function->GetName().AsCString("<unknown function>"));
15820b57cec5SDimitry Andric name_str.append("()");
15830b57cec5SDimitry Andric Address return_value_address(return_value.GetAsUInt64());
15840b57cec5SDimitry Andric ValueObjectSP return_value_sp = ValueObjectMemory::Create(
15850b57cec5SDimitry Andric &frame, name_str, return_value_address, return_type);
15860b57cec5SDimitry Andric return GetValueForDereferincingOffset(frame, return_value_sp, offset);
15870b57cec5SDimitry Andric }
15880b57cec5SDimitry Andric }
15890b57cec5SDimitry Andric
15900b57cec5SDimitry Andric continue;
15910b57cec5SDimitry Andric }
15920b57cec5SDimitry Andric
15930b57cec5SDimitry Andric llvm::SmallVector<Instruction::Operand, 2> operands;
15940b57cec5SDimitry Andric if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
15950b57cec5SDimitry Andric continue;
15960b57cec5SDimitry Andric }
15970b57cec5SDimitry Andric
15980b57cec5SDimitry Andric Instruction::Operand *origin_operand = nullptr;
15990b57cec5SDimitry Andric auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
16000b57cec5SDimitry Andric return MatchRegOp(*reg_info)(op) && op.m_clobbered;
16010b57cec5SDimitry Andric };
16020b57cec5SDimitry Andric
16030b57cec5SDimitry Andric if (clobbered_reg_matcher(operands[0])) {
16040b57cec5SDimitry Andric origin_operand = &operands[1];
16050b57cec5SDimitry Andric }
16060b57cec5SDimitry Andric else if (clobbered_reg_matcher(operands[1])) {
16070b57cec5SDimitry Andric origin_operand = &operands[0];
16080b57cec5SDimitry Andric }
16090b57cec5SDimitry Andric else {
16100b57cec5SDimitry Andric continue;
16110b57cec5SDimitry Andric }
16120b57cec5SDimitry Andric
16130b57cec5SDimitry Andric // We have an origin operand. Can we track its value down?
16140b57cec5SDimitry Andric ValueObjectSP source_path;
16150b57cec5SDimitry Andric ConstString origin_register;
16160b57cec5SDimitry Andric int64_t origin_offset = 0;
16170b57cec5SDimitry Andric
16180b57cec5SDimitry Andric if (FetchRegOp(origin_register)(*origin_operand)) {
16190b57cec5SDimitry Andric source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
16200b57cec5SDimitry Andric variables, instruction_sp->GetAddress());
16210b57cec5SDimitry Andric } else if (MatchUnaryOp(
16220b57cec5SDimitry Andric MatchOpType(Instruction::Operand::Type::Dereference),
16230b57cec5SDimitry Andric FetchRegOp(origin_register))(*origin_operand) ||
16240b57cec5SDimitry Andric MatchUnaryOp(
16250b57cec5SDimitry Andric MatchOpType(Instruction::Operand::Type::Dereference),
16260b57cec5SDimitry Andric MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
16270b57cec5SDimitry Andric FetchRegOp(origin_register),
16280b57cec5SDimitry Andric FetchImmOp(origin_offset)))(*origin_operand)) {
16290b57cec5SDimitry Andric source_path =
16300b57cec5SDimitry Andric DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
16310b57cec5SDimitry Andric variables, instruction_sp->GetAddress());
16320b57cec5SDimitry Andric if (!source_path) {
16330b57cec5SDimitry Andric continue;
16340b57cec5SDimitry Andric }
16350b57cec5SDimitry Andric source_path =
16360b57cec5SDimitry Andric GetValueForDereferincingOffset(frame, source_path, offset);
16370b57cec5SDimitry Andric }
16380b57cec5SDimitry Andric
16390b57cec5SDimitry Andric if (source_path) {
16400b57cec5SDimitry Andric return source_path;
16410b57cec5SDimitry Andric }
16420b57cec5SDimitry Andric }
16430b57cec5SDimitry Andric
16440b57cec5SDimitry Andric return ValueObjectSP();
16450b57cec5SDimitry Andric }
16460b57cec5SDimitry Andric }
16470b57cec5SDimitry Andric
GuessValueForRegisterAndOffset(ConstString reg,int64_t offset)16480b57cec5SDimitry Andric lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
16490b57cec5SDimitry Andric int64_t offset) {
16500b57cec5SDimitry Andric TargetSP target_sp = CalculateTarget();
16510b57cec5SDimitry Andric
16520b57cec5SDimitry Andric const ArchSpec &target_arch = target_sp->GetArchitecture();
16530b57cec5SDimitry Andric
16540b57cec5SDimitry Andric Block *frame_block = GetFrameBlock();
16550b57cec5SDimitry Andric
16560b57cec5SDimitry Andric if (!frame_block) {
16570b57cec5SDimitry Andric return ValueObjectSP();
16580b57cec5SDimitry Andric }
16590b57cec5SDimitry Andric
16600b57cec5SDimitry Andric Function *function = frame_block->CalculateSymbolContextFunction();
16610b57cec5SDimitry Andric if (!function) {
16620b57cec5SDimitry Andric return ValueObjectSP();
16630b57cec5SDimitry Andric }
16640b57cec5SDimitry Andric
16650b57cec5SDimitry Andric AddressRange pc_range = function->GetAddressRange();
16660b57cec5SDimitry Andric
16670b57cec5SDimitry Andric if (GetFrameCodeAddress().GetFileAddress() <
16680b57cec5SDimitry Andric pc_range.GetBaseAddress().GetFileAddress() ||
16690b57cec5SDimitry Andric GetFrameCodeAddress().GetFileAddress() -
16700b57cec5SDimitry Andric pc_range.GetBaseAddress().GetFileAddress() >=
16710b57cec5SDimitry Andric pc_range.GetByteSize()) {
16720b57cec5SDimitry Andric return ValueObjectSP();
16730b57cec5SDimitry Andric }
16740b57cec5SDimitry Andric
16750b57cec5SDimitry Andric const char *plugin_name = nullptr;
16760b57cec5SDimitry Andric const char *flavor = nullptr;
1677*5f7ddb14SDimitry Andric const bool force_live_memory = true;
16785ffd83dbSDimitry Andric DisassemblerSP disassembler_sp =
16795ffd83dbSDimitry Andric Disassembler::DisassembleRange(target_arch, plugin_name, flavor,
1680*5f7ddb14SDimitry Andric *target_sp, pc_range, force_live_memory);
16810b57cec5SDimitry Andric
16820b57cec5SDimitry Andric if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
16830b57cec5SDimitry Andric return ValueObjectSP();
16840b57cec5SDimitry Andric }
16850b57cec5SDimitry Andric
16860b57cec5SDimitry Andric const bool get_file_globals = false;
16870b57cec5SDimitry Andric VariableList *variables = GetVariableList(get_file_globals);
16880b57cec5SDimitry Andric
16890b57cec5SDimitry Andric if (!variables) {
16900b57cec5SDimitry Andric return ValueObjectSP();
16910b57cec5SDimitry Andric }
16920b57cec5SDimitry Andric
16930b57cec5SDimitry Andric return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
16940b57cec5SDimitry Andric GetFrameCodeAddress());
16950b57cec5SDimitry Andric }
16960b57cec5SDimitry Andric
FindVariable(ConstString name)16970b57cec5SDimitry Andric lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
16980b57cec5SDimitry Andric ValueObjectSP value_sp;
16990b57cec5SDimitry Andric
17000b57cec5SDimitry Andric if (!name)
17010b57cec5SDimitry Andric return value_sp;
17020b57cec5SDimitry Andric
17030b57cec5SDimitry Andric TargetSP target_sp = CalculateTarget();
17040b57cec5SDimitry Andric ProcessSP process_sp = CalculateProcess();
17050b57cec5SDimitry Andric
17060b57cec5SDimitry Andric if (!target_sp && !process_sp)
17070b57cec5SDimitry Andric return value_sp;
17080b57cec5SDimitry Andric
17090b57cec5SDimitry Andric VariableList variable_list;
17100b57cec5SDimitry Andric VariableSP var_sp;
17110b57cec5SDimitry Andric SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
17120b57cec5SDimitry Andric
17130b57cec5SDimitry Andric if (sc.block) {
17140b57cec5SDimitry Andric const bool can_create = true;
17150b57cec5SDimitry Andric const bool get_parent_variables = true;
17160b57cec5SDimitry Andric const bool stop_if_block_is_inlined_function = true;
17170b57cec5SDimitry Andric
17180b57cec5SDimitry Andric if (sc.block->AppendVariables(
17190b57cec5SDimitry Andric can_create, get_parent_variables, stop_if_block_is_inlined_function,
17200b57cec5SDimitry Andric [this](Variable *v) { return v->IsInScope(this); },
17210b57cec5SDimitry Andric &variable_list)) {
17220b57cec5SDimitry Andric var_sp = variable_list.FindVariable(name);
17230b57cec5SDimitry Andric }
17240b57cec5SDimitry Andric
17250b57cec5SDimitry Andric if (var_sp)
17260b57cec5SDimitry Andric value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
17270b57cec5SDimitry Andric }
17280b57cec5SDimitry Andric
17290b57cec5SDimitry Andric return value_sp;
17300b57cec5SDimitry Andric }
17310b57cec5SDimitry Andric
CalculateTarget()17320b57cec5SDimitry Andric TargetSP StackFrame::CalculateTarget() {
17330b57cec5SDimitry Andric TargetSP target_sp;
17340b57cec5SDimitry Andric ThreadSP thread_sp(GetThread());
17350b57cec5SDimitry Andric if (thread_sp) {
17360b57cec5SDimitry Andric ProcessSP process_sp(thread_sp->CalculateProcess());
17370b57cec5SDimitry Andric if (process_sp)
17380b57cec5SDimitry Andric target_sp = process_sp->CalculateTarget();
17390b57cec5SDimitry Andric }
17400b57cec5SDimitry Andric return target_sp;
17410b57cec5SDimitry Andric }
17420b57cec5SDimitry Andric
CalculateProcess()17430b57cec5SDimitry Andric ProcessSP StackFrame::CalculateProcess() {
17440b57cec5SDimitry Andric ProcessSP process_sp;
17450b57cec5SDimitry Andric ThreadSP thread_sp(GetThread());
17460b57cec5SDimitry Andric if (thread_sp)
17470b57cec5SDimitry Andric process_sp = thread_sp->CalculateProcess();
17480b57cec5SDimitry Andric return process_sp;
17490b57cec5SDimitry Andric }
17500b57cec5SDimitry Andric
CalculateThread()17510b57cec5SDimitry Andric ThreadSP StackFrame::CalculateThread() { return GetThread(); }
17520b57cec5SDimitry Andric
CalculateStackFrame()17530b57cec5SDimitry Andric StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
17540b57cec5SDimitry Andric
CalculateExecutionContext(ExecutionContext & exe_ctx)17550b57cec5SDimitry Andric void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
17560b57cec5SDimitry Andric exe_ctx.SetContext(shared_from_this());
17570b57cec5SDimitry Andric }
17580b57cec5SDimitry Andric
DumpUsingSettingsFormat(Stream * strm,bool show_unique,const char * frame_marker)17590b57cec5SDimitry Andric void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
17600b57cec5SDimitry Andric const char *frame_marker) {
17610b57cec5SDimitry Andric if (strm == nullptr)
17620b57cec5SDimitry Andric return;
17630b57cec5SDimitry Andric
17640b57cec5SDimitry Andric GetSymbolContext(eSymbolContextEverything);
17650b57cec5SDimitry Andric ExecutionContext exe_ctx(shared_from_this());
17660b57cec5SDimitry Andric StreamString s;
17670b57cec5SDimitry Andric
17680b57cec5SDimitry Andric if (frame_marker)
17690b57cec5SDimitry Andric s.PutCString(frame_marker);
17700b57cec5SDimitry Andric
17710b57cec5SDimitry Andric const FormatEntity::Entry *frame_format = nullptr;
17720b57cec5SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
17730b57cec5SDimitry Andric if (target) {
17740b57cec5SDimitry Andric if (show_unique) {
17750b57cec5SDimitry Andric frame_format = target->GetDebugger().GetFrameFormatUnique();
17760b57cec5SDimitry Andric } else {
17770b57cec5SDimitry Andric frame_format = target->GetDebugger().GetFrameFormat();
17780b57cec5SDimitry Andric }
17790b57cec5SDimitry Andric }
17800b57cec5SDimitry Andric if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
17810b57cec5SDimitry Andric nullptr, nullptr, false, false)) {
17820b57cec5SDimitry Andric strm->PutCString(s.GetString());
17830b57cec5SDimitry Andric } else {
17840b57cec5SDimitry Andric Dump(strm, true, false);
17850b57cec5SDimitry Andric strm->EOL();
17860b57cec5SDimitry Andric }
17870b57cec5SDimitry Andric }
17880b57cec5SDimitry Andric
Dump(Stream * strm,bool show_frame_index,bool show_fullpaths)17890b57cec5SDimitry Andric void StackFrame::Dump(Stream *strm, bool show_frame_index,
17900b57cec5SDimitry Andric bool show_fullpaths) {
17910b57cec5SDimitry Andric if (strm == nullptr)
17920b57cec5SDimitry Andric return;
17930b57cec5SDimitry Andric
17940b57cec5SDimitry Andric if (show_frame_index)
17950b57cec5SDimitry Andric strm->Printf("frame #%u: ", m_frame_index);
17960b57cec5SDimitry Andric ExecutionContext exe_ctx(shared_from_this());
17970b57cec5SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
17980b57cec5SDimitry Andric strm->Printf("0x%0*" PRIx64 " ",
17990b57cec5SDimitry Andric target ? (target->GetArchitecture().GetAddressByteSize() * 2)
18000b57cec5SDimitry Andric : 16,
18010b57cec5SDimitry Andric GetFrameCodeAddress().GetLoadAddress(target));
18020b57cec5SDimitry Andric GetSymbolContext(eSymbolContextEverything);
18030b57cec5SDimitry Andric const bool show_module = true;
18040b57cec5SDimitry Andric const bool show_inline = true;
18050b57cec5SDimitry Andric const bool show_function_arguments = true;
18060b57cec5SDimitry Andric const bool show_function_name = true;
18070b57cec5SDimitry Andric m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
18080b57cec5SDimitry Andric GetFrameCodeAddress(), show_fullpaths, show_module,
18090b57cec5SDimitry Andric show_inline, show_function_arguments,
18100b57cec5SDimitry Andric show_function_name);
18110b57cec5SDimitry Andric }
18120b57cec5SDimitry Andric
UpdateCurrentFrameFromPreviousFrame(StackFrame & prev_frame)18130b57cec5SDimitry Andric void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
18140b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
18150b57cec5SDimitry Andric assert(GetStackID() ==
18160b57cec5SDimitry Andric prev_frame.GetStackID()); // TODO: remove this after some testing
18170b57cec5SDimitry Andric m_variable_list_sp = prev_frame.m_variable_list_sp;
18180b57cec5SDimitry Andric m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
18190b57cec5SDimitry Andric if (!m_disassembly.GetString().empty()) {
18200b57cec5SDimitry Andric m_disassembly.Clear();
18210b57cec5SDimitry Andric m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
18220b57cec5SDimitry Andric }
18230b57cec5SDimitry Andric }
18240b57cec5SDimitry Andric
UpdatePreviousFrameFromCurrentFrame(StackFrame & curr_frame)18250b57cec5SDimitry Andric void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
18260b57cec5SDimitry Andric std::lock_guard<std::recursive_mutex> guard(m_mutex);
18270b57cec5SDimitry Andric assert(GetStackID() ==
18280b57cec5SDimitry Andric curr_frame.GetStackID()); // TODO: remove this after some testing
18290b57cec5SDimitry Andric m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
18300b57cec5SDimitry Andric assert(GetThread() == curr_frame.GetThread());
18310b57cec5SDimitry Andric m_frame_index = curr_frame.m_frame_index;
18320b57cec5SDimitry Andric m_concrete_frame_index = curr_frame.m_concrete_frame_index;
18330b57cec5SDimitry Andric m_reg_context_sp = curr_frame.m_reg_context_sp;
18340b57cec5SDimitry Andric m_frame_code_addr = curr_frame.m_frame_code_addr;
18355ffd83dbSDimitry Andric m_behaves_like_zeroth_frame = curr_frame.m_behaves_like_zeroth_frame;
18360b57cec5SDimitry Andric assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
18370b57cec5SDimitry Andric m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
18380b57cec5SDimitry Andric assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
18390b57cec5SDimitry Andric m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
18400b57cec5SDimitry Andric assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
18410b57cec5SDimitry Andric m_sc.comp_unit == curr_frame.m_sc.comp_unit);
18420b57cec5SDimitry Andric assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
18430b57cec5SDimitry Andric m_sc.function == curr_frame.m_sc.function);
18440b57cec5SDimitry Andric m_sc = curr_frame.m_sc;
18450b57cec5SDimitry Andric m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
18460b57cec5SDimitry Andric m_flags.Set(m_sc.GetResolvedMask());
18470b57cec5SDimitry Andric m_frame_base.Clear();
18480b57cec5SDimitry Andric m_frame_base_error.Clear();
18490b57cec5SDimitry Andric }
18500b57cec5SDimitry Andric
HasCachedData() const18510b57cec5SDimitry Andric bool StackFrame::HasCachedData() const {
18520b57cec5SDimitry Andric if (m_variable_list_sp)
18530b57cec5SDimitry Andric return true;
18540b57cec5SDimitry Andric if (m_variable_list_value_objects.GetSize() > 0)
18550b57cec5SDimitry Andric return true;
18560b57cec5SDimitry Andric if (!m_disassembly.GetString().empty())
18570b57cec5SDimitry Andric return true;
18580b57cec5SDimitry Andric return false;
18590b57cec5SDimitry Andric }
18600b57cec5SDimitry Andric
GetStatus(Stream & strm,bool show_frame_info,bool show_source,bool show_unique,const char * frame_marker)18610b57cec5SDimitry Andric bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
18620b57cec5SDimitry Andric bool show_unique, const char *frame_marker) {
18630b57cec5SDimitry Andric if (show_frame_info) {
18640b57cec5SDimitry Andric strm.Indent();
18650b57cec5SDimitry Andric DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
18660b57cec5SDimitry Andric }
18670b57cec5SDimitry Andric
18680b57cec5SDimitry Andric if (show_source) {
18690b57cec5SDimitry Andric ExecutionContext exe_ctx(shared_from_this());
18700b57cec5SDimitry Andric bool have_source = false, have_debuginfo = false;
18710b57cec5SDimitry Andric Debugger::StopDisassemblyType disasm_display =
18720b57cec5SDimitry Andric Debugger::eStopDisassemblyTypeNever;
18730b57cec5SDimitry Andric Target *target = exe_ctx.GetTargetPtr();
18740b57cec5SDimitry Andric if (target) {
18750b57cec5SDimitry Andric Debugger &debugger = target->GetDebugger();
18760b57cec5SDimitry Andric const uint32_t source_lines_before =
18770b57cec5SDimitry Andric debugger.GetStopSourceLineCount(true);
18780b57cec5SDimitry Andric const uint32_t source_lines_after =
18790b57cec5SDimitry Andric debugger.GetStopSourceLineCount(false);
18800b57cec5SDimitry Andric disasm_display = debugger.GetStopDisassemblyDisplay();
18810b57cec5SDimitry Andric
18820b57cec5SDimitry Andric GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
18830b57cec5SDimitry Andric if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
18840b57cec5SDimitry Andric have_debuginfo = true;
18850b57cec5SDimitry Andric if (source_lines_before > 0 || source_lines_after > 0) {
18860b57cec5SDimitry Andric size_t num_lines =
18870b57cec5SDimitry Andric target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
18880b57cec5SDimitry Andric m_sc.line_entry.file, m_sc.line_entry.line,
18890b57cec5SDimitry Andric m_sc.line_entry.column, source_lines_before,
18900b57cec5SDimitry Andric source_lines_after, "->", &strm);
18910b57cec5SDimitry Andric if (num_lines != 0)
18920b57cec5SDimitry Andric have_source = true;
18930b57cec5SDimitry Andric // TODO: Give here a one time warning if source file is missing.
18940b57cec5SDimitry Andric }
18950b57cec5SDimitry Andric }
18960b57cec5SDimitry Andric switch (disasm_display) {
18970b57cec5SDimitry Andric case Debugger::eStopDisassemblyTypeNever:
18980b57cec5SDimitry Andric break;
18990b57cec5SDimitry Andric
19000b57cec5SDimitry Andric case Debugger::eStopDisassemblyTypeNoDebugInfo:
19010b57cec5SDimitry Andric if (have_debuginfo)
19020b57cec5SDimitry Andric break;
19030b57cec5SDimitry Andric LLVM_FALLTHROUGH;
19040b57cec5SDimitry Andric
19050b57cec5SDimitry Andric case Debugger::eStopDisassemblyTypeNoSource:
19060b57cec5SDimitry Andric if (have_source)
19070b57cec5SDimitry Andric break;
19080b57cec5SDimitry Andric LLVM_FALLTHROUGH;
19090b57cec5SDimitry Andric
19100b57cec5SDimitry Andric case Debugger::eStopDisassemblyTypeAlways:
19110b57cec5SDimitry Andric if (target) {
19120b57cec5SDimitry Andric const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
19130b57cec5SDimitry Andric if (disasm_lines > 0) {
19140b57cec5SDimitry Andric const ArchSpec &target_arch = target->GetArchitecture();
19150b57cec5SDimitry Andric const char *plugin_name = nullptr;
19160b57cec5SDimitry Andric const char *flavor = nullptr;
19170b57cec5SDimitry Andric const bool mixed_source_and_assembly = false;
19180b57cec5SDimitry Andric Disassembler::Disassemble(
19190b57cec5SDimitry Andric target->GetDebugger(), target_arch, plugin_name, flavor,
19205ffd83dbSDimitry Andric exe_ctx, GetFrameCodeAddress(),
19215ffd83dbSDimitry Andric {Disassembler::Limit::Instructions, disasm_lines},
19225ffd83dbSDimitry Andric mixed_source_and_assembly, 0,
19230b57cec5SDimitry Andric Disassembler::eOptionMarkPCAddress, strm);
19240b57cec5SDimitry Andric }
19250b57cec5SDimitry Andric }
19260b57cec5SDimitry Andric break;
19270b57cec5SDimitry Andric }
19280b57cec5SDimitry Andric }
19290b57cec5SDimitry Andric }
19300b57cec5SDimitry Andric return true;
19310b57cec5SDimitry Andric }
19320b57cec5SDimitry Andric
GetRecognizedFrame()19330b57cec5SDimitry Andric RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
19340b57cec5SDimitry Andric if (!m_recognized_frame_sp) {
1935af732203SDimitry Andric m_recognized_frame_sp = GetThread()
1936af732203SDimitry Andric ->GetProcess()
1937af732203SDimitry Andric ->GetTarget()
1938af732203SDimitry Andric .GetFrameRecognizerManager()
1939af732203SDimitry Andric .RecognizeFrame(CalculateStackFrame());
19400b57cec5SDimitry Andric }
19410b57cec5SDimitry Andric return m_recognized_frame_sp;
19420b57cec5SDimitry Andric }
1943