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