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