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