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/Value.h"
14 #include "lldb/Expression/DWARFExpression.h"
15 #include "lldb/Symbol/ArmUnwindInfo.h"
16 #include "lldb/Symbol/DWARFCallFrameInfo.h"
17 #include "lldb/Symbol/FuncUnwinders.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Symbol/SymbolContext.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/DynamicLoader.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Platform.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/SectionLoadList.h"
28 #include "lldb/Target/StackFrame.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Utility/DataBufferHeap.h"
32 #include "lldb/Utility/Log.h"
33 #include "lldb/Utility/RegisterValue.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),
58 m_afa(LLDB_INVALID_ADDRESS), m_start_pc(),
59 m_current_pc(), m_current_offset(0), m_current_offset_backed_up_one(0),
60 m_sym_ctx(sym_ctx), m_sym_ctx_valid(false), m_frame_number(frame_number),
61 m_registers(), m_parent_unwind(unwind_lldb) {
62 m_sym_ctx.Clear(false);
63 m_sym_ctx_valid = false;
66 InitializeZerothFrame();
68 InitializeNonZerothFrame();
71 // This same code exists over in the GetFullUnwindPlanForFrame() but it may
72 // not have been executed yet
73 if (IsFrameZero() || next_frame->m_frame_type == eTrapHandlerFrame ||
74 next_frame->m_frame_type == eDebuggerFrame) {
75 m_all_registers_available = true;
79 bool RegisterContextLLDB::IsUnwindPlanValidForCurrentPC(
80 lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset) {
84 // check if m_current_pc is valid
85 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
86 // yes - current offset can be used as is
87 valid_pc_offset = m_current_offset;
91 // if m_current_offset <= 0, we've got nothing else to try
92 if (m_current_offset <= 0)
95 // check pc - 1 to see if it's valid
96 Address pc_minus_one(m_current_pc);
97 pc_minus_one.SetOffset(m_current_pc.GetOffset() - 1);
98 if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) {
99 // *valid_pc_offset = m_current_offset - 1;
100 valid_pc_offset = m_current_pc.GetOffset() - 1;
107 // Initialize a RegisterContextLLDB which is the first frame of a stack -- the
108 // zeroth frame or currently executing frame.
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
136 ABI *abi = process->GetABI().get();
138 current_pc = abi->FixCodeAddress(current_pc);
140 // Initialize m_current_pc, an Address object, based on current_pc, an
142 m_current_pc.SetLoadAddress(current_pc, &process->GetTarget());
144 // If we don't have a Module for some reason, we're not going to find
145 // symbol/function information - just stick in some reasonable defaults and
146 // hope we can unwind past this frame.
147 ModuleSP pc_module_sp(m_current_pc.GetModule());
148 if (!m_current_pc.IsValid() || !pc_module_sp) {
149 UnwindLogMsg("using architectural default unwind method");
152 // We require either a symbol or function in the symbols context to be
153 // successfully filled in or this context is of no use to us.
154 const SymbolContextItem resolve_scope =
155 eSymbolContextFunction | eSymbolContextSymbol;
156 if (pc_module_sp.get() && (pc_module_sp->ResolveSymbolContextForAddress(
157 m_current_pc, resolve_scope, m_sym_ctx) &
159 m_sym_ctx_valid = true;
162 if (m_sym_ctx.symbol) {
163 UnwindLogMsg("with pc value of 0x%" PRIx64 ", symbol name is '%s'",
164 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
165 } else if (m_sym_ctx.function) {
166 UnwindLogMsg("with pc value of 0x%" PRIx64 ", function name is '%s'",
167 current_pc, GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
169 UnwindLogMsg("with pc value of 0x%" PRIx64
170 ", no symbol/function name is known.",
174 AddressRange addr_range;
175 m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range);
177 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
178 m_frame_type = eTrapHandlerFrame;
180 // FIXME: Detect eDebuggerFrame here.
181 m_frame_type = eNormalFrame;
184 // If we were able to find a symbol/function, set addr_range to the bounds of
185 // that symbol/function. else treat the current pc value as the start_pc and
187 if (addr_range.GetBaseAddress().IsValid()) {
188 m_start_pc = addr_range.GetBaseAddress();
189 if (m_current_pc.GetSection() == m_start_pc.GetSection()) {
190 m_current_offset = m_current_pc.GetOffset() - m_start_pc.GetOffset();
191 } else if (m_current_pc.GetModule() == m_start_pc.GetModule()) {
192 // This means that whatever symbol we kicked up isn't really correct ---
193 // we should not cross section boundaries ... We really should NULL out
194 // the function/symbol in this case unless there is a bad assumption here
195 // due to inlined functions?
197 m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
199 m_current_offset_backed_up_one = m_current_offset;
201 m_start_pc = m_current_pc;
202 m_current_offset = -1;
203 m_current_offset_backed_up_one = -1;
206 // We've set m_frame_type and m_sym_ctx before these calls.
208 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame();
209 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
211 UnwindPlan::RowSP active_row;
212 lldb::RegisterKind row_register_kind = eRegisterKindGeneric;
213 if (m_full_unwind_plan_sp &&
214 m_full_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
216 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
217 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
218 if (active_row.get() && log) {
219 StreamString active_row_strm;
220 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
221 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
222 UnwindLogMsg("%s", active_row_strm.GetData());
226 if (!active_row.get()) {
227 UnwindLogMsg("could not find an unwindplan row for this frame's pc");
228 m_frame_type = eNotAValidFrame;
232 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
233 // Try the fall back unwind plan since the
234 // full unwind plan failed.
235 FuncUnwindersSP func_unwinders_sp;
236 UnwindPlanSP call_site_unwind_plan;
237 bool cfa_status = false;
239 if (m_sym_ctx_valid) {
241 pc_module_sp->GetObjectFile()
243 .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx);
246 if (func_unwinders_sp.get() != nullptr)
247 call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(
248 process->GetTarget(), m_current_offset_backed_up_one);
250 if (call_site_unwind_plan.get() != nullptr) {
251 m_fallback_unwind_plan_sp = call_site_unwind_plan;
252 if (TryFallbackUnwindPlan())
256 UnwindLogMsg("could not read CFA value for first frame.");
257 m_frame_type = eNotAValidFrame;
261 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
263 UnwindLogMsg("initialized frame current pc is 0x%" PRIx64 " cfa is 0x%" PRIx64
264 " afa is 0x%" PRIx64 " using %s UnwindPlan",
265 (uint64_t)m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()),
268 m_full_unwind_plan_sp->GetSourceName().GetCString());
271 // Initialize a RegisterContextLLDB for the non-zeroth frame -- rely on the
272 // RegisterContextLLDB "below" it to provide things like its current pc value.
274 void RegisterContextLLDB::InitializeNonZerothFrame() {
275 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
277 m_frame_type = eNotAValidFrame;
278 UnwindLogMsg("non-zeroth frame tests positive for IsFrameZero -- that "
279 "shouldn't happen.");
283 if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) {
284 m_frame_type = eNotAValidFrame;
285 UnwindLogMsg("Could not get next frame, marking this frame as invalid.");
288 if (!m_thread.GetRegisterContext()) {
289 m_frame_type = eNotAValidFrame;
290 UnwindLogMsg("Could not get register context for this thread, marking this "
291 "frame as invalid.");
296 if (!ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) {
297 UnwindLogMsg("could not get pc value");
298 m_frame_type = eNotAValidFrame;
302 ExecutionContext exe_ctx(m_thread.shared_from_this());
303 Process *process = exe_ctx.GetProcessPtr();
304 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
305 // this will strip bit zero in case we read a PC from memory or from the LR.
306 ABI *abi = process->GetABI().get();
308 pc = abi->FixCodeAddress(pc);
311 UnwindLogMsg("pc = 0x%" PRIx64, pc);
313 if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP, reg_val))
314 UnwindLogMsg("fp = 0x%" PRIx64, reg_val);
315 if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP, reg_val))
316 UnwindLogMsg("sp = 0x%" PRIx64, reg_val);
319 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap
321 bool above_trap_handler = false;
322 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
323 GetNextFrame()->IsTrapHandlerFrame())
324 above_trap_handler = true;
326 if (pc == 0 || pc == 0x1) {
327 if (!above_trap_handler) {
328 m_frame_type = eNotAValidFrame;
329 UnwindLogMsg("this frame has a pc of 0x0");
334 const bool allow_section_end = true;
335 m_current_pc.SetLoadAddress(pc, &process->GetTarget(), allow_section_end);
337 // If we don't have a Module for some reason, we're not going to find
338 // symbol/function information - just stick in some reasonable defaults and
339 // hope we can unwind past this frame.
340 ModuleSP pc_module_sp(m_current_pc.GetModule());
341 if (!m_current_pc.IsValid() || !pc_module_sp) {
342 UnwindLogMsg("using architectural default unwind method");
344 // Test the pc value to see if we know it's in an unmapped/non-executable
346 uint32_t permissions;
347 if (process->GetLoadAddressPermissions(pc, permissions) &&
348 (permissions & ePermissionsExecutable) == 0) {
349 // If this is the second frame off the stack, we may have unwound the
350 // first frame incorrectly. But using the architecture default unwind
351 // plan may get us back on track -- albeit possibly skipping a real
352 // frame. Give this frame a clearly-invalid pc and see if we can get any
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
363 // we're 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 (!ReadFrameAddress(row_register_kind, row->GetCFAValue(), 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 ReadFrameAddress(row_register_kind, row->GetAFAValue(), m_afa);
396 // A couple of sanity checks..
397 if (m_cfa == LLDB_INVALID_ADDRESS || m_cfa == 0 || m_cfa == 1) {
398 UnwindLogMsg("could not find a valid cfa address");
399 m_frame_type = eNotAValidFrame;
403 // m_cfa should point into the stack memory; if we can query memory
404 // region permissions, see if the memory is allocated & readable.
405 if (process->GetLoadAddressPermissions(m_cfa, permissions) &&
406 (permissions & ePermissionsReadable) == 0) {
407 m_frame_type = eNotAValidFrame;
409 "the CFA points to a region of memory that is not readable");
413 UnwindLogMsg("could not find a row for function offset zero");
414 m_frame_type = eNotAValidFrame;
418 if (CheckIfLoopingStack()) {
419 TryFallbackUnwindPlan();
420 if (CheckIfLoopingStack()) {
421 UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
422 "looping - stopping");
423 m_frame_type = eNotAValidFrame;
428 UnwindLogMsg("initialized frame cfa is 0x%" PRIx64 " afa is 0x%" PRIx64,
429 (uint64_t)m_cfa, (uint64_t)m_afa);
432 m_frame_type = eNotAValidFrame;
433 UnwindLogMsg("could not find any symbol for this pc, or a default unwind "
434 "plan, to continue unwind.");
438 bool resolve_tail_call_address = false; // m_current_pc can be one past the
439 // address range of the function...
440 // If the saved pc does not point to a function/symbol because it is beyond
441 // the bounds of the correct function and there's no symbol there, we do
442 // *not* want ResolveSymbolContextForAddress to back up the pc by 1, because
443 // then we might not find the correct unwind information later. Instead, let
444 // ResolveSymbolContextForAddress fail, and handle the case via
445 // decr_pc_and_recompute_addr_range below.
446 const SymbolContextItem resolve_scope =
447 eSymbolContextFunction | eSymbolContextSymbol;
448 uint32_t resolved_scope = pc_module_sp->ResolveSymbolContextForAddress(
449 m_current_pc, resolve_scope, m_sym_ctx, resolve_tail_call_address);
451 // We require either a symbol or function in the symbols context to be
452 // successfully filled in or this context is of no use to us.
453 if (resolve_scope & resolved_scope) {
454 m_sym_ctx_valid = true;
457 if (m_sym_ctx.symbol) {
458 UnwindLogMsg("with pc value of 0x%" PRIx64 ", symbol name is '%s'", pc,
459 GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
460 } else if (m_sym_ctx.function) {
461 UnwindLogMsg("with pc value of 0x%" PRIx64 ", function name is '%s'", pc,
462 GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
464 UnwindLogMsg("with pc value of 0x%" PRIx64
465 ", no symbol/function name is known.",
469 AddressRange addr_range;
470 if (!m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range)) {
471 m_sym_ctx_valid = false;
474 bool decr_pc_and_recompute_addr_range = false;
476 // If the symbol lookup failed...
477 if (!m_sym_ctx_valid)
478 decr_pc_and_recompute_addr_range = true;
480 // Or if we're in the middle of the stack (and not "above" an asynchronous
481 // event like sigtramp), and our "current" pc is the start of a function...
482 if (GetNextFrame()->m_frame_type != eTrapHandlerFrame &&
483 GetNextFrame()->m_frame_type != eDebuggerFrame &&
485 (addr_range.GetBaseAddress().IsValid() &&
486 addr_range.GetBaseAddress().GetSection() == m_current_pc.GetSection() &&
487 addr_range.GetBaseAddress().GetOffset() == m_current_pc.GetOffset()))) {
488 decr_pc_and_recompute_addr_range = true;
491 // We need to back up the pc by 1 byte and re-search for the Symbol to handle
492 // the case where the "saved pc" value is pointing to the next function, e.g.
493 // if a function ends with a CALL instruction.
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 SymbolContextItem resolve_scope =
506 eSymbolContextFunction | eSymbolContextSymbol;
508 ModuleSP temporary_module_sp = temporary_pc.GetModule();
509 if (temporary_module_sp &&
510 temporary_module_sp->ResolveSymbolContextForAddress(
511 temporary_pc, resolve_scope, m_sym_ctx) &
513 if (m_sym_ctx.GetAddressRange(resolve_scope, 0, false, addr_range))
514 m_sym_ctx_valid = true;
516 UnwindLogMsg("Symbol is now %s",
517 GetSymbolOrFunctionName(m_sym_ctx).AsCString(""));
520 // If we were able to find a symbol/function, set addr_range_ptr to the
521 // bounds of that symbol/function. else treat the current pc value as the
522 // start_pc and record no offset.
523 if (addr_range.GetBaseAddress().IsValid()) {
524 m_start_pc = addr_range.GetBaseAddress();
525 m_current_offset = pc - m_start_pc.GetLoadAddress(&process->GetTarget());
526 m_current_offset_backed_up_one = m_current_offset;
527 if (decr_pc_and_recompute_addr_range &&
528 m_current_offset_backed_up_one > 0) {
529 m_current_offset_backed_up_one--;
530 if (m_sym_ctx_valid) {
531 m_current_pc.SetLoadAddress(pc - 1, &process->GetTarget());
535 m_start_pc = m_current_pc;
536 m_current_offset = -1;
537 m_current_offset_backed_up_one = -1;
540 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
541 m_frame_type = eTrapHandlerFrame;
543 // FIXME: Detect eDebuggerFrame here.
544 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
546 m_frame_type = eNormalFrame;
550 // We've set m_frame_type and m_sym_ctx before this call.
551 m_fast_unwind_plan_sp = GetFastUnwindPlanForFrame();
553 UnwindPlan::RowSP active_row;
554 RegisterKind row_register_kind = eRegisterKindGeneric;
556 // Try to get by with just the fast UnwindPlan if possible - the full
557 // UnwindPlan may be expensive to get (e.g. if we have to parse the entire
558 // eh_frame section of an ObjectFile for the first time.)
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 (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
594 UnwindLogMsg("failed to get cfa");
595 m_frame_type = eNotAValidFrame;
599 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
601 UnwindLogMsg("m_cfa = 0x%" PRIx64 " m_afa = 0x%" PRIx64, m_cfa, m_afa);
603 if (CheckIfLoopingStack()) {
604 TryFallbackUnwindPlan();
605 if (CheckIfLoopingStack()) {
606 UnwindLogMsg("same CFA address as next frame, assuming the unwind is "
607 "looping - stopping");
608 m_frame_type = eNotAValidFrame;
613 UnwindLogMsg("initialized frame current pc is 0x%" PRIx64
614 " cfa is 0x%" PRIx64 " afa is 0x%" PRIx64,
615 (uint64_t)m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()),
620 bool RegisterContextLLDB::CheckIfLoopingStack() {
621 // If we have a bad stack setup, we can get the same CFA value multiple times
622 // -- or even more devious, we can actually oscillate between two CFA values.
623 // Detect that here and break out to avoid a possible infinite loop in lldb
624 // trying to unwind the stack. To detect when we have the same CFA value
625 // multiple times, we compare the
626 // CFA of the current
627 // frame with the 2nd next frame because in some specail case (e.g. signal
628 // hanlders, hand written assembly without ABI compiance) we can have 2
629 // frames with the same
631 // can have arbitrary number of frames with the same CFA, but more then 2 is
632 // very very unlikely)
634 RegisterContextLLDB::SharedPtr next_frame = GetNextFrame();
636 RegisterContextLLDB::SharedPtr next_next_frame = next_frame->GetNextFrame();
637 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
638 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) {
639 if (next_next_frame_cfa == m_cfa) {
640 // We have a loop in the stack unwind
648 bool RegisterContextLLDB::IsFrameZero() const { return m_frame_number == 0; }
650 // Find a fast unwind plan for this frame, if possible.
652 // On entry to this method,
654 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
655 // if either of those are correct,
656 // 2. m_sym_ctx should already be filled in, and
657 // 3. m_current_pc should have the current pc value for this frame
658 // 4. m_current_offset_backed_up_one should have the current byte offset into
659 // the function, maybe backed up by 1, -1 if unknown
661 UnwindPlanSP RegisterContextLLDB::GetFastUnwindPlanForFrame() {
662 UnwindPlanSP unwind_plan_sp;
663 ModuleSP pc_module_sp(m_current_pc.GetModule());
665 if (!m_current_pc.IsValid() || !pc_module_sp ||
666 pc_module_sp->GetObjectFile() == NULL)
667 return unwind_plan_sp;
670 return unwind_plan_sp;
672 FuncUnwindersSP func_unwinders_sp(
673 pc_module_sp->GetObjectFile()
675 .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx));
676 if (!func_unwinders_sp)
677 return unwind_plan_sp;
679 // If we're in _sigtramp(), unwinding past this frame requires special
681 if (m_frame_type == eTrapHandlerFrame || m_frame_type == eDebuggerFrame)
682 return unwind_plan_sp;
684 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanFastUnwind(
685 *m_thread.CalculateTarget(), m_thread);
686 if (unwind_plan_sp) {
687 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
688 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
689 if (log && log->GetVerbose()) {
690 if (m_fast_unwind_plan_sp)
691 UnwindLogMsgVerbose("frame, and has a fast UnwindPlan");
693 UnwindLogMsgVerbose("frame");
695 m_frame_type = eNormalFrame;
696 return unwind_plan_sp;
698 unwind_plan_sp.reset();
701 return unwind_plan_sp;
704 // On entry to this method,
706 // 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
707 // if either of those are correct,
708 // 2. m_sym_ctx should already be filled in, and
709 // 3. m_current_pc should have the current pc value for this frame
710 // 4. m_current_offset_backed_up_one should have the current byte offset into
711 // the function, maybe backed up by 1, -1 if unknown
713 UnwindPlanSP RegisterContextLLDB::GetFullUnwindPlanForFrame() {
714 UnwindPlanSP unwind_plan_sp;
715 UnwindPlanSP arch_default_unwind_plan_sp;
716 ExecutionContext exe_ctx(m_thread.shared_from_this());
717 Process *process = exe_ctx.GetProcessPtr();
718 ABI *abi = process ? process->GetABI().get() : NULL;
720 arch_default_unwind_plan_sp.reset(
721 new UnwindPlan(lldb::eRegisterKindGeneric));
722 abi->CreateDefaultUnwindPlan(*arch_default_unwind_plan_sp);
725 "unable to get architectural default UnwindPlan from ABI plugin");
728 bool behaves_like_zeroth_frame = false;
729 if (IsFrameZero() || GetNextFrame()->m_frame_type == eTrapHandlerFrame ||
730 GetNextFrame()->m_frame_type == eDebuggerFrame) {
731 behaves_like_zeroth_frame = true;
732 // If this frame behaves like a 0th frame (currently executing or
733 // interrupted asynchronously), all registers can be retrieved.
734 m_all_registers_available = true;
737 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
738 // so the pc is 0x0 in the zeroth frame, we need to use the "unwind at first
739 // instruction" arch default UnwindPlan Also, if this Process can report on
740 // memory region attributes, any non-executable region means we jumped
741 // through a bad function pointer - handle the same way as 0x0. Note, if we
742 // have a symbol context & a symbol, we don't want to follow this code path.
743 // This is for jumping to memory regions without any information available.
745 if ((!m_sym_ctx_valid ||
746 (m_sym_ctx.function == NULL && m_sym_ctx.symbol == NULL)) &&
747 behaves_like_zeroth_frame && m_current_pc.IsValid()) {
748 uint32_t permissions;
749 addr_t current_pc_addr =
750 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr());
751 if (current_pc_addr == 0 ||
753 process->GetLoadAddressPermissions(current_pc_addr, permissions) &&
754 (permissions & ePermissionsExecutable) == 0)) {
756 unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
757 abi->CreateFunctionEntryUnwindPlan(*unwind_plan_sp);
758 m_frame_type = eNormalFrame;
759 return unwind_plan_sp;
764 // No Module for the current pc, try using the architecture default unwind.
765 ModuleSP pc_module_sp(m_current_pc.GetModule());
766 if (!m_current_pc.IsValid() || !pc_module_sp ||
767 pc_module_sp->GetObjectFile() == NULL) {
768 m_frame_type = eNormalFrame;
769 return arch_default_unwind_plan_sp;
772 FuncUnwindersSP func_unwinders_sp;
773 if (m_sym_ctx_valid) {
775 pc_module_sp->GetObjectFile()
777 .GetFuncUnwindersContainingAddress(m_current_pc, m_sym_ctx);
780 // No FuncUnwinders available for this pc (stripped function symbols, lldb
781 // could not augment its function table with another source, like
782 // LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). See if eh_frame or the
783 // .ARM.exidx tables have unwind information for this address, else fall back
784 // to the architectural default unwind.
785 if (!func_unwinders_sp) {
786 m_frame_type = eNormalFrame;
788 if (!pc_module_sp || !pc_module_sp->GetObjectFile() ||
789 !m_current_pc.IsValid())
790 return arch_default_unwind_plan_sp;
792 // Even with -fomit-frame-pointer, we can try eh_frame to get back on
794 DWARFCallFrameInfo *eh_frame =
795 pc_module_sp->GetObjectFile()->GetUnwindTable().GetEHFrameInfo();
797 unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
798 if (eh_frame->GetUnwindPlan(m_current_pc, *unwind_plan_sp))
799 return unwind_plan_sp;
801 unwind_plan_sp.reset();
804 ArmUnwindInfo *arm_exidx =
805 pc_module_sp->GetObjectFile()->GetUnwindTable().GetArmUnwindInfo();
807 unwind_plan_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
808 if (arm_exidx->GetUnwindPlan(exe_ctx.GetTargetRef(), m_current_pc,
810 return unwind_plan_sp;
812 unwind_plan_sp.reset();
815 return arch_default_unwind_plan_sp;
818 // If we're in _sigtramp(), unwinding past this frame requires special
819 // knowledge. On Mac OS X this knowledge is properly encoded in the eh_frame
820 // section, so prefer that if available. On other platforms we may need to
821 // provide a platform-specific UnwindPlan which encodes the details of how to
822 // unwind out of sigtramp.
823 if (m_frame_type == eTrapHandlerFrame && process) {
824 m_fast_unwind_plan_sp.reset();
825 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan(
826 process->GetTarget(), m_current_offset_backed_up_one);
827 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc) &&
828 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) {
829 return unwind_plan_sp;
833 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
834 // even when it's frame zero This comes up if we have hand-written functions
835 // in a Module and hand-written eh_frame. The assembly instruction
836 // inspection may fail and the eh_frame CFI were probably written with some
837 // care to do the right thing. It'd be nice if there was a way to ask the
838 // eh_frame directly if it is asynchronous (can be trusted at every
839 // instruction point) or synchronous (the normal case - only at call sites).
841 if (process && process->GetDynamicLoader() &&
842 process->GetDynamicLoader()->AlwaysRelyOnEHUnwindInfo(m_sym_ctx)) {
843 // We must specifically call the GetEHFrameUnwindPlan() method here --
844 // normally we would call GetUnwindPlanAtCallSite() -- because CallSite may
845 // return an unwind plan sourced from either eh_frame (that's what we
846 // intend) or compact unwind (this won't work)
847 unwind_plan_sp = func_unwinders_sp->GetEHFrameUnwindPlan(
848 process->GetTarget(), m_current_offset_backed_up_one);
849 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
850 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan because the "
851 "DynamicLoader suggested we prefer it",
852 unwind_plan_sp->GetSourceName().GetCString());
853 return unwind_plan_sp;
857 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
858 // the assembly language instructions
859 if (behaves_like_zeroth_frame && process) {
860 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
861 process->GetTarget(), m_thread, m_current_offset_backed_up_one);
862 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
863 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
864 // We probably have an UnwindPlan created by inspecting assembly
865 // instructions. The assembly profilers work really well with compiler-
866 // generated functions but hand- written assembly can be problematic.
867 // We set the eh_frame based unwind plan as our fallback unwind plan if
868 // instruction emulation doesn't work out even for non call sites if it
869 // is available and use the architecture default unwind plan if it is
870 // not available. The eh_frame unwind plan is more reliable even on non
871 // call sites then the architecture default plan and for hand written
872 // assembly code it is often written in a way that it valid at all
873 // location what helps in the most common cases when the instruction
875 UnwindPlanSP call_site_unwind_plan =
876 func_unwinders_sp->GetUnwindPlanAtCallSite(
877 process->GetTarget(), m_current_offset_backed_up_one);
878 if (call_site_unwind_plan &&
879 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
880 call_site_unwind_plan->GetSourceName() !=
881 unwind_plan_sp->GetSourceName()) {
882 m_fallback_unwind_plan_sp = call_site_unwind_plan;
884 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
887 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
888 unwind_plan_sp->GetSourceName().GetCString());
889 return unwind_plan_sp;
893 // Typically this is unwind info from an eh_frame section intended for
894 // exception handling; only valid at call sites
896 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite(
897 process->GetTarget(), m_current_offset_backed_up_one);
899 int valid_offset = -1;
900 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) {
901 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
902 unwind_plan_sp->GetSourceName().GetCString());
903 return unwind_plan_sp;
906 // We'd prefer to use an UnwindPlan intended for call sites when we're at a
907 // call site but if we've struck out on that, fall back to using the non-
908 // call-site assembly inspection UnwindPlan if possible.
910 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
911 process->GetTarget(), m_thread, m_current_offset_backed_up_one);
913 if (unwind_plan_sp &&
914 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
915 // We probably have an UnwindPlan created by inspecting assembly
916 // instructions. The assembly profilers work really well with compiler-
917 // generated functions but hand- written assembly can be problematic. We
918 // set the eh_frame based unwind plan as our fallback unwind plan if
919 // instruction emulation doesn't work out even for non call sites if it is
920 // available and use the architecture default unwind plan if it is not
921 // available. The eh_frame unwind plan is more reliable even on non call
922 // sites then the architecture default plan and for hand written assembly
923 // code it is often written in a way that it valid at all location what
924 // helps in the most common cases when the instruction emulation fails.
925 UnwindPlanSP call_site_unwind_plan =
926 func_unwinders_sp->GetUnwindPlanAtCallSite(
927 process->GetTarget(), m_current_offset_backed_up_one);
928 if (call_site_unwind_plan &&
929 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
930 call_site_unwind_plan->GetSourceName() !=
931 unwind_plan_sp->GetSourceName()) {
932 m_fallback_unwind_plan_sp = call_site_unwind_plan;
934 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
938 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp, valid_offset)) {
939 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
940 unwind_plan_sp->GetSourceName().GetCString());
941 return unwind_plan_sp;
944 // If we're on the first instruction of a function, and we have an
945 // architectural default UnwindPlan for the initial instruction of a
946 // function, use that.
947 if (m_current_offset_backed_up_one == 0) {
949 func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
951 if (unwind_plan_sp) {
952 UnwindLogMsgVerbose("frame uses %s for full UnwindPlan",
953 unwind_plan_sp->GetSourceName().GetCString());
954 return unwind_plan_sp;
958 // If nothing else, use the architectural default UnwindPlan and hope that
960 if (arch_default_unwind_plan_sp)
962 "frame uses %s for full UnwindPlan",
963 arch_default_unwind_plan_sp->GetSourceName().GetCString());
966 "Unable to find any UnwindPlan for full unwind of this frame.");
968 return arch_default_unwind_plan_sp;
971 void RegisterContextLLDB::InvalidateAllRegisters() {
972 m_frame_type = eNotAValidFrame;
975 size_t RegisterContextLLDB::GetRegisterCount() {
976 return m_thread.GetRegisterContext()->GetRegisterCount();
979 const RegisterInfo *RegisterContextLLDB::GetRegisterInfoAtIndex(size_t reg) {
980 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex(reg);
983 size_t RegisterContextLLDB::GetRegisterSetCount() {
984 return m_thread.GetRegisterContext()->GetRegisterSetCount();
987 const RegisterSet *RegisterContextLLDB::GetRegisterSet(size_t reg_set) {
988 return m_thread.GetRegisterContext()->GetRegisterSet(reg_set);
991 uint32_t RegisterContextLLDB::ConvertRegisterKindToRegisterNumber(
992 lldb::RegisterKind kind, uint32_t num) {
993 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
997 bool RegisterContextLLDB::ReadRegisterValueFromRegisterLocation(
998 lldb_private::UnwindLLDB::RegisterLocation regloc,
999 const RegisterInfo *reg_info, RegisterValue &value) {
1002 bool success = false;
1004 switch (regloc.type) {
1005 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: {
1006 const RegisterInfo *other_reg_info =
1007 GetRegisterInfoAtIndex(regloc.location.register_number);
1009 if (!other_reg_info)
1013 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1015 case UnwindLLDB::RegisterLocation::eRegisterInRegister: {
1016 const RegisterInfo *other_reg_info =
1017 GetRegisterInfoAtIndex(regloc.location.register_number);
1019 if (!other_reg_info)
1022 if (IsFrameZero()) {
1024 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1026 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1029 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1031 value.SetUInt(regloc.location.inferred_value, reg_info->byte_size);
1034 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1036 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1037 llvm_unreachable("FIXME debugger inferior function call unwind");
1038 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: {
1039 Status error(ReadRegisterValueFromMemory(
1040 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1042 success = error.Success();
1045 llvm_unreachable("Unknown RegisterLocation type.");
1050 bool RegisterContextLLDB::WriteRegisterValueToRegisterLocation(
1051 lldb_private::UnwindLLDB::RegisterLocation regloc,
1052 const RegisterInfo *reg_info, const RegisterValue &value) {
1056 bool success = false;
1058 switch (regloc.type) {
1059 case UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext: {
1060 const RegisterInfo *other_reg_info =
1061 GetRegisterInfoAtIndex(regloc.location.register_number);
1063 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1065 case UnwindLLDB::RegisterLocation::eRegisterInRegister: {
1066 const RegisterInfo *other_reg_info =
1067 GetRegisterInfoAtIndex(regloc.location.register_number);
1068 if (IsFrameZero()) {
1070 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1072 success = GetNextFrame()->WriteRegister(other_reg_info, value);
1075 case UnwindLLDB::RegisterLocation::eRegisterValueInferred:
1076 case UnwindLLDB::RegisterLocation::eRegisterNotSaved:
1078 case UnwindLLDB::RegisterLocation::eRegisterSavedAtHostMemoryLocation:
1079 llvm_unreachable("FIXME debugger inferior function call unwind");
1080 case UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation: {
1081 Status error(WriteRegisterValueToMemory(
1082 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1084 success = error.Success();
1087 llvm_unreachable("Unknown RegisterLocation type.");
1092 bool RegisterContextLLDB::IsValid() const {
1093 return m_frame_type != eNotAValidFrame;
1096 // After the final stack frame in a stack walk we'll get one invalid
1097 // (eNotAValidFrame) stack frame -- one past the end of the stack walk. But
1098 // higher-level code will need to tell the differnece between "the unwind plan
1099 // below this frame failed" versus "we successfully completed the stack walk"
1100 // so this method helps to disambiguate that.
1102 bool RegisterContextLLDB::IsTrapHandlerFrame() const {
1103 return m_frame_type == eTrapHandlerFrame;
1106 // A skip frame is a bogus frame on the stack -- but one where we're likely to
1107 // find a real frame farther
1108 // up the stack if we keep looking. It's always the second frame in an unwind
1109 // (i.e. the first frame after frame zero) where unwinding can be the
1110 // trickiest. Ideally we'll mark up this frame in some way so the user knows
1111 // we're displaying bad data and we may have skipped one frame of their real
1112 // program in the process of getting back on track.
1114 bool RegisterContextLLDB::IsSkipFrame() const {
1115 return m_frame_type == eSkipFrame;
1118 bool RegisterContextLLDB::IsTrapHandlerSymbol(
1119 lldb_private::Process *process,
1120 const lldb_private::SymbolContext &m_sym_ctx) const {
1121 PlatformSP platform_sp(process->GetTarget().GetPlatform());
1123 const std::vector<ConstString> trap_handler_names(
1124 platform_sp->GetTrapHandlerSymbolNames());
1125 for (ConstString name : trap_handler_names) {
1126 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1127 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1132 const std::vector<ConstString> user_specified_trap_handler_names(
1133 m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames());
1134 for (ConstString name : user_specified_trap_handler_names) {
1135 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1136 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1144 // Answer the question: Where did THIS frame save the CALLER frame ("previous"
1145 // frame)'s register value?
1147 enum UnwindLLDB::RegisterSearchResult
1148 RegisterContextLLDB::SavedLocationForRegister(
1149 uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation ®loc) {
1150 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1152 // Have we already found this register location?
1153 if (!m_registers.empty()) {
1155 lldb_private::UnwindLLDB::RegisterLocation>::const_iterator
1157 iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1158 if (iterator != m_registers.end()) {
1159 regloc = iterator->second;
1160 UnwindLogMsg("supplying caller's saved %s (%d)'s location, cached",
1161 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1162 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1166 // Look through the available UnwindPlans for the register location.
1168 UnwindPlan::Row::RegisterLocation unwindplan_regloc;
1169 bool have_unwindplan_regloc = false;
1170 RegisterKind unwindplan_registerkind = kNumRegisterKinds;
1172 if (m_fast_unwind_plan_sp) {
1173 UnwindPlan::RowSP active_row =
1174 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1175 unwindplan_registerkind = m_fast_unwind_plan_sp->GetRegisterKind();
1176 if (regnum.GetAsKind(unwindplan_registerkind) == LLDB_INVALID_REGNUM) {
1177 UnwindLogMsg("could not convert lldb regnum %s (%d) into %d RegisterKind "
1178 "reg numbering scheme",
1179 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1180 (int)unwindplan_registerkind);
1181 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1183 if (active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
1184 unwindplan_regloc)) {
1186 "supplying caller's saved %s (%d)'s location using FastUnwindPlan",
1187 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1188 have_unwindplan_regloc = true;
1192 if (!have_unwindplan_regloc) {
1193 // m_full_unwind_plan_sp being NULL means that we haven't tried to find a
1194 // full UnwindPlan yet
1195 if (!m_full_unwind_plan_sp)
1196 m_full_unwind_plan_sp = GetFullUnwindPlanForFrame();
1198 if (m_full_unwind_plan_sp) {
1199 RegisterNumber pc_regnum(m_thread, eRegisterKindGeneric,
1200 LLDB_REGNUM_GENERIC_PC);
1202 UnwindPlan::RowSP active_row =
1203 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1204 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind();
1206 RegisterNumber return_address_reg;
1208 // If we're fetching the saved pc and this UnwindPlan defines a
1209 // ReturnAddress register (e.g. lr on arm), look for the return address
1210 // register number in the UnwindPlan's row.
1211 if (pc_regnum.IsValid() && pc_regnum == regnum &&
1212 m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1213 LLDB_INVALID_REGNUM) {
1215 return_address_reg.init(
1216 m_thread, m_full_unwind_plan_sp->GetRegisterKind(),
1217 m_full_unwind_plan_sp->GetReturnAddressRegister());
1218 regnum = return_address_reg;
1219 UnwindLogMsg("requested caller's saved PC but this UnwindPlan uses a "
1220 "RA reg; getting %s (%d) instead",
1221 return_address_reg.GetName(),
1222 return_address_reg.GetAsKind(eRegisterKindLLDB));
1224 if (regnum.GetAsKind(unwindplan_registerkind) == LLDB_INVALID_REGNUM) {
1225 if (unwindplan_registerkind == eRegisterKindGeneric) {
1226 UnwindLogMsg("could not convert lldb regnum %s (%d) into "
1227 "eRegisterKindGeneric reg numbering scheme",
1228 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1230 UnwindLogMsg("could not convert lldb regnum %s (%d) into %d "
1231 "RegisterKind reg numbering scheme",
1232 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1233 (int)unwindplan_registerkind);
1235 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1239 if (regnum.IsValid() &&
1240 active_row->GetRegisterInfo(regnum.GetAsKind(unwindplan_registerkind),
1241 unwindplan_regloc)) {
1242 have_unwindplan_regloc = true;
1244 "supplying caller's saved %s (%d)'s location using %s UnwindPlan",
1245 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1246 m_full_unwind_plan_sp->GetSourceName().GetCString());
1249 // This is frame 0 and we're retrieving the PC and it's saved in a Return
1250 // Address register and it hasn't been saved anywhere yet -- that is,
1251 // it's still live in the actual register. Handle this specially.
1253 if (!have_unwindplan_regloc && return_address_reg.IsValid() &&
1255 if (return_address_reg.GetAsKind(eRegisterKindLLDB) !=
1256 LLDB_INVALID_REGNUM) {
1257 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1259 UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1260 new_regloc.location.register_number =
1261 return_address_reg.GetAsKind(eRegisterKindLLDB);
1262 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1263 regloc = new_regloc;
1264 UnwindLogMsg("supplying caller's register %s (%d) from the live "
1265 "RegisterContext at frame 0, saved in %d",
1266 return_address_reg.GetName(),
1267 return_address_reg.GetAsKind(eRegisterKindLLDB),
1268 return_address_reg.GetAsKind(eRegisterKindLLDB));
1269 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1273 // If this architecture stores the return address in a register (it
1274 // defines a Return Address register) and we're on a non-zero stack frame
1275 // and the Full UnwindPlan says that the pc is stored in the
1276 // RA registers (e.g. lr on arm), then we know that the full unwindplan is
1277 // not trustworthy -- this
1278 // is an impossible situation and the instruction emulation code has
1279 // likely been misled. If this stack frame meets those criteria, we need
1280 // to throw away the Full UnwindPlan that the instruction emulation came
1281 // up with and fall back to the architecture's Default UnwindPlan so the
1282 // stack walk can get past this point.
1284 // Special note: If the Full UnwindPlan was generated from the compiler,
1285 // don't second-guess it when we're at a call site location.
1287 // arch_default_ra_regnum is the return address register # in the Full
1288 // UnwindPlan register numbering
1289 RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1290 LLDB_REGNUM_GENERIC_RA);
1292 if (arch_default_ra_regnum.GetAsKind(unwindplan_registerkind) !=
1293 LLDB_INVALID_REGNUM &&
1294 pc_regnum == regnum && unwindplan_regloc.IsInOtherRegister() &&
1295 unwindplan_regloc.GetRegisterNumber() ==
1296 arch_default_ra_regnum.GetAsKind(unwindplan_registerkind) &&
1297 m_full_unwind_plan_sp->GetSourcedFromCompiler() != eLazyBoolYes &&
1298 !m_all_registers_available) {
1299 UnwindLogMsg("%s UnwindPlan tried to restore the pc from the link "
1300 "register but this is a non-zero frame",
1301 m_full_unwind_plan_sp->GetSourceName().GetCString());
1303 // Throw away the full unwindplan; install the arch default unwindplan
1304 if (ForceSwitchToFallbackUnwindPlan()) {
1305 // Update for the possibly new unwind plan
1306 unwindplan_registerkind = m_full_unwind_plan_sp->GetRegisterKind();
1307 UnwindPlan::RowSP active_row =
1308 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1310 // Sanity check: Verify that we can fetch a pc value and CFA value
1311 // with this unwind plan
1313 RegisterNumber arch_default_pc_reg(m_thread, eRegisterKindGeneric,
1314 LLDB_REGNUM_GENERIC_PC);
1315 bool can_fetch_pc_value = false;
1316 bool can_fetch_cfa = false;
1319 if (arch_default_pc_reg.GetAsKind(unwindplan_registerkind) !=
1320 LLDB_INVALID_REGNUM &&
1321 active_row->GetRegisterInfo(
1322 arch_default_pc_reg.GetAsKind(unwindplan_registerkind),
1323 unwindplan_regloc)) {
1324 can_fetch_pc_value = true;
1326 if (ReadFrameAddress(unwindplan_registerkind,
1327 active_row->GetCFAValue(), cfa_value)) {
1328 can_fetch_cfa = true;
1332 have_unwindplan_regloc = can_fetch_pc_value && can_fetch_cfa;
1334 // We were unable to fall back to another unwind plan
1335 have_unwindplan_regloc = false;
1341 ExecutionContext exe_ctx(m_thread.shared_from_this());
1342 Process *process = exe_ctx.GetProcessPtr();
1343 if (!have_unwindplan_regloc) {
1344 // If the UnwindPlan failed to give us an unwind location for this
1345 // register, we may be able to fall back to some ABI-defined default. For
1346 // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1347 // the ABI may set volatile registers to the undefined state.
1348 ABI *abi = process ? process->GetABI().get() : NULL;
1350 const RegisterInfo *reg_info =
1351 GetRegisterInfoAtIndex(regnum.GetAsKind(eRegisterKindLLDB));
1353 abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1355 "supplying caller's saved %s (%d)'s location using ABI default",
1356 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1357 have_unwindplan_regloc = true;
1362 if (!have_unwindplan_regloc) {
1363 if (IsFrameZero()) {
1364 // This is frame 0 - we should return the actual live register context
1366 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1368 UnwindLLDB::RegisterLocation::eRegisterInLiveRegisterContext;
1369 new_regloc.location.register_number = regnum.GetAsKind(eRegisterKindLLDB);
1370 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1371 regloc = new_regloc;
1372 UnwindLogMsg("supplying caller's register %s (%d) from the live "
1373 "RegisterContext at frame 0",
1374 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1375 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1377 std::string unwindplan_name("");
1378 if (m_full_unwind_plan_sp) {
1379 unwindplan_name += "via '";
1380 unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString();
1381 unwindplan_name += "'";
1383 UnwindLogMsg("no save location for %s (%d) %s", regnum.GetName(),
1384 regnum.GetAsKind(eRegisterKindLLDB),
1385 unwindplan_name.c_str());
1387 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1390 // unwindplan_regloc has valid contents about where to retrieve the register
1391 if (unwindplan_regloc.IsUnspecified()) {
1392 lldb_private::UnwindLLDB::RegisterLocation new_regloc;
1393 new_regloc.type = UnwindLLDB::RegisterLocation::eRegisterNotSaved;
1394 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1395 UnwindLogMsg("save location for %s (%d) is unspecified, continue searching",
1396 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1397 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1400 if (unwindplan_regloc.IsUndefined()) {
1402 "did not supply reg location for %s (%d) because it is volatile",
1403 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1404 return UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile;
1407 if (unwindplan_regloc.IsSame()) {
1408 if (!IsFrameZero() &&
1409 (regnum.GetAsKind(eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_PC ||
1410 regnum.GetAsKind(eRegisterKindGeneric) == LLDB_REGNUM_GENERIC_RA)) {
1411 UnwindLogMsg("register %s (%d) is marked as 'IsSame' - it is a pc or "
1412 "return address reg on a non-zero frame -- treat as if we "
1413 "have no information",
1414 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1415 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1417 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1418 regloc.location.register_number = regnum.GetAsKind(eRegisterKindLLDB);
1419 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1421 "supplying caller's register %s (%d), saved in register %s (%d)",
1422 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1423 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1424 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1428 if (unwindplan_regloc.IsCFAPlusOffset()) {
1429 int offset = unwindplan_regloc.GetOffset();
1430 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1431 regloc.location.inferred_value = m_cfa + offset;
1432 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1433 UnwindLogMsg("supplying caller's register %s (%d), value is CFA plus "
1434 "offset %d [value is 0x%" PRIx64 "]",
1435 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1436 regloc.location.inferred_value);
1437 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1440 if (unwindplan_regloc.IsAtCFAPlusOffset()) {
1441 int offset = unwindplan_regloc.GetOffset();
1442 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1443 regloc.location.target_memory_location = m_cfa + offset;
1444 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1445 UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1446 "CFA plus offset %d [saved at 0x%" PRIx64 "]",
1447 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1448 regloc.location.target_memory_location);
1449 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1452 if (unwindplan_regloc.IsAFAPlusOffset()) {
1453 if (m_afa == LLDB_INVALID_ADDRESS)
1454 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1456 int offset = unwindplan_regloc.GetOffset();
1457 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1458 regloc.location.inferred_value = m_afa + offset;
1459 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1460 UnwindLogMsg("supplying caller's register %s (%d), value is AFA plus "
1461 "offset %d [value is 0x%" PRIx64 "]",
1462 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1463 regloc.location.inferred_value);
1464 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1467 if (unwindplan_regloc.IsAtAFAPlusOffset()) {
1468 if (m_afa == LLDB_INVALID_ADDRESS)
1469 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1471 int offset = unwindplan_regloc.GetOffset();
1472 regloc.type = UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1473 regloc.location.target_memory_location = m_afa + offset;
1474 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1475 UnwindLogMsg("supplying caller's register %s (%d) from the stack, saved at "
1476 "AFA plus offset %d [saved at 0x%" PRIx64 "]",
1477 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1478 regloc.location.target_memory_location);
1479 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1482 if (unwindplan_regloc.IsInOtherRegister()) {
1483 uint32_t unwindplan_regnum = unwindplan_regloc.GetRegisterNumber();
1484 RegisterNumber row_regnum(m_thread, unwindplan_registerkind,
1486 if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1487 UnwindLogMsg("could not supply caller's %s (%d) location - was saved in "
1488 "another reg but couldn't convert that regnum",
1489 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1490 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1492 regloc.type = UnwindLLDB::RegisterLocation::eRegisterInRegister;
1493 regloc.location.register_number = row_regnum.GetAsKind(eRegisterKindLLDB);
1494 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1496 "supplying caller's register %s (%d), saved in register %s (%d)",
1497 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1498 row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1499 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1502 if (unwindplan_regloc.IsDWARFExpression() ||
1503 unwindplan_regloc.IsAtDWARFExpression()) {
1504 DataExtractor dwarfdata(unwindplan_regloc.GetDWARFExpressionBytes(),
1505 unwindplan_regloc.GetDWARFExpressionLength(),
1506 process->GetByteOrder(),
1507 process->GetAddressByteSize());
1508 ModuleSP opcode_ctx;
1509 DWARFExpression dwarfexpr(opcode_ctx, dwarfdata, nullptr, 0,
1510 unwindplan_regloc.GetDWARFExpressionLength());
1511 dwarfexpr.SetRegisterKind(unwindplan_registerkind);
1514 if (dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr, result,
1517 val = result.GetScalar().ULongLong();
1518 if (unwindplan_regloc.IsDWARFExpression()) {
1519 regloc.type = UnwindLLDB::RegisterLocation::eRegisterValueInferred;
1520 regloc.location.inferred_value = val;
1521 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1522 UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1523 "(IsDWARFExpression)",
1524 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1525 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1528 UnwindLLDB::RegisterLocation::eRegisterSavedAtMemoryLocation;
1529 regloc.location.target_memory_location = val;
1530 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1531 UnwindLogMsg("supplying caller's register %s (%d) via DWARF expression "
1532 "(IsAtDWARFExpression)",
1533 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1534 return UnwindLLDB::RegisterSearchResult::eRegisterFound;
1537 UnwindLogMsg("tried to use IsDWARFExpression or IsAtDWARFExpression for %s "
1539 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1540 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1543 UnwindLogMsg("no save location for %s (%d) in this stack frame",
1544 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1546 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1549 return UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
1552 // TryFallbackUnwindPlan() -- this method is a little tricky.
1554 // When this is called, the frame above -- the caller frame, the "previous"
1555 // frame -- is invalid or bad.
1557 // Instead of stopping the stack walk here, we'll try a different UnwindPlan
1558 // and see if we can get a valid frame above us.
1560 // This most often happens when an unwind plan based on assembly instruction
1561 // inspection is not correct -- mostly with hand-written assembly functions or
1562 // functions where the stack frame is set up "out of band", e.g. the kernel
1563 // saved the register context and then called an asynchronous trap handler like
1566 // Often in these cases, if we just do a dumb stack walk we'll get past this
1567 // tricky frame and our usual techniques can continue to be used.
1569 bool RegisterContextLLDB::TryFallbackUnwindPlan() {
1570 if (m_fallback_unwind_plan_sp.get() == nullptr)
1573 if (m_full_unwind_plan_sp.get() == nullptr)
1576 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
1577 m_full_unwind_plan_sp->GetSourceName() ==
1578 m_fallback_unwind_plan_sp->GetSourceName()) {
1582 // If a compiler generated unwind plan failed, trying the arch default
1583 // unwindplan isn't going to do any better.
1584 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1587 // Get the caller's pc value and our own CFA value. Swap in the fallback
1588 // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1589 // same, then the fallback unwind plan provides no benefit.
1591 RegisterNumber pc_regnum(m_thread, eRegisterKindGeneric,
1592 LLDB_REGNUM_GENERIC_PC);
1594 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1595 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1596 UnwindLLDB::RegisterLocation regloc;
1597 if (SavedLocationForRegister(pc_regnum.GetAsKind(eRegisterKindLLDB),
1599 UnwindLLDB::RegisterSearchResult::eRegisterFound) {
1600 const RegisterInfo *reg_info =
1601 GetRegisterInfoAtIndex(pc_regnum.GetAsKind(eRegisterKindLLDB));
1603 RegisterValue reg_value;
1604 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1605 old_caller_pc_value = reg_value.GetAsUInt64();
1610 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1611 // impossible register location for the full unwind plan, it may call
1612 // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1613 // unwindplan with the fallback... in short, we're done, we're using the
1614 // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1615 // at the top -- the only way it became nullptr since then is via
1616 // SavedLocationForRegister().
1617 if (m_fallback_unwind_plan_sp.get() == nullptr)
1620 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1621 // this isn't working, we need to restore. We'll also need to save & restore
1622 // the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1623 UnwindPlanSP original_full_unwind_plan_sp = m_full_unwind_plan_sp;
1624 addr_t old_cfa = m_cfa;
1625 addr_t old_afa = m_afa;
1627 m_registers.clear();
1629 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1631 UnwindPlan::RowSP active_row =
1632 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1635 active_row->GetCFAValue().GetValueType() !=
1636 UnwindPlan::Row::FAValue::unspecified) {
1638 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1639 active_row->GetCFAValue(), new_cfa) ||
1640 new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) {
1641 UnwindLogMsg("failed to get cfa with fallback unwindplan");
1642 m_fallback_unwind_plan_sp.reset();
1643 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1648 ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1649 active_row->GetAFAValue(), m_afa);
1651 if (SavedLocationForRegister(pc_regnum.GetAsKind(eRegisterKindLLDB),
1653 UnwindLLDB::RegisterSearchResult::eRegisterFound) {
1654 const RegisterInfo *reg_info =
1655 GetRegisterInfoAtIndex(pc_regnum.GetAsKind(eRegisterKindLLDB));
1657 RegisterValue reg_value;
1658 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
1660 new_caller_pc_value = reg_value.GetAsUInt64();
1665 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
1666 UnwindLogMsg("failed to get a pc value for the caller frame with the "
1667 "fallback unwind plan");
1668 m_fallback_unwind_plan_sp.reset();
1669 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1675 if (old_caller_pc_value == new_caller_pc_value &&
1678 UnwindLogMsg("fallback unwind plan got the same values for this frame "
1679 "CFA and caller frame pc, not using");
1680 m_fallback_unwind_plan_sp.reset();
1681 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1685 UnwindLogMsg("trying to unwind from this function with the UnwindPlan '%s' "
1686 "because UnwindPlan '%s' failed.",
1687 m_fallback_unwind_plan_sp->GetSourceName().GetCString(),
1688 original_full_unwind_plan_sp->GetSourceName().GetCString());
1690 // We've copied the fallback unwind plan into the full - now clear the
1692 m_fallback_unwind_plan_sp.reset();
1698 bool RegisterContextLLDB::ForceSwitchToFallbackUnwindPlan() {
1699 if (m_fallback_unwind_plan_sp.get() == NULL)
1702 if (m_full_unwind_plan_sp.get() == NULL)
1705 if (m_full_unwind_plan_sp.get() == m_fallback_unwind_plan_sp.get() ||
1706 m_full_unwind_plan_sp->GetSourceName() ==
1707 m_fallback_unwind_plan_sp->GetSourceName()) {
1711 UnwindPlan::RowSP active_row =
1712 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1715 active_row->GetCFAValue().GetValueType() !=
1716 UnwindPlan::Row::FAValue::unspecified) {
1718 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1719 active_row->GetCFAValue(), new_cfa) ||
1720 new_cfa == 0 || new_cfa == 1 || new_cfa == LLDB_INVALID_ADDRESS) {
1721 UnwindLogMsg("failed to get cfa with fallback unwindplan");
1722 m_fallback_unwind_plan_sp.reset();
1726 ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1727 active_row->GetAFAValue(), m_afa);
1729 m_full_unwind_plan_sp = m_fallback_unwind_plan_sp;
1730 m_fallback_unwind_plan_sp.reset();
1732 m_registers.clear();
1736 UnwindLogMsg("switched unconditionally to the fallback unwindplan %s",
1737 m_full_unwind_plan_sp->GetSourceName().GetCString());
1743 bool RegisterContextLLDB::ReadFrameAddress(
1744 lldb::RegisterKind row_register_kind, UnwindPlan::Row::FAValue &fa,
1746 RegisterValue reg_value;
1748 address = LLDB_INVALID_ADDRESS;
1749 addr_t cfa_reg_contents;
1751 switch (fa.GetValueType()) {
1752 case UnwindPlan::Row::FAValue::isRegisterDereferenced: {
1753 RegisterNumber cfa_reg(m_thread, row_register_kind,
1754 fa.GetRegisterNumber());
1755 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
1756 const RegisterInfo *reg_info =
1757 GetRegisterInfoAtIndex(cfa_reg.GetAsKind(eRegisterKindLLDB));
1758 RegisterValue reg_value;
1760 Status error = ReadRegisterValueFromMemory(
1761 reg_info, cfa_reg_contents, reg_info->byte_size, reg_value);
1762 if (error.Success()) {
1763 address = reg_value.GetAsUInt64();
1765 "CFA value via dereferencing reg %s (%d): reg has val 0x%" PRIx64
1766 ", CFA value is 0x%" PRIx64,
1767 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1768 cfa_reg_contents, address);
1771 UnwindLogMsg("Tried to deref reg %s (%d) [0x%" PRIx64
1772 "] but memory read failed.",
1773 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1780 case UnwindPlan::Row::FAValue::isRegisterPlusOffset: {
1781 RegisterNumber cfa_reg(m_thread, row_register_kind,
1782 fa.GetRegisterNumber());
1783 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
1784 if (cfa_reg_contents == LLDB_INVALID_ADDRESS || cfa_reg_contents == 0 ||
1785 cfa_reg_contents == 1) {
1787 "Got an invalid CFA register value - reg %s (%d), value 0x%" PRIx64,
1788 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1790 cfa_reg_contents = LLDB_INVALID_ADDRESS;
1793 address = cfa_reg_contents + fa.GetOffset();
1795 "CFA is 0x%" PRIx64 ": Register %s (%d) contents are 0x%" PRIx64
1797 address, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
1798 cfa_reg_contents, fa.GetOffset());
1803 case UnwindPlan::Row::FAValue::isDWARFExpression: {
1804 ExecutionContext exe_ctx(m_thread.shared_from_this());
1805 Process *process = exe_ctx.GetProcessPtr();
1806 DataExtractor dwarfdata(fa.GetDWARFExpressionBytes(),
1807 fa.GetDWARFExpressionLength(),
1808 process->GetByteOrder(),
1809 process->GetAddressByteSize());
1810 ModuleSP opcode_ctx;
1811 DWARFExpression dwarfexpr(opcode_ctx, dwarfdata, nullptr, 0,
1812 fa.GetDWARFExpressionLength());
1813 dwarfexpr.SetRegisterKind(row_register_kind);
1816 if (dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr, result,
1818 address = result.GetScalar().ULongLong();
1820 UnwindLogMsg("CFA value set by DWARF expression is 0x%" PRIx64,
1824 UnwindLogMsg("Failed to set CFA value via DWARF expression: %s",
1834 // Retrieve a general purpose register value for THIS frame, as saved by the
1835 // NEXT frame, i.e. the frame that
1836 // this frame called. e.g.
1839 // bar () { foo (); }
1840 // main () { bar (); }
1842 // stopped in foo() so
1846 // and this RegisterContext is for frame 1 (bar) - if we want to get the pc
1847 // value for frame 1, we need to ask
1848 // where frame 0 (the "next" frame) saved that and retrieve the value.
1850 bool RegisterContextLLDB::ReadGPRValue(lldb::RegisterKind register_kind,
1851 uint32_t regnum, addr_t &value) {
1855 uint32_t lldb_regnum;
1856 if (register_kind == eRegisterKindLLDB) {
1857 lldb_regnum = regnum;
1858 } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
1859 register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
1863 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
1864 RegisterValue reg_value;
1865 // if this is frame 0 (currently executing frame), get the requested reg
1866 // contents from the actual thread registers
1867 if (IsFrameZero()) {
1868 if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
1869 value = reg_value.GetAsUInt64();
1875 bool pc_register = false;
1876 uint32_t generic_regnum;
1877 if (register_kind == eRegisterKindGeneric &&
1878 (regnum == LLDB_REGNUM_GENERIC_PC || regnum == LLDB_REGNUM_GENERIC_RA)) {
1880 } else if (m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
1881 register_kind, regnum, eRegisterKindGeneric, generic_regnum) &&
1882 (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
1883 generic_regnum == LLDB_REGNUM_GENERIC_RA)) {
1887 lldb_private::UnwindLLDB::RegisterLocation regloc;
1888 if (!m_parent_unwind.SearchForSavedLocationForRegister(
1889 lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
1892 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1893 value = reg_value.GetAsUInt64();
1899 bool RegisterContextLLDB::ReadGPRValue(const RegisterNumber ®num,
1901 return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
1905 // Find the value of a register in THIS frame
1907 bool RegisterContextLLDB::ReadRegister(const RegisterInfo *reg_info,
1908 RegisterValue &value) {
1912 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1913 UnwindLogMsgVerbose("looking for register saved location for reg %d",
1916 // If this is the 0th frame, hand this over to the live register context
1917 if (IsFrameZero()) {
1918 UnwindLogMsgVerbose("passing along to the live register context for reg %d",
1920 return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
1923 bool is_pc_regnum = false;
1924 if (reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_PC ||
1925 reg_info->kinds[eRegisterKindGeneric] == LLDB_REGNUM_GENERIC_RA) {
1926 is_pc_regnum = true;
1929 lldb_private::UnwindLLDB::RegisterLocation regloc;
1930 // Find out where the NEXT frame saved THIS frame's register contents
1931 if (!m_parent_unwind.SearchForSavedLocationForRegister(
1932 lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
1935 return ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
1938 bool RegisterContextLLDB::WriteRegister(const RegisterInfo *reg_info,
1939 const RegisterValue &value) {
1943 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
1944 UnwindLogMsgVerbose("looking for register saved location for reg %d",
1947 // If this is the 0th frame, hand this over to the live register context
1948 if (IsFrameZero()) {
1949 UnwindLogMsgVerbose("passing along to the live register context for reg %d",
1951 return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
1954 lldb_private::UnwindLLDB::RegisterLocation regloc;
1955 // Find out where the NEXT frame saved THIS frame's register contents
1956 if (!m_parent_unwind.SearchForSavedLocationForRegister(
1957 lldb_regnum, regloc, m_frame_number - 1, false))
1960 return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
1963 // Don't need to implement this one
1964 bool RegisterContextLLDB::ReadAllRegisterValues(lldb::DataBufferSP &data_sp) {
1968 // Don't need to implement this one
1969 bool RegisterContextLLDB::WriteAllRegisterValues(
1970 const lldb::DataBufferSP &data_sp) {
1974 // Retrieve the pc value for THIS from
1976 bool RegisterContextLLDB::GetCFA(addr_t &cfa) {
1980 if (m_cfa == LLDB_INVALID_ADDRESS) {
1987 RegisterContextLLDB::SharedPtr RegisterContextLLDB::GetNextFrame() const {
1988 RegisterContextLLDB::SharedPtr regctx;
1989 if (m_frame_number == 0)
1991 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
1994 RegisterContextLLDB::SharedPtr RegisterContextLLDB::GetPrevFrame() const {
1995 RegisterContextLLDB::SharedPtr regctx;
1996 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number + 1);
1999 // Retrieve the address of the start of the function of THIS frame
2001 bool RegisterContextLLDB::GetStartPC(addr_t &start_pc) {
2005 if (!m_start_pc.IsValid()) {
2006 bool read_successfully = ReadPC (start_pc);
2007 if (read_successfully)
2009 ProcessSP process_sp (m_thread.GetProcess());
2012 ABI *abi = process_sp->GetABI().get();
2014 start_pc = abi->FixCodeAddress(start_pc);
2017 return read_successfully;
2019 start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2023 // Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2025 bool RegisterContextLLDB::ReadPC(addr_t &pc) {
2029 bool above_trap_handler = false;
2030 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2031 GetNextFrame()->IsTrapHandlerFrame())
2032 above_trap_handler = true;
2034 if (ReadGPRValue(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC, pc)) {
2035 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2036 // indicates the end of a stack walk.
2037 // On the currently executing frame (or such a frame interrupted
2038 // asynchronously by sigtramp et al) this may occur if code has jumped
2039 // through a NULL pointer -- we want to be able to unwind past that frame
2040 // to help find the bug.
2042 ProcessSP process_sp (m_thread.GetProcess());
2045 ABI *abi = process_sp->GetABI().get();
2047 pc = abi->FixCodeAddress(pc);
2050 return !(m_all_registers_available == false &&
2051 above_trap_handler == false && (pc == 0 || pc == 1));
2057 void RegisterContextLLDB::UnwindLogMsg(const char *fmt, ...) {
2058 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
2061 va_start(args, fmt);
2064 if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == NULL) {
2072 log->Printf("%*sth%d/fr%u %s", m_frame_number < 100 ? m_frame_number : 100,
2073 "", m_thread.GetIndexID(), m_frame_number, logmsg);
2078 void RegisterContextLLDB::UnwindLogMsgVerbose(const char *fmt, ...) {
2079 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_UNWIND));
2080 if (log && log->GetVerbose()) {
2082 va_start(args, fmt);
2085 if (vasprintf(&logmsg, fmt, args) == -1 || logmsg == NULL) {
2093 log->Printf("%*sth%d/fr%u %s", m_frame_number < 100 ? m_frame_number : 100,
2094 "", m_thread.GetIndexID(), m_frame_number, logmsg);