LLDB mainline
RegisterContextUnwind.cpp
Go to the documentation of this file.
1//===-- RegisterContextUnwind.cpp -----------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/Core/Address.h"
12#include "lldb/Core/Module.h"
13#include "lldb/Core/Value.h"
21#include "lldb/Symbol/Symbol.h"
24#include "lldb/Target/ABI.h"
29#include "lldb/Target/Process.h"
32#include "lldb/Target/Target.h"
33#include "lldb/Target/Thread.h"
36#include "lldb/Utility/Log.h"
39#include "lldb/lldb-private.h"
40#include "llvm/Support/FormatAdapters.h"
41#include <cassert>
42#include <memory>
43
44using namespace lldb;
45using namespace lldb_private;
46
48 if (sym_ctx.symbol)
49 return sym_ctx.symbol->GetName();
50 else if (sym_ctx.function)
51 return sym_ctx.function->GetName();
52 return ConstString();
53}
54
55static bool CallFrameAddressIsValid(ABISP abi_sp, lldb::addr_t cfa) {
56 if (cfa == LLDB_INVALID_ADDRESS)
57 return false;
58 if (abi_sp)
59 return abi_sp->CallFrameAddressIsValid(cfa);
60 return cfa != 0 && cfa != 1;
61}
62
63#define UNWIND_LOG_IMPL(LOG_FN, log, ...) \
64 LOG_FN(log, "{0}th{1}/fr{2} {3}", \
65 llvm::indent(std::min(m_frame_number, 100U)), m_thread.GetIndexID(), \
66 m_frame_number, llvm::formatv(__VA_ARGS__))
67
68#define UNWIND_LOG(log, ...) UNWIND_LOG_IMPL(LLDB_LOG, log, __VA_ARGS__)
69
70#define UNWIND_LOG_VERBOSE(log, ...) \
71 UNWIND_LOG_IMPL(LLDB_LOG_VERBOSE, log, __VA_ARGS__)
72
74 const SharedPtr &next_frame,
75 SymbolContext &sym_ctx,
76 uint32_t frame_number,
77 UnwindLLDB &unwind_lldb)
78 : RegisterContext(thread, frame_number), m_thread(thread),
85 m_sym_ctx_valid(false), m_frame_number(frame_number), m_registers(),
86 m_parent_unwind(unwind_lldb) {
87 m_sym_ctx.Clear(false);
88 m_sym_ctx_valid = false;
89
90 if (IsFrameZero()) {
92 } else {
94 }
95
96 // This same code exists over in the GetFullUnwindPlanForFrame() but it may
97 // not have been executed yet
98 if (IsFrameZero() || next_frame->m_frame_type == eTrapHandlerFrame ||
99 next_frame->m_frame_type == eDebuggerFrame) {
100 m_all_registers_available = true;
101 }
102}
103
105 std::shared_ptr<const UnwindPlan> unwind_plan_sp) {
106 if (!unwind_plan_sp)
107 return false;
108
109 // check if m_current_pc is valid
110 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
111 // yes - current offset can be used as is
112 return true;
113 }
114
115 // If don't have an offset or we're at the start of the function, we've got
116 // nothing else to try.
118 return false;
119
120 // check pc - 1 to see if it's valid
121 Address pc_minus_one(m_current_pc);
122 pc_minus_one.Slide(-1);
123 if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) {
124 return true;
125 }
126
127 return false;
128}
129
130// Initialize a RegisterContextUnwind which is the first frame of a stack -- the
131// zeroth frame or currently executing frame.
132
134 Log *log = GetLog(LLDBLog::Unwind);
135 ExecutionContext exe_ctx(m_thread.shared_from_this());
136 RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
137
138 if (reg_ctx_sp.get() == nullptr) {
140 UNWIND_LOG(log, "frame does not have a register context");
141 return;
142 }
143
144 addr_t current_pc = reg_ctx_sp->GetPC();
145
146 if (current_pc == LLDB_INVALID_ADDRESS) {
148 UNWIND_LOG(log, "frame does not have a pc");
149 return;
150 }
151
152 Process *process = exe_ctx.GetProcessPtr();
153
154 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
155 // this will strip bit zero in case we read a PC from memory or from the LR.
156 // (which would be a no-op in frame 0 where we get it from the register set,
157 // but still a good idea to make the call here for other ABIs that may
158 // exist.)
159 if (ABISP abi_sp = process->GetABI())
160 current_pc = abi_sp->FixCodeAddress(current_pc);
161
162 std::shared_ptr<const UnwindPlan> lang_runtime_plan_sp =
165 if (lang_runtime_plan_sp.get()) {
166 UNWIND_LOG(log, "This is an async frame");
167 }
168
169 // Initialize m_current_pc, an Address object, based on current_pc, an
170 // addr_t.
171 m_current_pc.SetLoadAddress(current_pc, &process->GetTarget());
172
173 // If we don't have a Module for some reason, we're not going to find
174 // symbol/function information - just stick in some reasonable defaults and
175 // hope we can unwind past this frame.
176 ModuleSP pc_module_sp(m_current_pc.GetModule());
177 if (!m_current_pc.IsValid() || !pc_module_sp) {
178 UNWIND_LOG(log, "using architectural default unwind method");
179 }
180
181 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
182
183 if (m_sym_ctx.symbol) {
184 UNWIND_LOG(log, "with pc value of {0:x}, symbol name is '{1}'", current_pc,
186 } else if (m_sym_ctx.function) {
187 UNWIND_LOG(log, "with pc value of {0:x}, function name is '{1}'",
189 } else {
190 UNWIND_LOG(log, "with pc value of {0:x}, no symbol/function name is known.",
191 current_pc);
192 }
193
194 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
196 } else {
197 // FIXME: Detect eDebuggerFrame here.
199 }
200
201 // If we were able to find a symbol/function, set addr_range to the bounds of
202 // that symbol/function. else treat the current pc value as the start_pc and
203 // record no offset.
204 if (m_sym_ctx_valid) {
205 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
206 if (m_current_pc.GetModule() == m_start_pc.GetModule()) {
208 m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
209 }
211 } else {
213 m_current_offset = std::nullopt;
214 m_current_offset_backed_up_one = std::nullopt;
215 }
216
217 // We've set m_frame_type and m_sym_ctx before these calls.
218
221
222 const UnwindPlan::Row *active_row = nullptr;
223 lldb::RegisterKind row_register_kind = eRegisterKindGeneric;
224
225 // If we have LanguageRuntime UnwindPlan for this unwind, use those
226 // rules to find the caller frame instead of the function's normal
227 // UnwindPlans. The full unwind plan for this frame will be
228 // the LanguageRuntime-provided unwind plan, and there will not be a
229 // fast unwind plan.
230 if (lang_runtime_plan_sp.get()) {
231 active_row =
232 lang_runtime_plan_sp->GetRowForFunctionOffset(m_current_offset);
233 row_register_kind = lang_runtime_plan_sp->GetRegisterKind();
234 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(),
235 m_cfa)) {
236 UNWIND_LOG(log, "Cannot set cfa");
237 } else {
238 m_full_unwind_plan_sp = lang_runtime_plan_sp;
239 if (log) {
240 StreamString active_row_strm;
241 active_row->Dump(active_row_strm, lang_runtime_plan_sp.get(), &m_thread,
242 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
243 UNWIND_LOG(log, "async active row: {0}", active_row_strm.GetString());
244 }
245 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
246 UNWIND_LOG(log,
247 "initialized async frame current pc is {0:x} cfa is {1:x} afa "
248 "is {2:x}",
249 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa,
250 m_afa);
251
252 return;
253 }
254 }
255
257 m_full_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
258 active_row =
259 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
260 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
262 if (active_row && log) {
263 StreamString active_row_strm;
264 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
265 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
266 UNWIND_LOG(log, "{0}", active_row_strm.GetString());
267 }
268 }
269
270 if (!active_row) {
271 UNWIND_LOG(log, "could not find an unwindplan row for this frame's pc");
273 return;
274 }
275
276 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
277 // Try the fall back unwind plan since the
278 // full unwind plan failed.
279 FuncUnwindersSP func_unwinders_sp;
280 std::shared_ptr<const UnwindPlan> call_site_unwind_plan;
281 bool cfa_status = false;
282
283 if (m_sym_ctx_valid) {
284 func_unwinders_sp =
285 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
287 }
288
289 if (func_unwinders_sp.get() != nullptr)
290 call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(
291 process->GetTarget(), m_thread);
292
293 if (call_site_unwind_plan != nullptr) {
294 m_fallback_unwind_plan_sp = call_site_unwind_plan;
296 cfa_status = true;
297 }
298 if (!cfa_status) {
299 UNWIND_LOG(log, "could not read CFA value for first frame.");
301 return;
302 }
303 } else
304 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
305
307 UNWIND_LOG(log,
308 "could not read CFA or AFA values for first frame, not valid.");
310 return;
311 }
312
313 // Give the Architecture a chance to replace the UnwindPlan.
315
316 UNWIND_LOG(log,
317 "initialized frame current pc is {0:x} cfa is {1:x} afa is {2:x} "
318 "using {3} UnwindPlan",
319 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa, m_afa,
320 m_full_unwind_plan_sp->GetSourceName());
321}
322
323// Initialize a RegisterContextUnwind for the non-zeroth frame -- rely on the
324// RegisterContextUnwind "below" it to provide things like its current pc value.
325
327 Log *log = GetLog(LLDBLog::Unwind);
328 if (IsFrameZero()) {
330 UNWIND_LOG(log, "non-zeroth frame tests positive for IsFrameZero -- that "
331 "shouldn't happen.");
332 return;
333 }
334
335 if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) {
337 UNWIND_LOG(log, "Could not get next frame, marking this frame as invalid.");
338 return;
339 }
340 if (!m_thread.GetRegisterContext()) {
342 UNWIND_LOG(log, "Could not get register context for this thread, marking "
343 "this frame as invalid.");
344 return;
345 }
346
347 ExecutionContext exe_ctx(m_thread.shared_from_this());
348 Process *process = exe_ctx.GetProcessPtr();
349
350 // Some languages may have a logical parent stack frame which is
351 // not a real stack frame, but the programmer would consider it to
352 // be the caller of the frame, e.g. Swift asynchronous frames.
353 //
354 // A LanguageRuntime may provide an UnwindPlan that is used in this
355 // stack trace base on the RegisterContext contents, intsead
356 // of the normal UnwindPlans we would use for the return-pc.
357 std::shared_ptr<const UnwindPlan> lang_runtime_plan_sp =
360 if (lang_runtime_plan_sp.get()) {
361 UNWIND_LOG(log, "This is an async frame");
362 }
363
364 addr_t pc;
366 UNWIND_LOG(log, "could not get pc value");
368 return;
369 }
370
371 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
372 // this will strip bit zero in case we read a PC from memory or from the LR.
373 ABISP abi_sp = process->GetABI();
374 if (abi_sp)
375 pc = abi_sp->FixCodeAddress(pc);
376
377 if (log) {
378 UNWIND_LOG(log, "pc = {0:x}", pc);
379 addr_t reg_val;
381 UNWIND_LOG(log, "fp = {0:x}", reg_val);
383 UNWIND_LOG(log, "sp = {0:x}", reg_val);
384 }
385
386 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap
387 // handler function
388 bool above_trap_handler = false;
389 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
391 above_trap_handler = true;
392
393 if (pc == 0 || pc == 0x1) {
394 if (!above_trap_handler) {
396 UNWIND_LOG(log, "this frame has a pc of 0x0");
397 return;
398 }
399 }
400
401 const bool allow_section_end = true;
402 m_current_pc.SetLoadAddress(pc, &process->GetTarget(), allow_section_end);
403
404 // If we don't have a Module for some reason, we're not going to find
405 // symbol/function information - just stick in some reasonable defaults and
406 // hope we can unwind past this frame. If we're above a trap handler,
407 // we may be at a bogus address because we jumped through a bogus function
408 // pointer and trapped, so don't force the arch default unwind plan in that
409 // case.
410 ModuleSP pc_module_sp(m_current_pc.GetModule());
411 if ((!m_current_pc.IsValid() || !pc_module_sp) &&
412 above_trap_handler == false) {
413 UNWIND_LOG(log, "using architectural default unwind method");
414
415 // Test the pc value to see if we know it's in an unmapped/non-executable
416 // region of memory.
417 uint32_t permissions;
418 if (process->GetLoadAddressPermissions(pc, permissions) &&
419 (permissions & ePermissionsExecutable) == 0) {
420 // If this is the second frame off the stack, we may have unwound the
421 // first frame incorrectly. But using the architecture default unwind
422 // plan may get us back on track -- albeit possibly skipping a real
423 // frame. Give this frame a clearly-invalid pc and see if we can get any
424 // further.
425 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
427 UNWIND_LOG(log,
428 "had a pc of {0:x} which is not in executable memory but on "
429 "frame 1 -- allowing it once.",
430 pc);
432 } else {
433 // anywhere other than the second frame, a non-executable pc means
434 // we're off in the weeds -- stop now.
436 UNWIND_LOG(log, "pc is in a non-executable section of memory and this "
437 "isn't the 2nd frame in the stack walk.");
438 return;
439 }
440 }
441
442 if (abi_sp) {
443 m_fast_unwind_plan_sp.reset();
444 m_full_unwind_plan_sp = abi_sp->CreateDefaultUnwindPlan();
445 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
446 {
448 }
450 m_current_offset = std::nullopt;
451 m_current_offset_backed_up_one = std::nullopt;
452 RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
453 if (const UnwindPlan::Row *row =
454 m_full_unwind_plan_sp->GetRowForFunctionOffset(0)) {
455 if (!ReadFrameAddress(row_register_kind, row->GetCFAValue(), m_cfa)) {
456 UNWIND_LOG(log, "failed to get cfa value");
457 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
458 {
460 }
461 return;
462 }
463
464 ReadFrameAddress(row_register_kind, row->GetAFAValue(), m_afa);
465
466 // A couple of sanity checks..
467 if (!CallFrameAddressIsValid(abi_sp, m_cfa)) {
468 UNWIND_LOG(log, "could not find a valid cfa address");
470 return;
471 }
472
473 // m_cfa should point into the stack memory; if we can query memory
474 // region permissions, see if the memory is allocated & readable.
475 if (process->GetLoadAddressPermissions(m_cfa, permissions) &&
476 (permissions & ePermissionsReadable) == 0) {
479 log, "the CFA points to a region of memory that is not readable");
480 return;
481 }
482 } else {
483 UNWIND_LOG(log, "could not find a row for function offset zero");
485 return;
486 }
487
488 if (CheckIfLoopingStack()) {
490 if (CheckIfLoopingStack()) {
491 UNWIND_LOG(log, "same CFA address as next frame, assuming the unwind "
492 "is looping - stopping");
494 return;
495 }
496 }
497
498 // Give the Architecture a chance to replace the UnwindPlan.
500
501 UNWIND_LOG(log, "initialized frame cfa is {0:x} afa is {1:x}", m_cfa,
502 m_afa);
503 return;
504 }
506 UNWIND_LOG(log, "could not find any symbol for this pc, or a default "
507 "unwind plan, to continue unwind.");
508 return;
509 }
510
511 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
512
513 if (m_sym_ctx.symbol) {
514 UNWIND_LOG(log, "with pc value of {0:x}, symbol name is '{1}'", pc,
516 } else if (m_sym_ctx.function) {
517 UNWIND_LOG(log, "with pc value of {0:x}, function name is '{1}'", pc,
519 } else {
520 UNWIND_LOG(log, "with pc value of {0:x}, no symbol/function name is known.",
521 pc);
522 }
523
524 bool decr_pc_and_recompute_addr_range;
525
526 if (!m_sym_ctx_valid) {
527 // Always decrement and recompute if the symbol lookup failed
528 decr_pc_and_recompute_addr_range = true;
531 // Don't decrement if we're "above" an asynchronous event like
532 // sigtramp.
533 decr_pc_and_recompute_addr_range = false;
534 } else if (Address addr = m_sym_ctx.GetFunctionOrSymbolAddress();
535 addr != m_current_pc) {
536 // If our "current" pc isn't the start of a function, decrement the pc
537 // if we're up the stack.
539 decr_pc_and_recompute_addr_range = false;
540 else
541 decr_pc_and_recompute_addr_range = true;
542 } else if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
543 // Signal dispatch may set the return address of the handler it calls to
544 // point to the first byte of a return trampoline (like __kernel_rt_sigreturn),
545 // so do not decrement and recompute if the symbol we already found is a trap
546 // handler.
547 decr_pc_and_recompute_addr_range = false;
548 } else if (m_behaves_like_zeroth_frame) {
549 decr_pc_and_recompute_addr_range = false;
550 } else {
551 // Decrement to find the function containing the call.
552 decr_pc_and_recompute_addr_range = true;
553 }
554
555 // We need to back up the pc by 1 byte and re-search for the Symbol to handle
556 // the case where the "saved pc" value is pointing to the next function, e.g.
557 // if a function ends with a CALL instruction.
558 // FIXME this may need to be an architectural-dependent behavior; if so we'll
559 // need to add a member function
560 // to the ABI plugin and consult that.
561 if (decr_pc_and_recompute_addr_range) {
562 UNWIND_LOG(log,
563 "Backing up the pc value of {0:x} by 1 and re-doing symbol "
564 "lookup; old symbol was {1}",
566 Address temporary_pc;
567 temporary_pc.SetLoadAddress(pc - 1, &process->GetTarget());
568 m_sym_ctx.Clear(false);
570
571 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
572 }
573
574 // If we were able to find a symbol/function, set addr_range_ptr to the
575 // bounds of that symbol/function. else treat the current pc value as the
576 // start_pc and record no offset.
577 if (m_sym_ctx_valid) {
578 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
579 m_current_offset = pc - m_start_pc.GetLoadAddress(&process->GetTarget());
581 if (decr_pc_and_recompute_addr_range &&
584 if (m_sym_ctx_valid) {
585 m_current_pc.SetLoadAddress(pc - 1, &process->GetTarget());
586 }
587 }
588 } else {
590 m_current_offset = std::nullopt;
591 m_current_offset_backed_up_one = std::nullopt;
592 }
593
594 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
596 } else {
597 // FIXME: Detect eDebuggerFrame here.
598 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
599 {
601 }
602 }
603
604 const UnwindPlan::Row *active_row;
605 RegisterKind row_register_kind = eRegisterKindGeneric;
606
607 // If we have LanguageRuntime UnwindPlan for this unwind, use those
608 // rules to find the caller frame instead of the function's normal
609 // UnwindPlans. The full unwind plan for this frame will be
610 // the LanguageRuntime-provided unwind plan, and there will not be a
611 // fast unwind plan.
612 if (lang_runtime_plan_sp.get()) {
613 active_row =
614 lang_runtime_plan_sp->GetRowForFunctionOffset(m_current_offset);
615 row_register_kind = lang_runtime_plan_sp->GetRegisterKind();
616 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(),
617 m_cfa)) {
618 UNWIND_LOG(log, "Cannot set cfa");
619 } else {
620 m_full_unwind_plan_sp = lang_runtime_plan_sp;
621 if (log) {
622 StreamString active_row_strm;
623 active_row->Dump(active_row_strm, lang_runtime_plan_sp.get(), &m_thread,
624 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
625 UNWIND_LOG(log, "async active row: {0}", active_row_strm.GetString());
626 }
627 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
628 UNWIND_LOG(log,
629 "initialized async frame current pc is {0:x} cfa is {1:x} afa "
630 "is {2:x}",
631 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa,
632 m_afa);
633
634 return;
635 }
636 }
637
638 // We've set m_frame_type and m_sym_ctx before this call.
640
641 // Try to get by with just the fast UnwindPlan if possible - the full
642 // UnwindPlan may be expensive to get (e.g. if we have to parse the entire
643 // eh_frame section of an ObjectFile for the first time.)
644
646 m_fast_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
647 active_row =
648 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
649 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind();
651 if (active_row && log) {
652 StreamString active_row_strm;
653 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread,
654 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
655 UNWIND_LOG(log, "Using fast unwind plan '{0}'",
656 m_fast_unwind_plan_sp->GetSourceName());
657 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
658 }
659 } else {
662 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset(
664 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
666 if (active_row && log) {
667 StreamString active_row_strm;
668 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
669 &m_thread,
670 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
671 UNWIND_LOG(log, "Using full unwind plan '{0}'",
672 m_full_unwind_plan_sp->GetSourceName());
673 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
674 }
675 }
676 }
677
678 if (!active_row) {
680 UNWIND_LOG(log, "could not find unwind row for this pc");
681 return;
682 }
683
684 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
685 UNWIND_LOG(log, "failed to get cfa");
687 return;
688 }
689
690 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
691
692 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
693
694 if (CheckIfLoopingStack()) {
696 if (CheckIfLoopingStack()) {
697 UNWIND_LOG(log, "same CFA address as next frame, assuming the unwind is "
698 "looping - stopping");
700 return;
701 }
702 }
703
704 // Give the Architecture a chance to replace the UnwindPlan.
706
707 UNWIND_LOG(log,
708 "initialized frame current pc is {0:x} cfa is {1:x} afa is {2:x}",
709 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa, m_afa);
710}
711
713 // If we have a bad stack setup, we can get the same CFA value multiple times
714 // -- or even more devious, we can actually oscillate between two CFA values.
715 // Detect that here and break out to avoid a possible infinite loop in lldb
716 // trying to unwind the stack. To detect when we have the same CFA value
717 // multiple times, we compare the
718 // CFA of the current
719 // frame with the 2nd next frame because in some specail case (e.g. signal
720 // hanlders, hand written assembly without ABI compliance) we can have 2
721 // frames with the same
722 // CFA (in theory we
723 // can have arbitrary number of frames with the same CFA, but more then 2 is
724 // very unlikely)
725
727 if (next_frame) {
728 RegisterContextUnwind::SharedPtr next_next_frame =
729 next_frame->GetNextFrame();
730 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
731 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) {
732 if (next_next_frame_cfa == m_cfa) {
733 // We have a loop in the stack unwind
734 return true;
735 }
736 }
737 }
738 return false;
739}
740
742
744 if (m_frame_number == 0)
745 return true;
747 return true;
748 return false;
749}
750
751// Find a fast unwind plan for this frame, if possible.
752//
753// On entry to this method,
754//
755// 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
756// if either of those are correct,
757// 2. m_sym_ctx should already be filled in, and
758// 3. m_current_pc should have the current pc value for this frame
759// 4. m_current_offset_backed_up_one should have the current byte offset into
760// the function, maybe backed up by 1, std::nullopt if unknown
761
762std::shared_ptr<const UnwindPlan>
764 ModuleSP pc_module_sp(m_current_pc.GetModule());
765
766 if (!m_current_pc.IsValid() || !pc_module_sp ||
767 pc_module_sp->GetObjectFile() == nullptr)
768 return nullptr;
769
770 if (IsFrameZero())
771 return nullptr;
772
773 FuncUnwindersSP func_unwinders_sp(
774 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
776 if (!func_unwinders_sp)
777 return nullptr;
778
779 // If we're in _sigtramp(), unwinding past this frame requires special
780 // knowledge.
782 return nullptr;
783
784 if (std::shared_ptr<const UnwindPlan> unwind_plan_sp =
785 func_unwinders_sp->GetUnwindPlanFastUnwind(
786 *m_thread.CalculateTarget(), m_thread)) {
787 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
789 return unwind_plan_sp;
790 }
791 }
792 return nullptr;
793}
794
795// On entry to this method,
796//
797// 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
798// if either of those are correct,
799// 2. m_sym_ctx should already be filled in, and
800// 3. m_current_pc should have the current pc value for this frame
801// 4. m_current_offset_backed_up_one should have the current byte offset into
802// the function, maybe backed up by 1, std::nullopt if unknown
803
804std::shared_ptr<const UnwindPlan>
806 Log *log = GetLog(LLDBLog::Unwind);
807 std::shared_ptr<const UnwindPlan> arch_default_unwind_plan_sp;
808 ExecutionContext exe_ctx(m_thread.shared_from_this());
809 Process *process = exe_ctx.GetProcessPtr();
810 ABI *abi = process ? process->GetABI().get() : nullptr;
811 if (abi) {
812 arch_default_unwind_plan_sp = abi->CreateDefaultUnwindPlan();
813 } else {
815 log, "unable to get architectural default UnwindPlan from ABI plugin");
816 }
817
821 // If this frame behaves like a 0th frame (currently executing or
822 // interrupted asynchronously), all registers can be retrieved.
824 }
825
826 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
827 // so the pc is 0x0 in the zeroth frame, we need to use the "unwind at first
828 // instruction" arch default UnwindPlan Also, if this Process can report on
829 // memory region attributes, any non-executable region means we jumped
830 // through a bad function pointer - handle the same way as 0x0. Note, if we
831 // have a symbol context & a symbol, we don't want to follow this code path.
832 // This is for jumping to memory regions without any information available.
833
834 if ((!m_sym_ctx_valid ||
835 (m_sym_ctx.function == nullptr && m_sym_ctx.symbol == nullptr)) &&
837 uint32_t permissions;
838 addr_t current_pc_addr =
839 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr());
840 if (current_pc_addr == 0 ||
841 (process &&
842 process->GetLoadAddressPermissions(current_pc_addr, permissions) &&
843 (permissions & ePermissionsExecutable) == 0)) {
844 if (abi) {
846 return abi->CreateFunctionEntryUnwindPlan();
847 }
848 }
849 }
850
851 // No Module for the current pc, try using the architecture default unwind.
852 ModuleSP pc_module_sp(m_current_pc.GetModule());
853 if (!m_current_pc.IsValid() || !pc_module_sp ||
854 pc_module_sp->GetObjectFile() == nullptr) {
856 return arch_default_unwind_plan_sp;
857 }
858
859 FuncUnwindersSP func_unwinders_sp;
860 if (m_sym_ctx_valid) {
861 func_unwinders_sp =
862 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
864 }
865
866 // No FuncUnwinders available for this pc (stripped function symbols, lldb
867 // could not augment its function table with another source, like
868 // LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). See if eh_frame or the
869 // .ARM.exidx tables have unwind information for this address, else fall back
870 // to the architectural default unwind.
871 if (!func_unwinders_sp) {
873
874 if (!pc_module_sp || !pc_module_sp->GetObjectFile() ||
875 !m_current_pc.IsValid())
876 return arch_default_unwind_plan_sp;
877
878 // Even with -fomit-frame-pointer, we can try eh_frame to get back on
879 // track.
880 if (DWARFCallFrameInfo *eh_frame =
881 pc_module_sp->GetUnwindTable().GetEHFrameInfo()) {
882 if (std::unique_ptr<UnwindPlan> plan_up =
883 eh_frame->GetUnwindPlan(m_current_pc))
884 return plan_up;
885 }
886
887 ArmUnwindInfo *arm_exidx =
888 pc_module_sp->GetUnwindTable().GetArmUnwindInfo();
889 if (arm_exidx) {
890 auto unwind_plan_sp =
891 std::make_shared<UnwindPlan>(lldb::eRegisterKindGeneric);
892 if (arm_exidx->GetUnwindPlan(exe_ctx.GetTargetRef(), m_current_pc,
893 *unwind_plan_sp))
894 return unwind_plan_sp;
895 }
896
897 CallFrameInfo *object_file_unwind =
898 pc_module_sp->GetUnwindTable().GetObjectFileUnwindInfo();
899 if (object_file_unwind) {
900 if (std::unique_ptr<UnwindPlan> plan_up =
901 object_file_unwind->GetUnwindPlan(m_current_pc))
902 return plan_up;
903 }
904
905 return arch_default_unwind_plan_sp;
906 }
907
908 if (m_frame_type == eTrapHandlerFrame && process) {
909 m_fast_unwind_plan_sp.reset();
910
911 // On some platforms the unwind information for signal handlers is not
912 // present or correct. Give the platform plugins a chance to provide
913 // substitute plan. Otherwise, use eh_frame.
914 if (m_sym_ctx_valid) {
915 lldb::PlatformSP platform = process->GetTarget().GetPlatform();
916 const ArchSpec arch = process->GetTarget().GetArchitecture();
917 if (auto unwind_plan_sp = platform->GetTrapHandlerUnwindPlan(
919 return unwind_plan_sp;
920 }
921
922 auto unwind_plan_sp =
923 func_unwinders_sp->GetEHFrameUnwindPlan(process->GetTarget());
924 if (!unwind_plan_sp)
925 unwind_plan_sp =
926 func_unwinders_sp->GetObjectFileUnwindPlan(process->GetTarget());
927 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc) &&
928 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) {
929 return unwind_plan_sp;
930 }
931 }
932
933 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
934 // even when it's frame zero This comes up if we have hand-written functions
935 // in a Module and hand-written eh_frame. The assembly instruction
936 // inspection may fail and the eh_frame CFI were probably written with some
937 // care to do the right thing. It'd be nice if there was a way to ask the
938 // eh_frame directly if it is asynchronous (can be trusted at every
939 // instruction point) or synchronous (the normal case - only at call sites).
940 // But there is not.
941 if (process && process->GetDynamicLoader() &&
943 // We must specifically call the GetEHFrameUnwindPlan() method here --
944 // normally we would call GetUnwindPlanAtCallSite() -- because CallSite may
945 // return an unwind plan sourced from either eh_frame (that's what we
946 // intend) or compact unwind (this won't work)
947 auto unwind_plan_sp =
948 func_unwinders_sp->GetEHFrameUnwindPlan(process->GetTarget());
949 if (!unwind_plan_sp)
950 unwind_plan_sp =
951 func_unwinders_sp->GetObjectFileUnwindPlan(process->GetTarget());
952 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
954 "frame uses {0} for full UnwindPlan because the "
955 "DynamicLoader suggested we prefer it",
956 unwind_plan_sp->GetSourceName());
957 return unwind_plan_sp;
958 }
959 }
960
961 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
962 // the assembly language instructions
963 if (m_behaves_like_zeroth_frame && process) {
964 auto unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
965 process->GetTarget(), m_thread);
966 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
967 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
968 // We probably have an UnwindPlan created by inspecting assembly
969 // instructions. The assembly profilers work really well with compiler-
970 // generated functions but hand- written assembly can be problematic.
971 // We set the eh_frame based unwind plan as our fallback unwind plan if
972 // instruction emulation doesn't work out even for non call sites if it
973 // is available and use the architecture default unwind plan if it is
974 // not available. The eh_frame unwind plan is more reliable even on non
975 // call sites then the architecture default plan and for hand written
976 // assembly code it is often written in a way that it valid at all
977 // location what helps in the most common cases when the instruction
978 // emulation fails.
979 std::shared_ptr<const UnwindPlan> call_site_unwind_plan =
980 func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(),
981 m_thread);
982 if (call_site_unwind_plan &&
983 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
984 call_site_unwind_plan->GetSourceName() !=
985 unwind_plan_sp->GetSourceName()) {
986 m_fallback_unwind_plan_sp = call_site_unwind_plan;
987 } else {
988 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
989 }
990 }
992 log,
993 "frame uses {0} for full UnwindPlan because this is the non-call "
994 "site unwind plan and this is a zeroth frame",
995 unwind_plan_sp->GetSourceName());
996 return unwind_plan_sp;
997 }
998
999 // If we're on the first instruction of a function, and we have an
1000 // architectural default UnwindPlan for the initial instruction of a
1001 // function, use that.
1002 if (m_current_offset == 0) {
1003 unwind_plan_sp =
1004 func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
1005 m_thread);
1006 if (unwind_plan_sp) {
1008 "frame uses {0} for full UnwindPlan because we are "
1009 "at the first instruction of a function",
1010 unwind_plan_sp->GetSourceName());
1011 return unwind_plan_sp;
1012 }
1013 }
1014 }
1015
1016 std::shared_ptr<const UnwindPlan> unwind_plan_sp;
1017 // Typically this is unwind info from an eh_frame section intended for
1018 // exception handling; only valid at call sites
1019 if (process) {
1020 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite(
1021 process->GetTarget(), m_thread);
1022 }
1023 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp)) {
1025 "frame uses {0} for full UnwindPlan because this is the "
1026 "call-site unwind plan",
1027 unwind_plan_sp->GetSourceName());
1028 return unwind_plan_sp;
1029 }
1030
1031 // We'd prefer to use an UnwindPlan intended for call sites when we're at a
1032 // call site but if we've struck out on that, fall back to using the non-
1033 // call-site assembly inspection UnwindPlan if possible.
1034 if (process) {
1035 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
1036 process->GetTarget(), m_thread);
1037 }
1038 if (unwind_plan_sp &&
1039 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
1040 // We probably have an UnwindPlan created by inspecting assembly
1041 // instructions. The assembly profilers work really well with compiler-
1042 // generated functions but hand- written assembly can be problematic. We
1043 // set the eh_frame based unwind plan as our fallback unwind plan if
1044 // instruction emulation doesn't work out even for non call sites if it is
1045 // available and use the architecture default unwind plan if it is not
1046 // available. The eh_frame unwind plan is more reliable even on non call
1047 // sites then the architecture default plan and for hand written assembly
1048 // code it is often written in a way that it valid at all location what
1049 // helps in the most common cases when the instruction emulation fails.
1050 std::shared_ptr<const UnwindPlan> call_site_unwind_plan =
1051 func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(),
1052 m_thread);
1053 if (call_site_unwind_plan &&
1054 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
1055 call_site_unwind_plan->GetSourceName() !=
1056 unwind_plan_sp->GetSourceName()) {
1057 m_fallback_unwind_plan_sp = call_site_unwind_plan;
1058 } else {
1059 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
1060 }
1061 }
1062
1063 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp)) {
1065 "frame uses {0} for full UnwindPlan because we failed "
1066 "to find a call-site unwind plan that would work",
1067 unwind_plan_sp->GetSourceName());
1068 return unwind_plan_sp;
1069 }
1070
1071 // If nothing else, use the architectural default UnwindPlan and hope that
1072 // does the job.
1073 if (arch_default_unwind_plan_sp)
1075 "frame uses {0} for full UnwindPlan because we are "
1076 "falling back to the arch default plan",
1077 arch_default_unwind_plan_sp->GetSourceName());
1078 else
1079 UNWIND_LOG(log,
1080 "Unable to find any UnwindPlan for full unwind of this frame.");
1081
1082 return arch_default_unwind_plan_sp;
1083}
1084
1088
1090 return m_thread.GetRegisterContext()->GetRegisterCount();
1091}
1092
1094 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex(reg);
1095}
1096
1098 return m_thread.GetRegisterContext()->GetRegisterSetCount();
1099}
1100
1102 return m_thread.GetRegisterContext()->GetRegisterSet(reg_set);
1103}
1104
1106 lldb::RegisterKind kind, uint32_t num) {
1107 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
1108 kind, num);
1109}
1110
1113 const RegisterInfo *reg_info, RegisterValue &value) {
1114 if (!IsValid())
1115 return false;
1116 bool success = false;
1117
1118 switch (regloc.type) {
1120 const RegisterInfo *other_reg_info =
1122
1123 if (!other_reg_info)
1124 return false;
1125
1126 success =
1127 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1128 } break;
1130 const RegisterInfo *other_reg_info =
1132
1133 if (!other_reg_info)
1134 return false;
1135
1136 if (IsFrameZero()) {
1137 success =
1138 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1139 } else {
1140 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1141 }
1142 } break;
1144 auto regnum = regloc.location.reg_plus_offset.register_number;
1145 const RegisterInfo *other_reg_info =
1147
1148 if (!other_reg_info)
1149 return false;
1150
1151 if (IsFrameZero()) {
1152 success =
1153 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1154 } else {
1155 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1156 }
1157 if (success) {
1158 Log *log = GetLog(LLDBLog::Unwind);
1159 UNWIND_LOG(log, "read ({0})'s location", regnum);
1160 value = value.GetAsUInt64(~0ull, &success) +
1162 UNWIND_LOG(log, "success {0}", success ? "yes" : "no");
1163 }
1164 } break;
1166 success =
1167 value.SetUInt(regloc.location.inferred_value, reg_info->byte_size);
1168 break;
1169
1171 break;
1173 llvm_unreachable("FIXME debugger inferior function call unwind");
1176 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1177 value));
1178 success = error.Success();
1179 } break;
1180 default:
1181 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1182 }
1183 return success;
1184}
1185
1188 const RegisterInfo *reg_info, const RegisterValue &value) {
1189 if (!IsValid())
1190 return false;
1191
1192 bool success = false;
1193
1194 switch (regloc.type) {
1196 const RegisterInfo *other_reg_info =
1198 success =
1199 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1200 } break;
1202 const RegisterInfo *other_reg_info =
1204 if (IsFrameZero()) {
1205 success =
1206 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1207 } else {
1208 success = GetNextFrame()->WriteRegister(other_reg_info, value);
1209 }
1210 } break;
1214 break;
1216 llvm_unreachable("FIXME debugger inferior function call unwind");
1219 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1220 value));
1221 success = error.Success();
1222 } break;
1223 default:
1224 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1225 }
1226 return success;
1227}
1228
1232
1233// After the final stack frame in a stack walk we'll get one invalid
1234// (eNotAValidFrame) stack frame -- one past the end of the stack walk. But
1235// higher-level code will need to tell the difference between "the unwind plan
1236// below this frame failed" versus "we successfully completed the stack walk"
1237// so this method helps to disambiguate that.
1238
1242
1243// A skip frame is a bogus frame on the stack -- but one where we're likely to
1244// find a real frame farther
1245// up the stack if we keep looking. It's always the second frame in an unwind
1246// (i.e. the first frame after frame zero) where unwinding can be the
1247// trickiest. Ideally we'll mark up this frame in some way so the user knows
1248// we're displaying bad data and we may have skipped one frame of their real
1249// program in the process of getting back on track.
1250
1254
1256 lldb_private::Process *process,
1258 PlatformSP platform_sp(process->GetTarget().GetPlatform());
1259 if (platform_sp) {
1260 const std::vector<ConstString> trap_handler_names(
1261 platform_sp->GetTrapHandlerSymbolNames());
1262 for (ConstString name : trap_handler_names) {
1263 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1264 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1265 return true;
1266 }
1267 }
1268 }
1269 const std::vector<ConstString> user_specified_trap_handler_names(
1270 m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames());
1271 for (ConstString name : user_specified_trap_handler_names) {
1272 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1273 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1274 return true;
1275 }
1276 }
1277
1278 return false;
1279}
1280
1281// Search this stack frame's UnwindPlans for the AbstractRegisterLocation
1282// for this register.
1283//
1284// \param[in] lldb_regnum
1285// The register number (in the eRegisterKindLLDB register numbering)
1286// we are searching for.
1287//
1288// \param[out] kind
1289// Set to the RegisterKind of the UnwindPlan which is the basis for
1290// the returned AbstractRegisterLocation; if the location is in terms
1291// of another register number, this Kind is needed to interpret it
1292// correctly.
1293//
1294// \return
1295// An empty optional indicaTes that there was an error in processing
1296// the request.
1297//
1298// If there is no unwind rule for a volatile (caller-preserved) register,
1299// the returned AbstractRegisterLocation will be IsUndefined,
1300// indicating that we should stop searching.
1301//
1302// If there is no unwind rule for a non-volatile (callee-preserved)
1303// register, the returned AbstractRegisterLocation will be IsSame.
1304// In frame 0, IsSame means get the value from the live register context.
1305// Else it means to continue descending down the stack to more-live frames
1306// looking for a location/value.
1307//
1308// If an AbstractRegisterLocation is found in an UnwindPlan, that will
1309// be returned, with no consideration of the current ABI rules for
1310// registers. Functions using an alternate ABI calling convention
1311// will work as long as the UnwindPlans are exhaustive about what
1312// registers are volatile/non-volatile.
1313std::optional<UnwindPlan::Row::AbstractRegisterLocation>
1315 lldb::RegisterKind &kind) {
1316 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1317 Log *log = GetLog(LLDBLog::Unwind);
1318
1319 kind = eRegisterKindLLDB;
1321
1322 // First, try to find a register location via the FastUnwindPlan
1324 const UnwindPlan::Row *active_row =
1325 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1326 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1327 UNWIND_LOG(log,
1328 "could not convert lldb regnum {0} ({1}) into {2} "
1329 "RegisterKind reg numbering scheme",
1330 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1331 return {};
1332 }
1333 kind = m_fast_unwind_plan_sp->GetRegisterKind();
1334 // The Fast UnwindPlan typically only provides fp & pc as we move up
1335 // the stack, without requiring additional parsing or memory reads.
1336 // It may mark all other registers as IsUndefined() because, indicating
1337 // that it doesn't know if they were spilled to stack or not.
1338 // If this case, for an IsUndefined register, we should continue on
1339 // to the Full UnwindPlan which may have more accurate information
1340 // about register locations of all registers.
1341 if (active_row &&
1342 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1343 unwindplan_regloc) &&
1344 !unwindplan_regloc.IsUndefined()) {
1345 UNWIND_LOG(
1346 log,
1347 "supplying caller's saved {0} ({1})'s location using FastUnwindPlan",
1348 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1349 return unwindplan_regloc;
1350 }
1351 }
1352
1353 // Second, try to find a register location via the FullUnwindPlan.
1354 bool got_new_full_unwindplan = false;
1355 if (!m_full_unwind_plan_sp) {
1357 got_new_full_unwindplan = true;
1358 }
1362
1363 const UnwindPlan::Row *active_row =
1364 m_full_unwind_plan_sp->GetRowForFunctionOffset(
1366 kind = m_full_unwind_plan_sp->GetRegisterKind();
1367
1368 if (got_new_full_unwindplan && active_row && log) {
1369 StreamString active_row_strm;
1370 ExecutionContext exe_ctx(m_thread.shared_from_this());
1371 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
1372 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
1373 UNWIND_LOG(log, "Using full unwind plan '{0}'",
1374 m_full_unwind_plan_sp->GetSourceName());
1375 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
1376 }
1377
1378 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1379 if (kind == eRegisterKindGeneric)
1380 UNWIND_LOG(log,
1381 "could not convert lldb regnum {0} ({1}) into "
1382 "eRegisterKindGeneric reg numbering scheme",
1383 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1384 else
1385 UNWIND_LOG(log,
1386 "could not convert lldb regnum {0} ({1}) into {2} "
1387 "RegisterKind reg numbering scheme",
1388 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1389 return {};
1390 }
1391
1392 if (regnum.IsValid() && active_row &&
1393 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1394 unwindplan_regloc)) {
1395 UNWIND_LOG(
1396 log,
1397 "supplying caller's saved {0} ({1})'s location using {2} UnwindPlan",
1398 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1399 m_full_unwind_plan_sp->GetSourceName());
1400 return unwindplan_regloc;
1401 }
1402
1403 // When asking for the caller's pc, and did not find a register
1404 // location for PC above in the UnwindPlan. Check if we have a
1405 // Return Address register on this target.
1406 //
1407 // On a Return Address Register architecture like arm/mips/riscv,
1408 // the caller's pc is in the RA register, and will be spilled to
1409 // stack before any other function is called. If no function
1410 // has been called yet, the return address may still be in the
1411 // live RA reg.
1412 //
1413 // There's a lot of variety of what we might see in an UnwindPlan.
1414 // We may have
1415 // ra=IsSame {unncessary}
1416 // ra=StackAddr {caller's return addr spilled to stack}
1417 // or no reg location for pc or ra at all, in a frameless function -
1418 // the caller's return address is in live ra reg.
1419 //
1420 // If a function has been interrupted in a non-call way --
1421 // async signal/sigtramp, or a hardware exception / interrupt / fault --
1422 // then the "pc" and "ra" are two distinct values, and must be
1423 // handled separately. The "pc" is the pc value at the point
1424 // the function was interrupted. The "ra" is the return address
1425 // register value at that point.
1426 // The UnwindPlan for the sigtramp/trap handler will normally have
1427 // register loations for both pc and lr, and so we'll have already
1428 // fetched them above.
1429 if (pc_regnum.IsValid() && pc_regnum == regnum) {
1430 uint32_t return_address_regnum = LLDB_INVALID_REGNUM;
1431
1432 // Get the return address register number from the UnwindPlan
1433 // or the register set definition.
1434 if (m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1436 return_address_regnum =
1437 m_full_unwind_plan_sp->GetReturnAddressRegister();
1438 } else {
1439 RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1441 return_address_regnum = arch_default_ra_regnum.GetAsKind(kind);
1442 }
1443
1444 // This system is using a return address register.
1445 if (return_address_regnum != LLDB_INVALID_REGNUM) {
1446 RegisterNumber return_address_reg;
1447 return_address_reg.init(m_thread,
1448 m_full_unwind_plan_sp->GetRegisterKind(),
1449 return_address_regnum);
1450 UNWIND_LOG(log,
1451 "requested caller's saved PC but this UnwindPlan uses a RA "
1452 "reg; getting {0} ({1}) instead",
1453 return_address_reg.GetName(),
1454 return_address_reg.GetAsKind(eRegisterKindLLDB));
1455
1456 // Do we have a location for the ra register?
1457 if (active_row &&
1458 active_row->GetRegisterInfo(return_address_reg.GetAsKind(kind),
1459 unwindplan_regloc)) {
1460 UNWIND_LOG(log,
1461 "supplying caller's saved {0} ({1})'s location using {2} "
1462 "UnwindPlan",
1463 return_address_reg.GetName(),
1464 return_address_reg.GetAsKind(eRegisterKindLLDB),
1465 m_full_unwind_plan_sp->GetSourceName());
1466 // If we have "ra=IsSame", rewrite to "ra=InRegister(ra)" because the
1467 // calling function thinks it is fetching "pc" and if we return an
1468 // IsSame register location, it will try to read pc.
1469 if (unwindplan_regloc.IsSame())
1470 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1471 return unwindplan_regloc;
1472 } else {
1473 // No unwind rule for the return address reg on frame 0, or an
1474 // interrupted function, means that the caller's address is still in
1475 // RA reg (0th frame) or the trap handler below this one (sigtramp
1476 // etc) has a save location for the RA reg.
1477 if (BehavesLikeZerothFrame()) {
1478 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1479 return unwindplan_regloc;
1480 }
1481 }
1482 }
1483 }
1484 }
1485
1486 ExecutionContext exe_ctx(m_thread.shared_from_this());
1487 Process *process = exe_ctx.GetProcessPtr();
1488
1489 // Third, try finding a register location via the ABI
1490 // FallbackRegisterLocation.
1491 //
1492 // If the UnwindPlan failed to give us an unwind location for this
1493 // register, we may be able to fall back to some ABI-defined default. For
1494 // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1495 // the ABI willset volatile registers to the undefined state.
1496 ABI *abi = process ? process->GetABI().get() : nullptr;
1497 if (abi) {
1498 const RegisterInfo *reg_info =
1500 if (reg_info &&
1501 abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1502 if (!unwindplan_regloc.IsUndefined())
1503 UNWIND_LOG(
1504 log,
1505 "supplying caller's saved {0} ({1})'s location using ABI default",
1506 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1507 // ABI defined volatile registers with no register location
1508 // will be returned as IsUndefined, stopping the search down
1509 // the stack.
1510 return unwindplan_regloc;
1511 }
1512 }
1513
1514 // We have no AbstractRegisterLocation, and the ABI says this is a
1515 // non-volatile / callee-preserved register. Continue down the stack
1516 // or to frame 0 & the live RegisterContext.
1517 std::string unwindplan_name;
1519 unwindplan_name += "via '";
1520 unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString("");
1521 unwindplan_name += "'";
1522 }
1523 UNWIND_LOG(log, "no save location for {0} ({1}) {2}", regnum.GetName(),
1524 regnum.GetAsKind(eRegisterKindLLDB), unwindplan_name);
1525
1526 unwindplan_regloc.SetSame();
1527 return unwindplan_regloc;
1528}
1529
1530// Answer the question: Where did THIS frame save the CALLER frame ("previous"
1531// frame)'s register value?
1532
1535 uint32_t lldb_regnum,
1537 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1538 Log *log = GetLog(LLDBLog::Unwind);
1539
1540 // Have we already found this register location?
1541 if (!m_registers.empty()) {
1542 auto iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1543 if (iterator != m_registers.end()) {
1544 regloc = iterator->second;
1545 UNWIND_LOG(log, "supplying caller's saved {0} ({1})'s location, cached",
1546 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1548 }
1549 }
1550
1551 RegisterKind abs_regkind;
1552 std::optional<UnwindPlan::Row::AbstractRegisterLocation> abs_regloc =
1553 GetAbstractRegisterLocation(lldb_regnum, abs_regkind);
1554
1555 if (!abs_regloc)
1557
1558 if (abs_regloc->IsUndefined()) {
1559 UNWIND_LOG(
1560 log, "did not supply reg location for {0} ({1}) because it is volatile",
1561 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1563 }
1564
1565 ExecutionContext exe_ctx(m_thread.shared_from_this());
1566 Process *process = exe_ctx.GetProcessPtr();
1567 // abs_regloc has valid contents about where to retrieve the register
1568 if (abs_regloc->IsUnspecified()) {
1571 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1572 UNWIND_LOG(log,
1573 "save location for {0} ({1}) is unspecified, continue searching",
1574 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1576 }
1577
1578 if (abs_regloc->IsSame()) {
1579 if (IsFrameZero()) {
1580 regloc.type =
1583 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1584 UNWIND_LOG(log,
1585 "supplying caller's register {0} ({1}) from the live "
1586 "RegisterContext at frame 0",
1587 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1589 }
1590 // PC/RA reg don't follow the usual "callee-saved aka non-volatile" versus
1591 // "caller saved aka volatile" system. A stack frame can provide its caller
1592 // return address, but if we don't find a rule for pc/RA mid-stack, we
1593 // never want to iterate further down the stack looking for it.
1594 // Defensively prevent iterating down the stack for these two.
1595 if (!BehavesLikeZerothFrame() &&
1598 UNWIND_LOG(log,
1599 "register {0} ({1}) is marked as 'IsSame' - it is a pc or "
1600 "return address reg on a frame which does not have all "
1601 "registers available -- treat as if we have no information",
1602 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1604 }
1605
1608 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1609 UNWIND_LOG(log,
1610 "supplying caller's register {0} ({1}) value is unmodified in "
1611 "this frame",
1612 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1614 }
1615
1616 if (abs_regloc->IsCFAPlusOffset()) {
1617 int offset = abs_regloc->GetOffset();
1619 regloc.location.inferred_value = m_cfa + offset;
1620 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1621 UNWIND_LOG(log,
1622 "supplying caller's register {0} ({1}), value is CFA plus "
1623 "offset {2} [value is {3:x}]",
1624 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1625 regloc.location.inferred_value);
1627 }
1628
1629 if (abs_regloc->IsAtCFAPlusOffset()) {
1630 int offset = abs_regloc->GetOffset();
1631 regloc.type =
1633 regloc.location.target_memory_location = m_cfa + offset;
1634 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1635 UNWIND_LOG(log,
1636 "supplying caller's register {0} ({1}) from the stack, saved at "
1637 "CFA plus offset {2} [saved at {3:x}]",
1638 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1641 }
1642
1643 if (abs_regloc->IsAFAPlusOffset()) {
1646
1647 int offset = abs_regloc->GetOffset();
1649 regloc.location.inferred_value = m_afa + offset;
1650 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1651 UNWIND_LOG(log,
1652 "supplying caller's register {0} ({1}), value is AFA plus "
1653 "offset {2} [value is {3:x}]",
1654 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1655 regloc.location.inferred_value);
1657 }
1658
1659 if (abs_regloc->IsAtAFAPlusOffset()) {
1662
1663 int offset = abs_regloc->GetOffset();
1664 regloc.type =
1666 regloc.location.target_memory_location = m_afa + offset;
1667 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1668 UNWIND_LOG(log,
1669 "supplying caller's register {0} ({1}) from the stack, saved at "
1670 "AFA plus offset {2} [saved at {3:x}]",
1671 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1674 }
1675
1676 if (abs_regloc->IsInOtherRegister()) {
1677 RegisterNumber row_regnum(m_thread, abs_regkind,
1678 abs_regloc->GetRegisterNumber());
1679 if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1680 UNWIND_LOG(log,
1681 "could not supply caller's {0} ({1}) location - was saved in "
1682 "another reg but couldn't convert that regnum",
1683 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1685 }
1688 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1689 UNWIND_LOG(
1690 log,
1691 "supplying caller's register {0} ({1}), saved in register {2} ({3})",
1692 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1693 row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1695 }
1696
1697 if (abs_regloc->IsDWARFExpression() || abs_regloc->IsAtDWARFExpression()) {
1698 DataExtractor dwarfdata(abs_regloc->GetDWARFExpressionBytes(),
1699 abs_regloc->GetDWARFExpressionLength(),
1700 process->GetByteOrder(),
1701 process->GetAddressByteSize());
1702 ModuleSP opcode_ctx;
1703 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
1704 dwarfexpr.GetMutableExpressionAtAddress()->SetRegisterKind(abs_regkind);
1705 Value cfa_val = Scalar(m_cfa);
1707 llvm::Expected<Value> result =
1708 dwarfexpr.Evaluate(&exe_ctx, this, 0, &cfa_val, nullptr);
1709 if (!result) {
1710 LLDB_LOG_ERROR(log, result.takeError(),
1711 "DWARF expression failed to evaluate: {0}");
1712 } else {
1713 addr_t val;
1714 val = result->GetScalar().ULongLong();
1715 if (abs_regloc->IsDWARFExpression()) {
1716 regloc.type =
1718 regloc.location.inferred_value = val;
1719 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1720 UNWIND_LOG(log,
1721 "supplying caller's register {0} ({1}) via DWARF expression "
1722 "(IsDWARFExpression)",
1723 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1725 } else {
1726 regloc.type = UnwindLLDB::ConcreteRegisterLocation::
1727 eRegisterSavedAtMemoryLocation;
1728 regloc.location.target_memory_location = val;
1729 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1730 UNWIND_LOG(log,
1731 "supplying caller's register {0} ({1}) via DWARF expression "
1732 "(IsAtDWARFExpression)",
1733 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1735 }
1736 }
1737 UNWIND_LOG(log,
1738 "tried to use IsDWARFExpression or IsAtDWARFExpression for {0} "
1739 "({1}) but failed",
1740 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1742 }
1743
1744 if (abs_regloc->IsConstant()) {
1746 regloc.location.inferred_value = abs_regloc->GetConstant();
1747 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1748 UNWIND_LOG(log, "supplying caller's register {0} ({1}) via constant value",
1749 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1751 }
1752
1753 UNWIND_LOG(log, "no save location for {0} ({1}) in this stack frame",
1754 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1755
1756 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1757 // unsupported.
1758
1760}
1761
1764 return {};
1765 ProcessSP process_sp = m_thread.GetProcess();
1766 if (!process_sp)
1767 return {};
1768
1769 UnwindPlanSP arch_override_plan_sp;
1770 if (Architecture *arch = process_sp->GetTarget().GetArchitecturePlugin())
1771 arch_override_plan_sp =
1772 arch->GetArchitectureUnwindPlan(m_thread, this, m_full_unwind_plan_sp);
1773
1774 if (arch_override_plan_sp) {
1775 m_full_unwind_plan_sp = arch_override_plan_sp;
1777 m_registers.clear();
1778 if (Log *log = GetLog(LLDBLog::Unwind)) {
1779 UNWIND_LOG(
1780 log, "Replacing Full Unwindplan with Architecture UnwindPlan, '{0}'",
1781 m_full_unwind_plan_sp->GetSourceName());
1782 const UnwindPlan::Row *active_row =
1783 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1784 if (active_row) {
1785 StreamString active_row_strm;
1786 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
1787 &m_thread,
1788 m_start_pc.GetLoadAddress(&process_sp->GetTarget()));
1789 UNWIND_LOG(log, "{0}", active_row_strm.GetString());
1790 }
1791 }
1792 }
1793
1794 return {};
1795}
1796
1797// TryFallbackUnwindPlan() -- this method is a little tricky.
1798//
1799// When this is called, the frame above -- the caller frame, the "previous"
1800// frame -- is invalid or bad.
1801//
1802// Instead of stopping the stack walk here, we'll try a different UnwindPlan
1803// and see if we can get a valid frame above us.
1804//
1805// This most often happens when an unwind plan based on assembly instruction
1806// inspection is not correct -- mostly with hand-written assembly functions or
1807// functions where the stack frame is set up "out of band", e.g. the kernel
1808// saved the register context and then called an asynchronous trap handler like
1809// _sigtramp.
1810//
1811// Often in these cases, if we just do a dumb stack walk we'll get past this
1812// tricky frame and our usual techniques can continue to be used.
1813
1815 if (m_fallback_unwind_plan_sp == nullptr)
1816 return false;
1817
1818 if (m_full_unwind_plan_sp == nullptr)
1819 return false;
1820
1822 m_full_unwind_plan_sp->GetSourceName() ==
1823 m_fallback_unwind_plan_sp->GetSourceName()) {
1824 return false;
1825 }
1826
1827 // If a compiler generated unwind plan failed, trying the arch default
1828 // unwindplan isn't going to do any better.
1829 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1830 return false;
1831
1832 // Get the caller's pc value and our own CFA value. Swap in the fallback
1833 // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1834 // same, then the fallback unwind plan provides no benefit.
1835
1838
1839 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1840 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1843 regloc) ==
1845 const RegisterInfo *reg_info =
1847 if (reg_info) {
1848 RegisterValue reg_value;
1849 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1850 old_caller_pc_value = reg_value.GetAsUInt64();
1851 if (ProcessSP process_sp = m_thread.GetProcess()) {
1852 if (ABISP abi_sp = process_sp->GetABI())
1853 old_caller_pc_value = abi_sp->FixCodeAddress(old_caller_pc_value);
1854 }
1855 }
1856 }
1857 }
1858
1859 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1860 // impossible register location for the full unwind plan, it may call
1861 // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1862 // unwindplan with the fallback... in short, we're done, we're using the
1863 // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1864 // at the top -- the only way it became nullptr since then is via
1865 // SavedLocationForRegister().
1866 if (m_fallback_unwind_plan_sp == nullptr)
1867 return true;
1868
1869 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1870 // this isn't working, we need to restore. We'll also need to save & restore
1871 // the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1872 std::shared_ptr<const UnwindPlan> original_full_unwind_plan_sp =
1874 addr_t old_cfa = m_cfa;
1875 addr_t old_afa = m_afa;
1876
1877 m_registers.clear();
1878
1880
1881 const UnwindPlan::Row *active_row =
1882 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(
1884
1885 Log *log = GetLog(LLDBLog::Unwind);
1886 if (active_row &&
1887 active_row->GetCFAValue().GetValueType() !=
1889 addr_t new_cfa;
1890 ProcessSP process_sp = m_thread.GetProcess();
1891 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
1892 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1893 active_row->GetCFAValue(), new_cfa) ||
1894 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
1895 UNWIND_LOG(log, "failed to get cfa with fallback unwindplan");
1897 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1898 return false;
1899 }
1900 m_cfa = new_cfa;
1901
1903 active_row->GetAFAValue(), m_afa);
1904
1906 regloc) ==
1908 const RegisterInfo *reg_info =
1910 if (reg_info) {
1911 RegisterValue reg_value;
1912 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
1913 reg_value)) {
1914 new_caller_pc_value = reg_value.GetAsUInt64();
1915 if (process_sp)
1916 new_caller_pc_value =
1917 process_sp->FixCodeAddress(new_caller_pc_value);
1918 }
1919 }
1920 }
1921
1922 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
1923 UNWIND_LOG(log, "failed to get a pc value for the caller frame with the "
1924 "fallback unwind plan");
1926 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1927 m_cfa = old_cfa;
1928 m_afa = old_afa;
1929 return false;
1930 }
1931
1932 if (old_caller_pc_value == new_caller_pc_value &&
1933 m_cfa == old_cfa &&
1934 m_afa == old_afa) {
1935 UNWIND_LOG(log, "fallback unwind plan got the same values for this frame "
1936 "CFA and caller frame pc, not using");
1938 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1939 return false;
1940 }
1941
1942 UNWIND_LOG(log,
1943 "trying to unwind from this function with the UnwindPlan '{0}' "
1944 "because UnwindPlan '{1}' failed.",
1945 m_fallback_unwind_plan_sp->GetSourceName(),
1946 original_full_unwind_plan_sp->GetSourceName());
1947
1948 // We've copied the fallback unwind plan into the full - now clear the
1949 // fallback.
1952 }
1953
1954 return true;
1955}
1956
1958 if (m_fallback_unwind_plan_sp == nullptr)
1959 return false;
1960
1961 if (m_full_unwind_plan_sp == nullptr)
1962 return false;
1963
1965 m_full_unwind_plan_sp->GetSourceName() ==
1966 m_fallback_unwind_plan_sp->GetSourceName()) {
1967 return false;
1968 }
1969
1970 const UnwindPlan::Row *active_row =
1971 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1972
1973 if (active_row &&
1974 active_row->GetCFAValue().GetValueType() !=
1976 addr_t new_cfa;
1977 ProcessSP process_sp = m_thread.GetProcess();
1978 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
1979 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1980 active_row->GetCFAValue(), new_cfa) ||
1981 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
1983 "failed to get cfa with fallback unwindplan");
1985 return false;
1986 }
1987
1989 active_row->GetAFAValue(), m_afa);
1990
1993
1994 m_registers.clear();
1995
1996 m_cfa = new_cfa;
1997
1999
2001 "switched unconditionally to the fallback unwindplan {0}",
2002 m_full_unwind_plan_sp->GetSourceName());
2003 return true;
2004 }
2005 return false;
2006}
2007
2009 std::shared_ptr<const UnwindPlan> unwind_plan) {
2010 if (unwind_plan->GetUnwindPlanForSignalTrap() != eLazyBoolYes) {
2011 // Unwind plan does not indicate trap handler. Do nothing. We may
2012 // already be flagged as trap handler flag due to the symbol being
2013 // in the trap handler symbol list, and that should take precedence.
2014 return;
2015 } else if (m_frame_type != eNormalFrame) {
2016 // If this is already a trap handler frame, nothing to do.
2017 // If this is a skip or debug or invalid frame, don't override that.
2018 return;
2019 }
2020
2022
2023 Log *log = GetLog(LLDBLog::Unwind);
2024 UNWIND_LOG(log, "This frame is marked as a trap handler via its UnwindPlan");
2025
2027 // We backed up the pc by 1 to compute the symbol context, but
2028 // now need to undo that because the pc of the trap handler
2029 // frame may in fact be the first instruction of a signal return
2030 // trampoline, rather than the instruction after a call. This
2031 // happens on systems where the signal handler dispatch code, rather
2032 // than calling the handler and being returned to, jumps to the
2033 // handler after pushing the address of a return trampoline on the
2034 // stack -- on these systems, when the handler returns, control will
2035 // be transferred to the return trampoline, so that's the best
2036 // symbol we can present in the callstack.
2037 UNWIND_LOG(log,
2038 "Resetting current offset and re-doing symbol lookup; old "
2039 "symbol was {0}",
2042
2043 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
2044
2045 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
2046
2047 ExecutionContext exe_ctx(m_thread.shared_from_this());
2048 Process *process = exe_ctx.GetProcessPtr();
2049 Target *target = &process->GetTarget();
2050
2051 if (m_sym_ctx_valid) {
2052 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
2053 m_current_offset = m_current_pc.GetLoadAddress(target) -
2054 m_start_pc.GetLoadAddress(target);
2055 }
2056 }
2057}
2058
2060 lldb::RegisterKind row_register_kind, const UnwindPlan::Row::FAValue &fa,
2061 addr_t &address) {
2062 RegisterValue reg_value;
2063
2064 address = LLDB_INVALID_ADDRESS;
2065 addr_t cfa_reg_contents;
2066 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2067
2068 Log *log = GetLog(LLDBLog::Unwind);
2069 switch (fa.GetValueType()) {
2071 UNWIND_LOG(log, "CFA value via dereferencing reg");
2072 RegisterNumber regnum_to_deref(m_thread, row_register_kind,
2073 fa.GetRegisterNumber());
2074 addr_t reg_to_deref_contents;
2075 if (ReadGPRValue(regnum_to_deref, reg_to_deref_contents)) {
2076 const RegisterInfo *reg_info =
2078 RegisterValue reg_value;
2079 if (reg_info) {
2081 reg_info, reg_to_deref_contents, reg_info->byte_size, reg_value);
2082 if (error.Success()) {
2083 address = reg_value.GetAsUInt64();
2084 UNWIND_LOG(log,
2085 "CFA value via dereferencing reg {0} ({1}): reg has val "
2086 "{2:x}, CFA value is {3:x}",
2087 regnum_to_deref.GetName(),
2088 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2089 reg_to_deref_contents, address);
2090 return true;
2091 } else {
2092 UNWIND_LOG(
2093 log,
2094 "Tried to deref reg {0} ({1}) [{2:x}] but memory read failed.",
2095 regnum_to_deref.GetName(),
2096 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2097 reg_to_deref_contents);
2098 }
2099 }
2100 }
2101 break;
2102 }
2104 UNWIND_LOG(log, "CFA value via register plus offset");
2105 RegisterNumber cfa_reg(m_thread, row_register_kind,
2106 fa.GetRegisterNumber());
2107 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
2108 if (!CallFrameAddressIsValid(abi_sp, cfa_reg_contents)) {
2109 UNWIND_LOG(
2110 log,
2111 "Got an invalid CFA register value - reg {0} ({1}), value {2:x}",
2112 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2113 cfa_reg_contents);
2114 return false;
2115 }
2116 address = cfa_reg_contents + fa.GetOffset();
2117 UNWIND_LOG(
2118 log,
2119 "CFA is {0:x}: Register {1} ({2}) contents are {3:x}, offset is {4}",
2120 address, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2121 cfa_reg_contents, fa.GetOffset());
2122 return true;
2123 }
2124 UNWIND_LOG(log, "unable to read CFA register {0} ({1})", cfa_reg.GetName(),
2125 cfa_reg.GetAsKind(eRegisterKindLLDB));
2126 break;
2127 }
2129 UNWIND_LOG(log, "CFA value via DWARF expression");
2130 ExecutionContext exe_ctx(m_thread.shared_from_this());
2131 Process *process = exe_ctx.GetProcessPtr();
2134 process->GetByteOrder(),
2135 process->GetAddressByteSize());
2136 ModuleSP opcode_ctx;
2137 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
2139 row_register_kind);
2140 llvm::Expected<Value> result =
2141 dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr);
2142 if (result) {
2143 address = result->GetScalar().ULongLong();
2144 UNWIND_LOG(log, "CFA value set by DWARF expression is {0:x}", address);
2145 return true;
2146 }
2147 UNWIND_LOG(log, "Failed to set CFA value via DWARF expression: {0}",
2148 fmt_consume(result.takeError()));
2149 break;
2150 }
2152 UNWIND_LOG(log, "CFA value via heuristic search");
2153 Process &process = *m_thread.GetProcess();
2154 lldb::addr_t return_address_hint = GetReturnAddressHint(fa.GetOffset());
2155 if (return_address_hint == LLDB_INVALID_ADDRESS)
2156 return false;
2157 const unsigned max_iterations = 256;
2158 for (unsigned i = 0; i < max_iterations; ++i) {
2159 Status st;
2160 lldb::addr_t candidate_addr =
2161 return_address_hint + i * process.GetAddressByteSize();
2162 lldb::addr_t candidate =
2163 process.ReadPointerFromMemory(candidate_addr, st);
2164 if (st.Fail()) {
2165 UNWIND_LOG(log, "Cannot read memory at {0:x}: {1}", candidate_addr, st);
2166 return false;
2167 }
2168 Address addr;
2169 uint32_t permissions;
2170 if (process.GetLoadAddressPermissions(candidate, permissions) &&
2171 permissions & lldb::ePermissionsExecutable) {
2172 address = candidate_addr;
2173 UNWIND_LOG(log, "Heuristically found CFA: {0:x}", address);
2174 return true;
2175 }
2176 }
2177 UNWIND_LOG(log, "No suitable CFA found");
2178 break;
2179 }
2181 address = fa.GetConstant();
2182 UNWIND_LOG(log, "CFA value set by constant is {0:x}", address);
2183 return true;
2184 }
2185 default:
2186 return false;
2187 }
2188 return false;
2189}
2190
2192 addr_t hint;
2194 return LLDB_INVALID_ADDRESS;
2195 if (!m_sym_ctx.module_sp || !m_sym_ctx.symbol)
2196 return LLDB_INVALID_ADDRESS;
2197 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2198 hint = abi_sp->FixCodeAddress(hint);
2199
2200 hint += plan_offset;
2201
2202 if (auto next = GetNextFrame()) {
2203 if (!next->m_sym_ctx.module_sp || !next->m_sym_ctx.symbol)
2204 return LLDB_INVALID_ADDRESS;
2205 if (auto expected_size =
2206 next->m_sym_ctx.module_sp->GetSymbolFile()->GetParameterStackSize(
2207 *next->m_sym_ctx.symbol))
2208 hint += *expected_size;
2209 else {
2211 "Could not retrieve parameter size: {0}",
2212 fmt_consume(expected_size.takeError()));
2213 return LLDB_INVALID_ADDRESS;
2214 }
2215 }
2216 return hint;
2217}
2218
2219// Retrieve a general purpose register value for THIS frame, as saved by the
2220// NEXT frame, i.e. the frame that
2221// this frame called. e.g.
2222//
2223// foo () { }
2224// bar () { foo (); }
2225// main () { bar (); }
2226//
2227// stopped in foo() so
2228// frame 0 - foo
2229// frame 1 - bar
2230// frame 2 - main
2231// and this RegisterContext is for frame 1 (bar) - if we want to get the pc
2232// value for frame 1, we need to ask
2233// where frame 0 (the "next" frame) saved that and retrieve the value.
2234
2236 uint32_t regnum, addr_t &value) {
2237 if (!IsValid())
2238 return false;
2239
2240 uint32_t lldb_regnum;
2241 if (register_kind == eRegisterKindLLDB) {
2242 lldb_regnum = regnum;
2243 } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2244 register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
2245 return false;
2246 }
2247
2248 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
2249 assert(reg_info);
2250 if (!reg_info) {
2251 UNWIND_LOG(
2253 "Could not find RegisterInfo definition for lldb register number {0}",
2254 lldb_regnum);
2255 return false;
2256 }
2257
2258 uint32_t generic_regnum = LLDB_INVALID_REGNUM;
2259 if (register_kind == eRegisterKindGeneric)
2260 generic_regnum = regnum;
2261 else
2262 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2263 register_kind, regnum, eRegisterKindGeneric, generic_regnum);
2264 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2265
2266 RegisterValue reg_value;
2267 // if this is frame 0 (currently executing frame), get the requested reg
2268 // contents from the actual thread registers
2269 if (IsFrameZero()) {
2270 if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
2271 value = reg_value.GetAsUInt64();
2272 if (abi_sp && generic_regnum != LLDB_INVALID_REGNUM) {
2273 if (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2274 generic_regnum == LLDB_REGNUM_GENERIC_RA)
2275 value = abi_sp->FixCodeAddress(value);
2276 }
2277 return true;
2278 }
2279 return false;
2280 }
2281
2282 bool pc_register = false;
2283 if (generic_regnum != LLDB_INVALID_REGNUM &&
2284 (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2285 generic_regnum == LLDB_REGNUM_GENERIC_RA))
2286 pc_register = true;
2287
2289 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2290 lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
2291 return false;
2292 }
2293 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
2294 value = reg_value.GetAsUInt64();
2295 if (pc_register) {
2296 if (ABISP abi_sp = m_thread.GetProcess()->GetABI()) {
2297 value = abi_sp->FixCodeAddress(value);
2298 }
2299 }
2300 return true;
2301 }
2302 return false;
2303}
2304
2306 addr_t &value) {
2307 return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
2308 value);
2309}
2310
2311// Find the value of a register in THIS frame
2312
2314 RegisterValue &value) {
2315 if (!IsValid())
2316 return false;
2317
2318 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2319 Log *log = GetLog(LLDBLog::Unwind);
2320 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2321 lldb_regnum);
2322
2323 // If this is the 0th frame, hand this over to the live register context
2324 if (IsFrameZero()) {
2326 "passing along to the live register context for reg {0}",
2327 lldb_regnum);
2328 return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
2329 }
2330
2331 bool is_pc_regnum = false;
2334 is_pc_regnum = true;
2335 }
2336
2338 // Find out where the NEXT frame saved THIS frame's register contents
2339 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2340 lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
2341 return false;
2342
2343 bool result = ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
2344 if (result) {
2345 if (is_pc_regnum && value.GetType() == RegisterValue::eTypeUInt64) {
2346 addr_t reg_value = value.GetAsUInt64(LLDB_INVALID_ADDRESS);
2347 if (reg_value != LLDB_INVALID_ADDRESS) {
2348 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2349 value = abi_sp->FixCodeAddress(reg_value);
2350 }
2351 }
2352 }
2353 return result;
2354}
2355
2357 const RegisterValue &value) {
2358 if (!IsValid())
2359 return false;
2360
2361 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2362 Log *log = GetLog(LLDBLog::Unwind);
2363 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2364 lldb_regnum);
2365
2366 // If this is the 0th frame, hand this over to the live register context
2367 if (IsFrameZero()) {
2369 "passing along to the live register context for reg {0}",
2370 lldb_regnum);
2371 return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
2372 }
2373
2375 // Find out where the NEXT frame saved THIS frame's register contents
2376 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2377 lldb_regnum, regloc, m_frame_number - 1, false))
2378 return false;
2379
2380 return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
2381}
2382
2383// Don't need to implement this one
2385 lldb::WritableDataBufferSP &data_sp) {
2386 return false;
2387}
2388
2389// Don't need to implement this one
2391 const lldb::DataBufferSP &data_sp) {
2392 return false;
2393}
2394
2395// Retrieve the pc value for THIS from
2396
2398 if (!IsValid()) {
2399 return false;
2400 }
2401 if (m_cfa == LLDB_INVALID_ADDRESS) {
2402 return false;
2403 }
2404 cfa = m_cfa;
2405 return true;
2406}
2407
2410 if (m_frame_number == 0)
2411 return regctx;
2412 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
2413}
2414
2419
2420// Retrieve the address of the start of the function of THIS frame
2421
2423 if (!IsValid())
2424 return false;
2425
2426 if (!m_start_pc.IsValid()) {
2427 bool read_successfully = ReadPC (start_pc);
2428 if (read_successfully)
2429 {
2430 ProcessSP process_sp (m_thread.GetProcess());
2431 if (process_sp)
2432 {
2433 if (ABISP abi_sp = process_sp->GetABI())
2434 start_pc = abi_sp->FixCodeAddress(start_pc);
2435 }
2436 }
2437 return read_successfully;
2438 }
2439 start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2440 return true;
2441}
2442
2443// Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2444
2446 if (!IsValid())
2447 return false;
2448
2449 bool above_trap_handler = false;
2450 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2452 above_trap_handler = true;
2453
2455 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2456 // indicates the end of a stack walk.
2457 // On the currently executing frame (or such a frame interrupted
2458 // asynchronously by sigtramp et al) this may occur if code has jumped
2459 // through a NULL pointer -- we want to be able to unwind past that frame
2460 // to help find the bug.
2461
2462 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2463 pc = abi_sp->FixCodeAddress(pc);
2464
2465 return !(m_all_registers_available == false &&
2466 above_trap_handler == false && (pc == 0 || pc == 1));
2467 } else {
2468 return false;
2469 }
2470}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
static ConstString GetSymbolOrFunctionName(const SymbolContext &sym_ctx)
#define UNWIND_LOG_VERBOSE(log,...)
static bool CallFrameAddressIsValid(ABISP abi_sp, lldb::addr_t cfa)
#define UNWIND_LOG(log,...)
A class to represent register numbers, and able to convert between different register numbering schem...
bool IsValid() const
uint32_t GetAsKind(lldb::RegisterKind kind)
lldb::RegisterKind GetRegisterKind() const
uint32_t GetRegisterNumber() const
void init(lldb_private::Thread &thread, lldb::RegisterKind kind, uint32_t num)
const char * GetName()
virtual lldb::UnwindPlanSP CreateDefaultUnwindPlan()=0
virtual bool GetFallbackRegisterLocation(const RegisterInfo *reg_info, UnwindPlan::Row::AbstractRegisterLocation &unwind_regloc)
Definition ABI.cpp:202
virtual lldb::UnwindPlanSP CreateFunctionEntryUnwindPlan()=0
A section + offset based address class.
Definition Address.h:62
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition Address.cpp:1034
bool Slide(int64_t offset)
Definition Address.h:452
bool ResolveFunctionScope(lldb_private::SymbolContext &sym_ctx)
Resolve this address to its containing function.
Definition Address.cpp:266
An architecture specification class.
Definition ArchSpec.h:32
bool GetUnwindPlan(Target &target, const Address &addr, UnwindPlan &unwind_plan)
virtual std::unique_ptr< UnwindPlan > GetUnwindPlan(llvm::ArrayRef< AddressRange > ranges, const Address &addr)=0
A uniqued constant string class.
Definition ConstString.h:40
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::addr_t func_load_addr, const Value *initial_value_ptr, const Value *object_address_ptr) const
DWARFExpression * GetMutableExpressionAtAddress(lldb::addr_t func_load_addr=LLDB_INVALID_ADDRESS, lldb::addr_t load_addr=0)
void SetRegisterKind(lldb::RegisterKind reg_kind)
Set the call-frame-info style register kind.
An data extractor class.
virtual bool AlwaysRelyOnEHUnwindInfo(SymbolContext &sym_ctx)
Ask if the eh_frame information for the given SymbolContext should be relied on even when it's the fi...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Target & GetTargetRef() const
Returns a reference to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
ConstString GetName() const
Definition Function.cpp:709
static lldb::UnwindPlanSP GetRuntimeUnwindPlan(lldb_private::Thread &thread, lldb_private::RegisterContext *regctx, bool &behaves_like_zeroth_frame)
A language runtime may be able to provide a special UnwindPlan for the frame represented by the regis...
A plug-in interface definition class for debugging a process.
Definition Process.h:355
virtual bool GetLoadAddressPermissions(lldb::addr_t load_addr, uint32_t &permissions)
Attempt to get the attributes for a region of memory in the process.
Definition Process.cpp:2673
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3770
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2377
uint32_t GetAddressByteSize() const
Definition Process.cpp:3774
virtual DynamicLoader * GetDynamicLoader()
Get the dynamic loader plug-in for this process.
Definition Process.cpp:2962
const lldb::ABISP & GetABI()
Definition Process.cpp:1459
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1251
std::optional< UnwindPlan::Row::AbstractRegisterLocation > GetAbstractRegisterLocation(uint32_t lldb_regnum, lldb::RegisterKind &kind)
bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override
void PropagateTrapHandlerFlagFromUnwindPlan(std::shared_ptr< const UnwindPlan > unwind_plan)
Check if the given unwind plan indicates a signal trap handler, and update frame type and symbol cont...
const lldb_private::RegisterInfo * GetRegisterInfoAtIndex(size_t reg) override
bool ReadRegisterValueFromRegisterLocation(lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc, const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value)
const lldb_private::RegisterSet * GetRegisterSet(size_t reg_set) override
std::shared_ptr< RegisterContextUnwind > SharedPtr
bool ReadFrameAddress(lldb::RegisterKind register_kind, const UnwindPlan::Row::FAValue &fa, lldb::addr_t &address)
bool ReadAllRegisterValues(lldb::WritableDataBufferSP &data_sp) override
bool WriteRegister(const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value) override
RegisterContextUnwind(lldb_private::Thread &thread, const SharedPtr &next_frame, lldb_private::SymbolContext &sym_ctx, uint32_t frame_number, lldb_private::UnwindLLDB &unwind_lldb)
std::shared_ptr< const UnwindPlan > GetFastUnwindPlanForFrame()
std::shared_ptr< const UnwindPlan > m_fast_unwind_plan_sp
std::map< uint32_t, lldb_private::UnwindLLDB::ConcreteRegisterLocation > m_registers
std::shared_ptr< const UnwindPlan > GetFullUnwindPlanForFrame()
bool ForceSwitchToFallbackUnwindPlan()
Switch to the fallback unwind plan unconditionally without any safety checks that it is providing bet...
lldb_private::UnwindLLDB::RegisterSearchResult SavedLocationForRegister(uint32_t lldb_regnum, lldb_private::UnwindLLDB::ConcreteRegisterLocation &regloc)
bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum, lldb::addr_t &value)
lldb_private::UnwindLLDB & m_parent_unwind
bool WriteRegisterValueToRegisterLocation(lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc, const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value)
bool TryFallbackUnwindPlan()
If the unwind has to the caller frame has failed, try something else.
std::optional< int > m_current_offset
How far into the function we've executed.
bool ReadRegister(const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value) override
lldb::addr_t GetReturnAddressHint(int32_t plan_offset)
bool IsTrapHandlerSymbol(lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const
Determines if a SymbolContext is a trap handler or not.
uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override
Convert from a given register numbering scheme to the lldb register numbering scheme.
bool IsUnwindPlanValidForCurrentPC(std::shared_ptr< const UnwindPlan > unwind_plan_sp)
std::shared_ptr< const UnwindPlan > m_fallback_unwind_plan_sp
lldb_private::SymbolContext & m_sym_ctx
bool BehavesLikeZerothFrame() const override
Indicates that this frame is currently executing code, that the PC value is not a return-pc but an ac...
std::shared_ptr< const UnwindPlan > m_full_unwind_plan_sp
virtual Status ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, RegisterValue &reg_value)
RegisterContext(Thread &thread, uint32_t concrete_frame_idx)
lldb::TargetSP CalculateTarget() override
virtual Status WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, const RegisterValue &reg_value)
bool SetUInt(uint64_t uint, uint32_t byte_size)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
RegisterValue::Type GetType() const
An error handling class.
Definition Status.h:118
bool Fail() const
Test for error condition.
Definition Status.cpp:293
llvm::StringRef GetString() const
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
Symbol * symbol
The Symbol for a given query.
ConstString GetName() const
Definition Symbol.cpp:511
lldb::PlatformSP GetPlatform()
Definition Target.h:1694
const ArchSpec & GetArchitecture() const
Definition Target.h:1199
const uint8_t * GetDWARFExpressionBytes() const
Definition UnwindPlan.h:311
const FAValue & GetAFAValue() const
Definition UnwindPlan.h:368
const FAValue & GetCFAValue() const
Definition UnwindPlan.h:365
bool GetRegisterInfo(uint32_t reg_num, AbstractRegisterLocation &register_location) const
void Dump(Stream &s, const UnwindPlan *unwind_plan, Thread *thread, lldb::addr_t base_addr) const
@ LoadAddress
A load address value.
Definition Value.h:49
void SetValueType(ValueType value_type)
Definition Value.h:89
#define LLDB_REGNUM_GENERIC_RA
#define LLDB_REGNUM_GENERIC_SP
#define LLDB_INVALID_ADDRESS
#define LLDB_INVALID_REGNUM
#define LLDB_REGNUM_GENERIC_PC
#define LLDB_REGNUM_GENERIC_FP
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
std::shared_ptr< lldb_private::ABI > ABISP
std::shared_ptr< lldb_private::Platform > PlatformSP
std::shared_ptr< lldb_private::FuncUnwinders > FuncUnwindersSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::UnwindPlan > UnwindPlanSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
std::shared_ptr< lldb_private::Module > ModuleSP
RegisterKind
Register numbering types.
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ eRegisterKindLLDB
lldb's internal register numbers
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
Registers are grouped into register sets.
An UnwindPlan::Row::AbstractRegisterLocation, combined with the register context and memory for a spe...
Definition UnwindLLDB.h:46
union lldb_private::UnwindLLDB::ConcreteRegisterLocation::@112231307016025255352221255122100277032134020214 location
struct lldb_private::UnwindLLDB::ConcreteRegisterLocation::@112231307016025255352221255122100277032134020214::@342316333304072166237122265045271116073153235374 reg_plus_offset