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