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