130fdc8d8SChris Lattner //===-- StackFrame.cpp ------------------------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner #include "lldb/Target/StackFrame.h" 1130fdc8d8SChris Lattner 1230fdc8d8SChris Lattner // C Includes 1330fdc8d8SChris Lattner // C++ Includes 1430fdc8d8SChris Lattner // Other libraries and framework includes 1530fdc8d8SChris Lattner // Project includes 1630fdc8d8SChris Lattner #include "lldb/Core/Module.h" 1730fdc8d8SChris Lattner #include "lldb/Core/Disassembler.h" 1830fdc8d8SChris Lattner #include "lldb/Core/Value.h" 1930fdc8d8SChris Lattner #include "lldb/Symbol/Function.h" 2030fdc8d8SChris Lattner #include "lldb/Target/ExecutionContext.h" 2130fdc8d8SChris Lattner #include "lldb/Target/Process.h" 2230fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h" 2330fdc8d8SChris Lattner #include "lldb/Target/Target.h" 2430fdc8d8SChris Lattner #include "lldb/Target/Thread.h" 2530fdc8d8SChris Lattner 2630fdc8d8SChris Lattner using namespace lldb; 2730fdc8d8SChris Lattner using namespace lldb_private; 2830fdc8d8SChris Lattner 2930fdc8d8SChris Lattner // The first bits in the flags are reserved for the SymbolContext::Scope bits 3030fdc8d8SChris Lattner // so we know if we have tried to look up information in our internal symbol 3130fdc8d8SChris Lattner // context (m_sc) already. 32*59e8fc1cSGreg Clayton #define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1)) 33*59e8fc1cSGreg Clayton #define RESOLVED_FRAME_ID_START_ADDR (RESOLVED_FRAME_CODE_ADDR << 1) 34*59e8fc1cSGreg Clayton #define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_ID_START_ADDR << 1) 35*59e8fc1cSGreg Clayton #define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1) 36*59e8fc1cSGreg Clayton #define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1) 3730fdc8d8SChris Lattner 381b72fcb7SGreg Clayton StackFrame::StackFrame 391b72fcb7SGreg Clayton ( 401b72fcb7SGreg Clayton lldb::user_id_t frame_idx, 41*59e8fc1cSGreg Clayton lldb::user_id_t unwind_frame_index, 421b72fcb7SGreg Clayton Thread &thread, 431b72fcb7SGreg Clayton lldb::addr_t cfa, 441b72fcb7SGreg Clayton lldb::addr_t pc, 451b72fcb7SGreg Clayton const SymbolContext *sc_ptr 461b72fcb7SGreg Clayton ) : 471b72fcb7SGreg Clayton m_frame_index (frame_idx), 48*59e8fc1cSGreg Clayton m_unwind_frame_index (unwind_frame_index), 4930fdc8d8SChris Lattner m_thread (thread), 5030fdc8d8SChris Lattner m_reg_context_sp (), 51*59e8fc1cSGreg Clayton m_id (LLDB_INVALID_ADDRESS, cfa, NULL), 5212fc3e0fSGreg Clayton m_frame_code_addr (NULL, pc), 5330fdc8d8SChris Lattner m_sc (), 5430fdc8d8SChris Lattner m_flags (), 5530fdc8d8SChris Lattner m_frame_base (), 5630fdc8d8SChris Lattner m_frame_base_error (), 5730fdc8d8SChris Lattner m_variable_list_sp (), 5830fdc8d8SChris Lattner m_value_object_list () 5930fdc8d8SChris Lattner { 6030fdc8d8SChris Lattner if (sc_ptr != NULL) 611b72fcb7SGreg Clayton { 6230fdc8d8SChris Lattner m_sc = *sc_ptr; 631b72fcb7SGreg Clayton m_flags.Set(m_sc.GetResolvedMask ()); 641b72fcb7SGreg Clayton } 6530fdc8d8SChris Lattner } 6630fdc8d8SChris Lattner 671b72fcb7SGreg Clayton StackFrame::StackFrame 681b72fcb7SGreg Clayton ( 691b72fcb7SGreg Clayton lldb::user_id_t frame_idx, 70*59e8fc1cSGreg Clayton lldb::user_id_t unwind_frame_index, 711b72fcb7SGreg Clayton Thread &thread, 721b72fcb7SGreg Clayton const RegisterContextSP ®_context_sp, 731b72fcb7SGreg Clayton lldb::addr_t cfa, 741b72fcb7SGreg Clayton lldb::addr_t pc, 751b72fcb7SGreg Clayton const SymbolContext *sc_ptr 761b72fcb7SGreg Clayton ) : 771b72fcb7SGreg Clayton m_frame_index (frame_idx), 78*59e8fc1cSGreg Clayton m_unwind_frame_index (unwind_frame_index), 7930fdc8d8SChris Lattner m_thread (thread), 8030fdc8d8SChris Lattner m_reg_context_sp (reg_context_sp), 81*59e8fc1cSGreg Clayton m_id (LLDB_INVALID_ADDRESS, cfa, NULL), 8212fc3e0fSGreg Clayton m_frame_code_addr (NULL, pc), 8330fdc8d8SChris Lattner m_sc (), 8430fdc8d8SChris Lattner m_flags (), 8530fdc8d8SChris Lattner m_frame_base (), 8630fdc8d8SChris Lattner m_frame_base_error (), 8730fdc8d8SChris Lattner m_variable_list_sp (), 8830fdc8d8SChris Lattner m_value_object_list () 8930fdc8d8SChris Lattner { 9030fdc8d8SChris Lattner if (sc_ptr != NULL) 911b72fcb7SGreg Clayton { 9230fdc8d8SChris Lattner m_sc = *sc_ptr; 931b72fcb7SGreg Clayton m_flags.Set(m_sc.GetResolvedMask ()); 941b72fcb7SGreg Clayton } 951b72fcb7SGreg Clayton 961b72fcb7SGreg Clayton if (reg_context_sp && !m_sc.target_sp) 971b72fcb7SGreg Clayton { 981b72fcb7SGreg Clayton m_sc.target_sp = reg_context_sp->GetThread().GetProcess().GetTarget().GetSP(); 991b72fcb7SGreg Clayton m_flags.Set (eSymbolContextTarget); 1001b72fcb7SGreg Clayton } 1011b72fcb7SGreg Clayton } 1021b72fcb7SGreg Clayton 1031b72fcb7SGreg Clayton StackFrame::StackFrame 1041b72fcb7SGreg Clayton ( 1051b72fcb7SGreg Clayton lldb::user_id_t frame_idx, 106*59e8fc1cSGreg Clayton lldb::user_id_t unwind_frame_index, 1071b72fcb7SGreg Clayton Thread &thread, 1081b72fcb7SGreg Clayton const RegisterContextSP ®_context_sp, 1091b72fcb7SGreg Clayton lldb::addr_t cfa, 1101b72fcb7SGreg Clayton const Address& pc_addr, 1111b72fcb7SGreg Clayton const SymbolContext *sc_ptr 1121b72fcb7SGreg Clayton ) : 1131b72fcb7SGreg Clayton m_frame_index (frame_idx), 114*59e8fc1cSGreg Clayton m_unwind_frame_index (unwind_frame_index), 1151b72fcb7SGreg Clayton m_thread (thread), 1161b72fcb7SGreg Clayton m_reg_context_sp (reg_context_sp), 117*59e8fc1cSGreg Clayton m_id (LLDB_INVALID_ADDRESS, cfa, NULL), 11812fc3e0fSGreg Clayton m_frame_code_addr (pc_addr), 1191b72fcb7SGreg Clayton m_sc (), 1201b72fcb7SGreg Clayton m_flags (), 1211b72fcb7SGreg Clayton m_frame_base (), 1221b72fcb7SGreg Clayton m_frame_base_error (), 1231b72fcb7SGreg Clayton m_variable_list_sp (), 1241b72fcb7SGreg Clayton m_value_object_list () 1251b72fcb7SGreg Clayton { 1261b72fcb7SGreg Clayton if (sc_ptr != NULL) 1271b72fcb7SGreg Clayton { 1281b72fcb7SGreg Clayton m_sc = *sc_ptr; 1291b72fcb7SGreg Clayton m_flags.Set(m_sc.GetResolvedMask ()); 1301b72fcb7SGreg Clayton } 1311b72fcb7SGreg Clayton 1321b72fcb7SGreg Clayton if (m_sc.target_sp.get() == NULL && reg_context_sp) 1331b72fcb7SGreg Clayton { 1341b72fcb7SGreg Clayton m_sc.target_sp = reg_context_sp->GetThread().GetProcess().GetTarget().GetSP(); 1351b72fcb7SGreg Clayton m_flags.Set (eSymbolContextTarget); 1361b72fcb7SGreg Clayton } 1371b72fcb7SGreg Clayton 1381b72fcb7SGreg Clayton if (m_sc.module_sp.get() == NULL && pc_addr.GetSection()) 1391b72fcb7SGreg Clayton { 1401b72fcb7SGreg Clayton Module *pc_module = pc_addr.GetSection()->GetModule(); 1411b72fcb7SGreg Clayton if (pc_module) 1421b72fcb7SGreg Clayton { 1431b72fcb7SGreg Clayton m_sc.module_sp = pc_module->GetSP(); 1441b72fcb7SGreg Clayton m_flags.Set (eSymbolContextModule); 1451b72fcb7SGreg Clayton } 1461b72fcb7SGreg Clayton } 14730fdc8d8SChris Lattner } 14830fdc8d8SChris Lattner 14930fdc8d8SChris Lattner 15030fdc8d8SChris Lattner //---------------------------------------------------------------------- 15130fdc8d8SChris Lattner // Destructor 15230fdc8d8SChris Lattner //---------------------------------------------------------------------- 15330fdc8d8SChris Lattner StackFrame::~StackFrame() 15430fdc8d8SChris Lattner { 15530fdc8d8SChris Lattner } 15630fdc8d8SChris Lattner 15730fdc8d8SChris Lattner StackID& 15830fdc8d8SChris Lattner StackFrame::GetStackID() 15930fdc8d8SChris Lattner { 16012fc3e0fSGreg Clayton // Make sure we have resolved our stack ID's start PC before we give 16112fc3e0fSGreg Clayton // it out to any external clients. This allows us to not have to lookup 16212fc3e0fSGreg Clayton // this information if it is never asked for. 163*59e8fc1cSGreg Clayton if (m_flags.IsClear(RESOLVED_FRAME_ID_START_ADDR)) 16430fdc8d8SChris Lattner { 165*59e8fc1cSGreg Clayton m_flags.Set (RESOLVED_FRAME_ID_START_ADDR); 16630fdc8d8SChris Lattner 167*59e8fc1cSGreg Clayton if (m_id.GetStartAddress() == LLDB_INVALID_ADDRESS) 168*59e8fc1cSGreg Clayton { 16930fdc8d8SChris Lattner // Resolve our PC to section offset if we haven't alreday done so 17030fdc8d8SChris Lattner // and if we don't have a module. The resolved address section will 17130fdc8d8SChris Lattner // contain the module to which it belongs. 172*59e8fc1cSGreg Clayton if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR)) 1739da7bd07SGreg Clayton GetFrameCodeAddress(); 17430fdc8d8SChris Lattner 1759da7bd07SGreg Clayton if (GetSymbolContext (eSymbolContextFunction).function) 17630fdc8d8SChris Lattner { 17712fc3e0fSGreg Clayton m_id.SetStartAddress (m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (&m_thread.GetProcess())); 17830fdc8d8SChris Lattner } 1799da7bd07SGreg Clayton else if (GetSymbolContext (eSymbolContextSymbol).symbol) 18030fdc8d8SChris Lattner { 18130fdc8d8SChris Lattner AddressRange *symbol_range_ptr = m_sc.symbol->GetAddressRangePtr(); 18230fdc8d8SChris Lattner if (symbol_range_ptr) 18312fc3e0fSGreg Clayton m_id.SetStartAddress(symbol_range_ptr->GetBaseAddress().GetLoadAddress (&m_thread.GetProcess())); 18430fdc8d8SChris Lattner } 18512fc3e0fSGreg Clayton 18612fc3e0fSGreg Clayton // We didn't find a function or symbol, just use the frame code address 18712fc3e0fSGreg Clayton // which will be the same as the PC in the frame. 18812fc3e0fSGreg Clayton if (m_id.GetStartAddress() == LLDB_INVALID_ADDRESS) 18912fc3e0fSGreg Clayton m_id.SetStartAddress (m_frame_code_addr.GetLoadAddress (&m_thread.GetProcess())); 19030fdc8d8SChris Lattner } 191*59e8fc1cSGreg Clayton } 192*59e8fc1cSGreg Clayton 193*59e8fc1cSGreg Clayton if (m_flags.IsClear (RESOLVED_FRAME_ID_SYMBOL_SCOPE)) 194*59e8fc1cSGreg Clayton { 195*59e8fc1cSGreg Clayton if (m_id.GetSymbolContextScope ()) 196*59e8fc1cSGreg Clayton { 197*59e8fc1cSGreg Clayton m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE); 198*59e8fc1cSGreg Clayton } 199*59e8fc1cSGreg Clayton else 200*59e8fc1cSGreg Clayton { 201*59e8fc1cSGreg Clayton GetSymbolContext (eSymbolContextFunction | eSymbolContextBlock); 202*59e8fc1cSGreg Clayton 203*59e8fc1cSGreg Clayton if (m_sc.block) 204*59e8fc1cSGreg Clayton { 205*59e8fc1cSGreg Clayton Block *inline_block = m_sc.block->GetContainingInlinedBlock(); 206*59e8fc1cSGreg Clayton if (inline_block) 207*59e8fc1cSGreg Clayton { 208*59e8fc1cSGreg Clayton // Use the block with the inlined function info 209*59e8fc1cSGreg Clayton // as the symbol context since we want this frame 210*59e8fc1cSGreg Clayton // to have only the variables for the inlined function 211*59e8fc1cSGreg Clayton SetSymbolContextScope (inline_block); 212*59e8fc1cSGreg Clayton } 213*59e8fc1cSGreg Clayton else 214*59e8fc1cSGreg Clayton { 215*59e8fc1cSGreg Clayton // This block is not inlined with means it has no 216*59e8fc1cSGreg Clayton // inlined parents either, so we want to use the top 217*59e8fc1cSGreg Clayton // most function block. 218*59e8fc1cSGreg Clayton SetSymbolContextScope (&m_sc.function->GetBlock(false)); 219*59e8fc1cSGreg Clayton } 220*59e8fc1cSGreg Clayton } 221*59e8fc1cSGreg Clayton else 222*59e8fc1cSGreg Clayton { 223*59e8fc1cSGreg Clayton // The current stack frame doesn't have a block. Check to see 224*59e8fc1cSGreg Clayton // if it has a symbol. If it does we will use this as the 225*59e8fc1cSGreg Clayton // symbol scope. It is ok if "m_sc.symbol" is NULL below as 226*59e8fc1cSGreg Clayton // it will set the symbol context to NULL and set the 227*59e8fc1cSGreg Clayton // RESOLVED_FRAME_ID_SYMBOL_SCOPE flag bit. 228*59e8fc1cSGreg Clayton GetSymbolContext (eSymbolContextSymbol); 229*59e8fc1cSGreg Clayton SetSymbolContextScope (m_sc.symbol); 230*59e8fc1cSGreg Clayton } 231*59e8fc1cSGreg Clayton } 232*59e8fc1cSGreg Clayton } 23330fdc8d8SChris Lattner return m_id; 23430fdc8d8SChris Lattner } 23530fdc8d8SChris Lattner 236*59e8fc1cSGreg Clayton void 237*59e8fc1cSGreg Clayton StackFrame::SetSymbolContextScope (SymbolContextScope *symbol_scope) 238*59e8fc1cSGreg Clayton { 239*59e8fc1cSGreg Clayton m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE); 240*59e8fc1cSGreg Clayton m_id.SetSymbolContextScope (symbol_scope); 241*59e8fc1cSGreg Clayton } 242*59e8fc1cSGreg Clayton 24330fdc8d8SChris Lattner Address& 2449da7bd07SGreg Clayton StackFrame::GetFrameCodeAddress() 24530fdc8d8SChris Lattner { 246*59e8fc1cSGreg Clayton if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && !m_frame_code_addr.IsSectionOffset()) 24730fdc8d8SChris Lattner { 248*59e8fc1cSGreg Clayton m_flags.Set (RESOLVED_FRAME_CODE_ADDR); 24930fdc8d8SChris Lattner 25030fdc8d8SChris Lattner // Resolve the PC into a temporary address because if ResolveLoadAddress 25130fdc8d8SChris Lattner // fails to resolve the address, it will clear the address object... 25230fdc8d8SChris Lattner Address resolved_pc; 25312fc3e0fSGreg Clayton if (m_thread.GetProcess().ResolveLoadAddress(m_frame_code_addr.GetOffset(), resolved_pc)) 25430fdc8d8SChris Lattner { 25512fc3e0fSGreg Clayton m_frame_code_addr = resolved_pc; 25612fc3e0fSGreg Clayton const Section *section = m_frame_code_addr.GetSection(); 25730fdc8d8SChris Lattner if (section) 25830fdc8d8SChris Lattner { 25930fdc8d8SChris Lattner Module *module = section->GetModule(); 26030fdc8d8SChris Lattner if (module) 26130fdc8d8SChris Lattner { 26230fdc8d8SChris Lattner m_sc.module_sp = module->GetSP(); 26330fdc8d8SChris Lattner if (m_sc.module_sp) 26430fdc8d8SChris Lattner m_flags.Set(eSymbolContextModule); 26530fdc8d8SChris Lattner } 26630fdc8d8SChris Lattner } 26730fdc8d8SChris Lattner } 26830fdc8d8SChris Lattner } 26912fc3e0fSGreg Clayton return m_frame_code_addr; 27030fdc8d8SChris Lattner } 27130fdc8d8SChris Lattner 27230fdc8d8SChris Lattner void 27330fdc8d8SChris Lattner StackFrame::ChangePC (addr_t pc) 27430fdc8d8SChris Lattner { 27512fc3e0fSGreg Clayton m_frame_code_addr.SetOffset(pc); 27612fc3e0fSGreg Clayton m_frame_code_addr.SetSection(NULL); 27730fdc8d8SChris Lattner m_sc.Clear(); 27830fdc8d8SChris Lattner m_flags.SetAllFlagBits(0); 27930fdc8d8SChris Lattner m_thread.ClearStackFrames (); 28030fdc8d8SChris Lattner } 28130fdc8d8SChris Lattner 28230fdc8d8SChris Lattner const char * 28330fdc8d8SChris Lattner StackFrame::Disassemble () 28430fdc8d8SChris Lattner { 28530fdc8d8SChris Lattner if (m_disassembly.GetSize() == 0) 28630fdc8d8SChris Lattner { 28730fdc8d8SChris Lattner ExecutionContext exe_ctx; 28830fdc8d8SChris Lattner Calculate(exe_ctx); 2896611103cSGreg Clayton Target &target = m_thread.GetProcess().GetTarget(); 2906611103cSGreg Clayton Disassembler::Disassemble (target.GetDebugger(), 2916611103cSGreg Clayton target.GetArchitecture(), 29230fdc8d8SChris Lattner exe_ctx, 29330fdc8d8SChris Lattner 0, 294dda4f7b5SGreg Clayton false, 29530fdc8d8SChris Lattner m_disassembly); 29630fdc8d8SChris Lattner if (m_disassembly.GetSize() == 0) 29730fdc8d8SChris Lattner return NULL; 29830fdc8d8SChris Lattner } 29930fdc8d8SChris Lattner return m_disassembly.GetData(); 30030fdc8d8SChris Lattner } 30130fdc8d8SChris Lattner 30230fdc8d8SChris Lattner //---------------------------------------------------------------------- 30330fdc8d8SChris Lattner // Get the symbol context if we already haven't done so by resolving the 30430fdc8d8SChris Lattner // PC address as much as possible. This way when we pass around a 30530fdc8d8SChris Lattner // StackFrame object, everyone will have as much information as 30630fdc8d8SChris Lattner // possible and no one will ever have to look things up manually. 30730fdc8d8SChris Lattner //---------------------------------------------------------------------- 30830fdc8d8SChris Lattner const SymbolContext& 30930fdc8d8SChris Lattner StackFrame::GetSymbolContext (uint32_t resolve_scope) 31030fdc8d8SChris Lattner { 31130fdc8d8SChris Lattner // Copy our internal symbol context into "sc". 31230fdc8d8SChris Lattner if ((m_flags.GetAllFlagBits() & resolve_scope) != resolve_scope) 31330fdc8d8SChris Lattner { 31430fdc8d8SChris Lattner // Resolve our PC to section offset if we haven't alreday done so 31530fdc8d8SChris Lattner // and if we don't have a module. The resolved address section will 31630fdc8d8SChris Lattner // contain the module to which it belongs 317*59e8fc1cSGreg Clayton if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR)) 3189da7bd07SGreg Clayton GetFrameCodeAddress(); 31930fdc8d8SChris Lattner 32030fdc8d8SChris Lattner // If this is not frame zero, then we need to subtract 1 from the PC 32130fdc8d8SChris Lattner // value when doing address lookups since the PC will be on the 32230fdc8d8SChris Lattner // instruction following the function call instruction... 32330fdc8d8SChris Lattner 3249da7bd07SGreg Clayton Address lookup_addr(GetFrameCodeAddress()); 3251b72fcb7SGreg Clayton if (m_frame_index > 0 && lookup_addr.IsValid()) 32630fdc8d8SChris Lattner { 32730fdc8d8SChris Lattner addr_t offset = lookup_addr.GetOffset(); 32830fdc8d8SChris Lattner if (offset > 0) 32930fdc8d8SChris Lattner lookup_addr.SetOffset(offset - 1); 33030fdc8d8SChris Lattner } 33130fdc8d8SChris Lattner 3329da7bd07SGreg Clayton 3339da7bd07SGreg Clayton uint32_t resolved = 0; 33430fdc8d8SChris Lattner if (m_sc.module_sp) 33530fdc8d8SChris Lattner { 33630fdc8d8SChris Lattner // We have something in our stack frame symbol context, lets check 33730fdc8d8SChris Lattner // if we haven't already tried to lookup one of those things. If we 33830fdc8d8SChris Lattner // haven't then we will do the query. 3391b72fcb7SGreg Clayton 3401b72fcb7SGreg Clayton uint32_t actual_resolve_scope = 0; 3411b72fcb7SGreg Clayton 3421b72fcb7SGreg Clayton if (resolve_scope & eSymbolContextCompUnit) 3431b72fcb7SGreg Clayton { 3441b72fcb7SGreg Clayton if (m_flags.IsClear (eSymbolContextCompUnit)) 3451b72fcb7SGreg Clayton { 3461b72fcb7SGreg Clayton if (m_sc.comp_unit) 3479da7bd07SGreg Clayton resolved |= eSymbolContextCompUnit; 3481b72fcb7SGreg Clayton else 3491b72fcb7SGreg Clayton actual_resolve_scope |= eSymbolContextCompUnit; 3501b72fcb7SGreg Clayton } 3511b72fcb7SGreg Clayton } 3521b72fcb7SGreg Clayton 3531b72fcb7SGreg Clayton if (resolve_scope & eSymbolContextFunction) 3541b72fcb7SGreg Clayton { 3551b72fcb7SGreg Clayton if (m_flags.IsClear (eSymbolContextFunction)) 3561b72fcb7SGreg Clayton { 3571b72fcb7SGreg Clayton if (m_sc.function) 3589da7bd07SGreg Clayton resolved |= eSymbolContextFunction; 3591b72fcb7SGreg Clayton else 3601b72fcb7SGreg Clayton actual_resolve_scope |= eSymbolContextFunction; 3611b72fcb7SGreg Clayton } 3621b72fcb7SGreg Clayton } 3631b72fcb7SGreg Clayton 3641b72fcb7SGreg Clayton if (resolve_scope & eSymbolContextBlock) 3651b72fcb7SGreg Clayton { 3661b72fcb7SGreg Clayton if (m_flags.IsClear (eSymbolContextBlock)) 3671b72fcb7SGreg Clayton { 3681b72fcb7SGreg Clayton if (m_sc.block) 3699da7bd07SGreg Clayton resolved |= eSymbolContextBlock; 3701b72fcb7SGreg Clayton else 3711b72fcb7SGreg Clayton actual_resolve_scope |= eSymbolContextBlock; 3721b72fcb7SGreg Clayton } 3731b72fcb7SGreg Clayton } 3741b72fcb7SGreg Clayton 3751b72fcb7SGreg Clayton if (resolve_scope & eSymbolContextSymbol) 3761b72fcb7SGreg Clayton { 3771b72fcb7SGreg Clayton if (m_flags.IsClear (eSymbolContextSymbol)) 3781b72fcb7SGreg Clayton { 3791b72fcb7SGreg Clayton if (m_sc.symbol) 3809da7bd07SGreg Clayton resolved |= eSymbolContextSymbol; 3811b72fcb7SGreg Clayton else 3821b72fcb7SGreg Clayton actual_resolve_scope |= eSymbolContextSymbol; 3831b72fcb7SGreg Clayton } 3841b72fcb7SGreg Clayton } 3851b72fcb7SGreg Clayton 3861b72fcb7SGreg Clayton if (resolve_scope & eSymbolContextLineEntry) 3871b72fcb7SGreg Clayton { 3881b72fcb7SGreg Clayton if (m_flags.IsClear (eSymbolContextLineEntry)) 3891b72fcb7SGreg Clayton { 3901b72fcb7SGreg Clayton if (m_sc.line_entry.IsValid()) 3919da7bd07SGreg Clayton resolved |= eSymbolContextLineEntry; 3921b72fcb7SGreg Clayton else 3931b72fcb7SGreg Clayton actual_resolve_scope |= eSymbolContextLineEntry; 3941b72fcb7SGreg Clayton } 3951b72fcb7SGreg Clayton } 3961b72fcb7SGreg Clayton 3971b72fcb7SGreg Clayton if (actual_resolve_scope) 39830fdc8d8SChris Lattner { 39930fdc8d8SChris Lattner // We might be resolving less information than what is already 40030fdc8d8SChris Lattner // in our current symbol context so resolve into a temporary 40130fdc8d8SChris Lattner // symbol context "sc" so we don't clear out data we have 40230fdc8d8SChris Lattner // already found in "m_sc" 40330fdc8d8SChris Lattner SymbolContext sc; 40430fdc8d8SChris Lattner // Set flags that indicate what we have tried to resolve 4059da7bd07SGreg Clayton resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc); 4061b72fcb7SGreg Clayton // Only replace what we didn't already have as we may have 4071b72fcb7SGreg Clayton // information for an inlined function scope that won't match 4081b72fcb7SGreg Clayton // what a standard lookup by address would match 4099da7bd07SGreg Clayton if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == NULL) 4109da7bd07SGreg Clayton m_sc.comp_unit = sc.comp_unit; 4119da7bd07SGreg Clayton if ((resolved & eSymbolContextFunction) && m_sc.function == NULL) 4129da7bd07SGreg Clayton m_sc.function = sc.function; 4139da7bd07SGreg Clayton if ((resolved & eSymbolContextBlock) && m_sc.block == NULL) 4149da7bd07SGreg Clayton m_sc.block = sc.block; 4159da7bd07SGreg Clayton if ((resolved & eSymbolContextSymbol) && m_sc.symbol == NULL) 4169da7bd07SGreg Clayton m_sc.symbol = sc.symbol; 4179da7bd07SGreg Clayton if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid()) 4189da7bd07SGreg Clayton m_sc.line_entry = sc.line_entry; 4199da7bd07SGreg Clayton 42030fdc8d8SChris Lattner } 42130fdc8d8SChris Lattner } 42230fdc8d8SChris Lattner else 42330fdc8d8SChris Lattner { 42430fdc8d8SChris Lattner // If we don't have a module, then we can't have the compile unit, 42530fdc8d8SChris Lattner // function, block, line entry or symbol, so we can safely call 42630fdc8d8SChris Lattner // ResolveSymbolContextForAddress with our symbol context member m_sc. 4279da7bd07SGreg Clayton resolved |= m_thread.GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc); 42830fdc8d8SChris Lattner } 42930fdc8d8SChris Lattner 43030fdc8d8SChris Lattner // If the target was requested add that: 43130fdc8d8SChris Lattner if (m_sc.target_sp.get() == NULL) 4329da7bd07SGreg Clayton { 43330fdc8d8SChris Lattner m_sc.target_sp = CalculateProcess()->GetTarget().GetSP(); 4349da7bd07SGreg Clayton if (m_sc.target_sp) 4359da7bd07SGreg Clayton resolved |= eSymbolContextTarget; 4369da7bd07SGreg Clayton } 43730fdc8d8SChris Lattner 43830fdc8d8SChris Lattner // Update our internal flags so we remember what we have tried to locate so 43930fdc8d8SChris Lattner // we don't have to keep trying when more calls to this function are made. 4409da7bd07SGreg Clayton // We might have dug up more information that was requested (for example 4419da7bd07SGreg Clayton // if we were asked to only get the block, we will have gotten the 4429da7bd07SGreg Clayton // compile unit, and function) so set any additional bits that we resolved 4439da7bd07SGreg Clayton m_flags.Set (resolve_scope | resolved); 44430fdc8d8SChris Lattner } 44530fdc8d8SChris Lattner 44630fdc8d8SChris Lattner // Return the symbol context with everything that was possible to resolve 44730fdc8d8SChris Lattner // resolved. 44830fdc8d8SChris Lattner return m_sc; 44930fdc8d8SChris Lattner } 45030fdc8d8SChris Lattner 45130fdc8d8SChris Lattner 45230fdc8d8SChris Lattner VariableList * 45330fdc8d8SChris Lattner StackFrame::GetVariableList () 45430fdc8d8SChris Lattner { 45530fdc8d8SChris Lattner if (m_flags.IsClear(RESOLVED_VARIABLES)) 45630fdc8d8SChris Lattner { 45730fdc8d8SChris Lattner m_flags.Set(RESOLVED_VARIABLES); 45830fdc8d8SChris Lattner 4599da7bd07SGreg Clayton if (GetSymbolContext (eSymbolContextFunction).function) 46030fdc8d8SChris Lattner { 46130fdc8d8SChris Lattner bool get_child_variables = true; 46230fdc8d8SChris Lattner bool can_create = true; 4630b76a2c2SGreg Clayton m_variable_list_sp = m_sc.function->GetBlock (can_create).GetVariableList (get_child_variables, can_create); 46430fdc8d8SChris Lattner } 46530fdc8d8SChris Lattner } 46630fdc8d8SChris Lattner return m_variable_list_sp.get(); 46730fdc8d8SChris Lattner } 46830fdc8d8SChris Lattner 46930fdc8d8SChris Lattner 47030fdc8d8SChris Lattner bool 47130fdc8d8SChris Lattner StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr) 47230fdc8d8SChris Lattner { 47330fdc8d8SChris Lattner if (m_flags.IsClear(GOT_FRAME_BASE)) 47430fdc8d8SChris Lattner { 47530fdc8d8SChris Lattner if (m_sc.function) 47630fdc8d8SChris Lattner { 47730fdc8d8SChris Lattner m_frame_base.Clear(); 47830fdc8d8SChris Lattner m_frame_base_error.Clear(); 47930fdc8d8SChris Lattner 48030fdc8d8SChris Lattner m_flags.Set(GOT_FRAME_BASE); 48130fdc8d8SChris Lattner ExecutionContext exe_ctx (&m_thread.GetProcess(), &m_thread, this); 48230fdc8d8SChris Lattner Value expr_value; 48330fdc8d8SChris Lattner if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx, NULL, NULL, expr_value, &m_frame_base_error) < 0) 48430fdc8d8SChris Lattner { 48530fdc8d8SChris Lattner // We should really have an error if evaluate returns, but in case 48630fdc8d8SChris Lattner // we don't, lets set the error to something at least. 48730fdc8d8SChris Lattner if (m_frame_base_error.Success()) 48830fdc8d8SChris Lattner m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed."); 48930fdc8d8SChris Lattner } 49030fdc8d8SChris Lattner else 49130fdc8d8SChris Lattner { 49230fdc8d8SChris Lattner m_frame_base = expr_value.ResolveValue(&exe_ctx, NULL); 49330fdc8d8SChris Lattner } 49430fdc8d8SChris Lattner } 49530fdc8d8SChris Lattner else 49630fdc8d8SChris Lattner { 49730fdc8d8SChris Lattner m_frame_base_error.SetErrorString ("No function in symbol context."); 49830fdc8d8SChris Lattner } 49930fdc8d8SChris Lattner } 50030fdc8d8SChris Lattner 50130fdc8d8SChris Lattner if (m_frame_base_error.Success()) 50230fdc8d8SChris Lattner frame_base = m_frame_base; 50330fdc8d8SChris Lattner 50430fdc8d8SChris Lattner if (error_ptr) 50530fdc8d8SChris Lattner *error_ptr = m_frame_base_error; 50630fdc8d8SChris Lattner return m_frame_base_error.Success(); 50730fdc8d8SChris Lattner } 50830fdc8d8SChris Lattner 50930fdc8d8SChris Lattner RegisterContext * 51030fdc8d8SChris Lattner StackFrame::GetRegisterContext () 51130fdc8d8SChris Lattner { 51230fdc8d8SChris Lattner if (m_reg_context_sp.get() == NULL) 51330fdc8d8SChris Lattner m_reg_context_sp.reset (m_thread.CreateRegisterContextForFrame (this)); 51430fdc8d8SChris Lattner return m_reg_context_sp.get(); 51530fdc8d8SChris Lattner } 51630fdc8d8SChris Lattner 51730fdc8d8SChris Lattner bool 51830fdc8d8SChris Lattner StackFrame::HasDebugInformation () 51930fdc8d8SChris Lattner { 52030fdc8d8SChris Lattner GetSymbolContext (eSymbolContextLineEntry); 52130fdc8d8SChris Lattner return m_sc.line_entry.IsValid(); 52230fdc8d8SChris Lattner } 52330fdc8d8SChris Lattner 52430fdc8d8SChris Lattner ValueObjectList & 52530fdc8d8SChris Lattner StackFrame::GetValueObjectList() 52630fdc8d8SChris Lattner { 52730fdc8d8SChris Lattner return m_value_object_list; 52830fdc8d8SChris Lattner } 52930fdc8d8SChris Lattner 5306b8379c4SJim Ingham bool 5316b8379c4SJim Ingham StackFrame::IsInlined () 5326b8379c4SJim Ingham { 533*59e8fc1cSGreg Clayton if (m_sc.block == NULL) 534*59e8fc1cSGreg Clayton GetSymbolContext (eSymbolContextBlock); 535*59e8fc1cSGreg Clayton if (m_sc.block) 536*59e8fc1cSGreg Clayton return m_sc.block->GetContainingInlinedBlock() != NULL; 537*59e8fc1cSGreg Clayton return false; 5386b8379c4SJim Ingham } 5396b8379c4SJim Ingham 54030fdc8d8SChris Lattner Target * 54130fdc8d8SChris Lattner StackFrame::CalculateTarget () 54230fdc8d8SChris Lattner { 54330fdc8d8SChris Lattner return m_thread.CalculateTarget(); 54430fdc8d8SChris Lattner } 54530fdc8d8SChris Lattner 54630fdc8d8SChris Lattner Process * 54730fdc8d8SChris Lattner StackFrame::CalculateProcess () 54830fdc8d8SChris Lattner { 54930fdc8d8SChris Lattner return m_thread.CalculateProcess(); 55030fdc8d8SChris Lattner } 55130fdc8d8SChris Lattner 55230fdc8d8SChris Lattner Thread * 55330fdc8d8SChris Lattner StackFrame::CalculateThread () 55430fdc8d8SChris Lattner { 55530fdc8d8SChris Lattner return &m_thread; 55630fdc8d8SChris Lattner } 55730fdc8d8SChris Lattner 55830fdc8d8SChris Lattner StackFrame * 55930fdc8d8SChris Lattner StackFrame::CalculateStackFrame () 56030fdc8d8SChris Lattner { 56130fdc8d8SChris Lattner return this; 56230fdc8d8SChris Lattner } 56330fdc8d8SChris Lattner 56430fdc8d8SChris Lattner 56530fdc8d8SChris Lattner void 56630fdc8d8SChris Lattner StackFrame::Calculate (ExecutionContext &exe_ctx) 56730fdc8d8SChris Lattner { 56830fdc8d8SChris Lattner m_thread.Calculate (exe_ctx); 56930fdc8d8SChris Lattner exe_ctx.frame = this; 57030fdc8d8SChris Lattner } 57130fdc8d8SChris Lattner 57230fdc8d8SChris Lattner void 57330fdc8d8SChris Lattner StackFrame::Dump (Stream *strm, bool show_frame_index) 57430fdc8d8SChris Lattner { 57530fdc8d8SChris Lattner if (strm == NULL) 57630fdc8d8SChris Lattner return; 57730fdc8d8SChris Lattner 57830fdc8d8SChris Lattner if (show_frame_index) 5791b72fcb7SGreg Clayton strm->Printf("frame #%u: ", m_frame_index); 5809da7bd07SGreg Clayton strm->Printf("0x%0*llx", m_thread.GetProcess().GetAddressByteSize() * 2, GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess())); 5819da7bd07SGreg Clayton GetSymbolContext(eSymbolContextEverything); 58230fdc8d8SChris Lattner strm->PutCString(", where = "); 5831b72fcb7SGreg Clayton // TODO: need to get the 5841b72fcb7SGreg Clayton const bool show_module = true; 5851b72fcb7SGreg Clayton const bool show_inline = true; 5869da7bd07SGreg Clayton m_sc.DumpStopContext(strm, &m_thread.GetProcess(), GetFrameCodeAddress(), show_module, show_inline); 58730fdc8d8SChris Lattner } 58830fdc8d8SChris Lattner 5895082c5fdSGreg Clayton void 590*59e8fc1cSGreg Clayton StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame) 5915082c5fdSGreg Clayton { 592*59e8fc1cSGreg Clayton assert (GetStackID() == prev_frame.GetStackID()); // TODO: remove this after some testing 593*59e8fc1cSGreg Clayton m_variable_list_sp = prev_frame.m_variable_list_sp; 594*59e8fc1cSGreg Clayton m_value_object_list.Swap (prev_frame.m_value_object_list); 59568275d5eSGreg Clayton if (!m_disassembly.GetString().empty()) 59668275d5eSGreg Clayton m_disassembly.GetString().swap (m_disassembly.GetString()); 5975082c5fdSGreg Clayton } 59868275d5eSGreg Clayton 59968275d5eSGreg Clayton 600*59e8fc1cSGreg Clayton void 601*59e8fc1cSGreg Clayton StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame) 602*59e8fc1cSGreg Clayton { 603*59e8fc1cSGreg Clayton assert (GetStackID() == curr_frame.GetStackID()); // TODO: remove this after some testing 604*59e8fc1cSGreg Clayton assert (&m_thread == &curr_frame.m_thread); 605*59e8fc1cSGreg Clayton m_frame_index = curr_frame.m_frame_index; 606*59e8fc1cSGreg Clayton m_unwind_frame_index = curr_frame.m_unwind_frame_index; 607*59e8fc1cSGreg Clayton m_reg_context_sp = curr_frame.m_reg_context_sp; 608*59e8fc1cSGreg Clayton m_frame_code_addr = curr_frame.m_frame_code_addr; 609*59e8fc1cSGreg Clayton assert (m_sc.target_sp.get() == NULL || curr_frame.m_sc.target_sp.get() == NULL || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get()); 610*59e8fc1cSGreg Clayton assert (m_sc.module_sp.get() == NULL || curr_frame.m_sc.module_sp.get() == NULL || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get()); 611*59e8fc1cSGreg Clayton assert (m_sc.comp_unit == NULL || curr_frame.m_sc.comp_unit == NULL || m_sc.comp_unit == curr_frame.m_sc.comp_unit); 612*59e8fc1cSGreg Clayton assert (m_sc.function == NULL || curr_frame.m_sc.function == NULL || m_sc.function == curr_frame.m_sc.function); 613*59e8fc1cSGreg Clayton assert (m_sc.symbol == NULL || curr_frame.m_sc.symbol == NULL || m_sc.symbol == curr_frame.m_sc.symbol); 614*59e8fc1cSGreg Clayton m_sc = curr_frame.m_sc; 615*59e8fc1cSGreg Clayton m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything); 616*59e8fc1cSGreg Clayton m_flags.Set (m_sc.GetResolvedMask()); 617*59e8fc1cSGreg Clayton m_frame_base.Clear(); 618*59e8fc1cSGreg Clayton m_frame_base_error.Clear(); 619*59e8fc1cSGreg Clayton } 620*59e8fc1cSGreg Clayton 621*59e8fc1cSGreg Clayton 622