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