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