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