Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 1 | //===-- StackFrame.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 | [diff] [blame] | 10 | #include "lldb/Target/StackFrame.h" |
Greg Clayton | 0603aa9 | 2010-10-04 01:05:56 | [diff] [blame] | 11 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 12 | #include "lldb/Core/Disassembler.h" |
Greg Clayton | 554f68d | 2015-02-04 22:00:53 | [diff] [blame] | 13 | #include "lldb/Core/FormatEntity.h" |
Enrico Granata | 592afe7 | 2016-03-15 21:50:51 | [diff] [blame] | 14 | #include "lldb/Core/Mangled.h" |
| 15 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 16 | #include "lldb/Core/Value.h" |
Greg Clayton | 54979cd | 2010-12-15 05:08:08 | [diff] [blame] | 17 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 18 | #include "lldb/Core/ValueObjectMemory.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 19 | #include "lldb/Core/ValueObjectVariable.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 | [diff] [blame] | 20 | #include "lldb/Symbol/CompileUnit.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 21 | #include "lldb/Symbol/Function.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 | [diff] [blame] | 22 | #include "lldb/Symbol/Symbol.h" |
| 23 | #include "lldb/Symbol/SymbolContextScope.h" |
Enrico Granata | 4625239 | 2015-11-19 22:28:58 | [diff] [blame] | 24 | #include "lldb/Symbol/Type.h" |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 | [diff] [blame] | 25 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 26 | #include "lldb/Target/ABI.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 27 | #include "lldb/Target/ExecutionContext.h" |
| 28 | #include "lldb/Target/Process.h" |
| 29 | #include "lldb/Target/RegisterContext.h" |
Kuba Mracek | 41ae8e7 | 2018-10-31 04:00:22 | [diff] [blame] | 30 | #include "lldb/Target/StackFrameRecognizer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 31 | #include "lldb/Target/Target.h" |
| 32 | #include "lldb/Target/Thread.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 | [diff] [blame] | 33 | #include "lldb/Utility/RegisterValue.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 34 | |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 | [diff] [blame] | 35 | #include "lldb/lldb-enumerations.h" |
| 36 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 37 | using namespace lldb; |
| 38 | using namespace lldb_private; |
| 39 | |
| 40 | // The first bits in the flags are reserved for the SymbolContext::Scope bits |
| 41 | // so we know if we have tried to look up information in our internal symbol |
| 42 | // context (m_sc) already. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 43 | #define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1)) |
| 44 | #define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1) |
| 45 | #define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1) |
| 46 | #define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1) |
| 47 | #define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 48 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 49 | StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, |
| 50 | user_id_t unwind_frame_index, addr_t cfa, |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 51 | bool cfa_is_valid, addr_t pc, StackFrame::Kind kind, |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 | [diff] [blame] | 52 | const SymbolContext *sc_ptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 53 | : m_thread_wp(thread_sp), m_frame_index(frame_idx), |
| 54 | m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(), |
| 55 | m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(), |
| 56 | m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid), |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 57 | m_stack_frame_kind(kind), m_variable_list_sp(), |
Kuba Mracek | 41ae8e7 | 2018-10-31 04:00:22 | [diff] [blame] | 58 | m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(), |
| 59 | m_mutex() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 60 | // If we don't have a CFA value, use the frame index for our StackID so that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 61 | // recursive functions properly aren't confused with one another on a history |
| 62 | // stack. |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 63 | if (IsHistorical() && !m_cfa_is_valid) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 64 | m_id.SetCFA(m_frame_index); |
| 65 | } |
Jason Molenda | 9961847 | 2013-11-04 11:02:52 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 67 | if (sc_ptr != nullptr) { |
| 68 | m_sc = *sc_ptr; |
| 69 | m_flags.Set(m_sc.GetResolvedMask()); |
| 70 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 73 | StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, |
| 74 | user_id_t unwind_frame_index, |
| 75 | const RegisterContextSP ®_context_sp, addr_t cfa, |
| 76 | addr_t pc, const SymbolContext *sc_ptr) |
| 77 | : m_thread_wp(thread_sp), m_frame_index(frame_idx), |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 | [diff] [blame] | 78 | m_concrete_frame_index(unwind_frame_index), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 79 | m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr), |
| 80 | m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(), |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 81 | m_frame_base_error(), m_cfa_is_valid(true), |
| 82 | m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(), |
Kuba Mracek | 41ae8e7 | 2018-10-31 04:00:22 | [diff] [blame] | 83 | m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(), |
| 84 | m_mutex() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 85 | if (sc_ptr != nullptr) { |
| 86 | m_sc = *sc_ptr; |
| 87 | m_flags.Set(m_sc.GetResolvedMask()); |
| 88 | } |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 | [diff] [blame] | 89 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 90 | if (reg_context_sp && !m_sc.target_sp) { |
| 91 | m_sc.target_sp = reg_context_sp->CalculateTarget(); |
| 92 | if (m_sc.target_sp) |
| 93 | m_flags.Set(eSymbolContextTarget); |
| 94 | } |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame] | 95 | } |
| 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 97 | StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, |
| 98 | user_id_t unwind_frame_index, |
| 99 | const RegisterContextSP ®_context_sp, addr_t cfa, |
| 100 | const Address &pc_addr, const SymbolContext *sc_ptr) |
| 101 | : m_thread_wp(thread_sp), m_frame_index(frame_idx), |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 | [diff] [blame] | 102 | m_concrete_frame_index(unwind_frame_index), |
| 103 | m_reg_context_sp(reg_context_sp), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 104 | m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa, |
| 105 | nullptr), |
| 106 | m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(), |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 107 | m_frame_base_error(), m_cfa_is_valid(true), |
| 108 | m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(), |
Kuba Mracek | 41ae8e7 | 2018-10-31 04:00:22 | [diff] [blame] | 109 | m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(), |
| 110 | m_mutex() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 111 | if (sc_ptr != nullptr) { |
| 112 | m_sc = *sc_ptr; |
| 113 | m_flags.Set(m_sc.GetResolvedMask()); |
| 114 | } |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 116 | if (!m_sc.target_sp && reg_context_sp) { |
| 117 | m_sc.target_sp = reg_context_sp->CalculateTarget(); |
| 118 | if (m_sc.target_sp) |
| 119 | m_flags.Set(eSymbolContextTarget); |
| 120 | } |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 | [diff] [blame] | 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 122 | ModuleSP pc_module_sp(pc_addr.GetModule()); |
| 123 | if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) { |
| 124 | if (pc_module_sp) { |
| 125 | m_sc.module_sp = pc_module_sp; |
| 126 | m_flags.Set(eSymbolContextModule); |
| 127 | } else { |
| 128 | m_sc.module_sp.reset(); |
Greg Clayton | 1b72fcb | 2010-08-24 00:45:41 | [diff] [blame] | 129 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 130 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 131 | } |
| 132 | |
Eugene Zelenko | d70a6e7 | 2016-02-18 18:52:47 | [diff] [blame] | 133 | StackFrame::~StackFrame() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 135 | StackID &StackFrame::GetStackID() { |
| 136 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 137 | // Make sure we have resolved the StackID object's symbol context scope if we |
| 138 | // already haven't looked it up. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 140 | if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) { |
| 141 | if (m_id.GetSymbolContextScope()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 142 | // We already have a symbol context scope, we just don't have our flag |
| 143 | // bit set. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 144 | m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE); |
| 145 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 146 | // Calculate the frame block and use this for the stack ID symbol context |
| 147 | // scope if we have one. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 148 | SymbolContextScope *scope = GetFrameBlock(); |
| 149 | if (scope == nullptr) { |
| 150 | // We don't have a block, so use the symbol |
| 151 | if (m_flags.IsClear(eSymbolContextSymbol)) |
| 152 | GetSymbolContext(eSymbolContextSymbol); |
| 153 | |
| 154 | // It is ok if m_sc.symbol is nullptr here |
| 155 | scope = m_sc.symbol; |
| 156 | } |
| 157 | // Set the symbol context scope (the accessor will set the |
| 158 | // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags). |
| 159 | SetSymbolContextScope(scope); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 160 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 161 | } |
| 162 | return m_id; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 163 | } |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 165 | uint32_t StackFrame::GetFrameIndex() const { |
| 166 | ThreadSP thread_sp = GetThread(); |
| 167 | if (thread_sp) |
| 168 | return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex( |
| 169 | m_frame_index); |
| 170 | else |
| 171 | return m_frame_index; |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 | [diff] [blame] | 172 | } |
| 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 174 | void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) { |
| 175 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 176 | m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE); |
| 177 | m_id.SetSymbolContextScope(symbol_scope); |
Greg Clayton | 59e8fc1c | 2010-08-30 18:11:35 | [diff] [blame] | 178 | } |
| 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 180 | const Address &StackFrame::GetFrameCodeAddress() { |
| 181 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 182 | if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && |
| 183 | !m_frame_code_addr.IsSectionOffset()) { |
| 184 | m_flags.Set(RESOLVED_FRAME_CODE_ADDR); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 185 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 186 | // Resolve the PC into a temporary address because if ResolveLoadAddress |
| 187 | // fails to resolve the address, it will clear the address object... |
| 188 | ThreadSP thread_sp(GetThread()); |
| 189 | if (thread_sp) { |
| 190 | TargetSP target_sp(thread_sp->CalculateTarget()); |
| 191 | if (target_sp) { |
Pavel Labath | c3c7212 | 2017-06-08 13:26:35 | [diff] [blame] | 192 | const bool allow_section_end = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 193 | if (m_frame_code_addr.SetOpcodeLoadAddress( |
| 194 | m_frame_code_addr.GetOffset(), target_sp.get(), |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 | [diff] [blame] | 195 | AddressClass::eCode, allow_section_end)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 196 | ModuleSP module_sp(m_frame_code_addr.GetModule()); |
| 197 | if (module_sp) { |
| 198 | m_sc.module_sp = module_sp; |
| 199 | m_flags.Set(eSymbolContextModule); |
| 200 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 201 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 202 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 203 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 204 | } |
| 205 | return m_frame_code_addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 208 | bool StackFrame::ChangePC(addr_t pc) { |
| 209 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 210 | // We can't change the pc value of a history stack frame - it is immutable. |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 211 | if (IsHistorical()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 212 | return false; |
| 213 | m_frame_code_addr.SetRawAddress(pc); |
| 214 | m_sc.Clear(false); |
| 215 | m_flags.Reset(0); |
| 216 | ThreadSP thread_sp(GetThread()); |
| 217 | if (thread_sp) |
| 218 | thread_sp->ClearStackFrames(); |
| 219 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 220 | } |
| 221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 222 | const char *StackFrame::Disassemble() { |
| 223 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Zachary Turner | 8b3f2160 | 2017-02-28 17:59:59 | [diff] [blame] | 224 | if (m_disassembly.Empty()) { |
| 225 | ExecutionContext exe_ctx(shared_from_this()); |
| 226 | Target *target = exe_ctx.GetTargetPtr(); |
| 227 | if (target) { |
| 228 | const char *plugin_name = nullptr; |
| 229 | const char *flavor = nullptr; |
| 230 | Disassembler::Disassemble(target->GetDebugger(), |
| 231 | target->GetArchitecture(), plugin_name, flavor, |
| 232 | exe_ctx, 0, false, 0, 0, m_disassembly); |
| 233 | } |
| 234 | if (m_disassembly.Empty()) |
| 235 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 236 | } |
Zachary Turner | 8b3f2160 | 2017-02-28 17:59:59 | [diff] [blame] | 237 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 238 | return m_disassembly.GetData(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 239 | } |
| 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 241 | Block *StackFrame::GetFrameBlock() { |
| 242 | if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock)) |
| 243 | GetSymbolContext(eSymbolContextBlock); |
Greg Clayton | 95897c6 | 2010-09-07 04:20:48 | [diff] [blame] | 244 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 245 | if (m_sc.block) { |
| 246 | Block *inline_block = m_sc.block->GetContainingInlinedBlock(); |
| 247 | if (inline_block) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 248 | // Use the block with the inlined function info as the frame block we |
| 249 | // want this frame to have only the variables for the inlined function |
| 250 | // and its non-inlined block child blocks. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 251 | return inline_block; |
| 252 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 253 | // This block is not contained within any inlined function blocks with so |
| 254 | // we want to use the top most function block. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 255 | return &m_sc.function->GetBlock(false); |
| 256 | } |
| 257 | } |
| 258 | return nullptr; |
Greg Clayton | 95897c6 | 2010-09-07 04:20:48 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 261 | //---------------------------------------------------------------------- |
| 262 | // Get the symbol context if we already haven't done so by resolving the |
| 263 | // PC address as much as possible. This way when we pass around a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 264 | // StackFrame object, everyone will have as much information as possible and no |
| 265 | // one will ever have to look things up manually. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 266 | //---------------------------------------------------------------------- |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 | [diff] [blame] | 267 | const SymbolContext & |
| 268 | StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 269 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 270 | // Copy our internal symbol context into "sc". |
| 271 | if ((m_flags.Get() & resolve_scope) != resolve_scope) { |
| 272 | uint32_t resolved = 0; |
Greg Clayton | 75a0333 | 2012-11-29 00:53:06 | [diff] [blame] | 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 274 | // If the target was requested add that: |
| 275 | if (!m_sc.target_sp) { |
| 276 | m_sc.target_sp = CalculateTarget(); |
| 277 | if (m_sc.target_sp) |
| 278 | resolved |= eSymbolContextTarget; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 279 | } |
| 280 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 281 | // Resolve our PC to section offset if we haven't already done so and if we |
| 282 | // don't have a module. The resolved address section will contain the |
| 283 | // module to which it belongs |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 284 | if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR)) |
| 285 | GetFrameCodeAddress(); |
| 286 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 287 | // If this is not frame zero, then we need to subtract 1 from the PC value |
| 288 | // when doing address lookups since the PC will be on the instruction |
| 289 | // following the function call instruction... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 290 | |
| 291 | Address lookup_addr(GetFrameCodeAddress()); |
| 292 | if (m_frame_index > 0 && lookup_addr.IsValid()) { |
| 293 | addr_t offset = lookup_addr.GetOffset(); |
| 294 | if (offset > 0) { |
| 295 | lookup_addr.SetOffset(offset - 1); |
| 296 | |
| 297 | } else { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 298 | // lookup_addr is the start of a section. We need do the math on the |
| 299 | // actual load address and re-compute the section. We're working with |
| 300 | // a 'noreturn' function at the end of a section. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 301 | ThreadSP thread_sp(GetThread()); |
| 302 | if (thread_sp) { |
| 303 | TargetSP target_sp(thread_sp->CalculateTarget()); |
| 304 | if (target_sp) { |
| 305 | addr_t addr_minus_one = |
| 306 | lookup_addr.GetLoadAddress(target_sp.get()) - 1; |
| 307 | lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get()); |
| 308 | } else { |
| 309 | lookup_addr.SetOffset(offset - 1); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (m_sc.module_sp) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 316 | // We have something in our stack frame symbol context, lets check if we |
| 317 | // haven't already tried to lookup one of those things. If we haven't |
| 318 | // then we will do the query. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 319 | |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 | [diff] [blame] | 320 | SymbolContextItem actual_resolve_scope = SymbolContextItem(0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 321 | |
| 322 | if (resolve_scope & eSymbolContextCompUnit) { |
| 323 | if (m_flags.IsClear(eSymbolContextCompUnit)) { |
| 324 | if (m_sc.comp_unit) |
| 325 | resolved |= eSymbolContextCompUnit; |
| 326 | else |
| 327 | actual_resolve_scope |= eSymbolContextCompUnit; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | if (resolve_scope & eSymbolContextFunction) { |
| 332 | if (m_flags.IsClear(eSymbolContextFunction)) { |
| 333 | if (m_sc.function) |
| 334 | resolved |= eSymbolContextFunction; |
| 335 | else |
| 336 | actual_resolve_scope |= eSymbolContextFunction; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (resolve_scope & eSymbolContextBlock) { |
| 341 | if (m_flags.IsClear(eSymbolContextBlock)) { |
| 342 | if (m_sc.block) |
| 343 | resolved |= eSymbolContextBlock; |
| 344 | else |
| 345 | actual_resolve_scope |= eSymbolContextBlock; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (resolve_scope & eSymbolContextSymbol) { |
| 350 | if (m_flags.IsClear(eSymbolContextSymbol)) { |
| 351 | if (m_sc.symbol) |
| 352 | resolved |= eSymbolContextSymbol; |
| 353 | else |
| 354 | actual_resolve_scope |= eSymbolContextSymbol; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | if (resolve_scope & eSymbolContextLineEntry) { |
| 359 | if (m_flags.IsClear(eSymbolContextLineEntry)) { |
| 360 | if (m_sc.line_entry.IsValid()) |
| 361 | resolved |= eSymbolContextLineEntry; |
| 362 | else |
| 363 | actual_resolve_scope |= eSymbolContextLineEntry; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (actual_resolve_scope) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 368 | // We might be resolving less information than what is already in our |
| 369 | // current symbol context so resolve into a temporary symbol context |
| 370 | // "sc" so we don't clear out data we have already found in "m_sc" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 371 | SymbolContext sc; |
| 372 | // Set flags that indicate what we have tried to resolve |
| 373 | resolved |= m_sc.module_sp->ResolveSymbolContextForAddress( |
| 374 | lookup_addr, actual_resolve_scope, sc); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 375 | // Only replace what we didn't already have as we may have information |
| 376 | // for an inlined function scope that won't match what a standard |
| 377 | // lookup by address would match |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 378 | if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr) |
| 379 | m_sc.comp_unit = sc.comp_unit; |
| 380 | if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr) |
| 381 | m_sc.function = sc.function; |
| 382 | if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr) |
| 383 | m_sc.block = sc.block; |
| 384 | if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr) |
| 385 | m_sc.symbol = sc.symbol; |
| 386 | if ((resolved & eSymbolContextLineEntry) && |
| 387 | !m_sc.line_entry.IsValid()) { |
| 388 | m_sc.line_entry = sc.line_entry; |
| 389 | m_sc.line_entry.ApplyFileMappings(m_sc.target_sp); |
| 390 | } |
| 391 | } |
| 392 | } else { |
| 393 | // If we don't have a module, then we can't have the compile unit, |
| 394 | // function, block, line entry or symbol, so we can safely call |
| 395 | // ResolveSymbolContextForAddress with our symbol context member m_sc. |
| 396 | if (m_sc.target_sp) { |
| 397 | resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress( |
| 398 | lookup_addr, resolve_scope, m_sc); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // Update our internal flags so we remember what we have tried to locate so |
| 403 | // we don't have to keep trying when more calls to this function are made. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 404 | // We might have dug up more information that was requested (for example if |
| 405 | // we were asked to only get the block, we will have gotten the compile |
| 406 | // unit, and function) so set any additional bits that we resolved |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 407 | m_flags.Set(resolve_scope | resolved); |
| 408 | } |
| 409 | |
| 410 | // Return the symbol context with everything that was possible to resolve |
| 411 | // resolved. |
| 412 | return m_sc; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 413 | } |
| 414 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 415 | VariableList *StackFrame::GetVariableList(bool get_file_globals) { |
| 416 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 417 | if (m_flags.IsClear(RESOLVED_VARIABLES)) { |
| 418 | m_flags.Set(RESOLVED_VARIABLES); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 420 | Block *frame_block = GetFrameBlock(); |
| 421 | |
| 422 | if (frame_block) { |
| 423 | const bool get_child_variables = true; |
| 424 | const bool can_create = true; |
| 425 | const bool stop_if_child_block_is_inlined_function = true; |
| 426 | m_variable_list_sp.reset(new VariableList()); |
| 427 | frame_block->AppendBlockVariables(can_create, get_child_variables, |
| 428 | stop_if_child_block_is_inlined_function, |
Zachary Turner | 3bc714b | 2017-03-02 00:05:25 | [diff] [blame] | 429 | [](Variable *v) { return true; }, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 430 | m_variable_list_sp.get()); |
Sean Callanan | 7c0962d | 2010-11-01 04:38:59 | [diff] [blame] | 431 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) { |
| 435 | m_flags.Set(RESOLVED_GLOBAL_VARIABLES); |
| 436 | |
| 437 | if (m_flags.IsClear(eSymbolContextCompUnit)) |
| 438 | GetSymbolContext(eSymbolContextCompUnit); |
| 439 | |
| 440 | if (m_sc.comp_unit) { |
| 441 | VariableListSP global_variable_list_sp( |
| 442 | m_sc.comp_unit->GetVariableList(true)); |
| 443 | if (m_variable_list_sp) |
| 444 | m_variable_list_sp->AddVariables(global_variable_list_sp.get()); |
| 445 | else |
| 446 | m_variable_list_sp = global_variable_list_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 447 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return m_variable_list_sp.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 451 | } |
| 452 | |
Greg Clayton | d41f032 | 2011-08-02 23:35:43 | [diff] [blame] | 453 | VariableListSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 454 | StackFrame::GetInScopeVariableList(bool get_file_globals, |
| 455 | bool must_have_valid_location) { |
| 456 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 457 | // We can't fetch variable information for a history stack frame. |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 458 | if (IsHistorical()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 459 | return VariableListSP(); |
Jason Molenda | 9961847 | 2013-11-04 11:02:52 | [diff] [blame] | 460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 461 | VariableListSP var_list_sp(new VariableList); |
| 462 | GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock); |
Greg Clayton | d41f032 | 2011-08-02 23:35:43 | [diff] [blame] | 463 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 464 | if (m_sc.block) { |
| 465 | const bool can_create = true; |
| 466 | const bool get_parent_variables = true; |
| 467 | const bool stop_if_block_is_inlined_function = true; |
| 468 | m_sc.block->AppendVariables( |
| 469 | can_create, get_parent_variables, stop_if_block_is_inlined_function, |
| 470 | [this, must_have_valid_location](Variable *v) { |
| 471 | return v->IsInScope(this) && (!must_have_valid_location || |
| 472 | v->LocationIsValidForFrame(this)); |
| 473 | }, |
| 474 | var_list_sp.get()); |
| 475 | } |
| 476 | |
| 477 | if (m_sc.comp_unit && get_file_globals) { |
| 478 | VariableListSP global_variable_list_sp( |
| 479 | m_sc.comp_unit->GetVariableList(true)); |
| 480 | if (global_variable_list_sp) |
| 481 | var_list_sp->AddVariables(global_variable_list_sp.get()); |
| 482 | } |
| 483 | |
| 484 | return var_list_sp; |
Greg Clayton | d41f032 | 2011-08-02 23:35:43 | [diff] [blame] | 485 | } |
| 486 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 487 | ValueObjectSP StackFrame::GetValueForVariableExpressionPath( |
Zachary Turner | 0eb31a1 | 2016-11-17 05:14:32 | [diff] [blame] | 488 | llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 489 | VariableSP &var_sp, Status &error) { |
Zachary Turner | 0eb31a1 | 2016-11-17 05:14:32 | [diff] [blame] | 490 | llvm::StringRef original_var_expr = var_expr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 491 | // We can't fetch variable information for a history stack frame. |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 492 | if (IsHistorical()) |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 | [diff] [blame] | 493 | return ValueObjectSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 494 | |
Zachary Turner | 0eb31a1 | 2016-11-17 05:14:32 | [diff] [blame] | 495 | if (var_expr.empty()) { |
| 496 | error.SetErrorStringWithFormat("invalid variable path '%s'", |
| 497 | var_expr.str().c_str()); |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 498 | return ValueObjectSP(); |
| 499 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 500 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 501 | const bool check_ptr_vs_member = |
| 502 | (options & eExpressionPathOptionCheckPtrVsMember) != 0; |
| 503 | const bool no_fragile_ivar = |
| 504 | (options & eExpressionPathOptionsNoFragileObjcIvar) != 0; |
| 505 | const bool no_synth_child = |
| 506 | (options & eExpressionPathOptionsNoSyntheticChildren) != 0; |
| 507 | // const bool no_synth_array = (options & |
| 508 | // eExpressionPathOptionsNoSyntheticArrayRange) != 0; |
| 509 | error.Clear(); |
| 510 | bool deref = false; |
| 511 | bool address_of = false; |
| 512 | ValueObjectSP valobj_sp; |
| 513 | const bool get_file_globals = true; |
| 514 | // When looking up a variable for an expression, we need only consider the |
| 515 | // variables that are in scope. |
| 516 | VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals)); |
| 517 | VariableList *variable_list = var_list_sp.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 518 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 519 | if (!variable_list) |
| 520 | return ValueObjectSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 521 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 522 | // If first character is a '*', then show pointer contents |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 523 | std::string var_expr_storage; |
| 524 | if (var_expr[0] == '*') { |
| 525 | deref = true; |
| 526 | var_expr = var_expr.drop_front(); // Skip the '*' |
| 527 | } else if (var_expr[0] == '&') { |
| 528 | address_of = true; |
| 529 | var_expr = var_expr.drop_front(); // Skip the '&' |
| 530 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 531 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 532 | size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}"); |
| 533 | StreamString var_expr_path_strm; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 534 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 535 | ConstString name_const_string(var_expr.substr(0, separator_idx)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 536 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 537 | var_sp = variable_list->FindVariable(name_const_string, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 538 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 539 | bool synthetically_added_instance_object = false; |
| 540 | |
| 541 | if (var_sp) { |
| 542 | var_expr = var_expr.drop_front(name_const_string.GetLength()); |
| 543 | } |
| 544 | |
| 545 | if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 546 | // Check for direct ivars access which helps us with implicit access to |
| 547 | // ivars with the "this->" or "self->" |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 548 | GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock); |
| 549 | lldb::LanguageType method_language = eLanguageTypeUnknown; |
| 550 | bool is_instance_method = false; |
| 551 | ConstString method_object_name; |
| 552 | if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method, |
| 553 | method_object_name)) { |
| 554 | if (is_instance_method && method_object_name) { |
| 555 | var_sp = variable_list->FindVariable(method_object_name); |
| 556 | if (var_sp) { |
| 557 | separator_idx = 0; |
| 558 | var_expr_storage = "->"; |
| 559 | var_expr_storage += var_expr; |
| 560 | var_expr = var_expr_storage; |
| 561 | synthetically_added_instance_object = true; |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 | [diff] [blame] | 562 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 563 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 564 | } |
| 565 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 566 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 567 | if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) { |
| 568 | // Check if any anonymous unions are there which contain a variable with |
| 569 | // the name we need |
| 570 | for (size_t i = 0; i < variable_list->GetSize(); i++) { |
| 571 | VariableSP variable_sp = variable_list->GetVariableAtIndex(i); |
| 572 | if (!variable_sp) |
| 573 | continue; |
| 574 | if (!variable_sp->GetName().IsEmpty()) |
| 575 | continue; |
| 576 | |
| 577 | Type *var_type = variable_sp->GetType(); |
| 578 | if (!var_type) |
| 579 | continue; |
| 580 | |
| 581 | if (!var_type->GetForwardCompilerType().IsAnonymousType()) |
| 582 | continue; |
| 583 | valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic); |
| 584 | if (!valobj_sp) |
| 585 | return valobj_sp; |
| 586 | valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true); |
| 587 | if (valobj_sp) |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (var_sp && !valobj_sp) { |
| 593 | valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic); |
| 594 | if (!valobj_sp) |
| 595 | return valobj_sp; |
| 596 | } |
| 597 | if (!valobj_sp) { |
| 598 | error.SetErrorStringWithFormat("no variable named '%s' found in this frame", |
| 599 | name_const_string.GetCString()); |
| 600 | return ValueObjectSP(); |
| 601 | } |
| 602 | |
| 603 | // We are dumping at least one child |
| 604 | while (separator_idx != std::string::npos) { |
| 605 | // Calculate the next separator index ahead of time |
| 606 | ValueObjectSP child_valobj_sp; |
| 607 | const char separator_type = var_expr[0]; |
Tamas Berghammer | 4c08fe2 | 2017-03-31 20:23:22 | [diff] [blame] | 608 | bool expr_is_ptr = false; |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 609 | switch (separator_type) { |
| 610 | case '-': |
Tamas Berghammer | 4c08fe2 | 2017-03-31 20:23:22 | [diff] [blame] | 611 | expr_is_ptr = true; |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 612 | if (var_expr.size() >= 2 && var_expr[1] != '>') |
| 613 | return ValueObjectSP(); |
| 614 | |
| 615 | if (no_fragile_ivar) { |
| 616 | // Make sure we aren't trying to deref an objective |
| 617 | // C ivar if this is not allowed |
| 618 | const uint32_t pointer_type_flags = |
| 619 | valobj_sp->GetCompilerType().GetTypeInfo(nullptr); |
| 620 | if ((pointer_type_flags & eTypeIsObjC) && |
| 621 | (pointer_type_flags & eTypeIsPointer)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 622 | // This was an objective C object pointer and it was requested we |
| 623 | // skip any fragile ivars so return nothing here |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 624 | return ValueObjectSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 625 | } |
| 626 | } |
Tamas Berghammer | 4c08fe2 | 2017-03-31 20:23:22 | [diff] [blame] | 627 | |
| 628 | // If we have a non pointer type with a sythetic value then lets check if |
| 629 | // we have an sythetic dereference specified. |
| 630 | if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 631 | Status deref_error; |
Tamas Berghammer | 4c08fe2 | 2017-03-31 20:23:22 | [diff] [blame] | 632 | if (valobj_sp->GetCompilerType().IsReferenceType()) { |
| 633 | valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error); |
| 634 | if (error.Fail()) { |
| 635 | error.SetErrorStringWithFormatv( |
| 636 | "Failed to dereference reference type: %s", deref_error); |
| 637 | return ValueObjectSP(); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | valobj_sp = valobj_sp->Dereference(deref_error); |
| 642 | if (error.Fail()) { |
| 643 | error.SetErrorStringWithFormatv( |
| 644 | "Failed to dereference sythetic value: %s", deref_error); |
| 645 | return ValueObjectSP(); |
| 646 | } |
| 647 | expr_is_ptr = false; |
| 648 | } |
| 649 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 650 | var_expr = var_expr.drop_front(); // Remove the '-' |
| 651 | LLVM_FALLTHROUGH; |
| 652 | case '.': { |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 653 | var_expr = var_expr.drop_front(); // Remove the '.' or '>' |
| 654 | separator_idx = var_expr.find_first_of(".-["); |
| 655 | ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-["))); |
| 656 | |
| 657 | if (check_ptr_vs_member) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 658 | // We either have a pointer type and need to verify valobj_sp is a |
| 659 | // pointer, or we have a member of a class/union/struct being accessed |
| 660 | // with the . syntax and need to verify we don't have a pointer. |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 661 | const bool actual_is_ptr = valobj_sp->IsPointerType(); |
| 662 | |
| 663 | if (actual_is_ptr != expr_is_ptr) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 664 | // Incorrect use of "." with a pointer, or "->" with a |
| 665 | // class/union/struct instance or reference. |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 666 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 667 | if (actual_is_ptr) |
| 668 | error.SetErrorStringWithFormat( |
| 669 | "\"%s\" is a pointer and . was used to attempt to access " |
| 670 | "\"%s\". Did you mean \"%s->%s\"?", |
| 671 | var_expr_path_strm.GetData(), child_name.GetCString(), |
| 672 | var_expr_path_strm.GetData(), var_expr.str().c_str()); |
| 673 | else |
| 674 | error.SetErrorStringWithFormat( |
| 675 | "\"%s\" is not a pointer and -> was used to attempt to " |
| 676 | "access \"%s\". Did you mean \"%s.%s\"?", |
| 677 | var_expr_path_strm.GetData(), child_name.GetCString(), |
| 678 | var_expr_path_strm.GetData(), var_expr.str().c_str()); |
| 679 | return ValueObjectSP(); |
| 680 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 681 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 682 | child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true); |
| 683 | if (!child_valobj_sp) { |
| 684 | if (!no_synth_child) { |
| 685 | child_valobj_sp = valobj_sp->GetSyntheticValue(); |
| 686 | if (child_valobj_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 687 | child_valobj_sp = |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 688 | child_valobj_sp->GetChildMemberWithName(child_name, true); |
| 689 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 690 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 691 | if (no_synth_child || !child_valobj_sp) { |
| 692 | // No child member with name "child_name" |
| 693 | if (synthetically_added_instance_object) { |
| 694 | // We added a "this->" or "self->" to the beginning of the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 695 | // expression and this is the first pointer ivar access, so just |
| 696 | // return the normal error |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 697 | error.SetErrorStringWithFormat( |
| 698 | "no variable or instance variable named '%s' found in " |
| 699 | "this frame", |
| 700 | name_const_string.GetCString()); |
| 701 | } else { |
| 702 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 703 | if (child_name) { |
| 704 | error.SetErrorStringWithFormat( |
| 705 | "\"%s\" is not a member of \"(%s) %s\"", |
| 706 | child_name.GetCString(), |
| 707 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 708 | var_expr_path_strm.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 709 | } else { |
| 710 | error.SetErrorStringWithFormat( |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 711 | "incomplete expression path after \"%s\" in \"%s\"", |
Zachary Turner | 0eb31a1 | 2016-11-17 05:14:32 | [diff] [blame] | 712 | var_expr_path_strm.GetData(), |
| 713 | original_var_expr.str().c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 714 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 715 | } |
| 716 | return ValueObjectSP(); |
| 717 | } |
| 718 | } |
| 719 | synthetically_added_instance_object = false; |
| 720 | // Remove the child name from the path |
| 721 | var_expr = var_expr.drop_front(child_name.GetLength()); |
| 722 | if (use_dynamic != eNoDynamicValues) { |
| 723 | ValueObjectSP dynamic_value_sp( |
| 724 | child_valobj_sp->GetDynamicValue(use_dynamic)); |
| 725 | if (dynamic_value_sp) |
| 726 | child_valobj_sp = dynamic_value_sp; |
| 727 | } |
| 728 | } break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 729 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 730 | case '[': { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 731 | // Array member access, or treating pointer as an array Need at least two |
| 732 | // brackets and a number |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 733 | if (var_expr.size() <= 2) { |
| 734 | error.SetErrorStringWithFormat( |
| 735 | "invalid square bracket encountered after \"%s\" in \"%s\"", |
| 736 | var_expr_path_strm.GetData(), var_expr.str().c_str()); |
| 737 | return ValueObjectSP(); |
| 738 | } |
| 739 | |
| 740 | // Drop the open brace. |
| 741 | var_expr = var_expr.drop_front(); |
| 742 | long child_index = 0; |
| 743 | |
| 744 | // If there's no closing brace, this is an invalid expression. |
| 745 | size_t end_pos = var_expr.find_first_of(']'); |
| 746 | if (end_pos == llvm::StringRef::npos) { |
| 747 | error.SetErrorStringWithFormat( |
| 748 | "missing closing square bracket in expression \"%s\"", |
| 749 | var_expr_path_strm.GetData()); |
| 750 | return ValueObjectSP(); |
| 751 | } |
| 752 | llvm::StringRef index_expr = var_expr.take_front(end_pos); |
| 753 | llvm::StringRef original_index_expr = index_expr; |
| 754 | // Drop all of "[index_expr]" |
| 755 | var_expr = var_expr.drop_front(end_pos + 1); |
| 756 | |
| 757 | if (index_expr.consumeInteger(0, child_index)) { |
| 758 | // If there was no integer anywhere in the index expression, this is |
| 759 | // erroneous expression. |
| 760 | error.SetErrorStringWithFormat("invalid index expression \"%s\"", |
| 761 | index_expr.str().c_str()); |
| 762 | return ValueObjectSP(); |
| 763 | } |
| 764 | |
| 765 | if (index_expr.empty()) { |
| 766 | // The entire index expression was a single integer. |
| 767 | |
| 768 | if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) { |
| 769 | // what we have is *ptr[low]. the most similar C++ syntax is to deref |
| 770 | // ptr and extract bit low out of it. reading array item low would be |
| 771 | // done by saying ptr[low], without a deref * sign |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 772 | Status error; |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 773 | ValueObjectSP temp(valobj_sp->Dereference(error)); |
| 774 | if (error.Fail()) { |
| 775 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 776 | error.SetErrorStringWithFormat( |
| 777 | "could not dereference \"(%s) %s\"", |
| 778 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 779 | var_expr_path_strm.GetData()); |
| 780 | return ValueObjectSP(); |
| 781 | } |
| 782 | valobj_sp = temp; |
| 783 | deref = false; |
| 784 | } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && |
| 785 | deref) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 786 | // what we have is *arr[low]. the most similar C++ syntax is to get |
| 787 | // arr[0] (an operation that is equivalent to deref-ing arr) and |
| 788 | // extract bit low out of it. reading array item low would be done by |
| 789 | // saying arr[low], without a deref * sign |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 790 | Status error; |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 791 | ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true)); |
| 792 | if (error.Fail()) { |
| 793 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 794 | error.SetErrorStringWithFormat( |
| 795 | "could not get item 0 for \"(%s) %s\"", |
| 796 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 797 | var_expr_path_strm.GetData()); |
| 798 | return ValueObjectSP(); |
| 799 | } |
| 800 | valobj_sp = temp; |
| 801 | deref = false; |
| 802 | } |
| 803 | |
| 804 | bool is_incomplete_array = false; |
| 805 | if (valobj_sp->IsPointerType()) { |
| 806 | bool is_objc_pointer = true; |
| 807 | |
| 808 | if (valobj_sp->GetCompilerType().GetMinimumLanguage() != |
| 809 | eLanguageTypeObjC) |
| 810 | is_objc_pointer = false; |
| 811 | else if (!valobj_sp->GetCompilerType().IsPointerType()) |
| 812 | is_objc_pointer = false; |
| 813 | |
| 814 | if (no_synth_child && is_objc_pointer) { |
| 815 | error.SetErrorStringWithFormat( |
| 816 | "\"(%s) %s\" is an Objective-C pointer, and cannot be " |
| 817 | "subscripted", |
| 818 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 819 | var_expr_path_strm.GetData()); |
| 820 | |
| 821 | return ValueObjectSP(); |
| 822 | } else if (is_objc_pointer) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 823 | // dereferencing ObjC variables is not valid.. so let's try and |
| 824 | // recur to synthetic children |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 825 | ValueObjectSP synthetic = valobj_sp->GetSyntheticValue(); |
| 826 | if (!synthetic /* no synthetic */ |
| 827 | || synthetic == valobj_sp) /* synthetic is the same as |
| 828 | the original object */ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 829 | { |
| 830 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 831 | error.SetErrorStringWithFormat( |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 832 | "\"(%s) %s\" is not an array type", |
| 833 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 834 | var_expr_path_strm.GetData()); |
| 835 | } else if ( |
| 836 | static_cast<uint32_t>(child_index) >= |
| 837 | synthetic |
| 838 | ->GetNumChildren() /* synthetic does not have that many values */) { |
| 839 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 840 | error.SetErrorStringWithFormat( |
| 841 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
| 842 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 843 | var_expr_path_strm.GetData()); |
| 844 | } else { |
| 845 | child_valobj_sp = synthetic->GetChildAtIndex(child_index, true); |
| 846 | if (!child_valobj_sp) { |
| 847 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 848 | error.SetErrorStringWithFormat( |
| 849 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
| 850 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 851 | var_expr_path_strm.GetData()); |
| 852 | } |
| 853 | } |
| 854 | } else { |
| 855 | child_valobj_sp = |
| 856 | valobj_sp->GetSyntheticArrayMember(child_index, true); |
| 857 | if (!child_valobj_sp) { |
| 858 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 859 | error.SetErrorStringWithFormat( |
| 860 | "failed to use pointer as array for index %ld for " |
| 861 | "\"(%s) %s\"", |
| 862 | child_index, |
| 863 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 864 | var_expr_path_strm.GetData()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 865 | } |
| 866 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 867 | } else if (valobj_sp->GetCompilerType().IsArrayType( |
| 868 | nullptr, nullptr, &is_incomplete_array)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 869 | // Pass false to dynamic_value here so we can tell the difference |
| 870 | // between no dynamic value and no member of this type... |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 871 | child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true); |
| 872 | if (!child_valobj_sp && (is_incomplete_array || !no_synth_child)) |
| 873 | child_valobj_sp = |
| 874 | valobj_sp->GetSyntheticArrayMember(child_index, true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 875 | |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 876 | if (!child_valobj_sp) { |
| 877 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 878 | error.SetErrorStringWithFormat( |
| 879 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
| 880 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 881 | var_expr_path_strm.GetData()); |
| 882 | } |
| 883 | } else if (valobj_sp->GetCompilerType().IsScalarType()) { |
| 884 | // this is a bitfield asking to display just one bit |
| 885 | child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild( |
| 886 | child_index, child_index, true); |
| 887 | if (!child_valobj_sp) { |
| 888 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 889 | error.SetErrorStringWithFormat( |
| 890 | "bitfield range %ld-%ld is not valid for \"(%s) %s\"", |
| 891 | child_index, child_index, |
| 892 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 893 | var_expr_path_strm.GetData()); |
| 894 | } |
| 895 | } else { |
| 896 | ValueObjectSP synthetic = valobj_sp->GetSyntheticValue(); |
| 897 | if (no_synth_child /* synthetic is forbidden */ || |
| 898 | !synthetic /* no synthetic */ |
| 899 | || synthetic == valobj_sp) /* synthetic is the same as the |
| 900 | original object */ |
| 901 | { |
| 902 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 903 | error.SetErrorStringWithFormat( |
| 904 | "\"(%s) %s\" is not an array type", |
| 905 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 906 | var_expr_path_strm.GetData()); |
| 907 | } else if ( |
| 908 | static_cast<uint32_t>(child_index) >= |
| 909 | synthetic |
| 910 | ->GetNumChildren() /* synthetic does not have that many values */) { |
| 911 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 912 | error.SetErrorStringWithFormat( |
| 913 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
| 914 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 915 | var_expr_path_strm.GetData()); |
| 916 | } else { |
| 917 | child_valobj_sp = synthetic->GetChildAtIndex(child_index, true); |
| 918 | if (!child_valobj_sp) { |
| 919 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 920 | error.SetErrorStringWithFormat( |
| 921 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
| 922 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 923 | var_expr_path_strm.GetData()); |
| 924 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 925 | } |
| 926 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 927 | |
| 928 | if (!child_valobj_sp) { |
| 929 | // Invalid array index... |
| 930 | return ValueObjectSP(); |
| 931 | } |
| 932 | |
| 933 | separator_idx = var_expr.find_first_of(".-["); |
| 934 | if (use_dynamic != eNoDynamicValues) { |
| 935 | ValueObjectSP dynamic_value_sp( |
| 936 | child_valobj_sp->GetDynamicValue(use_dynamic)); |
| 937 | if (dynamic_value_sp) |
| 938 | child_valobj_sp = dynamic_value_sp; |
| 939 | } |
| 940 | // Break out early from the switch since we were able to find the child |
| 941 | // member |
| 942 | break; |
| 943 | } |
| 944 | |
| 945 | // this is most probably a BitField, let's take a look |
| 946 | if (index_expr.front() != '-') { |
| 947 | error.SetErrorStringWithFormat("invalid range expression \"'%s'\"", |
| 948 | original_index_expr.str().c_str()); |
| 949 | return ValueObjectSP(); |
| 950 | } |
| 951 | |
Zachary Turner | 7edc3a6 | 2016-11-21 23:18:13 | [diff] [blame] | 952 | index_expr = index_expr.drop_front(); |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 953 | long final_index = 0; |
| 954 | if (index_expr.getAsInteger(0, final_index)) { |
| 955 | error.SetErrorStringWithFormat("invalid range expression \"'%s'\"", |
| 956 | original_index_expr.str().c_str()); |
| 957 | return ValueObjectSP(); |
| 958 | } |
| 959 | |
| 960 | // if the format given is [high-low], swap range |
| 961 | if (child_index > final_index) { |
| 962 | long temp = child_index; |
| 963 | child_index = final_index; |
| 964 | final_index = temp; |
| 965 | } |
| 966 | |
| 967 | if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) { |
| 968 | // what we have is *ptr[low-high]. the most similar C++ syntax is to |
| 969 | // deref ptr and extract bits low thru high out of it. reading array |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 970 | // items low thru high would be done by saying ptr[low-high], without a |
| 971 | // deref * sign |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 972 | Status error; |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 973 | ValueObjectSP temp(valobj_sp->Dereference(error)); |
| 974 | if (error.Fail()) { |
| 975 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 976 | error.SetErrorStringWithFormat( |
| 977 | "could not dereference \"(%s) %s\"", |
| 978 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 979 | var_expr_path_strm.GetData()); |
| 980 | return ValueObjectSP(); |
| 981 | } |
| 982 | valobj_sp = temp; |
| 983 | deref = false; |
| 984 | } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 985 | // what we have is *arr[low-high]. the most similar C++ syntax is to |
| 986 | // get arr[0] (an operation that is equivalent to deref-ing arr) and |
| 987 | // extract bits low thru high out of it. reading array items low thru |
| 988 | // high would be done by saying arr[low-high], without a deref * sign |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 989 | Status error; |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 990 | ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true)); |
| 991 | if (error.Fail()) { |
| 992 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 993 | error.SetErrorStringWithFormat( |
| 994 | "could not get item 0 for \"(%s) %s\"", |
| 995 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 996 | var_expr_path_strm.GetData()); |
| 997 | return ValueObjectSP(); |
| 998 | } |
| 999 | valobj_sp = temp; |
| 1000 | deref = false; |
| 1001 | } |
| 1002 | |
| 1003 | child_valobj_sp = |
| 1004 | valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true); |
| 1005 | if (!child_valobj_sp) { |
| 1006 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1007 | error.SetErrorStringWithFormat( |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 1008 | "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index, |
| 1009 | final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"), |
| 1010 | var_expr_path_strm.GetData()); |
| 1011 | } |
| 1012 | |
| 1013 | if (!child_valobj_sp) { |
| 1014 | // Invalid bitfield range... |
| 1015 | return ValueObjectSP(); |
| 1016 | } |
| 1017 | |
| 1018 | separator_idx = var_expr.find_first_of(".-["); |
| 1019 | if (use_dynamic != eNoDynamicValues) { |
| 1020 | ValueObjectSP dynamic_value_sp( |
| 1021 | child_valobj_sp->GetDynamicValue(use_dynamic)); |
| 1022 | if (dynamic_value_sp) |
| 1023 | child_valobj_sp = dynamic_value_sp; |
| 1024 | } |
| 1025 | // Break out early from the switch since we were able to find the child |
| 1026 | // member |
| 1027 | break; |
| 1028 | } |
| 1029 | default: |
| 1030 | // Failure... |
| 1031 | { |
| 1032 | valobj_sp->GetExpressionPath(var_expr_path_strm, false); |
| 1033 | error.SetErrorStringWithFormat( |
| 1034 | "unexpected char '%c' encountered after \"%s\" in \"%s\"", |
| 1035 | separator_type, var_expr_path_strm.GetData(), |
| 1036 | var_expr.str().c_str()); |
| 1037 | |
| 1038 | return ValueObjectSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1039 | } |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 | [diff] [blame] | 1040 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 1041 | |
| 1042 | if (child_valobj_sp) |
| 1043 | valobj_sp = child_valobj_sp; |
| 1044 | |
| 1045 | if (var_expr.empty()) |
| 1046 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1047 | } |
Zachary Turner | 24bd317 | 2016-11-17 01:37:52 | [diff] [blame] | 1048 | if (valobj_sp) { |
| 1049 | if (deref) { |
| 1050 | ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error)); |
| 1051 | valobj_sp = deref_valobj_sp; |
| 1052 | } else if (address_of) { |
| 1053 | ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error)); |
| 1054 | valobj_sp = address_of_valobj_sp; |
| 1055 | } |
| 1056 | } |
| 1057 | return valobj_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1058 | } |
| 1059 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 1060 | bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1061 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1062 | if (!m_cfa_is_valid) { |
| 1063 | m_frame_base_error.SetErrorString( |
| 1064 | "No frame base available for this historical stack frame."); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | if (m_flags.IsClear(GOT_FRAME_BASE)) { |
| 1069 | if (m_sc.function) { |
| 1070 | m_frame_base.Clear(); |
| 1071 | m_frame_base_error.Clear(); |
| 1072 | |
| 1073 | m_flags.Set(GOT_FRAME_BASE); |
| 1074 | ExecutionContext exe_ctx(shared_from_this()); |
| 1075 | Value expr_value; |
| 1076 | addr_t loclist_base_addr = LLDB_INVALID_ADDRESS; |
| 1077 | if (m_sc.function->GetFrameBaseExpression().IsLocationList()) |
| 1078 | loclist_base_addr = |
| 1079 | m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress( |
| 1080 | exe_ctx.GetTargetPtr()); |
| 1081 | |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 | [diff] [blame^] | 1082 | if (!m_sc.function->GetFrameBaseExpression().Evaluate( |
Tamas Berghammer | bba2c83 | 2017-08-16 11:45:10 | [diff] [blame] | 1083 | &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr, |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 | [diff] [blame^] | 1084 | expr_value, &m_frame_base_error)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 1085 | // We should really have an error if evaluate returns, but in case we |
| 1086 | // don't, lets set the error to something at least. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1087 | if (m_frame_base_error.Success()) |
| 1088 | m_frame_base_error.SetErrorString( |
| 1089 | "Evaluation of the frame base expression failed."); |
| 1090 | } else { |
| 1091 | m_frame_base = expr_value.ResolveValue(&exe_ctx); |
| 1092 | } |
| 1093 | } else { |
| 1094 | m_frame_base_error.SetErrorString("No function in symbol context."); |
Jim Ingham | 78a685a | 2011-04-16 00:01:13 | [diff] [blame] | 1095 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | if (m_frame_base_error.Success()) |
| 1099 | frame_base = m_frame_base; |
| 1100 | |
| 1101 | if (error_ptr) |
| 1102 | *error_ptr = m_frame_base_error; |
| 1103 | return m_frame_base_error.Success(); |
| 1104 | } |
| 1105 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 1106 | DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1107 | if (!m_sc.function) { |
| 1108 | if (error_ptr) { |
| 1109 | error_ptr->SetErrorString("No function in symbol context."); |
| 1110 | } |
| 1111 | return nullptr; |
| 1112 | } |
| 1113 | |
| 1114 | return &m_sc.function->GetFrameBaseExpression(); |
| 1115 | } |
| 1116 | |
| 1117 | RegisterContextSP StackFrame::GetRegisterContext() { |
| 1118 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1119 | if (!m_reg_context_sp) { |
| 1120 | ThreadSP thread_sp(GetThread()); |
| 1121 | if (thread_sp) |
| 1122 | m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this); |
| 1123 | } |
| 1124 | return m_reg_context_sp; |
| 1125 | } |
| 1126 | |
| 1127 | bool StackFrame::HasDebugInformation() { |
| 1128 | GetSymbolContext(eSymbolContextLineEntry); |
| 1129 | return m_sc.line_entry.IsValid(); |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | ValueObjectSP |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1133 | StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp, |
| 1134 | DynamicValueType use_dynamic) { |
| 1135 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1136 | ValueObjectSP valobj_sp; |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 1137 | if (IsHistorical()) { |
Greg Clayton | 288bdf9 | 2010-09-02 02:59:18 | [diff] [blame] | 1138 | return valobj_sp; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1139 | } |
| 1140 | VariableList *var_list = GetVariableList(true); |
| 1141 | if (var_list) { |
| 1142 | // Make sure the variable is a frame variable |
| 1143 | const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get()); |
| 1144 | const uint32_t num_variables = var_list->GetSize(); |
| 1145 | if (var_idx < num_variables) { |
| 1146 | valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx); |
| 1147 | if (!valobj_sp) { |
| 1148 | if (m_variable_list_value_objects.GetSize() < num_variables) |
| 1149 | m_variable_list_value_objects.Resize(num_variables); |
| 1150 | valobj_sp = ValueObjectVariable::Create(this, variable_sp); |
| 1151 | m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp); |
| 1152 | } |
Enrico Granata | 592afe7 | 2016-03-15 21:50:51 | [diff] [blame] | 1153 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1154 | } |
| 1155 | if (use_dynamic != eNoDynamicValues && valobj_sp) { |
| 1156 | ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic); |
| 1157 | if (dynamic_sp) |
| 1158 | return dynamic_sp; |
| 1159 | } |
| 1160 | return valobj_sp; |
Enrico Granata | 592afe7 | 2016-03-15 21:50:51 | [diff] [blame] | 1161 | } |
| 1162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1163 | ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp, |
| 1164 | DynamicValueType use_dynamic) { |
| 1165 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 1166 | if (IsHistorical()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1167 | return ValueObjectSP(); |
| 1168 | |
| 1169 | // Check to make sure we aren't already tracking this variable? |
| 1170 | ValueObjectSP valobj_sp( |
| 1171 | GetValueObjectForFrameVariable(variable_sp, use_dynamic)); |
| 1172 | if (!valobj_sp) { |
| 1173 | // We aren't already tracking this global |
| 1174 | VariableList *var_list = GetVariableList(true); |
| 1175 | // If this frame has no variables, create a new list |
| 1176 | if (var_list == nullptr) |
| 1177 | m_variable_list_sp.reset(new VariableList()); |
| 1178 | |
| 1179 | // Add the global/static variable to this frame |
| 1180 | m_variable_list_sp->AddVariable(variable_sp); |
| 1181 | |
| 1182 | // Now make a value object for it so we can track its changes |
| 1183 | valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic); |
| 1184 | } |
| 1185 | return valobj_sp; |
| 1186 | } |
| 1187 | |
| 1188 | bool StackFrame::IsInlined() { |
| 1189 | if (m_sc.block == nullptr) |
| 1190 | GetSymbolContext(eSymbolContextBlock); |
| 1191 | if (m_sc.block) |
| 1192 | return m_sc.block->GetContainingInlinedBlock() != nullptr; |
| 1193 | return false; |
| 1194 | } |
| 1195 | |
Vedant Kumar | 4b36f79 | 2018-10-05 23:23:15 | [diff] [blame] | 1196 | bool StackFrame::IsHistorical() const { |
| 1197 | return m_stack_frame_kind == StackFrame::Kind::History; |
| 1198 | } |
| 1199 | |
| 1200 | bool StackFrame::IsArtificial() const { |
| 1201 | return m_stack_frame_kind == StackFrame::Kind::Artificial; |
| 1202 | } |
| 1203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1204 | lldb::LanguageType StackFrame::GetLanguage() { |
| 1205 | CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit; |
| 1206 | if (cu) |
| 1207 | return cu->GetLanguage(); |
| 1208 | return lldb::eLanguageTypeUnknown; |
| 1209 | } |
| 1210 | |
| 1211 | lldb::LanguageType StackFrame::GuessLanguage() { |
| 1212 | LanguageType lang_type = GetLanguage(); |
| 1213 | |
| 1214 | if (lang_type == eLanguageTypeUnknown) { |
Jim Ingham | bdbdd22 | 2017-04-12 00:19:54 | [diff] [blame] | 1215 | SymbolContext sc = GetSymbolContext(eSymbolContextFunction |
| 1216 | | eSymbolContextSymbol); |
| 1217 | if (sc.function) { |
| 1218 | lang_type = sc.function->GetMangled().GuessLanguage(); |
| 1219 | } |
| 1220 | else if (sc.symbol) |
| 1221 | { |
| 1222 | lang_type = sc.symbol->GetMangled().GuessLanguage(); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1223 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | return lang_type; |
| 1227 | } |
| 1228 | |
| 1229 | namespace { |
| 1230 | std::pair<const Instruction::Operand *, int64_t> |
| 1231 | GetBaseExplainingValue(const Instruction::Operand &operand, |
| 1232 | RegisterContext ®ister_context, lldb::addr_t value) { |
| 1233 | switch (operand.m_type) { |
| 1234 | case Instruction::Operand::Type::Dereference: |
| 1235 | case Instruction::Operand::Type::Immediate: |
| 1236 | case Instruction::Operand::Type::Invalid: |
| 1237 | case Instruction::Operand::Type::Product: |
| 1238 | // These are not currently interesting |
| 1239 | return std::make_pair(nullptr, 0); |
| 1240 | case Instruction::Operand::Type::Sum: { |
| 1241 | const Instruction::Operand *immediate_child = nullptr; |
| 1242 | const Instruction::Operand *variable_child = nullptr; |
| 1243 | if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) { |
| 1244 | immediate_child = &operand.m_children[0]; |
| 1245 | variable_child = &operand.m_children[1]; |
| 1246 | } else if (operand.m_children[1].m_type == |
| 1247 | Instruction::Operand::Type::Immediate) { |
| 1248 | immediate_child = &operand.m_children[1]; |
| 1249 | variable_child = &operand.m_children[0]; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1250 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1251 | if (!immediate_child) { |
| 1252 | return std::make_pair(nullptr, 0); |
| 1253 | } |
| 1254 | lldb::addr_t adjusted_value = value; |
| 1255 | if (immediate_child->m_negative) { |
| 1256 | adjusted_value += immediate_child->m_immediate; |
| 1257 | } else { |
| 1258 | adjusted_value -= immediate_child->m_immediate; |
| 1259 | } |
| 1260 | std::pair<const Instruction::Operand *, int64_t> base_and_offset = |
| 1261 | GetBaseExplainingValue(*variable_child, register_context, |
| 1262 | adjusted_value); |
| 1263 | if (!base_and_offset.first) { |
| 1264 | return std::make_pair(nullptr, 0); |
| 1265 | } |
| 1266 | if (immediate_child->m_negative) { |
| 1267 | base_and_offset.second -= immediate_child->m_immediate; |
| 1268 | } else { |
| 1269 | base_and_offset.second += immediate_child->m_immediate; |
| 1270 | } |
| 1271 | return base_and_offset; |
| 1272 | } |
| 1273 | case Instruction::Operand::Type::Register: { |
| 1274 | const RegisterInfo *info = |
| 1275 | register_context.GetRegisterInfoByName(operand.m_register.AsCString()); |
| 1276 | if (!info) { |
| 1277 | return std::make_pair(nullptr, 0); |
| 1278 | } |
| 1279 | RegisterValue reg_value; |
| 1280 | if (!register_context.ReadRegister(info, reg_value)) { |
| 1281 | return std::make_pair(nullptr, 0); |
| 1282 | } |
| 1283 | if (reg_value.GetAsUInt64() == value) { |
| 1284 | return std::make_pair(&operand, 0); |
| 1285 | } else { |
| 1286 | return std::make_pair(nullptr, 0); |
| 1287 | } |
| 1288 | } |
| 1289 | } |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 | [diff] [blame] | 1290 | return std::make_pair(nullptr, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | std::pair<const Instruction::Operand *, int64_t> |
| 1294 | GetBaseExplainingDereference(const Instruction::Operand &operand, |
| 1295 | RegisterContext ®ister_context, |
| 1296 | lldb::addr_t addr) { |
| 1297 | if (operand.m_type == Instruction::Operand::Type::Dereference) { |
| 1298 | return GetBaseExplainingValue(operand.m_children[0], register_context, |
| 1299 | addr); |
| 1300 | } |
| 1301 | return std::make_pair(nullptr, 0); |
| 1302 | } |
Ilia K | 4f730dc | 2016-09-12 05:25:33 | [diff] [blame] | 1303 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1305 | lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) { |
| 1306 | TargetSP target_sp = CalculateTarget(); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1308 | const ArchSpec &target_arch = target_sp->GetArchitecture(); |
| 1309 | |
| 1310 | AddressRange pc_range; |
| 1311 | pc_range.GetBaseAddress() = GetFrameCodeAddress(); |
| 1312 | pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize()); |
| 1313 | |
| 1314 | ExecutionContext exe_ctx(shared_from_this()); |
| 1315 | |
| 1316 | const char *plugin_name = nullptr; |
| 1317 | const char *flavor = nullptr; |
| 1318 | const bool prefer_file_cache = false; |
| 1319 | |
| 1320 | DisassemblerSP disassembler_sp = Disassembler::DisassembleRange( |
| 1321 | target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache); |
| 1322 | |
Jim Ingham | 99d1e28 | 2017-03-31 22:39:55 | [diff] [blame] | 1323 | if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1324 | return ValueObjectSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1325 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1326 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1327 | InstructionSP instruction_sp = |
| 1328 | disassembler_sp->GetInstructionList().GetInstructionAtIndex(0); |
| 1329 | |
| 1330 | llvm::SmallVector<Instruction::Operand, 3> operands; |
| 1331 | |
| 1332 | if (!instruction_sp->ParseOperands(operands)) { |
| 1333 | return ValueObjectSP(); |
| 1334 | } |
| 1335 | |
| 1336 | RegisterContextSP register_context_sp = GetRegisterContext(); |
| 1337 | |
| 1338 | if (!register_context_sp) { |
| 1339 | return ValueObjectSP(); |
| 1340 | } |
| 1341 | |
| 1342 | for (const Instruction::Operand &operand : operands) { |
| 1343 | std::pair<const Instruction::Operand *, int64_t> base_and_offset = |
| 1344 | GetBaseExplainingDereference(operand, *register_context_sp, addr); |
| 1345 | |
| 1346 | if (!base_and_offset.first) { |
| 1347 | continue; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1348 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1349 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1350 | switch (base_and_offset.first->m_type) { |
| 1351 | case Instruction::Operand::Type::Immediate: { |
| 1352 | lldb_private::Address addr; |
| 1353 | if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate + |
| 1354 | base_and_offset.second, |
| 1355 | addr)) { |
| 1356 | TypeSystem *c_type_system = |
| 1357 | target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC); |
| 1358 | if (!c_type_system) { |
| 1359 | return ValueObjectSP(); |
| 1360 | } else { |
| 1361 | CompilerType void_ptr_type = |
| 1362 | c_type_system |
| 1363 | ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar) |
| 1364 | .GetPointerType(); |
| 1365 | return ValueObjectMemory::Create(this, "", addr, void_ptr_type); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1366 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1367 | } else { |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1368 | return ValueObjectSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1369 | } |
| 1370 | break; |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1371 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1372 | case Instruction::Operand::Type::Register: { |
| 1373 | return GuessValueForRegisterAndOffset(base_and_offset.first->m_register, |
| 1374 | base_and_offset.second); |
| 1375 | } |
| 1376 | default: |
| 1377 | return ValueObjectSP(); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | return ValueObjectSP(); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1382 | } |
| 1383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1384 | namespace { |
| 1385 | ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent, |
| 1386 | int64_t offset) { |
| 1387 | if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) { |
| 1388 | return ValueObjectSP(); |
| 1389 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1390 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1391 | if (parent->IsPointerOrReferenceType()) { |
| 1392 | return parent; |
| 1393 | } |
| 1394 | |
| 1395 | for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) { |
| 1396 | const bool can_create = true; |
| 1397 | ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create); |
| 1398 | |
| 1399 | if (!child_sp) { |
| 1400 | return ValueObjectSP(); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1401 | } |
| 1402 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1403 | int64_t child_offset = child_sp->GetByteOffset(); |
| 1404 | int64_t child_size = child_sp->GetByteSize(); |
| 1405 | |
| 1406 | if (offset >= child_offset && offset < (child_offset + child_size)) { |
| 1407 | return GetValueForOffset(frame, child_sp, offset - child_offset); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1408 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1409 | } |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1411 | if (offset == 0) { |
| 1412 | return parent; |
| 1413 | } else { |
| 1414 | return ValueObjectSP(); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame, |
| 1419 | ValueObjectSP &base, |
| 1420 | int64_t offset) { |
| 1421 | // base is a pointer to something |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 1422 | // offset is the thing to add to the pointer We return the most sensible |
| 1423 | // ValueObject for the result of *(base+offset) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1424 | |
| 1425 | if (!base->IsPointerOrReferenceType()) { |
| 1426 | return ValueObjectSP(); |
| 1427 | } |
| 1428 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 | [diff] [blame] | 1429 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1430 | ValueObjectSP pointee = base->Dereference(error); |
Sean Callanan | a0a1d2d | 2016-09-29 00:16:37 | [diff] [blame] | 1431 | |
| 1432 | if (!pointee) { |
| 1433 | return ValueObjectSP(); |
| 1434 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1435 | |
Ilia K | 4f730dc | 2016-09-12 05:25:33 | [diff] [blame] | 1436 | if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1437 | int64_t index = offset / pointee->GetByteSize(); |
| 1438 | offset = offset % pointee->GetByteSize(); |
| 1439 | const bool can_create = true; |
| 1440 | pointee = base->GetSyntheticArrayMember(index, can_create); |
| 1441 | } |
| 1442 | |
| 1443 | if (!pointee || error.Fail()) { |
| 1444 | return ValueObjectSP(); |
| 1445 | } |
| 1446 | |
| 1447 | return GetValueForOffset(frame, pointee, offset); |
| 1448 | } |
| 1449 | |
| 1450 | //------------------------------------------------------------------ |
| 1451 | /// Attempt to reconstruct the ValueObject for the address contained in a |
| 1452 | /// given register plus an offset. |
| 1453 | /// |
| 1454 | /// @params [in] frame |
| 1455 | /// The current stack frame. |
| 1456 | /// |
| 1457 | /// @params [in] reg |
| 1458 | /// The register. |
| 1459 | /// |
| 1460 | /// @params [in] offset |
| 1461 | /// The offset from the register. |
| 1462 | /// |
| 1463 | /// @param [in] disassembler |
| 1464 | /// A disassembler containing instructions valid up to the current PC. |
| 1465 | /// |
| 1466 | /// @param [in] variables |
| 1467 | /// The variable list from the current frame, |
| 1468 | /// |
| 1469 | /// @param [in] pc |
| 1470 | /// The program counter for the instruction considered the 'user'. |
| 1471 | /// |
| 1472 | /// @return |
| 1473 | /// A string describing the base for the ExpressionPath. This could be a |
| 1474 | /// variable, a register value, an argument, or a function return value. |
| 1475 | /// The ValueObject if found. If valid, it has a valid ExpressionPath. |
| 1476 | //------------------------------------------------------------------ |
| 1477 | lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg, |
| 1478 | int64_t offset, Disassembler &disassembler, |
| 1479 | VariableList &variables, const Address &pc) { |
| 1480 | // Example of operation for Intel: |
| 1481 | // |
| 1482 | // +14: movq -0x8(%rbp), %rdi |
| 1483 | // +18: movq 0x8(%rdi), %rdi |
| 1484 | // +22: addl 0x4(%rdi), %eax |
| 1485 | // |
| 1486 | // f, a pointer to a struct, is known to be at -0x8(%rbp). |
| 1487 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 1488 | // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at |
| 1489 | // +18 that assigns to rdi, and calls itself recursively for that dereference |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1490 | // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at |
| 1491 | // +14 that assigns to rdi, and calls itself recursively for that |
| 1492 | // derefernece |
| 1493 | // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the |
| 1494 | // variable list. |
| 1495 | // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14) |
| 1496 | // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8 |
| 1497 | // at +18) |
| 1498 | // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at |
| 1499 | // rdi+4 at +22) |
| 1500 | |
| 1501 | // First, check the variable list to see if anything is at the specified |
| 1502 | // location. |
Sean Callanan | 807ee2f | 2016-09-13 21:18:27 | [diff] [blame] | 1503 | |
Sean Callanan | 5085710 | 2016-09-14 00:48:19 | [diff] [blame] | 1504 | using namespace OperandMatchers; |
| 1505 | |
Sean Callanan | 0ac172d | 2016-09-14 20:29:57 | [diff] [blame] | 1506 | const RegisterInfo *reg_info = |
| 1507 | frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString()); |
| 1508 | if (!reg_info) { |
| 1509 | return ValueObjectSP(); |
| 1510 | } |
| 1511 | |
Sean Callanan | 807ee2f | 2016-09-13 21:18:27 | [diff] [blame] | 1512 | Instruction::Operand op = |
| 1513 | offset ? Instruction::Operand::BuildDereference( |
| 1514 | Instruction::Operand::BuildSum( |
| 1515 | Instruction::Operand::BuildRegister(reg), |
| 1516 | Instruction::Operand::BuildImmediate(offset))) |
| 1517 | : Instruction::Operand::BuildDereference( |
| 1518 | Instruction::Operand::BuildRegister(reg)); |
| 1519 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1520 | for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) { |
| 1521 | VariableSP var_sp = variables.GetVariableAtIndex(vi); |
Sean Callanan | 807ee2f | 2016-09-13 21:18:27 | [diff] [blame] | 1522 | if (var_sp->LocationExpression().MatchesOperand(frame, op)) { |
| 1523 | return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues); |
Sean Callanan | 4740a73 | 2016-09-06 04:48:36 | [diff] [blame] | 1524 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1525 | } |
| 1526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1527 | const uint32_t current_inst = |
| 1528 | disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc); |
| 1529 | if (current_inst == UINT32_MAX) { |
| 1530 | return ValueObjectSP(); |
| 1531 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 1532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1533 | for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) { |
| 1534 | // This is not an exact algorithm, and it sacrifices accuracy for |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 | [diff] [blame] | 1535 | // generality. Recognizing "mov" and "ld" instructions –– and which |
| 1536 | // are their source and destination operands -- is something the |
| 1537 | // disassembler should do for us. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1538 | InstructionSP instruction_sp = |
| 1539 | disassembler.GetInstructionList().GetInstructionAtIndex(ii); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 1540 | |
Sean Callanan | 5085710 | 2016-09-14 00:48:19 | [diff] [blame] | 1541 | if (instruction_sp->IsCall()) { |
| 1542 | ABISP abi_sp = frame.CalculateProcess()->GetABI(); |
| 1543 | if (!abi_sp) { |
| 1544 | continue; |
| 1545 | } |
| 1546 | |
| 1547 | const char *return_register_name; |
| 1548 | if (!abi_sp->GetPointerReturnRegister(return_register_name)) { |
| 1549 | continue; |
| 1550 | } |
| 1551 | |
| 1552 | const RegisterInfo *return_register_info = |
| 1553 | frame.GetRegisterContext()->GetRegisterInfoByName( |
| 1554 | return_register_name); |
| 1555 | if (!return_register_info) { |
| 1556 | continue; |
| 1557 | } |
| 1558 | |
| 1559 | int64_t offset = 0; |
| 1560 | |
| 1561 | if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), |
| 1562 | MatchRegOp(*return_register_info))(op) && |
| 1563 | !MatchUnaryOp( |
| 1564 | MatchOpType(Instruction::Operand::Type::Dereference), |
| 1565 | MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), |
| 1566 | MatchRegOp(*return_register_info), |
| 1567 | FetchImmOp(offset)))(op)) { |
| 1568 | continue; |
| 1569 | } |
| 1570 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1571 | llvm::SmallVector<Instruction::Operand, 1> operands; |
| 1572 | if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) { |
| 1573 | continue; |
| 1574 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 | [diff] [blame] | 1575 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1576 | switch (operands[0].m_type) { |
| 1577 | default: |
| 1578 | break; |
| 1579 | case Instruction::Operand::Type::Immediate: { |
| 1580 | SymbolContext sc; |
| 1581 | Address load_address; |
| 1582 | if (!frame.CalculateTarget()->ResolveLoadAddress( |
| 1583 | operands[0].m_immediate, load_address)) { |
| 1584 | break; |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 | [diff] [blame] | 1585 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1586 | frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress( |
| 1587 | load_address, eSymbolContextFunction, sc); |
| 1588 | if (!sc.function) { |
| 1589 | break; |
| 1590 | } |
| 1591 | CompilerType function_type = sc.function->GetCompilerType(); |
| 1592 | if (!function_type.IsFunctionType()) { |
| 1593 | break; |
| 1594 | } |
| 1595 | CompilerType return_type = function_type.GetFunctionReturnType(); |
| 1596 | RegisterValue return_value; |
Sean Callanan | 5085710 | 2016-09-14 00:48:19 | [diff] [blame] | 1597 | if (!frame.GetRegisterContext()->ReadRegister(return_register_info, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1598 | return_value)) { |
| 1599 | break; |
| 1600 | } |
| 1601 | std::string name_str( |
| 1602 | sc.function->GetName().AsCString("<unknown function>")); |
| 1603 | name_str.append("()"); |
| 1604 | Address return_value_address(return_value.GetAsUInt64()); |
| 1605 | ValueObjectSP return_value_sp = ValueObjectMemory::Create( |
Zachary Turner | 22a2628 | 2016-11-12 18:17:36 | [diff] [blame] | 1606 | &frame, name_str, return_value_address, return_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1607 | return GetValueForDereferincingOffset(frame, return_value_sp, offset); |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | continue; |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 | [diff] [blame] | 1612 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1613 | |
| 1614 | llvm::SmallVector<Instruction::Operand, 2> operands; |
| 1615 | if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) { |
| 1616 | continue; |
| 1617 | } |
| 1618 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1619 | Instruction::Operand *origin_operand = nullptr; |
Sean Callanan | aa4b44c | 2016-09-14 20:58:31 | [diff] [blame] | 1620 | auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) { |
| 1621 | return MatchRegOp(*reg_info)(op) && op.m_clobbered; |
| 1622 | }; |
Sean Callanan | 0ac172d | 2016-09-14 20:29:57 | [diff] [blame] | 1623 | |
| 1624 | if (clobbered_reg_matcher(operands[0])) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1625 | origin_operand = &operands[1]; |
Sean Callanan | 0ac172d | 2016-09-14 20:29:57 | [diff] [blame] | 1626 | } |
| 1627 | else if (clobbered_reg_matcher(operands[1])) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1628 | origin_operand = &operands[0]; |
Sean Callanan | 0ac172d | 2016-09-14 20:29:57 | [diff] [blame] | 1629 | } |
| 1630 | else { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1631 | continue; |
| 1632 | } |
| 1633 | |
| 1634 | // We have an origin operand. Can we track its value down? |
Sean Callanan | 561a9bb | 2016-09-14 21:54:28 | [diff] [blame] | 1635 | ValueObjectSP source_path; |
| 1636 | ConstString origin_register; |
| 1637 | int64_t origin_offset = 0; |
| 1638 | |
| 1639 | if (FetchRegOp(origin_register)(*origin_operand)) { |
| 1640 | source_path = DoGuessValueAt(frame, origin_register, 0, disassembler, |
| 1641 | variables, instruction_sp->GetAddress()); |
| 1642 | } else if (MatchUnaryOp( |
| 1643 | MatchOpType(Instruction::Operand::Type::Dereference), |
| 1644 | FetchRegOp(origin_register))(*origin_operand) || |
| 1645 | MatchUnaryOp( |
| 1646 | MatchOpType(Instruction::Operand::Type::Dereference), |
| 1647 | MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), |
| 1648 | FetchRegOp(origin_register), |
| 1649 | FetchImmOp(origin_offset)))(*origin_operand)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1650 | source_path = |
Sean Callanan | 561a9bb | 2016-09-14 21:54:28 | [diff] [blame] | 1651 | DoGuessValueAt(frame, origin_register, origin_offset, disassembler, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1652 | variables, instruction_sp->GetAddress()); |
Sean Callanan | 561a9bb | 2016-09-14 21:54:28 | [diff] [blame] | 1653 | if (!source_path) { |
| 1654 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1655 | } |
Sean Callanan | 561a9bb | 2016-09-14 21:54:28 | [diff] [blame] | 1656 | source_path = |
| 1657 | GetValueForDereferincingOffset(frame, source_path, offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1658 | } |
| 1659 | |
| 1660 | if (source_path) { |
| 1661 | return source_path; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | return ValueObjectSP(); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg, |
| 1670 | int64_t offset) { |
| 1671 | TargetSP target_sp = CalculateTarget(); |
| 1672 | |
| 1673 | const ArchSpec &target_arch = target_sp->GetArchitecture(); |
| 1674 | |
| 1675 | Block *frame_block = GetFrameBlock(); |
| 1676 | |
| 1677 | if (!frame_block) { |
| 1678 | return ValueObjectSP(); |
| 1679 | } |
| 1680 | |
| 1681 | Function *function = frame_block->CalculateSymbolContextFunction(); |
| 1682 | if (!function) { |
| 1683 | return ValueObjectSP(); |
| 1684 | } |
| 1685 | |
| 1686 | AddressRange pc_range = function->GetAddressRange(); |
| 1687 | |
| 1688 | if (GetFrameCodeAddress().GetFileAddress() < |
| 1689 | pc_range.GetBaseAddress().GetFileAddress() || |
| 1690 | GetFrameCodeAddress().GetFileAddress() - |
| 1691 | pc_range.GetBaseAddress().GetFileAddress() >= |
| 1692 | pc_range.GetByteSize()) { |
| 1693 | return ValueObjectSP(); |
| 1694 | } |
| 1695 | |
| 1696 | ExecutionContext exe_ctx(shared_from_this()); |
| 1697 | |
| 1698 | const char *plugin_name = nullptr; |
| 1699 | const char *flavor = nullptr; |
| 1700 | const bool prefer_file_cache = false; |
| 1701 | DisassemblerSP disassembler_sp = Disassembler::DisassembleRange( |
| 1702 | target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache); |
| 1703 | |
| 1704 | if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) { |
| 1705 | return ValueObjectSP(); |
| 1706 | } |
| 1707 | |
| 1708 | const bool get_file_globals = false; |
| 1709 | VariableList *variables = GetVariableList(get_file_globals); |
| 1710 | |
| 1711 | if (!variables) { |
| 1712 | return ValueObjectSP(); |
| 1713 | } |
| 1714 | |
| 1715 | return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables, |
| 1716 | GetFrameCodeAddress()); |
| 1717 | } |
| 1718 | |
Shafik Yaghmour | e23d0b63 | 2018-09-20 17:06:34 | [diff] [blame] | 1719 | lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) { |
| 1720 | ValueObjectSP value_sp; |
| 1721 | |
| 1722 | if (!name) |
| 1723 | return value_sp; |
| 1724 | |
| 1725 | TargetSP target_sp = CalculateTarget(); |
| 1726 | ProcessSP process_sp = CalculateProcess(); |
| 1727 | |
| 1728 | if (!target_sp && !process_sp) |
| 1729 | return value_sp; |
| 1730 | |
| 1731 | VariableList variable_list; |
| 1732 | VariableSP var_sp; |
| 1733 | SymbolContext sc(GetSymbolContext(eSymbolContextBlock)); |
| 1734 | |
| 1735 | if (sc.block) { |
| 1736 | const bool can_create = true; |
| 1737 | const bool get_parent_variables = true; |
| 1738 | const bool stop_if_block_is_inlined_function = true; |
| 1739 | |
| 1740 | if (sc.block->AppendVariables( |
| 1741 | can_create, get_parent_variables, stop_if_block_is_inlined_function, |
| 1742 | [this](Variable *v) { return v->IsInScope(this); }, |
| 1743 | &variable_list)) { |
| 1744 | var_sp = variable_list.FindVariable(name); |
| 1745 | } |
| 1746 | |
| 1747 | if (var_sp) |
| 1748 | value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues); |
| 1749 | } |
| 1750 | |
| 1751 | return value_sp; |
| 1752 | } |
| 1753 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1754 | TargetSP StackFrame::CalculateTarget() { |
| 1755 | TargetSP target_sp; |
| 1756 | ThreadSP thread_sp(GetThread()); |
| 1757 | if (thread_sp) { |
| 1758 | ProcessSP process_sp(thread_sp->CalculateProcess()); |
| 1759 | if (process_sp) |
| 1760 | target_sp = process_sp->CalculateTarget(); |
| 1761 | } |
| 1762 | return target_sp; |
| 1763 | } |
| 1764 | |
| 1765 | ProcessSP StackFrame::CalculateProcess() { |
| 1766 | ProcessSP process_sp; |
| 1767 | ThreadSP thread_sp(GetThread()); |
| 1768 | if (thread_sp) |
| 1769 | process_sp = thread_sp->CalculateProcess(); |
| 1770 | return process_sp; |
| 1771 | } |
| 1772 | |
| 1773 | ThreadSP StackFrame::CalculateThread() { return GetThread(); } |
| 1774 | |
| 1775 | StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); } |
| 1776 | |
| 1777 | void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) { |
| 1778 | exe_ctx.SetContext(shared_from_this()); |
| 1779 | } |
| 1780 | |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 | [diff] [blame] | 1781 | void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1782 | const char *frame_marker) { |
| 1783 | if (strm == nullptr) |
| 1784 | return; |
| 1785 | |
| 1786 | GetSymbolContext(eSymbolContextEverything); |
| 1787 | ExecutionContext exe_ctx(shared_from_this()); |
| 1788 | StreamString s; |
| 1789 | |
| 1790 | if (frame_marker) |
| 1791 | s.PutCString(frame_marker); |
| 1792 | |
| 1793 | const FormatEntity::Entry *frame_format = nullptr; |
| 1794 | Target *target = exe_ctx.GetTargetPtr(); |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 | [diff] [blame] | 1795 | if (target) { |
| 1796 | if (show_unique) { |
| 1797 | frame_format = target->GetDebugger().GetFrameFormatUnique(); |
| 1798 | } else { |
| 1799 | frame_format = target->GetDebugger().GetFrameFormat(); |
| 1800 | } |
| 1801 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1802 | if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx, |
| 1803 | nullptr, nullptr, false, false)) { |
Zachary Turner | c156427 | 2016-11-16 21:15:24 | [diff] [blame] | 1804 | strm->PutCString(s.GetString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1805 | } else { |
| 1806 | Dump(strm, true, false); |
| 1807 | strm->EOL(); |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | void StackFrame::Dump(Stream *strm, bool show_frame_index, |
| 1812 | bool show_fullpaths) { |
| 1813 | if (strm == nullptr) |
| 1814 | return; |
| 1815 | |
| 1816 | if (show_frame_index) |
| 1817 | strm->Printf("frame #%u: ", m_frame_index); |
| 1818 | ExecutionContext exe_ctx(shared_from_this()); |
| 1819 | Target *target = exe_ctx.GetTargetPtr(); |
| 1820 | strm->Printf("0x%0*" PRIx64 " ", |
| 1821 | target ? (target->GetArchitecture().GetAddressByteSize() * 2) |
| 1822 | : 16, |
| 1823 | GetFrameCodeAddress().GetLoadAddress(target)); |
| 1824 | GetSymbolContext(eSymbolContextEverything); |
| 1825 | const bool show_module = true; |
| 1826 | const bool show_inline = true; |
| 1827 | const bool show_function_arguments = true; |
| 1828 | const bool show_function_name = true; |
| 1829 | m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(), |
| 1830 | GetFrameCodeAddress(), show_fullpaths, show_module, |
| 1831 | show_inline, show_function_arguments, |
| 1832 | show_function_name); |
| 1833 | } |
| 1834 | |
| 1835 | void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) { |
| 1836 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1837 | assert(GetStackID() == |
| 1838 | prev_frame.GetStackID()); // TODO: remove this after some testing |
| 1839 | m_variable_list_sp = prev_frame.m_variable_list_sp; |
| 1840 | m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects); |
Zachary Turner | c156427 | 2016-11-16 21:15:24 | [diff] [blame] | 1841 | if (!m_disassembly.GetString().empty()) { |
| 1842 | m_disassembly.Clear(); |
| 1843 | m_disassembly.PutCString(prev_frame.m_disassembly.GetString()); |
| 1844 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) { |
| 1848 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 1849 | assert(GetStackID() == |
| 1850 | curr_frame.GetStackID()); // TODO: remove this after some testing |
| 1851 | m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value |
| 1852 | assert(GetThread() == curr_frame.GetThread()); |
| 1853 | m_frame_index = curr_frame.m_frame_index; |
| 1854 | m_concrete_frame_index = curr_frame.m_concrete_frame_index; |
| 1855 | m_reg_context_sp = curr_frame.m_reg_context_sp; |
| 1856 | m_frame_code_addr = curr_frame.m_frame_code_addr; |
| 1857 | assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp || |
| 1858 | m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get()); |
| 1859 | assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp || |
| 1860 | m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get()); |
| 1861 | assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || |
| 1862 | m_sc.comp_unit == curr_frame.m_sc.comp_unit); |
| 1863 | assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr || |
| 1864 | m_sc.function == curr_frame.m_sc.function); |
| 1865 | m_sc = curr_frame.m_sc; |
| 1866 | m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything); |
| 1867 | m_flags.Set(m_sc.GetResolvedMask()); |
| 1868 | m_frame_base.Clear(); |
| 1869 | m_frame_base_error.Clear(); |
| 1870 | } |
| 1871 | |
| 1872 | bool StackFrame::HasCachedData() const { |
| 1873 | if (m_variable_list_sp) |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 | [diff] [blame] | 1874 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1875 | if (m_variable_list_value_objects.GetSize() > 0) |
| 1876 | return true; |
| 1877 | if (!m_disassembly.GetString().empty()) |
| 1878 | return true; |
| 1879 | return false; |
| 1880 | } |
| 1881 | |
| 1882 | bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source, |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 | [diff] [blame] | 1883 | bool show_unique, const char *frame_marker) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1884 | if (show_frame_info) { |
| 1885 | strm.Indent(); |
Pavel Labath | 7f1c121 | 2017-06-12 16:25:24 | [diff] [blame] | 1886 | DumpUsingSettingsFormat(&strm, show_unique, frame_marker); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | if (show_source) { |
| 1890 | ExecutionContext exe_ctx(shared_from_this()); |
| 1891 | bool have_source = false, have_debuginfo = false; |
| 1892 | Debugger::StopDisassemblyType disasm_display = |
| 1893 | Debugger::eStopDisassemblyTypeNever; |
| 1894 | Target *target = exe_ctx.GetTargetPtr(); |
| 1895 | if (target) { |
| 1896 | Debugger &debugger = target->GetDebugger(); |
| 1897 | const uint32_t source_lines_before = |
| 1898 | debugger.GetStopSourceLineCount(true); |
| 1899 | const uint32_t source_lines_after = |
| 1900 | debugger.GetStopSourceLineCount(false); |
| 1901 | disasm_display = debugger.GetStopDisassemblyDisplay(); |
| 1902 | |
| 1903 | GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry); |
| 1904 | if (m_sc.comp_unit && m_sc.line_entry.IsValid()) { |
| 1905 | have_debuginfo = true; |
| 1906 | if (source_lines_before > 0 || source_lines_after > 0) { |
| 1907 | size_t num_lines = |
| 1908 | target->GetSourceManager().DisplaySourceLinesWithLineNumbers( |
| 1909 | m_sc.line_entry.file, m_sc.line_entry.line, |
Todd Fiala | 9666ba7 | 2016-09-21 20:13:14 | [diff] [blame] | 1910 | m_sc.line_entry.column, source_lines_before, |
| 1911 | source_lines_after, "->", &strm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1912 | if (num_lines != 0) |
| 1913 | have_source = true; |
| 1914 | // TODO: Give here a one time warning if source file is missing. |
| 1915 | } |
| 1916 | } |
| 1917 | switch (disasm_display) { |
| 1918 | case Debugger::eStopDisassemblyTypeNever: |
| 1919 | break; |
| 1920 | |
| 1921 | case Debugger::eStopDisassemblyTypeNoDebugInfo: |
| 1922 | if (have_debuginfo) |
| 1923 | break; |
| 1924 | LLVM_FALLTHROUGH; |
| 1925 | |
| 1926 | case Debugger::eStopDisassemblyTypeNoSource: |
| 1927 | if (have_source) |
| 1928 | break; |
| 1929 | LLVM_FALLTHROUGH; |
| 1930 | |
| 1931 | case Debugger::eStopDisassemblyTypeAlways: |
| 1932 | if (target) { |
| 1933 | const uint32_t disasm_lines = debugger.GetDisassemblyLineCount(); |
| 1934 | if (disasm_lines > 0) { |
| 1935 | const ArchSpec &target_arch = target->GetArchitecture(); |
| 1936 | AddressRange pc_range; |
| 1937 | pc_range.GetBaseAddress() = GetFrameCodeAddress(); |
| 1938 | pc_range.SetByteSize(disasm_lines * |
| 1939 | target_arch.GetMaximumOpcodeByteSize()); |
| 1940 | const char *plugin_name = nullptr; |
| 1941 | const char *flavor = nullptr; |
Jason Molenda | 0b4c26b | 2016-09-08 05:12:41 | [diff] [blame] | 1942 | const bool mixed_source_and_assembly = false; |
| 1943 | Disassembler::Disassemble( |
| 1944 | target->GetDebugger(), target_arch, plugin_name, flavor, |
| 1945 | exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0, |
| 1946 | Disassembler::eOptionMarkPCAddress, strm); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 1947 | } |
| 1948 | } |
| 1949 | break; |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | return true; |
Greg Clayton | 7260f62 | 2011-04-18 08:33:37 | [diff] [blame] | 1954 | } |
Kuba Mracek | 41ae8e7 | 2018-10-31 04:00:22 | [diff] [blame] | 1955 | |
| 1956 | RecognizedStackFrameSP StackFrame::GetRecognizedFrame() { |
| 1957 | if (!m_recognized_frame_sp) { |
| 1958 | m_recognized_frame_sp = |
| 1959 | StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame()); |
| 1960 | } |
| 1961 | return m_recognized_frame_sp; |
| 1962 | } |