1 //===-- RegisterContextLLDB.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 11 #include "lldb/lldb-private.h" 12 #include "lldb/Core/Address.h" 13 #include "lldb/Core/AddressRange.h" 14 #include "lldb/Core/DataBufferHeap.h" 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/RegisterValue.h" 18 #include "lldb/Core/Value.h" 19 #include "lldb/Expression/DWARFExpression.h" 20 #include "lldb/Symbol/ArmUnwindInfo.h" 21 #include "lldb/Symbol/DWARFCallFrameInfo.h" 22 #include "lldb/Symbol/FuncUnwinders.h" 23 #include "lldb/Symbol/Function.h" 24 #include "lldb/Symbol/ObjectFile.h" 25 #include "lldb/Symbol/Symbol.h" 26 #include "lldb/Symbol/SymbolContext.h" 27 #include "lldb/Target/ABI.h" 28 #include "lldb/Target/DynamicLoader.h" 29 #include "lldb/Target/ExecutionContext.h" 30 #include "lldb/Target/Platform.h" 31 #include "lldb/Target/Process.h" 32 #include "lldb/Target/SectionLoadList.h" 33 #include "lldb/Target/StackFrame.h" 34 #include "lldb/Target/Target.h" 35 #include "lldb/Target/Thread.h" 36 37 #include "RegisterContextLLDB.h" 38 39 using namespace lldb; 40 using namespace lldb_private; 41 42 static ConstString GetSymbolOrFunctionName(const SymbolContext &sym_ctx) 43 { 44 if (sym_ctx.symbol) 45 return sym_ctx.symbol->GetName(); 46 else if (sym_ctx.function) 47 return sym_ctx.function->GetName(); 48 return ConstString(); 49 } 50 51 RegisterContextLLDB::RegisterContextLLDB 52 ( 53 Thread& thread, 54 const SharedPtr &next_frame, 55 SymbolContext& sym_ctx, 56 uint32_t frame_number, 57 UnwindLLDB& unwind_lldb 58 ) : 59 RegisterContext (thread, frame_number), 60 m_thread(thread), 61 m_fast_unwind_plan_sp (), 62 m_full_unwind_plan_sp (), 63 m_fallback_unwind_plan_sp (), 64 m_all_registers_available(false), 65 m_frame_type (-1), 66 m_cfa (LLDB_INVALID_ADDRESS), 67 m_start_pc (), 68 m_current_pc (), 69 m_current_offset (0), 70 m_current_offset_backed_up_one (0), 71 m_sym_ctx(sym_ctx), 72 m_sym_ctx_valid (false), 73 m_frame_number (frame_number), 74 m_registers(), 75 m_parent_unwind (unwind_lldb) 76 { 77 m_sym_ctx.Clear(false); 78 m_sym_ctx_valid = false; 79 80 if (IsFrameZero ()) 81 { 82 InitializeZerothFrame (); 83 } 84 else 85 { 86 InitializeNonZerothFrame (); 87 } 88 89 // This same code exists over in the GetFullUnwindPlanForFrame() but it may not have been executed yet 90 if (IsFrameZero() 91 || next_frame->m_frame_type == eTrapHandlerFrame 92 || next_frame->m_frame_type == eDebuggerFrame) 93 { 94 m_all_registers_available = true; 95 } 96 } 97 98 bool 99 RegisterContextLLDB::IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset) 100 { 101 if (!unwind_plan_sp) 102 return false; 103 104 // check if m_current_pc is valid 105 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) 106 { 107 // yes - current offset can be used as is 108 valid_pc_offset = m_current_offset; 109 return true; 110 } 111 112 // if m_current_offset <= 0, we've got nothing else to try 113 if (m_current_offset <= 0) 114 return false; 115 116 // check pc - 1 to see if it's valid 117 Address pc_minus_one (m_current_pc); 118 pc_minus_one.SetOffset(m_current_pc.GetOffset() - 1); 119 if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) 120 { 121 // *valid_pc_offset = m_current_offset - 1; 122 valid_pc_offset = m_current_pc.GetOffset() - 1; 123 return true; 124 } 125 126 return false; 127 } 128 129 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the zeroth frame or currently 130 // executing frame. 131 132 void 133 RegisterContextLLDB::InitializeZerothFrame() 134 { 135 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 136 ExecutionContext exe_ctx(m_thread.shared_from_this()); 137 RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext(); 138 139 if (reg_ctx_sp.get() == NULL) 140 { 141 m_frame_type = eNotAValidFrame; 142 UnwindLogMsg ("frame does not have a register context"); 143 return; 144 } 145 146 addr_t current_pc = reg_ctx_sp->GetPC(); 147 148 if (current_pc == LLDB_INVALID_ADDRESS) 149 { 150 m_frame_type = eNotAValidFrame; 151 UnwindLogMsg ("frame does not have a pc"); 152 return; 153 } 154 155 Process *process = exe_ctx.GetProcessPtr(); 156 157 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs 158 // this will strip bit zero in case we read a PC from memory or from the LR. 159 // (which would be a no-op in frame 0 where we get it from the register set, 160 // but still a good idea to make the call here for other ABIs that may exist.) 161 ABI *abi = process->GetABI().get(); 162 if (abi) 163 current_pc = abi->FixCodeAddress(current_pc); 164 165 // Initialize m_current_pc, an Address object, based on current_pc, an addr_t. 166 m_current_pc.SetLoadAddress (current_pc, &process->GetTarget()); 167 168 // If we don't have a Module for some reason, we're not going to find symbol/function information - just 169 // stick in some reasonable defaults and hope we can unwind past this frame. 170 ModuleSP pc_module_sp (m_current_pc.GetModule()); 171 if (!m_current_pc.IsValid() || !pc_module_sp) 172 { 173 UnwindLogMsg ("using architectural default unwind method"); 174 } 175 176 // We require either a symbol or function in the symbols context to be successfully 177 // filled in or this context is of no use to us. 178 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; 179 if (pc_module_sp.get() 180 && (pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, resolve_scope, m_sym_ctx) & resolve_scope)) 181 { 182 m_sym_ctx_valid = true; 183 } 184 185 if (m_sym_ctx.symbol) 186 { 187 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", symbol name is '%s'", 188 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString("")); 189 } 190 else if (m_sym_ctx.function) 191 { 192 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", function name is '%s'", 193 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString("")); 194 } 195 else 196 { 197 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", no symbol/function name is known.", current_pc); 198 } 199 200 AddressRange addr_range; 201 m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range); 202 203 if (IsTrapHandlerSymbol (process, m_sym_ctx)) 204 { 205 m_frame_type = eTrapHandlerFrame; 206 } 207 else 208 { 209 // FIXME: Detect eDebuggerFrame here. 210 m_frame_type = eNormalFrame; 211 } 212 213 // If we were able to find a symbol/function, set addr_range to the bounds of that symbol/function. 214 // else treat the current pc value as the start_pc and record no offset. 215 if (addr_range.GetBaseAddress().IsValid()) 216 { 217 m_start_pc = addr_range.GetBaseAddress(); 218 if (m_current_pc.GetSection() == m_start_pc.GetSection()) 219 { 220 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset(); 221 } 222 else if (m_current_pc.GetModule() == m_start_pc.GetModule()) 223 { 224 // This means that whatever symbol we kicked up isn't really correct 225 // --- we should not cross section boundaries ... We really should NULL out 226 // the function/symbol in this case unless there is a bad assumption 227 // here due to inlined functions? 228 m_current_offset = m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress(); 229 } 230 m_current_offset_backed_up_one = m_current_offset; 231 } 232 else 233 { 234 m_start_pc = m_current_pc; 235 m_current_offset = -1; 236 m_current_offset_backed_up_one = -1; 237 } 238 239 // We've set m_frame_type and m_sym_ctx before these calls. 240 241 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame (); 242 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); 243 244 UnwindPlan::RowSP active_row; 245 lldb::RegisterKind row_register_kind = eRegisterKindGeneric; 246 if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 247 { 248 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 249 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); 250 if (active_row.get() && log) 251 { 252 StreamString active_row_strm; 253 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); 254 UnwindLogMsg ("%s", active_row_strm.GetString().c_str()); 255 } 256 } 257 258 if (!active_row.get()) 259 { 260 UnwindLogMsg ("could not find an unwindplan row for this frame's pc"); 261 m_frame_type = eNotAValidFrame; 262 return; 263 } 264 265 266 if (!ReadCFAValueForRow (row_register_kind, active_row, m_cfa)) 267 { 268 // Try the fall back unwind plan since the 269 // full unwind plan failed. 270 FuncUnwindersSP func_unwinders_sp; 271 UnwindPlanSP call_site_unwind_plan; 272 bool cfa_status = false; 273 274 if (m_sym_ctx_valid) 275 { 276 func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx); 277 } 278 279 if(func_unwinders_sp.get() != nullptr) 280 call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(), m_current_offset_backed_up_one); 281 282 if (call_site_unwind_plan.get() != nullptr) 283 { 284 m_fallback_unwind_plan_sp = call_site_unwind_plan; 285 if(TryFallbackUnwindPlan()) 286 cfa_status = true; 287 } 288 if (!cfa_status) 289 { 290 UnwindLogMsg ("could not read CFA value for first frame."); 291 m_frame_type = eNotAValidFrame; 292 return; 293 } 294 } 295 296 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64 " using %s UnwindPlan", 297 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), 298 (uint64_t) m_cfa, 299 m_full_unwind_plan_sp->GetSourceName().GetCString()); 300 } 301 302 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the RegisterContextLLDB "below" it 303 // to provide things like its current pc value. 304 305 void 306 RegisterContextLLDB::InitializeNonZerothFrame() 307 { 308 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 309 if (IsFrameZero ()) 310 { 311 m_frame_type = eNotAValidFrame; 312 UnwindLogMsg ("non-zeroth frame tests positive for IsFrameZero -- that shouldn't happen."); 313 return; 314 } 315 316 if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) 317 { 318 m_frame_type = eNotAValidFrame; 319 UnwindLogMsg ("Could not get next frame, marking this frame as invalid."); 320 return; 321 } 322 if (!m_thread.GetRegisterContext()) 323 { 324 m_frame_type = eNotAValidFrame; 325 UnwindLogMsg ("Could not get register context for this thread, marking this frame as invalid."); 326 return; 327 } 328 329 addr_t pc; 330 if (!ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) 331 { 332 UnwindLogMsg ("could not get pc value"); 333 m_frame_type = eNotAValidFrame; 334 return; 335 } 336 337 if (log) 338 { 339 UnwindLogMsg ("pc = 0x%" PRIx64, pc); 340 addr_t reg_val; 341 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val)) 342 UnwindLogMsg ("fp = 0x%" PRIx64, reg_val); 343 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val)) 344 UnwindLogMsg ("sp = 0x%" PRIx64, reg_val); 345 } 346 347 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap handler function 348 bool above_trap_handler = false; 349 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsTrapHandlerFrame()) 350 above_trap_handler = true; 351 352 if (pc == 0 || pc == 0x1) 353 { 354 if (above_trap_handler == false) 355 { 356 m_frame_type = eNotAValidFrame; 357 UnwindLogMsg ("this frame has a pc of 0x0"); 358 return; 359 } 360 } 361 362 ExecutionContext exe_ctx(m_thread.shared_from_this()); 363 Process *process = exe_ctx.GetProcessPtr(); 364 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs 365 // this will strip bit zero in case we read a PC from memory or from the LR. 366 ABI *abi = process->GetABI().get(); 367 if (abi) 368 pc = abi->FixCodeAddress(pc); 369 370 m_current_pc.SetLoadAddress (pc, &process->GetTarget()); 371 372 // If we don't have a Module for some reason, we're not going to find symbol/function information - just 373 // stick in some reasonable defaults and hope we can unwind past this frame. 374 ModuleSP pc_module_sp (m_current_pc.GetModule()); 375 if (!m_current_pc.IsValid() || !pc_module_sp) 376 { 377 UnwindLogMsg ("using architectural default unwind method"); 378 379 // Test the pc value to see if we know it's in an unmapped/non-executable region of memory. 380 uint32_t permissions; 381 if (process->GetLoadAddressPermissions(pc, permissions) 382 && (permissions & ePermissionsExecutable) == 0) 383 { 384 // If this is the second frame off the stack, we may have unwound the first frame 385 // incorrectly. But using the architecture default unwind plan may get us back on 386 // track -- albeit possibly skipping a real frame. Give this frame a clearly-invalid 387 // pc and see if we can get any further. 388 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsFrameZero()) 389 { 390 UnwindLogMsg ("had a pc of 0x%" PRIx64 " which is not in executable memory but on frame 1 -- allowing it once.", 391 (uint64_t) pc); 392 m_frame_type = eSkipFrame; 393 } 394 else 395 { 396 // anywhere other than the second frame, a non-executable pc means we're off in the weeds -- stop now. 397 m_frame_type = eNotAValidFrame; 398 UnwindLogMsg ("pc is in a non-executable section of memory and this isn't the 2nd frame in the stack walk."); 399 return; 400 } 401 } 402 403 if (abi) 404 { 405 m_fast_unwind_plan_sp.reset (); 406 m_full_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 407 abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp); 408 if (m_frame_type != eSkipFrame) // don't override eSkipFrame 409 { 410 m_frame_type = eNormalFrame; 411 } 412 m_all_registers_available = false; 413 m_current_offset = -1; 414 m_current_offset_backed_up_one = -1; 415 RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); 416 UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0); 417 if (row.get()) 418 { 419 if (!ReadCFAValueForRow (row_register_kind, row, m_cfa)) 420 { 421 UnwindLogMsg ("failed to get cfa value"); 422 if (m_frame_type != eSkipFrame) // don't override eSkipFrame 423 { 424 m_frame_type = eNotAValidFrame; 425 } 426 return; 427 } 428 429 // A couple of sanity checks.. 430 if (m_cfa == LLDB_INVALID_ADDRESS || m_cfa == 0 || m_cfa == 1) 431 { 432 UnwindLogMsg ("could not find a valid cfa address"); 433 m_frame_type = eNotAValidFrame; 434 return; 435 } 436 437 // m_cfa should point into the stack memory; if we can query memory region permissions, 438 // see if the memory is allocated & readable. 439 if (process->GetLoadAddressPermissions(m_cfa, permissions) 440 && (permissions & ePermissionsReadable) == 0) 441 { 442 m_frame_type = eNotAValidFrame; 443 UnwindLogMsg ("the CFA points to a region of memory that is not readable"); 444 return; 445 } 446 } 447 else 448 { 449 UnwindLogMsg ("could not find a row for function offset zero"); 450 m_frame_type = eNotAValidFrame; 451 return; 452 } 453 454 if (CheckIfLoopingStack ()) 455 { 456 TryFallbackUnwindPlan(); 457 if (CheckIfLoopingStack ()) 458 { 459 UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping"); 460 m_frame_type = eNotAValidFrame; 461 return; 462 } 463 } 464 465 UnwindLogMsg ("initialized frame cfa is 0x%" PRIx64, (uint64_t) m_cfa); 466 return; 467 } 468 m_frame_type = eNotAValidFrame; 469 UnwindLogMsg ("could not find any symbol for this pc, or a default unwind plan, to continue unwind."); 470 return; 471 } 472 473 bool resolve_tail_call_address = false; // m_current_pc can be one past the address range of the function... 474 // If the saved pc does not point to a function/symbol because it is 475 // beyond the bounds of the correct function and there's no symbol there, 476 // we do *not* want ResolveSymbolContextForAddress to back up the pc by 1, 477 // because then we might not find the correct unwind information later. 478 // Instead, let ResolveSymbolContextForAddress fail, and handle the case 479 // via decr_pc_and_recompute_addr_range below. 480 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; 481 uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, 482 resolve_scope, 483 m_sym_ctx, resolve_tail_call_address); 484 485 // We require either a symbol or function in the symbols context to be successfully 486 // filled in or this context is of no use to us. 487 if (resolve_scope & resolved_scope) 488 { 489 m_sym_ctx_valid = true; 490 } 491 492 if (m_sym_ctx.symbol) 493 { 494 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", symbol name is '%s'", 495 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString("")); 496 } 497 else if (m_sym_ctx.function) 498 { 499 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", function name is '%s'", 500 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString("")); 501 } 502 else 503 { 504 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", no symbol/function name is known.", pc); 505 } 506 507 AddressRange addr_range; 508 if (!m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range)) 509 { 510 m_sym_ctx_valid = false; 511 } 512 513 bool decr_pc_and_recompute_addr_range = false; 514 515 // If the symbol lookup failed... 516 if (m_sym_ctx_valid == false) 517 decr_pc_and_recompute_addr_range = true; 518 519 // Or if we're in the middle of the stack (and not "above" an asynchronous event like sigtramp), 520 // and our "current" pc is the start of a function... 521 if (m_sym_ctx_valid 522 && GetNextFrame()->m_frame_type != eTrapHandlerFrame 523 && GetNextFrame()->m_frame_type != eDebuggerFrame 524 && addr_range.GetBaseAddress().IsValid() 525 && addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection() 526 && addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset()) 527 { 528 decr_pc_and_recompute_addr_range = true; 529 } 530 531 // We need to back up the pc by 1 byte and re-search for the Symbol to handle the case where the "saved pc" 532 // value is pointing to the next function, e.g. if a function ends with a CALL instruction. 533 // FIXME this may need to be an architectural-dependent behavior; if so we'll need to add a member function 534 // to the ABI plugin and consult that. 535 if (decr_pc_and_recompute_addr_range) 536 { 537 UnwindLogMsg ("Backing up the pc value of 0x%" PRIx64 " by 1 and re-doing symbol lookup; old symbol was %s", 538 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString("")); 539 Address temporary_pc; 540 temporary_pc.SetLoadAddress (pc - 1, &process->GetTarget()); 541 m_sym_ctx.Clear (false); 542 m_sym_ctx_valid = false; 543 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol; 544 545 ModuleSP temporary_module_sp = temporary_pc.GetModule(); 546 if (temporary_module_sp && 547 temporary_module_sp->ResolveSymbolContextForAddress (temporary_pc, resolve_scope, m_sym_ctx) & resolve_scope) 548 { 549 if (m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range)) 550 m_sym_ctx_valid = true; 551 } 552 UnwindLogMsg ("Symbol is now %s", GetSymbolOrFunctionName(m_sym_ctx).AsCString("")); 553 } 554 555 // If we were able to find a symbol/function, set addr_range_ptr to the bounds of that symbol/function. 556 // else treat the current pc value as the start_pc and record no offset. 557 if (addr_range.GetBaseAddress().IsValid()) 558 { 559 m_start_pc = addr_range.GetBaseAddress(); 560 m_current_offset = pc - m_start_pc.GetLoadAddress (&process->GetTarget()); 561 m_current_offset_backed_up_one = m_current_offset; 562 if (decr_pc_and_recompute_addr_range && m_current_offset_backed_up_one > 0) 563 { 564 m_current_offset_backed_up_one--; 565 if (m_sym_ctx_valid) 566 { 567 m_current_pc.SetLoadAddress (pc - 1, &process->GetTarget()); 568 } 569 } 570 } 571 else 572 { 573 m_start_pc = m_current_pc; 574 m_current_offset = -1; 575 m_current_offset_backed_up_one = -1; 576 } 577 578 if (IsTrapHandlerSymbol (process, m_sym_ctx)) 579 { 580 m_frame_type = eTrapHandlerFrame; 581 } 582 else 583 { 584 // FIXME: Detect eDebuggerFrame here. 585 if (m_frame_type != eSkipFrame) // don't override eSkipFrame 586 { 587 m_frame_type = eNormalFrame; 588 } 589 } 590 591 // We've set m_frame_type and m_sym_ctx before this call. 592 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame (); 593 594 UnwindPlan::RowSP active_row; 595 RegisterKind row_register_kind = eRegisterKindGeneric; 596 597 // Try to get by with just the fast UnwindPlan if possible - the full UnwindPlan may be expensive to get 598 // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for the first time.) 599 600 if (m_fast_unwind_plan_sp && m_fast_unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 601 { 602 active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 603 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind (); 604 if (active_row.get() && log) 605 { 606 StreamString active_row_strm; 607 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); 608 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str()); 609 } 610 } 611 else 612 { 613 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); 614 int valid_offset = -1; 615 if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp, valid_offset)) 616 { 617 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (valid_offset); 618 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind (); 619 if (active_row.get() && log) 620 { 621 StreamString active_row_strm; 622 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr())); 623 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str()); 624 } 625 } 626 } 627 628 if (!active_row.get()) 629 { 630 m_frame_type = eNotAValidFrame; 631 UnwindLogMsg ("could not find unwind row for this pc"); 632 return; 633 } 634 635 if (!ReadCFAValueForRow (row_register_kind, active_row, m_cfa)) 636 { 637 UnwindLogMsg ("failed to get cfa"); 638 m_frame_type = eNotAValidFrame; 639 return; 640 } 641 642 UnwindLogMsg ("m_cfa = 0x%" PRIx64, m_cfa); 643 644 if (CheckIfLoopingStack ()) 645 { 646 TryFallbackUnwindPlan(); 647 if (CheckIfLoopingStack ()) 648 { 649 UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping"); 650 m_frame_type = eNotAValidFrame; 651 return; 652 } 653 } 654 655 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64, 656 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), (uint64_t) m_cfa); 657 } 658 659 bool 660 RegisterContextLLDB::CheckIfLoopingStack () 661 { 662 // If we have a bad stack setup, we can get the same CFA value multiple times -- or even 663 // more devious, we can actually oscillate between two CFA values. Detect that here and 664 // break out to avoid a possible infinite loop in lldb trying to unwind the stack. 665 // To detect when we have the same CFA value multiple times, we compare the CFA of the current 666 // frame with the 2nd next frame because in some specail case (e.g. signal hanlders, hand 667 // written assembly without ABI compiance) we can have 2 frames with the same CFA (in theory we 668 // can have arbitrary number of frames with the same CFA, but more then 2 is very very unlikely) 669 670 RegisterContextLLDB::SharedPtr next_frame = GetNextFrame(); 671 if (next_frame) 672 { 673 RegisterContextLLDB::SharedPtr next_next_frame = next_frame->GetNextFrame(); 674 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS; 675 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) 676 { 677 if (next_next_frame_cfa == m_cfa) 678 { 679 // We have a loop in the stack unwind 680 return true; 681 } 682 } 683 } 684 return false; 685 } 686 687 bool 688 RegisterContextLLDB::IsFrameZero () const 689 { 690 return m_frame_number == 0; 691 } 692 693 694 // Find a fast unwind plan for this frame, if possible. 695 // 696 // On entry to this method, 697 // 698 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct, 699 // 2. m_sym_ctx should already be filled in, and 700 // 3. m_current_pc should have the current pc value for this frame 701 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown 702 703 UnwindPlanSP 704 RegisterContextLLDB::GetFastUnwindPlanForFrame () 705 { 706 UnwindPlanSP unwind_plan_sp; 707 ModuleSP pc_module_sp (m_current_pc.GetModule()); 708 709 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL) 710 return unwind_plan_sp; 711 712 if (IsFrameZero ()) 713 return unwind_plan_sp; 714 715 FuncUnwindersSP func_unwinders_sp (pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx)); 716 if (!func_unwinders_sp) 717 return unwind_plan_sp; 718 719 // If we're in _sigtramp(), unwinding past this frame requires special knowledge. 720 if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame) 721 return unwind_plan_sp; 722 723 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind (*m_thread.CalculateTarget(), m_thread); 724 if (unwind_plan_sp) 725 { 726 if (unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 727 { 728 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 729 if (log && log->GetVerbose()) 730 { 731 if (m_fast_unwind_plan_sp) 732 UnwindLogMsgVerbose ("frame, and has a fast UnwindPlan"); 733 else 734 UnwindLogMsgVerbose ("frame"); 735 } 736 m_frame_type = eNormalFrame; 737 return unwind_plan_sp; 738 } 739 else 740 { 741 unwind_plan_sp.reset(); 742 } 743 } 744 return unwind_plan_sp; 745 } 746 747 // On entry to this method, 748 // 749 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct, 750 // 2. m_sym_ctx should already be filled in, and 751 // 3. m_current_pc should have the current pc value for this frame 752 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown 753 754 UnwindPlanSP 755 RegisterContextLLDB::GetFullUnwindPlanForFrame () 756 { 757 UnwindPlanSP unwind_plan_sp; 758 UnwindPlanSP arch_default_unwind_plan_sp; 759 ExecutionContext exe_ctx(m_thread.shared_from_this()); 760 Process *process = exe_ctx.GetProcessPtr(); 761 ABI *abi = process ? process->GetABI().get() : NULL; 762 if (abi) 763 { 764 arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 765 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp); 766 } 767 else 768 { 769 UnwindLogMsg ("unable to get architectural default UnwindPlan from ABI plugin"); 770 } 771 772 bool behaves_like_zeroth_frame = false; 773 if (IsFrameZero () 774 || GetNextFrame()->m_frame_type == eTrapHandlerFrame 775 || GetNextFrame()->m_frame_type == eDebuggerFrame) 776 { 777 behaves_like_zeroth_frame = true; 778 // If this frame behaves like a 0th frame (currently executing or 779 // interrupted asynchronously), all registers can be retrieved. 780 m_all_registers_available = true; 781 } 782 783 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer) so the pc is 0x0 784 // in the zeroth frame, we need to use the "unwind at first instruction" arch default UnwindPlan 785 // Also, if this Process can report on memory region attributes, any non-executable region means 786 // we jumped through a bad function pointer - handle the same way as 0x0. 787 // Note, if we have a symbol context & a symbol, we don't want to follow this code path. This is 788 // for jumping to memory regions without any information available. 789 790 if ((!m_sym_ctx_valid || (m_sym_ctx.function == NULL && m_sym_ctx.symbol == NULL)) && behaves_like_zeroth_frame && m_current_pc.IsValid()) 791 { 792 uint32_t permissions; 793 addr_t current_pc_addr = m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()); 794 if (current_pc_addr == 0 795 || (process && 796 process->GetLoadAddressPermissions (current_pc_addr, permissions) 797 && (permissions & ePermissionsExecutable) == 0)) 798 { 799 if (abi) 800 { 801 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 802 abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp); 803 m_frame_type = eNormalFrame; 804 return unwind_plan_sp; 805 } 806 } 807 } 808 809 // No Module for the current pc, try using the architecture default unwind. 810 ModuleSP pc_module_sp (m_current_pc.GetModule()); 811 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL) 812 { 813 m_frame_type = eNormalFrame; 814 return arch_default_unwind_plan_sp; 815 } 816 817 FuncUnwindersSP func_unwinders_sp; 818 if (m_sym_ctx_valid) 819 { 820 func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx); 821 } 822 823 // No FuncUnwinders available for this pc (stripped function symbols, lldb could not augment its 824 // function table with another source, like LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). 825 // See if eh_frame or the .ARM.exidx tables have unwind information for this address, else fall 826 // back to the architectural default unwind. 827 if (!func_unwinders_sp) 828 { 829 m_frame_type = eNormalFrame; 830 831 if (!pc_module_sp || !pc_module_sp->GetObjectFile() || !m_current_pc.IsValid()) 832 return arch_default_unwind_plan_sp; 833 834 // Even with -fomit-frame-pointer, we can try eh_frame to get back on track. 835 DWARFCallFrameInfo *eh_frame = pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo(); 836 if (eh_frame) 837 { 838 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 839 if (eh_frame->GetUnwindPlan (m_current_pc, *unwind_plan_sp)) 840 return unwind_plan_sp; 841 else 842 unwind_plan_sp.reset(); 843 } 844 845 ArmUnwindInfo *arm_exidx = pc_module_sp->GetObjectFile()->GetUnwindTable().GetArmUnwindInfo(); 846 if (arm_exidx) 847 { 848 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 849 if (arm_exidx->GetUnwindPlan (exe_ctx.GetTargetRef(), m_current_pc, *unwind_plan_sp)) 850 return unwind_plan_sp; 851 else 852 unwind_plan_sp.reset(); 853 } 854 855 return arch_default_unwind_plan_sp; 856 } 857 858 // If we're in _sigtramp(), unwinding past this frame requires special knowledge. On Mac OS X this knowledge 859 // is properly encoded in the eh_frame section, so prefer that if available. 860 // On other platforms we may need to provide a platform-specific UnwindPlan which encodes the details of 861 // how to unwind out of sigtramp. 862 if (m_frame_type == eTrapHandlerFrame && process) 863 { 864 m_fast_unwind_plan_sp.reset(); 865 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan (process->GetTarget(), m_current_offset_backed_up_one); 866 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc) && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) 867 { 868 return unwind_plan_sp; 869 } 870 } 871 872 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame even when it's frame zero 873 // This comes up if we have hand-written functions in a Module and hand-written eh_frame. The assembly 874 // instruction inspection may fail and the eh_frame CFI were probably written with some care to do the 875 // right thing. It'd be nice if there was a way to ask the eh_frame directly if it is asynchronous 876 // (can be trusted at every instruction point) or synchronous (the normal case - only at call sites). 877 // But there is not. 878 if (process && process->GetDynamicLoader() && process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo (m_sym_ctx)) 879 { 880 // We must specifically call the GetEHFrameUnwindPlan() method here -- normally we would 881 // call GetUnwindPlanAtCallSite() -- because CallSite may return an unwind plan sourced from 882 // either eh_frame (that's what we intend) or compact unwind (this won't work) 883 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan (process->GetTarget(), m_current_offset_backed_up_one); 884 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 885 { 886 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan because the DynamicLoader suggested we prefer it", 887 unwind_plan_sp->GetSourceName().GetCString()); 888 return unwind_plan_sp; 889 } 890 } 891 892 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting the assembly language instructions 893 if (behaves_like_zeroth_frame && process) 894 { 895 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one); 896 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc)) 897 { 898 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) 899 { 900 // We probably have an UnwindPlan created by inspecting assembly instructions. The 901 // assembly profilers work really well with compiler-generated functions but hand- 902 // written assembly can be problematic. We set the eh_frame based unwind plan as our 903 // fallback unwind plan if instruction emulation doesn't work out even for non call 904 // sites if it is available and use the architecture default unwind plan if it is 905 // not available. The eh_frame unwind plan is more reliable even on non call sites 906 // then the architecture default plan and for hand written assembly code it is often 907 // written in a way that it valid at all location what helps in the most common 908 // cases when the instruction emulation fails. 909 UnwindPlanSP call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(), m_current_offset_backed_up_one); 910 if (call_site_unwind_plan && 911 call_site_unwind_plan.get() != unwind_plan_sp.get() && 912 call_site_unwind_plan->GetSourceName() != unwind_plan_sp->GetSourceName()) 913 { 914 m_fallback_unwind_plan_sp = call_site_unwind_plan; 915 } 916 else 917 { 918 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp; 919 } 920 } 921 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 922 return unwind_plan_sp; 923 } 924 } 925 926 // Typically this is unwind info from an eh_frame section intended for exception handling; only valid at call sites 927 if (process) 928 { 929 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (process->GetTarget(), m_current_offset_backed_up_one); 930 } 931 int valid_offset = -1; 932 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) 933 { 934 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 935 return unwind_plan_sp; 936 } 937 938 // We'd prefer to use an UnwindPlan intended for call sites when we're at a call site but if we've 939 // struck out on that, fall back to using the non-call-site assembly inspection UnwindPlan if possible. 940 if (process) 941 { 942 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one); 943 } 944 if (unwind_plan_sp && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) 945 { 946 // We probably have an UnwindPlan created by inspecting assembly instructions. The assembly 947 // profilers work really well with compiler-generated functions but hand- written assembly 948 // can be problematic. We set the eh_frame based unwind plan as our fallback unwind plan if 949 // instruction emulation doesn't work out even for non call sites if it is available and use 950 // the architecture default unwind plan if it is not available. The eh_frame unwind plan is 951 // more reliable even on non call sites then the architecture default plan and for hand 952 // written assembly code it is often written in a way that it valid at all location what 953 // helps in the most common cases when the instruction emulation fails. 954 UnwindPlanSP call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(), m_current_offset_backed_up_one); 955 if (call_site_unwind_plan && 956 call_site_unwind_plan.get() != unwind_plan_sp.get() && 957 call_site_unwind_plan->GetSourceName() != unwind_plan_sp->GetSourceName()) 958 { 959 m_fallback_unwind_plan_sp = call_site_unwind_plan; 960 } 961 else 962 { 963 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp; 964 } 965 } 966 967 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) 968 { 969 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 970 return unwind_plan_sp; 971 } 972 973 // If we're on the first instruction of a function, and we have an architectural default UnwindPlan 974 // for the initial instruction of a function, use that. 975 if (m_current_offset_backed_up_one == 0) 976 { 977 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry (m_thread); 978 if (unwind_plan_sp) 979 { 980 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString()); 981 return unwind_plan_sp; 982 } 983 } 984 985 // If nothing else, use the architectural default UnwindPlan and hope that does the job. 986 if (arch_default_unwind_plan_sp) 987 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", arch_default_unwind_plan_sp->GetSourceName().GetCString()); 988 else 989 UnwindLogMsg ("Unable to find any UnwindPlan for full unwind of this frame."); 990 991 return arch_default_unwind_plan_sp; 992 } 993 994 995 void 996 RegisterContextLLDB::InvalidateAllRegisters () 997 { 998 m_frame_type = eNotAValidFrame; 999 } 1000 1001 size_t 1002 RegisterContextLLDB::GetRegisterCount () 1003 { 1004 return m_thread.GetRegisterContext()->GetRegisterCount(); 1005 } 1006 1007 const RegisterInfo * 1008 RegisterContextLLDB::GetRegisterInfoAtIndex (size_t reg) 1009 { 1010 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex (reg); 1011 } 1012 1013 size_t 1014 RegisterContextLLDB::GetRegisterSetCount () 1015 { 1016 return m_thread.GetRegisterContext()->GetRegisterSetCount (); 1017 } 1018 1019 const RegisterSet * 1020 RegisterContextLLDB::GetRegisterSet (size_t reg_set) 1021 { 1022 return m_thread.GetRegisterContext()->GetRegisterSet (reg_set); 1023 } 1024 1025 uint32_t 1026 RegisterContextLLDB::ConvertRegisterKindToRegisterNumber (lldb::RegisterKind kind, uint32_t num) 1027 { 1028 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber (kind, num); 1029 } 1030 1031 bool 1032 RegisterContextLLDB::ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc, 1033 const RegisterInfo *reg_info, 1034 RegisterValue &value) 1035 { 1036 if (!IsValid()) 1037 return false; 1038 bool success = false; 1039 1040 switch (regloc.type) 1041 { 1042 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: 1043 { 1044 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); 1045 1046 if (!other_reg_info) 1047 return false; 1048 1049 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value); 1050 } 1051 break; 1052 case UnwindLLDB::RegisterLocation::eRegisterInRegister: 1053 { 1054 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); 1055 1056 if (!other_reg_info) 1057 return false; 1058 1059 if (IsFrameZero ()) 1060 { 1061 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value); 1062 } 1063 else 1064 { 1065 success = GetNextFrame()->ReadRegister (other_reg_info, value); 1066 } 1067 } 1068 break; 1069 case UnwindLLDB::RegisterLocation::eRegisterValueInferred: 1070 success = value.SetUInt (regloc.location.inferred_value, reg_info->byte_size); 1071 break; 1072 1073 case UnwindLLDB::RegisterLocation::eRegisterNotSaved: 1074 break; 1075 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation: 1076 assert ("FIXME debugger inferior function call unwind"); 1077 break; 1078 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: 1079 { 1080 Error error (ReadRegisterValueFromMemory(reg_info, 1081 regloc.location.target_memory_location, 1082 reg_info->byte_size, 1083 value)); 1084 success = error.Success(); 1085 } 1086 break; 1087 default: 1088 assert ("Unknown RegisterLocation type."); 1089 break; 1090 } 1091 return success; 1092 } 1093 1094 bool 1095 RegisterContextLLDB::WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc, 1096 const RegisterInfo *reg_info, 1097 const RegisterValue &value) 1098 { 1099 if (!IsValid()) 1100 return false; 1101 1102 bool success = false; 1103 1104 switch (regloc.type) 1105 { 1106 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: 1107 { 1108 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); 1109 success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value); 1110 } 1111 break; 1112 case UnwindLLDB::RegisterLocation::eRegisterInRegister: 1113 { 1114 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number); 1115 if (IsFrameZero ()) 1116 { 1117 success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value); 1118 } 1119 else 1120 { 1121 success = GetNextFrame()->WriteRegister (other_reg_info, value); 1122 } 1123 } 1124 break; 1125 case UnwindLLDB::RegisterLocation::eRegisterValueInferred: 1126 case UnwindLLDB::RegisterLocation::eRegisterNotSaved: 1127 break; 1128 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation: 1129 assert ("FIXME debugger inferior function call unwind"); 1130 break; 1131 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: 1132 { 1133 Error error (WriteRegisterValueToMemory (reg_info, 1134 regloc.location.target_memory_location, 1135 reg_info->byte_size, 1136 value)); 1137 success = error.Success(); 1138 } 1139 break; 1140 default: 1141 assert ("Unknown RegisterLocation type."); 1142 break; 1143 } 1144 return success; 1145 } 1146 1147 1148 bool 1149 RegisterContextLLDB::IsValid () const 1150 { 1151 return m_frame_type != eNotAValidFrame; 1152 } 1153 1154 // After the final stack frame in a stack walk we'll get one invalid (eNotAValidFrame) stack frame -- 1155 // one past the end of the stack walk. But higher-level code will need to tell the differnece between 1156 // "the unwind plan below this frame failed" versus "we successfully completed the stack walk" so 1157 // this method helps to disambiguate that. 1158 1159 bool 1160 RegisterContextLLDB::IsTrapHandlerFrame () const 1161 { 1162 return m_frame_type == eTrapHandlerFrame; 1163 } 1164 1165 // A skip frame is a bogus frame on the stack -- but one where we're likely to find a real frame farther 1166 // up the stack if we keep looking. It's always the second frame in an unwind (i.e. the first frame after 1167 // frame zero) where unwinding can be the trickiest. Ideally we'll mark up this frame in some way so the 1168 // user knows we're displaying bad data and we may have skipped one frame of their real program in the 1169 // process of getting back on track. 1170 1171 bool 1172 RegisterContextLLDB::IsSkipFrame () const 1173 { 1174 return m_frame_type == eSkipFrame; 1175 } 1176 1177 bool 1178 RegisterContextLLDB::IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const 1179 { 1180 PlatformSP platform_sp (process->GetTarget().GetPlatform()); 1181 if (platform_sp) 1182 { 1183 const std::vector<ConstString> trap_handler_names (platform_sp->GetTrapHandlerSymbolNames()); 1184 for (ConstString name : trap_handler_names) 1185 { 1186 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) || 1187 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) 1188 { 1189 return true; 1190 } 1191 } 1192 } 1193 const std::vector<ConstString> user_specified_trap_handler_names (m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames()); 1194 for (ConstString name : user_specified_trap_handler_names) 1195 { 1196 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) || 1197 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) 1198 { 1199 return true; 1200 } 1201 } 1202 1203 return false; 1204 } 1205 1206 // Answer the question: Where did THIS frame save the CALLER frame ("previous" frame)'s register value? 1207 1208 enum UnwindLLDB::RegisterSearchResult 1209 RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc) 1210 { 1211 RegisterNumber regnum (m_thread, eRegisterKindLLDB, lldb_regnum); 1212 1213 // Have we already found this register location? 1214 if (!m_registers.empty()) 1215 { 1216 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>::const_iterator iterator; 1217 iterator = m_registers.find (regnum.GetAsKind (eRegisterKindLLDB)); 1218 if (iterator != m_registers.end()) 1219 { 1220 regloc = iterator->second; 1221 UnwindLogMsg ("supplying caller's saved %s (%d)'s location, cached", 1222 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1223 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1224 } 1225 } 1226 1227 // Look through the available UnwindPlans for the register location. 1228 1229 UnwindPlan::Row::RegisterLocation unwindplan_regloc; 1230 bool have_unwindplan_regloc = false; 1231 RegisterKind unwindplan_registerkind = kNumRegisterKinds; 1232 1233 if (m_fast_unwind_plan_sp) 1234 { 1235 UnwindPlan::RowSP active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1236 unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind (); 1237 if (regnum.GetAsKind (unwindplan_registerkind) == LLDB_INVALID_REGNUM) 1238 { 1239 UnwindLogMsg ("could not convert lldb regnum %s (%d) into %d RegisterKind reg numbering scheme", 1240 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), (int) unwindplan_registerkind); 1241 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1242 } 1243 if (active_row->GetRegisterInfo (regnum.GetAsKind (unwindplan_registerkind), unwindplan_regloc)) 1244 { 1245 UnwindLogMsg ("supplying caller's saved %s (%d)'s location using FastUnwindPlan", 1246 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1247 have_unwindplan_regloc = true; 1248 } 1249 } 1250 1251 if (!have_unwindplan_regloc) 1252 { 1253 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a full UnwindPlan yet 1254 if (!m_full_unwind_plan_sp) 1255 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame (); 1256 1257 if (m_full_unwind_plan_sp) 1258 { 1259 RegisterNumber pc_regnum (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1260 1261 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1262 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind (); 1263 1264 RegisterNumber return_address_reg; 1265 1266 // If we're fetching the saved pc and this UnwindPlan defines a ReturnAddress register (e.g. lr on arm), 1267 // look for the return address register number in the UnwindPlan's row. 1268 if (pc_regnum.IsValid() 1269 && pc_regnum == regnum 1270 && m_full_unwind_plan_sp->GetReturnAddressRegister() != LLDB_INVALID_REGNUM) 1271 { 1272 1273 return_address_reg.init (m_thread, m_full_unwind_plan_sp->GetRegisterKind(), m_full_unwind_plan_sp->GetReturnAddressRegister()); 1274 regnum = return_address_reg; 1275 UnwindLogMsg ("requested caller's saved PC but this UnwindPlan uses a RA reg; getting %s (%d) instead", 1276 return_address_reg.GetName(), return_address_reg.GetAsKind (eRegisterKindLLDB)); 1277 } 1278 else 1279 { 1280 if (regnum.GetAsKind (unwindplan_registerkind) == LLDB_INVALID_REGNUM) 1281 { 1282 if (unwindplan_registerkind == eRegisterKindGeneric) 1283 { 1284 UnwindLogMsg ("could not convert lldb regnum %s (%d) into eRegisterKindGeneric reg numbering scheme", 1285 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1286 } 1287 else 1288 { 1289 UnwindLogMsg ("could not convert lldb regnum %s (%d) into %d RegisterKind reg numbering scheme", 1290 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), (int) unwindplan_registerkind); 1291 } 1292 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1293 } 1294 } 1295 1296 if (regnum.IsValid() 1297 && active_row->GetRegisterInfo (regnum.GetAsKind (unwindplan_registerkind), unwindplan_regloc)) 1298 { 1299 have_unwindplan_regloc = true; 1300 UnwindLogMsg ("supplying caller's saved %s (%d)'s location using %s UnwindPlan", 1301 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), 1302 m_full_unwind_plan_sp->GetSourceName().GetCString()); 1303 } 1304 1305 // This is frame 0 and we're retrieving the PC and it's saved in a Return Address register and 1306 // it hasn't been saved anywhere yet -- that is, it's still live in the actual register. 1307 // Handle this specially. 1308 1309 if (have_unwindplan_regloc == false 1310 && return_address_reg.IsValid() 1311 && IsFrameZero()) 1312 { 1313 if (return_address_reg.GetAsKind (eRegisterKindLLDB) != LLDB_INVALID_REGNUM) 1314 { 1315 lldb_private::UnwindLLDB::RegisterLocation new_regloc; 1316 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext; 1317 new_regloc.location.register_number = return_address_reg.GetAsKind (eRegisterKindLLDB); 1318 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = new_regloc; 1319 regloc = new_regloc; 1320 UnwindLogMsg ("supplying caller's register %s (%d) from the live RegisterContext at frame 0, saved in %d", 1321 return_address_reg.GetName(), return_address_reg.GetAsKind (eRegisterKindLLDB), 1322 return_address_reg.GetAsKind (eRegisterKindLLDB)); 1323 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1324 } 1325 } 1326 1327 // If this architecture stores the return address in a register (it defines a Return Address register) 1328 // and we're on a non-zero stack frame and the Full UnwindPlan says that the pc is stored in the 1329 // RA registers (e.g. lr on arm), then we know that the full unwindplan is not trustworthy -- this 1330 // is an impossible situation and the instruction emulation code has likely been misled. 1331 // If this stack frame meets those criteria, we need to throw away the Full UnwindPlan that the 1332 // instruction emulation came up with and fall back to the architecture's Default UnwindPlan so 1333 // the stack walk can get past this point. 1334 1335 // Special note: If the Full UnwindPlan was generated from the compiler, don't second-guess it 1336 // when we're at a call site location. 1337 1338 // arch_default_ra_regnum is the return address register # in the Full UnwindPlan register numbering 1339 RegisterNumber arch_default_ra_regnum (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA); 1340 1341 if (arch_default_ra_regnum.GetAsKind (unwindplan_registerkind) != LLDB_INVALID_REGNUM 1342 && pc_regnum == regnum 1343 && unwindplan_regloc.IsInOtherRegister() 1344 && unwindplan_regloc.GetRegisterNumber() == arch_default_ra_regnum.GetAsKind (unwindplan_registerkind) 1345 && m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes 1346 && !m_all_registers_available) 1347 { 1348 UnwindLogMsg ("%s UnwindPlan tried to restore the pc from the link register but this is a non-zero frame", 1349 m_full_unwind_plan_sp->GetSourceName().GetCString()); 1350 1351 // Throw away the full unwindplan; install the arch default unwindplan 1352 if (ForceSwitchToFallbackUnwindPlan()) 1353 { 1354 // Update for the possibly new unwind plan 1355 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind (); 1356 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1357 1358 // Sanity check: Verify that we can fetch a pc value and CFA value with this unwind plan 1359 1360 RegisterNumber arch_default_pc_reg (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1361 bool can_fetch_pc_value = false; 1362 bool can_fetch_cfa = false; 1363 addr_t cfa_value; 1364 if (active_row) 1365 { 1366 if (arch_default_pc_reg.GetAsKind (unwindplan_registerkind) != LLDB_INVALID_REGNUM 1367 && active_row->GetRegisterInfo (arch_default_pc_reg.GetAsKind (unwindplan_registerkind), unwindplan_regloc)) 1368 { 1369 can_fetch_pc_value = true; 1370 } 1371 if (ReadCFAValueForRow (unwindplan_registerkind, active_row, cfa_value)) 1372 { 1373 can_fetch_cfa = true; 1374 } 1375 } 1376 1377 if (can_fetch_pc_value && can_fetch_cfa) 1378 { 1379 have_unwindplan_regloc = true; 1380 } 1381 else 1382 { 1383 have_unwindplan_regloc = false; 1384 } 1385 } 1386 else 1387 { 1388 // We were unable to fall back to another unwind plan 1389 have_unwindplan_regloc = false; 1390 } 1391 } 1392 } 1393 } 1394 1395 ExecutionContext exe_ctx(m_thread.shared_from_this()); 1396 Process *process = exe_ctx.GetProcessPtr(); 1397 if (have_unwindplan_regloc == false) 1398 { 1399 // If the UnwindPlan failed to give us an unwind location for this register, we may be able to fall back 1400 // to some ABI-defined default. For example, some ABIs allow to determine the caller's SP via the CFA. 1401 // Also, the ABI may set volatile registers to the undefined state. 1402 ABI *abi = process ? process->GetABI().get() : NULL; 1403 if (abi) 1404 { 1405 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(regnum.GetAsKind (eRegisterKindLLDB)); 1406 if (reg_info && abi->GetFallbackRegisterLocation (reg_info, unwindplan_regloc)) 1407 { 1408 UnwindLogMsg ("supplying caller's saved %s (%d)'s location using ABI default", 1409 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1410 have_unwindplan_regloc = true; 1411 } 1412 } 1413 } 1414 1415 if (have_unwindplan_regloc == false) 1416 { 1417 if (IsFrameZero ()) 1418 { 1419 // This is frame 0 - we should return the actual live register context value 1420 lldb_private::UnwindLLDB::RegisterLocation new_regloc; 1421 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext; 1422 new_regloc.location.register_number = regnum.GetAsKind (eRegisterKindLLDB); 1423 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = new_regloc; 1424 regloc = new_regloc; 1425 UnwindLogMsg ("supplying caller's register %s (%d) from the live RegisterContext at frame 0", 1426 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1427 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1428 } 1429 else 1430 { 1431 std::string unwindplan_name (""); 1432 if (m_full_unwind_plan_sp) 1433 { 1434 unwindplan_name += "via '"; 1435 unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString(); 1436 unwindplan_name += "'"; 1437 } 1438 UnwindLogMsg ("no save location for %s (%d) %s", 1439 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), 1440 unwindplan_name.c_str()); 1441 } 1442 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1443 } 1444 1445 // unwindplan_regloc has valid contents about where to retrieve the register 1446 if (unwindplan_regloc.IsUnspecified()) 1447 { 1448 lldb_private::UnwindLLDB::RegisterLocation new_regloc; 1449 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved; 1450 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = new_regloc; 1451 UnwindLogMsg ("save location for %s (%d) is unspecified, continue searching", 1452 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1453 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1454 } 1455 1456 if (unwindplan_regloc.IsUndefined()) 1457 { 1458 UnwindLogMsg ("did not supply reg location for %s (%d) because it is volatile", 1459 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1460 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile; 1461 } 1462 1463 if (unwindplan_regloc.IsSame()) 1464 { 1465 if (IsFrameZero() == false 1466 && (regnum.GetAsKind (eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_PC 1467 || regnum.GetAsKind (eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_RA)) 1468 { 1469 UnwindLogMsg ("register %s (%d) is marked as 'IsSame' - it is a pc or return address reg on a non-zero frame -- treat as if we have no information", 1470 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1471 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1472 } 1473 else 1474 { 1475 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; 1476 regloc.location.register_number = regnum.GetAsKind (eRegisterKindLLDB); 1477 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc; 1478 UnwindLogMsg ("supplying caller's register %s (%d), saved in register %s (%d)", 1479 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), 1480 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1481 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1482 } 1483 } 1484 1485 if (unwindplan_regloc.IsCFAPlusOffset()) 1486 { 1487 int offset = unwindplan_regloc.GetOffset(); 1488 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; 1489 regloc.location.inferred_value = m_cfa + offset; 1490 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc; 1491 UnwindLogMsg ("supplying caller's register %s (%d), value is CFA plus offset %d [value is 0x%" PRIx64 "]", 1492 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), 1493 offset, regloc.location.inferred_value); 1494 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1495 } 1496 1497 if (unwindplan_regloc.IsAtCFAPlusOffset()) 1498 { 1499 int offset = unwindplan_regloc.GetOffset(); 1500 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation; 1501 regloc.location.target_memory_location = m_cfa + offset; 1502 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc; 1503 UnwindLogMsg ("supplying caller's register %s (%d) from the stack, saved at CFA plus offset %d [saved at 0x%" PRIx64 "]", 1504 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), 1505 offset, regloc.location.target_memory_location); 1506 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1507 } 1508 1509 if (unwindplan_regloc.IsInOtherRegister()) 1510 { 1511 uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber(); 1512 RegisterNumber row_regnum (m_thread, unwindplan_registerkind, unwindplan_regnum); 1513 if (row_regnum.GetAsKind (eRegisterKindLLDB) == LLDB_INVALID_REGNUM) 1514 { 1515 UnwindLogMsg ("could not supply caller's %s (%d) location - was saved in another reg but couldn't convert that regnum", 1516 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1517 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1518 } 1519 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister; 1520 regloc.location.register_number = row_regnum.GetAsKind (eRegisterKindLLDB); 1521 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc; 1522 UnwindLogMsg ("supplying caller's register %s (%d), saved in register %s (%d)", 1523 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), 1524 row_regnum.GetName(), row_regnum.GetAsKind (eRegisterKindLLDB)); 1525 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1526 } 1527 1528 if (unwindplan_regloc.IsDWARFExpression() || unwindplan_regloc.IsAtDWARFExpression()) 1529 { 1530 DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(), 1531 unwindplan_regloc.GetDWARFExpressionLength(), 1532 process->GetByteOrder(), process->GetAddressByteSize()); 1533 ModuleSP opcode_ctx; 1534 DWARFExpression dwarfexpr (opcode_ctx, 1535 dwarfdata, 1536 nullptr, 1537 0, 1538 unwindplan_regloc.GetDWARFExpressionLength()); 1539 dwarfexpr.SetRegisterKind (unwindplan_registerkind); 1540 Value result; 1541 Error error; 1542 if (dwarfexpr.Evaluate (&exe_ctx, nullptr, nullptr, this, 0, nullptr, nullptr, result, &error)) 1543 { 1544 addr_t val; 1545 val = result.GetScalar().ULongLong(); 1546 if (unwindplan_regloc.IsDWARFExpression()) 1547 { 1548 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred; 1549 regloc.location.inferred_value = val; 1550 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc; 1551 UnwindLogMsg ("supplying caller's register %s (%d) via DWARF expression (IsDWARFExpression)", 1552 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1553 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1554 } 1555 else 1556 { 1557 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation; 1558 regloc.location.target_memory_location = val; 1559 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc; 1560 UnwindLogMsg ("supplying caller's register %s (%d) via DWARF expression (IsAtDWARFExpression)", 1561 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1562 return UnwindLLDB::RegisterSearchResult::eRegisterFound; 1563 } 1564 } 1565 UnwindLogMsg ("tried to use IsDWARFExpression or IsAtDWARFExpression for %s (%d) but failed", 1566 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1567 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1568 } 1569 1570 UnwindLogMsg ("no save location for %s (%d) in this stack frame", 1571 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB)); 1572 1573 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are unsupported. 1574 1575 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound; 1576 } 1577 1578 // TryFallbackUnwindPlan() -- this method is a little tricky. 1579 // 1580 // When this is called, the frame above -- the caller frame, the "previous" frame -- 1581 // is invalid or bad. 1582 // 1583 // Instead of stopping the stack walk here, we'll try a different UnwindPlan and see 1584 // if we can get a valid frame above us. 1585 // 1586 // This most often happens when an unwind plan based on assembly instruction inspection 1587 // is not correct -- mostly with hand-written assembly functions or functions where the 1588 // stack frame is set up "out of band", e.g. the kernel saved the register context and 1589 // then called an asynchronous trap handler like _sigtramp. 1590 // 1591 // Often in these cases, if we just do a dumb stack walk we'll get past this tricky 1592 // frame and our usual techniques can continue to be used. 1593 1594 bool 1595 RegisterContextLLDB::TryFallbackUnwindPlan () 1596 { 1597 if (m_fallback_unwind_plan_sp.get() == nullptr) 1598 return false; 1599 1600 if (m_full_unwind_plan_sp.get() == nullptr) 1601 return false; 1602 1603 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() 1604 || m_full_unwind_plan_sp->GetSourceName() == m_fallback_unwind_plan_sp->GetSourceName()) 1605 { 1606 return false; 1607 } 1608 1609 // If a compiler generated unwind plan failed, trying the arch default unwindplan 1610 // isn't going to do any better. 1611 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) 1612 return false; 1613 1614 1615 // Get the caller's pc value and our own CFA value. 1616 // Swap in the fallback unwind plan, re-fetch the caller's pc value and CFA value. 1617 // If they're the same, then the fallback unwind plan provides no benefit. 1618 1619 RegisterNumber pc_regnum (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 1620 1621 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS; 1622 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS; 1623 addr_t old_this_frame_cfa_value = m_cfa; 1624 UnwindLLDB::RegisterLocation regloc; 1625 if (SavedLocationForRegister (pc_regnum.GetAsKind (eRegisterKindLLDB), regloc) == UnwindLLDB::RegisterSearchResult::eRegisterFound) 1626 { 1627 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(pc_regnum.GetAsKind (eRegisterKindLLDB)); 1628 if (reg_info) 1629 { 1630 RegisterValue reg_value; 1631 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value)) 1632 { 1633 old_caller_pc_value = reg_value.GetAsUInt64(); 1634 } 1635 } 1636 } 1637 1638 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really impossible 1639 // register location for the full unwind plan, it may call ForceSwitchToFallbackUnwindPlan() 1640 // which in turn replaces the full unwindplan with the fallback... in short, we're done, 1641 // we're using the fallback UnwindPlan. 1642 // We checked if m_fallback_unwind_plan_sp was nullptr at the top -- the only way it 1643 // became nullptr since then is via SavedLocationForRegister(). 1644 if (m_fallback_unwind_plan_sp.get() == nullptr) 1645 return true; 1646 1647 1648 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide this isn't 1649 // working, we need to restore. 1650 // We'll also need to save & restore the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'. 1651 UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp; 1652 addr_t old_cfa = m_cfa; 1653 1654 m_registers.clear(); 1655 1656 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp; 1657 1658 UnwindPlan::RowSP active_row = m_fallback_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1659 1660 if (active_row && active_row->GetCFAValue().GetValueType() != UnwindPlan::Row::CFAValue::unspecified) 1661 { 1662 addr_t new_cfa; 1663 if (!ReadCFAValueForRow (m_fallback_unwind_plan_sp->GetRegisterKind(), active_row, new_cfa) 1664 || new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) 1665 { 1666 UnwindLogMsg ("failed to get cfa with fallback unwindplan"); 1667 m_fallback_unwind_plan_sp.reset(); 1668 m_full_unwind_plan_sp = original_full_unwind_plan_sp; 1669 m_cfa = old_cfa; 1670 return false; 1671 } 1672 m_cfa = new_cfa; 1673 1674 if (SavedLocationForRegister (pc_regnum.GetAsKind (eRegisterKindLLDB), regloc) == UnwindLLDB::RegisterSearchResult::eRegisterFound) 1675 { 1676 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(pc_regnum.GetAsKind (eRegisterKindLLDB)); 1677 if (reg_info) 1678 { 1679 RegisterValue reg_value; 1680 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value)) 1681 { 1682 new_caller_pc_value = reg_value.GetAsUInt64(); 1683 } 1684 } 1685 } 1686 1687 1688 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) 1689 { 1690 UnwindLogMsg ("failed to get a pc value for the caller frame with the fallback unwind plan"); 1691 m_fallback_unwind_plan_sp.reset(); 1692 m_full_unwind_plan_sp = original_full_unwind_plan_sp; 1693 m_cfa = old_cfa; 1694 return false; 1695 } 1696 1697 if (old_caller_pc_value != LLDB_INVALID_ADDRESS) 1698 { 1699 if (old_caller_pc_value == new_caller_pc_value && new_cfa == old_this_frame_cfa_value) 1700 { 1701 UnwindLogMsg ("fallback unwind plan got the same values for this frame CFA and caller frame pc, not using"); 1702 m_fallback_unwind_plan_sp.reset(); 1703 m_full_unwind_plan_sp = original_full_unwind_plan_sp; 1704 m_cfa = old_cfa; 1705 return false; 1706 } 1707 } 1708 1709 UnwindLogMsg ("trying to unwind from this function with the UnwindPlan '%s' because UnwindPlan '%s' failed.", 1710 m_fallback_unwind_plan_sp->GetSourceName().GetCString(), 1711 original_full_unwind_plan_sp->GetSourceName().GetCString()); 1712 1713 // We've copied the fallback unwind plan into the full - now clear the fallback. 1714 m_fallback_unwind_plan_sp.reset(); 1715 } 1716 1717 return true; 1718 } 1719 1720 bool 1721 RegisterContextLLDB::ForceSwitchToFallbackUnwindPlan () 1722 { 1723 if (m_fallback_unwind_plan_sp.get() == NULL) 1724 return false; 1725 1726 if (m_full_unwind_plan_sp.get() == NULL) 1727 return false; 1728 1729 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() 1730 || m_full_unwind_plan_sp->GetSourceName() == m_fallback_unwind_plan_sp->GetSourceName()) 1731 { 1732 return false; 1733 } 1734 1735 UnwindPlan::RowSP active_row = m_fallback_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset); 1736 1737 if (active_row && active_row->GetCFAValue().GetValueType() != UnwindPlan::Row::CFAValue::unspecified) 1738 { 1739 addr_t new_cfa; 1740 if (!ReadCFAValueForRow (m_fallback_unwind_plan_sp->GetRegisterKind(), active_row, new_cfa) 1741 || new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) 1742 { 1743 UnwindLogMsg ("failed to get cfa with fallback unwindplan"); 1744 m_fallback_unwind_plan_sp.reset(); 1745 return false; 1746 } 1747 1748 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp; 1749 m_fallback_unwind_plan_sp.reset(); 1750 1751 m_registers.clear(); 1752 1753 m_cfa = new_cfa; 1754 1755 UnwindLogMsg ("switched unconditionally to the fallback unwindplan %s", m_full_unwind_plan_sp->GetSourceName().GetCString()); 1756 return true; 1757 } 1758 return false; 1759 } 1760 1761 bool 1762 RegisterContextLLDB::ReadCFAValueForRow (lldb::RegisterKind row_register_kind, 1763 const UnwindPlan::RowSP &row, 1764 addr_t &cfa_value) 1765 { 1766 RegisterValue reg_value; 1767 1768 cfa_value = LLDB_INVALID_ADDRESS; 1769 addr_t cfa_reg_contents; 1770 1771 switch (row->GetCFAValue().GetValueType()) 1772 { 1773 case UnwindPlan::Row::CFAValue::isRegisterDereferenced: 1774 { 1775 RegisterNumber cfa_reg (m_thread, row_register_kind, row->GetCFAValue().GetRegisterNumber()); 1776 if (ReadGPRValue (cfa_reg, cfa_reg_contents)) 1777 { 1778 const RegisterInfo *reg_info = GetRegisterInfoAtIndex (cfa_reg.GetAsKind (eRegisterKindLLDB)); 1779 RegisterValue reg_value; 1780 if (reg_info) 1781 { 1782 Error error = ReadRegisterValueFromMemory(reg_info, 1783 cfa_reg_contents, 1784 reg_info->byte_size, 1785 reg_value); 1786 if (error.Success ()) 1787 { 1788 cfa_value = reg_value.GetAsUInt64(); 1789 UnwindLogMsg ("CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64 ", CFA value is 0x%" PRIx64, 1790 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB), 1791 cfa_reg_contents, cfa_value); 1792 return true; 1793 } 1794 else 1795 { 1796 UnwindLogMsg ("Tried to deref reg %s (%d) [0x%" PRIx64 "] but memory read failed.", 1797 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB), 1798 cfa_reg_contents); 1799 } 1800 } 1801 } 1802 break; 1803 } 1804 case UnwindPlan::Row::CFAValue::isRegisterPlusOffset: 1805 { 1806 RegisterNumber cfa_reg (m_thread, row_register_kind, row->GetCFAValue().GetRegisterNumber()); 1807 if (ReadGPRValue (cfa_reg, cfa_reg_contents)) 1808 { 1809 if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 || cfa_reg_contents == 1) 1810 { 1811 UnwindLogMsg ("Got an invalid CFA register value - reg %s (%d), value 0x%" PRIx64, 1812 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB), 1813 cfa_reg_contents); 1814 cfa_reg_contents = LLDB_INVALID_ADDRESS; 1815 return false; 1816 } 1817 cfa_value = cfa_reg_contents + row->GetCFAValue().GetOffset(); 1818 UnwindLogMsg ("CFA is 0x%" PRIx64 ": Register %s (%d) contents are 0x%" PRIx64 ", offset is %d", 1819 cfa_value, 1820 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB), 1821 cfa_reg_contents, row->GetCFAValue().GetOffset()); 1822 return true; 1823 } 1824 break; 1825 } 1826 case UnwindPlan::Row::CFAValue::isDWARFExpression: 1827 { 1828 ExecutionContext exe_ctx(m_thread.shared_from_this()); 1829 Process *process = exe_ctx.GetProcessPtr(); 1830 DataExtractor dwarfdata (row->GetCFAValue().GetDWARFExpressionBytes(), 1831 row->GetCFAValue().GetDWARFExpressionLength(), 1832 process->GetByteOrder(), process->GetAddressByteSize()); 1833 ModuleSP opcode_ctx; 1834 DWARFExpression dwarfexpr (opcode_ctx, 1835 dwarfdata, 1836 nullptr, 1837 0, 1838 row->GetCFAValue().GetDWARFExpressionLength()); 1839 dwarfexpr.SetRegisterKind (row_register_kind); 1840 Value result; 1841 Error error; 1842 if (dwarfexpr.Evaluate (&exe_ctx, nullptr, nullptr, this, 0, nullptr, nullptr, result, &error)) 1843 { 1844 cfa_value = result.GetScalar().ULongLong(); 1845 1846 UnwindLogMsg ("CFA value set by DWARF expression is 0x%" PRIx64, cfa_value); 1847 return true; 1848 } 1849 UnwindLogMsg ("Failed to set CFA value via DWARF expression: %s", error.AsCString()); 1850 break; 1851 } 1852 default: 1853 return false; 1854 } 1855 return false; 1856 } 1857 1858 // Retrieve a general purpose register value for THIS frame, as saved by the NEXT frame, i.e. the frame that 1859 // this frame called. e.g. 1860 // 1861 // foo () { } 1862 // bar () { foo (); } 1863 // main () { bar (); } 1864 // 1865 // stopped in foo() so 1866 // frame 0 - foo 1867 // frame 1 - bar 1868 // frame 2 - main 1869 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc value for frame 1, we need to ask 1870 // where frame 0 (the "next" frame) saved that and retrieve the value. 1871 1872 bool 1873 RegisterContextLLDB::ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, addr_t &value) 1874 { 1875 if (!IsValid()) 1876 return false; 1877 1878 uint32_t lldb_regnum; 1879 if (register_kind == eRegisterKindLLDB) 1880 { 1881 lldb_regnum = regnum; 1882 } 1883 else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) 1884 { 1885 return false; 1886 } 1887 1888 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum); 1889 RegisterValue reg_value; 1890 // if this is frame 0 (currently executing frame), get the requested reg contents from the actual thread registers 1891 if (IsFrameZero ()) 1892 { 1893 if (m_thread.GetRegisterContext()->ReadRegister (reg_info, reg_value)) 1894 { 1895 value = reg_value.GetAsUInt64(); 1896 return true; 1897 } 1898 return false; 1899 } 1900 1901 bool pc_register = false; 1902 uint32_t generic_regnum; 1903 if (register_kind == eRegisterKindGeneric 1904 && (regnum == LLDB_REGNUM_GENERIC_PC || regnum == LLDB_REGNUM_GENERIC_RA)) 1905 { 1906 pc_register = true; 1907 } 1908 else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindGeneric, generic_regnum) 1909 && (generic_regnum == LLDB_REGNUM_GENERIC_PC || generic_regnum == LLDB_REGNUM_GENERIC_RA)) 1910 { 1911 pc_register = true; 1912 } 1913 1914 lldb_private::UnwindLLDB::RegisterLocation regloc; 1915 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, pc_register)) 1916 { 1917 return false; 1918 } 1919 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value)) 1920 { 1921 value = reg_value.GetAsUInt64(); 1922 return true; 1923 } 1924 return false; 1925 } 1926 1927 bool 1928 RegisterContextLLDB::ReadGPRValue (const RegisterNumber ®num, addr_t &value) 1929 { 1930 return ReadGPRValue (regnum.GetRegisterKind(), regnum.GetRegisterNumber(), value); 1931 } 1932 1933 // Find the value of a register in THIS frame 1934 1935 bool 1936 RegisterContextLLDB::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value) 1937 { 1938 if (!IsValid()) 1939 return false; 1940 1941 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB]; 1942 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum); 1943 1944 // If this is the 0th frame, hand this over to the live register context 1945 if (IsFrameZero ()) 1946 { 1947 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum); 1948 return m_thread.GetRegisterContext()->ReadRegister (reg_info, value); 1949 } 1950 1951 bool is_pc_regnum = false; 1952 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_PC 1953 || reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA) 1954 { 1955 is_pc_regnum = true; 1956 } 1957 1958 lldb_private::UnwindLLDB::RegisterLocation regloc; 1959 // Find out where the NEXT frame saved THIS frame's register contents 1960 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum)) 1961 return false; 1962 1963 return ReadRegisterValueFromRegisterLocation (regloc, reg_info, value); 1964 } 1965 1966 bool 1967 RegisterContextLLDB::WriteRegister (const RegisterInfo *reg_info, const RegisterValue &value) 1968 { 1969 if (!IsValid()) 1970 return false; 1971 1972 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB]; 1973 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum); 1974 1975 // If this is the 0th frame, hand this over to the live register context 1976 if (IsFrameZero ()) 1977 { 1978 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum); 1979 return m_thread.GetRegisterContext()->WriteRegister (reg_info, value); 1980 } 1981 1982 lldb_private::UnwindLLDB::RegisterLocation regloc; 1983 // Find out where the NEXT frame saved THIS frame's register contents 1984 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false)) 1985 return false; 1986 1987 return WriteRegisterValueToRegisterLocation (regloc, reg_info, value); 1988 } 1989 1990 // Don't need to implement this one 1991 bool 1992 RegisterContextLLDB::ReadAllRegisterValues (lldb::DataBufferSP &data_sp) 1993 { 1994 return false; 1995 } 1996 1997 // Don't need to implement this one 1998 bool 1999 RegisterContextLLDB::WriteAllRegisterValues (const lldb::DataBufferSP& data_sp) 2000 { 2001 return false; 2002 } 2003 2004 // Retrieve the pc value for THIS from 2005 2006 bool 2007 RegisterContextLLDB::GetCFA (addr_t& cfa) 2008 { 2009 if (!IsValid()) 2010 { 2011 return false; 2012 } 2013 if (m_cfa == LLDB_INVALID_ADDRESS) 2014 { 2015 return false; 2016 } 2017 cfa = m_cfa; 2018 return true; 2019 } 2020 2021 2022 RegisterContextLLDB::SharedPtr 2023 RegisterContextLLDB::GetNextFrame () const 2024 { 2025 RegisterContextLLDB::SharedPtr regctx; 2026 if (m_frame_number == 0) 2027 return regctx; 2028 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number - 1); 2029 } 2030 2031 RegisterContextLLDB::SharedPtr 2032 RegisterContextLLDB::GetPrevFrame () const 2033 { 2034 RegisterContextLLDB::SharedPtr regctx; 2035 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number + 1); 2036 } 2037 2038 // Retrieve the address of the start of the function of THIS frame 2039 2040 bool 2041 RegisterContextLLDB::GetStartPC (addr_t& start_pc) 2042 { 2043 if (!IsValid()) 2044 return false; 2045 2046 if (!m_start_pc.IsValid()) 2047 { 2048 return ReadPC (start_pc); 2049 } 2050 start_pc = m_start_pc.GetLoadAddress (CalculateTarget().get()); 2051 return true; 2052 } 2053 2054 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame. 2055 2056 bool 2057 RegisterContextLLDB::ReadPC (addr_t& pc) 2058 { 2059 if (!IsValid()) 2060 return false; 2061 2062 bool above_trap_handler = false; 2063 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsTrapHandlerFrame()) 2064 above_trap_handler = true; 2065 2066 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) 2067 { 2068 // A pc value of 0 or 1 is impossible in the middle of the stack -- it indicates the end of a stack walk. 2069 // On the currently executing frame (or such a frame interrupted asynchronously by sigtramp et al) this may 2070 // occur if code has jumped through a NULL pointer -- we want to be able to unwind past that frame to help 2071 // find the bug. 2072 2073 if (m_all_registers_available == false 2074 && above_trap_handler == false 2075 && (pc == 0 || pc == 1)) 2076 { 2077 return false; 2078 } 2079 else 2080 { 2081 return true; 2082 } 2083 } 2084 else 2085 { 2086 return false; 2087 } 2088 } 2089 2090 2091 void 2092 RegisterContextLLDB::UnwindLogMsg (const char *fmt, ...) 2093 { 2094 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 2095 if (log) 2096 { 2097 va_list args; 2098 va_start (args, fmt); 2099 2100 char *logmsg; 2101 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL) 2102 { 2103 if (logmsg) 2104 free (logmsg); 2105 va_end (args); 2106 return; 2107 } 2108 va_end (args); 2109 2110 log->Printf ("%*sth%d/fr%u %s", 2111 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number, 2112 logmsg); 2113 free (logmsg); 2114 } 2115 } 2116 2117 void 2118 RegisterContextLLDB::UnwindLogMsgVerbose (const char *fmt, ...) 2119 { 2120 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND)); 2121 if (log && log->GetVerbose()) 2122 { 2123 va_list args; 2124 va_start (args, fmt); 2125 2126 char *logmsg; 2127 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL) 2128 { 2129 if (logmsg) 2130 free (logmsg); 2131 va_end (args); 2132 return; 2133 } 2134 va_end (args); 2135 2136 log->Printf ("%*sth%d/fr%u %s", 2137 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number, 2138 logmsg); 2139 free (logmsg); 2140 } 2141 } 2142 2143 2144