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 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Address.h"
11 #include "lldb/Core/AddressRange.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/RegisterValue.h"
14 #include "lldb/Core/Value.h"
15 #include "lldb/Expression/DWARFExpression.h"
16 #include "lldb/Symbol/ArmUnwindInfo.h"
17 #include "lldb/Symbol/DWARFCallFrameInfo.h"
18 #include "lldb/Symbol/FuncUnwinders.h"
19 #include "lldb/Symbol/Function.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/Symbol.h"
22 #include "lldb/Symbol/SymbolContext.h"
23 #include "lldb/Target/ABI.h"
24 #include "lldb/Target/DynamicLoader.h"
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/Target/Platform.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/SectionLoadList.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Utility/DataBufferHeap.h"
33 #include "lldb/Utility/Log.h"
34 #include "lldb/lldb-private.h"
36 #include "RegisterContextLLDB.h"
39 using namespace lldb_private;
41 static ConstString GetSymbolOrFunctionName(const SymbolContext &sym_ctx) {
43 return sym_ctx.symbol->GetName();
44 else if (sym_ctx.function)
45 return sym_ctx.function->GetName();
49 RegisterContextLLDB::RegisterContextLLDB(Thread &thread,
50 const SharedPtr &next_frame,
51 SymbolContext &sym_ctx,
52 uint32_t frame_number,
53 UnwindLLDB &unwind_lldb)
54 : RegisterContext(thread, frame_number), m_thread(thread),
55 m_fast_unwind_plan_sp(), m_full_unwind_plan_sp(),
56 m_fallback_unwind_plan_sp(), m_all_registers_available(false),
57 m_frame_type(-1), m_cfa(LLDB_INVALID_ADDRESS), m_start_pc(),
58 m_current_pc(), m_current_offset(0), m_current_offset_backed_up_one(0),
59 m_sym_ctx(sym_ctx), m_sym_ctx_valid(false), m_frame_number(frame_number),
60 m_registers(), m_parent_unwind(unwind_lldb) {
61 m_sym_ctx.Clear(false);
62 m_sym_ctx_valid = false;
65 InitializeZerothFrame();
67 InitializeNonZerothFrame();
70 // This same code exists over in the GetFullUnwindPlanForFrame() but it may
71 // not have been executed yet
72 if (IsFrameZero() || next_frame->m_frame_type == eTrapHandlerFrame ||
73 next_frame->m_frame_type == eDebuggerFrame) {
74 m_all_registers_available = true;
78 bool RegisterContextLLDB::IsUnwindPlanValidForCurrentPC(
79 lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset) {
83 // check if m_current_pc is valid
84 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
85 // yes - current offset can be used as is
86 valid_pc_offset = m_current_offset;
90 // if m_current_offset <= 0, we've got nothing else to try
91 if (m_current_offset <= 0)
94 // check pc - 1 to see if it's valid
95 Address pc_minus_one(m_current_pc);
96 pc_minus_one.SetOffset(m_current_pc.GetOffset() - 1);
97 if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) {
98 // *valid_pc_offset = m_current_offset - 1;
99 valid_pc_offset = m_current_pc.GetOffset() - 1;
106 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the
107 // zeroth frame or currently
110 void RegisterContextLLDB::InitializeZerothFrame() {
111 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
112 ExecutionContext exe_ctx(m_thread.shared_from_this());
113 RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
115 if (reg_ctx_sp.get() == NULL) {
116 m_frame_type = eNotAValidFrame;
117 UnwindLogMsg("frame does not have a register context");
121 addr_t current_pc = reg_ctx_sp->GetPC();
123 if (current_pc == LLDB_INVALID_ADDRESS) {
124 m_frame_type = eNotAValidFrame;
125 UnwindLogMsg("frame does not have a pc");
129 Process *process = exe_ctx.GetProcessPtr();
131 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
132 // this will strip bit zero in case we read a PC from memory or from the LR.
133 // (which would be a no-op in frame 0 where we get it from the register set,
134 // but still a good idea to make the call here for other ABIs that may exist.)
135 ABI *abi = process->GetABI().get();
137 current_pc = abi->FixCodeAddress(current_pc);
139 // Initialize m_current_pc, an Address object, based on current_pc, an addr_t.
140 m_current_pc.SetLoadAddress(current_pc, &process->GetTarget());
142 // If we don't have a Module for some reason, we're not going to find
143 // symbol/function information - just
144 // stick in some reasonable defaults and hope we can unwind past this frame.
145 ModuleSP pc_module_sp(m_current_pc.GetModule());
146 if (!m_current_pc.IsValid() || !pc_module_sp) {
147 UnwindLogMsg("using architectural default unwind method");
150 // We require either a symbol or function in the symbols context to be
152 // filled in or this context is of no use to us.
153 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
154 if (pc_module_sp.get() && (pc_module_sp->ResolveSymbolContextForAddress(
155 m_current_pc, resolve_scope, m_sym_ctx) &
157 m_sym_ctx_valid = true;
160 if (m_sym_ctx.symbol) {
161 UnwindLogMsg("with pc value of 0x%" PRIx64 ", symbol name is '%s'",
162 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
163 } else if (m_sym_ctx.function) {
164 UnwindLogMsg("with pc value of 0x%" PRIx64 ", function name is '%s'",
165 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
167 UnwindLogMsg("with pc value of 0x%" PRIx64
168 ", no symbol/function name is known.",
172 AddressRange addr_range;
173 m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range);
175 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
176 m_frame_type = eTrapHandlerFrame;
178 // FIXME: Detect eDebuggerFrame here.
179 m_frame_type = eNormalFrame;
182 // If we were able to find a symbol/function, set addr_range to the bounds of
183 // that symbol/function.
184 // else treat the current pc value as the start_pc and record no offset.
185 if (addr_range.GetBaseAddress().IsValid()) {
186 m_start_pc = addr_range.GetBaseAddress();
187 if (m_current_pc.GetSection() == m_start_pc.GetSection()) {
188 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
189 } else if (m_current_pc.GetModule() == m_start_pc.GetModule()) {
190 // This means that whatever symbol we kicked up isn't really correct
191 // --- we should not cross section boundaries ... We really should NULL
193 // the function/symbol in this case unless there is a bad assumption
194 // here 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;
212 if (m_full_unwind_plan_sp &&
213 m_full_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
215 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
216 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
217 if (active_row.get() && log) {
218 StreamString active_row_strm;
219 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
220 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
221 UnwindLogMsg("%s", active_row_strm.GetData());
225 if (!active_row.get()) {
226 UnwindLogMsg("could not find an unwindplan row for this frame's pc");
227 m_frame_type = eNotAValidFrame;
231 if (!ReadCFAValueForRow(row_register_kind, active_row, m_cfa)) {
232 // Try the fall back unwind plan since the
233 // full unwind plan failed.
234 FuncUnwindersSP func_unwinders_sp;
235 UnwindPlanSP call_site_unwind_plan;
236 bool cfa_status = false;
238 if (m_sym_ctx_valid) {
240 pc_module_sp->GetObjectFile()
242 .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx);
245 if (func_unwinders_sp.get() != nullptr)
246 call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(
247 process->GetTarget(), m_current_offset_backed_up_one);
249 if (call_site_unwind_plan.get() != nullptr) {
250 m_fallback_unwind_plan_sp = call_site_unwind_plan;
251 if (TryFallbackUnwindPlan())
255 UnwindLogMsg("could not read CFA value for first frame.");
256 m_frame_type = eNotAValidFrame;
261 UnwindLogMsg("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64
262 " using %s UnwindPlan",
263 (uint64_t)m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()),
265 m_full_unwind_plan_sp->GetSourceName().GetCString());
268 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the
269 // RegisterContextLLDB "below" it
270 // to provide things like its current pc value.
272 void RegisterContextLLDB::InitializeNonZerothFrame() {
273 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
275 m_frame_type = eNotAValidFrame;
276 UnwindLogMsg("non-zeroth frame tests positive for IsFrameZero -- that "
277 "shouldn't happen.");
281 if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) {
282 m_frame_type = eNotAValidFrame;
283 UnwindLogMsg("Could not get next frame, marking this frame as invalid.");
286 if (!m_thread.GetRegisterContext()) {
287 m_frame_type = eNotAValidFrame;
288 UnwindLogMsg("Could not get register context for this thread, marking this "
289 "frame as invalid.");
294 if (!ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) {
295 UnwindLogMsg("could not get pc value");
296 m_frame_type = eNotAValidFrame;
300 ExecutionContext exe_ctx(m_thread.shared_from_this());
301 Process *process = exe_ctx.GetProcessPtr();
302 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
303 // this will strip bit zero in case we read a PC from memory or from the LR.
304 ABI *abi = process->GetABI().get();
306 pc = abi->FixCodeAddress(pc);
309 UnwindLogMsg("pc = 0x%" PRIx64, pc);
311 if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val))
312 UnwindLogMsg("fp = 0x%" PRIx64, reg_val);
313 if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val))
314 UnwindLogMsg("sp = 0x%" PRIx64, reg_val);
317 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap
319 bool above_trap_handler = false;
320 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
321 GetNextFrame()->IsTrapHandlerFrame())
322 above_trap_handler = true;
324 if (pc == 0 || pc == 0x1) {
325 if (above_trap_handler == false) {
326 m_frame_type = eNotAValidFrame;
327 UnwindLogMsg("this frame has a pc of 0x0");
332 const bool allow_section_end = true;
333 m_current_pc.SetLoadAddress(pc, &process->GetTarget(), allow_section_end);
335 // If we don't have a Module for some reason, we're not going to find
336 // symbol/function information - just
337 // stick in some reasonable defaults and hope we can unwind past this frame.
338 ModuleSP pc_module_sp(m_current_pc.GetModule());
339 if (!m_current_pc.IsValid() || !pc_module_sp) {
340 UnwindLogMsg("using architectural default unwind method");
342 // Test the pc value to see if we know it's in an unmapped/non-executable
344 uint32_t permissions;
345 if (process->GetLoadAddressPermissions(pc, permissions) &&
346 (permissions & ePermissionsExecutable) == 0) {
347 // If this is the second frame off the stack, we may have unwound the
349 // incorrectly. But using the architecture default unwind plan may get us
351 // track -- albeit possibly skipping a real frame. Give this frame a
353 // pc and see if we can get any further.
354 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
355 GetNextFrame()->IsFrameZero()) {
356 UnwindLogMsg("had a pc of 0x%" PRIx64 " which is not in executable "
357 "memory but on frame 1 -- "
360 m_frame_type = eSkipFrame;
362 // anywhere other than the second frame, a non-executable pc means we're
363 // off in the weeds -- stop now.
364 m_frame_type = eNotAValidFrame;
365 UnwindLogMsg("pc is in a non-executable section of memory and this "
366 "isn't the 2nd frame in the stack walk.");
372 m_fast_unwind_plan_sp.reset();
373 m_full_unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
374 abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp);
375 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
377 m_frame_type = eNormalFrame;
379 m_all_registers_available = false;
380 m_current_offset = -1;
381 m_current_offset_backed_up_one = -1;
382 RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
383 UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0);
385 if (!ReadCFAValueForRow(row_register_kind, row, m_cfa)) {
386 UnwindLogMsg("failed to get cfa value");
387 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
389 m_frame_type = eNotAValidFrame;
394 // A couple of sanity checks..
395 if (m_cfa == LLDB_INVALID_ADDRESS || m_cfa == 0 || m_cfa == 1) {
396 UnwindLogMsg("could not find a valid cfa address");
397 m_frame_type = eNotAValidFrame;
401 // m_cfa should point into the stack memory; if we can query memory
402 // region permissions,
403 // see if the memory is allocated & readable.
404 if (process->GetLoadAddressPermissions(m_cfa, permissions) &&
405 (permissions & ePermissionsReadable) == 0) {
406 m_frame_type = eNotAValidFrame;
408 "the CFA points to a region of memory that is not readable");
412 UnwindLogMsg("could not find a row for function offset zero");
413 m_frame_type = eNotAValidFrame;
417 if (CheckIfLoopingStack()) {
418 TryFallbackUnwindPlan();
419 if (CheckIfLoopingStack()) {
420 UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
421 "looping - stopping");
422 m_frame_type = eNotAValidFrame;
427 UnwindLogMsg("initialized frame cfa is 0x%" PRIx64, (uint64_t)m_cfa);
430 m_frame_type = eNotAValidFrame;
431 UnwindLogMsg("could not find any symbol for this pc, or a default unwind "
432 "plan, to continue unwind.");
436 bool resolve_tail_call_address = false; // m_current_pc can be one past the
437 // address range of the function...
438 // If the saved pc does not point to a function/symbol because it is
439 // beyond the bounds of the correct function and there's no symbol there,
440 // we do *not* want ResolveSymbolContextForAddress to back up the pc by 1,
441 // because then we might not find the correct unwind information later.
442 // Instead, let ResolveSymbolContextForAddress fail, and handle the case
443 // via decr_pc_and_recompute_addr_range below.
444 const uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
445 uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress(
446 m_current_pc, resolve_scope, m_sym_ctx, resolve_tail_call_address);
448 // We require either a symbol or function in the symbols context to be
450 // filled in or this context is of no use to us.
451 if (resolve_scope & resolved_scope) {
452 m_sym_ctx_valid = true;
455 if (m_sym_ctx.symbol) {
456 UnwindLogMsg("with pc value of 0x%" PRIx64 ", symbol name is '%s'", pc,
457 GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
458 } else if (m_sym_ctx.function) {
459 UnwindLogMsg("with pc value of 0x%" PRIx64 ", function name is '%s'", pc,
460 GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
462 UnwindLogMsg("with pc value of 0x%" PRIx64
463 ", no symbol/function name is known.",
467 AddressRange addr_range;
468 if (!m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range)) {
469 m_sym_ctx_valid = false;
472 bool decr_pc_and_recompute_addr_range = false;
474 // If the symbol lookup failed...
475 if (m_sym_ctx_valid == false)
476 decr_pc_and_recompute_addr_range = true;
478 // Or if we're in the middle of the stack (and not "above" an asynchronous
479 // event like sigtramp),
480 // and our "current" pc is the start of a function...
481 if (GetNextFrame()->m_frame_type != eTrapHandlerFrame &&
482 GetNextFrame()->m_frame_type != eDebuggerFrame &&
484 (addr_range.GetBaseAddress().IsValid() &&
485 addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection() &&
486 addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset()))) {
487 decr_pc_and_recompute_addr_range = true;
490 // We need to back up the pc by 1 byte and re-search for the Symbol to handle
491 // the case where the "saved pc"
492 // value is pointing to the next function, e.g. if a function ends with a CALL
494 // FIXME this may need to be an architectural-dependent behavior; if so we'll
495 // need to add a member function
496 // to the ABI plugin and consult that.
497 if (decr_pc_and_recompute_addr_range) {
498 UnwindLogMsg("Backing up the pc value of 0x%" PRIx64
499 " by 1 and re-doing symbol lookup; old symbol was %s",
500 pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
501 Address temporary_pc;
502 temporary_pc.SetLoadAddress(pc - 1, &process->GetTarget());
503 m_sym_ctx.Clear(false);
504 m_sym_ctx_valid = false;
505 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
507 ModuleSP temporary_module_sp = temporary_pc.GetModule();
508 if (temporary_module_sp &&
509 temporary_module_sp->ResolveSymbolContextForAddress(
510 temporary_pc, resolve_scope, m_sym_ctx) &
512 if (m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range))
513 m_sym_ctx_valid = true;
515 UnwindLogMsg("Symbol is now %s",
516 GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
519 // If we were able to find a symbol/function, set addr_range_ptr to the bounds
520 // of that symbol/function.
521 // else treat the current pc value as the start_pc and record no offset.
522 if (addr_range.GetBaseAddress().IsValid()) {
523 m_start_pc = addr_range.GetBaseAddress();
524 m_current_offset = pc - m_start_pc.GetLoadAddress(&process->GetTarget());
525 m_current_offset_backed_up_one = m_current_offset;
526 if (decr_pc_and_recompute_addr_range &&
527 m_current_offset_backed_up_one > 0) {
528 m_current_offset_backed_up_one--;
529 if (m_sym_ctx_valid) {
530 m_current_pc.SetLoadAddress(pc - 1, &process->GetTarget());
534 m_start_pc = m_current_pc;
535 m_current_offset = -1;
536 m_current_offset_backed_up_one = -1;
539 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
540 m_frame_type = eTrapHandlerFrame;
542 // FIXME: Detect eDebuggerFrame here.
543 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
545 m_frame_type = eNormalFrame;
549 // We've set m_frame_type and m_sym_ctx before this call.
550 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame();
552 UnwindPlan::RowSP active_row;
553 RegisterKind row_register_kind = eRegisterKindGeneric;
555 // Try to get by with just the fast UnwindPlan if possible - the full
556 // UnwindPlan may be expensive to get
557 // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for
560 if (m_fast_unwind_plan_sp &&
561 m_fast_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
563 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
564 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind();
565 if (active_row.get() && log) {
566 StreamString active_row_strm;
567 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread,
568 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
569 UnwindLogMsg("active row: %s", active_row_strm.GetData());
572 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
573 int valid_offset = -1;
574 if (IsUnwindPlanValidForCurrentPC(m_full_unwind_plan_sp, valid_offset)) {
575 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset(valid_offset);
576 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
577 if (active_row.get() && log) {
578 StreamString active_row_strm;
579 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
581 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
582 UnwindLogMsg("active row: %s", active_row_strm.GetData());
587 if (!active_row.get()) {
588 m_frame_type = eNotAValidFrame;
589 UnwindLogMsg("could not find unwind row for this pc");
593 if (!ReadCFAValueForRow(row_register_kind, active_row, m_cfa)) {
594 UnwindLogMsg("failed to get cfa");
595 m_frame_type = eNotAValidFrame;
599 UnwindLogMsg("m_cfa = 0x%" PRIx64, m_cfa);
601 if (CheckIfLoopingStack()) {
602 TryFallbackUnwindPlan();
603 if (CheckIfLoopingStack()) {
604 UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
605 "looping - stopping");
606 m_frame_type = eNotAValidFrame;
611 UnwindLogMsg("initialized frame current pc is 0x%" PRIx64
612 " cfa is 0x%" PRIx64,
613 (uint64_t)m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()),
617 bool RegisterContextLLDB::CheckIfLoopingStack() {
618 // If we have a bad stack setup, we can get the same CFA value multiple times
620 // more devious, we can actually oscillate between two CFA values. Detect that
622 // break out to avoid a possible infinite loop in lldb trying to unwind the
624 // To detect when we have the same CFA value multiple times, we compare the
625 // CFA of the current
626 // frame with the 2nd next frame because in some specail case (e.g. signal
628 // written assembly without ABI compiance) we can have 2 frames with the same
630 // can have arbitrary number of frames with the same CFA, but more then 2 is
631 // very very unlikely)
633 RegisterContextLLDB::SharedPtr next_frame = GetNextFrame();
635 RegisterContextLLDB::SharedPtr next_next_frame = next_frame->GetNextFrame();
636 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
637 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) {
638 if (next_next_frame_cfa == m_cfa) {
639 // We have a loop in the stack unwind
647 bool RegisterContextLLDB::IsFrameZero() const { return m_frame_number == 0; }
649 // Find a fast unwind plan for this frame, if possible.
651 // On entry to this method,
653 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
654 // if either of those are correct,
655 // 2. m_sym_ctx should already be filled in, and
656 // 3. m_current_pc should have the current pc value for this frame
657 // 4. m_current_offset_backed_up_one should have the current byte offset into
658 // the function, maybe backed up by 1, -1 if unknown
660 UnwindPlanSP RegisterContextLLDB::GetFastUnwindPlanForFrame() {
661 UnwindPlanSP unwind_plan_sp;
662 ModuleSP pc_module_sp(m_current_pc.GetModule());
664 if (!m_current_pc.IsValid() || !pc_module_sp ||
665 pc_module_sp->GetObjectFile() == NULL)
666 return unwind_plan_sp;
669 return unwind_plan_sp;
671 FuncUnwindersSP func_unwinders_sp(
672 pc_module_sp->GetObjectFile()
674 .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx));
675 if (!func_unwinders_sp)
676 return unwind_plan_sp;
678 // If we're in _sigtramp(), unwinding past this frame requires special
680 if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame)
681 return unwind_plan_sp;
683 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind(
684 *m_thread.CalculateTarget(), m_thread);
685 if (unwind_plan_sp) {
686 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
687 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
688 if (log && log->GetVerbose()) {
689 if (m_fast_unwind_plan_sp)
690 UnwindLogMsgVerbose("frame, and has a fast UnwindPlan");
692 UnwindLogMsgVerbose("frame");
694 m_frame_type = eNormalFrame;
695 return unwind_plan_sp;
697 unwind_plan_sp.reset();
700 return unwind_plan_sp;
703 // On entry to this method,
705 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
706 // if either of those are correct,
707 // 2. m_sym_ctx should already be filled in, and
708 // 3. m_current_pc should have the current pc value for this frame
709 // 4. m_current_offset_backed_up_one should have the current byte offset into
710 // the function, maybe backed up by 1, -1 if unknown
712 UnwindPlanSP RegisterContextLLDB::GetFullUnwindPlanForFrame() {
713 UnwindPlanSP unwind_plan_sp;
714 UnwindPlanSP arch_default_unwind_plan_sp;
715 ExecutionContext exe_ctx(m_thread.shared_from_this());
716 Process *process = exe_ctx.GetProcessPtr();
717 ABI *abi = process ? process->GetABI().get() : NULL;
719 arch_default_unwind_plan_sp.reset(
720 new UnwindPlan(lldb::eRegisterKindGeneric));
721 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
724 "unable to get architectural default UnwindPlan from ABI plugin");
727 bool behaves_like_zeroth_frame = false;
728 if (IsFrameZero() || GetNextFrame()->m_frame_type == eTrapHandlerFrame ||
729 GetNextFrame()->m_frame_type == eDebuggerFrame) {
730 behaves_like_zeroth_frame = true;
731 // If this frame behaves like a 0th frame (currently executing or
732 // interrupted asynchronously), all registers can be retrieved.
733 m_all_registers_available = true;
736 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
738 // in the zeroth frame, we need to use the "unwind at first instruction" arch
739 // default UnwindPlan
740 // Also, if this Process can report on memory region attributes, any
741 // non-executable region means
742 // we jumped through a bad function pointer - handle the same way as 0x0.
743 // Note, if we have a symbol context & a symbol, we don't want to follow this
744 // code path. This is
745 // for jumping to memory regions without any information available.
747 if ((!m_sym_ctx_valid ||
748 (m_sym_ctx.function == NULL && m_sym_ctx.symbol == NULL)) &&
749 behaves_like_zeroth_frame && m_current_pc.IsValid()) {
750 uint32_t permissions;
751 addr_t current_pc_addr =
752 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr());
753 if (current_pc_addr == 0 ||
755 process->GetLoadAddressPermissions(current_pc_addr, permissions) &&
756 (permissions & ePermissionsExecutable) == 0)) {
758 unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
759 abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp);
760 m_frame_type = eNormalFrame;
761 return unwind_plan_sp;
766 // No Module for the current pc, try using the architecture default unwind.
767 ModuleSP pc_module_sp(m_current_pc.GetModule());
768 if (!m_current_pc.IsValid() || !pc_module_sp ||
769 pc_module_sp->GetObjectFile() == NULL) {
770 m_frame_type = eNormalFrame;
771 return arch_default_unwind_plan_sp;
774 FuncUnwindersSP func_unwinders_sp;
775 if (m_sym_ctx_valid) {
777 pc_module_sp->GetObjectFile()
779 .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx);
782 // No FuncUnwinders available for this pc (stripped function symbols, lldb
783 // could not augment its
784 // function table with another source, like LC_FUNCTION_STARTS or eh_frame in
786 // See if eh_frame or the .ARM.exidx tables have unwind information for this
787 // address, else fall
788 // back to the architectural default unwind.
789 if (!func_unwinders_sp) {
790 m_frame_type = eNormalFrame;
792 if (!pc_module_sp || !pc_module_sp->GetObjectFile() ||
793 !m_current_pc.IsValid())
794 return arch_default_unwind_plan_sp;
796 // Even with -fomit-frame-pointer, we can try eh_frame to get back on track.
797 DWARFCallFrameInfo *eh_frame =
798 pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo();
800 unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
801 if (eh_frame->GetUnwindPlan(m_current_pc, *unwind_plan_sp))
802 return unwind_plan_sp;
804 unwind_plan_sp.reset();
807 ArmUnwindInfo *arm_exidx =
808 pc_module_sp->GetObjectFile()->GetUnwindTable().GetArmUnwindInfo();
810 unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
811 if (arm_exidx->GetUnwindPlan(exe_ctx.GetTargetRef(), m_current_pc,
813 return unwind_plan_sp;
815 unwind_plan_sp.reset();
818 return arch_default_unwind_plan_sp;
821 // If we're in _sigtramp(), unwinding past this frame requires special
822 // knowledge. On Mac OS X this knowledge
823 // is properly encoded in the eh_frame section, so prefer that if available.
824 // On other platforms we may need to provide a platform-specific UnwindPlan
825 // which encodes the details of
826 // how to unwind out of sigtramp.
827 if (m_frame_type == eTrapHandlerFrame && process) {
828 m_fast_unwind_plan_sp.reset();
829 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan(
830 process->GetTarget(), m_current_offset_backed_up_one);
831 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc) &&
832 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) {
833 return unwind_plan_sp;
837 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
838 // even when it's frame zero
839 // This comes up if we have hand-written functions in a Module and
840 // hand-written eh_frame. The assembly
841 // instruction inspection may fail and the eh_frame CFI were probably written
842 // with some care to do the
843 // right thing. It'd be nice if there was a way to ask the eh_frame directly
844 // if it is asynchronous
845 // (can be trusted at every instruction point) or synchronous (the normal case
846 // - only at call sites).
848 if (process && process->GetDynamicLoader() &&
849 process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo(m_sym_ctx)) {
850 // We must specifically call the GetEHFrameUnwindPlan() method here --
852 // call GetUnwindPlanAtCallSite() -- because CallSite may return an unwind
854 // either eh_frame (that's what we intend) or compact unwind (this won't
856 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan(
857 process->GetTarget(), m_current_offset_backed_up_one);
858 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
859 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because the "
860 "DynamicLoader suggested we prefer it",
861 unwind_plan_sp->GetSourceName().GetCString());
862 return unwind_plan_sp;
866 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
867 // the assembly language instructions
868 if (behaves_like_zeroth_frame && process) {
869 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
870 process->GetTarget(), m_thread, m_current_offset_backed_up_one);
871 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
872 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
873 // We probably have an UnwindPlan created by inspecting assembly
875 // assembly profilers work really well with compiler-generated functions
877 // written assembly can be problematic. We set the eh_frame based unwind
879 // fallback unwind plan if instruction emulation doesn't work out even
881 // sites if it is available and use the architecture default unwind plan
883 // not available. The eh_frame unwind plan is more reliable even on non
885 // then the architecture default plan and for hand written assembly code
887 // written in a way that it valid at all location what helps in the most
889 // cases when the instruction emulation fails.
890 UnwindPlanSP call_site_unwind_plan =
891 func_unwinders_sp->GetUnwindPlanAtCallSite(
892 process->GetTarget(), m_current_offset_backed_up_one);
893 if (call_site_unwind_plan &&
894 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
895 call_site_unwind_plan->GetSourceName() !=
896 unwind_plan_sp->GetSourceName()) {
897 m_fallback_unwind_plan_sp = call_site_unwind_plan;
899 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
902 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
903 unwind_plan_sp->GetSourceName().GetCString());
904 return unwind_plan_sp;
908 // Typically this is unwind info from an eh_frame section intended for
909 // exception handling; only valid at call sites
911 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite(
912 process->GetTarget(), m_current_offset_backed_up_one);
914 int valid_offset = -1;
915 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) {
916 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
917 unwind_plan_sp->GetSourceName().GetCString());
918 return unwind_plan_sp;
921 // We'd prefer to use an UnwindPlan intended for call sites when we're at a
922 // call site but if we've
923 // struck out on that, fall back to using the non-call-site assembly
924 // inspection UnwindPlan if possible.
926 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
927 process->GetTarget(), m_thread, m_current_offset_backed_up_one);
929 if (unwind_plan_sp &&
930 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
931 // We probably have an UnwindPlan created by inspecting assembly
932 // instructions. The assembly
933 // profilers work really well with compiler-generated functions but hand-
935 // can be problematic. We set the eh_frame based unwind plan as our fallback
937 // instruction emulation doesn't work out even for non call sites if it is
939 // the architecture default unwind plan if it is not available. The eh_frame
941 // more reliable even on non call sites then the architecture default plan
943 // written assembly code it is often written in a way that it valid at all
945 // helps in the most common cases when the instruction emulation fails.
946 UnwindPlanSP call_site_unwind_plan =
947 func_unwinders_sp->GetUnwindPlanAtCallSite(
948 process->GetTarget(), m_current_offset_backed_up_one);
949 if (call_site_unwind_plan &&
950 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
951 call_site_unwind_plan->GetSourceName() !=
952 unwind_plan_sp->GetSourceName()) {
953 m_fallback_unwind_plan_sp = call_site_unwind_plan;
955 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
959 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) {
960 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
961 unwind_plan_sp->GetSourceName().GetCString());
962 return unwind_plan_sp;
965 // If we're on the first instruction of a function, and we have an
966 // architectural default UnwindPlan
967 // for the initial instruction of a function, use that.
968 if (m_current_offset_backed_up_one == 0) {
970 func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
972 if (unwind_plan_sp) {
973 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
974 unwind_plan_sp->GetSourceName().GetCString());
975 return unwind_plan_sp;
979 // If nothing else, use the architectural default UnwindPlan and hope that
981 if (arch_default_unwind_plan_sp)
983 "frame uses %s for full UnwindPlan",
984 arch_default_unwind_plan_sp->GetSourceName().GetCString());
987 "Unable to find any UnwindPlan for full unwind of this frame.");
989 return arch_default_unwind_plan_sp;
992 void RegisterContextLLDB::InvalidateAllRegisters() {
993 m_frame_type = eNotAValidFrame;
996 size_t RegisterContextLLDB::GetRegisterCount() {
997 return m_thread.GetRegisterContext()->GetRegisterCount();
1000 const RegisterInfo *RegisterContextLLDB::GetRegisterInfoAtIndex(size_t reg) {
1001 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex(reg);
1004 size_t RegisterContextLLDB::GetRegisterSetCount() {
1005 return m_thread.GetRegisterContext()->GetRegisterSetCount();
1008 const RegisterSet *RegisterContextLLDB::GetRegisterSet(size_t reg_set) {
1009 return m_thread.GetRegisterContext()->GetRegisterSet(reg_set);
1012 uint32_t RegisterContextLLDB::ConvertRegisterKindToRegisterNumber(
1013 lldb::RegisterKind kind, uint32_t num) {
1014 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
1018 bool RegisterContextLLDB::ReadRegisterValueFromRegisterLocation(
1019 lldb_private::UnwindLLDB::RegisterLocation regloc,
1020 const RegisterInfo *reg_info, RegisterValue &value) {
1023 bool success = false;
1025 switch (regloc.type) {
1026 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: {
1027 const RegisterInfo *other_reg_info =
1028 GetRegisterInfoAtIndex(regloc.location.register_number);
1030 if (!other_reg_info)
1034 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1036 case UnwindLLDB::RegisterLocation::eRegisterInRegister: {
1037 const RegisterInfo *other_reg_info =
1038 GetRegisterInfoAtIndex(regloc.location.register_number);
1040 if (!other_reg_info)
1043 if (IsFrameZero()) {
1045 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1047 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1050 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1052 value.SetUInt(regloc.location.inferred_value, reg_info->byte_size);
1055 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1057 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1058 llvm_unreachable("FIXME debugger inferior function call unwind");
1059 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: {
1060 Status error(ReadRegisterValueFromMemory(
1061 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1063 success = error.Success();
1066 llvm_unreachable("Unknown RegisterLocation type.");
1071 bool RegisterContextLLDB::WriteRegisterValueToRegisterLocation(
1072 lldb_private::UnwindLLDB::RegisterLocation regloc,
1073 const RegisterInfo *reg_info, const RegisterValue &value) {
1077 bool success = false;
1079 switch (regloc.type) {
1080 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: {
1081 const RegisterInfo *other_reg_info =
1082 GetRegisterInfoAtIndex(regloc.location.register_number);
1084 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1086 case UnwindLLDB::RegisterLocation::eRegisterInRegister: {
1087 const RegisterInfo *other_reg_info =
1088 GetRegisterInfoAtIndex(regloc.location.register_number);
1089 if (IsFrameZero()) {
1091 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1093 success = GetNextFrame()->WriteRegister(other_reg_info, value);
1096 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1097 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1099 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1100 llvm_unreachable("FIXME debugger inferior function call unwind");
1101 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: {
1102 Status error(WriteRegisterValueToMemory(
1103 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1105 success = error.Success();
1108 llvm_unreachable("Unknown RegisterLocation type.");
1113 bool RegisterContextLLDB::IsValid() const {
1114 return m_frame_type != eNotAValidFrame;
1117 // After the final stack frame in a stack walk we'll get one invalid
1118 // (eNotAValidFrame) stack frame --
1119 // one past the end of the stack walk. But higher-level code will need to tell
1120 // the differnece between
1121 // "the unwind plan below this frame failed" versus "we successfully completed
1122 // the stack walk" so
1123 // this method helps to disambiguate that.
1125 bool RegisterContextLLDB::IsTrapHandlerFrame() const {
1126 return m_frame_type == eTrapHandlerFrame;
1129 // A skip frame is a bogus frame on the stack -- but one where we're likely to
1130 // find a real frame farther
1131 // up the stack if we keep looking. It's always the second frame in an unwind
1132 // (i.e. the first frame after
1133 // frame zero) where unwinding can be the trickiest. Ideally we'll mark up this
1134 // frame in some way so the
1135 // user knows we're displaying bad data and we may have skipped one frame of
1136 // their real program in the
1137 // process of getting back on track.
1139 bool RegisterContextLLDB::IsSkipFrame() const {
1140 return m_frame_type == eSkipFrame;
1143 bool RegisterContextLLDB::IsTrapHandlerSymbol(
1144 lldb_private::Process *process,
1145 const lldb_private::SymbolContext &m_sym_ctx) const {
1146 PlatformSP platform_sp(process->GetTarget().GetPlatform());
1148 const std::vector<ConstString> trap_handler_names(
1149 platform_sp->GetTrapHandlerSymbolNames());
1150 for (ConstString name : trap_handler_names) {
1151 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1152 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1157 const std::vector<ConstString> user_specified_trap_handler_names(
1158 m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames());
1159 for (ConstString name : user_specified_trap_handler_names) {
1160 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1161 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1169 // Answer the question: Where did THIS frame save the CALLER frame ("previous"
1170 // frame)'s register value?
1172 enum UnwindLLDB::RegisterSearchResult
1173 RegisterContextLLDB::SavedLocationForRegister(
1174 uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc) {
1175 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1177 // Have we already found this register location?
1178 if (!m_registers.empty()) {
1180 lldb_private::UnwindLLDB::RegisterLocation>::const_iterator
1182 iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1183 if (iterator != m_registers.end()) {
1184 regloc = iterator->second;
1185 UnwindLogMsg("supplying caller's saved %s (%d)'s location, cached",
1186 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1187 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1191 // Look through the available UnwindPlans for the register location.
1193 UnwindPlan::Row::RegisterLocation unwindplan_regloc;
1194 bool have_unwindplan_regloc = false;
1195 RegisterKind unwindplan_registerkind = kNumRegisterKinds;
1197 if (m_fast_unwind_plan_sp) {
1198 UnwindPlan::RowSP active_row =
1199 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1200 unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind();
1201 if (regnum.GetAsKind(unwindplan_registerkind) == LLDB_INVALID_REGNUM) {
1202 UnwindLogMsg("could not convert lldb regnum %s (%d) into %d RegisterKind "
1203 "reg numbering scheme",
1204 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1205 (int)unwindplan_registerkind);
1206 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1208 if (active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
1209 unwindplan_regloc)) {
1211 "supplying caller's saved %s (%d)'s location using FastUnwindPlan",
1212 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1213 have_unwindplan_regloc = true;
1217 if (!have_unwindplan_regloc) {
1218 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a
1219 // full UnwindPlan yet
1220 if (!m_full_unwind_plan_sp)
1221 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
1223 if (m_full_unwind_plan_sp) {
1224 RegisterNumber pc_regnum(m_thread, eRegisterKindGeneric,
1225 LLDB_REGNUM_GENERIC_PC);
1227 UnwindPlan::RowSP active_row =
1228 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1229 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind();
1231 RegisterNumber return_address_reg;
1233 // If we're fetching the saved pc and this UnwindPlan defines a
1234 // ReturnAddress register (e.g. lr on arm),
1235 // look for the return address register number in the UnwindPlan's row.
1236 if (pc_regnum.IsValid() && pc_regnum == regnum &&
1237 m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1238 LLDB_INVALID_REGNUM) {
1240 return_address_reg.init(
1241 m_thread, m_full_unwind_plan_sp->GetRegisterKind(),
1242 m_full_unwind_plan_sp->GetReturnAddressRegister());
1243 regnum = return_address_reg;
1244 UnwindLogMsg("requested caller's saved PC but this UnwindPlan uses a "
1245 "RA reg; getting %s (%d) instead",
1246 return_address_reg.GetName(),
1247 return_address_reg.GetAsKind(eRegisterKindLLDB));
1249 if (regnum.GetAsKind(unwindplan_registerkind) == LLDB_INVALID_REGNUM) {
1250 if (unwindplan_registerkind == eRegisterKindGeneric) {
1251 UnwindLogMsg("could not convert lldb regnum %s (%d) into "
1252 "eRegisterKindGeneric reg numbering scheme",
1253 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1255 UnwindLogMsg("could not convert lldb regnum %s (%d) into %d "
1256 "RegisterKind reg numbering scheme",
1257 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1258 (int)unwindplan_registerkind);
1260 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1264 if (regnum.IsValid() &&
1265 active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
1266 unwindplan_regloc)) {
1267 have_unwindplan_regloc = true;
1269 "supplying caller's saved %s (%d)'s location using %s UnwindPlan",
1270 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1271 m_full_unwind_plan_sp->GetSourceName().GetCString());
1274 // This is frame 0 and we're retrieving the PC and it's saved in a Return
1275 // Address register and
1276 // it hasn't been saved anywhere yet -- that is, it's still live in the
1278 // Handle this specially.
1280 if (have_unwindplan_regloc == false && return_address_reg.IsValid() &&
1282 if (return_address_reg.GetAsKind(eRegisterKindLLDB) !=
1283 LLDB_INVALID_REGNUM) {
1284 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1286 UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1287 new_regloc.location.register_number =
1288 return_address_reg.GetAsKind(eRegisterKindLLDB);
1289 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1290 regloc = new_regloc;
1291 UnwindLogMsg("supplying caller's register %s (%d) from the live "
1292 "RegisterContext at frame 0, saved in %d",
1293 return_address_reg.GetName(),
1294 return_address_reg.GetAsKind(eRegisterKindLLDB),
1295 return_address_reg.GetAsKind(eRegisterKindLLDB));
1296 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1300 // If this architecture stores the return address in a register (it
1301 // defines a Return Address register)
1302 // and we're on a non-zero stack frame and the Full UnwindPlan says that
1303 // the pc is stored in the
1304 // RA registers (e.g. lr on arm), then we know that the full unwindplan is
1305 // not trustworthy -- this
1306 // is an impossible situation and the instruction emulation code has
1307 // likely been misled.
1308 // If this stack frame meets those criteria, we need to throw away the
1309 // Full UnwindPlan that the
1310 // instruction emulation came up with and fall back to the architecture's
1311 // Default UnwindPlan so
1312 // the stack walk can get past this point.
1314 // Special note: If the Full UnwindPlan was generated from the compiler,
1315 // don't second-guess it
1316 // when we're at a call site location.
1318 // arch_default_ra_regnum is the return address register # in the Full
1319 // UnwindPlan register numbering
1320 RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1321 LLDB_REGNUM_GENERIC_RA);
1323 if (arch_default_ra_regnum.GetAsKind(unwindplan_registerkind) !=
1324 LLDB_INVALID_REGNUM &&
1325 pc_regnum == regnum && unwindplan_regloc.IsInOtherRegister() &&
1326 unwindplan_regloc.GetRegisterNumber() ==
1327 arch_default_ra_regnum.GetAsKind(unwindplan_registerkind) &&
1328 m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes &&
1329 !m_all_registers_available) {
1330 UnwindLogMsg("%s UnwindPlan tried to restore the pc from the link "
1331 "register but this is a non-zero frame",
1332 m_full_unwind_plan_sp->GetSourceName().GetCString());
1334 // Throw away the full unwindplan; install the arch default unwindplan
1335 if (ForceSwitchToFallbackUnwindPlan()) {
1336 // Update for the possibly new unwind plan
1337 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind();
1338 UnwindPlan::RowSP active_row =
1339 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1341 // Sanity check: Verify that we can fetch a pc value and CFA value
1342 // with this unwind plan
1344 RegisterNumber arch_default_pc_reg(m_thread, eRegisterKindGeneric,
1345 LLDB_REGNUM_GENERIC_PC);
1346 bool can_fetch_pc_value = false;
1347 bool can_fetch_cfa = false;
1350 if (arch_default_pc_reg.GetAsKind(unwindplan_registerkind) !=
1351 LLDB_INVALID_REGNUM &&
1352 active_row->GetRegisterInfo(
1353 arch_default_pc_reg.GetAsKind(unwindplan_registerkind),
1354 unwindplan_regloc)) {
1355 can_fetch_pc_value = true;
1357 if (ReadCFAValueForRow(unwindplan_registerkind, active_row,
1359 can_fetch_cfa = true;
1363 if (can_fetch_pc_value && can_fetch_cfa) {
1364 have_unwindplan_regloc = true;
1366 have_unwindplan_regloc = false;
1369 // We were unable to fall back to another unwind plan
1370 have_unwindplan_regloc = false;
1376 ExecutionContext exe_ctx(m_thread.shared_from_this());
1377 Process *process = exe_ctx.GetProcessPtr();
1378 if (have_unwindplan_regloc == false) {
1379 // If the UnwindPlan failed to give us an unwind location for this register,
1380 // we may be able to fall back
1381 // to some ABI-defined default. For example, some ABIs allow to determine
1382 // the caller's SP via the CFA.
1383 // Also, the ABI may set volatile registers to the undefined state.
1384 ABI *abi = process ? process->GetABI().get() : NULL;
1386 const RegisterInfo *reg_info =
1387 GetRegisterInfoAtIndex(regnum.GetAsKind(eRegisterKindLLDB));
1389 abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1391 "supplying caller's saved %s (%d)'s location using ABI default",
1392 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1393 have_unwindplan_regloc = true;
1398 if (have_unwindplan_regloc == false) {
1399 if (IsFrameZero()) {
1400 // This is frame 0 - we should return the actual live register context
1402 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1404 UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1405 new_regloc.location.register_number = regnum.GetAsKind(eRegisterKindLLDB);
1406 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1407 regloc = new_regloc;
1408 UnwindLogMsg("supplying caller's register %s (%d) from the live "
1409 "RegisterContext at frame 0",
1410 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1411 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1413 std::string unwindplan_name("");
1414 if (m_full_unwind_plan_sp) {
1415 unwindplan_name += "via '";
1416 unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString();
1417 unwindplan_name += "'";
1419 UnwindLogMsg("no save location for %s (%d) %s", regnum.GetName(),
1420 regnum.GetAsKind(eRegisterKindLLDB),
1421 unwindplan_name.c_str());
1423 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1426 // unwindplan_regloc has valid contents about where to retrieve the register
1427 if (unwindplan_regloc.IsUnspecified()) {
1428 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1429 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved;
1430 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1431 UnwindLogMsg("save location for %s (%d) is unspecified, continue searching",
1432 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1433 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1436 if (unwindplan_regloc.IsUndefined()) {
1438 "did not supply reg location for %s (%d) because it is volatile",
1439 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1440 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile;
1443 if (unwindplan_regloc.IsSame()) {
1444 if (IsFrameZero() == false &&
1445 (regnum.GetAsKind(eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_PC ||
1446 regnum.GetAsKind(eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_RA)) {
1447 UnwindLogMsg("register %s (%d) is marked as 'IsSame' - it is a pc or "
1448 "return address reg on a non-zero frame -- treat as if we "
1449 "have no information",
1450 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1451 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1453 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1454 regloc.location.register_number = regnum.GetAsKind(eRegisterKindLLDB);
1455 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1457 "supplying caller's register %s (%d), saved in register %s (%d)",
1458 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1459 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1460 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1464 if (unwindplan_regloc.IsCFAPlusOffset()) {
1465 int offset = unwindplan_regloc.GetOffset();
1466 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1467 regloc.location.inferred_value = m_cfa + offset;
1468 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1469 UnwindLogMsg("supplying caller's register %s (%d), value is CFA plus "
1470 "offset %d [value is 0x%" PRIx64 "]",
1471 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1472 regloc.location.inferred_value);
1473 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1476 if (unwindplan_regloc.IsAtCFAPlusOffset()) {
1477 int offset = unwindplan_regloc.GetOffset();
1478 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1479 regloc.location.target_memory_location = m_cfa + offset;
1480 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1481 UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1482 "CFA plus offset %d [saved at 0x%" PRIx64 "]",
1483 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1484 regloc.location.target_memory_location);
1485 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1488 if (unwindplan_regloc.IsInOtherRegister()) {
1489 uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber();
1490 RegisterNumber row_regnum(m_thread, unwindplan_registerkind,
1492 if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1493 UnwindLogMsg("could not supply caller's %s (%d) location - was saved in "
1494 "another reg but couldn't convert that regnum",
1495 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1496 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1498 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1499 regloc.location.register_number = row_regnum.GetAsKind(eRegisterKindLLDB);
1500 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1502 "supplying caller's register %s (%d), saved in register %s (%d)",
1503 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1504 row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1505 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1508 if (unwindplan_regloc.IsDWARFExpression() ||
1509 unwindplan_regloc.IsAtDWARFExpression()) {
1510 DataExtractor dwarfdata(unwindplan_regloc.GetDWARFExpressionBytes(),
1511 unwindplan_regloc.GetDWARFExpressionLength(),
1512 process->GetByteOrder(),
1513 process->GetAddressByteSize());
1514 ModuleSP opcode_ctx;
1515 DWARFExpression dwarfexpr(opcode_ctx, dwarfdata, nullptr, 0,
1516 unwindplan_regloc.GetDWARFExpressionLength());
1517 dwarfexpr.SetRegisterKind(unwindplan_registerkind);
1520 if (dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr, result,
1523 val = result.GetScalar().ULongLong();
1524 if (unwindplan_regloc.IsDWARFExpression()) {
1525 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1526 regloc.location.inferred_value = val;
1527 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1528 UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1529 "(IsDWARFExpression)",
1530 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1531 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1534 UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1535 regloc.location.target_memory_location = val;
1536 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1537 UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1538 "(IsAtDWARFExpression)",
1539 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1540 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1543 UnwindLogMsg("tried to use IsDWARFExpression or IsAtDWARFExpression for %s "
1545 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1546 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1549 UnwindLogMsg("no save location for %s (%d) in this stack frame",
1550 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1552 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1555 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1558 // TryFallbackUnwindPlan() -- this method is a little tricky.
1560 // When this is called, the frame above -- the caller frame, the "previous"
1562 // is invalid or bad.
1564 // Instead of stopping the stack walk here, we'll try a different UnwindPlan and
1566 // if we can get a valid frame above us.
1568 // This most often happens when an unwind plan based on assembly instruction
1570 // is not correct -- mostly with hand-written assembly functions or functions
1572 // stack frame is set up "out of band", e.g. the kernel saved the register
1574 // then called an asynchronous trap handler like _sigtramp.
1576 // Often in these cases, if we just do a dumb stack walk we'll get past this
1578 // frame and our usual techniques can continue to be used.
1580 bool RegisterContextLLDB::TryFallbackUnwindPlan() {
1581 if (m_fallback_unwind_plan_sp.get() == nullptr)
1584 if (m_full_unwind_plan_sp.get() == nullptr)
1587 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
1588 m_full_unwind_plan_sp->GetSourceName() ==
1589 m_fallback_unwind_plan_sp->GetSourceName()) {
1593 // If a compiler generated unwind plan failed, trying the arch default
1595 // isn't going to do any better.
1596 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1599 // Get the caller's pc value and our own CFA value.
1600 // Swap in the fallback unwind plan, re-fetch the caller's pc value and CFA
1602 // If they're the same, then the fallback unwind plan provides no benefit.
1604 RegisterNumber pc_regnum(m_thread, eRegisterKindGeneric,
1605 LLDB_REGNUM_GENERIC_PC);
1607 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1608 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1609 addr_t old_this_frame_cfa_value = m_cfa;
1610 UnwindLLDB::RegisterLocation regloc;
1611 if (SavedLocationForRegister(pc_regnum.GetAsKind(eRegisterKindLLDB),
1613 UnwindLLDB::RegisterSearchResult::eRegisterFound) {
1614 const RegisterInfo *reg_info =
1615 GetRegisterInfoAtIndex(pc_regnum.GetAsKind(eRegisterKindLLDB));
1617 RegisterValue reg_value;
1618 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1619 old_caller_pc_value = reg_value.GetAsUInt64();
1624 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1626 // register location for the full unwind plan, it may call
1627 // ForceSwitchToFallbackUnwindPlan()
1628 // which in turn replaces the full unwindplan with the fallback... in short,
1630 // we're using the fallback UnwindPlan.
1631 // We checked if m_fallback_unwind_plan_sp was nullptr at the top -- the only
1633 // became nullptr since then is via SavedLocationForRegister().
1634 if (m_fallback_unwind_plan_sp.get() == nullptr)
1637 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1639 // working, we need to restore.
1640 // We'll also need to save & restore the value of the m_cfa ivar. Save is
1641 // down below a bit in 'old_cfa'.
1642 UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp;
1643 addr_t old_cfa = m_cfa;
1645 m_registers.clear();
1647 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1649 UnwindPlan::RowSP active_row =
1650 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1653 active_row->GetCFAValue().GetValueType() !=
1654 UnwindPlan::Row::CFAValue::unspecified) {
1656 if (!ReadCFAValueForRow(m_fallback_unwind_plan_sp->GetRegisterKind(),
1657 active_row, new_cfa) ||
1658 new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) {
1659 UnwindLogMsg("failed to get cfa with fallback unwindplan");
1660 m_fallback_unwind_plan_sp.reset();
1661 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1667 if (SavedLocationForRegister(pc_regnum.GetAsKind(eRegisterKindLLDB),
1669 UnwindLLDB::RegisterSearchResult::eRegisterFound) {
1670 const RegisterInfo *reg_info =
1671 GetRegisterInfoAtIndex(pc_regnum.GetAsKind(eRegisterKindLLDB));
1673 RegisterValue reg_value;
1674 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
1676 new_caller_pc_value = reg_value.GetAsUInt64();
1681 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
1682 UnwindLogMsg("failed to get a pc value for the caller frame with the "
1683 "fallback unwind plan");
1684 m_fallback_unwind_plan_sp.reset();
1685 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1690 if (old_caller_pc_value != LLDB_INVALID_ADDRESS) {
1691 if (old_caller_pc_value == new_caller_pc_value &&
1692 new_cfa == old_this_frame_cfa_value) {
1693 UnwindLogMsg("fallback unwind plan got the same values for this frame "
1694 "CFA and caller frame pc, not using");
1695 m_fallback_unwind_plan_sp.reset();
1696 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1702 UnwindLogMsg("trying to unwind from this function with the UnwindPlan '%s' "
1703 "because UnwindPlan '%s' failed.",
1704 m_fallback_unwind_plan_sp->GetSourceName().GetCString(),
1705 original_full_unwind_plan_sp->GetSourceName().GetCString());
1707 // We've copied the fallback unwind plan into the full - now clear the
1709 m_fallback_unwind_plan_sp.reset();
1715 bool RegisterContextLLDB::ForceSwitchToFallbackUnwindPlan() {
1716 if (m_fallback_unwind_plan_sp.get() == NULL)
1719 if (m_full_unwind_plan_sp.get() == NULL)
1722 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
1723 m_full_unwind_plan_sp->GetSourceName() ==
1724 m_fallback_unwind_plan_sp->GetSourceName()) {
1728 UnwindPlan::RowSP active_row =
1729 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1732 active_row->GetCFAValue().GetValueType() !=
1733 UnwindPlan::Row::CFAValue::unspecified) {
1735 if (!ReadCFAValueForRow(m_fallback_unwind_plan_sp->GetRegisterKind(),
1736 active_row, new_cfa) ||
1737 new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) {
1738 UnwindLogMsg("failed to get cfa with fallback unwindplan");
1739 m_fallback_unwind_plan_sp.reset();
1743 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1744 m_fallback_unwind_plan_sp.reset();
1746 m_registers.clear();
1750 UnwindLogMsg("switched unconditionally to the fallback unwindplan %s",
1751 m_full_unwind_plan_sp->GetSourceName().GetCString());
1757 bool RegisterContextLLDB::ReadCFAValueForRow(
1758 lldb::RegisterKind row_register_kind, const UnwindPlan::RowSP &row,
1759 addr_t &cfa_value) {
1760 RegisterValue reg_value;
1762 cfa_value = LLDB_INVALID_ADDRESS;
1763 addr_t cfa_reg_contents;
1765 switch (row->GetCFAValue().GetValueType()) {
1766 case UnwindPlan::Row::CFAValue::isRegisterDereferenced: {
1767 RegisterNumber cfa_reg(m_thread, row_register_kind,
1768 row->GetCFAValue().GetRegisterNumber());
1769 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
1770 const RegisterInfo *reg_info =
1771 GetRegisterInfoAtIndex(cfa_reg.GetAsKind(eRegisterKindLLDB));
1772 RegisterValue reg_value;
1774 Status error = ReadRegisterValueFromMemory(
1775 reg_info, cfa_reg_contents, reg_info->byte_size, reg_value);
1776 if (error.Success()) {
1777 cfa_value = reg_value.GetAsUInt64();
1779 "CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64
1780 ", CFA value is 0x%" PRIx64,
1781 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1782 cfa_reg_contents, cfa_value);
1785 UnwindLogMsg("Tried to deref reg %s (%d) [0x%" PRIx64
1786 "] but memory read failed.",
1787 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1794 case UnwindPlan::Row::CFAValue::isRegisterPlusOffset: {
1795 RegisterNumber cfa_reg(m_thread, row_register_kind,
1796 row->GetCFAValue().GetRegisterNumber());
1797 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
1798 if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 ||
1799 cfa_reg_contents == 1) {
1801 "Got an invalid CFA register value - reg %s (%d), value 0x%" PRIx64,
1802 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1804 cfa_reg_contents = LLDB_INVALID_ADDRESS;
1807 cfa_value = cfa_reg_contents + row->GetCFAValue().GetOffset();
1809 "CFA is 0x%" PRIx64 ": Register %s (%d) contents are 0x%" PRIx64
1811 cfa_value, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1812 cfa_reg_contents, row->GetCFAValue().GetOffset());
1817 case UnwindPlan::Row::CFAValue::isDWARFExpression: {
1818 ExecutionContext exe_ctx(m_thread.shared_from_this());
1819 Process *process = exe_ctx.GetProcessPtr();
1820 DataExtractor dwarfdata(row->GetCFAValue().GetDWARFExpressionBytes(),
1821 row->GetCFAValue().GetDWARFExpressionLength(),
1822 process->GetByteOrder(),
1823 process->GetAddressByteSize());
1824 ModuleSP opcode_ctx;
1825 DWARFExpression dwarfexpr(opcode_ctx, dwarfdata, nullptr, 0,
1826 row->GetCFAValue().GetDWARFExpressionLength());
1827 dwarfexpr.SetRegisterKind(row_register_kind);
1830 if (dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr, result,
1832 cfa_value = result.GetScalar().ULongLong();
1834 UnwindLogMsg("CFA value set by DWARF expression is 0x%" PRIx64,
1838 UnwindLogMsg("Failed to set CFA value via DWARF expression: %s",
1848 // Retrieve a general purpose register value for THIS frame, as saved by the
1849 // NEXT frame, i.e. the frame that
1850 // this frame called. e.g.
1853 // bar () { foo (); }
1854 // main () { bar (); }
1856 // stopped in foo() so
1860 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc
1861 // value for frame 1, we need to ask
1862 // where frame 0 (the "next" frame) saved that and retrieve the value.
1864 bool RegisterContextLLDB::ReadGPRValue(lldb::RegisterKind register_kind,
1865 uint32_t regnum, addr_t &value) {
1869 uint32_t lldb_regnum;
1870 if (register_kind == eRegisterKindLLDB) {
1871 lldb_regnum = regnum;
1872 } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
1873 register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
1877 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
1878 RegisterValue reg_value;
1879 // if this is frame 0 (currently executing frame), get the requested reg
1880 // contents from the actual thread registers
1881 if (IsFrameZero()) {
1882 if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
1883 value = reg_value.GetAsUInt64();
1889 bool pc_register = false;
1890 uint32_t generic_regnum;
1891 if (register_kind == eRegisterKindGeneric &&
1892 (regnum == LLDB_REGNUM_GENERIC_PC || regnum == LLDB_REGNUM_GENERIC_RA)) {
1894 } else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
1895 register_kind, regnum, eRegisterKindGeneric, generic_regnum) &&
1896 (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
1897 generic_regnum == LLDB_REGNUM_GENERIC_RA)) {
1901 lldb_private::UnwindLLDB::RegisterLocation regloc;
1902 if (!m_parent_unwind.SearchForSavedLocationForRegister(
1903 lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
1906 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1907 value = reg_value.GetAsUInt64();
1913 bool RegisterContextLLDB::ReadGPRValue(const RegisterNumber ®num,
1915 return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
1919 // Find the value of a register in THIS frame
1921 bool RegisterContextLLDB::ReadRegister(const RegisterInfo *reg_info,
1922 RegisterValue &value) {
1926 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1927 UnwindLogMsgVerbose("looking for register saved location for reg %d",
1930 // If this is the 0th frame, hand this over to the live register context
1931 if (IsFrameZero()) {
1932 UnwindLogMsgVerbose("passing along to the live register context for reg %d",
1934 return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
1937 bool is_pc_regnum = false;
1938 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_PC ||
1939 reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA) {
1940 is_pc_regnum = true;
1943 lldb_private::UnwindLLDB::RegisterLocation regloc;
1944 // Find out where the NEXT frame saved THIS frame's register contents
1945 if (!m_parent_unwind.SearchForSavedLocationForRegister(
1946 lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
1949 return ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
1952 bool RegisterContextLLDB::WriteRegister(const RegisterInfo *reg_info,
1953 const RegisterValue &value) {
1957 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1958 UnwindLogMsgVerbose("looking for register saved location for reg %d",
1961 // If this is the 0th frame, hand this over to the live register context
1962 if (IsFrameZero()) {
1963 UnwindLogMsgVerbose("passing along to the live register context for reg %d",
1965 return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
1968 lldb_private::UnwindLLDB::RegisterLocation regloc;
1969 // Find out where the NEXT frame saved THIS frame's register contents
1970 if (!m_parent_unwind.SearchForSavedLocationForRegister(
1971 lldb_regnum, regloc, m_frame_number - 1, false))
1974 return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
1977 // Don't need to implement this one
1978 bool RegisterContextLLDB::ReadAllRegisterValues(lldb::DataBufferSP &data_sp) {
1982 // Don't need to implement this one
1983 bool RegisterContextLLDB::WriteAllRegisterValues(
1984 const lldb::DataBufferSP &data_sp) {
1988 // Retrieve the pc value for THIS from
1990 bool RegisterContextLLDB::GetCFA(addr_t &cfa) {
1994 if (m_cfa == LLDB_INVALID_ADDRESS) {
2001 RegisterContextLLDB::SharedPtr RegisterContextLLDB::GetNextFrame() const {
2002 RegisterContextLLDB::SharedPtr regctx;
2003 if (m_frame_number == 0)
2005 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
2008 RegisterContextLLDB::SharedPtr RegisterContextLLDB::GetPrevFrame() const {
2009 RegisterContextLLDB::SharedPtr regctx;
2010 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number + 1);
2013 // Retrieve the address of the start of the function of THIS frame
2015 bool RegisterContextLLDB::GetStartPC(addr_t &start_pc) {
2019 if (!m_start_pc.IsValid()) {
2020 bool read_successfully = ReadPC (start_pc);
2021 if (read_successfully)
2023 ProcessSP process_sp (m_thread.GetProcess());
2026 ABI *abi = process_sp->GetABI().get();
2028 start_pc = abi->FixCodeAddress(start_pc);
2031 return read_successfully;
2033 start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2037 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2039 bool RegisterContextLLDB::ReadPC(addr_t &pc) {
2043 bool above_trap_handler = false;
2044 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2045 GetNextFrame()->IsTrapHandlerFrame())
2046 above_trap_handler = true;
2048 if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) {
2049 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2050 // indicates the end of a stack walk.
2051 // On the currently executing frame (or such a frame interrupted
2052 // asynchronously by sigtramp et al) this may
2053 // occur if code has jumped through a NULL pointer -- we want to be able to
2054 // unwind past that frame to help
2057 ProcessSP process_sp (m_thread.GetProcess());
2060 ABI *abi = process_sp->GetABI().get();
2062 pc = abi->FixCodeAddress(pc);
2065 if (m_all_registers_available == false && above_trap_handler == false &&
2066 (pc == 0 || pc == 1)) {
2076 void RegisterContextLLDB::UnwindLogMsg(const char *fmt, ...) {
2077 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
2080 va_start(args, fmt);
2083 if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == NULL) {
2091 log->Printf("%*sth%d/fr%u %s", m_frame_number < 100 ? m_frame_number : 100,
2092 "", m_thread.GetIndexID(), m_frame_number, logmsg);
2097 void RegisterContextLLDB::UnwindLogMsgVerbose(const char *fmt, ...) {
2098 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
2099 if (log && log->GetVerbose()) {
2101 va_start(args, fmt);
2104 if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == NULL) {
2112 log->Printf("%*sth%d/fr%u %s", m_frame_number < 100 ? m_frame_number : 100,
2113 "", m_thread.GetIndexID(), m_frame_number, logmsg);