1 //===-- RegisterContextLLDB.cpp --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "lldb/lldb-private.h"
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/AddressRange.h"
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/RegisterValue.h"
18 #include "lldb/Core/Value.h"
19 #include "lldb/Expression/DWARFExpression.h"
20 #include "lldb/Symbol/ArmUnwindInfo.h"
21 #include "lldb/Symbol/DWARFCallFrameInfo.h"
22 #include "lldb/Symbol/FuncUnwinders.h"
23 #include "lldb/Symbol/Function.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/Symbol.h"
26 #include "lldb/Symbol/SymbolContext.h"
27 #include "lldb/Target/ABI.h"
28 #include "lldb/Target/DynamicLoader.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Platform.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/SectionLoadList.h"
33 #include "lldb/Target/StackFrame.h"
34 #include "lldb/Target/Target.h"
35 #include "lldb/Target/Thread.h"
37 #include "RegisterContextLLDB.h"
40 using namespace lldb_private;
42 static ConstString GetSymbolOrFunctionName(const SymbolContext &sym_ctx)
45 return sym_ctx.symbol->GetName();
46 else if (sym_ctx.function)
47 return sym_ctx.function->GetName();
51 RegisterContextLLDB::RegisterContextLLDB
54 const SharedPtr &next_frame,
55 SymbolContext& sym_ctx,
56 uint32_t frame_number,
57 UnwindLLDB& unwind_lldb
59 RegisterContext (thread, frame_number),
61 m_fast_unwind_plan_sp (),
62 m_full_unwind_plan_sp (),
63 m_fallback_unwind_plan_sp (),
64 m_all_registers_available(false),
66 m_cfa (LLDB_INVALID_ADDRESS),
70 m_current_offset_backed_up_one (0),
72 m_sym_ctx_valid (false),
73 m_frame_number (frame_number),
75 m_parent_unwind (unwind_lldb)
77 m_sym_ctx.Clear(false);
78 m_sym_ctx_valid = false;
82 InitializeZerothFrame ();
86 InitializeNonZerothFrame ();
89 // This same code exists over in the GetFullUnwindPlanForFrame() but it may not have been executed yet
91 || next_frame->m_frame_type == eTrapHandlerFrame
92 || next_frame->m_frame_type == eDebuggerFrame)
94 m_all_registers_available = true;
99 RegisterContextLLDB::IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset)
104 // check if m_current_pc is valid
105 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc))
107 // yes - current offset can be used as is
108 valid_pc_offset = m_current_offset;
112 // if m_current_offset <= 0, we've got nothing else to try
113 if (m_current_offset <= 0)
116 // check pc - 1 to see if it's valid
117 Address pc_minus_one (m_current_pc);
118 pc_minus_one.SetOffset(m_current_pc.GetOffset() - 1);
119 if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one))
121 // *valid_pc_offset = m_current_offset - 1;
122 valid_pc_offset = m_current_pc.GetOffset() - 1;
129 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the zeroth frame or currently
133 RegisterContextLLDB::InitializeZerothFrame()
135 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
136 ExecutionContext exe_ctx(m_thread.shared_from_this());
137 RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
139 if (reg_ctx_sp.get() == NULL)
141 m_frame_type = eNotAValidFrame;
142 UnwindLogMsg ("frame does not have a register context");
146 addr_t current_pc = reg_ctx_sp->GetPC();
148 if (current_pc == LLDB_INVALID_ADDRESS)
150 m_frame_type = eNotAValidFrame;
151 UnwindLogMsg ("frame does not have a pc");
155 Process *process = exe_ctx.GetProcessPtr();
157 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
158 // this will strip bit zero in case we read a PC from memory or from the LR.
159 // (which would be a no-op in frame 0 where we get it from the register set,
160 // but still a good idea to make the call here for other ABIs that may exist.)
161 ABI *abi = process->GetABI().get();
163 current_pc = abi->FixCodeAddress(current_pc);
165 // Initialize m_current_pc, an Address object, based on current_pc, an addr_t.
166 m_current_pc.SetLoadAddress (current_pc, &process->GetTarget());
168 // If we don't have a Module for some reason, we're not going to find symbol/function information - just
169 // stick in some reasonable defaults and hope we can unwind past this frame.
170 ModuleSP pc_module_sp (m_current_pc.GetModule());
171 if (!m_current_pc.IsValid() || !pc_module_sp)
173 UnwindLogMsg ("using architectural default unwind method");
176 // We require either a symbol or function in the symbols context to be successfully
177 // filled in or this context is of no use to us.
178 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
179 if (pc_module_sp.get()
180 && (pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, resolve_scope, m_sym_ctx) & resolve_scope))
182 m_sym_ctx_valid = true;
185 if (m_sym_ctx.symbol)
187 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", symbol name is '%s'",
188 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
190 else if (m_sym_ctx.function)
192 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", function name is '%s'",
193 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
197 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", no symbol/function name is known.", current_pc);
200 AddressRange addr_range;
201 m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range);
203 if (IsTrapHandlerSymbol (process, m_sym_ctx))
205 m_frame_type = eTrapHandlerFrame;
209 // FIXME: Detect eDebuggerFrame here.
210 m_frame_type = eNormalFrame;
213 // If we were able to find a symbol/function, set addr_range to the bounds of that symbol/function.
214 // else treat the current pc value as the start_pc and record no offset.
215 if (addr_range.GetBaseAddress().IsValid())
217 m_start_pc = addr_range.GetBaseAddress();
218 if (m_current_pc.GetSection() == m_start_pc.GetSection())
220 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
222 else if (m_current_pc.GetModule() == m_start_pc.GetModule())
224 // This means that whatever symbol we kicked up isn't really correct
225 // --- we should not cross section boundaries ... We really should NULL out
226 // the function/symbol in this case unless there is a bad assumption
227 // here due to inlined functions?
228 m_current_offset = m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
230 m_current_offset_backed_up_one = m_current_offset;
234 m_start_pc = m_current_pc;
235 m_current_offset = -1;
236 m_current_offset_backed_up_one = -1;
239 // We've set m_frame_type and m_sym_ctx before these calls.
241 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame ();
242 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
244 UnwindPlan::RowSP active_row;
245 lldb::RegisterKind row_register_kind = eRegisterKindGeneric;
246 if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
248 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
249 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
250 if (active_row.get() && log)
252 StreamString active_row_strm;
253 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
254 UnwindLogMsg ("%s", active_row_strm.GetString().c_str());
258 if (!active_row.get())
260 UnwindLogMsg ("could not find an unwindplan row for this frame's pc");
261 m_frame_type = eNotAValidFrame;
266 if (!ReadCFAValueForRow (row_register_kind, active_row, m_cfa))
268 // Try the fall back unwind plan since the
269 // full unwind plan failed.
270 FuncUnwindersSP func_unwinders_sp;
271 UnwindPlanSP call_site_unwind_plan;
272 bool cfa_status = false;
276 func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx);
279 if(func_unwinders_sp.get() != nullptr)
280 call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(), m_current_offset_backed_up_one);
282 if (call_site_unwind_plan.get() != nullptr)
284 m_fallback_unwind_plan_sp = call_site_unwind_plan;
285 if(TryFallbackUnwindPlan())
290 UnwindLogMsg ("could not read CFA value for first frame.");
291 m_frame_type = eNotAValidFrame;
296 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64 " using %s UnwindPlan",
297 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()),
299 m_full_unwind_plan_sp->GetSourceName().GetCString());
302 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the RegisterContextLLDB "below" it
303 // to provide things like its current pc value.
306 RegisterContextLLDB::InitializeNonZerothFrame()
308 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
311 m_frame_type = eNotAValidFrame;
312 UnwindLogMsg ("non-zeroth frame tests positive for IsFrameZero -- that shouldn't happen.");
316 if (!GetNextFrame().get() || !GetNextFrame()->IsValid())
318 m_frame_type = eNotAValidFrame;
319 UnwindLogMsg ("Could not get next frame, marking this frame as invalid.");
322 if (!m_thread.GetRegisterContext())
324 m_frame_type = eNotAValidFrame;
325 UnwindLogMsg ("Could not get register context for this thread, marking this frame as invalid.");
330 if (!ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc))
332 UnwindLogMsg ("could not get pc value");
333 m_frame_type = eNotAValidFrame;
339 UnwindLogMsg ("pc = 0x%" PRIx64, pc);
341 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val))
342 UnwindLogMsg ("fp = 0x%" PRIx64, reg_val);
343 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val))
344 UnwindLogMsg ("sp = 0x%" PRIx64, reg_val);
347 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap handler function
348 bool above_trap_handler = false;
349 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsTrapHandlerFrame())
350 above_trap_handler = true;
352 if (pc == 0 || pc == 0x1)
354 if (above_trap_handler == false)
356 m_frame_type = eNotAValidFrame;
357 UnwindLogMsg ("this frame has a pc of 0x0");
362 ExecutionContext exe_ctx(m_thread.shared_from_this());
363 Process *process = exe_ctx.GetProcessPtr();
364 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
365 // this will strip bit zero in case we read a PC from memory or from the LR.
366 ABI *abi = process->GetABI().get();
368 pc = abi->FixCodeAddress(pc);
370 m_current_pc.SetLoadAddress (pc, &process->GetTarget());
372 // If we don't have a Module for some reason, we're not going to find symbol/function information - just
373 // stick in some reasonable defaults and hope we can unwind past this frame.
374 ModuleSP pc_module_sp (m_current_pc.GetModule());
375 if (!m_current_pc.IsValid() || !pc_module_sp)
377 UnwindLogMsg ("using architectural default unwind method");
379 // Test the pc value to see if we know it's in an unmapped/non-executable region of memory.
380 uint32_t permissions;
381 if (process->GetLoadAddressPermissions(pc, permissions)
382 && (permissions & ePermissionsExecutable) == 0)
384 // If this is the second frame off the stack, we may have unwound the first frame
385 // incorrectly. But using the architecture default unwind plan may get us back on
386 // track -- albeit possibly skipping a real frame. Give this frame a clearly-invalid
387 // pc and see if we can get any further.
388 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsFrameZero())
390 UnwindLogMsg ("had a pc of 0x%" PRIx64 " which is not in executable memory but on frame 1 -- allowing it once.",
392 m_frame_type = eSkipFrame;
396 // anywhere other than the second frame, a non-executable pc means we're off in the weeds -- stop now.
397 m_frame_type = eNotAValidFrame;
398 UnwindLogMsg ("pc is in a non-executable section of memory and this isn't the 2nd frame in the stack walk.");
405 m_fast_unwind_plan_sp.reset ();
406 m_full_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
407 abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp);
408 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
410 m_frame_type = eNormalFrame;
412 m_all_registers_available = false;
413 m_current_offset = -1;
414 m_current_offset_backed_up_one = -1;
415 RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
416 UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0);
419 if (!ReadCFAValueForRow (row_register_kind, row, m_cfa))
421 UnwindLogMsg ("failed to get cfa value");
422 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
424 m_frame_type = eNotAValidFrame;
429 // A couple of sanity checks..
430 if (m_cfa == LLDB_INVALID_ADDRESS || m_cfa == 0 || m_cfa == 1)
432 UnwindLogMsg ("could not find a valid cfa address");
433 m_frame_type = eNotAValidFrame;
437 // m_cfa should point into the stack memory; if we can query memory region permissions,
438 // see if the memory is allocated & readable.
439 if (process->GetLoadAddressPermissions(m_cfa, permissions)
440 && (permissions & ePermissionsReadable) == 0)
442 m_frame_type = eNotAValidFrame;
443 UnwindLogMsg ("the CFA points to a region of memory that is not readable");
449 UnwindLogMsg ("could not find a row for function offset zero");
450 m_frame_type = eNotAValidFrame;
454 if (CheckIfLoopingStack ())
456 TryFallbackUnwindPlan();
457 if (CheckIfLoopingStack ())
459 UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping");
460 m_frame_type = eNotAValidFrame;
465 UnwindLogMsg ("initialized frame cfa is 0x%" PRIx64, (uint64_t) m_cfa);
468 m_frame_type = eNotAValidFrame;
469 UnwindLogMsg ("could not find any symbol for this pc, or a default unwind plan, to continue unwind.");
473 bool resolve_tail_call_address = false; // m_current_pc can be one past the address range of the function...
474 // If the saved pc does not point to a function/symbol because it is
475 // beyond the bounds of the correct function and there's no symbol there,
476 // we do *not* want ResolveSymbolContextForAddress to back up the pc by 1,
477 // because then we might not find the correct unwind information later.
478 // Instead, let ResolveSymbolContextForAddress fail, and handle the case
479 // via decr_pc_and_recompute_addr_range below.
480 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
481 uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress (m_current_pc,
483 m_sym_ctx, resolve_tail_call_address);
485 // We require either a symbol or function in the symbols context to be successfully
486 // filled in or this context is of no use to us.
487 if (resolve_scope & resolved_scope)
489 m_sym_ctx_valid = true;
492 if (m_sym_ctx.symbol)
494 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", symbol name is '%s'",
495 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
497 else if (m_sym_ctx.function)
499 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", function name is '%s'",
500 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
504 UnwindLogMsg ("with pc value of 0x%" PRIx64 ", no symbol/function name is known.", pc);
507 AddressRange addr_range;
508 if (!m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range))
510 m_sym_ctx_valid = false;
513 bool decr_pc_and_recompute_addr_range = false;
515 // If the symbol lookup failed...
516 if (m_sym_ctx_valid == false)
517 decr_pc_and_recompute_addr_range = true;
519 // Or if we're in the middle of the stack (and not "above" an asynchronous event like sigtramp),
520 // and our "current" pc is the start of a function...
522 && GetNextFrame()->m_frame_type != eTrapHandlerFrame
523 && GetNextFrame()->m_frame_type != eDebuggerFrame
524 && addr_range.GetBaseAddress().IsValid()
525 && addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection()
526 && addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset())
528 decr_pc_and_recompute_addr_range = true;
531 // We need to back up the pc by 1 byte and re-search for the Symbol to handle the case where the "saved pc"
532 // value is pointing to the next function, e.g. if a function ends with a CALL instruction.
533 // FIXME this may need to be an architectural-dependent behavior; if so we'll need to add a member function
534 // to the ABI plugin and consult that.
535 if (decr_pc_and_recompute_addr_range)
537 UnwindLogMsg ("Backing up the pc value of 0x%" PRIx64 " by 1 and re-doing symbol lookup; old symbol was %s",
538 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
539 Address temporary_pc;
540 temporary_pc.SetLoadAddress (pc - 1, &process->GetTarget());
541 m_sym_ctx.Clear (false);
542 m_sym_ctx_valid = false;
543 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
545 ModuleSP temporary_module_sp = temporary_pc.GetModule();
546 if (temporary_module_sp &&
547 temporary_module_sp->ResolveSymbolContextForAddress (temporary_pc, resolve_scope, m_sym_ctx) & resolve_scope)
549 if (m_sym_ctx.GetAddressRange (resolve_scope, 0, false, addr_range))
550 m_sym_ctx_valid = true;
552 UnwindLogMsg ("Symbol is now %s", GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
555 // If we were able to find a symbol/function, set addr_range_ptr to the bounds of that symbol/function.
556 // else treat the current pc value as the start_pc and record no offset.
557 if (addr_range.GetBaseAddress().IsValid())
559 m_start_pc = addr_range.GetBaseAddress();
560 m_current_offset = pc - m_start_pc.GetLoadAddress (&process->GetTarget());
561 m_current_offset_backed_up_one = m_current_offset;
562 if (decr_pc_and_recompute_addr_range && m_current_offset_backed_up_one > 0)
564 m_current_offset_backed_up_one--;
567 m_current_pc.SetLoadAddress (pc - 1, &process->GetTarget());
573 m_start_pc = m_current_pc;
574 m_current_offset = -1;
575 m_current_offset_backed_up_one = -1;
578 if (IsTrapHandlerSymbol (process, m_sym_ctx))
580 m_frame_type = eTrapHandlerFrame;
584 // FIXME: Detect eDebuggerFrame here.
585 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
587 m_frame_type = eNormalFrame;
591 // We've set m_frame_type and m_sym_ctx before this call.
592 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame ();
594 UnwindPlan::RowSP active_row;
595 RegisterKind row_register_kind = eRegisterKindGeneric;
597 // Try to get by with just the fast UnwindPlan if possible - the full UnwindPlan may be expensive to get
598 // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for the first time.)
600 if (m_fast_unwind_plan_sp && m_fast_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
602 active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
603 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind ();
604 if (active_row.get() && log)
606 StreamString active_row_strm;
607 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
608 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str());
613 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
614 int valid_offset = -1;
615 if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp, valid_offset))
617 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (valid_offset);
618 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
619 if (active_row.get() && log)
621 StreamString active_row_strm;
622 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
623 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str());
628 if (!active_row.get())
630 m_frame_type = eNotAValidFrame;
631 UnwindLogMsg ("could not find unwind row for this pc");
635 if (!ReadCFAValueForRow (row_register_kind, active_row, m_cfa))
637 UnwindLogMsg ("failed to get cfa");
638 m_frame_type = eNotAValidFrame;
642 UnwindLogMsg ("m_cfa = 0x%" PRIx64, m_cfa);
644 if (CheckIfLoopingStack ())
646 TryFallbackUnwindPlan();
647 if (CheckIfLoopingStack ())
649 UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping");
650 m_frame_type = eNotAValidFrame;
655 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64,
656 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), (uint64_t) m_cfa);
660 RegisterContextLLDB::CheckIfLoopingStack ()
662 // If we have a bad stack setup, we can get the same CFA value multiple times -- or even
663 // more devious, we can actually oscillate between two CFA values. Detect that here and
664 // break out to avoid a possible infinite loop in lldb trying to unwind the stack.
665 // To detect when we have the same CFA value multiple times, we compare the CFA of the current
666 // frame with the 2nd next frame because in some specail case (e.g. signal hanlders, hand
667 // written assembly without ABI compiance) we can have 2 frames with the same CFA (in theory we
668 // can have arbitrary number of frames with the same CFA, but more then 2 is very very unlikely)
670 RegisterContextLLDB::SharedPtr next_frame = GetNextFrame();
673 RegisterContextLLDB::SharedPtr next_next_frame = next_frame->GetNextFrame();
674 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
675 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa))
677 if (next_next_frame_cfa == m_cfa)
679 // We have a loop in the stack unwind
688 RegisterContextLLDB::IsFrameZero () const
690 return m_frame_number == 0;
694 // Find a fast unwind plan for this frame, if possible.
696 // On entry to this method,
698 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct,
699 // 2. m_sym_ctx should already be filled in, and
700 // 3. m_current_pc should have the current pc value for this frame
701 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown
704 RegisterContextLLDB::GetFastUnwindPlanForFrame ()
706 UnwindPlanSP unwind_plan_sp;
707 ModuleSP pc_module_sp (m_current_pc.GetModule());
709 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL)
710 return unwind_plan_sp;
713 return unwind_plan_sp;
715 FuncUnwindersSP func_unwinders_sp (pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx));
716 if (!func_unwinders_sp)
717 return unwind_plan_sp;
719 // If we're in _sigtramp(), unwinding past this frame requires special knowledge.
720 if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame)
721 return unwind_plan_sp;
723 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind (*m_thread.CalculateTarget(), m_thread);
726 if (unwind_plan_sp->PlanValidAtAddress (m_current_pc))
728 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
729 if (log && log->GetVerbose())
731 if (m_fast_unwind_plan_sp)
732 UnwindLogMsgVerbose ("frame, and has a fast UnwindPlan");
734 UnwindLogMsgVerbose ("frame");
736 m_frame_type = eNormalFrame;
737 return unwind_plan_sp;
741 unwind_plan_sp.reset();
744 return unwind_plan_sp;
747 // On entry to this method,
749 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame if either of those are correct,
750 // 2. m_sym_ctx should already be filled in, and
751 // 3. m_current_pc should have the current pc value for this frame
752 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown
755 RegisterContextLLDB::GetFullUnwindPlanForFrame ()
757 UnwindPlanSP unwind_plan_sp;
758 UnwindPlanSP arch_default_unwind_plan_sp;
759 ExecutionContext exe_ctx(m_thread.shared_from_this());
760 Process *process = exe_ctx.GetProcessPtr();
761 ABI *abi = process ? process->GetABI().get() : NULL;
764 arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
765 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
769 UnwindLogMsg ("unable to get architectural default UnwindPlan from ABI plugin");
772 bool behaves_like_zeroth_frame = false;
774 || GetNextFrame()->m_frame_type == eTrapHandlerFrame
775 || GetNextFrame()->m_frame_type == eDebuggerFrame)
777 behaves_like_zeroth_frame = true;
778 // If this frame behaves like a 0th frame (currently executing or
779 // interrupted asynchronously), all registers can be retrieved.
780 m_all_registers_available = true;
783 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer) so the pc is 0x0
784 // in the zeroth frame, we need to use the "unwind at first instruction" arch default UnwindPlan
785 // Also, if this Process can report on memory region attributes, any non-executable region means
786 // we jumped through a bad function pointer - handle the same way as 0x0.
787 // Note, if we have a symbol context & a symbol, we don't want to follow this code path. This is
788 // for jumping to memory regions without any information available.
790 if ((!m_sym_ctx_valid || (m_sym_ctx.function == NULL && m_sym_ctx.symbol == NULL)) && behaves_like_zeroth_frame && m_current_pc.IsValid())
792 uint32_t permissions;
793 addr_t current_pc_addr = m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr());
794 if (current_pc_addr == 0
796 process->GetLoadAddressPermissions (current_pc_addr, permissions)
797 && (permissions & ePermissionsExecutable) == 0))
801 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
802 abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp);
803 m_frame_type = eNormalFrame;
804 return unwind_plan_sp;
809 // No Module for the current pc, try using the architecture default unwind.
810 ModuleSP pc_module_sp (m_current_pc.GetModule());
811 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL)
813 m_frame_type = eNormalFrame;
814 return arch_default_unwind_plan_sp;
817 FuncUnwindersSP func_unwinders_sp;
820 func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx);
823 // No FuncUnwinders available for this pc (stripped function symbols, lldb could not augment its
824 // function table with another source, like LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO).
825 // See if eh_frame or the .ARM.exidx tables have unwind information for this address, else fall
826 // back to the architectural default unwind.
827 if (!func_unwinders_sp)
829 m_frame_type = eNormalFrame;
831 if (!pc_module_sp || !pc_module_sp->GetObjectFile() || !m_current_pc.IsValid())
832 return arch_default_unwind_plan_sp;
834 // Even with -fomit-frame-pointer, we can try eh_frame to get back on track.
835 DWARFCallFrameInfo *eh_frame = pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo();
838 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
839 if (eh_frame->GetUnwindPlan (m_current_pc, *unwind_plan_sp))
840 return unwind_plan_sp;
842 unwind_plan_sp.reset();
845 ArmUnwindInfo *arm_exidx = pc_module_sp->GetObjectFile()->GetUnwindTable().GetArmUnwindInfo();
848 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
849 if (arm_exidx->GetUnwindPlan (exe_ctx.GetTargetRef(), m_current_pc, *unwind_plan_sp))
850 return unwind_plan_sp;
852 unwind_plan_sp.reset();
855 return arch_default_unwind_plan_sp;
858 // If we're in _sigtramp(), unwinding past this frame requires special knowledge. On Mac OS X this knowledge
859 // is properly encoded in the eh_frame section, so prefer that if available.
860 // On other platforms we may need to provide a platform-specific UnwindPlan which encodes the details of
861 // how to unwind out of sigtramp.
862 if (m_frame_type == eTrapHandlerFrame && process)
864 m_fast_unwind_plan_sp.reset();
865 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan (process->GetTarget(), m_current_offset_backed_up_one);
866 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc) && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
868 return unwind_plan_sp;
872 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame even when it's frame zero
873 // This comes up if we have hand-written functions in a Module and hand-written eh_frame. The assembly
874 // instruction inspection may fail and the eh_frame CFI were probably written with some care to do the
875 // right thing. It'd be nice if there was a way to ask the eh_frame directly if it is asynchronous
876 // (can be trusted at every instruction point) or synchronous (the normal case - only at call sites).
878 if (process && process->GetDynamicLoader() && process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo (m_sym_ctx))
880 // We must specifically call the GetEHFrameUnwindPlan() method here -- normally we would
881 // call GetUnwindPlanAtCallSite() -- because CallSite may return an unwind plan sourced from
882 // either eh_frame (that's what we intend) or compact unwind (this won't work)
883 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan (process->GetTarget(), m_current_offset_backed_up_one);
884 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
886 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan because the DynamicLoader suggested we prefer it",
887 unwind_plan_sp->GetSourceName().GetCString());
888 return unwind_plan_sp;
892 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting the assembly language instructions
893 if (behaves_like_zeroth_frame && process)
895 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one);
896 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
898 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo)
900 // We probably have an UnwindPlan created by inspecting assembly instructions. The
901 // assembly profilers work really well with compiler-generated functions but hand-
902 // written assembly can be problematic. We set the eh_frame based unwind plan as our
903 // fallback unwind plan if instruction emulation doesn't work out even for non call
904 // sites if it is available and use the architecture default unwind plan if it is
905 // not available. The eh_frame unwind plan is more reliable even on non call sites
906 // then the architecture default plan and for hand written assembly code it is often
907 // written in a way that it valid at all location what helps in the most common
908 // cases when the instruction emulation fails.
909 UnwindPlanSP call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(), m_current_offset_backed_up_one);
910 if (call_site_unwind_plan &&
911 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
912 call_site_unwind_plan->GetSourceName() != unwind_plan_sp->GetSourceName())
914 m_fallback_unwind_plan_sp = call_site_unwind_plan;
918 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
921 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
922 return unwind_plan_sp;
926 // Typically this is unwind info from an eh_frame section intended for exception handling; only valid at call sites
929 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (process->GetTarget(), m_current_offset_backed_up_one);
931 int valid_offset = -1;
932 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset))
934 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
935 return unwind_plan_sp;
938 // We'd prefer to use an UnwindPlan intended for call sites when we're at a call site but if we've
939 // struck out on that, fall back to using the non-call-site assembly inspection UnwindPlan if possible.
942 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (process->GetTarget(), m_thread, m_current_offset_backed_up_one);
944 if (unwind_plan_sp && unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo)
946 // We probably have an UnwindPlan created by inspecting assembly instructions. The assembly
947 // profilers work really well with compiler-generated functions but hand- written assembly
948 // can be problematic. We set the eh_frame based unwind plan as our fallback unwind plan if
949 // instruction emulation doesn't work out even for non call sites if it is available and use
950 // the architecture default unwind plan if it is not available. The eh_frame unwind plan is
951 // more reliable even on non call sites then the architecture default plan and for hand
952 // written assembly code it is often written in a way that it valid at all location what
953 // helps in the most common cases when the instruction emulation fails.
954 UnwindPlanSP call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(), m_current_offset_backed_up_one);
955 if (call_site_unwind_plan &&
956 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
957 call_site_unwind_plan->GetSourceName() != unwind_plan_sp->GetSourceName())
959 m_fallback_unwind_plan_sp = call_site_unwind_plan;
963 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
967 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset))
969 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
970 return unwind_plan_sp;
973 // If we're on the first instruction of a function, and we have an architectural default UnwindPlan
974 // for the initial instruction of a function, use that.
975 if (m_current_offset_backed_up_one == 0)
977 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry (m_thread);
980 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
981 return unwind_plan_sp;
985 // If nothing else, use the architectural default UnwindPlan and hope that does the job.
986 if (arch_default_unwind_plan_sp)
987 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", arch_default_unwind_plan_sp->GetSourceName().GetCString());
989 UnwindLogMsg ("Unable to find any UnwindPlan for full unwind of this frame.");
991 return arch_default_unwind_plan_sp;
996 RegisterContextLLDB::InvalidateAllRegisters ()
998 m_frame_type = eNotAValidFrame;
1002 RegisterContextLLDB::GetRegisterCount ()
1004 return m_thread.GetRegisterContext()->GetRegisterCount();
1007 const RegisterInfo *
1008 RegisterContextLLDB::GetRegisterInfoAtIndex (size_t reg)
1010 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex (reg);
1014 RegisterContextLLDB::GetRegisterSetCount ()
1016 return m_thread.GetRegisterContext()->GetRegisterSetCount ();
1020 RegisterContextLLDB::GetRegisterSet (size_t reg_set)
1022 return m_thread.GetRegisterContext()->GetRegisterSet (reg_set);
1026 RegisterContextLLDB::ConvertRegisterKindToRegisterNumber (lldb::RegisterKind kind, uint32_t num)
1028 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber (kind, num);
1032 RegisterContextLLDB::ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
1033 const RegisterInfo *reg_info,
1034 RegisterValue &value)
1038 bool success = false;
1040 switch (regloc.type)
1042 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext:
1044 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
1046 if (!other_reg_info)
1049 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value);
1052 case UnwindLLDB::RegisterLocation::eRegisterInRegister:
1054 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
1056 if (!other_reg_info)
1061 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value);
1065 success = GetNextFrame()->ReadRegister (other_reg_info, value);
1069 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1070 success = value.SetUInt (regloc.location.inferred_value, reg_info->byte_size);
1073 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1075 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1076 assert ("FIXME debugger inferior function call unwind");
1078 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation:
1080 Error error (ReadRegisterValueFromMemory(reg_info,
1081 regloc.location.target_memory_location,
1082 reg_info->byte_size,
1084 success = error.Success();
1088 assert ("Unknown RegisterLocation type.");
1095 RegisterContextLLDB::WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
1096 const RegisterInfo *reg_info,
1097 const RegisterValue &value)
1102 bool success = false;
1104 switch (regloc.type)
1106 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext:
1108 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
1109 success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value);
1112 case UnwindLLDB::RegisterLocation::eRegisterInRegister:
1114 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
1117 success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value);
1121 success = GetNextFrame()->WriteRegister (other_reg_info, value);
1125 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1126 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1128 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1129 assert ("FIXME debugger inferior function call unwind");
1131 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation:
1133 Error error (WriteRegisterValueToMemory (reg_info,
1134 regloc.location.target_memory_location,
1135 reg_info->byte_size,
1137 success = error.Success();
1141 assert ("Unknown RegisterLocation type.");
1149 RegisterContextLLDB::IsValid () const
1151 return m_frame_type != eNotAValidFrame;
1154 // After the final stack frame in a stack walk we'll get one invalid (eNotAValidFrame) stack frame --
1155 // one past the end of the stack walk. But higher-level code will need to tell the differnece between
1156 // "the unwind plan below this frame failed" versus "we successfully completed the stack walk" so
1157 // this method helps to disambiguate that.
1160 RegisterContextLLDB::IsTrapHandlerFrame () const
1162 return m_frame_type == eTrapHandlerFrame;
1165 // A skip frame is a bogus frame on the stack -- but one where we're likely to find a real frame farther
1166 // up the stack if we keep looking. It's always the second frame in an unwind (i.e. the first frame after
1167 // frame zero) where unwinding can be the trickiest. Ideally we'll mark up this frame in some way so the
1168 // user knows we're displaying bad data and we may have skipped one frame of their real program in the
1169 // process of getting back on track.
1172 RegisterContextLLDB::IsSkipFrame () const
1174 return m_frame_type == eSkipFrame;
1178 RegisterContextLLDB::IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const
1180 PlatformSP platform_sp (process->GetTarget().GetPlatform());
1183 const std::vector<ConstString> trap_handler_names (platform_sp->GetTrapHandlerSymbolNames());
1184 for (ConstString name : trap_handler_names)
1186 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1187 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name))
1193 const std::vector<ConstString> user_specified_trap_handler_names (m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames());
1194 for (ConstString name : user_specified_trap_handler_names)
1196 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1197 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name))
1206 // Answer the question: Where did THIS frame save the CALLER frame ("previous" frame)'s register value?
1208 enum UnwindLLDB::RegisterSearchResult
1209 RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc)
1211 RegisterNumber regnum (m_thread, eRegisterKindLLDB, lldb_regnum);
1213 // Have we already found this register location?
1214 if (!m_registers.empty())
1216 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>::const_iterator iterator;
1217 iterator = m_registers.find (regnum.GetAsKind (eRegisterKindLLDB));
1218 if (iterator != m_registers.end())
1220 regloc = iterator->second;
1221 UnwindLogMsg ("supplying caller's saved %s (%d)'s location, cached",
1222 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1223 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1227 // Look through the available UnwindPlans for the register location.
1229 UnwindPlan::Row::RegisterLocation unwindplan_regloc;
1230 bool have_unwindplan_regloc = false;
1231 RegisterKind unwindplan_registerkind = kNumRegisterKinds;
1233 if (m_fast_unwind_plan_sp)
1235 UnwindPlan::RowSP active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1236 unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind ();
1237 if (regnum.GetAsKind (unwindplan_registerkind) == LLDB_INVALID_REGNUM)
1239 UnwindLogMsg ("could not convert lldb regnum %s (%d) into %d RegisterKind reg numbering scheme",
1240 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), (int) unwindplan_registerkind);
1241 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1243 if (active_row->GetRegisterInfo (regnum.GetAsKind (unwindplan_registerkind), unwindplan_regloc))
1245 UnwindLogMsg ("supplying caller's saved %s (%d)'s location using FastUnwindPlan",
1246 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1247 have_unwindplan_regloc = true;
1251 if (!have_unwindplan_regloc)
1253 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a full UnwindPlan yet
1254 if (!m_full_unwind_plan_sp)
1255 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
1257 if (m_full_unwind_plan_sp)
1259 RegisterNumber pc_regnum (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1261 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1262 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind ();
1264 RegisterNumber return_address_reg;
1266 // If we're fetching the saved pc and this UnwindPlan defines a ReturnAddress register (e.g. lr on arm),
1267 // look for the return address register number in the UnwindPlan's row.
1268 if (pc_regnum.IsValid()
1269 && pc_regnum == regnum
1270 && m_full_unwind_plan_sp->GetReturnAddressRegister() != LLDB_INVALID_REGNUM)
1273 return_address_reg.init (m_thread, m_full_unwind_plan_sp->GetRegisterKind(), m_full_unwind_plan_sp->GetReturnAddressRegister());
1274 regnum = return_address_reg;
1275 UnwindLogMsg ("requested caller's saved PC but this UnwindPlan uses a RA reg; getting %s (%d) instead",
1276 return_address_reg.GetName(), return_address_reg.GetAsKind (eRegisterKindLLDB));
1280 if (regnum.GetAsKind (unwindplan_registerkind) == LLDB_INVALID_REGNUM)
1282 if (unwindplan_registerkind == eRegisterKindGeneric)
1284 UnwindLogMsg ("could not convert lldb regnum %s (%d) into eRegisterKindGeneric reg numbering scheme",
1285 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1289 UnwindLogMsg ("could not convert lldb regnum %s (%d) into %d RegisterKind reg numbering scheme",
1290 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB), (int) unwindplan_registerkind);
1292 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1296 if (regnum.IsValid()
1297 && active_row->GetRegisterInfo (regnum.GetAsKind (unwindplan_registerkind), unwindplan_regloc))
1299 have_unwindplan_regloc = true;
1300 UnwindLogMsg ("supplying caller's saved %s (%d)'s location using %s UnwindPlan",
1301 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB),
1302 m_full_unwind_plan_sp->GetSourceName().GetCString());
1305 // This is frame 0 and we're retrieving the PC and it's saved in a Return Address register and
1306 // it hasn't been saved anywhere yet -- that is, it's still live in the actual register.
1307 // Handle this specially.
1309 if (have_unwindplan_regloc == false
1310 && return_address_reg.IsValid()
1313 if (return_address_reg.GetAsKind (eRegisterKindLLDB) != LLDB_INVALID_REGNUM)
1315 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1316 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1317 new_regloc.location.register_number = return_address_reg.GetAsKind (eRegisterKindLLDB);
1318 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = new_regloc;
1319 regloc = new_regloc;
1320 UnwindLogMsg ("supplying caller's register %s (%d) from the live RegisterContext at frame 0, saved in %d",
1321 return_address_reg.GetName(), return_address_reg.GetAsKind (eRegisterKindLLDB),
1322 return_address_reg.GetAsKind (eRegisterKindLLDB));
1323 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1327 // If this architecture stores the return address in a register (it defines a Return Address register)
1328 // and we're on a non-zero stack frame and the Full UnwindPlan says that the pc is stored in the
1329 // RA registers (e.g. lr on arm), then we know that the full unwindplan is not trustworthy -- this
1330 // is an impossible situation and the instruction emulation code has likely been misled.
1331 // If this stack frame meets those criteria, we need to throw away the Full UnwindPlan that the
1332 // instruction emulation came up with and fall back to the architecture's Default UnwindPlan so
1333 // the stack walk can get past this point.
1335 // Special note: If the Full UnwindPlan was generated from the compiler, don't second-guess it
1336 // when we're at a call site location.
1338 // arch_default_ra_regnum is the return address register # in the Full UnwindPlan register numbering
1339 RegisterNumber arch_default_ra_regnum (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA);
1341 if (arch_default_ra_regnum.GetAsKind (unwindplan_registerkind) != LLDB_INVALID_REGNUM
1342 && pc_regnum == regnum
1343 && unwindplan_regloc.IsInOtherRegister()
1344 && unwindplan_regloc.GetRegisterNumber() == arch_default_ra_regnum.GetAsKind (unwindplan_registerkind)
1345 && m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes
1346 && !m_all_registers_available)
1348 UnwindLogMsg ("%s UnwindPlan tried to restore the pc from the link register but this is a non-zero frame",
1349 m_full_unwind_plan_sp->GetSourceName().GetCString());
1351 // Throw away the full unwindplan; install the arch default unwindplan
1352 if (ForceSwitchToFallbackUnwindPlan())
1354 // Update for the possibly new unwind plan
1355 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind ();
1356 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1358 // Sanity check: Verify that we can fetch a pc value and CFA value with this unwind plan
1360 RegisterNumber arch_default_pc_reg (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1361 bool can_fetch_pc_value = false;
1362 bool can_fetch_cfa = false;
1366 if (arch_default_pc_reg.GetAsKind (unwindplan_registerkind) != LLDB_INVALID_REGNUM
1367 && active_row->GetRegisterInfo (arch_default_pc_reg.GetAsKind (unwindplan_registerkind), unwindplan_regloc))
1369 can_fetch_pc_value = true;
1371 if (ReadCFAValueForRow (unwindplan_registerkind, active_row, cfa_value))
1373 can_fetch_cfa = true;
1377 if (can_fetch_pc_value && can_fetch_cfa)
1379 have_unwindplan_regloc = true;
1383 have_unwindplan_regloc = false;
1388 // We were unable to fall back to another unwind plan
1389 have_unwindplan_regloc = false;
1395 ExecutionContext exe_ctx(m_thread.shared_from_this());
1396 Process *process = exe_ctx.GetProcessPtr();
1397 if (have_unwindplan_regloc == false)
1399 // If the UnwindPlan failed to give us an unwind location for this register, we may be able to fall back
1400 // to some ABI-defined default. For example, some ABIs allow to determine the caller's SP via the CFA.
1401 // Also, the ABI may set volatile registers to the undefined state.
1402 ABI *abi = process ? process->GetABI().get() : NULL;
1405 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(regnum.GetAsKind (eRegisterKindLLDB));
1406 if (reg_info && abi->GetFallbackRegisterLocation (reg_info, unwindplan_regloc))
1408 UnwindLogMsg ("supplying caller's saved %s (%d)'s location using ABI default",
1409 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1410 have_unwindplan_regloc = true;
1415 if (have_unwindplan_regloc == false)
1419 // This is frame 0 - we should return the actual live register context value
1420 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1421 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1422 new_regloc.location.register_number = regnum.GetAsKind (eRegisterKindLLDB);
1423 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = new_regloc;
1424 regloc = new_regloc;
1425 UnwindLogMsg ("supplying caller's register %s (%d) from the live RegisterContext at frame 0",
1426 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1427 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1431 std::string unwindplan_name ("");
1432 if (m_full_unwind_plan_sp)
1434 unwindplan_name += "via '";
1435 unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString();
1436 unwindplan_name += "'";
1438 UnwindLogMsg ("no save location for %s (%d) %s",
1439 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB),
1440 unwindplan_name.c_str());
1442 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1445 // unwindplan_regloc has valid contents about where to retrieve the register
1446 if (unwindplan_regloc.IsUnspecified())
1448 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1449 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved;
1450 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = new_regloc;
1451 UnwindLogMsg ("save location for %s (%d) is unspecified, continue searching",
1452 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1453 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1456 if (unwindplan_regloc.IsUndefined())
1458 UnwindLogMsg ("did not supply reg location for %s (%d) because it is volatile",
1459 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1460 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile;
1463 if (unwindplan_regloc.IsSame())
1465 if (IsFrameZero() == false
1466 && (regnum.GetAsKind (eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_PC
1467 || regnum.GetAsKind (eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_RA))
1469 UnwindLogMsg ("register %s (%d) is marked as 'IsSame' - it is a pc or return address reg on a non-zero frame -- treat as if we have no information",
1470 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1471 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1475 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1476 regloc.location.register_number = regnum.GetAsKind (eRegisterKindLLDB);
1477 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc;
1478 UnwindLogMsg ("supplying caller's register %s (%d), saved in register %s (%d)",
1479 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB),
1480 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1481 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1485 if (unwindplan_regloc.IsCFAPlusOffset())
1487 int offset = unwindplan_regloc.GetOffset();
1488 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1489 regloc.location.inferred_value = m_cfa + offset;
1490 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc;
1491 UnwindLogMsg ("supplying caller's register %s (%d), value is CFA plus offset %d [value is 0x%" PRIx64 "]",
1492 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB),
1493 offset, regloc.location.inferred_value);
1494 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1497 if (unwindplan_regloc.IsAtCFAPlusOffset())
1499 int offset = unwindplan_regloc.GetOffset();
1500 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1501 regloc.location.target_memory_location = m_cfa + offset;
1502 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc;
1503 UnwindLogMsg ("supplying caller's register %s (%d) from the stack, saved at CFA plus offset %d [saved at 0x%" PRIx64 "]",
1504 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB),
1505 offset, regloc.location.target_memory_location);
1506 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1509 if (unwindplan_regloc.IsInOtherRegister())
1511 uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber();
1512 RegisterNumber row_regnum (m_thread, unwindplan_registerkind, unwindplan_regnum);
1513 if (row_regnum.GetAsKind (eRegisterKindLLDB) == LLDB_INVALID_REGNUM)
1515 UnwindLogMsg ("could not supply caller's %s (%d) location - was saved in another reg but couldn't convert that regnum",
1516 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1517 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1519 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1520 regloc.location.register_number = row_regnum.GetAsKind (eRegisterKindLLDB);
1521 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc;
1522 UnwindLogMsg ("supplying caller's register %s (%d), saved in register %s (%d)",
1523 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB),
1524 row_regnum.GetName(), row_regnum.GetAsKind (eRegisterKindLLDB));
1525 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1528 if (unwindplan_regloc.IsDWARFExpression() || unwindplan_regloc.IsAtDWARFExpression())
1530 DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(),
1531 unwindplan_regloc.GetDWARFExpressionLength(),
1532 process->GetByteOrder(), process->GetAddressByteSize());
1533 ModuleSP opcode_ctx;
1534 DWARFExpression dwarfexpr (opcode_ctx,
1538 unwindplan_regloc.GetDWARFExpressionLength());
1539 dwarfexpr.SetRegisterKind (unwindplan_registerkind);
1542 if (dwarfexpr.Evaluate (&exe_ctx, nullptr, nullptr, this, 0, nullptr, nullptr, result, &error))
1545 val = result.GetScalar().ULongLong();
1546 if (unwindplan_regloc.IsDWARFExpression())
1548 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1549 regloc.location.inferred_value = val;
1550 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc;
1551 UnwindLogMsg ("supplying caller's register %s (%d) via DWARF expression (IsDWARFExpression)",
1552 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1553 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1557 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1558 regloc.location.target_memory_location = val;
1559 m_registers[regnum.GetAsKind (eRegisterKindLLDB)] = regloc;
1560 UnwindLogMsg ("supplying caller's register %s (%d) via DWARF expression (IsAtDWARFExpression)",
1561 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1562 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1565 UnwindLogMsg ("tried to use IsDWARFExpression or IsAtDWARFExpression for %s (%d) but failed",
1566 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1567 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1570 UnwindLogMsg ("no save location for %s (%d) in this stack frame",
1571 regnum.GetName(), regnum.GetAsKind (eRegisterKindLLDB));
1573 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are unsupported.
1575 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1578 // TryFallbackUnwindPlan() -- this method is a little tricky.
1580 // When this is called, the frame above -- the caller frame, the "previous" frame --
1581 // is invalid or bad.
1583 // Instead of stopping the stack walk here, we'll try a different UnwindPlan and see
1584 // if we can get a valid frame above us.
1586 // This most often happens when an unwind plan based on assembly instruction inspection
1587 // is not correct -- mostly with hand-written assembly functions or functions where the
1588 // stack frame is set up "out of band", e.g. the kernel saved the register context and
1589 // then called an asynchronous trap handler like _sigtramp.
1591 // Often in these cases, if we just do a dumb stack walk we'll get past this tricky
1592 // frame and our usual techniques can continue to be used.
1595 RegisterContextLLDB::TryFallbackUnwindPlan ()
1597 if (m_fallback_unwind_plan_sp.get() == nullptr)
1600 if (m_full_unwind_plan_sp.get() == nullptr)
1603 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get()
1604 || m_full_unwind_plan_sp->GetSourceName() == m_fallback_unwind_plan_sp->GetSourceName())
1609 // If a compiler generated unwind plan failed, trying the arch default unwindplan
1610 // isn't going to do any better.
1611 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1615 // Get the caller's pc value and our own CFA value.
1616 // Swap in the fallback unwind plan, re-fetch the caller's pc value and CFA value.
1617 // If they're the same, then the fallback unwind plan provides no benefit.
1619 RegisterNumber pc_regnum (m_thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1621 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1622 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1623 addr_t old_this_frame_cfa_value = m_cfa;
1624 UnwindLLDB::RegisterLocation regloc;
1625 if (SavedLocationForRegister (pc_regnum.GetAsKind (eRegisterKindLLDB), regloc) == UnwindLLDB::RegisterSearchResult::eRegisterFound)
1627 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(pc_regnum.GetAsKind (eRegisterKindLLDB));
1630 RegisterValue reg_value;
1631 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value))
1633 old_caller_pc_value = reg_value.GetAsUInt64();
1638 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really impossible
1639 // register location for the full unwind plan, it may call ForceSwitchToFallbackUnwindPlan()
1640 // which in turn replaces the full unwindplan with the fallback... in short, we're done,
1641 // we're using the fallback UnwindPlan.
1642 // We checked if m_fallback_unwind_plan_sp was nullptr at the top -- the only way it
1643 // became nullptr since then is via SavedLocationForRegister().
1644 if (m_fallback_unwind_plan_sp.get() == nullptr)
1648 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide this isn't
1649 // working, we need to restore.
1650 // We'll also need to save & restore the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1651 UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp;
1652 addr_t old_cfa = m_cfa;
1654 m_registers.clear();
1656 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1658 UnwindPlan::RowSP active_row = m_fallback_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1660 if (active_row && active_row->GetCFAValue().GetValueType() != UnwindPlan::Row::CFAValue::unspecified)
1663 if (!ReadCFAValueForRow (m_fallback_unwind_plan_sp->GetRegisterKind(), active_row, new_cfa)
1664 || new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS)
1666 UnwindLogMsg ("failed to get cfa with fallback unwindplan");
1667 m_fallback_unwind_plan_sp.reset();
1668 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1674 if (SavedLocationForRegister (pc_regnum.GetAsKind (eRegisterKindLLDB), regloc) == UnwindLLDB::RegisterSearchResult::eRegisterFound)
1676 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(pc_regnum.GetAsKind (eRegisterKindLLDB));
1679 RegisterValue reg_value;
1680 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value))
1682 new_caller_pc_value = reg_value.GetAsUInt64();
1688 if (new_caller_pc_value == LLDB_INVALID_ADDRESS)
1690 UnwindLogMsg ("failed to get a pc value for the caller frame with the fallback unwind plan");
1691 m_fallback_unwind_plan_sp.reset();
1692 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1697 if (old_caller_pc_value != LLDB_INVALID_ADDRESS)
1699 if (old_caller_pc_value == new_caller_pc_value && new_cfa == old_this_frame_cfa_value)
1701 UnwindLogMsg ("fallback unwind plan got the same values for this frame CFA and caller frame pc, not using");
1702 m_fallback_unwind_plan_sp.reset();
1703 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1709 UnwindLogMsg ("trying to unwind from this function with the UnwindPlan '%s' because UnwindPlan '%s' failed.",
1710 m_fallback_unwind_plan_sp->GetSourceName().GetCString(),
1711 original_full_unwind_plan_sp->GetSourceName().GetCString());
1713 // We've copied the fallback unwind plan into the full - now clear the fallback.
1714 m_fallback_unwind_plan_sp.reset();
1721 RegisterContextLLDB::ForceSwitchToFallbackUnwindPlan ()
1723 if (m_fallback_unwind_plan_sp.get() == NULL)
1726 if (m_full_unwind_plan_sp.get() == NULL)
1729 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get()
1730 || m_full_unwind_plan_sp->GetSourceName() == m_fallback_unwind_plan_sp->GetSourceName())
1735 UnwindPlan::RowSP active_row = m_fallback_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1737 if (active_row && active_row->GetCFAValue().GetValueType() != UnwindPlan::Row::CFAValue::unspecified)
1740 if (!ReadCFAValueForRow (m_fallback_unwind_plan_sp->GetRegisterKind(), active_row, new_cfa)
1741 || new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS)
1743 UnwindLogMsg ("failed to get cfa with fallback unwindplan");
1744 m_fallback_unwind_plan_sp.reset();
1748 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1749 m_fallback_unwind_plan_sp.reset();
1751 m_registers.clear();
1755 UnwindLogMsg ("switched unconditionally to the fallback unwindplan %s", m_full_unwind_plan_sp->GetSourceName().GetCString());
1762 RegisterContextLLDB::ReadCFAValueForRow (lldb::RegisterKind row_register_kind,
1763 const UnwindPlan::RowSP &row,
1766 RegisterValue reg_value;
1768 cfa_value = LLDB_INVALID_ADDRESS;
1769 addr_t cfa_reg_contents;
1771 switch (row->GetCFAValue().GetValueType())
1773 case UnwindPlan::Row::CFAValue::isRegisterDereferenced:
1775 RegisterNumber cfa_reg (m_thread, row_register_kind, row->GetCFAValue().GetRegisterNumber());
1776 if (ReadGPRValue (cfa_reg, cfa_reg_contents))
1778 const RegisterInfo *reg_info = GetRegisterInfoAtIndex (cfa_reg.GetAsKind (eRegisterKindLLDB));
1779 RegisterValue reg_value;
1782 Error error = ReadRegisterValueFromMemory(reg_info,
1784 reg_info->byte_size,
1786 if (error.Success ())
1788 cfa_value = reg_value.GetAsUInt64();
1789 UnwindLogMsg ("CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64 ", CFA value is 0x%" PRIx64,
1790 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB),
1791 cfa_reg_contents, cfa_value);
1796 UnwindLogMsg ("Tried to deref reg %s (%d) [0x%" PRIx64 "] but memory read failed.",
1797 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB),
1804 case UnwindPlan::Row::CFAValue::isRegisterPlusOffset:
1806 RegisterNumber cfa_reg (m_thread, row_register_kind, row->GetCFAValue().GetRegisterNumber());
1807 if (ReadGPRValue (cfa_reg, cfa_reg_contents))
1809 if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 || cfa_reg_contents == 1)
1811 UnwindLogMsg ("Got an invalid CFA register value - reg %s (%d), value 0x%" PRIx64,
1812 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB),
1814 cfa_reg_contents = LLDB_INVALID_ADDRESS;
1817 cfa_value = cfa_reg_contents + row->GetCFAValue().GetOffset();
1818 UnwindLogMsg ("CFA is 0x%" PRIx64 ": Register %s (%d) contents are 0x%" PRIx64 ", offset is %d",
1820 cfa_reg.GetName(), cfa_reg.GetAsKind (eRegisterKindLLDB),
1821 cfa_reg_contents, row->GetCFAValue().GetOffset());
1826 case UnwindPlan::Row::CFAValue::isDWARFExpression:
1828 ExecutionContext exe_ctx(m_thread.shared_from_this());
1829 Process *process = exe_ctx.GetProcessPtr();
1830 DataExtractor dwarfdata (row->GetCFAValue().GetDWARFExpressionBytes(),
1831 row->GetCFAValue().GetDWARFExpressionLength(),
1832 process->GetByteOrder(), process->GetAddressByteSize());
1833 ModuleSP opcode_ctx;
1834 DWARFExpression dwarfexpr (opcode_ctx,
1838 row->GetCFAValue().GetDWARFExpressionLength());
1839 dwarfexpr.SetRegisterKind (row_register_kind);
1842 if (dwarfexpr.Evaluate (&exe_ctx, nullptr, nullptr, this, 0, nullptr, nullptr, result, &error))
1844 cfa_value = result.GetScalar().ULongLong();
1846 UnwindLogMsg ("CFA value set by DWARF expression is 0x%" PRIx64, cfa_value);
1849 UnwindLogMsg ("Failed to set CFA value via DWARF expression: %s", error.AsCString());
1858 // Retrieve a general purpose register value for THIS frame, as saved by the NEXT frame, i.e. the frame that
1859 // this frame called. e.g.
1862 // bar () { foo (); }
1863 // main () { bar (); }
1865 // stopped in foo() so
1869 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc value for frame 1, we need to ask
1870 // where frame 0 (the "next" frame) saved that and retrieve the value.
1873 RegisterContextLLDB::ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, addr_t &value)
1878 uint32_t lldb_regnum;
1879 if (register_kind == eRegisterKindLLDB)
1881 lldb_regnum = regnum;
1883 else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindLLDB, lldb_regnum))
1888 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
1889 RegisterValue reg_value;
1890 // if this is frame 0 (currently executing frame), get the requested reg contents from the actual thread registers
1893 if (m_thread.GetRegisterContext()->ReadRegister (reg_info, reg_value))
1895 value = reg_value.GetAsUInt64();
1901 bool pc_register = false;
1902 uint32_t generic_regnum;
1903 if (register_kind == eRegisterKindGeneric
1904 && (regnum == LLDB_REGNUM_GENERIC_PC || regnum == LLDB_REGNUM_GENERIC_RA))
1908 else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindGeneric, generic_regnum)
1909 && (generic_regnum == LLDB_REGNUM_GENERIC_PC || generic_regnum == LLDB_REGNUM_GENERIC_RA))
1914 lldb_private::UnwindLLDB::RegisterLocation regloc;
1915 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, pc_register))
1919 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value))
1921 value = reg_value.GetAsUInt64();
1928 RegisterContextLLDB::ReadGPRValue (const RegisterNumber ®num, addr_t &value)
1930 return ReadGPRValue (regnum.GetRegisterKind(), regnum.GetRegisterNumber(), value);
1933 // Find the value of a register in THIS frame
1936 RegisterContextLLDB::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value)
1941 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1942 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum);
1944 // If this is the 0th frame, hand this over to the live register context
1947 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum);
1948 return m_thread.GetRegisterContext()->ReadRegister (reg_info, value);
1951 bool is_pc_regnum = false;
1952 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_PC
1953 || reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA)
1955 is_pc_regnum = true;
1958 lldb_private::UnwindLLDB::RegisterLocation regloc;
1959 // Find out where the NEXT frame saved THIS frame's register contents
1960 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
1963 return ReadRegisterValueFromRegisterLocation (regloc, reg_info, value);
1967 RegisterContextLLDB::WriteRegister (const RegisterInfo *reg_info, const RegisterValue &value)
1972 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1973 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum);
1975 // If this is the 0th frame, hand this over to the live register context
1978 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum);
1979 return m_thread.GetRegisterContext()->WriteRegister (reg_info, value);
1982 lldb_private::UnwindLLDB::RegisterLocation regloc;
1983 // Find out where the NEXT frame saved THIS frame's register contents
1984 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false))
1987 return WriteRegisterValueToRegisterLocation (regloc, reg_info, value);
1990 // Don't need to implement this one
1992 RegisterContextLLDB::ReadAllRegisterValues (lldb::DataBufferSP &data_sp)
1997 // Don't need to implement this one
1999 RegisterContextLLDB::WriteAllRegisterValues (const lldb::DataBufferSP& data_sp)
2004 // Retrieve the pc value for THIS from
2007 RegisterContextLLDB::GetCFA (addr_t& cfa)
2013 if (m_cfa == LLDB_INVALID_ADDRESS)
2022 RegisterContextLLDB::SharedPtr
2023 RegisterContextLLDB::GetNextFrame () const
2025 RegisterContextLLDB::SharedPtr regctx;
2026 if (m_frame_number == 0)
2028 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number - 1);
2031 RegisterContextLLDB::SharedPtr
2032 RegisterContextLLDB::GetPrevFrame () const
2034 RegisterContextLLDB::SharedPtr regctx;
2035 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number + 1);
2038 // Retrieve the address of the start of the function of THIS frame
2041 RegisterContextLLDB::GetStartPC (addr_t& start_pc)
2046 if (!m_start_pc.IsValid())
2048 return ReadPC (start_pc);
2050 start_pc = m_start_pc.GetLoadAddress (CalculateTarget().get());
2054 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2057 RegisterContextLLDB::ReadPC (addr_t& pc)
2062 bool above_trap_handler = false;
2063 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsTrapHandlerFrame())
2064 above_trap_handler = true;
2066 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc))
2068 // A pc value of 0 or 1 is impossible in the middle of the stack -- it indicates the end of a stack walk.
2069 // On the currently executing frame (or such a frame interrupted asynchronously by sigtramp et al) this may
2070 // occur if code has jumped through a NULL pointer -- we want to be able to unwind past that frame to help
2073 if (m_all_registers_available == false
2074 && above_trap_handler == false
2075 && (pc == 0 || pc == 1))
2092 RegisterContextLLDB::UnwindLogMsg (const char *fmt, ...)
2094 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
2098 va_start (args, fmt);
2101 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL)
2110 log->Printf ("%*sth%d/fr%u %s",
2111 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number,
2118 RegisterContextLLDB::UnwindLogMsgVerbose (const char *fmt, ...)
2120 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
2121 if (log && log->GetVerbose())
2124 va_start (args, fmt);
2127 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL)
2136 log->Printf ("%*sth%d/fr%u %s",
2137 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number,