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 assert(((!m_full_unwind_plan_sp ||
446 m_full_unwind_plan_sp->GetRowCount() == 0 ||
447 m_full_unwind_plan_sp->GetRowAtIndex(0)
448 ->GetUnspecifiedRegistersAreUndefined())) &&
449 "Default UnwindPlan must set "
450 "UnspecifiedRegistersAreUndefined to true");
451 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
452 {
454 }
456 m_current_offset = std::nullopt;
457 m_current_offset_backed_up_one = std::nullopt;
458 RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
459 if (const UnwindPlan::Row *row =
460 m_full_unwind_plan_sp->GetRowForFunctionOffset(0)) {
461 if (!ReadFrameAddress(row_register_kind, row->GetCFAValue(), m_cfa)) {
462 UNWIND_LOG(log, "failed to get cfa value");
463 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
464 {
466 }
467 return;
468 }
469
470 ReadFrameAddress(row_register_kind, row->GetAFAValue(), m_afa);
471
472 // A couple of sanity checks..
473 if (!CallFrameAddressIsValid(abi_sp, m_cfa)) {
474 UNWIND_LOG(log, "could not find a valid cfa address");
476 return;
477 }
478
479 // m_cfa should point into the stack memory; if we can query memory
480 // region permissions, see if the memory is allocated & readable.
481 if (process->GetLoadAddressPermissions(m_cfa, permissions) &&
482 (permissions & ePermissionsReadable) == 0) {
485 log, "the CFA points to a region of memory that is not readable");
486 return;
487 }
488 } else {
489 UNWIND_LOG(log, "could not find a row for function offset zero");
491 return;
492 }
493
494 if (CheckIfLoopingStack()) {
496 if (CheckIfLoopingStack()) {
497 UNWIND_LOG(log, "same CFA address as next frame, assuming the unwind "
498 "is looping - stopping");
500 return;
501 }
502 }
503
504 // Give the Architecture a chance to replace the UnwindPlan.
506
507 UNWIND_LOG(log, "initialized frame cfa is {0:x} afa is {1:x}", m_cfa,
508 m_afa);
509 return;
510 }
512 UNWIND_LOG(log, "could not find any symbol for this pc, or a default "
513 "unwind plan, to continue unwind.");
514 return;
515 }
516
517 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
518
519 if (m_sym_ctx.symbol) {
520 UNWIND_LOG(log, "with pc value of {0:x}, symbol name is '{1}'", pc,
522 } else if (m_sym_ctx.function) {
523 UNWIND_LOG(log, "with pc value of {0:x}, function name is '{1}'", pc,
525 } else {
526 UNWIND_LOG(log, "with pc value of {0:x}, no symbol/function name is known.",
527 pc);
528 }
529
530 bool decr_pc_and_recompute_addr_range;
531
532 if (!m_sym_ctx_valid) {
533 // Always decrement and recompute if the symbol lookup failed
534 decr_pc_and_recompute_addr_range = true;
537 // Don't decrement if we're "above" an asynchronous event like
538 // sigtramp.
539 decr_pc_and_recompute_addr_range = false;
540 } else if (Address addr = m_sym_ctx.GetFunctionOrSymbolAddress();
541 addr != m_current_pc) {
542 // If our "current" pc isn't the start of a function, decrement the pc
543 // if we're up the stack.
545 decr_pc_and_recompute_addr_range = false;
546 else
547 decr_pc_and_recompute_addr_range = true;
548 } else if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
549 // Signal dispatch may set the return address of the handler it calls to
550 // point to the first byte of a return trampoline (like __kernel_rt_sigreturn),
551 // so do not decrement and recompute if the symbol we already found is a trap
552 // handler.
553 decr_pc_and_recompute_addr_range = false;
554 } else if (m_behaves_like_zeroth_frame) {
555 decr_pc_and_recompute_addr_range = false;
556 } else {
557 // Decrement to find the function containing the call.
558 decr_pc_and_recompute_addr_range = true;
559 }
560
561 // We need to back up the pc by 1 byte and re-search for the Symbol to handle
562 // the case where the "saved pc" value is pointing to the next function, e.g.
563 // if a function ends with a CALL instruction.
564 // FIXME this may need to be an architectural-dependent behavior; if so we'll
565 // need to add a member function
566 // to the ABI plugin and consult that.
567 if (decr_pc_and_recompute_addr_range) {
568 UNWIND_LOG(log,
569 "Backing up the pc value of {0:x} by 1 and re-doing symbol "
570 "lookup; old symbol was {1}",
572 Address temporary_pc;
573 temporary_pc.SetLoadAddress(pc - 1, &process->GetTarget());
574 m_sym_ctx.Clear(false);
576
577 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
578 }
579
580 // If we were able to find a symbol/function, set addr_range_ptr to the
581 // bounds of that symbol/function. else treat the current pc value as the
582 // start_pc and record no offset.
583 if (m_sym_ctx_valid) {
584 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
585 m_current_offset = pc - m_start_pc.GetLoadAddress(&process->GetTarget());
587 if (decr_pc_and_recompute_addr_range &&
590 if (m_sym_ctx_valid) {
591 m_current_pc.SetLoadAddress(pc - 1, &process->GetTarget());
592 }
593 }
594 } else {
596 m_current_offset = std::nullopt;
597 m_current_offset_backed_up_one = std::nullopt;
598 }
599
600 if (IsTrapHandlerSymbol(process, m_sym_ctx)) {
602 } else {
603 // FIXME: Detect eDebuggerFrame here.
604 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
605 {
607 }
608 }
609
610 const UnwindPlan::Row *active_row;
611 RegisterKind row_register_kind = eRegisterKindGeneric;
612
613 // If we have LanguageRuntime UnwindPlan for this unwind, use those
614 // rules to find the caller frame instead of the function's normal
615 // UnwindPlans. The full unwind plan for this frame will be
616 // the LanguageRuntime-provided unwind plan, and there will not be a
617 // fast unwind plan.
618 if (lang_runtime_plan_sp.get()) {
619 active_row =
620 lang_runtime_plan_sp->GetRowForFunctionOffset(m_current_offset);
621 row_register_kind = lang_runtime_plan_sp->GetRegisterKind();
622 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(),
623 m_cfa)) {
624 UNWIND_LOG(log, "Cannot set cfa");
625 } else {
626 m_full_unwind_plan_sp = lang_runtime_plan_sp;
627 if (log) {
628 StreamString active_row_strm;
629 active_row->Dump(active_row_strm, lang_runtime_plan_sp.get(), &m_thread,
630 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
631 UNWIND_LOG(log, "async active row: {0}", active_row_strm.GetString());
632 }
633 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
634 UNWIND_LOG(log,
635 "initialized async frame current pc is {0:x} cfa is {1:x} afa "
636 "is {2:x}",
637 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa,
638 m_afa);
639
640 return;
641 }
642 }
643
644 // We've set m_frame_type and m_sym_ctx before this call.
646
647 // Try to get by with just the fast UnwindPlan if possible - the full
648 // UnwindPlan may be expensive to get (e.g. if we have to parse the entire
649 // eh_frame section of an ObjectFile for the first time.)
650
652 m_fast_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
653 active_row =
654 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
655 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind();
657 if (active_row && log) {
658 StreamString active_row_strm;
659 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread,
660 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
661 UNWIND_LOG(log, "Using fast unwind plan '{0}'",
662 m_fast_unwind_plan_sp->GetSourceName());
663 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
664 }
665 } else {
668 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset(
670 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
672 if (active_row && log) {
673 StreamString active_row_strm;
674 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
675 &m_thread,
676 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
677 UNWIND_LOG(log, "Using full unwind plan '{0}'",
678 m_full_unwind_plan_sp->GetSourceName());
679 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
680 }
681 }
682 }
683
684 if (!active_row) {
686 UNWIND_LOG(log, "could not find unwind row for this pc");
687 return;
688 }
689
690 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
691 UNWIND_LOG(log, "failed to get cfa");
693 return;
694 }
695
696 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
697
698 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
699
700 if (CheckIfLoopingStack()) {
702 if (CheckIfLoopingStack()) {
703 UNWIND_LOG(log, "same CFA address as next frame, assuming the unwind is "
704 "looping - stopping");
706 return;
707 }
708 }
709
710 // Give the Architecture a chance to replace the UnwindPlan.
712
713 UNWIND_LOG(log,
714 "initialized frame current pc is {0:x} cfa is {1:x} afa is {2:x}",
715 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa, m_afa);
716}
717
719 // If we have a bad stack setup, we can get the same CFA value multiple times
720 // -- or even more devious, we can actually oscillate between two CFA values.
721 // Detect that here and break out to avoid a possible infinite loop in lldb
722 // trying to unwind the stack. To detect when we have the same CFA value
723 // multiple times, we compare the
724 // CFA of the current
725 // frame with the 2nd next frame because in some specail case (e.g. signal
726 // hanlders, hand written assembly without ABI compliance) we can have 2
727 // frames with the same
728 // CFA (in theory we
729 // can have arbitrary number of frames with the same CFA, but more then 2 is
730 // very unlikely)
731
733 if (next_frame) {
734 RegisterContextUnwind::SharedPtr next_next_frame =
735 next_frame->GetNextFrame();
736 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
737 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) {
738 if (next_next_frame_cfa == m_cfa) {
739 // We have a loop in the stack unwind
740 return true;
741 }
742 }
743 }
744 return false;
745}
746
748
750 if (m_frame_number == 0)
751 return true;
753 return true;
754 return false;
755}
756
757// Find a fast unwind plan for this frame, if possible.
758//
759// On entry to this method,
760//
761// 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
762// if either of those are correct,
763// 2. m_sym_ctx should already be filled in, and
764// 3. m_current_pc should have the current pc value for this frame
765// 4. m_current_offset_backed_up_one should have the current byte offset into
766// the function, maybe backed up by 1, std::nullopt if unknown
767
768std::shared_ptr<const UnwindPlan>
770 ModuleSP pc_module_sp(m_current_pc.GetModule());
771
772 if (!m_current_pc.IsValid() || !pc_module_sp ||
773 pc_module_sp->GetObjectFile() == nullptr)
774 return nullptr;
775
776 if (IsFrameZero())
777 return nullptr;
778
779 FuncUnwindersSP func_unwinders_sp(
780 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
782 if (!func_unwinders_sp)
783 return nullptr;
784
785 // If we're in _sigtramp(), unwinding past this frame requires special
786 // knowledge.
788 return nullptr;
789
790 if (std::shared_ptr<const UnwindPlan> unwind_plan_sp =
791 func_unwinders_sp->GetUnwindPlanFastUnwind(
792 *m_thread.CalculateTarget(), m_thread)) {
793 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
795 return unwind_plan_sp;
796 }
797 }
798 return nullptr;
799}
800
801// On entry to this method,
802//
803// 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
804// if either of those are correct,
805// 2. m_sym_ctx should already be filled in, and
806// 3. m_current_pc should have the current pc value for this frame
807// 4. m_current_offset_backed_up_one should have the current byte offset into
808// the function, maybe backed up by 1, std::nullopt if unknown
809
810std::shared_ptr<const UnwindPlan>
812 Log *log = GetLog(LLDBLog::Unwind);
813 std::shared_ptr<const UnwindPlan> arch_default_unwind_plan_sp;
814 ExecutionContext exe_ctx(m_thread.shared_from_this());
815 Process *process = exe_ctx.GetProcessPtr();
816 ABI *abi = process ? process->GetABI().get() : nullptr;
817 if (abi) {
818 arch_default_unwind_plan_sp = abi->CreateDefaultUnwindPlan();
819 assert(((!arch_default_unwind_plan_sp ||
820 arch_default_unwind_plan_sp->GetRowCount() == 0 ||
821 arch_default_unwind_plan_sp->GetRowAtIndex(0)
822 ->GetUnspecifiedRegistersAreUndefined())) &&
823 "Default UnwindPlan must set "
824 "UnspecifiedRegistersAreUndefined to true");
825 } else {
827 log, "unable to get architectural default UnwindPlan from ABI plugin");
828 }
829
833 // If this frame behaves like a 0th frame (currently executing or
834 // interrupted asynchronously), all registers can be retrieved.
836 }
837
838 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
839 // so the pc is 0x0 in the zeroth frame, we need to use the "unwind at first
840 // instruction" arch default UnwindPlan Also, if this Process can report on
841 // memory region attributes, any non-executable region means we jumped
842 // through a bad function pointer - handle the same way as 0x0. Note, if we
843 // have a symbol context & a symbol, we don't want to follow this code path.
844 // This is for jumping to memory regions without any information available.
845
846 if ((!m_sym_ctx_valid ||
847 (m_sym_ctx.function == nullptr && m_sym_ctx.symbol == nullptr)) &&
849 uint32_t permissions;
850 addr_t current_pc_addr =
851 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr());
852 if (current_pc_addr == 0 ||
853 (process &&
854 process->GetLoadAddressPermissions(current_pc_addr, permissions) &&
855 (permissions & ePermissionsExecutable) == 0)) {
856 if (abi) {
858 return abi->CreateFunctionEntryUnwindPlan();
859 }
860 }
861 }
862
863 // No Module for the current pc, try using the architecture default unwind.
864 ModuleSP pc_module_sp(m_current_pc.GetModule());
865 if (!m_current_pc.IsValid() || !pc_module_sp ||
866 pc_module_sp->GetObjectFile() == nullptr) {
868 return arch_default_unwind_plan_sp;
869 }
870
871 FuncUnwindersSP func_unwinders_sp;
872 if (m_sym_ctx_valid) {
873 func_unwinders_sp =
874 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
876 }
877
878 // No FuncUnwinders available for this pc (stripped function symbols, lldb
879 // could not augment its function table with another source, like
880 // LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). See if eh_frame or the
881 // .ARM.exidx tables have unwind information for this address, else fall back
882 // to the architectural default unwind.
883 if (!func_unwinders_sp) {
885
886 if (!pc_module_sp || !pc_module_sp->GetObjectFile() ||
887 !m_current_pc.IsValid())
888 return arch_default_unwind_plan_sp;
889
890 // Even with -fomit-frame-pointer, we can try eh_frame to get back on
891 // track.
892 if (DWARFCallFrameInfo *eh_frame =
893 pc_module_sp->GetUnwindTable().GetEHFrameInfo()) {
894 if (std::unique_ptr<UnwindPlan> plan_up =
895 eh_frame->GetUnwindPlan(m_current_pc))
896 return plan_up;
897 }
898
899 ArmUnwindInfo *arm_exidx =
900 pc_module_sp->GetUnwindTable().GetArmUnwindInfo();
901 if (arm_exidx) {
902 auto unwind_plan_sp =
903 std::make_shared<UnwindPlan>(lldb::eRegisterKindGeneric);
904 if (arm_exidx->GetUnwindPlan(exe_ctx.GetTargetRef(), m_current_pc,
905 *unwind_plan_sp))
906 return unwind_plan_sp;
907 }
908
909 CallFrameInfo *object_file_unwind =
910 pc_module_sp->GetUnwindTable().GetObjectFileUnwindInfo();
911 if (object_file_unwind) {
912 if (std::unique_ptr<UnwindPlan> plan_up =
913 object_file_unwind->GetUnwindPlan(m_current_pc))
914 return plan_up;
915 }
916
917 return arch_default_unwind_plan_sp;
918 }
919
920 if (m_frame_type == eTrapHandlerFrame && process) {
921 m_fast_unwind_plan_sp.reset();
922
923 // On some platforms the unwind information for signal handlers is not
924 // present or correct. Give the platform plugins a chance to provide
925 // substitute plan. Otherwise, use eh_frame.
926 if (m_sym_ctx_valid) {
927 lldb::PlatformSP platform = process->GetTarget().GetPlatform();
928 const ArchSpec arch = process->GetTarget().GetArchitecture();
929 if (auto unwind_plan_sp = platform->GetTrapHandlerUnwindPlan(
931 return unwind_plan_sp;
932 }
933
934 auto unwind_plan_sp =
935 func_unwinders_sp->GetEHFrameUnwindPlan(process->GetTarget());
936 if (!unwind_plan_sp)
937 unwind_plan_sp =
938 func_unwinders_sp->GetObjectFileUnwindPlan(process->GetTarget());
939 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc) &&
940 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) {
941 return unwind_plan_sp;
942 }
943 }
944
945 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
946 // even when it's frame zero This comes up if we have hand-written functions
947 // in a Module and hand-written eh_frame. The assembly instruction
948 // inspection may fail and the eh_frame CFI were probably written with some
949 // care to do the right thing. It'd be nice if there was a way to ask the
950 // eh_frame directly if it is asynchronous (can be trusted at every
951 // instruction point) or synchronous (the normal case - only at call sites).
952 // But there is not.
953 if (process && process->GetDynamicLoader() &&
955 // We must specifically call the GetEHFrameUnwindPlan() method here --
956 // normally we would call GetUnwindPlanAtCallSite() -- because CallSite may
957 // return an unwind plan sourced from either eh_frame (that's what we
958 // intend) or compact unwind (this won't work)
959 auto unwind_plan_sp =
960 func_unwinders_sp->GetEHFrameUnwindPlan(process->GetTarget());
961 if (!unwind_plan_sp)
962 unwind_plan_sp =
963 func_unwinders_sp->GetObjectFileUnwindPlan(process->GetTarget());
964 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
966 "frame uses {0} for full UnwindPlan because the "
967 "DynamicLoader suggested we prefer it",
968 unwind_plan_sp->GetSourceName());
969 return unwind_plan_sp;
970 }
971 }
972
973 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
974 // the assembly language instructions
975 if (m_behaves_like_zeroth_frame && process) {
976 auto unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
977 process->GetTarget(), m_thread);
978 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
979 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
980 // We probably have an UnwindPlan created by inspecting assembly
981 // instructions. The assembly profilers work really well with compiler-
982 // generated functions but hand- written assembly can be problematic.
983 // We set the eh_frame based unwind plan as our fallback unwind plan if
984 // instruction emulation doesn't work out even for non call sites if it
985 // is available and use the architecture default unwind plan if it is
986 // not available. The eh_frame unwind plan is more reliable even on non
987 // call sites then the architecture default plan and for hand written
988 // assembly code it is often written in a way that it valid at all
989 // location what helps in the most common cases when the instruction
990 // emulation fails.
991 std::shared_ptr<const UnwindPlan> call_site_unwind_plan =
992 func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(),
993 m_thread);
994 if (call_site_unwind_plan &&
995 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
996 call_site_unwind_plan->GetSourceName() !=
997 unwind_plan_sp->GetSourceName()) {
998 m_fallback_unwind_plan_sp = call_site_unwind_plan;
999 } else {
1000 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
1001 }
1002 }
1004 log,
1005 "frame uses {0} for full UnwindPlan because this is the non-call "
1006 "site unwind plan and this is a zeroth frame",
1007 unwind_plan_sp->GetSourceName());
1008 return unwind_plan_sp;
1009 }
1010
1011 // If we're on the first instruction of a function, and we have an
1012 // architectural default UnwindPlan for the initial instruction of a
1013 // function, use that.
1014 if (m_current_offset == 0) {
1015 unwind_plan_sp =
1016 func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
1017 m_thread);
1018 if (unwind_plan_sp) {
1020 "frame uses {0} for full UnwindPlan because we are "
1021 "at the first instruction of a function",
1022 unwind_plan_sp->GetSourceName());
1023 return unwind_plan_sp;
1024 }
1025 }
1026 }
1027
1028 std::shared_ptr<const UnwindPlan> unwind_plan_sp;
1029 // Typically this is unwind info from an eh_frame section intended for
1030 // exception handling; only valid at call sites
1031 if (process) {
1032 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite(
1033 process->GetTarget(), m_thread);
1034 }
1035 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp)) {
1037 "frame uses {0} for full UnwindPlan because this is the "
1038 "call-site unwind plan",
1039 unwind_plan_sp->GetSourceName());
1040 return unwind_plan_sp;
1041 }
1042
1043 // We'd prefer to use an UnwindPlan intended for call sites when we're at a
1044 // call site but if we've struck out on that, fall back to using the non-
1045 // call-site assembly inspection UnwindPlan if possible.
1046 if (process) {
1047 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
1048 process->GetTarget(), m_thread);
1049 }
1050 if (unwind_plan_sp &&
1051 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
1052 // We probably have an UnwindPlan created by inspecting assembly
1053 // instructions. The assembly profilers work really well with compiler-
1054 // generated functions but hand- written assembly can be problematic. We
1055 // set the eh_frame based unwind plan as our fallback unwind plan if
1056 // instruction emulation doesn't work out even for non call sites if it is
1057 // available and use the architecture default unwind plan if it is not
1058 // available. The eh_frame unwind plan is more reliable even on non call
1059 // sites then the architecture default plan and for hand written assembly
1060 // code it is often written in a way that it valid at all location what
1061 // helps in the most common cases when the instruction emulation fails.
1062 std::shared_ptr<const UnwindPlan> call_site_unwind_plan =
1063 func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(),
1064 m_thread);
1065 if (call_site_unwind_plan &&
1066 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
1067 call_site_unwind_plan->GetSourceName() !=
1068 unwind_plan_sp->GetSourceName()) {
1069 m_fallback_unwind_plan_sp = call_site_unwind_plan;
1070 } else {
1071 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
1072 }
1073 }
1074
1075 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp)) {
1077 "frame uses {0} for full UnwindPlan because we failed "
1078 "to find a call-site unwind plan that would work",
1079 unwind_plan_sp->GetSourceName());
1080 return unwind_plan_sp;
1081 }
1082
1083 // If nothing else, use the architectural default UnwindPlan and hope that
1084 // does the job.
1085 if (arch_default_unwind_plan_sp)
1087 "frame uses {0} for full UnwindPlan because we are "
1088 "falling back to the arch default plan",
1089 arch_default_unwind_plan_sp->GetSourceName());
1090 else
1091 UNWIND_LOG(log,
1092 "Unable to find any UnwindPlan for full unwind of this frame.");
1093
1094 return arch_default_unwind_plan_sp;
1095}
1096
1100
1102 return m_thread.GetRegisterContext()->GetRegisterCount();
1103}
1104
1106 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex(reg);
1107}
1108
1110 return m_thread.GetRegisterContext()->GetRegisterSetCount();
1111}
1112
1114 return m_thread.GetRegisterContext()->GetRegisterSet(reg_set);
1115}
1116
1118 lldb::RegisterKind kind, uint32_t num) {
1119 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
1120 kind, num);
1121}
1122
1125 const RegisterInfo *reg_info, RegisterValue &value) {
1126 if (!IsValid())
1127 return false;
1128 bool success = false;
1129
1130 switch (regloc.type) {
1132 const RegisterInfo *other_reg_info =
1134
1135 if (!other_reg_info)
1136 return false;
1137
1138 success =
1139 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1140 } break;
1142 const RegisterInfo *other_reg_info =
1144
1145 if (!other_reg_info)
1146 return false;
1147
1148 if (IsFrameZero()) {
1149 success =
1150 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1151 } else {
1152 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1153 }
1154 } break;
1156 auto regnum = regloc.location.reg_plus_offset.register_number;
1157 const RegisterInfo *other_reg_info =
1159
1160 if (!other_reg_info)
1161 return false;
1162
1163 if (IsFrameZero()) {
1164 success =
1165 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1166 } else {
1167 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1168 }
1169 if (success) {
1170 Log *log = GetLog(LLDBLog::Unwind);
1171 UNWIND_LOG(log, "read ({0})'s location", regnum);
1172 value = value.GetAsUInt64(~0ull, &success) +
1174 UNWIND_LOG(log, "success {0}", success ? "yes" : "no");
1175 }
1176 } break;
1178 success =
1179 value.SetUInt(regloc.location.inferred_value, reg_info->byte_size);
1180 break;
1181
1183 break;
1185 llvm_unreachable("FIXME debugger inferior function call unwind");
1188 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1189 value));
1190 success = error.Success();
1191 } break;
1192 default:
1193 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1194 }
1195 return success;
1196}
1197
1200 const RegisterInfo *reg_info, const RegisterValue &value) {
1201 if (!IsValid())
1202 return false;
1203
1204 bool success = false;
1205
1206 switch (regloc.type) {
1208 const RegisterInfo *other_reg_info =
1210 success =
1211 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1212 } break;
1214 const RegisterInfo *other_reg_info =
1216 if (IsFrameZero()) {
1217 success =
1218 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1219 } else {
1220 success = GetNextFrame()->WriteRegister(other_reg_info, value);
1221 }
1222 } break;
1226 break;
1228 llvm_unreachable("FIXME debugger inferior function call unwind");
1231 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1232 value));
1233 success = error.Success();
1234 } break;
1235 default:
1236 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1237 }
1238 return success;
1239}
1240
1244
1245// After the final stack frame in a stack walk we'll get one invalid
1246// (eNotAValidFrame) stack frame -- one past the end of the stack walk. But
1247// higher-level code will need to tell the difference between "the unwind plan
1248// below this frame failed" versus "we successfully completed the stack walk"
1249// so this method helps to disambiguate that.
1250
1254
1255// A skip frame is a bogus frame on the stack -- but one where we're likely to
1256// find a real frame farther
1257// up the stack if we keep looking. It's always the second frame in an unwind
1258// (i.e. the first frame after frame zero) where unwinding can be the
1259// trickiest. Ideally we'll mark up this frame in some way so the user knows
1260// we're displaying bad data and we may have skipped one frame of their real
1261// program in the process of getting back on track.
1262
1266
1268 lldb_private::Process *process,
1270 PlatformSP platform_sp(process->GetTarget().GetPlatform());
1271 if (platform_sp) {
1272 const std::vector<ConstString> trap_handler_names(
1273 platform_sp->GetTrapHandlerSymbolNames());
1274 for (ConstString name : trap_handler_names) {
1275 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1276 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1277 return true;
1278 }
1279 }
1280 }
1281 const std::vector<ConstString> user_specified_trap_handler_names(
1282 m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames());
1283 for (ConstString name : user_specified_trap_handler_names) {
1284 if ((m_sym_ctx.function && m_sym_ctx.function->GetName() == name) ||
1285 (m_sym_ctx.symbol && m_sym_ctx.symbol->GetName() == name)) {
1286 return true;
1287 }
1288 }
1289
1290 return false;
1291}
1292
1293// Search this stack frame's UnwindPlans for the AbstractRegisterLocation
1294// for this register.
1295//
1296// \param[in] lldb_regnum
1297// The register number (in the eRegisterKindLLDB register numbering)
1298// we are searching for.
1299//
1300// \param[out] kind
1301// Set to the RegisterKind of the UnwindPlan which is the basis for
1302// the returned AbstractRegisterLocation; if the location is in terms
1303// of another register number, this Kind is needed to interpret it
1304// correctly.
1305//
1306// \return
1307// An empty optional indicaTes that there was an error in processing
1308// the request.
1309//
1310// If there is no unwind rule for a volatile (caller-preserved) register,
1311// the returned AbstractRegisterLocation will be IsUndefined,
1312// indicating that we should stop searching.
1313//
1314// If there is no unwind rule for a non-volatile (callee-preserved)
1315// register, the returned AbstractRegisterLocation will be IsSame.
1316// In frame 0, IsSame means get the value from the live register context.
1317// Else it means to continue descending down the stack to more-live frames
1318// looking for a location/value.
1319//
1320// If an AbstractRegisterLocation is found in an UnwindPlan, that will
1321// be returned, with no consideration of the current ABI rules for
1322// registers. Functions using an alternate ABI calling convention
1323// will work as long as the UnwindPlans are exhaustive about what
1324// registers are volatile/non-volatile.
1325std::optional<UnwindPlan::Row::AbstractRegisterLocation>
1327 lldb::RegisterKind &kind) {
1328 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1329 Log *log = GetLog(LLDBLog::Unwind);
1330
1331 kind = eRegisterKindLLDB;
1333
1334 // First, try to find a register location via the FastUnwindPlan
1336 const UnwindPlan::Row *active_row =
1337 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1338 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1339 UNWIND_LOG(log,
1340 "could not convert lldb regnum {0} ({1}) into {2} "
1341 "RegisterKind reg numbering scheme",
1342 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1343 return {};
1344 }
1345 kind = m_fast_unwind_plan_sp->GetRegisterKind();
1346 // The Fast UnwindPlan typically only provides fp & pc as we move up
1347 // the stack, without requiring additional parsing or memory reads.
1348 // It may mark all other registers as IsUndefined() because, indicating
1349 // that it doesn't know if they were spilled to stack or not.
1350 // If this case, for an IsUndefined register, we should continue on
1351 // to the Full UnwindPlan which may have more accurate information
1352 // about register locations of all registers.
1353 if (active_row &&
1354 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1355 unwindplan_regloc) &&
1356 !unwindplan_regloc.IsUndefined()) {
1357 UNWIND_LOG(
1358 log,
1359 "supplying caller's saved {0} ({1})'s location using FastUnwindPlan",
1360 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1361 return unwindplan_regloc;
1362 }
1363 }
1364
1365 // Second, try to find a register location via the FullUnwindPlan.
1366 bool got_new_full_unwindplan = false;
1367 if (!m_full_unwind_plan_sp) {
1369 got_new_full_unwindplan = true;
1370 }
1374
1375 const UnwindPlan::Row *active_row =
1376 m_full_unwind_plan_sp->GetRowForFunctionOffset(
1378 kind = m_full_unwind_plan_sp->GetRegisterKind();
1379
1380 if (got_new_full_unwindplan && active_row && log) {
1381 StreamString active_row_strm;
1382 ExecutionContext exe_ctx(m_thread.shared_from_this());
1383 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
1384 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
1385 UNWIND_LOG(log, "Using full unwind plan '{0}'",
1386 m_full_unwind_plan_sp->GetSourceName());
1387 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
1388 }
1389
1390 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1391 if (kind == eRegisterKindGeneric)
1392 UNWIND_LOG(log,
1393 "could not convert lldb regnum {0} ({1}) into "
1394 "eRegisterKindGeneric reg numbering scheme",
1395 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1396 else
1397 UNWIND_LOG(log,
1398 "could not convert lldb regnum {0} ({1}) into {2} "
1399 "RegisterKind reg numbering scheme",
1400 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1401 if (active_row && active_row->GetUnspecifiedRegistersAreUndefined()) {
1402 UNWIND_LOG(
1403 log,
1404 "marking register {0} ({1}) as Undefined (volatile) in this "
1405 "stack frame because this row is UnspecifiedRegistersAreUndefined.",
1406 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1407 unwindplan_regloc.SetUndefined();
1408 return unwindplan_regloc;
1409 }
1410 return {};
1411 }
1412
1413 if (regnum.IsValid() && active_row &&
1414 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1415 unwindplan_regloc)) {
1416 UNWIND_LOG(
1417 log,
1418 "supplying caller's saved {0} ({1})'s location using {2} UnwindPlan",
1419 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1420 m_full_unwind_plan_sp->GetSourceName());
1421 return unwindplan_regloc;
1422 }
1423
1424 // When asking for the caller's pc, and did not find a register
1425 // location for PC above in the UnwindPlan. Check if we have a
1426 // Return Address register on this target.
1427 //
1428 // On a Return Address Register architecture like arm/mips/riscv,
1429 // the caller's pc is in the RA register, and will be spilled to
1430 // stack before any other function is called. If no function
1431 // has been called yet, the return address may still be in the
1432 // live RA reg.
1433 //
1434 // There's a lot of variety of what we might see in an UnwindPlan.
1435 // We may have
1436 // ra=IsSame {unncessary}
1437 // ra=StackAddr {caller's return addr spilled to stack}
1438 // or no reg location for pc or ra at all, in a frameless function -
1439 // the caller's return address is in live ra reg.
1440 //
1441 // If a function has been interrupted in a non-call way --
1442 // async signal/sigtramp, or a hardware exception / interrupt / fault --
1443 // then the "pc" and "ra" are two distinct values, and must be
1444 // handled separately. The "pc" is the pc value at the point
1445 // the function was interrupted. The "ra" is the return address
1446 // register value at that point.
1447 // The UnwindPlan for the sigtramp/trap handler will normally have
1448 // register loations for both pc and lr, and so we'll have already
1449 // fetched them above.
1450 if (pc_regnum.IsValid() && pc_regnum == regnum) {
1451 uint32_t return_address_regnum = LLDB_INVALID_REGNUM;
1452
1453 // Get the return address register number from the UnwindPlan
1454 // or the register set definition.
1455 if (m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1457 return_address_regnum =
1458 m_full_unwind_plan_sp->GetReturnAddressRegister();
1459 } else {
1460 RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1462 return_address_regnum = arch_default_ra_regnum.GetAsKind(kind);
1463 }
1464
1465 // This system is using a return address register.
1466 if (return_address_regnum != LLDB_INVALID_REGNUM) {
1467 RegisterNumber return_address_reg;
1468 return_address_reg.init(m_thread,
1469 m_full_unwind_plan_sp->GetRegisterKind(),
1470 return_address_regnum);
1471 UNWIND_LOG(log,
1472 "requested caller's saved PC but this UnwindPlan uses a RA "
1473 "reg; getting {0} ({1}) instead",
1474 return_address_reg.GetName(),
1475 return_address_reg.GetAsKind(eRegisterKindLLDB));
1476
1477 // Do we have a location for the ra register?
1478 if (active_row &&
1479 active_row->GetRegisterInfo(return_address_reg.GetAsKind(kind),
1480 unwindplan_regloc)) {
1481 UNWIND_LOG(log,
1482 "supplying caller's saved {0} ({1})'s location using {2} "
1483 "UnwindPlan",
1484 return_address_reg.GetName(),
1485 return_address_reg.GetAsKind(eRegisterKindLLDB),
1486 m_full_unwind_plan_sp->GetSourceName());
1487 // If we have "ra=IsSame", rewrite to "ra=InRegister(ra)" because the
1488 // calling function thinks it is fetching "pc" and if we return an
1489 // IsSame register location, it will try to read pc.
1490 if (unwindplan_regloc.IsSame())
1491 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1492 return unwindplan_regloc;
1493 } else {
1494 // No unwind rule for the return address reg on frame 0, or an
1495 // interrupted function, means that the caller's address is still in
1496 // RA reg (0th frame) or the trap handler below this one (sigtramp
1497 // etc) has a save location for the RA reg.
1498 if (BehavesLikeZerothFrame()) {
1499 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1500 return unwindplan_regloc;
1501 }
1502 }
1503 }
1504 }
1505 if (active_row && active_row->GetUnspecifiedRegistersAreUndefined()) {
1506 UNWIND_LOG(
1507 log,
1508 "marking register {0} ({1}) as Undefined (volatile) in this "
1509 "stack frame because this row is UnspecifiedRegistersAreUndefined.",
1510 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1511 unwindplan_regloc.SetUndefined();
1512 return unwindplan_regloc;
1513 }
1514 }
1515
1516 ExecutionContext exe_ctx(m_thread.shared_from_this());
1517 Process *process = exe_ctx.GetProcessPtr();
1518
1519 // Third, try finding a register location via the ABI
1520 // FallbackRegisterLocation.
1521 //
1522 // If the UnwindPlan failed to give us an unwind location for this
1523 // register, we may be able to fall back to some ABI-defined default. For
1524 // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1525 // the ABI willset volatile registers to the undefined state.
1526 ABI *abi = process ? process->GetABI().get() : nullptr;
1527 if (abi) {
1528 const RegisterInfo *reg_info =
1530 if (reg_info &&
1531 abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1532 if (!unwindplan_regloc.IsUndefined())
1533 UNWIND_LOG(
1534 log,
1535 "supplying caller's saved {0} ({1})'s location using ABI default",
1536 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1537 // ABI defined volatile registers with no register location
1538 // will be returned as IsUndefined, stopping the search down
1539 // the stack.
1540 return unwindplan_regloc;
1541 }
1542 }
1543
1544 // We have no AbstractRegisterLocation, and the ABI says this is a
1545 // non-volatile / callee-preserved register. Continue down the stack
1546 // or to frame 0 & the live RegisterContext.
1547 std::string unwindplan_name;
1549 unwindplan_name += "via '";
1550 unwindplan_name += m_full_unwind_plan_sp->GetSourceName().AsCString("");
1551 unwindplan_name += "'";
1552 }
1553 UNWIND_LOG(log, "no save location for {0} ({1}) {2}", regnum.GetName(),
1554 regnum.GetAsKind(eRegisterKindLLDB), unwindplan_name);
1555
1556 unwindplan_regloc.SetSame();
1557 return unwindplan_regloc;
1558}
1559
1560// Answer the question: Where did THIS frame save the CALLER frame ("previous"
1561// frame)'s register value?
1562
1565 uint32_t lldb_regnum,
1567 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1568 Log *log = GetLog(LLDBLog::Unwind);
1569
1570 // Have we already found this register location?
1571 if (!m_registers.empty()) {
1572 auto iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1573 if (iterator != m_registers.end()) {
1574 regloc = iterator->second;
1575 UNWIND_LOG(log, "supplying caller's saved {0} ({1})'s location, cached",
1576 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1578 }
1579 }
1580
1581 RegisterKind abs_regkind;
1582 std::optional<UnwindPlan::Row::AbstractRegisterLocation> abs_regloc =
1583 GetAbstractRegisterLocation(lldb_regnum, abs_regkind);
1584
1585 if (!abs_regloc)
1587
1588 if (abs_regloc->IsUndefined()) {
1589 UNWIND_LOG(
1590 log, "did not supply reg location for {0} ({1}) because it is volatile",
1591 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1593 }
1594
1595 ExecutionContext exe_ctx(m_thread.shared_from_this());
1596 Process *process = exe_ctx.GetProcessPtr();
1597 // abs_regloc has valid contents about where to retrieve the register
1598 if (abs_regloc->IsUnspecified()) {
1601 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1602 UNWIND_LOG(log,
1603 "save location for {0} ({1}) is unspecified, continue searching",
1604 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1606 }
1607
1608 if (abs_regloc->IsSame()) {
1609 if (IsFrameZero()) {
1610 regloc.type =
1613 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1614 UNWIND_LOG(log,
1615 "supplying caller's register {0} ({1}) from the live "
1616 "RegisterContext at frame 0",
1617 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1619 }
1620 // PC/RA reg don't follow the usual "callee-saved aka non-volatile" versus
1621 // "caller saved aka volatile" system. A stack frame can provide its caller
1622 // return address, but if we don't find a rule for pc/RA mid-stack, we
1623 // never want to iterate further down the stack looking for it.
1624 // Defensively prevent iterating down the stack for these two.
1625 if (!BehavesLikeZerothFrame() &&
1628 UNWIND_LOG(log,
1629 "register {0} ({1}) is marked as 'IsSame' - it is a pc or "
1630 "return address reg on a frame which does not have all "
1631 "registers available -- treat as if we have no information",
1632 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1634 }
1635
1638 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1639 UNWIND_LOG(log,
1640 "supplying caller's register {0} ({1}) value is unmodified in "
1641 "this frame",
1642 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1644 }
1645
1646 if (abs_regloc->IsCFAPlusOffset()) {
1647 int offset = abs_regloc->GetOffset();
1649 regloc.location.inferred_value = m_cfa + offset;
1650 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1651 UNWIND_LOG(log,
1652 "supplying caller's register {0} ({1}), value is CFA 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->IsAtCFAPlusOffset()) {
1660 int offset = abs_regloc->GetOffset();
1661 regloc.type =
1663 regloc.location.target_memory_location = m_cfa + offset;
1664 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1665 UNWIND_LOG(log,
1666 "supplying caller's register {0} ({1}) from the stack, saved at "
1667 "CFA plus offset {2} [saved at {3:x}]",
1668 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1671 }
1672
1673 if (abs_regloc->IsAFAPlusOffset()) {
1676
1677 int offset = abs_regloc->GetOffset();
1679 regloc.location.inferred_value = m_afa + offset;
1680 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1681 UNWIND_LOG(log,
1682 "supplying caller's register {0} ({1}), value is AFA plus "
1683 "offset {2} [value is {3:x}]",
1684 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1685 regloc.location.inferred_value);
1687 }
1688
1689 if (abs_regloc->IsAtAFAPlusOffset()) {
1692
1693 int offset = abs_regloc->GetOffset();
1694 regloc.type =
1696 regloc.location.target_memory_location = m_afa + offset;
1697 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1698 UNWIND_LOG(log,
1699 "supplying caller's register {0} ({1}) from the stack, saved at "
1700 "AFA plus offset {2} [saved at {3:x}]",
1701 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1704 }
1705
1706 if (abs_regloc->IsInOtherRegister()) {
1707 RegisterNumber row_regnum(m_thread, abs_regkind,
1708 abs_regloc->GetRegisterNumber());
1709 if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1710 UNWIND_LOG(log,
1711 "could not supply caller's {0} ({1}) location - was saved in "
1712 "another reg but couldn't convert that regnum",
1713 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1715 }
1718 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1719 UNWIND_LOG(
1720 log,
1721 "supplying caller's register {0} ({1}), saved in register {2} ({3})",
1722 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1723 row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1725 }
1726
1727 if (abs_regloc->IsDWARFExpression() || abs_regloc->IsAtDWARFExpression()) {
1728 DataExtractor dwarfdata(abs_regloc->GetDWARFExpressionBytes(),
1729 abs_regloc->GetDWARFExpressionLength(),
1730 process->GetByteOrder(),
1731 process->GetAddressByteSize());
1732 ModuleSP opcode_ctx;
1733 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
1734 dwarfexpr.GetMutableExpressionAtAddress()->SetRegisterKind(abs_regkind);
1735 Value cfa_val = Scalar(m_cfa);
1737 llvm::Expected<Value> result =
1738 dwarfexpr.Evaluate(&exe_ctx, this, 0, &cfa_val, nullptr);
1739 if (!result) {
1740 LLDB_LOG_ERROR(log, result.takeError(),
1741 "DWARF expression failed to evaluate: {0}");
1742 } else {
1743 addr_t val;
1744 val = result->GetScalar().ULongLong();
1745 if (abs_regloc->IsDWARFExpression()) {
1746 regloc.type =
1748 regloc.location.inferred_value = val;
1749 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1750 UNWIND_LOG(log,
1751 "supplying caller's register {0} ({1}) via DWARF expression "
1752 "(IsDWARFExpression)",
1753 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1755 } else {
1756 regloc.type = UnwindLLDB::ConcreteRegisterLocation::
1757 eRegisterSavedAtMemoryLocation;
1758 regloc.location.target_memory_location = val;
1759 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1760 UNWIND_LOG(log,
1761 "supplying caller's register {0} ({1}) via DWARF expression "
1762 "(IsAtDWARFExpression)",
1763 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1765 }
1766 }
1767 UNWIND_LOG(log,
1768 "tried to use IsDWARFExpression or IsAtDWARFExpression for {0} "
1769 "({1}) but failed",
1770 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1772 }
1773
1774 if (abs_regloc->IsConstant()) {
1776 regloc.location.inferred_value = abs_regloc->GetConstant();
1777 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1778 UNWIND_LOG(log, "supplying caller's register {0} ({1}) via constant value",
1779 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1781 }
1782
1783 UNWIND_LOG(log, "no save location for {0} ({1}) in this stack frame",
1784 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1785
1786 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1787 // unsupported.
1788
1790}
1791
1794 return {};
1795 ProcessSP process_sp = m_thread.GetProcess();
1796 if (!process_sp)
1797 return {};
1798
1799 UnwindPlanSP arch_override_plan_sp;
1800 if (Architecture *arch = process_sp->GetTarget().GetArchitecturePlugin())
1801 arch_override_plan_sp =
1802 arch->GetArchitectureUnwindPlan(m_thread, this, m_full_unwind_plan_sp);
1803
1804 if (arch_override_plan_sp) {
1805 m_full_unwind_plan_sp = arch_override_plan_sp;
1807 m_registers.clear();
1808 if (Log *log = GetLog(LLDBLog::Unwind)) {
1809 UNWIND_LOG(
1810 log, "Replacing Full Unwindplan with Architecture UnwindPlan, '{0}'",
1811 m_full_unwind_plan_sp->GetSourceName());
1812 const UnwindPlan::Row *active_row =
1813 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1814 if (active_row) {
1815 StreamString active_row_strm;
1816 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
1817 &m_thread,
1818 m_start_pc.GetLoadAddress(&process_sp->GetTarget()));
1819 UNWIND_LOG(log, "{0}", active_row_strm.GetString());
1820 }
1821 }
1822 }
1823
1824 return {};
1825}
1826
1827// TryFallbackUnwindPlan() -- this method is a little tricky.
1828//
1829// When this is called, the frame above -- the caller frame, the "previous"
1830// frame -- is invalid or bad.
1831//
1832// Instead of stopping the stack walk here, we'll try a different UnwindPlan
1833// and see if we can get a valid frame above us.
1834//
1835// This most often happens when an unwind plan based on assembly instruction
1836// inspection is not correct -- mostly with hand-written assembly functions or
1837// functions where the stack frame is set up "out of band", e.g. the kernel
1838// saved the register context and then called an asynchronous trap handler like
1839// _sigtramp.
1840//
1841// Often in these cases, if we just do a dumb stack walk we'll get past this
1842// tricky frame and our usual techniques can continue to be used.
1843
1845 if (m_fallback_unwind_plan_sp == nullptr)
1846 return false;
1847
1848 if (m_full_unwind_plan_sp == nullptr)
1849 return false;
1850
1852 m_full_unwind_plan_sp->GetSourceName() ==
1853 m_fallback_unwind_plan_sp->GetSourceName()) {
1854 return false;
1855 }
1856
1857 // If a compiler generated unwind plan failed, trying the arch default
1858 // unwindplan isn't going to do any better.
1859 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1860 return false;
1861
1862 // Get the caller's pc value and our own CFA value. Swap in the fallback
1863 // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1864 // same, then the fallback unwind plan provides no benefit.
1865
1868
1869 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1870 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1873 regloc) ==
1875 const RegisterInfo *reg_info =
1877 if (reg_info) {
1878 RegisterValue reg_value;
1879 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1880 old_caller_pc_value = reg_value.GetAsUInt64();
1881 if (ProcessSP process_sp = m_thread.GetProcess()) {
1882 if (ABISP abi_sp = process_sp->GetABI())
1883 old_caller_pc_value = abi_sp->FixCodeAddress(old_caller_pc_value);
1884 }
1885 }
1886 }
1887 }
1888
1889 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1890 // impossible register location for the full unwind plan, it may call
1891 // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1892 // unwindplan with the fallback... in short, we're done, we're using the
1893 // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1894 // at the top -- the only way it became nullptr since then is via
1895 // SavedLocationForRegister().
1896 if (m_fallback_unwind_plan_sp == nullptr)
1897 return true;
1898
1899 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1900 // this isn't working, we need to restore. We'll also need to save & restore
1901 // the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1902 std::shared_ptr<const UnwindPlan> original_full_unwind_plan_sp =
1904 addr_t old_cfa = m_cfa;
1905 addr_t old_afa = m_afa;
1906
1907 m_registers.clear();
1908
1910
1911 const UnwindPlan::Row *active_row =
1912 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(
1914
1915 Log *log = GetLog(LLDBLog::Unwind);
1916 if (active_row &&
1917 active_row->GetCFAValue().GetValueType() !=
1919 addr_t new_cfa;
1920 ProcessSP process_sp = m_thread.GetProcess();
1921 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
1922 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1923 active_row->GetCFAValue(), new_cfa) ||
1924 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
1925 UNWIND_LOG(log, "failed to get cfa with fallback unwindplan");
1927 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1928 return false;
1929 }
1930 m_cfa = new_cfa;
1931
1933 active_row->GetAFAValue(), m_afa);
1934
1936 regloc) ==
1938 const RegisterInfo *reg_info =
1940 if (reg_info) {
1941 RegisterValue reg_value;
1942 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
1943 reg_value)) {
1944 new_caller_pc_value = reg_value.GetAsUInt64();
1945 if (process_sp)
1946 new_caller_pc_value =
1947 process_sp->FixCodeAddress(new_caller_pc_value);
1948 }
1949 }
1950 }
1951
1952 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
1953 UNWIND_LOG(log, "failed to get a pc value for the caller frame with the "
1954 "fallback unwind plan");
1956 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1957 m_cfa = old_cfa;
1958 m_afa = old_afa;
1959 return false;
1960 }
1961
1962 if (old_caller_pc_value == new_caller_pc_value &&
1963 m_cfa == old_cfa &&
1964 m_afa == old_afa) {
1965 UNWIND_LOG(log, "fallback unwind plan got the same values for this frame "
1966 "CFA and caller frame pc, not using");
1968 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1969 return false;
1970 }
1971
1972 UNWIND_LOG(log,
1973 "trying to unwind from this function with the UnwindPlan '{0}' "
1974 "because UnwindPlan '{1}' failed.",
1975 m_fallback_unwind_plan_sp->GetSourceName(),
1976 original_full_unwind_plan_sp->GetSourceName());
1977
1978 // We've copied the fallback unwind plan into the full - now clear the
1979 // fallback.
1982 }
1983
1984 return true;
1985}
1986
1988 if (m_fallback_unwind_plan_sp == nullptr)
1989 return false;
1990
1991 if (m_full_unwind_plan_sp == nullptr)
1992 return false;
1993
1995 m_full_unwind_plan_sp->GetSourceName() ==
1996 m_fallback_unwind_plan_sp->GetSourceName()) {
1997 return false;
1998 }
1999
2000 const UnwindPlan::Row *active_row =
2001 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
2002
2003 if (active_row &&
2004 active_row->GetCFAValue().GetValueType() !=
2006 addr_t new_cfa;
2007 ProcessSP process_sp = m_thread.GetProcess();
2008 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
2009 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
2010 active_row->GetCFAValue(), new_cfa) ||
2011 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
2013 "failed to get cfa with fallback unwindplan");
2015 return false;
2016 }
2017
2019 active_row->GetAFAValue(), m_afa);
2020
2023
2024 m_registers.clear();
2025
2026 m_cfa = new_cfa;
2027
2029
2031 "switched unconditionally to the fallback unwindplan {0}",
2032 m_full_unwind_plan_sp->GetSourceName());
2033 return true;
2034 }
2035 return false;
2036}
2037
2039 std::shared_ptr<const UnwindPlan> unwind_plan) {
2040 if (unwind_plan->GetUnwindPlanForSignalTrap() != eLazyBoolYes) {
2041 // Unwind plan does not indicate trap handler. Do nothing. We may
2042 // already be flagged as trap handler flag due to the symbol being
2043 // in the trap handler symbol list, and that should take precedence.
2044 return;
2045 } else if (m_frame_type != eNormalFrame) {
2046 // If this is already a trap handler frame, nothing to do.
2047 // If this is a skip or debug or invalid frame, don't override that.
2048 return;
2049 }
2050
2052
2053 Log *log = GetLog(LLDBLog::Unwind);
2054 UNWIND_LOG(log, "This frame is marked as a trap handler via its UnwindPlan");
2055
2057 // We backed up the pc by 1 to compute the symbol context, but
2058 // now need to undo that because the pc of the trap handler
2059 // frame may in fact be the first instruction of a signal return
2060 // trampoline, rather than the instruction after a call. This
2061 // happens on systems where the signal handler dispatch code, rather
2062 // than calling the handler and being returned to, jumps to the
2063 // handler after pushing the address of a return trampoline on the
2064 // stack -- on these systems, when the handler returns, control will
2065 // be transferred to the return trampoline, so that's the best
2066 // symbol we can present in the callstack.
2067 UNWIND_LOG(log,
2068 "Resetting current offset and re-doing symbol lookup; old "
2069 "symbol was {0}",
2072
2073 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
2074
2075 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
2076
2077 ExecutionContext exe_ctx(m_thread.shared_from_this());
2078 Process *process = exe_ctx.GetProcessPtr();
2079 Target *target = &process->GetTarget();
2080
2081 if (m_sym_ctx_valid) {
2082 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
2083 m_current_offset = m_current_pc.GetLoadAddress(target) -
2084 m_start_pc.GetLoadAddress(target);
2085 }
2086 }
2087}
2088
2090 lldb::RegisterKind row_register_kind, const UnwindPlan::Row::FAValue &fa,
2091 addr_t &address) {
2092 RegisterValue reg_value;
2093
2094 address = LLDB_INVALID_ADDRESS;
2095 addr_t cfa_reg_contents;
2096 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2097
2098 Log *log = GetLog(LLDBLog::Unwind);
2099 switch (fa.GetValueType()) {
2101 UNWIND_LOG(log, "CFA value via dereferencing reg");
2102 RegisterNumber regnum_to_deref(m_thread, row_register_kind,
2103 fa.GetRegisterNumber());
2104 addr_t reg_to_deref_contents;
2105 if (ReadGPRValue(regnum_to_deref, reg_to_deref_contents)) {
2106 const RegisterInfo *reg_info =
2108 RegisterValue reg_value;
2109 if (reg_info) {
2111 reg_info, reg_to_deref_contents, reg_info->byte_size, reg_value);
2112 if (error.Success()) {
2113 address = reg_value.GetAsUInt64();
2114 UNWIND_LOG(log,
2115 "CFA value via dereferencing reg {0} ({1}): reg has val "
2116 "{2:x}, CFA value is {3:x}",
2117 regnum_to_deref.GetName(),
2118 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2119 reg_to_deref_contents, address);
2120 return true;
2121 } else {
2122 UNWIND_LOG(
2123 log,
2124 "Tried to deref reg {0} ({1}) [{2:x}] but memory read failed.",
2125 regnum_to_deref.GetName(),
2126 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2127 reg_to_deref_contents);
2128 }
2129 }
2130 }
2131 break;
2132 }
2134 UNWIND_LOG(log, "CFA value via register plus offset");
2135 RegisterNumber cfa_reg(m_thread, row_register_kind,
2136 fa.GetRegisterNumber());
2137 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
2138 if (!CallFrameAddressIsValid(abi_sp, cfa_reg_contents)) {
2139 UNWIND_LOG(
2140 log,
2141 "Got an invalid CFA register value - reg {0} ({1}), value {2:x}",
2142 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2143 cfa_reg_contents);
2144 return false;
2145 }
2146 address = cfa_reg_contents + fa.GetOffset();
2147 UNWIND_LOG(
2148 log,
2149 "CFA is {0:x}: Register {1} ({2}) contents are {3:x}, offset is {4}",
2150 address, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2151 cfa_reg_contents, fa.GetOffset());
2152 return true;
2153 }
2154 UNWIND_LOG(log, "unable to read CFA register {0} ({1})", cfa_reg.GetName(),
2155 cfa_reg.GetAsKind(eRegisterKindLLDB));
2156 break;
2157 }
2159 UNWIND_LOG(log, "CFA value via DWARF expression");
2160 ExecutionContext exe_ctx(m_thread.shared_from_this());
2161 Process *process = exe_ctx.GetProcessPtr();
2164 process->GetByteOrder(),
2165 process->GetAddressByteSize());
2166 ModuleSP opcode_ctx;
2167 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
2169 row_register_kind);
2170 llvm::Expected<Value> result =
2171 dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr);
2172 if (result) {
2173 address = result->GetScalar().ULongLong();
2174 UNWIND_LOG(log, "CFA value set by DWARF expression is {0:x}", address);
2175 return true;
2176 }
2177 UNWIND_LOG(log, "Failed to set CFA value via DWARF expression: {0}",
2178 fmt_consume(result.takeError()));
2179 break;
2180 }
2182 UNWIND_LOG(log, "CFA value via heuristic search");
2183 Process &process = *m_thread.GetProcess();
2184 lldb::addr_t return_address_hint = GetReturnAddressHint(fa.GetOffset());
2185 if (return_address_hint == LLDB_INVALID_ADDRESS)
2186 return false;
2187 const unsigned max_iterations = 256;
2188 for (unsigned i = 0; i < max_iterations; ++i) {
2189 Status st;
2190 lldb::addr_t candidate_addr =
2191 return_address_hint + i * process.GetAddressByteSize();
2192 lldb::addr_t candidate =
2193 process.ReadPointerFromMemory(candidate_addr, st);
2194 if (st.Fail()) {
2195 UNWIND_LOG(log, "Cannot read memory at {0:x}: {1}", candidate_addr, st);
2196 return false;
2197 }
2198 Address addr;
2199 uint32_t permissions;
2200 if (process.GetLoadAddressPermissions(candidate, permissions) &&
2201 permissions & lldb::ePermissionsExecutable) {
2202 address = candidate_addr;
2203 UNWIND_LOG(log, "Heuristically found CFA: {0:x}", address);
2204 return true;
2205 }
2206 }
2207 UNWIND_LOG(log, "No suitable CFA found");
2208 break;
2209 }
2211 address = fa.GetConstant();
2212 UNWIND_LOG(log, "CFA value set by constant is {0:x}", address);
2213 return true;
2214 }
2215 default:
2216 return false;
2217 }
2218 return false;
2219}
2220
2222 addr_t hint;
2224 return LLDB_INVALID_ADDRESS;
2225 if (!m_sym_ctx.module_sp || !m_sym_ctx.symbol)
2226 return LLDB_INVALID_ADDRESS;
2227 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2228 hint = abi_sp->FixCodeAddress(hint);
2229
2230 hint += plan_offset;
2231
2232 if (auto next = GetNextFrame()) {
2233 if (!next->m_sym_ctx.module_sp || !next->m_sym_ctx.symbol)
2234 return LLDB_INVALID_ADDRESS;
2235 if (auto expected_size =
2236 next->m_sym_ctx.module_sp->GetSymbolFile()->GetParameterStackSize(
2237 *next->m_sym_ctx.symbol))
2238 hint += *expected_size;
2239 else {
2241 "Could not retrieve parameter size: {0}",
2242 fmt_consume(expected_size.takeError()));
2243 return LLDB_INVALID_ADDRESS;
2244 }
2245 }
2246 return hint;
2247}
2248
2249// Retrieve a general purpose register value for THIS frame, as saved by the
2250// NEXT frame, i.e. the frame that
2251// this frame called. e.g.
2252//
2253// foo () { }
2254// bar () { foo (); }
2255// main () { bar (); }
2256//
2257// stopped in foo() so
2258// frame 0 - foo
2259// frame 1 - bar
2260// frame 2 - main
2261// and this RegisterContext is for frame 1 (bar) - if we want to get the pc
2262// value for frame 1, we need to ask
2263// where frame 0 (the "next" frame) saved that and retrieve the value.
2264
2266 uint32_t regnum, addr_t &value) {
2267 if (!IsValid())
2268 return false;
2269
2270 uint32_t lldb_regnum;
2271 if (register_kind == eRegisterKindLLDB) {
2272 lldb_regnum = regnum;
2273 } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2274 register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
2275 return false;
2276 }
2277
2278 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
2279 assert(reg_info);
2280 if (!reg_info) {
2281 UNWIND_LOG(
2283 "Could not find RegisterInfo definition for lldb register number {0}",
2284 lldb_regnum);
2285 return false;
2286 }
2287
2288 uint32_t generic_regnum = LLDB_INVALID_REGNUM;
2289 if (register_kind == eRegisterKindGeneric)
2290 generic_regnum = regnum;
2291 else
2292 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2293 register_kind, regnum, eRegisterKindGeneric, generic_regnum);
2294 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2295
2296 RegisterValue reg_value;
2297 // if this is frame 0 (currently executing frame), get the requested reg
2298 // contents from the actual thread registers
2299 if (IsFrameZero()) {
2300 if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
2301 value = reg_value.GetAsUInt64();
2302 if (abi_sp && generic_regnum != LLDB_INVALID_REGNUM) {
2303 if (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2304 generic_regnum == LLDB_REGNUM_GENERIC_RA)
2305 value = abi_sp->FixCodeAddress(value);
2306 }
2307 return true;
2308 }
2309 return false;
2310 }
2311
2312 bool pc_register = false;
2313 if (generic_regnum != LLDB_INVALID_REGNUM &&
2314 (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2315 generic_regnum == LLDB_REGNUM_GENERIC_RA))
2316 pc_register = true;
2317
2319 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2320 lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
2321 return false;
2322 }
2323 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
2324 value = reg_value.GetAsUInt64();
2325 if (pc_register) {
2326 if (ABISP abi_sp = m_thread.GetProcess()->GetABI()) {
2327 value = abi_sp->FixCodeAddress(value);
2328 }
2329 }
2330 return true;
2331 }
2332 return false;
2333}
2334
2336 addr_t &value) {
2337 return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
2338 value);
2339}
2340
2341// Find the value of a register in THIS frame
2342
2344 RegisterValue &value) {
2345 if (!IsValid())
2346 return false;
2347
2348 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2349 Log *log = GetLog(LLDBLog::Unwind);
2350 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2351 lldb_regnum);
2352
2353 // If this is the 0th frame, hand this over to the live register context
2354 if (IsFrameZero()) {
2356 "passing along to the live register context for reg {0}",
2357 lldb_regnum);
2358 return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
2359 }
2360
2361 bool is_pc_regnum = false;
2364 is_pc_regnum = true;
2365 }
2366
2368 // Find out where the NEXT frame saved THIS frame's register contents
2369 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2370 lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
2371 return false;
2372
2373 bool result = ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
2374 if (result) {
2375 if (is_pc_regnum && value.GetType() == RegisterValue::eTypeUInt64) {
2376 addr_t reg_value = value.GetAsUInt64(LLDB_INVALID_ADDRESS);
2377 if (reg_value != LLDB_INVALID_ADDRESS) {
2378 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2379 value = abi_sp->FixCodeAddress(reg_value);
2380 }
2381 }
2382 }
2383 return result;
2384}
2385
2387 const RegisterValue &value) {
2388 if (!IsValid())
2389 return false;
2390
2391 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2392 Log *log = GetLog(LLDBLog::Unwind);
2393 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2394 lldb_regnum);
2395
2396 // If this is the 0th frame, hand this over to the live register context
2397 if (IsFrameZero()) {
2399 "passing along to the live register context for reg {0}",
2400 lldb_regnum);
2401 return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
2402 }
2403
2405 // Find out where the NEXT frame saved THIS frame's register contents
2406 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2407 lldb_regnum, regloc, m_frame_number - 1, false))
2408 return false;
2409
2410 return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
2411}
2412
2413// Don't need to implement this one
2415 lldb::WritableDataBufferSP &data_sp) {
2416 return false;
2417}
2418
2419// Don't need to implement this one
2421 const lldb::DataBufferSP &data_sp) {
2422 return false;
2423}
2424
2425// Retrieve the pc value for THIS from
2426
2428 if (!IsValid()) {
2429 return false;
2430 }
2431 if (m_cfa == LLDB_INVALID_ADDRESS) {
2432 return false;
2433 }
2434 cfa = m_cfa;
2435 return true;
2436}
2437
2440 if (m_frame_number == 0)
2441 return regctx;
2442 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
2443}
2444
2449
2450// Retrieve the address of the start of the function of THIS frame
2451
2453 if (!IsValid())
2454 return false;
2455
2456 if (!m_start_pc.IsValid()) {
2457 bool read_successfully = ReadPC (start_pc);
2458 if (read_successfully)
2459 {
2460 ProcessSP process_sp (m_thread.GetProcess());
2461 if (process_sp)
2462 {
2463 if (ABISP abi_sp = process_sp->GetABI())
2464 start_pc = abi_sp->FixCodeAddress(start_pc);
2465 }
2466 }
2467 return read_successfully;
2468 }
2469 start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2470 return true;
2471}
2472
2473// Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2474
2476 if (!IsValid())
2477 return false;
2478
2479 bool above_trap_handler = false;
2480 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2482 above_trap_handler = true;
2483
2485 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2486 // indicates the end of a stack walk.
2487 // On the currently executing frame (or such a frame interrupted
2488 // asynchronously by sigtramp et al) this may occur if code has jumped
2489 // through a NULL pointer -- we want to be able to unwind past that frame
2490 // to help find the bug.
2491
2492 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2493 pc = abi_sp->FixCodeAddress(pc);
2494
2495 return !(m_all_registers_available == false &&
2496 above_trap_handler == false && (pc == 0 || pc == 1));
2497 } else {
2498 return false;
2499 }
2500}
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:744
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:357
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:2804
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3912
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2508
uint32_t GetAddressByteSize() const
Definition Process.cpp:3916
virtual DynamicLoader * GetDynamicLoader()
Get the dynamic loader plug-in for this process.
Definition Process.cpp:3093
const lldb::ABISP & GetABI()
Definition Process.cpp:1483
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1256
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:1969
const ArchSpec & GetArchitecture() const
Definition Target.h:1283
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
bool GetUnspecifiedRegistersAreUndefined() const
Definition UnwindPlan.h:412
@ 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