1 //===-- RegisterContextUnwind.cpp -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
45 using namespace lldb_private
;
47 static ConstString
GetSymbolOrFunctionName(const SymbolContext
&sym_ctx
) {
49 return sym_ctx
.symbol
->GetName();
50 else if (sym_ctx
.function
)
51 return sym_ctx
.function
->GetName();
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(false);
70 m_sym_ctx_valid
= false;
73 InitializeZerothFrame();
75 InitializeNonZerothFrame();
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;
86 bool RegisterContextUnwind::IsUnwindPlanValidForCurrentPC(
87 lldb::UnwindPlanSP unwind_plan_sp
) {
91 // check if m_current_pc is valid
92 if (unwind_plan_sp
->PlanValidAtAddress(m_current_pc
)) {
93 // yes - current offset can be used as is
97 // if m_current_offset <= 0, we've got nothing else to try
98 if (m_current_offset
<= 0)
101 // check pc - 1 to see if it's valid
102 Address
pc_minus_one(m_current_pc
);
103 pc_minus_one
.SetOffset(m_current_pc
.GetOffset() - 1);
104 if (unwind_plan_sp
->PlanValidAtAddress(pc_minus_one
)) {
111 // Initialize a RegisterContextUnwind which is the first frame of a stack -- the
112 // zeroth frame or currently executing frame.
114 void RegisterContextUnwind::InitializeZerothFrame() {
115 Log
*log
= GetLog(LLDBLog::Unwind
);
116 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
117 RegisterContextSP reg_ctx_sp
= m_thread
.GetRegisterContext();
119 if (reg_ctx_sp
.get() == nullptr) {
120 m_frame_type
= eNotAValidFrame
;
121 UnwindLogMsg("frame does not have a register context");
125 addr_t current_pc
= reg_ctx_sp
->GetPC();
127 if (current_pc
== LLDB_INVALID_ADDRESS
) {
128 m_frame_type
= eNotAValidFrame
;
129 UnwindLogMsg("frame does not have a pc");
133 Process
*process
= exe_ctx
.GetProcessPtr();
135 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
136 // this will strip bit zero in case we read a PC from memory or from the LR.
137 // (which would be a no-op in frame 0 where we get it from the register set,
138 // but still a good idea to make the call here for other ABIs that may
140 if (ABISP abi_sp
= process
->GetABI())
141 current_pc
= abi_sp
->FixCodeAddress(current_pc
);
143 UnwindPlanSP lang_runtime_plan_sp
= LanguageRuntime::GetRuntimeUnwindPlan(
144 m_thread
, this, m_behaves_like_zeroth_frame
);
145 if (lang_runtime_plan_sp
.get()) {
146 UnwindLogMsg("This is an async frame");
149 // Initialize m_current_pc, an Address object, based on current_pc, an
151 m_current_pc
.SetLoadAddress(current_pc
, &process
->GetTarget());
153 // If we don't have a Module for some reason, we're not going to find
154 // symbol/function information - just stick in some reasonable defaults and
155 // hope we can unwind past this frame.
156 ModuleSP
pc_module_sp(m_current_pc
.GetModule());
157 if (!m_current_pc
.IsValid() || !pc_module_sp
) {
158 UnwindLogMsg("using architectural default unwind method");
161 AddressRange addr_range
;
162 m_sym_ctx_valid
= m_current_pc
.ResolveFunctionScope(m_sym_ctx
, &addr_range
);
164 if (m_sym_ctx
.symbol
) {
165 UnwindLogMsg("with pc value of 0x%" PRIx64
", symbol name is '%s'",
166 current_pc
, GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
167 } else if (m_sym_ctx
.function
) {
168 UnwindLogMsg("with pc value of 0x%" PRIx64
", function name is '%s'",
169 current_pc
, GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
171 UnwindLogMsg("with pc value of 0x%" PRIx64
172 ", no symbol/function name is known.",
176 if (IsTrapHandlerSymbol(process
, m_sym_ctx
)) {
177 m_frame_type
= eTrapHandlerFrame
;
179 // FIXME: Detect eDebuggerFrame here.
180 m_frame_type
= eNormalFrame
;
183 // If we were able to find a symbol/function, set addr_range to the bounds of
184 // that symbol/function. else treat the current pc value as the start_pc and
186 if (addr_range
.GetBaseAddress().IsValid()) {
187 m_start_pc
= addr_range
.GetBaseAddress();
188 if (m_current_pc
.GetSection() == m_start_pc
.GetSection()) {
189 m_current_offset
= m_current_pc
.GetOffset() - m_start_pc
.GetOffset();
190 } else if (m_current_pc
.GetModule() == m_start_pc
.GetModule()) {
191 // This means that whatever symbol we kicked up isn't really correct ---
192 // we should not cross section boundaries ... We really should NULL out
193 // the function/symbol in this case unless there is a bad assumption here
194 // due to inlined functions?
196 m_current_pc
.GetFileAddress() - m_start_pc
.GetFileAddress();
198 m_current_offset_backed_up_one
= m_current_offset
;
200 m_start_pc
= m_current_pc
;
201 m_current_offset
= -1;
202 m_current_offset_backed_up_one
= -1;
205 // We've set m_frame_type and m_sym_ctx before these calls.
207 m_fast_unwind_plan_sp
= GetFastUnwindPlanForFrame();
208 m_full_unwind_plan_sp
= GetFullUnwindPlanForFrame();
210 UnwindPlan::RowSP active_row
;
211 lldb::RegisterKind row_register_kind
= eRegisterKindGeneric
;
213 // If we have LanguageRuntime UnwindPlan for this unwind, use those
214 // rules to find the caller frame instead of the function's normal
215 // UnwindPlans. The full unwind plan for this frame will be
216 // the LanguageRuntime-provided unwind plan, and there will not be a
218 if (lang_runtime_plan_sp
.get()) {
220 lang_runtime_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
221 row_register_kind
= lang_runtime_plan_sp
->GetRegisterKind();
222 if (!ReadFrameAddress(row_register_kind
, active_row
->GetCFAValue(),
224 UnwindLogMsg("Cannot set cfa");
226 m_full_unwind_plan_sp
= lang_runtime_plan_sp
;
228 StreamString active_row_strm
;
229 active_row
->Dump(active_row_strm
, lang_runtime_plan_sp
.get(), &m_thread
,
230 m_start_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()));
231 UnwindLogMsg("async active row: %s", active_row_strm
.GetData());
233 UnwindLogMsg("m_cfa = 0x%" PRIx64
" m_afa = 0x%" PRIx64
, m_cfa
, m_afa
);
235 "initialized async frame current pc is 0x%" PRIx64
236 " cfa is 0x%" PRIx64
" afa is 0x%" PRIx64
,
237 (uint64_t)m_current_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()),
238 (uint64_t)m_cfa
, (uint64_t)m_afa
);
244 if (m_full_unwind_plan_sp
&&
245 m_full_unwind_plan_sp
->PlanValidAtAddress(m_current_pc
)) {
247 m_full_unwind_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
248 row_register_kind
= m_full_unwind_plan_sp
->GetRegisterKind();
249 if (active_row
.get() && log
) {
250 StreamString active_row_strm
;
251 active_row
->Dump(active_row_strm
, m_full_unwind_plan_sp
.get(), &m_thread
,
252 m_start_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()));
253 UnwindLogMsg("%s", active_row_strm
.GetData());
257 if (!active_row
.get()) {
258 UnwindLogMsg("could not find an unwindplan row for this frame's pc");
259 m_frame_type
= eNotAValidFrame
;
263 if (!ReadFrameAddress(row_register_kind
, active_row
->GetCFAValue(), m_cfa
)) {
264 // Try the fall back unwind plan since the
265 // full unwind plan failed.
266 FuncUnwindersSP func_unwinders_sp
;
267 UnwindPlanSP call_site_unwind_plan
;
268 bool cfa_status
= false;
270 if (m_sym_ctx_valid
) {
272 pc_module_sp
->GetUnwindTable().GetFuncUnwindersContainingAddress(
273 m_current_pc
, m_sym_ctx
);
276 if (func_unwinders_sp
.get() != nullptr)
277 call_site_unwind_plan
= func_unwinders_sp
->GetUnwindPlanAtCallSite(
278 process
->GetTarget(), m_thread
);
280 if (call_site_unwind_plan
.get() != nullptr) {
281 m_fallback_unwind_plan_sp
= call_site_unwind_plan
;
282 if (TryFallbackUnwindPlan())
286 UnwindLogMsg("could not read CFA value for first frame.");
287 m_frame_type
= eNotAValidFrame
;
291 ReadFrameAddress(row_register_kind
, active_row
->GetAFAValue(), m_afa
);
293 if (m_cfa
== LLDB_INVALID_ADDRESS
&& m_afa
== LLDB_INVALID_ADDRESS
) {
295 "could not read CFA or AFA values for first frame, not valid.");
296 m_frame_type
= eNotAValidFrame
;
300 UnwindLogMsg("initialized frame current pc is 0x%" PRIx64
" cfa is 0x%" PRIx64
301 " afa is 0x%" PRIx64
" using %s UnwindPlan",
302 (uint64_t)m_current_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()),
305 m_full_unwind_plan_sp
->GetSourceName().GetCString());
308 // Initialize a RegisterContextUnwind for the non-zeroth frame -- rely on the
309 // RegisterContextUnwind "below" it to provide things like its current pc value.
311 void RegisterContextUnwind::InitializeNonZerothFrame() {
312 Log
*log
= GetLog(LLDBLog::Unwind
);
314 m_frame_type
= eNotAValidFrame
;
315 UnwindLogMsg("non-zeroth frame tests positive for IsFrameZero -- that "
316 "shouldn't happen.");
320 if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) {
321 m_frame_type
= eNotAValidFrame
;
322 UnwindLogMsg("Could not get next frame, marking this frame as invalid.");
325 if (!m_thread
.GetRegisterContext()) {
326 m_frame_type
= eNotAValidFrame
;
327 UnwindLogMsg("Could not get register context for this thread, marking this "
328 "frame as invalid.");
332 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
333 Process
*process
= exe_ctx
.GetProcessPtr();
335 // Some languages may have a logical parent stack frame which is
336 // not a real stack frame, but the programmer would consider it to
337 // be the caller of the frame, e.g. Swift asynchronous frames.
339 // A LanguageRuntime may provide an UnwindPlan that is used in this
340 // stack trace base on the RegisterContext contents, intsead
341 // of the normal UnwindPlans we would use for the return-pc.
342 UnwindPlanSP lang_runtime_plan_sp
= LanguageRuntime::GetRuntimeUnwindPlan(
343 m_thread
, this, m_behaves_like_zeroth_frame
);
344 if (lang_runtime_plan_sp
.get()) {
345 UnwindLogMsg("This is an async frame");
349 if (!ReadGPRValue(eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_PC
, pc
)) {
350 UnwindLogMsg("could not get pc value");
351 m_frame_type
= eNotAValidFrame
;
355 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
356 // this will strip bit zero in case we read a PC from memory or from the LR.
357 ABISP abi_sp
= process
->GetABI();
359 pc
= abi_sp
->FixCodeAddress(pc
);
362 UnwindLogMsg("pc = 0x%" PRIx64
, pc
);
364 if (ReadGPRValue(eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_FP
, reg_val
)) {
366 reg_val
= abi_sp
->FixDataAddress(reg_val
);
367 UnwindLogMsg("fp = 0x%" PRIx64
, reg_val
);
369 if (ReadGPRValue(eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_SP
, reg_val
)) {
371 reg_val
= abi_sp
->FixDataAddress(reg_val
);
372 UnwindLogMsg("sp = 0x%" PRIx64
, reg_val
);
376 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap
378 bool above_trap_handler
= false;
379 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
380 GetNextFrame()->IsTrapHandlerFrame())
381 above_trap_handler
= true;
383 if (pc
== 0 || pc
== 0x1) {
384 if (!above_trap_handler
) {
385 m_frame_type
= eNotAValidFrame
;
386 UnwindLogMsg("this frame has a pc of 0x0");
391 const bool allow_section_end
= true;
392 m_current_pc
.SetLoadAddress(pc
, &process
->GetTarget(), allow_section_end
);
394 // If we don't have a Module for some reason, we're not going to find
395 // symbol/function information - just stick in some reasonable defaults and
396 // hope we can unwind past this frame. If we're above a trap handler,
397 // we may be at a bogus address because we jumped through a bogus function
398 // pointer and trapped, so don't force the arch default unwind plan in that
400 ModuleSP
pc_module_sp(m_current_pc
.GetModule());
401 if ((!m_current_pc
.IsValid() || !pc_module_sp
) &&
402 above_trap_handler
== false) {
403 UnwindLogMsg("using architectural default unwind method");
405 // Test the pc value to see if we know it's in an unmapped/non-executable
407 uint32_t permissions
;
408 if (process
->GetLoadAddressPermissions(pc
, permissions
) &&
409 (permissions
& ePermissionsExecutable
) == 0) {
410 // If this is the second frame off the stack, we may have unwound the
411 // first frame incorrectly. But using the architecture default unwind
412 // plan may get us back on track -- albeit possibly skipping a real
413 // frame. Give this frame a clearly-invalid pc and see if we can get any
415 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
416 GetNextFrame()->IsFrameZero()) {
417 UnwindLogMsg("had a pc of 0x%" PRIx64
" which is not in executable "
418 "memory but on frame 1 -- "
421 m_frame_type
= eSkipFrame
;
423 // anywhere other than the second frame, a non-executable pc means
424 // we're off in the weeds -- stop now.
425 m_frame_type
= eNotAValidFrame
;
426 UnwindLogMsg("pc is in a non-executable section of memory and this "
427 "isn't the 2nd frame in the stack walk.");
433 m_fast_unwind_plan_sp
.reset();
434 m_full_unwind_plan_sp
=
435 std::make_shared
<UnwindPlan
>(lldb::eRegisterKindGeneric
);
436 abi_sp
->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp
);
437 if (m_frame_type
!= eSkipFrame
) // don't override eSkipFrame
439 m_frame_type
= eNormalFrame
;
441 m_all_registers_available
= false;
442 m_current_offset
= -1;
443 m_current_offset_backed_up_one
= -1;
444 RegisterKind row_register_kind
= m_full_unwind_plan_sp
->GetRegisterKind();
445 UnwindPlan::RowSP row
= m_full_unwind_plan_sp
->GetRowForFunctionOffset(0);
447 if (!ReadFrameAddress(row_register_kind
, row
->GetCFAValue(), m_cfa
)) {
448 UnwindLogMsg("failed to get cfa value");
449 if (m_frame_type
!= eSkipFrame
) // don't override eSkipFrame
451 m_frame_type
= eNotAValidFrame
;
456 ReadFrameAddress(row_register_kind
, row
->GetAFAValue(), m_afa
);
458 // A couple of sanity checks..
459 if (m_cfa
== LLDB_INVALID_ADDRESS
|| m_cfa
== 0 || m_cfa
== 1) {
460 UnwindLogMsg("could not find a valid cfa address");
461 m_frame_type
= eNotAValidFrame
;
465 // m_cfa should point into the stack memory; if we can query memory
466 // region permissions, see if the memory is allocated & readable.
467 if (process
->GetLoadAddressPermissions(m_cfa
, permissions
) &&
468 (permissions
& ePermissionsReadable
) == 0) {
469 m_frame_type
= eNotAValidFrame
;
471 "the CFA points to a region of memory that is not readable");
475 UnwindLogMsg("could not find a row for function offset zero");
476 m_frame_type
= eNotAValidFrame
;
480 if (CheckIfLoopingStack()) {
481 TryFallbackUnwindPlan();
482 if (CheckIfLoopingStack()) {
483 UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
484 "looping - stopping");
485 m_frame_type
= eNotAValidFrame
;
490 UnwindLogMsg("initialized frame cfa is 0x%" PRIx64
" afa is 0x%" PRIx64
,
491 (uint64_t)m_cfa
, (uint64_t)m_afa
);
494 m_frame_type
= eNotAValidFrame
;
495 UnwindLogMsg("could not find any symbol for this pc, or a default unwind "
496 "plan, to continue unwind.");
500 AddressRange addr_range
;
501 m_sym_ctx_valid
= m_current_pc
.ResolveFunctionScope(m_sym_ctx
, &addr_range
);
503 if (m_sym_ctx
.symbol
) {
504 UnwindLogMsg("with pc value of 0x%" PRIx64
", symbol name is '%s'", pc
,
505 GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
506 } else if (m_sym_ctx
.function
) {
507 UnwindLogMsg("with pc value of 0x%" PRIx64
", function name is '%s'", pc
,
508 GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
510 UnwindLogMsg("with pc value of 0x%" PRIx64
511 ", no symbol/function name is known.",
515 bool decr_pc_and_recompute_addr_range
;
517 if (!m_sym_ctx_valid
) {
518 // Always decrement and recompute if the symbol lookup failed
519 decr_pc_and_recompute_addr_range
= true;
520 } else if (GetNextFrame()->m_frame_type
== eTrapHandlerFrame
||
521 GetNextFrame()->m_frame_type
== eDebuggerFrame
) {
522 // Don't decrement if we're "above" an asynchronous event like
524 decr_pc_and_recompute_addr_range
= false;
525 } else if (!addr_range
.GetBaseAddress().IsValid() ||
526 addr_range
.GetBaseAddress().GetSection() != m_current_pc
.GetSection() ||
527 addr_range
.GetBaseAddress().GetOffset() != m_current_pc
.GetOffset()) {
528 // If our "current" pc isn't the start of a function, decrement the pc
529 // if we're up the stack.
530 if (m_behaves_like_zeroth_frame
)
531 decr_pc_and_recompute_addr_range
= false;
533 decr_pc_and_recompute_addr_range
= true;
534 } else if (IsTrapHandlerSymbol(process
, m_sym_ctx
)) {
535 // Signal dispatch may set the return address of the handler it calls to
536 // point to the first byte of a return trampoline (like __kernel_rt_sigreturn),
537 // so do not decrement and recompute if the symbol we already found is a trap
539 decr_pc_and_recompute_addr_range
= false;
540 } else if (m_behaves_like_zeroth_frame
) {
541 decr_pc_and_recompute_addr_range
= false;
543 // Decrement to find the function containing the call.
544 decr_pc_and_recompute_addr_range
= true;
547 // We need to back up the pc by 1 byte and re-search for the Symbol to handle
548 // the case where the "saved pc" value is pointing to the next function, e.g.
549 // if a function ends with a CALL instruction.
550 // FIXME this may need to be an architectural-dependent behavior; if so we'll
551 // need to add a member function
552 // to the ABI plugin and consult that.
553 if (decr_pc_and_recompute_addr_range
) {
554 UnwindLogMsg("Backing up the pc value of 0x%" PRIx64
555 " by 1 and re-doing symbol lookup; old symbol was %s",
556 pc
, GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
557 Address temporary_pc
;
558 temporary_pc
.SetLoadAddress(pc
- 1, &process
->GetTarget());
559 m_sym_ctx
.Clear(false);
560 m_sym_ctx_valid
= temporary_pc
.ResolveFunctionScope(m_sym_ctx
, &addr_range
);
562 UnwindLogMsg("Symbol is now %s",
563 GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
566 // If we were able to find a symbol/function, set addr_range_ptr to the
567 // bounds of that symbol/function. else treat the current pc value as the
568 // start_pc and record no offset.
569 if (addr_range
.GetBaseAddress().IsValid()) {
570 m_start_pc
= addr_range
.GetBaseAddress();
571 m_current_offset
= pc
- m_start_pc
.GetLoadAddress(&process
->GetTarget());
572 m_current_offset_backed_up_one
= m_current_offset
;
573 if (decr_pc_and_recompute_addr_range
&&
574 m_current_offset_backed_up_one
> 0) {
575 m_current_offset_backed_up_one
--;
576 if (m_sym_ctx_valid
) {
577 m_current_pc
.SetLoadAddress(pc
- 1, &process
->GetTarget());
581 m_start_pc
= m_current_pc
;
582 m_current_offset
= -1;
583 m_current_offset_backed_up_one
= -1;
586 if (IsTrapHandlerSymbol(process
, m_sym_ctx
)) {
587 m_frame_type
= eTrapHandlerFrame
;
589 // FIXME: Detect eDebuggerFrame here.
590 if (m_frame_type
!= eSkipFrame
) // don't override eSkipFrame
592 m_frame_type
= eNormalFrame
;
596 UnwindPlan::RowSP active_row
;
597 RegisterKind row_register_kind
= eRegisterKindGeneric
;
599 // If we have LanguageRuntime UnwindPlan for this unwind, use those
600 // rules to find the caller frame instead of the function's normal
601 // UnwindPlans. The full unwind plan for this frame will be
602 // the LanguageRuntime-provided unwind plan, and there will not be a
604 if (lang_runtime_plan_sp
.get()) {
606 lang_runtime_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
607 row_register_kind
= lang_runtime_plan_sp
->GetRegisterKind();
608 if (!ReadFrameAddress(row_register_kind
, active_row
->GetCFAValue(),
610 UnwindLogMsg("Cannot set cfa");
612 m_full_unwind_plan_sp
= lang_runtime_plan_sp
;
614 StreamString active_row_strm
;
615 active_row
->Dump(active_row_strm
, lang_runtime_plan_sp
.get(), &m_thread
,
616 m_start_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()));
617 UnwindLogMsg("async active row: %s", active_row_strm
.GetData());
619 UnwindLogMsg("m_cfa = 0x%" PRIx64
" m_afa = 0x%" PRIx64
, m_cfa
, m_afa
);
621 "initialized async frame current pc is 0x%" PRIx64
622 " cfa is 0x%" PRIx64
" afa is 0x%" PRIx64
,
623 (uint64_t)m_current_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()),
624 (uint64_t)m_cfa
, (uint64_t)m_afa
);
630 // We've set m_frame_type and m_sym_ctx before this call.
631 m_fast_unwind_plan_sp
= GetFastUnwindPlanForFrame();
633 // Try to get by with just the fast UnwindPlan if possible - the full
634 // UnwindPlan may be expensive to get (e.g. if we have to parse the entire
635 // eh_frame section of an ObjectFile for the first time.)
637 if (m_fast_unwind_plan_sp
&&
638 m_fast_unwind_plan_sp
->PlanValidAtAddress(m_current_pc
)) {
640 m_fast_unwind_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
641 row_register_kind
= m_fast_unwind_plan_sp
->GetRegisterKind();
642 PropagateTrapHandlerFlagFromUnwindPlan(m_fast_unwind_plan_sp
);
643 if (active_row
.get() && log
) {
644 StreamString active_row_strm
;
645 active_row
->Dump(active_row_strm
, m_fast_unwind_plan_sp
.get(), &m_thread
,
646 m_start_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()));
647 UnwindLogMsg("Using fast unwind plan '%s'",
648 m_fast_unwind_plan_sp
->GetSourceName().AsCString());
649 UnwindLogMsg("active row: %s", active_row_strm
.GetData());
652 m_full_unwind_plan_sp
= GetFullUnwindPlanForFrame();
653 if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp
)) {
654 active_row
= m_full_unwind_plan_sp
->GetRowForFunctionOffset(
655 m_current_offset_backed_up_one
);
656 row_register_kind
= m_full_unwind_plan_sp
->GetRegisterKind();
657 PropagateTrapHandlerFlagFromUnwindPlan(m_full_unwind_plan_sp
);
658 if (active_row
.get() && log
) {
659 StreamString active_row_strm
;
660 active_row
->Dump(active_row_strm
, m_full_unwind_plan_sp
.get(),
662 m_start_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()));
663 UnwindLogMsg("Using full unwind plan '%s'",
664 m_full_unwind_plan_sp
->GetSourceName().AsCString());
665 UnwindLogMsg("active row: %s", active_row_strm
.GetData());
670 if (!active_row
.get()) {
671 m_frame_type
= eNotAValidFrame
;
672 UnwindLogMsg("could not find unwind row for this pc");
676 if (!ReadFrameAddress(row_register_kind
, active_row
->GetCFAValue(), m_cfa
)) {
677 UnwindLogMsg("failed to get cfa");
678 m_frame_type
= eNotAValidFrame
;
682 ReadFrameAddress(row_register_kind
, active_row
->GetAFAValue(), m_afa
);
684 UnwindLogMsg("m_cfa = 0x%" PRIx64
" m_afa = 0x%" PRIx64
, m_cfa
, m_afa
);
686 if (CheckIfLoopingStack()) {
687 TryFallbackUnwindPlan();
688 if (CheckIfLoopingStack()) {
689 UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
690 "looping - stopping");
691 m_frame_type
= eNotAValidFrame
;
696 UnwindLogMsg("initialized frame current pc is 0x%" PRIx64
697 " cfa is 0x%" PRIx64
" afa is 0x%" PRIx64
,
698 (uint64_t)m_current_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()),
703 bool RegisterContextUnwind::CheckIfLoopingStack() {
704 // If we have a bad stack setup, we can get the same CFA value multiple times
705 // -- or even more devious, we can actually oscillate between two CFA values.
706 // Detect that here and break out to avoid a possible infinite loop in lldb
707 // trying to unwind the stack. To detect when we have the same CFA value
708 // multiple times, we compare the
709 // CFA of the current
710 // frame with the 2nd next frame because in some specail case (e.g. signal
711 // hanlders, hand written assembly without ABI compliance) we can have 2
712 // frames with the same
714 // can have arbitrary number of frames with the same CFA, but more then 2 is
717 RegisterContextUnwind::SharedPtr next_frame
= GetNextFrame();
719 RegisterContextUnwind::SharedPtr next_next_frame
=
720 next_frame
->GetNextFrame();
721 addr_t next_next_frame_cfa
= LLDB_INVALID_ADDRESS
;
722 if (next_next_frame
&& next_next_frame
->GetCFA(next_next_frame_cfa
)) {
723 if (next_next_frame_cfa
== m_cfa
) {
724 // We have a loop in the stack unwind
732 bool RegisterContextUnwind::IsFrameZero() const { return m_frame_number
== 0; }
734 bool RegisterContextUnwind::BehavesLikeZerothFrame() const {
735 if (m_frame_number
== 0)
737 if (m_behaves_like_zeroth_frame
)
742 // Find a fast unwind plan for this frame, if possible.
744 // On entry to this method,
746 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
747 // if either of those are correct,
748 // 2. m_sym_ctx should already be filled in, and
749 // 3. m_current_pc should have the current pc value for this frame
750 // 4. m_current_offset_backed_up_one should have the current byte offset into
751 // the function, maybe backed up by 1, -1 if unknown
753 UnwindPlanSP
RegisterContextUnwind::GetFastUnwindPlanForFrame() {
754 UnwindPlanSP unwind_plan_sp
;
755 ModuleSP
pc_module_sp(m_current_pc
.GetModule());
757 if (!m_current_pc
.IsValid() || !pc_module_sp
||
758 pc_module_sp
->GetObjectFile() == nullptr)
759 return unwind_plan_sp
;
762 return unwind_plan_sp
;
764 FuncUnwindersSP
func_unwinders_sp(
765 pc_module_sp
->GetUnwindTable().GetFuncUnwindersContainingAddress(
766 m_current_pc
, m_sym_ctx
));
767 if (!func_unwinders_sp
)
768 return unwind_plan_sp
;
770 // If we're in _sigtramp(), unwinding past this frame requires special
772 if (m_frame_type
== eTrapHandlerFrame
|| m_frame_type
== eDebuggerFrame
)
773 return unwind_plan_sp
;
775 unwind_plan_sp
= func_unwinders_sp
->GetUnwindPlanFastUnwind(
776 *m_thread
.CalculateTarget(), m_thread
);
777 if (unwind_plan_sp
) {
778 if (unwind_plan_sp
->PlanValidAtAddress(m_current_pc
)) {
779 m_frame_type
= eNormalFrame
;
780 return unwind_plan_sp
;
782 unwind_plan_sp
.reset();
785 return unwind_plan_sp
;
788 // On entry to this method,
790 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
791 // if either of those are correct,
792 // 2. m_sym_ctx should already be filled in, and
793 // 3. m_current_pc should have the current pc value for this frame
794 // 4. m_current_offset_backed_up_one should have the current byte offset into
795 // the function, maybe backed up by 1, -1 if unknown
797 UnwindPlanSP
RegisterContextUnwind::GetFullUnwindPlanForFrame() {
798 UnwindPlanSP unwind_plan_sp
;
799 UnwindPlanSP arch_default_unwind_plan_sp
;
800 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
801 Process
*process
= exe_ctx
.GetProcessPtr();
802 ABI
*abi
= process
? process
->GetABI().get() : nullptr;
804 arch_default_unwind_plan_sp
=
805 std::make_shared
<UnwindPlan
>(lldb::eRegisterKindGeneric
);
806 abi
->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp
);
809 "unable to get architectural default UnwindPlan from ABI plugin");
812 if (IsFrameZero() || GetNextFrame()->m_frame_type
== eTrapHandlerFrame
||
813 GetNextFrame()->m_frame_type
== eDebuggerFrame
) {
814 m_behaves_like_zeroth_frame
= true;
815 // If this frame behaves like a 0th frame (currently executing or
816 // interrupted asynchronously), all registers can be retrieved.
817 m_all_registers_available
= true;
820 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
821 // so the pc is 0x0 in the zeroth frame, we need to use the "unwind at first
822 // instruction" arch default UnwindPlan Also, if this Process can report on
823 // memory region attributes, any non-executable region means we jumped
824 // through a bad function pointer - handle the same way as 0x0. Note, if we
825 // have a symbol context & a symbol, we don't want to follow this code path.
826 // This is for jumping to memory regions without any information available.
828 if ((!m_sym_ctx_valid
||
829 (m_sym_ctx
.function
== nullptr && m_sym_ctx
.symbol
== nullptr)) &&
830 m_behaves_like_zeroth_frame
&& m_current_pc
.IsValid()) {
831 uint32_t permissions
;
832 addr_t current_pc_addr
=
833 m_current_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr());
834 if (current_pc_addr
== 0 ||
836 process
->GetLoadAddressPermissions(current_pc_addr
, permissions
) &&
837 (permissions
& ePermissionsExecutable
) == 0)) {
840 std::make_shared
<UnwindPlan
>(lldb::eRegisterKindGeneric
);
841 abi
->CreateFunctionEntryUnwindPlan(*unwind_plan_sp
);
842 m_frame_type
= eNormalFrame
;
843 return unwind_plan_sp
;
848 // No Module for the current pc, try using the architecture default unwind.
849 ModuleSP
pc_module_sp(m_current_pc
.GetModule());
850 if (!m_current_pc
.IsValid() || !pc_module_sp
||
851 pc_module_sp
->GetObjectFile() == nullptr) {
852 m_frame_type
= eNormalFrame
;
853 return arch_default_unwind_plan_sp
;
856 FuncUnwindersSP func_unwinders_sp
;
857 if (m_sym_ctx_valid
) {
859 pc_module_sp
->GetUnwindTable().GetFuncUnwindersContainingAddress(
860 m_current_pc
, m_sym_ctx
);
863 // No FuncUnwinders available for this pc (stripped function symbols, lldb
864 // could not augment its function table with another source, like
865 // LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). See if eh_frame or the
866 // .ARM.exidx tables have unwind information for this address, else fall back
867 // to the architectural default unwind.
868 if (!func_unwinders_sp
) {
869 m_frame_type
= eNormalFrame
;
871 if (!pc_module_sp
|| !pc_module_sp
->GetObjectFile() ||
872 !m_current_pc
.IsValid())
873 return arch_default_unwind_plan_sp
;
875 // Even with -fomit-frame-pointer, we can try eh_frame to get back on
877 DWARFCallFrameInfo
*eh_frame
=
878 pc_module_sp
->GetUnwindTable().GetEHFrameInfo();
880 unwind_plan_sp
= std::make_shared
<UnwindPlan
>(lldb::eRegisterKindGeneric
);
881 if (eh_frame
->GetUnwindPlan(m_current_pc
, *unwind_plan_sp
))
882 return unwind_plan_sp
;
884 unwind_plan_sp
.reset();
887 ArmUnwindInfo
*arm_exidx
=
888 pc_module_sp
->GetUnwindTable().GetArmUnwindInfo();
890 unwind_plan_sp
= std::make_shared
<UnwindPlan
>(lldb::eRegisterKindGeneric
);
891 if (arm_exidx
->GetUnwindPlan(exe_ctx
.GetTargetRef(), m_current_pc
,
893 return unwind_plan_sp
;
895 unwind_plan_sp
.reset();
898 CallFrameInfo
*object_file_unwind
=
899 pc_module_sp
->GetUnwindTable().GetObjectFileUnwindInfo();
900 if (object_file_unwind
) {
901 unwind_plan_sp
= std::make_shared
<UnwindPlan
>(lldb::eRegisterKindGeneric
);
902 if (object_file_unwind
->GetUnwindPlan(m_current_pc
, *unwind_plan_sp
))
903 return unwind_plan_sp
;
905 unwind_plan_sp
.reset();
908 return arch_default_unwind_plan_sp
;
911 if (m_frame_type
== eTrapHandlerFrame
&& process
) {
912 m_fast_unwind_plan_sp
.reset();
914 // On some platforms the unwind information for signal handlers is not
915 // present or correct. Give the platform plugins a chance to provide
916 // substitute plan. Otherwise, use eh_frame.
917 if (m_sym_ctx_valid
) {
918 lldb::PlatformSP platform
= process
->GetTarget().GetPlatform();
919 unwind_plan_sp
= platform
->GetTrapHandlerUnwindPlan(
920 process
->GetTarget().GetArchitecture().GetTriple(),
921 GetSymbolOrFunctionName(m_sym_ctx
));
924 return unwind_plan_sp
;
928 func_unwinders_sp
->GetEHFrameUnwindPlan(process
->GetTarget());
931 func_unwinders_sp
->GetObjectFileUnwindPlan(process
->GetTarget());
932 if (unwind_plan_sp
&& unwind_plan_sp
->PlanValidAtAddress(m_current_pc
) &&
933 unwind_plan_sp
->GetSourcedFromCompiler() == eLazyBoolYes
) {
934 return unwind_plan_sp
;
938 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
939 // even when it's frame zero This comes up if we have hand-written functions
940 // in a Module and hand-written eh_frame. The assembly instruction
941 // inspection may fail and the eh_frame CFI were probably written with some
942 // care to do the right thing. It'd be nice if there was a way to ask the
943 // eh_frame directly if it is asynchronous (can be trusted at every
944 // instruction point) or synchronous (the normal case - only at call sites).
946 if (process
&& process
->GetDynamicLoader() &&
947 process
->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo(m_sym_ctx
)) {
948 // We must specifically call the GetEHFrameUnwindPlan() method here --
949 // normally we would call GetUnwindPlanAtCallSite() -- because CallSite may
950 // return an unwind plan sourced from either eh_frame (that's what we
951 // intend) or compact unwind (this won't work)
953 func_unwinders_sp
->GetEHFrameUnwindPlan(process
->GetTarget());
956 func_unwinders_sp
->GetObjectFileUnwindPlan(process
->GetTarget());
957 if (unwind_plan_sp
&& unwind_plan_sp
->PlanValidAtAddress(m_current_pc
)) {
958 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because the "
959 "DynamicLoader suggested we prefer it",
960 unwind_plan_sp
->GetSourceName().GetCString());
961 return unwind_plan_sp
;
965 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
966 // the assembly language instructions
967 if (m_behaves_like_zeroth_frame
&& process
) {
968 unwind_plan_sp
= func_unwinders_sp
->GetUnwindPlanAtNonCallSite(
969 process
->GetTarget(), m_thread
);
970 if (unwind_plan_sp
&& unwind_plan_sp
->PlanValidAtAddress(m_current_pc
)) {
971 if (unwind_plan_sp
->GetSourcedFromCompiler() == eLazyBoolNo
) {
972 // We probably have an UnwindPlan created by inspecting assembly
973 // instructions. The assembly profilers work really well with compiler-
974 // generated functions but hand- written assembly can be problematic.
975 // We set the eh_frame based unwind plan as our fallback unwind plan if
976 // instruction emulation doesn't work out even for non call sites if it
977 // is available and use the architecture default unwind plan if it is
978 // not available. The eh_frame unwind plan is more reliable even on non
979 // call sites then the architecture default plan and for hand written
980 // assembly code it is often written in a way that it valid at all
981 // location what helps in the most common cases when the instruction
983 UnwindPlanSP call_site_unwind_plan
=
984 func_unwinders_sp
->GetUnwindPlanAtCallSite(process
->GetTarget(),
986 if (call_site_unwind_plan
&&
987 call_site_unwind_plan
.get() != unwind_plan_sp
.get() &&
988 call_site_unwind_plan
->GetSourceName() !=
989 unwind_plan_sp
->GetSourceName()) {
990 m_fallback_unwind_plan_sp
= call_site_unwind_plan
;
992 m_fallback_unwind_plan_sp
= arch_default_unwind_plan_sp
;
995 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because this "
996 "is the non-call site unwind plan and this is a "
998 unwind_plan_sp
->GetSourceName().GetCString());
999 return unwind_plan_sp
;
1002 // If we're on the first instruction of a function, and we have an
1003 // architectural default UnwindPlan for the initial instruction of a
1004 // function, use that.
1005 if (m_current_offset
== 0) {
1007 func_unwinders_sp
->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
1009 if (unwind_plan_sp
) {
1010 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because we are at "
1011 "the first instruction of a function",
1012 unwind_plan_sp
->GetSourceName().GetCString());
1013 return unwind_plan_sp
;
1018 // Typically this is unwind info from an eh_frame section intended for
1019 // exception handling; only valid at call sites
1021 unwind_plan_sp
= func_unwinders_sp
->GetUnwindPlanAtCallSite(
1022 process
->GetTarget(), m_thread
);
1024 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp
)) {
1025 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because this "
1026 "is the call-site unwind plan",
1027 unwind_plan_sp
->GetSourceName().GetCString());
1028 return unwind_plan_sp
;
1031 // We'd prefer to use an UnwindPlan intended for call sites when we're at a
1032 // call site but if we've struck out on that, fall back to using the non-
1033 // call-site assembly inspection UnwindPlan if possible.
1035 unwind_plan_sp
= func_unwinders_sp
->GetUnwindPlanAtNonCallSite(
1036 process
->GetTarget(), m_thread
);
1038 if (unwind_plan_sp
&&
1039 unwind_plan_sp
->GetSourcedFromCompiler() == eLazyBoolNo
) {
1040 // We probably have an UnwindPlan created by inspecting assembly
1041 // instructions. The assembly profilers work really well with compiler-
1042 // generated functions but hand- written assembly can be problematic. We
1043 // set the eh_frame based unwind plan as our fallback unwind plan if
1044 // instruction emulation doesn't work out even for non call sites if it is
1045 // available and use the architecture default unwind plan if it is not
1046 // available. The eh_frame unwind plan is more reliable even on non call
1047 // sites then the architecture default plan and for hand written assembly
1048 // code it is often written in a way that it valid at all location what
1049 // helps in the most common cases when the instruction emulation fails.
1050 UnwindPlanSP call_site_unwind_plan
=
1051 func_unwinders_sp
->GetUnwindPlanAtCallSite(process
->GetTarget(),
1053 if (call_site_unwind_plan
&&
1054 call_site_unwind_plan
.get() != unwind_plan_sp
.get() &&
1055 call_site_unwind_plan
->GetSourceName() !=
1056 unwind_plan_sp
->GetSourceName()) {
1057 m_fallback_unwind_plan_sp
= call_site_unwind_plan
;
1059 m_fallback_unwind_plan_sp
= arch_default_unwind_plan_sp
;
1063 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp
)) {
1064 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because we "
1065 "failed to find a call-site unwind plan that would work",
1066 unwind_plan_sp
->GetSourceName().GetCString());
1067 return unwind_plan_sp
;
1070 // If nothing else, use the architectural default UnwindPlan and hope that
1072 if (arch_default_unwind_plan_sp
)
1073 UnwindLogMsgVerbose(
1074 "frame uses %s for full UnwindPlan because we are falling back "
1075 "to the arch default plan",
1076 arch_default_unwind_plan_sp
->GetSourceName().GetCString());
1079 "Unable to find any UnwindPlan for full unwind of this frame.");
1081 return arch_default_unwind_plan_sp
;
1084 void RegisterContextUnwind::InvalidateAllRegisters() {
1085 m_frame_type
= eNotAValidFrame
;
1088 size_t RegisterContextUnwind::GetRegisterCount() {
1089 return m_thread
.GetRegisterContext()->GetRegisterCount();
1092 const RegisterInfo
*RegisterContextUnwind::GetRegisterInfoAtIndex(size_t reg
) {
1093 return m_thread
.GetRegisterContext()->GetRegisterInfoAtIndex(reg
);
1096 size_t RegisterContextUnwind::GetRegisterSetCount() {
1097 return m_thread
.GetRegisterContext()->GetRegisterSetCount();
1100 const RegisterSet
*RegisterContextUnwind::GetRegisterSet(size_t reg_set
) {
1101 return m_thread
.GetRegisterContext()->GetRegisterSet(reg_set
);
1104 uint32_t RegisterContextUnwind::ConvertRegisterKindToRegisterNumber(
1105 lldb::RegisterKind kind
, uint32_t num
) {
1106 return m_thread
.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
1110 bool RegisterContextUnwind::ReadRegisterValueFromRegisterLocation(
1111 lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc
,
1112 const RegisterInfo
*reg_info
, RegisterValue
&value
) {
1115 bool success
= false;
1117 switch (regloc
.type
) {
1118 case UnwindLLDB::ConcreteRegisterLocation::eRegisterInLiveRegisterContext
: {
1119 const RegisterInfo
*other_reg_info
=
1120 GetRegisterInfoAtIndex(regloc
.location
.register_number
);
1122 if (!other_reg_info
)
1126 m_thread
.GetRegisterContext()->ReadRegister(other_reg_info
, value
);
1128 case UnwindLLDB::ConcreteRegisterLocation::eRegisterInRegister
: {
1129 const RegisterInfo
*other_reg_info
=
1130 GetRegisterInfoAtIndex(regloc
.location
.register_number
);
1132 if (!other_reg_info
)
1135 if (IsFrameZero()) {
1137 m_thread
.GetRegisterContext()->ReadRegister(other_reg_info
, value
);
1139 success
= GetNextFrame()->ReadRegister(other_reg_info
, value
);
1142 case UnwindLLDB::ConcreteRegisterLocation::eRegisterValueInferred
:
1144 value
.SetUInt(regloc
.location
.inferred_value
, reg_info
->byte_size
);
1147 case UnwindLLDB::ConcreteRegisterLocation::eRegisterNotSaved
:
1149 case UnwindLLDB::ConcreteRegisterLocation::eRegisterSavedAtHostMemoryLocation
:
1150 llvm_unreachable("FIXME debugger inferior function call unwind");
1151 case UnwindLLDB::ConcreteRegisterLocation::eRegisterSavedAtMemoryLocation
: {
1152 Status
error(ReadRegisterValueFromMemory(
1153 reg_info
, regloc
.location
.target_memory_location
, reg_info
->byte_size
,
1155 success
= error
.Success();
1158 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1163 bool RegisterContextUnwind::WriteRegisterValueToRegisterLocation(
1164 lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc
,
1165 const RegisterInfo
*reg_info
, const RegisterValue
&value
) {
1169 bool success
= false;
1171 switch (regloc
.type
) {
1172 case UnwindLLDB::ConcreteRegisterLocation::eRegisterInLiveRegisterContext
: {
1173 const RegisterInfo
*other_reg_info
=
1174 GetRegisterInfoAtIndex(regloc
.location
.register_number
);
1176 m_thread
.GetRegisterContext()->WriteRegister(other_reg_info
, value
);
1178 case UnwindLLDB::ConcreteRegisterLocation::eRegisterInRegister
: {
1179 const RegisterInfo
*other_reg_info
=
1180 GetRegisterInfoAtIndex(regloc
.location
.register_number
);
1181 if (IsFrameZero()) {
1183 m_thread
.GetRegisterContext()->WriteRegister(other_reg_info
, value
);
1185 success
= GetNextFrame()->WriteRegister(other_reg_info
, value
);
1188 case UnwindLLDB::ConcreteRegisterLocation::eRegisterValueInferred
:
1189 case UnwindLLDB::ConcreteRegisterLocation::eRegisterNotSaved
:
1191 case UnwindLLDB::ConcreteRegisterLocation::eRegisterSavedAtHostMemoryLocation
:
1192 llvm_unreachable("FIXME debugger inferior function call unwind");
1193 case UnwindLLDB::ConcreteRegisterLocation::eRegisterSavedAtMemoryLocation
: {
1194 Status
error(WriteRegisterValueToMemory(
1195 reg_info
, regloc
.location
.target_memory_location
, reg_info
->byte_size
,
1197 success
= error
.Success();
1200 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1205 bool RegisterContextUnwind::IsValid() const {
1206 return m_frame_type
!= eNotAValidFrame
;
1209 // After the final stack frame in a stack walk we'll get one invalid
1210 // (eNotAValidFrame) stack frame -- one past the end of the stack walk. But
1211 // higher-level code will need to tell the difference between "the unwind plan
1212 // below this frame failed" versus "we successfully completed the stack walk"
1213 // so this method helps to disambiguate that.
1215 bool RegisterContextUnwind::IsTrapHandlerFrame() const {
1216 return m_frame_type
== eTrapHandlerFrame
;
1219 // A skip frame is a bogus frame on the stack -- but one where we're likely to
1220 // find a real frame farther
1221 // up the stack if we keep looking. It's always the second frame in an unwind
1222 // (i.e. the first frame after frame zero) where unwinding can be the
1223 // trickiest. Ideally we'll mark up this frame in some way so the user knows
1224 // we're displaying bad data and we may have skipped one frame of their real
1225 // program in the process of getting back on track.
1227 bool RegisterContextUnwind::IsSkipFrame() const {
1228 return m_frame_type
== eSkipFrame
;
1231 bool RegisterContextUnwind::IsTrapHandlerSymbol(
1232 lldb_private::Process
*process
,
1233 const lldb_private::SymbolContext
&m_sym_ctx
) const {
1234 PlatformSP
platform_sp(process
->GetTarget().GetPlatform());
1236 const std::vector
<ConstString
> trap_handler_names(
1237 platform_sp
->GetTrapHandlerSymbolNames());
1238 for (ConstString name
: trap_handler_names
) {
1239 if ((m_sym_ctx
.function
&& m_sym_ctx
.function
->GetName() == name
) ||
1240 (m_sym_ctx
.symbol
&& m_sym_ctx
.symbol
->GetName() == name
)) {
1245 const std::vector
<ConstString
> user_specified_trap_handler_names(
1246 m_parent_unwind
.GetUserSpecifiedTrapHandlerFunctionNames());
1247 for (ConstString name
: user_specified_trap_handler_names
) {
1248 if ((m_sym_ctx
.function
&& m_sym_ctx
.function
->GetName() == name
) ||
1249 (m_sym_ctx
.symbol
&& m_sym_ctx
.symbol
->GetName() == name
)) {
1257 // Answer the question: Where did THIS frame save the CALLER frame ("previous"
1258 // frame)'s register value?
1260 enum UnwindLLDB::RegisterSearchResult
1261 RegisterContextUnwind::SavedLocationForRegister(
1262 uint32_t lldb_regnum
,
1263 lldb_private::UnwindLLDB::ConcreteRegisterLocation
®loc
) {
1264 RegisterNumber
regnum(m_thread
, eRegisterKindLLDB
, lldb_regnum
);
1265 Log
*log
= GetLog(LLDBLog::Unwind
);
1267 // Have we already found this register location?
1268 if (!m_registers
.empty()) {
1270 lldb_private::UnwindLLDB::ConcreteRegisterLocation
>::const_iterator
1272 iterator
= m_registers
.find(regnum
.GetAsKind(eRegisterKindLLDB
));
1273 if (iterator
!= m_registers
.end()) {
1274 regloc
= iterator
->second
;
1275 UnwindLogMsg("supplying caller's saved %s (%d)'s location, cached",
1276 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1277 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1281 // Look through the available UnwindPlans for the register location.
1283 UnwindPlan::Row::AbstractRegisterLocation unwindplan_regloc
;
1284 bool have_unwindplan_regloc
= false;
1285 RegisterKind unwindplan_registerkind
= kNumRegisterKinds
;
1287 if (m_fast_unwind_plan_sp
) {
1288 UnwindPlan::RowSP active_row
=
1289 m_fast_unwind_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
1290 unwindplan_registerkind
= m_fast_unwind_plan_sp
->GetRegisterKind();
1291 if (regnum
.GetAsKind(unwindplan_registerkind
) == LLDB_INVALID_REGNUM
) {
1292 UnwindLogMsg("could not convert lldb regnum %s (%d) into %d RegisterKind "
1293 "reg numbering scheme",
1294 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
),
1295 (int)unwindplan_registerkind
);
1296 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1298 // The architecture default unwind plan marks unknown registers as
1299 // Undefined so that we don't forward them up the stack when a
1300 // jitted stack frame may have overwritten them. But when the
1301 // arch default unwind plan is used as the Fast Unwind Plan, we
1302 // need to recognize this & switch over to the Full Unwind Plan
1303 // to see what unwind rule that (more knoweldgeable, probably)
1304 // UnwindPlan has. If the full UnwindPlan says the register
1305 // location is Undefined, then it really is.
1306 if (active_row
->GetRegisterInfo(regnum
.GetAsKind(unwindplan_registerkind
),
1307 unwindplan_regloc
) &&
1308 !unwindplan_regloc
.IsUndefined()) {
1310 "supplying caller's saved %s (%d)'s location using FastUnwindPlan",
1311 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1312 have_unwindplan_regloc
= true;
1316 if (!have_unwindplan_regloc
) {
1317 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a
1318 // full UnwindPlan yet
1319 bool got_new_full_unwindplan
= false;
1320 if (!m_full_unwind_plan_sp
) {
1321 m_full_unwind_plan_sp
= GetFullUnwindPlanForFrame();
1322 got_new_full_unwindplan
= true;
1325 if (m_full_unwind_plan_sp
) {
1326 RegisterNumber
pc_regnum(m_thread
, eRegisterKindGeneric
,
1327 LLDB_REGNUM_GENERIC_PC
);
1329 UnwindPlan::RowSP active_row
=
1330 m_full_unwind_plan_sp
->GetRowForFunctionOffset(
1331 m_current_offset_backed_up_one
);
1332 unwindplan_registerkind
= m_full_unwind_plan_sp
->GetRegisterKind();
1334 if (got_new_full_unwindplan
&& active_row
.get() && log
) {
1335 StreamString active_row_strm
;
1336 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
1337 active_row
->Dump(active_row_strm
, m_full_unwind_plan_sp
.get(),
1339 m_start_pc
.GetLoadAddress(exe_ctx
.GetTargetPtr()));
1340 UnwindLogMsg("Using full unwind plan '%s'",
1341 m_full_unwind_plan_sp
->GetSourceName().AsCString());
1342 UnwindLogMsg("active row: %s", active_row_strm
.GetData());
1344 RegisterNumber return_address_reg
;
1346 // If we're fetching the saved pc and this UnwindPlan defines a
1347 // ReturnAddress register (e.g. lr on arm), look for the return address
1348 // register number in the UnwindPlan's row.
1349 if (pc_regnum
.IsValid() && pc_regnum
== regnum
&&
1350 m_full_unwind_plan_sp
->GetReturnAddressRegister() !=
1351 LLDB_INVALID_REGNUM
) {
1352 // If this is a trap handler frame, we should have access to
1353 // the complete register context when the interrupt/async
1354 // signal was received, we should fetch the actual saved $pc
1355 // value instead of the Return Address register.
1356 // If $pc is not available, fall back to the RA reg.
1357 UnwindPlan::Row::AbstractRegisterLocation scratch
;
1358 if (m_frame_type
== eTrapHandlerFrame
&&
1359 active_row
->GetRegisterInfo
1360 (pc_regnum
.GetAsKind (unwindplan_registerkind
), scratch
)) {
1361 UnwindLogMsg("Providing pc register instead of rewriting to "
1362 "RA reg because this is a trap handler and there is "
1363 "a location for the saved pc register value.");
1365 return_address_reg
.init(
1366 m_thread
, m_full_unwind_plan_sp
->GetRegisterKind(),
1367 m_full_unwind_plan_sp
->GetReturnAddressRegister());
1368 regnum
= return_address_reg
;
1369 UnwindLogMsg("requested caller's saved PC but this UnwindPlan uses a "
1370 "RA reg; getting %s (%d) instead",
1371 return_address_reg
.GetName(),
1372 return_address_reg
.GetAsKind(eRegisterKindLLDB
));
1375 if (regnum
.GetAsKind(unwindplan_registerkind
) == LLDB_INVALID_REGNUM
) {
1376 if (unwindplan_registerkind
== eRegisterKindGeneric
) {
1377 UnwindLogMsg("could not convert lldb regnum %s (%d) into "
1378 "eRegisterKindGeneric reg numbering scheme",
1379 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1381 UnwindLogMsg("could not convert lldb regnum %s (%d) into %d "
1382 "RegisterKind reg numbering scheme",
1383 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
),
1384 (int)unwindplan_registerkind
);
1386 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1390 if (regnum
.IsValid() &&
1391 active_row
->GetRegisterInfo(regnum
.GetAsKind(unwindplan_registerkind
),
1392 unwindplan_regloc
)) {
1393 have_unwindplan_regloc
= true;
1395 "supplying caller's saved %s (%d)'s location using %s UnwindPlan",
1396 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
),
1397 m_full_unwind_plan_sp
->GetSourceName().GetCString());
1400 // This is frame 0 and we're retrieving the PC and it's saved in a Return
1401 // Address register and it hasn't been saved anywhere yet -- that is,
1402 // it's still live in the actual register. Handle this specially.
1404 if (!have_unwindplan_regloc
&& return_address_reg
.IsValid() &&
1406 if (return_address_reg
.GetAsKind(eRegisterKindLLDB
) !=
1407 LLDB_INVALID_REGNUM
) {
1408 lldb_private::UnwindLLDB::ConcreteRegisterLocation new_regloc
;
1409 new_regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::
1410 eRegisterInLiveRegisterContext
;
1411 new_regloc
.location
.register_number
=
1412 return_address_reg
.GetAsKind(eRegisterKindLLDB
);
1413 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = new_regloc
;
1414 regloc
= new_regloc
;
1415 UnwindLogMsg("supplying caller's register %s (%d) from the live "
1416 "RegisterContext at frame 0, saved in %d",
1417 return_address_reg
.GetName(),
1418 return_address_reg
.GetAsKind(eRegisterKindLLDB
),
1419 return_address_reg
.GetAsKind(eRegisterKindLLDB
));
1420 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1424 // If this architecture stores the return address in a register (it
1425 // defines a Return Address register) and we're on a non-zero stack frame
1426 // and the Full UnwindPlan says that the pc is stored in the
1427 // RA registers (e.g. lr on arm), then we know that the full unwindplan is
1428 // not trustworthy -- this
1429 // is an impossible situation and the instruction emulation code has
1430 // likely been misled. If this stack frame meets those criteria, we need
1431 // to throw away the Full UnwindPlan that the instruction emulation came
1432 // up with and fall back to the architecture's Default UnwindPlan so the
1433 // stack walk can get past this point.
1435 // Special note: If the Full UnwindPlan was generated from the compiler,
1436 // don't second-guess it when we're at a call site location.
1438 // arch_default_ra_regnum is the return address register # in the Full
1439 // UnwindPlan register numbering
1440 RegisterNumber
arch_default_ra_regnum(m_thread
, eRegisterKindGeneric
,
1441 LLDB_REGNUM_GENERIC_RA
);
1443 if (arch_default_ra_regnum
.GetAsKind(unwindplan_registerkind
) !=
1444 LLDB_INVALID_REGNUM
&&
1445 pc_regnum
== regnum
&& unwindplan_regloc
.IsInOtherRegister() &&
1446 unwindplan_regloc
.GetRegisterNumber() ==
1447 arch_default_ra_regnum
.GetAsKind(unwindplan_registerkind
) &&
1448 m_full_unwind_plan_sp
->GetSourcedFromCompiler() != eLazyBoolYes
&&
1449 !m_all_registers_available
) {
1450 UnwindLogMsg("%s UnwindPlan tried to restore the pc from the link "
1451 "register but this is a non-zero frame",
1452 m_full_unwind_plan_sp
->GetSourceName().GetCString());
1454 // Throw away the full unwindplan; install the arch default unwindplan
1455 if (ForceSwitchToFallbackUnwindPlan()) {
1456 // Update for the possibly new unwind plan
1457 unwindplan_registerkind
= m_full_unwind_plan_sp
->GetRegisterKind();
1458 UnwindPlan::RowSP active_row
=
1459 m_full_unwind_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
1461 // Sanity check: Verify that we can fetch a pc value and CFA value
1462 // with this unwind plan
1464 RegisterNumber
arch_default_pc_reg(m_thread
, eRegisterKindGeneric
,
1465 LLDB_REGNUM_GENERIC_PC
);
1466 bool can_fetch_pc_value
= false;
1467 bool can_fetch_cfa
= false;
1470 if (arch_default_pc_reg
.GetAsKind(unwindplan_registerkind
) !=
1471 LLDB_INVALID_REGNUM
&&
1472 active_row
->GetRegisterInfo(
1473 arch_default_pc_reg
.GetAsKind(unwindplan_registerkind
),
1474 unwindplan_regloc
)) {
1475 can_fetch_pc_value
= true;
1477 if (ReadFrameAddress(unwindplan_registerkind
,
1478 active_row
->GetCFAValue(), cfa_value
)) {
1479 can_fetch_cfa
= true;
1483 have_unwindplan_regloc
= can_fetch_pc_value
&& can_fetch_cfa
;
1485 // We were unable to fall back to another unwind plan
1486 have_unwindplan_regloc
= false;
1492 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
1493 Process
*process
= exe_ctx
.GetProcessPtr();
1494 if (!have_unwindplan_regloc
) {
1495 // If the UnwindPlan failed to give us an unwind location for this
1496 // register, we may be able to fall back to some ABI-defined default. For
1497 // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1498 // the ABI may set volatile registers to the undefined state.
1499 ABI
*abi
= process
? process
->GetABI().get() : nullptr;
1501 const RegisterInfo
*reg_info
=
1502 GetRegisterInfoAtIndex(regnum
.GetAsKind(eRegisterKindLLDB
));
1504 abi
->GetFallbackRegisterLocation(reg_info
, unwindplan_regloc
)) {
1506 "supplying caller's saved %s (%d)'s location using ABI default",
1507 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1508 have_unwindplan_regloc
= true;
1513 if (!have_unwindplan_regloc
) {
1514 if (IsFrameZero()) {
1515 // This is frame 0 - we should return the actual live register context
1517 lldb_private::UnwindLLDB::ConcreteRegisterLocation new_regloc
;
1519 UnwindLLDB::ConcreteRegisterLocation::eRegisterInLiveRegisterContext
;
1520 new_regloc
.location
.register_number
= regnum
.GetAsKind(eRegisterKindLLDB
);
1521 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = new_regloc
;
1522 regloc
= new_regloc
;
1523 UnwindLogMsg("supplying caller's register %s (%d) from the live "
1524 "RegisterContext at frame 0",
1525 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1526 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1528 std::string unwindplan_name
;
1529 if (m_full_unwind_plan_sp
) {
1530 unwindplan_name
+= "via '";
1531 unwindplan_name
+= m_full_unwind_plan_sp
->GetSourceName().AsCString();
1532 unwindplan_name
+= "'";
1534 UnwindLogMsg("no save location for %s (%d) %s", regnum
.GetName(),
1535 regnum
.GetAsKind(eRegisterKindLLDB
),
1536 unwindplan_name
.c_str());
1538 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1541 // unwindplan_regloc has valid contents about where to retrieve the register
1542 if (unwindplan_regloc
.IsUnspecified()) {
1543 lldb_private::UnwindLLDB::ConcreteRegisterLocation new_regloc
= {};
1544 new_regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::eRegisterNotSaved
;
1545 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = new_regloc
;
1546 UnwindLogMsg("save location for %s (%d) is unspecified, continue searching",
1547 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1548 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1551 if (unwindplan_regloc
.IsUndefined()) {
1553 "did not supply reg location for %s (%d) because it is volatile",
1554 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1555 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile
;
1558 if (unwindplan_regloc
.IsSame()) {
1559 if (!m_all_registers_available
&&
1560 (regnum
.GetAsKind(eRegisterKindGeneric
) == LLDB_REGNUM_GENERIC_PC
||
1561 regnum
.GetAsKind(eRegisterKindGeneric
) == LLDB_REGNUM_GENERIC_RA
)) {
1562 UnwindLogMsg("register %s (%d) is marked as 'IsSame' - it is a pc or "
1563 "return address reg on a frame which does not have all "
1564 "registers available -- treat as if we have no information",
1565 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1566 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1568 regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::eRegisterInRegister
;
1569 regloc
.location
.register_number
= regnum
.GetAsKind(eRegisterKindLLDB
);
1570 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1572 "supplying caller's register %s (%d), saved in register %s (%d)",
1573 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
),
1574 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1575 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1579 if (unwindplan_regloc
.IsCFAPlusOffset()) {
1580 int offset
= unwindplan_regloc
.GetOffset();
1581 regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::eRegisterValueInferred
;
1582 regloc
.location
.inferred_value
= m_cfa
+ offset
;
1583 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1584 UnwindLogMsg("supplying caller's register %s (%d), value is CFA plus "
1585 "offset %d [value is 0x%" PRIx64
"]",
1586 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
), offset
,
1587 regloc
.location
.inferred_value
);
1588 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1591 if (unwindplan_regloc
.IsAtCFAPlusOffset()) {
1592 int offset
= unwindplan_regloc
.GetOffset();
1594 UnwindLLDB::ConcreteRegisterLocation::eRegisterSavedAtMemoryLocation
;
1595 regloc
.location
.target_memory_location
= m_cfa
+ offset
;
1596 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1597 UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1598 "CFA plus offset %d [saved at 0x%" PRIx64
"]",
1599 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
), offset
,
1600 regloc
.location
.target_memory_location
);
1601 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1604 if (unwindplan_regloc
.IsAFAPlusOffset()) {
1605 if (m_afa
== LLDB_INVALID_ADDRESS
)
1606 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1608 int offset
= unwindplan_regloc
.GetOffset();
1609 regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::eRegisterValueInferred
;
1610 regloc
.location
.inferred_value
= m_afa
+ offset
;
1611 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1612 UnwindLogMsg("supplying caller's register %s (%d), value is AFA plus "
1613 "offset %d [value is 0x%" PRIx64
"]",
1614 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
), offset
,
1615 regloc
.location
.inferred_value
);
1616 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1619 if (unwindplan_regloc
.IsAtAFAPlusOffset()) {
1620 if (m_afa
== LLDB_INVALID_ADDRESS
)
1621 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1623 int offset
= unwindplan_regloc
.GetOffset();
1625 UnwindLLDB::ConcreteRegisterLocation::eRegisterSavedAtMemoryLocation
;
1626 regloc
.location
.target_memory_location
= m_afa
+ offset
;
1627 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1628 UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1629 "AFA plus offset %d [saved at 0x%" PRIx64
"]",
1630 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
), offset
,
1631 regloc
.location
.target_memory_location
);
1632 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1635 if (unwindplan_regloc
.IsInOtherRegister()) {
1636 uint32_t unwindplan_regnum
= unwindplan_regloc
.GetRegisterNumber();
1637 RegisterNumber
row_regnum(m_thread
, unwindplan_registerkind
,
1639 if (row_regnum
.GetAsKind(eRegisterKindLLDB
) == LLDB_INVALID_REGNUM
) {
1640 UnwindLogMsg("could not supply caller's %s (%d) location - was saved in "
1641 "another reg but couldn't convert that regnum",
1642 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1643 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1645 regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::eRegisterInRegister
;
1646 regloc
.location
.register_number
= row_regnum
.GetAsKind(eRegisterKindLLDB
);
1647 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1649 "supplying caller's register %s (%d), saved in register %s (%d)",
1650 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
),
1651 row_regnum
.GetName(), row_regnum
.GetAsKind(eRegisterKindLLDB
));
1652 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1655 if (unwindplan_regloc
.IsDWARFExpression() ||
1656 unwindplan_regloc
.IsAtDWARFExpression()) {
1657 DataExtractor
dwarfdata(unwindplan_regloc
.GetDWARFExpressionBytes(),
1658 unwindplan_regloc
.GetDWARFExpressionLength(),
1659 process
->GetByteOrder(),
1660 process
->GetAddressByteSize());
1661 ModuleSP opcode_ctx
;
1662 DWARFExpressionList
dwarfexpr(opcode_ctx
, dwarfdata
, nullptr);
1663 dwarfexpr
.GetMutableExpressionAtAddress()->SetRegisterKind(
1664 unwindplan_registerkind
);
1665 Value cfa_val
= Scalar(m_cfa
);
1666 cfa_val
.SetValueType(Value::ValueType::LoadAddress
);
1667 llvm::Expected
<Value
> result
=
1668 dwarfexpr
.Evaluate(&exe_ctx
, this, 0, &cfa_val
, nullptr);
1670 LLDB_LOG_ERROR(log
, result
.takeError(),
1671 "DWARF expression failed to evaluate: {0}");
1674 val
= result
->GetScalar().ULongLong();
1675 if (unwindplan_regloc
.IsDWARFExpression()) {
1677 UnwindLLDB::ConcreteRegisterLocation::eRegisterValueInferred
;
1678 regloc
.location
.inferred_value
= val
;
1679 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1680 UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1681 "(IsDWARFExpression)",
1682 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1683 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1685 regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::
1686 eRegisterSavedAtMemoryLocation
;
1687 regloc
.location
.target_memory_location
= val
;
1688 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1689 UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1690 "(IsAtDWARFExpression)",
1691 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1692 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1695 UnwindLogMsg("tried to use IsDWARFExpression or IsAtDWARFExpression for %s "
1697 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1698 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1701 if (unwindplan_regloc
.IsConstant()) {
1702 regloc
.type
= UnwindLLDB::ConcreteRegisterLocation::eRegisterValueInferred
;
1703 regloc
.location
.inferred_value
= unwindplan_regloc
.GetConstant();
1704 m_registers
[regnum
.GetAsKind(eRegisterKindLLDB
)] = regloc
;
1705 UnwindLogMsg("supplying caller's register %s (%d) via constant value",
1706 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1707 return UnwindLLDB::RegisterSearchResult::eRegisterFound
;
1710 UnwindLogMsg("no save location for %s (%d) in this stack frame",
1711 regnum
.GetName(), regnum
.GetAsKind(eRegisterKindLLDB
));
1713 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1716 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound
;
1719 // TryFallbackUnwindPlan() -- this method is a little tricky.
1721 // When this is called, the frame above -- the caller frame, the "previous"
1722 // frame -- is invalid or bad.
1724 // Instead of stopping the stack walk here, we'll try a different UnwindPlan
1725 // and see if we can get a valid frame above us.
1727 // This most often happens when an unwind plan based on assembly instruction
1728 // inspection is not correct -- mostly with hand-written assembly functions or
1729 // functions where the stack frame is set up "out of band", e.g. the kernel
1730 // saved the register context and then called an asynchronous trap handler like
1733 // Often in these cases, if we just do a dumb stack walk we'll get past this
1734 // tricky frame and our usual techniques can continue to be used.
1736 bool RegisterContextUnwind::TryFallbackUnwindPlan() {
1737 if (m_fallback_unwind_plan_sp
.get() == nullptr)
1740 if (m_full_unwind_plan_sp
.get() == nullptr)
1743 if (m_full_unwind_plan_sp
.get() == m_fallback_unwind_plan_sp
.get() ||
1744 m_full_unwind_plan_sp
->GetSourceName() ==
1745 m_fallback_unwind_plan_sp
->GetSourceName()) {
1749 // If a compiler generated unwind plan failed, trying the arch default
1750 // unwindplan isn't going to do any better.
1751 if (m_full_unwind_plan_sp
->GetSourcedFromCompiler() == eLazyBoolYes
)
1754 // Get the caller's pc value and our own CFA value. Swap in the fallback
1755 // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1756 // same, then the fallback unwind plan provides no benefit.
1758 RegisterNumber
pc_regnum(m_thread
, eRegisterKindGeneric
,
1759 LLDB_REGNUM_GENERIC_PC
);
1761 addr_t old_caller_pc_value
= LLDB_INVALID_ADDRESS
;
1762 addr_t new_caller_pc_value
= LLDB_INVALID_ADDRESS
;
1763 UnwindLLDB::ConcreteRegisterLocation regloc
= {};
1764 if (SavedLocationForRegister(pc_regnum
.GetAsKind(eRegisterKindLLDB
),
1766 UnwindLLDB::RegisterSearchResult::eRegisterFound
) {
1767 const RegisterInfo
*reg_info
=
1768 GetRegisterInfoAtIndex(pc_regnum
.GetAsKind(eRegisterKindLLDB
));
1770 RegisterValue reg_value
;
1771 if (ReadRegisterValueFromRegisterLocation(regloc
, reg_info
, reg_value
)) {
1772 old_caller_pc_value
= reg_value
.GetAsUInt64();
1773 if (ProcessSP process_sp
= m_thread
.GetProcess()) {
1774 if (ABISP abi_sp
= process_sp
->GetABI())
1775 old_caller_pc_value
= abi_sp
->FixCodeAddress(old_caller_pc_value
);
1781 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1782 // impossible register location for the full unwind plan, it may call
1783 // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1784 // unwindplan with the fallback... in short, we're done, we're using the
1785 // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1786 // at the top -- the only way it became nullptr since then is via
1787 // SavedLocationForRegister().
1788 if (m_fallback_unwind_plan_sp
.get() == nullptr)
1791 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1792 // this isn't working, we need to restore. We'll also need to save & restore
1793 // the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1794 UnwindPlanSP original_full_unwind_plan_sp
= m_full_unwind_plan_sp
;
1795 addr_t old_cfa
= m_cfa
;
1796 addr_t old_afa
= m_afa
;
1798 m_registers
.clear();
1800 m_full_unwind_plan_sp
= m_fallback_unwind_plan_sp
;
1802 UnwindPlan::RowSP active_row
=
1803 m_fallback_unwind_plan_sp
->GetRowForFunctionOffset(
1804 m_current_offset_backed_up_one
);
1807 active_row
->GetCFAValue().GetValueType() !=
1808 UnwindPlan::Row::FAValue::unspecified
) {
1810 if (!ReadFrameAddress(m_fallback_unwind_plan_sp
->GetRegisterKind(),
1811 active_row
->GetCFAValue(), new_cfa
) ||
1812 new_cfa
== 0 || new_cfa
== 1 || new_cfa
== LLDB_INVALID_ADDRESS
) {
1813 UnwindLogMsg("failed to get cfa with fallback unwindplan");
1814 m_fallback_unwind_plan_sp
.reset();
1815 m_full_unwind_plan_sp
= original_full_unwind_plan_sp
;
1820 ReadFrameAddress(m_fallback_unwind_plan_sp
->GetRegisterKind(),
1821 active_row
->GetAFAValue(), m_afa
);
1823 if (SavedLocationForRegister(pc_regnum
.GetAsKind(eRegisterKindLLDB
),
1825 UnwindLLDB::RegisterSearchResult::eRegisterFound
) {
1826 const RegisterInfo
*reg_info
=
1827 GetRegisterInfoAtIndex(pc_regnum
.GetAsKind(eRegisterKindLLDB
));
1829 RegisterValue reg_value
;
1830 if (ReadRegisterValueFromRegisterLocation(regloc
, reg_info
,
1832 new_caller_pc_value
= reg_value
.GetAsUInt64();
1833 if (ProcessSP process_sp
= m_thread
.GetProcess()) {
1834 if (ABISP abi_sp
= process_sp
->GetABI())
1835 new_caller_pc_value
= abi_sp
->FixCodeAddress(new_caller_pc_value
);
1841 if (new_caller_pc_value
== LLDB_INVALID_ADDRESS
) {
1842 UnwindLogMsg("failed to get a pc value for the caller frame with the "
1843 "fallback unwind plan");
1844 m_fallback_unwind_plan_sp
.reset();
1845 m_full_unwind_plan_sp
= original_full_unwind_plan_sp
;
1851 if (old_caller_pc_value
== new_caller_pc_value
&&
1854 UnwindLogMsg("fallback unwind plan got the same values for this frame "
1855 "CFA and caller frame pc, not using");
1856 m_fallback_unwind_plan_sp
.reset();
1857 m_full_unwind_plan_sp
= original_full_unwind_plan_sp
;
1861 UnwindLogMsg("trying to unwind from this function with the UnwindPlan '%s' "
1862 "because UnwindPlan '%s' failed.",
1863 m_fallback_unwind_plan_sp
->GetSourceName().GetCString(),
1864 original_full_unwind_plan_sp
->GetSourceName().GetCString());
1866 // We've copied the fallback unwind plan into the full - now clear the
1868 m_fallback_unwind_plan_sp
.reset();
1869 PropagateTrapHandlerFlagFromUnwindPlan(m_full_unwind_plan_sp
);
1875 bool RegisterContextUnwind::ForceSwitchToFallbackUnwindPlan() {
1876 if (m_fallback_unwind_plan_sp
.get() == nullptr)
1879 if (m_full_unwind_plan_sp
.get() == nullptr)
1882 if (m_full_unwind_plan_sp
.get() == m_fallback_unwind_plan_sp
.get() ||
1883 m_full_unwind_plan_sp
->GetSourceName() ==
1884 m_fallback_unwind_plan_sp
->GetSourceName()) {
1888 UnwindPlan::RowSP active_row
=
1889 m_fallback_unwind_plan_sp
->GetRowForFunctionOffset(m_current_offset
);
1892 active_row
->GetCFAValue().GetValueType() !=
1893 UnwindPlan::Row::FAValue::unspecified
) {
1895 if (!ReadFrameAddress(m_fallback_unwind_plan_sp
->GetRegisterKind(),
1896 active_row
->GetCFAValue(), new_cfa
) ||
1897 new_cfa
== 0 || new_cfa
== 1 || new_cfa
== LLDB_INVALID_ADDRESS
) {
1898 UnwindLogMsg("failed to get cfa with fallback unwindplan");
1899 m_fallback_unwind_plan_sp
.reset();
1903 ReadFrameAddress(m_fallback_unwind_plan_sp
->GetRegisterKind(),
1904 active_row
->GetAFAValue(), m_afa
);
1906 m_full_unwind_plan_sp
= m_fallback_unwind_plan_sp
;
1907 m_fallback_unwind_plan_sp
.reset();
1909 m_registers
.clear();
1913 PropagateTrapHandlerFlagFromUnwindPlan(m_full_unwind_plan_sp
);
1915 UnwindLogMsg("switched unconditionally to the fallback unwindplan %s",
1916 m_full_unwind_plan_sp
->GetSourceName().GetCString());
1922 void RegisterContextUnwind::PropagateTrapHandlerFlagFromUnwindPlan(
1923 lldb::UnwindPlanSP unwind_plan
) {
1924 if (unwind_plan
->GetUnwindPlanForSignalTrap() != eLazyBoolYes
) {
1925 // Unwind plan does not indicate trap handler. Do nothing. We may
1926 // already be flagged as trap handler flag due to the symbol being
1927 // in the trap handler symbol list, and that should take precedence.
1929 } else if (m_frame_type
!= eNormalFrame
) {
1930 // If this is already a trap handler frame, nothing to do.
1931 // If this is a skip or debug or invalid frame, don't override that.
1935 m_frame_type
= eTrapHandlerFrame
;
1937 if (m_current_offset_backed_up_one
!= m_current_offset
) {
1938 // We backed up the pc by 1 to compute the symbol context, but
1939 // now need to undo that because the pc of the trap handler
1940 // frame may in fact be the first instruction of a signal return
1941 // trampoline, rather than the instruction after a call. This
1942 // happens on systems where the signal handler dispatch code, rather
1943 // than calling the handler and being returned to, jumps to the
1944 // handler after pushing the address of a return trampoline on the
1945 // stack -- on these systems, when the handler returns, control will
1946 // be transferred to the return trampoline, so that's the best
1947 // symbol we can present in the callstack.
1948 UnwindLogMsg("Resetting current offset and re-doing symbol lookup; "
1949 "old symbol was %s",
1950 GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
1951 m_current_offset_backed_up_one
= m_current_offset
;
1953 AddressRange addr_range
;
1954 m_sym_ctx_valid
= m_current_pc
.ResolveFunctionScope(m_sym_ctx
, &addr_range
);
1956 UnwindLogMsg("Symbol is now %s",
1957 GetSymbolOrFunctionName(m_sym_ctx
).AsCString(""));
1959 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
1960 Process
*process
= exe_ctx
.GetProcessPtr();
1961 Target
*target
= &process
->GetTarget();
1963 m_start_pc
= addr_range
.GetBaseAddress();
1965 m_current_pc
.GetLoadAddress(target
) - m_start_pc
.GetLoadAddress(target
);
1969 bool RegisterContextUnwind::ReadFrameAddress(
1970 lldb::RegisterKind row_register_kind
, UnwindPlan::Row::FAValue
&fa
,
1972 RegisterValue reg_value
;
1974 address
= LLDB_INVALID_ADDRESS
;
1975 addr_t cfa_reg_contents
;
1976 ABISP abi_sp
= m_thread
.GetProcess()->GetABI();
1978 switch (fa
.GetValueType()) {
1979 case UnwindPlan::Row::FAValue::isRegisterDereferenced
: {
1980 RegisterNumber
cfa_reg(m_thread
, row_register_kind
,
1981 fa
.GetRegisterNumber());
1982 if (ReadGPRValue(cfa_reg
, cfa_reg_contents
)) {
1983 const RegisterInfo
*reg_info
=
1984 GetRegisterInfoAtIndex(cfa_reg
.GetAsKind(eRegisterKindLLDB
));
1985 RegisterValue reg_value
;
1988 cfa_reg_contents
= abi_sp
->FixDataAddress(cfa_reg_contents
);
1989 Status error
= ReadRegisterValueFromMemory(
1990 reg_info
, cfa_reg_contents
, reg_info
->byte_size
, reg_value
);
1991 if (error
.Success()) {
1992 address
= reg_value
.GetAsUInt64();
1994 address
= abi_sp
->FixCodeAddress(address
);
1996 "CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64
1997 ", CFA value is 0x%" PRIx64
,
1998 cfa_reg
.GetName(), cfa_reg
.GetAsKind(eRegisterKindLLDB
),
1999 cfa_reg_contents
, address
);
2002 UnwindLogMsg("Tried to deref reg %s (%d) [0x%" PRIx64
2003 "] but memory read failed.",
2004 cfa_reg
.GetName(), cfa_reg
.GetAsKind(eRegisterKindLLDB
),
2011 case UnwindPlan::Row::FAValue::isRegisterPlusOffset
: {
2012 RegisterNumber
cfa_reg(m_thread
, row_register_kind
,
2013 fa
.GetRegisterNumber());
2014 if (ReadGPRValue(cfa_reg
, cfa_reg_contents
)) {
2016 cfa_reg_contents
= abi_sp
->FixDataAddress(cfa_reg_contents
);
2017 if (cfa_reg_contents
== LLDB_INVALID_ADDRESS
|| cfa_reg_contents
== 0 ||
2018 cfa_reg_contents
== 1) {
2020 "Got an invalid CFA register value - reg %s (%d), value 0x%" PRIx64
,
2021 cfa_reg
.GetName(), cfa_reg
.GetAsKind(eRegisterKindLLDB
),
2023 cfa_reg_contents
= LLDB_INVALID_ADDRESS
;
2026 address
= cfa_reg_contents
+ fa
.GetOffset();
2028 "CFA is 0x%" PRIx64
": Register %s (%d) contents are 0x%" PRIx64
2030 address
, cfa_reg
.GetName(), cfa_reg
.GetAsKind(eRegisterKindLLDB
),
2031 cfa_reg_contents
, fa
.GetOffset());
2036 case UnwindPlan::Row::FAValue::isDWARFExpression
: {
2037 ExecutionContext
exe_ctx(m_thread
.shared_from_this());
2038 Process
*process
= exe_ctx
.GetProcessPtr();
2039 DataExtractor
dwarfdata(fa
.GetDWARFExpressionBytes(),
2040 fa
.GetDWARFExpressionLength(),
2041 process
->GetByteOrder(),
2042 process
->GetAddressByteSize());
2043 ModuleSP opcode_ctx
;
2044 DWARFExpressionList
dwarfexpr(opcode_ctx
, dwarfdata
, nullptr);
2045 dwarfexpr
.GetMutableExpressionAtAddress()->SetRegisterKind(
2047 llvm::Expected
<Value
> result
=
2048 dwarfexpr
.Evaluate(&exe_ctx
, this, 0, nullptr, nullptr);
2050 address
= result
->GetScalar().ULongLong();
2051 if (ABISP abi_sp
= m_thread
.GetProcess()->GetABI())
2052 address
= abi_sp
->FixCodeAddress(address
);
2054 UnwindLogMsg("CFA value set by DWARF expression is 0x%" PRIx64
,
2058 UnwindLogMsg("Failed to set CFA value via DWARF expression: %s",
2059 llvm::toString(result
.takeError()).c_str());
2062 case UnwindPlan::Row::FAValue::isRaSearch
: {
2063 Process
&process
= *m_thread
.GetProcess();
2064 lldb::addr_t return_address_hint
= GetReturnAddressHint(fa
.GetOffset());
2065 if (return_address_hint
== LLDB_INVALID_ADDRESS
)
2067 const unsigned max_iterations
= 256;
2068 for (unsigned i
= 0; i
< max_iterations
; ++i
) {
2070 lldb::addr_t candidate_addr
=
2071 return_address_hint
+ i
* process
.GetAddressByteSize();
2072 lldb::addr_t candidate
=
2073 process
.ReadPointerFromMemory(candidate_addr
, st
);
2075 UnwindLogMsg("Cannot read memory at 0x%" PRIx64
": %s", candidate_addr
,
2080 uint32_t permissions
;
2081 if (process
.GetLoadAddressPermissions(candidate
, permissions
) &&
2082 permissions
& lldb::ePermissionsExecutable
) {
2083 address
= candidate_addr
;
2084 UnwindLogMsg("Heuristically found CFA: 0x%" PRIx64
, address
);
2088 UnwindLogMsg("No suitable CFA found");
2091 case UnwindPlan::Row::FAValue::isConstant
: {
2092 address
= fa
.GetConstant();
2093 address
= m_thread
.GetProcess()->FixDataAddress(address
);
2094 UnwindLogMsg("CFA value set by constant is 0x%" PRIx64
, address
);
2103 lldb::addr_t
RegisterContextUnwind::GetReturnAddressHint(int32_t plan_offset
) {
2105 if (!ReadGPRValue(eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_SP
, hint
))
2106 return LLDB_INVALID_ADDRESS
;
2107 if (!m_sym_ctx
.module_sp
|| !m_sym_ctx
.symbol
)
2108 return LLDB_INVALID_ADDRESS
;
2109 if (ABISP abi_sp
= m_thread
.GetProcess()->GetABI())
2110 hint
= abi_sp
->FixCodeAddress(hint
);
2112 hint
+= plan_offset
;
2114 if (auto next
= GetNextFrame()) {
2115 if (!next
->m_sym_ctx
.module_sp
|| !next
->m_sym_ctx
.symbol
)
2116 return LLDB_INVALID_ADDRESS
;
2117 if (auto expected_size
=
2118 next
->m_sym_ctx
.module_sp
->GetSymbolFile()->GetParameterStackSize(
2119 *next
->m_sym_ctx
.symbol
))
2120 hint
+= *expected_size
;
2122 UnwindLogMsgVerbose("Could not retrieve parameter size: %s",
2123 llvm::toString(expected_size
.takeError()).c_str());
2124 return LLDB_INVALID_ADDRESS
;
2130 // Retrieve a general purpose register value for THIS frame, as saved by the
2131 // NEXT frame, i.e. the frame that
2132 // this frame called. e.g.
2135 // bar () { foo (); }
2136 // main () { bar (); }
2138 // stopped in foo() so
2142 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc
2143 // value for frame 1, we need to ask
2144 // where frame 0 (the "next" frame) saved that and retrieve the value.
2146 bool RegisterContextUnwind::ReadGPRValue(lldb::RegisterKind register_kind
,
2147 uint32_t regnum
, addr_t
&value
) {
2151 uint32_t lldb_regnum
;
2152 if (register_kind
== eRegisterKindLLDB
) {
2153 lldb_regnum
= regnum
;
2154 } else if (!m_thread
.GetRegisterContext()->ConvertBetweenRegisterKinds(
2155 register_kind
, regnum
, eRegisterKindLLDB
, lldb_regnum
)) {
2159 const RegisterInfo
*reg_info
= GetRegisterInfoAtIndex(lldb_regnum
);
2163 "Could not find RegisterInfo definition for lldb register number %d",
2168 uint32_t generic_regnum
= LLDB_INVALID_REGNUM
;
2169 if (register_kind
== eRegisterKindGeneric
)
2170 generic_regnum
= regnum
;
2172 m_thread
.GetRegisterContext()->ConvertBetweenRegisterKinds(
2173 register_kind
, regnum
, eRegisterKindGeneric
, generic_regnum
);
2174 ABISP abi_sp
= m_thread
.GetProcess()->GetABI();
2176 RegisterValue reg_value
;
2177 // if this is frame 0 (currently executing frame), get the requested reg
2178 // contents from the actual thread registers
2179 if (IsFrameZero()) {
2180 if (m_thread
.GetRegisterContext()->ReadRegister(reg_info
, reg_value
)) {
2181 value
= reg_value
.GetAsUInt64();
2182 if (abi_sp
&& generic_regnum
!= LLDB_INVALID_REGNUM
) {
2183 if (generic_regnum
== LLDB_REGNUM_GENERIC_PC
||
2184 generic_regnum
== LLDB_REGNUM_GENERIC_RA
)
2185 value
= abi_sp
->FixCodeAddress(value
);
2186 if (generic_regnum
== LLDB_REGNUM_GENERIC_SP
||
2187 generic_regnum
== LLDB_REGNUM_GENERIC_FP
)
2188 value
= abi_sp
->FixDataAddress(value
);
2195 bool pc_register
= false;
2196 if (generic_regnum
!= LLDB_INVALID_REGNUM
&&
2197 (generic_regnum
== LLDB_REGNUM_GENERIC_PC
||
2198 generic_regnum
== LLDB_REGNUM_GENERIC_RA
))
2201 lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc
;
2202 if (!m_parent_unwind
.SearchForSavedLocationForRegister(
2203 lldb_regnum
, regloc
, m_frame_number
- 1, pc_register
)) {
2206 if (ReadRegisterValueFromRegisterLocation(regloc
, reg_info
, reg_value
)) {
2207 value
= reg_value
.GetAsUInt64();
2209 if (ABISP abi_sp
= m_thread
.GetProcess()->GetABI()) {
2210 value
= abi_sp
->FixCodeAddress(value
);
2218 bool RegisterContextUnwind::ReadGPRValue(const RegisterNumber
®num
,
2220 return ReadGPRValue(regnum
.GetRegisterKind(), regnum
.GetRegisterNumber(),
2224 // Find the value of a register in THIS frame
2226 bool RegisterContextUnwind::ReadRegister(const RegisterInfo
*reg_info
,
2227 RegisterValue
&value
) {
2231 const uint32_t lldb_regnum
= reg_info
->kinds
[eRegisterKindLLDB
];
2232 UnwindLogMsgVerbose("looking for register saved location for reg %d",
2235 // If this is the 0th frame, hand this over to the live register context
2236 if (IsFrameZero()) {
2237 UnwindLogMsgVerbose("passing along to the live register context for reg %d",
2239 return m_thread
.GetRegisterContext()->ReadRegister(reg_info
, value
);
2242 bool is_pc_regnum
= false;
2243 if (reg_info
->kinds
[eRegisterKindGeneric
] == LLDB_REGNUM_GENERIC_PC
||
2244 reg_info
->kinds
[eRegisterKindGeneric
] == LLDB_REGNUM_GENERIC_RA
) {
2245 is_pc_regnum
= true;
2248 lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc
;
2249 // Find out where the NEXT frame saved THIS frame's register contents
2250 if (!m_parent_unwind
.SearchForSavedLocationForRegister(
2251 lldb_regnum
, regloc
, m_frame_number
- 1, is_pc_regnum
))
2254 bool result
= ReadRegisterValueFromRegisterLocation(regloc
, reg_info
, value
);
2256 if (is_pc_regnum
&& value
.GetType() == RegisterValue::eTypeUInt64
) {
2257 addr_t reg_value
= value
.GetAsUInt64(LLDB_INVALID_ADDRESS
);
2258 if (reg_value
!= LLDB_INVALID_ADDRESS
) {
2259 if (ABISP abi_sp
= m_thread
.GetProcess()->GetABI())
2260 value
= abi_sp
->FixCodeAddress(reg_value
);
2267 bool RegisterContextUnwind::WriteRegister(const RegisterInfo
*reg_info
,
2268 const RegisterValue
&value
) {
2272 const uint32_t lldb_regnum
= reg_info
->kinds
[eRegisterKindLLDB
];
2273 UnwindLogMsgVerbose("looking for register saved location for reg %d",
2276 // If this is the 0th frame, hand this over to the live register context
2277 if (IsFrameZero()) {
2278 UnwindLogMsgVerbose("passing along to the live register context for reg %d",
2280 return m_thread
.GetRegisterContext()->WriteRegister(reg_info
, value
);
2283 lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc
;
2284 // Find out where the NEXT frame saved THIS frame's register contents
2285 if (!m_parent_unwind
.SearchForSavedLocationForRegister(
2286 lldb_regnum
, regloc
, m_frame_number
- 1, false))
2289 return WriteRegisterValueToRegisterLocation(regloc
, reg_info
, value
);
2292 // Don't need to implement this one
2293 bool RegisterContextUnwind::ReadAllRegisterValues(
2294 lldb::WritableDataBufferSP
&data_sp
) {
2298 // Don't need to implement this one
2299 bool RegisterContextUnwind::WriteAllRegisterValues(
2300 const lldb::DataBufferSP
&data_sp
) {
2304 // Retrieve the pc value for THIS from
2306 bool RegisterContextUnwind::GetCFA(addr_t
&cfa
) {
2310 if (m_cfa
== LLDB_INVALID_ADDRESS
) {
2317 RegisterContextUnwind::SharedPtr
RegisterContextUnwind::GetNextFrame() const {
2318 RegisterContextUnwind::SharedPtr regctx
;
2319 if (m_frame_number
== 0)
2321 return m_parent_unwind
.GetRegisterContextForFrameNum(m_frame_number
- 1);
2324 RegisterContextUnwind::SharedPtr
RegisterContextUnwind::GetPrevFrame() const {
2325 RegisterContextUnwind::SharedPtr regctx
;
2326 return m_parent_unwind
.GetRegisterContextForFrameNum(m_frame_number
+ 1);
2329 // Retrieve the address of the start of the function of THIS frame
2331 bool RegisterContextUnwind::GetStartPC(addr_t
&start_pc
) {
2335 if (!m_start_pc
.IsValid()) {
2336 bool read_successfully
= ReadPC (start_pc
);
2337 if (read_successfully
)
2339 ProcessSP
process_sp (m_thread
.GetProcess());
2342 if (ABISP abi_sp
= process_sp
->GetABI())
2343 start_pc
= abi_sp
->FixCodeAddress(start_pc
);
2346 return read_successfully
;
2348 start_pc
= m_start_pc
.GetLoadAddress(CalculateTarget().get());
2352 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2354 bool RegisterContextUnwind::ReadPC(addr_t
&pc
) {
2358 bool above_trap_handler
= false;
2359 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2360 GetNextFrame()->IsTrapHandlerFrame())
2361 above_trap_handler
= true;
2363 if (ReadGPRValue(eRegisterKindGeneric
, LLDB_REGNUM_GENERIC_PC
, pc
)) {
2364 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2365 // indicates the end of a stack walk.
2366 // On the currently executing frame (or such a frame interrupted
2367 // asynchronously by sigtramp et al) this may occur if code has jumped
2368 // through a NULL pointer -- we want to be able to unwind past that frame
2369 // to help find the bug.
2371 if (ABISP abi_sp
= m_thread
.GetProcess()->GetABI())
2372 pc
= abi_sp
->FixCodeAddress(pc
);
2374 return !(m_all_registers_available
== false &&
2375 above_trap_handler
== false && (pc
== 0 || pc
== 1));
2381 void RegisterContextUnwind::UnwindLogMsg(const char *fmt
, ...) {
2382 Log
*log
= GetLog(LLDBLog::Unwind
);
2387 va_start(args
, fmt
);
2389 llvm::SmallString
<0> logmsg
;
2390 if (VASprintf(logmsg
, fmt
, args
)) {
2391 LLDB_LOGF(log
, "%*sth%d/fr%u %s",
2392 m_frame_number
< 100 ? m_frame_number
: 100, "",
2393 m_thread
.GetIndexID(), m_frame_number
, logmsg
.c_str());
2398 void RegisterContextUnwind::UnwindLogMsgVerbose(const char *fmt
, ...) {
2399 Log
*log
= GetLog(LLDBLog::Unwind
);
2400 if (!log
|| !log
->GetVerbose())
2404 va_start(args
, fmt
);
2406 llvm::SmallString
<0> logmsg
;
2407 if (VASprintf(logmsg
, fmt
, args
)) {
2408 LLDB_LOGF(log
, "%*sth%d/fr%u %s",
2409 m_frame_number
< 100 ? m_frame_number
: 100, "",
2410 m_thread
.GetIndexID(), m_frame_number
, logmsg
.c_str());