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/DWARFCallFrameInfo.h"
21 #include "lldb/Symbol/FuncUnwinders.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/ObjectFile.h"
24 #include "lldb/Symbol/SymbolContext.h"
25 #include "lldb/Symbol/Symbol.h"
26 #include "lldb/Target/ABI.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Target/DynamicLoader.h"
34 #include "RegisterContextLLDB.h"
37 using namespace lldb_private;
39 RegisterContextLLDB::RegisterContextLLDB
42 const SharedPtr &next_frame,
43 SymbolContext& sym_ctx,
44 uint32_t frame_number,
45 UnwindLLDB& unwind_lldb
47 RegisterContext (thread, frame_number),
49 m_fast_unwind_plan_sp (),
50 m_full_unwind_plan_sp (),
51 m_all_registers_available(false),
53 m_cfa (LLDB_INVALID_ADDRESS),
57 m_current_offset_backed_up_one (0),
59 m_sym_ctx_valid (false),
60 m_frame_number (frame_number),
62 m_parent_unwind (unwind_lldb)
64 m_sym_ctx.Clear(false);
65 m_sym_ctx_valid = false;
69 InitializeZerothFrame ();
73 InitializeNonZerothFrame ();
76 // This same code exists over in the GetFullUnwindPlanForFrame() but it may not have been executed yet
78 || next_frame->m_frame_type == eSigtrampFrame
79 || next_frame->m_frame_type == eDebuggerFrame)
81 m_all_registers_available = true;
85 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the zeroth frame or currently
89 RegisterContextLLDB::InitializeZerothFrame()
91 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
92 ExecutionContext exe_ctx(m_thread.shared_from_this());
93 RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
95 if (reg_ctx_sp.get() == NULL)
97 m_frame_type = eNotAValidFrame;
101 addr_t current_pc = reg_ctx_sp->GetPC();
103 if (current_pc == LLDB_INVALID_ADDRESS)
105 m_frame_type = eNotAValidFrame;
109 Process *process = exe_ctx.GetProcessPtr();
111 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
112 // this will strip bit zero in case we read a PC from memory or from the LR.
113 // (which would be a no-op in frame 0 where we get it from the register set,
114 // but still a good idea to make the call here for other ABIs that may exist.)
115 ABI *abi = process->GetABI().get();
117 current_pc = abi->FixCodeAddress(current_pc);
119 // Initialize m_current_pc, an Address object, based on current_pc, an addr_t.
120 process->GetTarget().GetSectionLoadList().ResolveLoadAddress (current_pc, m_current_pc);
122 // If we don't have a Module for some reason, we're not going to find symbol/function information - just
123 // stick in some reasonable defaults and hope we can unwind past this frame.
124 ModuleSP pc_module_sp (m_current_pc.GetModule());
125 if (!m_current_pc.IsValid() || !pc_module_sp)
127 UnwindLogMsg ("using architectural default unwind method");
130 // We require that eSymbolContextSymbol be successfully filled in or this context is of no use to us.
131 if (pc_module_sp.get()
132 && (pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, eSymbolContextFunction| eSymbolContextSymbol, m_sym_ctx) & eSymbolContextSymbol) == eSymbolContextSymbol)
134 m_sym_ctx_valid = true;
137 AddressRange addr_range;
138 m_sym_ctx.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range);
140 static ConstString g_sigtramp_name ("_sigtramp");
141 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == g_sigtramp_name) ||
142 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == g_sigtramp_name))
144 m_frame_type = eSigtrampFrame;
148 // FIXME: Detect eDebuggerFrame here.
149 m_frame_type = eNormalFrame;
152 // If we were able to find a symbol/function, set addr_range to the bounds of that symbol/function.
153 // else treat the current pc value as the start_pc and record no offset.
154 if (addr_range.GetBaseAddress().IsValid())
156 m_start_pc = addr_range.GetBaseAddress();
157 if (m_current_pc.GetSection() == m_start_pc.GetSection())
159 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
161 else if (m_current_pc.GetModule() == m_start_pc.GetModule())
163 // This means that whatever symbol we kicked up isn't really correct
164 // --- we should not cross section boundaries ... We really should NULL out
165 // the function/symbol in this case unless there is a bad assumption
166 // here due to inlined functions?
167 m_current_offset = m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
169 m_current_offset_backed_up_one = m_current_offset;
173 m_start_pc = m_current_pc;
174 m_current_offset = -1;
175 m_current_offset_backed_up_one = -1;
178 // We've set m_frame_type and m_sym_ctx before these calls.
180 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame ();
181 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
183 UnwindPlan::RowSP active_row;
185 int row_register_kind = -1;
186 if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
188 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
189 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
190 if (active_row.get() && log)
192 StreamString active_row_strm;
193 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
194 UnwindLogMsg ("%s", active_row_strm.GetString().c_str());
198 if (!active_row.get())
200 m_frame_type = eNotAValidFrame;
205 addr_t cfa_regval = LLDB_INVALID_ADDRESS;
206 if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval))
208 m_frame_type = eNotAValidFrame;
212 cfa_offset = active_row->GetCFAOffset ();
213 m_cfa = cfa_regval + cfa_offset;
215 UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64 " (cfa_regval = 0x%16.16" PRIx64 ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset);
216 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64 " using %s UnwindPlan",
217 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()),
219 m_full_unwind_plan_sp->GetSourceName().GetCString());
222 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the RegisterContextLLDB "below" it
223 // to provide things like its current pc value.
226 RegisterContextLLDB::InitializeNonZerothFrame()
228 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
231 m_frame_type = eNotAValidFrame;
235 if (!GetNextFrame().get() || !GetNextFrame()->IsValid())
237 m_frame_type = eNotAValidFrame;
240 if (!m_thread.GetRegisterContext())
242 m_frame_type = eNotAValidFrame;
247 if (!ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc))
249 UnwindLogMsg ("could not get pc value");
250 m_frame_type = eNotAValidFrame;
256 UnwindLogMsg ("pc = 0x%16.16" PRIx64, pc);
258 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val))
259 UnwindLogMsg ("fp = 0x%16.16" PRIx64, reg_val);
260 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val))
261 UnwindLogMsg ("sp = 0x%16.16" PRIx64, reg_val);
264 // A pc of 0x0 means it's the end of the stack crawl
267 m_frame_type = eNotAValidFrame;
271 ExecutionContext exe_ctx(m_thread.shared_from_this());
272 Process *process = exe_ctx.GetProcessPtr();
273 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
274 // this will strip bit zero in case we read a PC from memory or from the LR.
275 ABI *abi = process->GetABI().get();
277 pc = abi->FixCodeAddress(pc);
279 process->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, m_current_pc);
281 // If we don't have a Module for some reason, we're not going to find symbol/function information - just
282 // stick in some reasonable defaults and hope we can unwind past this frame.
283 ModuleSP pc_module_sp (m_current_pc.GetModule());
284 if (!m_current_pc.IsValid() || !pc_module_sp)
286 UnwindLogMsg ("using architectural default unwind method");
288 // Test the pc value to see if we know it's in an unmapped/non-executable region of memory.
289 uint32_t permissions;
290 if (process->GetLoadAddressPermissions(pc, permissions)
291 && (permissions & ePermissionsExecutable) == 0)
293 // If this is the second frame off the stack, we may have unwound the first frame
294 // incorrectly. But using the architecture default unwind plan may get us back on
295 // track -- albeit possibly skipping a real frame. Give this frame a clearly-invalid
296 // pc and see if we can get any further.
297 if (GetNextFrame().get() && GetNextFrame()->IsValid() && GetNextFrame()->IsFrameZero())
299 UnwindLogMsg ("had a pc of 0x%" PRIx64 " which is not in executable memory but on frame 1 -- allowing it once.",
301 m_frame_type = eSkipFrame;
305 // anywhere other than the second frame, a non-executable pc means we're off in the weeds -- stop now.
306 m_frame_type = eNotAValidFrame;
313 m_fast_unwind_plan_sp.reset ();
314 m_full_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
315 abi->CreateDefaultUnwindPlan(*m_full_unwind_plan_sp);
316 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
318 m_frame_type = eNormalFrame;
320 m_all_registers_available = false;
321 m_current_offset = -1;
322 m_current_offset_backed_up_one = -1;
323 addr_t cfa_regval = LLDB_INVALID_ADDRESS;
324 int row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
325 UnwindPlan::RowSP row = m_full_unwind_plan_sp->GetRowForFunctionOffset(0);
328 uint32_t cfa_regnum = row->GetCFARegister();
329 int cfa_offset = row->GetCFAOffset();
330 if (!ReadGPRValue (row_register_kind, cfa_regnum, cfa_regval))
332 UnwindLogMsg ("failed to get cfa value");
333 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
335 m_frame_type = eNormalFrame;
339 m_cfa = cfa_regval + cfa_offset;
341 // A couple of sanity checks..
342 if (cfa_regval == LLDB_INVALID_ADDRESS || cfa_regval == 0 || cfa_regval == 1)
344 UnwindLogMsg ("could not find a valid cfa address");
345 m_frame_type = eNotAValidFrame;
349 // cfa_regval should point into the stack memory; if we can query memory region permissions,
350 // see if the memory is allocated & readable.
351 if (process->GetLoadAddressPermissions(cfa_regval, permissions)
352 && (permissions & ePermissionsReadable) == 0)
354 m_frame_type = eNotAValidFrame;
360 UnwindLogMsg ("could not find a row for function offset zero");
361 m_frame_type = eNotAValidFrame;
365 UnwindLogMsg ("initialized frame cfa is 0x%" PRIx64, (uint64_t) m_cfa);
368 m_frame_type = eNotAValidFrame;
372 // We require that eSymbolContextSymbol be successfully filled in or this context is of no use to us.
373 if ((pc_module_sp->ResolveSymbolContextForAddress (m_current_pc, eSymbolContextFunction| eSymbolContextSymbol, m_sym_ctx) & eSymbolContextSymbol) == eSymbolContextSymbol)
375 m_sym_ctx_valid = true;
378 AddressRange addr_range;
379 if (!m_sym_ctx.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
381 m_sym_ctx_valid = false;
384 bool decr_pc_and_recompute_addr_range = false;
386 // If the symbol lookup failed...
387 if (m_sym_ctx_valid == false)
388 decr_pc_and_recompute_addr_range = true;
390 // Or if we're in the middle of the stack (and not "above" an asynchronous event like sigtramp),
391 // and our "current" pc is the start of a function...
393 && GetNextFrame()->m_frame_type != eSigtrampFrame
394 && GetNextFrame()->m_frame_type != eDebuggerFrame
395 && addr_range.GetBaseAddress().IsValid()
396 && addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection()
397 && addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset())
399 decr_pc_and_recompute_addr_range = true;
402 // We need to back up the pc by 1 byte and re-search for the Symbol to handle the case where the "saved pc"
403 // value is pointing to the next function, e.g. if a function ends with a CALL instruction.
404 // FIXME this may need to be an architectural-dependent behavior; if so we'll need to add a member function
405 // to the ABI plugin and consult that.
406 if (decr_pc_and_recompute_addr_range)
408 Address temporary_pc(m_current_pc);
409 temporary_pc.SetOffset(m_current_pc.GetOffset() - 1);
410 m_sym_ctx.Clear(false);
411 m_sym_ctx_valid = false;
412 if ((pc_module_sp->ResolveSymbolContextForAddress (temporary_pc, eSymbolContextFunction| eSymbolContextSymbol, m_sym_ctx) & eSymbolContextSymbol) == eSymbolContextSymbol)
414 m_sym_ctx_valid = true;
416 if (!m_sym_ctx.GetAddressRange (eSymbolContextFunction | eSymbolContextSymbol, 0, false, addr_range))
418 m_sym_ctx_valid = false;
422 // If we were able to find a symbol/function, set addr_range_ptr to the bounds of that symbol/function.
423 // else treat the current pc value as the start_pc and record no offset.
424 if (addr_range.GetBaseAddress().IsValid())
426 m_start_pc = addr_range.GetBaseAddress();
427 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
428 m_current_offset_backed_up_one = m_current_offset;
429 if (decr_pc_and_recompute_addr_range && m_current_offset_backed_up_one > 0)
431 m_current_offset_backed_up_one--;
433 m_current_pc.SetOffset(m_current_pc.GetOffset() - 1);
438 m_start_pc = m_current_pc;
439 m_current_offset = -1;
440 m_current_offset_backed_up_one = -1;
443 static ConstString sigtramp_name ("_sigtramp");
444 if ((m_sym_ctx.function && m_sym_ctx.function->GetMangled().GetMangledName() == sigtramp_name)
445 || (m_sym_ctx.symbol && m_sym_ctx.symbol->GetMangled().GetMangledName() == sigtramp_name))
447 m_frame_type = eSigtrampFrame;
451 // FIXME: Detect eDebuggerFrame here.
452 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
454 m_frame_type = eNormalFrame;
458 // We've set m_frame_type and m_sym_ctx before this call.
459 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame ();
461 UnwindPlan::RowSP active_row;
463 int row_register_kind = -1;
465 // Try to get by with just the fast UnwindPlan if possible - the full UnwindPlan may be expensive to get
466 // (e.g. if we have to parse the entire eh_frame section of an ObjectFile for the first time.)
468 if (m_fast_unwind_plan_sp && m_fast_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
470 active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
471 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind ();
472 if (active_row.get() && log)
474 StreamString active_row_strm;
475 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
476 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str());
481 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
482 if (m_full_unwind_plan_sp && m_full_unwind_plan_sp->PlanValidAtAddress (m_current_pc))
484 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
485 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind ();
486 if (active_row.get() && log)
488 StreamString active_row_strm;
489 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread, m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
490 UnwindLogMsg ("active row: %s", active_row_strm.GetString().c_str());
495 if (!active_row.get())
497 m_frame_type = eNotAValidFrame;
501 addr_t cfa_regval = LLDB_INVALID_ADDRESS;
502 if (!ReadGPRValue (row_register_kind, active_row->GetCFARegister(), cfa_regval))
504 UnwindLogMsg ("failed to get cfa reg %d/%d", row_register_kind, active_row->GetCFARegister());
505 m_frame_type = eNotAValidFrame;
509 cfa_offset = active_row->GetCFAOffset ();
510 m_cfa = cfa_regval + cfa_offset;
512 UnwindLogMsg ("cfa_regval = 0x%16.16" PRIx64 " (cfa_regval = 0x%16.16" PRIx64 ", cfa_offset = %i)", m_cfa, cfa_regval, cfa_offset);
514 // A couple of sanity checks..
515 if (cfa_regval == LLDB_INVALID_ADDRESS || cfa_regval == 0 || cfa_regval == 1)
517 UnwindLogMsg ("could not find a valid cfa address");
518 m_frame_type = eNotAValidFrame;
522 // If we have a bad stack setup, we can get the same CFA value multiple times -- or even
523 // more devious, we can actually oscillate between two CFA values. Detect that here and
524 // break out to avoid a possible infinite loop in lldb trying to unwind the stack.
525 addr_t next_frame_cfa;
526 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
527 if (GetNextFrame().get() && GetNextFrame()->GetCFA(next_frame_cfa))
529 bool repeating_frames = false;
530 if (next_frame_cfa == m_cfa)
532 repeating_frames = true;
536 if (GetNextFrame()->GetNextFrame() && GetNextFrame()->GetNextFrame()->GetCFA(next_next_frame_cfa)
537 && next_next_frame_cfa == m_cfa)
539 repeating_frames = true;
542 if (repeating_frames && abi->FunctionCallsChangeCFA())
544 UnwindLogMsg ("same CFA address as next frame, assuming the unwind is looping - stopping");
545 m_frame_type = eNotAValidFrame;
550 UnwindLogMsg ("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64,
551 (uint64_t) m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr()), (uint64_t) m_cfa);
556 RegisterContextLLDB::IsFrameZero () const
558 return m_frame_number == 0;
562 // Find a fast unwind plan for this frame, if possible.
564 // On entry to this method,
566 // 1. m_frame_type should already be set to eSigtrampFrame/eDebuggerFrame if either of those are correct,
567 // 2. m_sym_ctx should already be filled in, and
568 // 3. m_current_pc should have the current pc value for this frame
569 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown
572 RegisterContextLLDB::GetFastUnwindPlanForFrame ()
574 UnwindPlanSP unwind_plan_sp;
575 ModuleSP pc_module_sp (m_current_pc.GetModule());
577 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL)
578 return unwind_plan_sp;
581 return unwind_plan_sp;
583 FuncUnwindersSP func_unwinders_sp (pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx));
584 if (!func_unwinders_sp)
585 return unwind_plan_sp;
587 // If we're in _sigtramp(), unwinding past this frame requires special knowledge.
588 if (m_frame_type == eSigtrampFrame || m_frame_type == eDebuggerFrame)
589 return unwind_plan_sp;
591 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind (m_thread);
594 if (unwind_plan_sp->PlanValidAtAddress (m_current_pc))
596 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
597 if (log && log->GetVerbose())
599 if (m_fast_unwind_plan_sp)
600 UnwindLogMsgVerbose ("frame, and has a fast UnwindPlan");
602 UnwindLogMsgVerbose ("frame");
604 m_frame_type = eNormalFrame;
605 return unwind_plan_sp;
609 unwind_plan_sp.reset();
612 return unwind_plan_sp;
615 // On entry to this method,
617 // 1. m_frame_type should already be set to eSigtrampFrame/eDebuggerFrame if either of those are correct,
618 // 2. m_sym_ctx should already be filled in, and
619 // 3. m_current_pc should have the current pc value for this frame
620 // 4. m_current_offset_backed_up_one should have the current byte offset into the function, maybe backed up by 1, -1 if unknown
623 RegisterContextLLDB::GetFullUnwindPlanForFrame ()
625 UnwindPlanSP unwind_plan_sp;
626 UnwindPlanSP arch_default_unwind_plan_sp;
627 ExecutionContext exe_ctx(m_thread.shared_from_this());
628 Process *process = exe_ctx.GetProcessPtr();
629 ABI *abi = process ? process->GetABI().get() : NULL;
632 arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
633 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
636 bool behaves_like_zeroth_frame = false;
638 || GetNextFrame()->m_frame_type == eSigtrampFrame
639 || GetNextFrame()->m_frame_type == eDebuggerFrame)
641 behaves_like_zeroth_frame = true;
642 // If this frame behaves like a 0th frame (currently executing or
643 // interrupted asynchronously), all registers can be retrieved.
644 m_all_registers_available = true;
647 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer) so the pc is 0x0
648 // in the zeroth frame, we need to use the "unwind at first instruction" arch default UnwindPlan
649 // Also, if this Process can report on memory region attributes, any non-executable region means
650 // we jumped through a bad function pointer - handle the same way as 0x0.
651 // Note, if the symbol context has a function for the symbol, then we don't need to do this check.
653 if ((!m_sym_ctx_valid || m_sym_ctx.function == NULL) && behaves_like_zeroth_frame && m_current_pc.IsValid())
655 uint32_t permissions;
656 addr_t current_pc_addr = m_current_pc.GetLoadAddress (exe_ctx.GetTargetPtr());
657 if (current_pc_addr == 0
658 || (process->GetLoadAddressPermissions(current_pc_addr, permissions)
659 && (permissions & ePermissionsExecutable) == 0))
661 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
662 abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp);
663 m_frame_type = eNormalFrame;
664 return unwind_plan_sp;
668 // No Module for the current pc, try using the architecture default unwind.
669 ModuleSP pc_module_sp (m_current_pc.GetModule());
670 if (!m_current_pc.IsValid() || !pc_module_sp || pc_module_sp->GetObjectFile() == NULL)
672 m_frame_type = eNormalFrame;
673 return arch_default_unwind_plan_sp;
676 FuncUnwindersSP func_unwinders_sp;
679 func_unwinders_sp = pc_module_sp->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx);
682 // No FuncUnwinders available for this pc (i.e. a stripped function symbol and -fomit-frame-pointer).
683 // Try using the eh_frame information relative to the current PC,
684 // and finally fall back on the architectural default unwind.
685 if (!func_unwinders_sp)
687 DWARFCallFrameInfo *eh_frame = pc_module_sp && pc_module_sp->GetObjectFile() ?
688 pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo() : nullptr;
690 m_frame_type = eNormalFrame;
691 if (eh_frame && m_current_pc.IsValid())
693 unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
694 // Even with -fomit-frame-pointer, we can try eh_frame to get back on track.
695 if (eh_frame->GetUnwindPlan (m_current_pc, *unwind_plan_sp))
696 return unwind_plan_sp;
698 unwind_plan_sp.reset();
700 return arch_default_unwind_plan_sp;
703 // If we're in _sigtramp(), unwinding past this frame requires special knowledge. On Mac OS X this knowledge
704 // is properly encoded in the eh_frame section, so prefer that if available.
705 // On other platforms we may need to provide a platform-specific UnwindPlan which encodes the details of
706 // how to unwind out of sigtramp.
707 if (m_frame_type == eSigtrampFrame)
709 m_fast_unwind_plan_sp.reset();
710 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one);
711 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
712 return unwind_plan_sp;
715 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame even when it's frame zero
716 // This comes up if we have hand-written functions in a Module and hand-written eh_frame. The assembly
717 // instruction inspection may fail and the eh_frame CFI were probably written with some care to do the
718 // right thing. It'd be nice if there was a way to ask the eh_frame directly if it is asynchronous
719 // (can be trusted at every instruction point) or synchronous (the normal case - only at call sites).
721 if (process && process->GetDynamicLoader() && process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo (m_sym_ctx))
723 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one);
724 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
726 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan because the DynamicLoader suggested we prefer it",
727 unwind_plan_sp->GetSourceName().GetCString());
728 return unwind_plan_sp;
732 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting the assembly language instructions
733 if (behaves_like_zeroth_frame)
735 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (m_thread);
736 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
738 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
739 return unwind_plan_sp;
743 // Typically this is unwind info from an eh_frame section intended for exception handling; only valid at call sites
744 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite (m_current_offset_backed_up_one);
745 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
747 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
748 return unwind_plan_sp;
751 // We'd prefer to use an UnwindPlan intended for call sites when we're at a call site but if we've
752 // struck out on that, fall back to using the non-call-site assembly inspection UnwindPlan if possible.
753 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite (m_thread);
754 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress (m_current_pc))
756 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", unwind_plan_sp->GetSourceName().GetCString());
757 return unwind_plan_sp;
760 // If nothing else, use the architectural default UnwindPlan and hope that does the job.
761 UnwindLogMsgVerbose ("frame uses %s for full UnwindPlan", arch_default_unwind_plan_sp->GetSourceName().GetCString());
762 return arch_default_unwind_plan_sp;
767 RegisterContextLLDB::InvalidateAllRegisters ()
769 m_frame_type = eNotAValidFrame;
773 RegisterContextLLDB::GetRegisterCount ()
775 return m_thread.GetRegisterContext()->GetRegisterCount();
779 RegisterContextLLDB::GetRegisterInfoAtIndex (size_t reg)
781 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex (reg);
785 RegisterContextLLDB::GetRegisterSetCount ()
787 return m_thread.GetRegisterContext()->GetRegisterSetCount ();
791 RegisterContextLLDB::GetRegisterSet (size_t reg_set)
793 return m_thread.GetRegisterContext()->GetRegisterSet (reg_set);
797 RegisterContextLLDB::ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num)
799 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber (kind, num);
803 RegisterContextLLDB::ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
804 const RegisterInfo *reg_info,
805 RegisterValue &value)
809 bool success = false;
813 case UnwindLLDB::RegisterLocation::eRegisterInRegister:
815 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
822 success = m_thread.GetRegisterContext()->ReadRegister (other_reg_info, value);
826 success = GetNextFrame()->ReadRegister (other_reg_info, value);
830 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
831 success = value.SetUInt (regloc.location.inferred_value, reg_info->byte_size);
834 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
836 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
837 assert ("FIXME debugger inferior function call unwind");
839 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation:
841 Error error (ReadRegisterValueFromMemory(reg_info,
842 regloc.location.target_memory_location,
845 success = error.Success();
849 assert ("Unknown RegisterLocation type.");
856 RegisterContextLLDB::WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
857 const RegisterInfo *reg_info,
858 const RegisterValue &value)
863 bool success = false;
867 case UnwindLLDB::RegisterLocation::eRegisterInRegister:
869 const RegisterInfo *other_reg_info = GetRegisterInfoAtIndex(regloc.location.register_number);
872 success = m_thread.GetRegisterContext()->WriteRegister (other_reg_info, value);
876 success = GetNextFrame()->WriteRegister (other_reg_info, value);
880 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
881 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
883 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
884 assert ("FIXME debugger inferior function call unwind");
886 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation:
888 Error error (WriteRegisterValueToMemory (reg_info,
889 regloc.location.target_memory_location,
892 success = error.Success();
896 assert ("Unknown RegisterLocation type.");
904 RegisterContextLLDB::IsValid () const
906 return m_frame_type != eNotAValidFrame;
909 // A skip frame is a bogus frame on the stack -- but one where we're likely to find a real frame farther
910 // up the stack if we keep looking. It's always the second frame in an unwind (i.e. the first frame after
911 // frame zero) where unwinding can be the trickiest. Ideally we'll mark up this frame in some way so the
912 // user knows we're displaying bad data and we may have skipped one frame of their real program in the
913 // process of getting back on track.
916 RegisterContextLLDB::IsSkipFrame () const
918 return m_frame_type == eSkipFrame;
921 // Answer the question: Where did THIS frame save the CALLER frame ("previous" frame)'s register value?
923 enum UnwindLLDB::RegisterSearchResult
924 RegisterContextLLDB::SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc)
926 // Have we already found this register location?
927 if (!m_registers.empty())
929 std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>::const_iterator iterator;
930 iterator = m_registers.find (lldb_regnum);
931 if (iterator != m_registers.end())
933 regloc = iterator->second;
934 UnwindLogMsg ("supplying caller's saved reg %d's location, cached", lldb_regnum);
935 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
939 uint32_t sp_regnum = LLDB_INVALID_REGNUM;
940 uint32_t pc_regnum = LLDB_INVALID_REGNUM;
941 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, eRegisterKindLLDB, sp_regnum);
942 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, eRegisterKindLLDB, pc_regnum);
944 // Are we looking for the CALLER's stack pointer? The stack pointer is defined to be the same as THIS frame's
945 // CFA so just return the CFA value. This is true on x86-32/x86-64 at least.
946 if (sp_regnum != LLDB_INVALID_REGNUM && sp_regnum == lldb_regnum)
948 // make sure we won't lose precision copying an addr_t (m_cfa) into a uint64_t (.inferred_value)
949 assert (sizeof (addr_t) <= sizeof (uint64_t));
950 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
951 regloc.location.inferred_value = m_cfa;
952 m_registers[lldb_regnum] = regloc;
953 UnwindLogMsg ("supplying caller's stack pointer (%d) value, computed from CFA", lldb_regnum);
954 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
957 // Look through the available UnwindPlans for the register location.
959 UnwindPlan::Row::RegisterLocation unwindplan_regloc;
960 bool have_unwindplan_regloc = false;
961 RegisterKind unwindplan_registerkind = (RegisterKind)-1;
963 if (m_fast_unwind_plan_sp)
965 UnwindPlan::RowSP active_row = m_fast_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
966 unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind ();
968 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum))
970 UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme",
971 lldb_regnum, (int) unwindplan_registerkind);
972 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
974 if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc))
976 UnwindLogMsg ("supplying caller's saved reg %d's location using FastUnwindPlan", lldb_regnum);
977 have_unwindplan_regloc = true;
981 if (!have_unwindplan_regloc)
983 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a full UnwindPlan yet
984 if (!m_full_unwind_plan_sp)
985 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame ();
987 if (m_full_unwind_plan_sp)
989 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
990 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind ();
992 bool row_register_rewritten_to_return_address_reg = false;
994 // If we're fetching the saved pc and this UnwindPlan defines a ReturnAddress register (e.g. lr on arm),
995 // look for the return address register number in the UnwindPlan's row.
996 if (lldb_regnum == pc_regnum && m_full_unwind_plan_sp->GetReturnAddressRegister() != LLDB_INVALID_REGNUM)
998 row_regnum = m_full_unwind_plan_sp->GetReturnAddressRegister();
999 row_register_rewritten_to_return_address_reg = true;
1000 UnwindLogMsg ("requested caller's saved PC but this UnwindPlan uses a RA reg; getting reg %d instead",
1005 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindLLDB, lldb_regnum, unwindplan_registerkind, row_regnum))
1007 if (unwindplan_registerkind == eRegisterKindGeneric)
1008 UnwindLogMsg ("could not convert lldb regnum %d into eRegisterKindGeneric reg numbering scheme", lldb_regnum);
1010 UnwindLogMsg ("could not convert lldb regnum %d into %d RegisterKind reg numbering scheme",
1011 lldb_regnum, (int) unwindplan_registerkind);
1012 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1016 if (active_row->GetRegisterInfo (row_regnum, unwindplan_regloc))
1018 have_unwindplan_regloc = true;
1019 UnwindLogMsg ("supplying caller's saved reg %d's location using %s UnwindPlan", lldb_regnum,
1020 m_full_unwind_plan_sp->GetSourceName().GetCString());
1023 // This is frame 0 and we're retrieving the PC and it's saved in a Return Address register and
1024 // it hasn't been saved anywhere yet -- that is, it's still live in the actual register.
1025 // Handle this specially.
1027 if (have_unwindplan_regloc == false
1028 && row_register_rewritten_to_return_address_reg == true
1030 && row_regnum != LLDB_INVALID_REGNUM)
1032 uint32_t ra_regnum_in_lldb_reg_numbering;
1033 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, row_regnum, eRegisterKindLLDB, ra_regnum_in_lldb_reg_numbering))
1035 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1036 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1037 new_regloc.location.register_number = ra_regnum_in_lldb_reg_numbering;
1038 m_registers[lldb_regnum] = new_regloc;
1039 regloc = new_regloc;
1040 UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0, saved in %d", lldb_regnum, ra_regnum_in_lldb_reg_numbering);
1041 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1045 // If this architecture stores the return address in a register (it defines a Return Address register)
1046 // and we're on a non-zero stack frame and the Full UnwindPlan says that the pc is stored in the
1047 // RA registers (e.g. lr on arm), then we know that the full unwindplan is not trustworthy -- this
1048 // is an impossible situation and the instruction emulation code has likely been misled.
1049 // If this stack frame meets those criteria, we need to throw away the Full UnwindPlan that the
1050 // instruction emulation came up with and fall back to the architecture's Default UnwindPlan so
1051 // the stack walk can get past this point.
1053 // Special note: If the Full UnwindPlan was generated from the compiler, don't second-guess it
1054 // when we're at a call site location.
1056 // arch_default_ra_regnum is the return address register # in the Full UnwindPlan register numbering
1057 uint32_t arch_default_ra_regnum = LLDB_INVALID_REGNUM;
1058 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_RA, unwindplan_registerkind, arch_default_ra_regnum)
1059 && arch_default_ra_regnum != LLDB_INVALID_REGNUM
1060 && pc_regnum != LLDB_INVALID_REGNUM
1061 && pc_regnum == lldb_regnum
1062 && unwindplan_regloc.IsInOtherRegister()
1063 && unwindplan_regloc.GetRegisterNumber() == arch_default_ra_regnum
1064 && m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes
1065 && !m_all_registers_available)
1067 UnwindLogMsg ("%s UnwindPlan tried to restore the pc from the link register but this is a non-zero frame",
1068 m_full_unwind_plan_sp->GetSourceName().GetCString());
1070 // Throw away the full unwindplan; install the arch default unwindplan
1071 InvalidateFullUnwindPlan();
1073 // Now re-fetch the pc value we're searching for
1074 uint32_t arch_default_pc_reg = LLDB_INVALID_REGNUM;
1075 UnwindPlan::RowSP active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1076 if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, m_full_unwind_plan_sp->GetRegisterKind(), arch_default_pc_reg)
1077 && arch_default_pc_reg != LLDB_INVALID_REGNUM
1079 && active_row->GetRegisterInfo (arch_default_pc_reg, unwindplan_regloc))
1081 have_unwindplan_regloc = true;
1085 have_unwindplan_regloc = false;
1092 ExecutionContext exe_ctx(m_thread.shared_from_this());
1093 Process *process = exe_ctx.GetProcessPtr();
1094 if (have_unwindplan_regloc == false)
1096 // If a volatile register is being requested, we don't want to forward the next frame's register contents
1097 // up the stack -- the register is not retrievable at this frame.
1098 ABI *abi = process ? process->GetABI().get() : NULL;
1101 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
1102 if (reg_info && abi->RegisterIsVolatile (reg_info))
1104 UnwindLogMsg ("did not supply reg location for %d because it is volatile", lldb_regnum);
1105 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile;
1111 // This is frame 0 - we should return the actual live register context value
1112 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1113 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1114 new_regloc.location.register_number = lldb_regnum;
1115 m_registers[lldb_regnum] = new_regloc;
1116 regloc = new_regloc;
1117 UnwindLogMsg ("supplying caller's register %d from the live RegisterContext at frame 0", lldb_regnum);
1118 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1121 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
1122 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1125 // unwindplan_regloc has valid contents about where to retrieve the register
1126 if (unwindplan_regloc.IsUnspecified())
1128 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1129 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved;
1130 m_registers[lldb_regnum] = new_regloc;
1131 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
1132 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1135 if (unwindplan_regloc.IsSame())
1139 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
1140 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1144 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1148 if (unwindplan_regloc.IsCFAPlusOffset())
1150 int offset = unwindplan_regloc.GetOffset();
1151 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1152 regloc.location.inferred_value = m_cfa + offset;
1153 m_registers[lldb_regnum] = regloc;
1154 UnwindLogMsg ("supplying caller's register %d, value is CFA plus offset", lldb_regnum);
1155 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1158 if (unwindplan_regloc.IsAtCFAPlusOffset())
1160 int offset = unwindplan_regloc.GetOffset();
1161 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1162 regloc.location.target_memory_location = m_cfa + offset;
1163 m_registers[lldb_regnum] = regloc;
1164 UnwindLogMsg ("supplying caller's register %d from the stack, saved at CFA plus offset", lldb_regnum);
1165 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1168 if (unwindplan_regloc.IsInOtherRegister())
1170 uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber();
1171 uint32_t row_regnum_in_lldb;
1172 if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (unwindplan_registerkind, unwindplan_regnum, eRegisterKindLLDB, row_regnum_in_lldb))
1174 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
1175 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1177 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1178 regloc.location.register_number = row_regnum_in_lldb;
1179 m_registers[lldb_regnum] = regloc;
1180 UnwindLogMsg ("supplying caller's register %d, saved in register %d", lldb_regnum, row_regnum_in_lldb);
1181 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1184 if (unwindplan_regloc.IsDWARFExpression() || unwindplan_regloc.IsAtDWARFExpression())
1186 DataExtractor dwarfdata (unwindplan_regloc.GetDWARFExpressionBytes(),
1187 unwindplan_regloc.GetDWARFExpressionLength(),
1188 process->GetByteOrder(), process->GetAddressByteSize());
1189 DWARFExpression dwarfexpr (dwarfdata, 0, unwindplan_regloc.GetDWARFExpressionLength());
1190 dwarfexpr.SetRegisterKind (unwindplan_registerkind);
1193 if (dwarfexpr.Evaluate (&exe_ctx, NULL, NULL, this, 0, NULL, result, &error))
1196 val = result.GetScalar().ULongLong();
1197 if (unwindplan_regloc.IsDWARFExpression())
1199 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1200 regloc.location.inferred_value = val;
1201 m_registers[lldb_regnum] = regloc;
1202 UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsDWARFExpression)", lldb_regnum);
1203 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1207 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1208 regloc.location.target_memory_location = val;
1209 m_registers[lldb_regnum] = regloc;
1210 UnwindLogMsg ("supplying caller's register %d via DWARF expression (IsAtDWARFExpression)", lldb_regnum);
1211 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1214 UnwindLogMsg ("tried to use IsDWARFExpression or IsAtDWARFExpression for reg %d but failed", lldb_regnum);
1215 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1218 UnwindLogMsg ("could not supply caller's reg %d location", lldb_regnum);
1220 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are unsupported.
1222 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1225 // If the Full unwindplan has been determined to be incorrect, this method will
1226 // replace it with the architecture's default unwindplna, if one is defined.
1227 // It will also find the FuncUnwinders object for this function and replace the
1228 // Full unwind method for the function there so we don't use the errant Full unwindplan
1229 // again in the future of this debug session.
1230 // We're most likely doing this because the Full unwindplan was generated by assembly
1231 // instruction profiling and the profiler got something wrong.
1234 RegisterContextLLDB::InvalidateFullUnwindPlan ()
1236 UnwindPlan::Row::RegisterLocation unwindplan_regloc;
1237 ExecutionContext exe_ctx (m_thread.shared_from_this());
1238 Process *process = exe_ctx.GetProcessPtr();
1239 ABI *abi = process ? process->GetABI().get() : NULL;
1242 UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp;
1243 UnwindPlanSP arch_default_unwind_plan_sp;
1244 arch_default_unwind_plan_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
1245 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
1246 if (arch_default_unwind_plan_sp)
1248 UnwindPlan::RowSP active_row = arch_default_unwind_plan_sp->GetRowForFunctionOffset (m_current_offset);
1250 if (active_row && active_row->GetCFARegister() != LLDB_INVALID_REGNUM)
1252 FuncUnwindersSP func_unwinders_sp;
1253 if (m_sym_ctx_valid && m_current_pc.IsValid() && m_current_pc.GetModule())
1255 func_unwinders_sp = m_current_pc.GetModule()->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (m_current_pc, m_sym_ctx);
1256 if (func_unwinders_sp)
1258 func_unwinders_sp->InvalidateNonCallSiteUnwindPlan (m_thread);
1261 m_registers.clear();
1262 m_full_unwind_plan_sp = arch_default_unwind_plan_sp;
1263 addr_t cfa_regval = LLDB_INVALID_ADDRESS;
1264 if (ReadGPRValue (arch_default_unwind_plan_sp->GetRegisterKind(), active_row->GetCFARegister(), cfa_regval))
1266 m_cfa = cfa_regval + active_row->GetCFAOffset ();
1269 UnwindLogMsg ("full unwind plan '%s' has been replaced by architecture default unwind plan '%s' for this function from now on.",
1270 original_full_unwind_plan_sp->GetSourceName().GetCString(), arch_default_unwind_plan_sp->GetSourceName().GetCString());
1276 // Retrieve a general purpose register value for THIS frame, as saved by the NEXT frame, i.e. the frame that
1277 // this frame called. e.g.
1280 // bar () { foo (); }
1281 // main () { bar (); }
1283 // stopped in foo() so
1287 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc value for frame 1, we need to ask
1288 // where frame 0 (the "next" frame) saved that and retrieve the value.
1291 RegisterContextLLDB::ReadGPRValue (int register_kind, uint32_t regnum, addr_t &value)
1296 uint32_t lldb_regnum;
1297 if (register_kind == eRegisterKindLLDB)
1299 lldb_regnum = regnum;
1301 else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindLLDB, lldb_regnum))
1306 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
1307 RegisterValue reg_value;
1308 // if this is frame 0 (currently executing frame), get the requested reg contents from the actual thread registers
1311 if (m_thread.GetRegisterContext()->ReadRegister (reg_info, reg_value))
1313 value = reg_value.GetAsUInt64();
1319 bool pc_register = false;
1320 uint32_t generic_regnum;
1321 if (register_kind == eRegisterKindGeneric && regnum == LLDB_REGNUM_GENERIC_PC)
1325 else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds (register_kind, regnum, eRegisterKindGeneric, generic_regnum)
1326 && generic_regnum == LLDB_REGNUM_GENERIC_PC)
1331 lldb_private::UnwindLLDB::RegisterLocation regloc;
1332 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, pc_register))
1336 if (ReadRegisterValueFromRegisterLocation (regloc, reg_info, reg_value))
1338 value = reg_value.GetAsUInt64();
1344 // Find the value of a register in THIS frame
1347 RegisterContextLLDB::ReadRegister (const RegisterInfo *reg_info, RegisterValue &value)
1352 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1353 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum);
1355 // If this is the 0th frame, hand this over to the live register context
1358 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum);
1359 return m_thread.GetRegisterContext()->ReadRegister (reg_info, value);
1362 lldb_private::UnwindLLDB::RegisterLocation regloc;
1363 // Find out where the NEXT frame saved THIS frame's register contents
1364 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false))
1367 return ReadRegisterValueFromRegisterLocation (regloc, reg_info, value);
1371 RegisterContextLLDB::WriteRegister (const RegisterInfo *reg_info, const RegisterValue &value)
1376 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1377 UnwindLogMsgVerbose ("looking for register saved location for reg %d", lldb_regnum);
1379 // If this is the 0th frame, hand this over to the live register context
1382 UnwindLogMsgVerbose ("passing along to the live register context for reg %d", lldb_regnum);
1383 return m_thread.GetRegisterContext()->WriteRegister (reg_info, value);
1386 lldb_private::UnwindLLDB::RegisterLocation regloc;
1387 // Find out where the NEXT frame saved THIS frame's register contents
1388 if (!m_parent_unwind.SearchForSavedLocationForRegister (lldb_regnum, regloc, m_frame_number - 1, false))
1391 return WriteRegisterValueToRegisterLocation (regloc, reg_info, value);
1394 // Don't need to implement this one
1396 RegisterContextLLDB::ReadAllRegisterValues (lldb::DataBufferSP &data_sp)
1401 // Don't need to implement this one
1403 RegisterContextLLDB::WriteAllRegisterValues (const lldb::DataBufferSP& data_sp)
1408 // Retrieve the pc value for THIS from
1411 RegisterContextLLDB::GetCFA (addr_t& cfa)
1417 if (m_cfa == LLDB_INVALID_ADDRESS)
1426 RegisterContextLLDB::SharedPtr
1427 RegisterContextLLDB::GetNextFrame () const
1429 RegisterContextLLDB::SharedPtr regctx;
1430 if (m_frame_number == 0)
1432 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number - 1);
1435 RegisterContextLLDB::SharedPtr
1436 RegisterContextLLDB::GetPrevFrame () const
1438 RegisterContextLLDB::SharedPtr regctx;
1439 return m_parent_unwind.GetRegisterContextForFrameNum (m_frame_number + 1);
1442 // Retrieve the address of the start of the function of THIS frame
1445 RegisterContextLLDB::GetStartPC (addr_t& start_pc)
1450 if (!m_start_pc.IsValid())
1452 return ReadPC (start_pc);
1454 start_pc = m_start_pc.GetLoadAddress (CalculateTarget().get());
1458 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
1461 RegisterContextLLDB::ReadPC (addr_t& pc)
1466 if (ReadGPRValue (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc))
1468 // A pc value of 0 or 1 is impossible in the middle of the stack -- it indicates the end of a stack walk.
1469 // On the currently executing frame (or such a frame interrupted asynchronously by sigtramp et al) this may
1470 // occur if code has jumped through a NULL pointer -- we want to be able to unwind past that frame to help
1473 if (m_all_registers_available == false
1474 && (pc == 0 || pc == 1))
1491 RegisterContextLLDB::UnwindLogMsg (const char *fmt, ...)
1493 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
1497 va_start (args, fmt);
1500 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL)
1509 log->Printf ("%*sth%d/fr%u %s",
1510 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number,
1517 RegisterContextLLDB::UnwindLogMsgVerbose (const char *fmt, ...)
1519 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
1520 if (log && log->GetVerbose())
1523 va_start (args, fmt);
1526 if (vasprintf (&logmsg, fmt, args) == -1 || logmsg == NULL)
1535 log->Printf ("%*sth%d/fr%u %s",
1536 m_frame_number < 100 ? m_frame_number : 100, "", m_thread.GetIndexID(), m_frame_number,