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