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