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