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