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