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