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