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/Error.h"
41#include "llvm/Support/FormatAdapters.h"
42#include <cassert>
43#include <memory>
44
45using namespace lldb;
46using namespace lldb_private;
47
49 if (sym_ctx.symbol)
50 return sym_ctx.symbol->GetName();
51 else if (sym_ctx.function)
52 return sym_ctx.function->GetName();
53 return ConstString();
54}
55
56static bool CallFrameAddressIsValid(ABISP abi_sp, lldb::addr_t cfa) {
57 if (cfa == LLDB_INVALID_ADDRESS)
58 return false;
59 if (abi_sp)
60 return abi_sp->CallFrameAddressIsValid(cfa);
61 return cfa != 0 && cfa != 1;
62}
63
64/// Identify a clang outlined function by symbol name.
65///
66/// The unwind information in outlined functions from clang can be
67/// incorrect, and because of when the outlining happens in the compilation,
68/// it may not be possible to fix. We will need to ignore any
69/// instruction-emulation or compiler-sourced unwind plans for these
70/// functions, and fall back to an ABI default unwindplan.
71static bool IsClangOutlinedFunction(const SymbolContext &sym_ctx) {
72 llvm::StringRef name = GetSymbolOrFunctionName(sym_ctx).GetStringRef();
73 if (name.starts_with("OUTLINED_FUNCTION_"))
74 return true;
75 return false;
76}
77
78#define UNWIND_LOG_IMPL(LOG_FN, log, ...) \
79 LOG_FN(log, "{0}th{1}/fr{2} {3}", \
80 llvm::indent(std::min(m_frame_number, 100U)), m_thread.GetIndexID(), \
81 m_frame_number, llvm::formatv(__VA_ARGS__))
82
83#define UNWIND_LOG(log, ...) UNWIND_LOG_IMPL(LLDB_LOG, log, __VA_ARGS__)
84
85#define UNWIND_LOG_VERBOSE(log, ...) \
86 UNWIND_LOG_IMPL(LLDB_LOG_VERBOSE, log, __VA_ARGS__)
87
89 const SharedPtr &next_frame,
90 SymbolContext &sym_ctx,
91 uint32_t frame_number,
92 UnwindLLDB &unwind_lldb)
93 : RegisterContext(thread, frame_number), m_thread(thread),
100 m_sym_ctx_valid(false), m_frame_number(frame_number), m_registers(),
101 m_parent_unwind(unwind_lldb) {
102 m_sym_ctx.Clear(false);
103 m_sym_ctx_valid = false;
104
105 if (IsFrameZero()) {
107 } else {
109 }
110
111 // This same code exists over in the GetFullUnwindPlanForFrame() but it may
112 // not have been executed yet
113 if (IsFrameZero() || next_frame->m_frame_type == eTrapHandlerFrame ||
114 next_frame->m_frame_type == eDebuggerFrame) {
115 m_all_registers_available = true;
116 }
117}
118
120 std::shared_ptr<const UnwindPlan> unwind_plan_sp) {
121 if (!unwind_plan_sp)
122 return false;
123
124 // check if m_current_pc is valid
125 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
126 // yes - current offset can be used as is
127 return true;
128 }
129
130 // If don't have an offset or we're at the start of the function, we've got
131 // nothing else to try.
133 return false;
134
135 // check pc - 1 to see if it's valid
136 Address pc_minus_one(m_current_pc);
137 pc_minus_one.Slide(-1);
138 if (unwind_plan_sp->PlanValidAtAddress(pc_minus_one)) {
139 return true;
140 }
141
142 return false;
143}
144
145// Initialize a RegisterContextUnwind which is the first frame of a stack -- the
146// zeroth frame or currently executing frame.
147
149 Log *log = GetLog(LLDBLog::Unwind);
150 ExecutionContext exe_ctx(m_thread.shared_from_this());
151 RegisterContextSP reg_ctx_sp = m_thread.GetRegisterContext();
152
153 if (reg_ctx_sp.get() == nullptr) {
155 UNWIND_LOG(log, "frame does not have a register context");
156 return;
157 }
158
159 addr_t current_pc = reg_ctx_sp->GetPC();
160
161 if (current_pc == LLDB_INVALID_ADDRESS) {
163 UNWIND_LOG(log, "frame does not have a pc");
164 return;
165 }
166
167 Process *process = exe_ctx.GetProcessPtr();
168
169 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
170 // this will strip bit zero in case we read a PC from memory or from the LR.
171 // (which would be a no-op in frame 0 where we get it from the register set,
172 // but still a good idea to make the call here for other ABIs that may
173 // exist.)
174 if (ABISP abi_sp = process->GetABI())
175 current_pc = abi_sp->FixCodeAddress(current_pc);
176
177 std::shared_ptr<const UnwindPlan> lang_runtime_plan_sp =
180 if (lang_runtime_plan_sp.get()) {
181 UNWIND_LOG(log, "This is an async frame");
182 }
183
184 // Initialize m_current_pc, an Address object, based on current_pc, an
185 // addr_t.
186 m_current_pc.SetLoadAddress(current_pc, &process->GetTarget());
187
188 // If we don't have a Module for some reason, we're not going to find
189 // symbol/function information - just stick in some reasonable defaults and
190 // hope we can unwind past this frame.
191 ModuleSP pc_module_sp(m_current_pc.GetModule());
192 if (!m_current_pc.IsValid() || !pc_module_sp) {
193 UNWIND_LOG(log, "using architectural default unwind method");
194 }
195
196 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
197
198 if (m_sym_ctx.symbol) {
199 UNWIND_LOG(log, "with pc value of {0:x}, symbol name is '{1}'", current_pc,
201 } else if (m_sym_ctx.function) {
202 UNWIND_LOG(log, "with pc value of {0:x}, function name is '{1}'",
204 } else {
205 UNWIND_LOG(log, "with pc value of {0:x}, no symbol/function name is known.",
206 current_pc);
207 }
208
209 if (IsTrapHandler(process)) {
211 } else {
212 // FIXME: Detect eDebuggerFrame here.
214 }
215
216 // If we were able to find a symbol/function, set addr_range to the bounds of
217 // that symbol/function. else treat the current pc value as the start_pc and
218 // record no offset.
219 if (m_sym_ctx_valid) {
220 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
221 if (m_current_pc.GetModule() == m_start_pc.GetModule()) {
223 m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
224 }
226 } else if (Address start =
227 pc_module_sp ? pc_module_sp->GetUnwindTable()
228 .GetFunctionStartFromObjectFileUnwindInfo(
230 : Address();
231 start.IsValid()) {
232 m_start_pc = start;
234 m_current_pc.GetFileAddress() - m_start_pc.GetFileAddress();
236 } else {
238 m_current_offset = std::nullopt;
239 m_current_offset_backed_up_one = std::nullopt;
240 }
241
242 // We've set m_frame_type and m_sym_ctx before these calls.
243
246
247 const UnwindPlan::Row *active_row = nullptr;
248 lldb::RegisterKind row_register_kind = eRegisterKindGeneric;
249
250 // If we have LanguageRuntime UnwindPlan for this unwind, use those
251 // rules to find the caller frame instead of the function's normal
252 // UnwindPlans. The full unwind plan for this frame will be
253 // the LanguageRuntime-provided unwind plan, and there will not be a
254 // fast unwind plan.
255 if (lang_runtime_plan_sp.get()) {
256 active_row =
257 lang_runtime_plan_sp->GetRowForFunctionOffset(m_current_offset);
258 row_register_kind = lang_runtime_plan_sp->GetRegisterKind();
259 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(),
260 m_cfa)) {
261 UNWIND_LOG(log, "Cannot set cfa");
262 } else {
263 m_full_unwind_plan_sp = lang_runtime_plan_sp;
264 if (log) {
265 StreamString active_row_strm;
266 active_row->Dump(active_row_strm, lang_runtime_plan_sp.get(), &m_thread,
267 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
268 UNWIND_LOG(log, "async active row: {0}", active_row_strm.GetString());
269 }
270 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
271 UNWIND_LOG(log,
272 "initialized async frame current pc is {0:x} cfa is {1:x} afa "
273 "is {2:x}",
274 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa,
275 m_afa);
276
277 return;
278 }
279 }
280
282 m_full_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
283 active_row =
284 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
285 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
287 if (active_row && log) {
288 StreamString active_row_strm;
289 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
290 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
291 UNWIND_LOG(log, "{0}", active_row_strm.GetString());
292 }
293 }
294
295 if (!active_row) {
296 UNWIND_LOG(log, "could not find an unwindplan row for this frame's pc");
298 return;
299 }
300
301 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
302 // Try the fall back unwind plan since the
303 // full unwind plan failed.
304 FuncUnwindersSP func_unwinders_sp;
305 std::shared_ptr<const UnwindPlan> call_site_unwind_plan;
306 bool cfa_status = false;
307
308 if (m_sym_ctx_valid) {
309 func_unwinders_sp =
310 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
312 }
313
314 if (func_unwinders_sp.get() != nullptr)
315 call_site_unwind_plan = func_unwinders_sp->GetUnwindPlanAtCallSite(
316 process->GetTarget(), m_thread);
317
318 if (call_site_unwind_plan != nullptr) {
319 m_fallback_unwind_plan_sp = call_site_unwind_plan;
321 cfa_status = true;
322 }
323 if (!cfa_status) {
324 UNWIND_LOG(log, "could not read CFA value for first frame.");
326 return;
327 }
328 } else
329 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
330
332 UNWIND_LOG(log,
333 "could not read CFA or AFA values for first frame, not valid.");
335 return;
336 }
337
338 // Give the Architecture a chance to replace the UnwindPlan.
340
341 UNWIND_LOG(log,
342 "initialized frame current pc is {0:x} cfa is {1:x} afa is {2:x} "
343 "using {3} UnwindPlan",
344 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa, m_afa,
345 m_full_unwind_plan_sp->GetSourceName());
346}
347
348// Initialize a RegisterContextUnwind for the non-zeroth frame -- rely on the
349// RegisterContextUnwind "below" it to provide things like its current pc value.
350
352 Log *log = GetLog(LLDBLog::Unwind);
353 if (IsFrameZero()) {
355 UNWIND_LOG(log, "non-zeroth frame tests positive for IsFrameZero -- that "
356 "shouldn't happen.");
357 return;
358 }
359
360 if (!GetNextFrame().get() || !GetNextFrame()->IsValid()) {
362 UNWIND_LOG(log, "Could not get next frame, marking this frame as invalid.");
363 return;
364 }
365 if (!m_thread.GetRegisterContext()) {
367 UNWIND_LOG(log, "Could not get register context for this thread, marking "
368 "this frame as invalid.");
369 return;
370 }
371
372 ExecutionContext exe_ctx(m_thread.shared_from_this());
373 Process *process = exe_ctx.GetProcessPtr();
374
375 // Some languages may have a logical parent stack frame which is
376 // not a real stack frame, but the programmer would consider it to
377 // be the caller of the frame, e.g. Swift asynchronous frames.
378 //
379 // A LanguageRuntime may provide an UnwindPlan that is used in this
380 // stack trace base on the RegisterContext contents, intsead
381 // of the normal UnwindPlans we would use for the return-pc.
382 std::shared_ptr<const UnwindPlan> lang_runtime_plan_sp =
385 if (lang_runtime_plan_sp.get()) {
386 UNWIND_LOG(log, "This is an async frame");
387 }
388
389 addr_t pc;
391 UNWIND_LOG(log, "could not get pc value");
393 return;
394 }
395
396 // Let ABIs fixup code addresses to make sure they are valid. In ARM ABIs
397 // this will strip bit zero in case we read a PC from memory or from the LR.
398 ABISP abi_sp = process->GetABI();
399 if (abi_sp)
400 pc = abi_sp->FixCodeAddress(pc);
401
402 if (log) {
403 UNWIND_LOG(log, "pc = {0:x}", pc);
404 addr_t reg_val;
406 UNWIND_LOG(log, "fp = {0:x}", reg_val);
408 UNWIND_LOG(log, "sp = {0:x}", reg_val);
409 }
410
411 // A pc of 0x0 means it's the end of the stack crawl unless we're above a trap
412 // handler function
413 bool above_trap_handler = false;
414 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
416 above_trap_handler = true;
417
418 if (pc == 0 || pc == 0x1) {
419 if (!above_trap_handler) {
421 UNWIND_LOG(log, "this frame has a pc of 0x0");
422 return;
423 }
424 }
425
426 const bool allow_section_end = true;
427 m_current_pc.SetLoadAddress(pc, &process->GetTarget(), allow_section_end);
428
429 // If we don't have a Module for some reason, we're not going to find
430 // symbol/function information - just stick in some reasonable defaults and
431 // hope we can unwind past this frame. If we're above a trap handler,
432 // we may be at a bogus address because we jumped through a bogus function
433 // pointer and trapped, so don't force the arch default unwind plan in that
434 // case.
435 ModuleSP pc_module_sp(m_current_pc.GetModule());
436 if ((!m_current_pc.IsValid() || !pc_module_sp) &&
437 above_trap_handler == false) {
438 UNWIND_LOG(log, "using architectural default unwind method");
439
440 // Test the pc value to see if we know it's in an unmapped/non-executable
441 // region of memory.
442 uint32_t permissions;
443 if (process->GetLoadAddressPermissions(pc, permissions) &&
444 (permissions & ePermissionsExecutable) == 0) {
445 // If this is the second frame off the stack, we may have unwound the
446 // first frame incorrectly. But using the architecture default unwind
447 // plan may get us back on track -- albeit possibly skipping a real
448 // frame. Give this frame a clearly-invalid pc and see if we can get any
449 // further.
450 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
452 UNWIND_LOG(log,
453 "had a pc of {0:x} which is not in executable memory but on "
454 "frame 1 -- allowing it once.",
455 pc);
457 } else {
458 // anywhere other than the second frame, a non-executable pc means
459 // we're off in the weeds -- stop now.
461 UNWIND_LOG(log, "pc is in a non-executable section of memory and this "
462 "isn't the 2nd frame in the stack walk.");
463 return;
464 }
465 }
466
467 if (abi_sp) {
468 m_fast_unwind_plan_sp.reset();
469 m_full_unwind_plan_sp = abi_sp->CreateDefaultUnwindPlan();
470 assert(((!m_full_unwind_plan_sp ||
471 m_full_unwind_plan_sp->GetRowCount() == 0 ||
472 m_full_unwind_plan_sp->GetRowAtIndex(0)
473 ->GetUnspecifiedRegistersAreUndefined())) &&
474 "Default UnwindPlan must set "
475 "UnspecifiedRegistersAreUndefined to true");
476 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
477 {
479 }
481 m_current_offset = std::nullopt;
482 m_current_offset_backed_up_one = std::nullopt;
483 RegisterKind row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
484 if (const UnwindPlan::Row *row =
485 m_full_unwind_plan_sp->GetRowForFunctionOffset(0)) {
486 if (!ReadFrameAddress(row_register_kind, row->GetCFAValue(), m_cfa)) {
487 UNWIND_LOG(log, "failed to get cfa value");
488 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
489 {
491 }
492 return;
493 }
494
495 ReadFrameAddress(row_register_kind, row->GetAFAValue(), m_afa);
496
497 // A couple of sanity checks..
498 if (!CallFrameAddressIsValid(abi_sp, m_cfa)) {
499 UNWIND_LOG(log, "could not find a valid cfa address");
501 return;
502 }
503
504 // m_cfa should point into the stack memory; if we can query memory
505 // region permissions, see if the memory is allocated & readable.
506 if (process->GetLoadAddressPermissions(m_cfa, permissions) &&
507 (permissions & ePermissionsReadable) == 0) {
510 log, "the CFA points to a region of memory that is not readable");
511 return;
512 }
513 } else {
514 UNWIND_LOG(log, "could not find a row for function offset zero");
516 return;
517 }
518
519 if (CheckIfLoopingStack()) {
521 if (CheckIfLoopingStack()) {
522 UNWIND_LOG(log, "same CFA address as next frame, assuming the unwind "
523 "is looping - stopping");
525 return;
526 }
527 }
528
529 // Give the Architecture a chance to replace the UnwindPlan.
531
532 UNWIND_LOG(log, "initialized frame cfa is {0:x} afa is {1:x}", m_cfa,
533 m_afa);
534 return;
535 }
537 UNWIND_LOG(log, "could not find any symbol for this pc, or a default "
538 "unwind plan, to continue unwind.");
539 return;
540 }
541
542 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
543
544 if (m_sym_ctx.symbol) {
545 UNWIND_LOG(log, "with pc value of {0:x}, symbol name is '{1}'", pc,
547 } else if (m_sym_ctx.function) {
548 UNWIND_LOG(log, "with pc value of {0:x}, function name is '{1}'", pc,
550 } else {
551 UNWIND_LOG(log, "with pc value of {0:x}, no symbol/function name is known.",
552 pc);
553 }
554
555 bool decr_pc_and_recompute_addr_range;
556
557 if (!m_sym_ctx_valid) {
558 // Always decrement and recompute if the symbol lookup failed
559 decr_pc_and_recompute_addr_range = true;
562 // Don't decrement if we're "above" an asynchronous event like
563 // sigtramp.
564 decr_pc_and_recompute_addr_range = false;
565 } else if (Address addr = m_sym_ctx.GetFunctionOrSymbolAddress();
566 addr != m_current_pc) {
567 // If our "current" pc isn't the start of a function, decrement the pc
568 // if we're up the stack.
570 decr_pc_and_recompute_addr_range = false;
571 else
572 decr_pc_and_recompute_addr_range = true;
573 } else if (IsTrapHandler(process)) {
574 // Signal dispatch may set the return address of the handler it calls to
575 // point to the first byte of a return trampoline (like
576 // __kernel_rt_sigreturn), so do not decrement and recompute if the current
577 // function is a trap handler.
578 decr_pc_and_recompute_addr_range = false;
579 } else if (m_behaves_like_zeroth_frame) {
580 decr_pc_and_recompute_addr_range = false;
581 } else {
582 // Decrement to find the function containing the call.
583 decr_pc_and_recompute_addr_range = true;
584 }
585
586 // We need to back up the pc by 1 byte and re-search for the Symbol to handle
587 // the case where the "saved pc" value is pointing to the next function, e.g.
588 // if a function ends with a CALL instruction.
589 // FIXME this may need to be an architectural-dependent behavior; if so we'll
590 // need to add a member function
591 // to the ABI plugin and consult that.
592 if (decr_pc_and_recompute_addr_range) {
593 UNWIND_LOG(log,
594 "Backing up the pc value of {0:x} by 1 and re-doing symbol "
595 "lookup; old symbol was {1}",
597 Address temporary_pc;
598 temporary_pc.SetLoadAddress(pc - 1, &process->GetTarget());
599 m_sym_ctx.Clear(false);
601
602 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
603 }
604
605 // If we were able to find a symbol/function, set addr_range_ptr to the
606 // bounds of that symbol/function. else treat the current pc value as the
607 // start_pc and record no offset.
608 if (m_sym_ctx_valid) {
609 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
610 m_current_offset = pc - m_start_pc.GetLoadAddress(&process->GetTarget());
612 if (decr_pc_and_recompute_addr_range &&
615 if (m_sym_ctx_valid) {
616 m_current_pc.SetLoadAddress(pc - 1, &process->GetTarget());
617 }
618 }
619 } else {
621 m_current_offset = std::nullopt;
622 m_current_offset_backed_up_one = std::nullopt;
623 }
624
625 if (IsTrapHandler(process)) {
627 } else {
628 // FIXME: Detect eDebuggerFrame here.
629 if (m_frame_type != eSkipFrame) // don't override eSkipFrame
630 {
632 }
633 }
634
635 const UnwindPlan::Row *active_row;
636 RegisterKind row_register_kind = eRegisterKindGeneric;
637
638 // If we have LanguageRuntime UnwindPlan for this unwind, use those
639 // rules to find the caller frame instead of the function's normal
640 // UnwindPlans. The full unwind plan for this frame will be
641 // the LanguageRuntime-provided unwind plan, and there will not be a
642 // fast unwind plan.
643 if (lang_runtime_plan_sp.get()) {
644 active_row =
645 lang_runtime_plan_sp->GetRowForFunctionOffset(m_current_offset);
646 row_register_kind = lang_runtime_plan_sp->GetRegisterKind();
647 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(),
648 m_cfa)) {
649 UNWIND_LOG(log, "Cannot set cfa");
650 } else {
651 m_full_unwind_plan_sp = lang_runtime_plan_sp;
652 if (log) {
653 StreamString active_row_strm;
654 active_row->Dump(active_row_strm, lang_runtime_plan_sp.get(), &m_thread,
655 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
656 UNWIND_LOG(log, "async active row: {0}", active_row_strm.GetString());
657 }
658 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
659 UNWIND_LOG(log,
660 "initialized async frame current pc is {0:x} cfa is {1:x} afa "
661 "is {2:x}",
662 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa,
663 m_afa);
664
665 return;
666 }
667 }
668
669 // We've set m_frame_type and m_sym_ctx before this call.
671
672 // Try to get by with just the fast UnwindPlan if possible - the full
673 // UnwindPlan may be expensive to get (e.g. if we have to parse the entire
674 // eh_frame section of an ObjectFile for the first time.)
675
677 m_fast_unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
678 active_row =
679 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
680 row_register_kind = m_fast_unwind_plan_sp->GetRegisterKind();
682 if (active_row && log) {
683 StreamString active_row_strm;
684 active_row->Dump(active_row_strm, m_fast_unwind_plan_sp.get(), &m_thread,
685 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
686 UNWIND_LOG(log, "Using fast unwind plan '{0}'",
687 m_fast_unwind_plan_sp->GetSourceName());
688 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
689 }
690 } else {
693 active_row = m_full_unwind_plan_sp->GetRowForFunctionOffset(
695 row_register_kind = m_full_unwind_plan_sp->GetRegisterKind();
697 if (active_row && log) {
698 StreamString active_row_strm;
699 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
700 &m_thread,
701 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
702 UNWIND_LOG(log, "Using full unwind plan '{0}'",
703 m_full_unwind_plan_sp->GetSourceName());
704 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
705 }
706 }
707 }
708
709 if (!active_row) {
711 UNWIND_LOG(log, "could not find unwind row for this pc");
712 return;
713 }
714
715 if (!ReadFrameAddress(row_register_kind, active_row->GetCFAValue(), m_cfa)) {
716 UNWIND_LOG(log, "failed to get cfa");
718 return;
719 }
720
721 ReadFrameAddress(row_register_kind, active_row->GetAFAValue(), m_afa);
722
723 UNWIND_LOG(log, "m_cfa = {0:x} m_afa = {1:x}", m_cfa, m_afa);
724
725 if (CheckIfLoopingStack()) {
727 if (CheckIfLoopingStack()) {
728 UNWIND_LOG(log, "same CFA address as next frame, assuming the unwind is "
729 "looping - stopping");
731 return;
732 }
733 }
734
735 // Give the Architecture a chance to replace the UnwindPlan.
737
738 UNWIND_LOG(log,
739 "initialized frame current pc is {0:x} cfa is {1:x} afa is {2:x}",
740 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr()), m_cfa, m_afa);
741}
742
744 // If we have a bad stack setup, we can get the same CFA value multiple times
745 // -- or even more devious, we can actually oscillate between two CFA values.
746 // Detect that here and break out to avoid a possible infinite loop in lldb
747 // trying to unwind the stack. To detect when we have the same CFA value
748 // multiple times, we compare the
749 // CFA of the current
750 // frame with the 2nd next frame because in some specail case (e.g. signal
751 // hanlders, hand written assembly without ABI compliance) we can have 2
752 // frames with the same
753 // CFA (in theory we
754 // can have arbitrary number of frames with the same CFA, but more then 2 is
755 // very unlikely)
756
758 if (next_frame) {
759 RegisterContextUnwind::SharedPtr next_next_frame =
760 next_frame->GetNextFrame();
761 addr_t next_next_frame_cfa = LLDB_INVALID_ADDRESS;
762 if (next_next_frame && next_next_frame->GetCFA(next_next_frame_cfa)) {
763 if (next_next_frame_cfa == m_cfa) {
764 // We have a loop in the stack unwind
765 return true;
766 }
767 }
768 }
769 return false;
770}
771
773
775 if (m_frame_number == 0)
776 return true;
778 return true;
779 return false;
780}
781
782// Find a fast unwind plan for this frame, if possible.
783//
784// On entry to this method,
785//
786// 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
787// if either of those are correct,
788// 2. m_sym_ctx should already be filled in, and
789// 3. m_current_pc should have the current pc value for this frame
790// 4. m_current_offset_backed_up_one should have the current byte offset into
791// the function, maybe backed up by 1, std::nullopt if unknown
792
793std::shared_ptr<const UnwindPlan>
795 ModuleSP pc_module_sp(m_current_pc.GetModule());
796
797 if (!m_current_pc.IsValid() || !pc_module_sp ||
798 pc_module_sp->GetObjectFile() == nullptr)
799 return nullptr;
800
801 if (IsFrameZero())
802 return nullptr;
803
804 FuncUnwindersSP func_unwinders_sp(
805 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
807 if (!func_unwinders_sp)
808 return nullptr;
809
810 // If we're in _sigtramp(), unwinding past this frame requires special
811 // knowledge.
813 return nullptr;
814
815 if (std::shared_ptr<const UnwindPlan> unwind_plan_sp =
816 func_unwinders_sp->GetUnwindPlanFastUnwind(
817 *m_thread.CalculateTarget(), m_thread)) {
818 if (unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
820 return unwind_plan_sp;
821 }
822 }
823 return nullptr;
824}
825
826// On entry to this method,
827//
828// 1. m_frame_type should already be set to eTrapHandlerFrame/eDebuggerFrame
829// if either of those are correct,
830// 2. m_sym_ctx should already be filled in, and
831// 3. m_current_pc should have the current pc value for this frame
832// 4. m_current_offset_backed_up_one should have the current byte offset into
833// the function, maybe backed up by 1, std::nullopt if unknown
834
835std::shared_ptr<const UnwindPlan>
837 Log *log = GetLog(LLDBLog::Unwind);
838 std::shared_ptr<const UnwindPlan> arch_default_unwind_plan_sp;
839 ExecutionContext exe_ctx(m_thread.shared_from_this());
840 Process *process = exe_ctx.GetProcessPtr();
841 ABI *abi = process ? process->GetABI().get() : nullptr;
842 if (abi) {
843 arch_default_unwind_plan_sp = abi->CreateDefaultUnwindPlan();
844 assert(((!arch_default_unwind_plan_sp ||
845 arch_default_unwind_plan_sp->GetRowCount() == 0 ||
846 arch_default_unwind_plan_sp->GetRowAtIndex(0)
847 ->GetUnspecifiedRegistersAreUndefined())) &&
848 "Default UnwindPlan must set "
849 "UnspecifiedRegistersAreUndefined to true");
850 } else {
852 log, "unable to get architectural default UnwindPlan from ABI plugin");
853 }
854
858 // If this frame behaves like a 0th frame (currently executing or
859 // interrupted asynchronously), all registers can be retrieved.
861 }
862
863 // If we've done a jmp 0x0 / bl 0x0 (called through a null function pointer)
864 // so the pc is 0x0 in the zeroth frame, we need to use the "unwind at first
865 // instruction" arch default UnwindPlan Also, if this Process can report on
866 // memory region attributes, any non-executable region means we jumped
867 // through a bad function pointer - handle the same way as 0x0. Note, if we
868 // have a symbol context & a symbol, we don't want to follow this code path.
869 // This is for jumping to memory regions without any information available.
870
871 if ((!m_sym_ctx_valid ||
872 (m_sym_ctx.function == nullptr && m_sym_ctx.symbol == nullptr)) &&
874 uint32_t permissions;
875 addr_t current_pc_addr =
876 m_current_pc.GetLoadAddress(exe_ctx.GetTargetPtr());
877 if (current_pc_addr == 0 ||
878 (process &&
879 process->GetLoadAddressPermissions(current_pc_addr, permissions) &&
880 (permissions & ePermissionsExecutable) == 0)) {
881 if (abi) {
883 return abi->CreateFunctionEntryUnwindPlan();
884 }
885 }
886 }
887
888 // No Module for the current pc, try using the architecture default unwind.
889 ModuleSP pc_module_sp(m_current_pc.GetModule());
890 if (!m_current_pc.IsValid() || !pc_module_sp ||
891 pc_module_sp->GetObjectFile() == nullptr) {
893 return arch_default_unwind_plan_sp;
894 }
895
896 // Function outlining is a clang feature where common blocks of instructions
897 // from separate functions can be put in a separate utility function, and
898 // the original functions call into the utility function to execute them,
899 // resulting in fewer bytes used for the code section overall.
900 //
901 // The call to the OUTLINED_FUNCTION may not be a normal ABI call (e.g.
902 // on RISCV it might be called `jal t0, OUTLINED_FUNCTION_<nn>` putting the
903 // return address in a temporary register instead of $ra). The unwind
904 // instructions in eh_frame/debug_frame are not correct today for an
905 // OUTLINED_FUNCTION, even when a normal ABI call is made.
906 // CFI may be absent or incorrect; instruction emulation may be incorrect
907 // because it assumes a normal ABI call was made.
908 if (m_sym_ctx_valid && arch_default_unwind_plan_sp) {
910 UNWIND_LOG(log,
911 "Overriding full unwind plan, using architectural default for "
912 "function {0}",
914 return arch_default_unwind_plan_sp;
915 }
916 }
917
918 FuncUnwindersSP func_unwinders_sp;
919 if (m_sym_ctx_valid) {
920 func_unwinders_sp =
921 pc_module_sp->GetUnwindTable().GetFuncUnwindersContainingAddress(
923 }
924
925 // No FuncUnwinders available for this pc (stripped function symbols, lldb
926 // could not augment its function table with another source, like
927 // LC_FUNCTION_STARTS or eh_frame in ObjectFileMachO). See if eh_frame, the
928 // .ARM.exidx tables or the object file's own unwind info (e.g. the PE EH info
929 // in .pdata) have unwind information for this address, else fall back to the
930 // architectural default unwind.
931 if (!func_unwinders_sp) {
933
934 if (!pc_module_sp || !pc_module_sp->GetObjectFile() ||
935 !m_current_pc.IsValid())
936 return arch_default_unwind_plan_sp;
937
938 // Even with -fomit-frame-pointer, we can try eh_frame to get back on
939 // track.
940 if (DWARFCallFrameInfo *eh_frame =
941 pc_module_sp->GetUnwindTable().GetEHFrameInfo()) {
942 if (std::unique_ptr<UnwindPlan> plan_up =
943 eh_frame->GetUnwindPlan(m_current_pc, m_start_pc))
944 return plan_up;
945 }
946
947 ArmUnwindInfo *arm_exidx =
948 pc_module_sp->GetUnwindTable().GetArmUnwindInfo();
949 if (arm_exidx) {
950 auto unwind_plan_sp =
951 std::make_shared<UnwindPlan>(lldb::eRegisterKindGeneric);
952 if (arm_exidx->GetUnwindPlan(exe_ctx.GetTargetRef(), m_current_pc,
953 *unwind_plan_sp))
954 return unwind_plan_sp;
955 }
956
957 CallFrameInfo *object_file_unwind =
958 pc_module_sp->GetUnwindTable().GetObjectFileUnwindInfo();
959 if (object_file_unwind) {
960 if (std::unique_ptr<UnwindPlan> plan_up =
961 object_file_unwind->GetUnwindPlan(m_current_pc))
962 return plan_up;
963 }
964
965 return arch_default_unwind_plan_sp;
966 }
967
968 if (m_frame_type == eTrapHandlerFrame && process) {
969 m_fast_unwind_plan_sp.reset();
970
971 // On some platforms the unwind information for signal handlers is not
972 // present or correct. Give the platform plugins a chance to provide
973 // substitute plan. Otherwise, use eh_frame.
974 if (m_sym_ctx_valid) {
975 lldb::PlatformSP platform = process->GetTarget().GetPlatform();
976 const ArchSpec arch = process->GetTarget().GetArchitecture();
977 if (auto unwind_plan_sp = platform->GetTrapHandlerUnwindPlan(
979 return unwind_plan_sp;
980 }
981
982 auto unwind_plan_sp =
983 func_unwinders_sp->GetEHFrameUnwindPlan(process->GetTarget());
984 if (!unwind_plan_sp)
985 unwind_plan_sp =
986 func_unwinders_sp->GetObjectFileUnwindPlan(process->GetTarget());
987 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc) &&
988 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes) {
989 return unwind_plan_sp;
990 }
991 }
992
993 // Ask the DynamicLoader if the eh_frame CFI should be trusted in this frame
994 // even when it's frame zero This comes up if we have hand-written functions
995 // in a Module and hand-written eh_frame. The assembly instruction
996 // inspection may fail and the eh_frame CFI were probably written with some
997 // care to do the right thing. It'd be nice if there was a way to ask the
998 // eh_frame directly if it is asynchronous (can be trusted at every
999 // instruction point) or synchronous (the normal case - only at call sites).
1000 // But there is not.
1001 if (process && process->GetDynamicLoader() &&
1003 // We must specifically call the GetEHFrameUnwindPlan() method here --
1004 // normally we would call GetUnwindPlanAtCallSite() -- because CallSite may
1005 // return an unwind plan sourced from either eh_frame (that's what we
1006 // intend) or compact unwind (this won't work)
1007 auto unwind_plan_sp =
1008 func_unwinders_sp->GetEHFrameUnwindPlan(process->GetTarget());
1009 if (!unwind_plan_sp)
1010 unwind_plan_sp =
1011 func_unwinders_sp->GetObjectFileUnwindPlan(process->GetTarget());
1012 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
1014 "frame uses {0} for full UnwindPlan because the "
1015 "DynamicLoader suggested we prefer it",
1016 unwind_plan_sp->GetSourceName());
1017 return unwind_plan_sp;
1018 }
1019 }
1020
1021 // Typically the NonCallSite UnwindPlan is the unwind created by inspecting
1022 // the assembly language instructions
1023 if (m_behaves_like_zeroth_frame && process) {
1024 auto unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
1025 process->GetTarget(), m_thread);
1026 if (unwind_plan_sp && unwind_plan_sp->PlanValidAtAddress(m_current_pc)) {
1027 if (unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
1028 // We probably have an UnwindPlan created by inspecting assembly
1029 // instructions. The assembly profilers work really well with compiler-
1030 // generated functions but hand- written assembly can be problematic.
1031 // We set the eh_frame based unwind plan as our fallback unwind plan if
1032 // instruction emulation doesn't work out even for non call sites if it
1033 // is available and use the architecture default unwind plan if it is
1034 // not available. The eh_frame unwind plan is more reliable even on non
1035 // call sites then the architecture default plan and for hand written
1036 // assembly code it is often written in a way that it valid at all
1037 // location what helps in the most common cases when the instruction
1038 // emulation fails.
1039 std::shared_ptr<const UnwindPlan> call_site_unwind_plan =
1040 func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(),
1041 m_thread);
1042 if (call_site_unwind_plan &&
1043 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
1044 call_site_unwind_plan->GetSourceName() !=
1045 unwind_plan_sp->GetSourceName()) {
1046 m_fallback_unwind_plan_sp = call_site_unwind_plan;
1047 } else {
1048 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
1049 }
1050 }
1052 log,
1053 "frame uses {0} for full UnwindPlan because this is the non-call "
1054 "site unwind plan and this is a zeroth frame",
1055 unwind_plan_sp->GetSourceName());
1056 return unwind_plan_sp;
1057 }
1058
1059 // If we're on the first instruction of a function, and we have an
1060 // architectural default UnwindPlan for the initial instruction of a
1061 // function, use that.
1062 if (m_current_offset == 0) {
1063 unwind_plan_sp =
1064 func_unwinders_sp->GetUnwindPlanArchitectureDefaultAtFunctionEntry(
1065 m_thread);
1066 if (unwind_plan_sp) {
1068 "frame uses {0} for full UnwindPlan because we are "
1069 "at the first instruction of a function",
1070 unwind_plan_sp->GetSourceName());
1071 return unwind_plan_sp;
1072 }
1073 }
1074 }
1075
1076 std::shared_ptr<const UnwindPlan> unwind_plan_sp;
1077 // Typically this is unwind info from an eh_frame section intended for
1078 // exception handling; only valid at call sites
1079 if (process) {
1080 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtCallSite(
1081 process->GetTarget(), m_thread);
1082 }
1083 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp)) {
1085 "frame uses {0} for full UnwindPlan because this is the "
1086 "call-site unwind plan",
1087 unwind_plan_sp->GetSourceName());
1088 return unwind_plan_sp;
1089 }
1090
1091 // We'd prefer to use an UnwindPlan intended for call sites when we're at a
1092 // call site but if we've struck out on that, fall back to using the non-
1093 // call-site assembly inspection UnwindPlan if possible.
1094 if (process) {
1095 unwind_plan_sp = func_unwinders_sp->GetUnwindPlanAtNonCallSite(
1096 process->GetTarget(), m_thread);
1097 }
1098 if (unwind_plan_sp &&
1099 unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolNo) {
1100 // We probably have an UnwindPlan created by inspecting assembly
1101 // instructions. The assembly profilers work really well with compiler-
1102 // generated functions but hand- written assembly can be problematic. We
1103 // set the eh_frame based unwind plan as our fallback unwind plan if
1104 // instruction emulation doesn't work out even for non call sites if it is
1105 // available and use the architecture default unwind plan if it is not
1106 // available. The eh_frame unwind plan is more reliable even on non call
1107 // sites then the architecture default plan and for hand written assembly
1108 // code it is often written in a way that it valid at all location what
1109 // helps in the most common cases when the instruction emulation fails.
1110 std::shared_ptr<const UnwindPlan> call_site_unwind_plan =
1111 func_unwinders_sp->GetUnwindPlanAtCallSite(process->GetTarget(),
1112 m_thread);
1113 if (call_site_unwind_plan &&
1114 call_site_unwind_plan.get() != unwind_plan_sp.get() &&
1115 call_site_unwind_plan->GetSourceName() !=
1116 unwind_plan_sp->GetSourceName()) {
1117 m_fallback_unwind_plan_sp = call_site_unwind_plan;
1118 } else {
1119 m_fallback_unwind_plan_sp = arch_default_unwind_plan_sp;
1120 }
1121 }
1122
1123 if (IsUnwindPlanValidForCurrentPC(unwind_plan_sp)) {
1125 "frame uses {0} for full UnwindPlan because we failed "
1126 "to find a call-site unwind plan that would work",
1127 unwind_plan_sp->GetSourceName());
1128 return unwind_plan_sp;
1129 }
1130
1131 // If nothing else, use the architectural default UnwindPlan and hope that
1132 // does the job.
1133 if (arch_default_unwind_plan_sp)
1135 "frame uses {0} for full UnwindPlan because we are "
1136 "falling back to the arch default plan",
1137 arch_default_unwind_plan_sp->GetSourceName());
1138 else
1139 UNWIND_LOG(log,
1140 "Unable to find any UnwindPlan for full unwind of this frame.");
1141
1142 return arch_default_unwind_plan_sp;
1143}
1144
1148
1150 return m_thread.GetRegisterContext()->GetRegisterCount();
1151}
1152
1154 return m_thread.GetRegisterContext()->GetRegisterInfoAtIndex(reg);
1155}
1156
1158 return m_thread.GetRegisterContext()->GetRegisterSetCount();
1159}
1160
1162 return m_thread.GetRegisterContext()->GetRegisterSet(reg_set);
1163}
1164
1166 lldb::RegisterKind kind, uint32_t num) {
1167 return m_thread.GetRegisterContext()->ConvertRegisterKindToRegisterNumber(
1168 kind, num);
1169}
1170
1173 const RegisterInfo *reg_info, RegisterValue &value) {
1174 if (!IsValid())
1175 return false;
1176 bool success = false;
1177
1178 switch (regloc.type) {
1180 const RegisterInfo *other_reg_info =
1182
1183 if (!other_reg_info)
1184 return false;
1185
1186 success =
1187 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1188 } break;
1190 const RegisterInfo *other_reg_info =
1192
1193 if (!other_reg_info)
1194 return false;
1195
1196 if (IsFrameZero()) {
1197 success =
1198 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1199 } else {
1200 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1201 }
1202 } break;
1204 auto regnum = regloc.location.reg_plus_offset.register_number;
1205 const RegisterInfo *other_reg_info =
1207
1208 if (!other_reg_info)
1209 return false;
1210
1211 if (IsFrameZero()) {
1212 success =
1213 m_thread.GetRegisterContext()->ReadRegister(other_reg_info, value);
1214 } else {
1215 success = GetNextFrame()->ReadRegister(other_reg_info, value);
1216 }
1217 if (success) {
1218 Log *log = GetLog(LLDBLog::Unwind);
1219 UNWIND_LOG(log, "read ({0})'s location", regnum);
1220 value = value.GetAsUInt64(~0ull, &success) +
1222 UNWIND_LOG(log, "success {0}", success ? "yes" : "no");
1223 }
1224 } break;
1226 success =
1227 value.SetUInt(regloc.location.inferred_value, reg_info->byte_size);
1228 break;
1229
1231 break;
1233 llvm_unreachable("FIXME debugger inferior function call unwind");
1236 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1237 value));
1238 success = error.Success();
1239 } break;
1240 default:
1241 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1242 }
1243 return success;
1244}
1245
1248 const RegisterInfo *reg_info, const RegisterValue &value) {
1249 if (!IsValid())
1250 return false;
1251
1252 bool success = false;
1253
1254 switch (regloc.type) {
1256 const RegisterInfo *other_reg_info =
1258 success =
1259 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1260 } break;
1262 const RegisterInfo *other_reg_info =
1264 if (IsFrameZero()) {
1265 success =
1266 m_thread.GetRegisterContext()->WriteRegister(other_reg_info, value);
1267 } else {
1268 success = GetNextFrame()->WriteRegister(other_reg_info, value);
1269 }
1270 } break;
1274 break;
1276 llvm_unreachable("FIXME debugger inferior function call unwind");
1279 reg_info, regloc.location.target_memory_location, reg_info->byte_size,
1280 value));
1281 success = error.Success();
1282 } break;
1283 default:
1284 llvm_unreachable("Unknown ConcreteRegisterLocation type.");
1285 }
1286 return success;
1287}
1288
1292
1293// After the final stack frame in a stack walk we'll get one invalid
1294// (eNotAValidFrame) stack frame -- one past the end of the stack walk. But
1295// higher-level code will need to tell the difference between "the unwind plan
1296// below this frame failed" versus "we successfully completed the stack walk"
1297// so this method helps to disambiguate that.
1298
1302
1303// A skip frame is a bogus frame on the stack -- but one where we're likely to
1304// find a real frame farther
1305// up the stack if we keep looking. It's always the second frame in an unwind
1306// (i.e. the first frame after frame zero) where unwinding can be the
1307// trickiest. Ideally we'll mark up this frame in some way so the user knows
1308// we're displaying bad data and we may have skipped one frame of their real
1309// program in the process of getting back on track.
1310
1314
1316 // See if signal-frame CFI exists at m_current_pc.
1317 ModuleSP module_sp = m_current_pc.GetModule();
1318 if (m_current_pc.IsValid() && module_sp && module_sp->GetObjectFile()) {
1319 if (DWARFCallFrameInfo *eh_frame =
1320 module_sp->GetUnwindTable().GetEHFrameInfo()) {
1321 std::unique_ptr<UnwindPlan> plan_up =
1322 eh_frame->GetUnwindPlan(m_current_pc);
1323 if (plan_up && plan_up->GetUnwindPlanForSignalTrap() == eLazyBoolYes &&
1324 plan_up->PlanValidAtAddress(m_current_pc))
1325 return true;
1326 }
1327 }
1328
1329 // Check trap handler names provided by platform.
1330 PlatformSP platform_sp(process->GetTarget().GetPlatform());
1331 if (platform_sp) {
1332 const std::vector<std::string> &trap_handler_names(
1333 platform_sp->GetTrapHandlerSymbolNames());
1334 for (const std::string &name : trap_handler_names) {
1335 if ((m_sym_ctx.function &&
1336 m_sym_ctx.function->GetName().GetStringRef() == name) ||
1337 (m_sym_ctx.symbol &&
1338 m_sym_ctx.symbol->GetName().GetStringRef() == name)) {
1339 return true;
1340 }
1341 }
1342 }
1343
1344 // Check trap handler names provided by user.
1345 const std::vector<std::string> &user_specified_trap_handler_names =
1346 m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames();
1347 for (const std::string &name : user_specified_trap_handler_names) {
1348 if ((m_sym_ctx.function &&
1349 m_sym_ctx.function->GetName().GetStringRef() == name) ||
1350 (m_sym_ctx.symbol &&
1351 m_sym_ctx.symbol->GetName().GetStringRef() == name)) {
1352 return true;
1353 }
1354 }
1355
1356 // This is not a trap handler.
1357 return false;
1358}
1359
1360// Search this stack frame's UnwindPlans for the AbstractRegisterLocation
1361// for this register.
1362//
1363// \param[in] lldb_regnum
1364// The register number (in the eRegisterKindLLDB register numbering)
1365// we are searching for.
1366//
1367// \param[out] kind
1368// Set to the RegisterKind of the UnwindPlan which is the basis for
1369// the returned AbstractRegisterLocation; if the location is in terms
1370// of another register number, this Kind is needed to interpret it
1371// correctly.
1372//
1373// \return
1374// An empty optional indicaTes that there was an error in processing
1375// the request.
1376//
1377// If there is no unwind rule for a volatile (caller-preserved) register,
1378// the returned AbstractRegisterLocation will be IsUndefined,
1379// indicating that we should stop searching.
1380//
1381// If there is no unwind rule for a non-volatile (callee-preserved)
1382// register, the returned AbstractRegisterLocation will be IsSame.
1383// In frame 0, IsSame means get the value from the live register context.
1384// Else it means to continue descending down the stack to more-live frames
1385// looking for a location/value.
1386//
1387// If an AbstractRegisterLocation is found in an UnwindPlan, that will
1388// be returned, with no consideration of the current ABI rules for
1389// registers. Functions using an alternate ABI calling convention
1390// will work as long as the UnwindPlans are exhaustive about what
1391// registers are volatile/non-volatile.
1392std::optional<UnwindPlan::Row::AbstractRegisterLocation>
1394 lldb::RegisterKind &kind) {
1395 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1396 Log *log = GetLog(LLDBLog::Unwind);
1397
1398 kind = eRegisterKindLLDB;
1400
1401 // First, try to find a register location via the FastUnwindPlan
1403 const UnwindPlan::Row *active_row =
1404 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1405 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1406 UNWIND_LOG(log,
1407 "could not convert lldb regnum {0} ({1}) into {2} "
1408 "RegisterKind reg numbering scheme",
1409 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1410 return {};
1411 }
1412 kind = m_fast_unwind_plan_sp->GetRegisterKind();
1413 // The Fast UnwindPlan typically only provides fp & pc as we move up
1414 // the stack, without requiring additional parsing or memory reads.
1415 // It may mark all other registers as IsUndefined() because, indicating
1416 // that it doesn't know if they were spilled to stack or not.
1417 // If this case, for an IsUndefined register, we should continue on
1418 // to the Full UnwindPlan which may have more accurate information
1419 // about register locations of all registers.
1420 if (active_row &&
1421 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1422 unwindplan_regloc) &&
1423 !unwindplan_regloc.IsUndefined()) {
1424 UNWIND_LOG(
1425 log,
1426 "supplying caller's saved {0} ({1})'s location using FastUnwindPlan",
1427 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1428 return unwindplan_regloc;
1429 }
1430 }
1431
1432 // Second, try to find a register location via the FullUnwindPlan.
1433 bool got_new_full_unwindplan = false;
1434 if (!m_full_unwind_plan_sp) {
1436 got_new_full_unwindplan = true;
1437 }
1441
1442 const UnwindPlan::Row *active_row =
1443 m_full_unwind_plan_sp->GetRowForFunctionOffset(
1445 kind = m_full_unwind_plan_sp->GetRegisterKind();
1446
1447 if (got_new_full_unwindplan && active_row && log) {
1448 StreamString active_row_strm;
1449 ExecutionContext exe_ctx(m_thread.shared_from_this());
1450 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
1451 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
1452 UNWIND_LOG(log, "Using full unwind plan '{0}'",
1453 m_full_unwind_plan_sp->GetSourceName());
1454 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
1455 }
1456
1457 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1458 if (kind == eRegisterKindGeneric)
1459 UNWIND_LOG(log,
1460 "could not convert lldb regnum {0} ({1}) into "
1461 "eRegisterKindGeneric reg numbering scheme",
1462 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1463 else
1464 UNWIND_LOG(log,
1465 "could not convert lldb regnum {0} ({1}) into {2} "
1466 "RegisterKind reg numbering scheme",
1467 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1468 if (active_row && active_row->GetUnspecifiedRegistersAreUndefined()) {
1469 UNWIND_LOG(
1470 log,
1471 "marking register {0} ({1}) as Undefined (volatile) in this "
1472 "stack frame because this row is UnspecifiedRegistersAreUndefined.",
1473 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1474 unwindplan_regloc.SetUndefined();
1475 return unwindplan_regloc;
1476 }
1477 return {};
1478 }
1479
1480 if (regnum.IsValid() && active_row &&
1481 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1482 unwindplan_regloc)) {
1483 UNWIND_LOG(
1484 log,
1485 "supplying caller's saved {0} ({1})'s location using {2} UnwindPlan",
1486 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1487 m_full_unwind_plan_sp->GetSourceName());
1488 return unwindplan_regloc;
1489 }
1490
1491 // When asking for the caller's pc, and did not find a register
1492 // location for PC above in the UnwindPlan. Check if we have a
1493 // Return Address register on this target.
1494 //
1495 // On a Return Address Register architecture like arm/mips/riscv,
1496 // the caller's pc is in the RA register, and will be spilled to
1497 // stack before any other function is called. If no function
1498 // has been called yet, the return address may still be in the
1499 // live RA reg.
1500 //
1501 // There's a lot of variety of what we might see in an UnwindPlan.
1502 // We may have
1503 // ra=IsSame {unncessary}
1504 // ra=StackAddr {caller's return addr spilled to stack}
1505 // or no reg location for pc or ra at all, in a frameless function -
1506 // the caller's return address is in live ra reg.
1507 //
1508 // If a function has been interrupted in a non-call way --
1509 // async signal/sigtramp, or a hardware exception / interrupt / fault --
1510 // then the "pc" and "ra" are two distinct values, and must be
1511 // handled separately. The "pc" is the pc value at the point
1512 // the function was interrupted. The "ra" is the return address
1513 // register value at that point.
1514 // The UnwindPlan for the sigtramp/trap handler will normally have
1515 // register loations for both pc and lr, and so we'll have already
1516 // fetched them above.
1517 if (pc_regnum.IsValid() && pc_regnum == regnum) {
1518 uint32_t return_address_regnum = LLDB_INVALID_REGNUM;
1519
1520 // Get the return address register number from the UnwindPlan
1521 // or the register set definition.
1522 if (m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1524 return_address_regnum =
1525 m_full_unwind_plan_sp->GetReturnAddressRegister();
1526 } else {
1527 RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1529 return_address_regnum = arch_default_ra_regnum.GetAsKind(kind);
1530 }
1531
1532 // This system is using a return address register.
1533 if (return_address_regnum != LLDB_INVALID_REGNUM) {
1534 RegisterNumber return_address_reg;
1535 return_address_reg.init(m_thread,
1536 m_full_unwind_plan_sp->GetRegisterKind(),
1537 return_address_regnum);
1538 UNWIND_LOG(log,
1539 "requested caller's saved PC but this UnwindPlan uses a RA "
1540 "reg; getting {0} ({1}) instead",
1541 return_address_reg.GetName(),
1542 return_address_reg.GetAsKind(eRegisterKindLLDB));
1543
1544 // Do we have a location for the ra register?
1545 if (active_row &&
1546 active_row->GetRegisterInfo(return_address_reg.GetAsKind(kind),
1547 unwindplan_regloc)) {
1548 UNWIND_LOG(log,
1549 "supplying caller's saved {0} ({1})'s location using {2} "
1550 "UnwindPlan",
1551 return_address_reg.GetName(),
1552 return_address_reg.GetAsKind(eRegisterKindLLDB),
1553 m_full_unwind_plan_sp->GetSourceName());
1554 // If we have "ra=IsSame", rewrite to "ra=InRegister(ra)" because the
1555 // calling function thinks it is fetching "pc" and if we return an
1556 // IsSame register location, it will try to read pc.
1557 if (unwindplan_regloc.IsSame())
1558 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1559 return unwindplan_regloc;
1560 } else {
1561 // No unwind rule for the return address reg on frame 0, or an
1562 // interrupted function, means that the caller's address is still in
1563 // RA reg (0th frame) or the trap handler below this one (sigtramp
1564 // etc) has a save location for the RA reg.
1565 if (BehavesLikeZerothFrame()) {
1566 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1567 return unwindplan_regloc;
1568 }
1569 }
1570 }
1571 }
1572 if (active_row && active_row->GetUnspecifiedRegistersAreUndefined()) {
1573 UNWIND_LOG(
1574 log,
1575 "marking register {0} ({1}) as Undefined (volatile) in this "
1576 "stack frame because this row is UnspecifiedRegistersAreUndefined.",
1577 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1578 unwindplan_regloc.SetUndefined();
1579 return unwindplan_regloc;
1580 }
1581 }
1582
1583 ExecutionContext exe_ctx(m_thread.shared_from_this());
1584 Process *process = exe_ctx.GetProcessPtr();
1585
1586 // Third, try finding a register location via the ABI
1587 // FallbackRegisterLocation.
1588 //
1589 // If the UnwindPlan failed to give us an unwind location for this
1590 // register, we may be able to fall back to some ABI-defined default. For
1591 // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1592 // the ABI willset volatile registers to the undefined state.
1593 ABI *abi = process ? process->GetABI().get() : nullptr;
1594 if (abi) {
1595 const RegisterInfo *reg_info =
1597 if (reg_info &&
1598 abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1599 if (!unwindplan_regloc.IsUndefined())
1600 UNWIND_LOG(
1601 log,
1602 "supplying caller's saved {0} ({1})'s location using ABI default",
1603 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1604 // ABI defined volatile registers with no register location
1605 // will be returned as IsUndefined, stopping the search down
1606 // the stack.
1607 return unwindplan_regloc;
1608 }
1609 }
1610
1611 // We have no AbstractRegisterLocation, and the ABI says this is a
1612 // non-volatile / callee-preserved register. Continue down the stack
1613 // or to frame 0 & the live RegisterContext.
1614 std::string unwindplan_name;
1616 unwindplan_name += "via '";
1617 unwindplan_name += m_full_unwind_plan_sp->GetSourceName();
1618 unwindplan_name += "'";
1619 }
1620 UNWIND_LOG(log, "no save location for {0} ({1}) {2}", regnum.GetName(),
1621 regnum.GetAsKind(eRegisterKindLLDB), unwindplan_name);
1622
1623 unwindplan_regloc.SetSame();
1624 return unwindplan_regloc;
1625}
1626
1627// Answer the question: Where did THIS frame save the CALLER frame ("previous"
1628// frame)'s register value?
1629
1632 uint32_t lldb_regnum,
1634 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1635 Log *log = GetLog(LLDBLog::Unwind);
1636
1637 // Have we already found this register location?
1638 if (!m_registers.empty()) {
1639 auto iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1640 if (iterator != m_registers.end()) {
1641 regloc = iterator->second;
1642 UNWIND_LOG(log, "supplying caller's saved {0} ({1})'s location, cached",
1643 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1645 }
1646 }
1647
1648 RegisterKind abs_regkind;
1649 std::optional<UnwindPlan::Row::AbstractRegisterLocation> abs_regloc =
1650 GetAbstractRegisterLocation(lldb_regnum, abs_regkind);
1651
1652 if (!abs_regloc)
1654
1655 if (abs_regloc->IsUndefined()) {
1656 UNWIND_LOG(
1657 log, "did not supply reg location for {0} ({1}) because it is volatile",
1658 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1660 }
1661
1662 ExecutionContext exe_ctx(m_thread.shared_from_this());
1663 Process *process = exe_ctx.GetProcessPtr();
1664 // abs_regloc has valid contents about where to retrieve the register
1665 if (abs_regloc->IsUnspecified()) {
1668 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1669 UNWIND_LOG(log,
1670 "save location for {0} ({1}) is unspecified, continue searching",
1671 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1673 }
1674
1675 if (abs_regloc->IsSame()) {
1676 if (IsFrameZero()) {
1677 regloc.type =
1680 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1681 UNWIND_LOG(log,
1682 "supplying caller's register {0} ({1}) from the live "
1683 "RegisterContext at frame 0",
1684 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1686 }
1687 // PC/RA reg don't follow the usual "callee-saved aka non-volatile" versus
1688 // "caller saved aka volatile" system. A stack frame can provide its caller
1689 // return address, but if we don't find a rule for pc/RA mid-stack, we
1690 // never want to iterate further down the stack looking for it.
1691 // Defensively prevent iterating down the stack for these two.
1692 if (!BehavesLikeZerothFrame() &&
1695 UNWIND_LOG(log,
1696 "register {0} ({1}) is marked as 'IsSame' - it is a pc or "
1697 "return address reg on a frame which does not have all "
1698 "registers available -- treat as if we have no information",
1699 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1701 }
1702
1705 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1706 UNWIND_LOG(log,
1707 "supplying caller's register {0} ({1}) value is unmodified in "
1708 "this frame",
1709 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1711 }
1712
1713 if (abs_regloc->IsCFAPlusOffset()) {
1714 int offset = abs_regloc->GetOffset();
1716 regloc.location.inferred_value = m_cfa + offset;
1717 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1718 UNWIND_LOG(log,
1719 "supplying caller's register {0} ({1}), value is CFA plus "
1720 "offset {2} [value is {3:x}]",
1721 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1722 regloc.location.inferred_value);
1724 }
1725
1726 if (abs_regloc->IsAtCFAPlusOffset()) {
1727 int offset = abs_regloc->GetOffset();
1728 regloc.type =
1730 regloc.location.target_memory_location = m_cfa + offset;
1731 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1732 UNWIND_LOG(log,
1733 "supplying caller's register {0} ({1}) from the stack, saved at "
1734 "CFA plus offset {2} [saved at {3:x}]",
1735 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1738 }
1739
1740 if (abs_regloc->IsAFAPlusOffset()) {
1743
1744 int offset = abs_regloc->GetOffset();
1746 regloc.location.inferred_value = m_afa + offset;
1747 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1748 UNWIND_LOG(log,
1749 "supplying caller's register {0} ({1}), value is AFA plus "
1750 "offset {2} [value is {3:x}]",
1751 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1752 regloc.location.inferred_value);
1754 }
1755
1756 if (abs_regloc->IsAtAFAPlusOffset()) {
1759
1760 int offset = abs_regloc->GetOffset();
1761 regloc.type =
1763 regloc.location.target_memory_location = m_afa + offset;
1764 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1765 UNWIND_LOG(log,
1766 "supplying caller's register {0} ({1}) from the stack, saved at "
1767 "AFA plus offset {2} [saved at {3:x}]",
1768 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1771 }
1772
1773 if (abs_regloc->IsInOtherRegister()) {
1774 RegisterNumber row_regnum(m_thread, abs_regkind,
1775 abs_regloc->GetRegisterNumber());
1776 if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1777 UNWIND_LOG(log,
1778 "could not supply caller's {0} ({1}) location - was saved in "
1779 "another reg but couldn't convert that regnum",
1780 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1782 }
1785 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1786 UNWIND_LOG(
1787 log,
1788 "supplying caller's register {0} ({1}), saved in register {2} ({3})",
1789 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1790 row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1792 }
1793
1794 if (abs_regloc->IsDWARFExpression() || abs_regloc->IsAtDWARFExpression()) {
1795 DataExtractor dwarfdata(abs_regloc->GetDWARFExpressionBytes(),
1796 abs_regloc->GetDWARFExpressionLength(),
1797 process->GetByteOrder(),
1798 process->GetAddressByteSize());
1799 ModuleSP opcode_ctx;
1800 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
1801 dwarfexpr.GetMutableExpressionAtAddress()->SetRegisterKind(abs_regkind);
1802 Value cfa_val = Scalar(m_cfa);
1804 llvm::Expected<Value> result =
1805 dwarfexpr.Evaluate(&exe_ctx, this, 0, &cfa_val, nullptr);
1806 if (!result) {
1807 LLDB_LOG_ERROR(log, result.takeError(),
1808 "DWARF expression failed to evaluate: {0}");
1809 } else {
1810 addr_t val;
1811 val = result->GetScalar().ULongLong();
1812 if (abs_regloc->IsDWARFExpression()) {
1813 regloc.type =
1815 regloc.location.inferred_value = val;
1816 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1817 UNWIND_LOG(log,
1818 "supplying caller's register {0} ({1}) via DWARF expression "
1819 "(IsDWARFExpression)",
1820 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1822 } else {
1823 regloc.type = UnwindLLDB::ConcreteRegisterLocation::
1824 eRegisterSavedAtMemoryLocation;
1825 regloc.location.target_memory_location = val;
1826 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1827 UNWIND_LOG(log,
1828 "supplying caller's register {0} ({1}) via DWARF expression "
1829 "(IsAtDWARFExpression)",
1830 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1832 }
1833 }
1834 UNWIND_LOG(log,
1835 "tried to use IsDWARFExpression or IsAtDWARFExpression for {0} "
1836 "({1}) but failed",
1837 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1839 }
1840
1841 if (abs_regloc->IsConstant()) {
1843 regloc.location.inferred_value = abs_regloc->GetConstant();
1844 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1845 UNWIND_LOG(log, "supplying caller's register {0} ({1}) via constant value",
1846 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1848 }
1849
1850 UNWIND_LOG(log, "no save location for {0} ({1}) in this stack frame",
1851 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1852
1853 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1854 // unsupported.
1855
1857}
1858
1861 return {};
1862 ProcessSP process_sp = m_thread.GetProcess();
1863 if (!process_sp)
1864 return {};
1865
1866 UnwindPlanSP arch_override_plan_sp;
1867 if (Architecture *arch = process_sp->GetTarget().GetArchitecturePlugin())
1868 arch_override_plan_sp =
1869 arch->GetArchitectureUnwindPlan(m_thread, this, m_full_unwind_plan_sp);
1870
1871 if (arch_override_plan_sp) {
1872 m_full_unwind_plan_sp = arch_override_plan_sp;
1874 m_registers.clear();
1875 if (Log *log = GetLog(LLDBLog::Unwind)) {
1876 UNWIND_LOG(
1877 log, "Replacing Full Unwindplan with Architecture UnwindPlan, '{0}'",
1878 m_full_unwind_plan_sp->GetSourceName());
1879 const UnwindPlan::Row *active_row =
1880 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1881 if (active_row) {
1882 StreamString active_row_strm;
1883 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
1884 &m_thread,
1885 m_start_pc.GetLoadAddress(&process_sp->GetTarget()));
1886 UNWIND_LOG(log, "{0}", active_row_strm.GetString());
1887 }
1888 }
1889 }
1890
1891 return {};
1892}
1893
1894// TryFallbackUnwindPlan() -- this method is a little tricky.
1895//
1896// When this is called, the frame above -- the caller frame, the "previous"
1897// frame -- is invalid or bad.
1898//
1899// Instead of stopping the stack walk here, we'll try a different UnwindPlan
1900// and see if we can get a valid frame above us.
1901//
1902// This most often happens when an unwind plan based on assembly instruction
1903// inspection is not correct -- mostly with hand-written assembly functions or
1904// functions where the stack frame is set up "out of band", e.g. the kernel
1905// saved the register context and then called an asynchronous trap handler like
1906// _sigtramp.
1907//
1908// Often in these cases, if we just do a dumb stack walk we'll get past this
1909// tricky frame and our usual techniques can continue to be used.
1910
1912 if (m_fallback_unwind_plan_sp == nullptr)
1913 return false;
1914
1915 if (m_full_unwind_plan_sp == nullptr)
1916 return false;
1917
1919 m_full_unwind_plan_sp->GetSourceName() ==
1920 m_fallback_unwind_plan_sp->GetSourceName()) {
1921 return false;
1922 }
1923
1924 // If a compiler generated unwind plan failed, trying the arch default
1925 // unwindplan isn't going to do any better.
1926 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1927 return false;
1928
1929 // Get the caller's pc value and our own CFA value. Swap in the fallback
1930 // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1931 // same, then the fallback unwind plan provides no benefit.
1932
1935
1936 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1937 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1940 regloc) ==
1942 const RegisterInfo *reg_info =
1944 if (reg_info) {
1945 RegisterValue reg_value;
1946 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1947 old_caller_pc_value = reg_value.GetAsUInt64();
1948 if (ProcessSP process_sp = m_thread.GetProcess()) {
1949 if (ABISP abi_sp = process_sp->GetABI())
1950 old_caller_pc_value = abi_sp->FixCodeAddress(old_caller_pc_value);
1951 }
1952 }
1953 }
1954 }
1955
1956 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1957 // impossible register location for the full unwind plan, it may call
1958 // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1959 // unwindplan with the fallback... in short, we're done, we're using the
1960 // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1961 // at the top -- the only way it became nullptr since then is via
1962 // SavedLocationForRegister().
1963 if (m_fallback_unwind_plan_sp == nullptr)
1964 return true;
1965
1966 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1967 // this isn't working, we need to restore. We'll also need to save & restore
1968 // the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1969 std::shared_ptr<const UnwindPlan> original_full_unwind_plan_sp =
1971 addr_t old_cfa = m_cfa;
1972 addr_t old_afa = m_afa;
1973
1974 m_registers.clear();
1975
1977
1978 const UnwindPlan::Row *active_row =
1979 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(
1981
1982 Log *log = GetLog(LLDBLog::Unwind);
1983 if (active_row &&
1984 active_row->GetCFAValue().GetValueType() !=
1986 addr_t new_cfa;
1987 ProcessSP process_sp = m_thread.GetProcess();
1988 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
1989 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1990 active_row->GetCFAValue(), new_cfa) ||
1991 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
1992 UNWIND_LOG(log, "failed to get cfa with fallback unwindplan");
1994 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1995 return false;
1996 }
1997 m_cfa = new_cfa;
1998
2000 active_row->GetAFAValue(), m_afa);
2001
2003 regloc) ==
2005 const RegisterInfo *reg_info =
2007 if (reg_info) {
2008 RegisterValue reg_value;
2009 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
2010 reg_value)) {
2011 new_caller_pc_value = reg_value.GetAsUInt64();
2012 if (process_sp)
2013 new_caller_pc_value =
2014 process_sp->FixCodeAddress(new_caller_pc_value);
2015 }
2016 }
2017 }
2018
2019 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
2020 UNWIND_LOG(log, "failed to get a pc value for the caller frame with the "
2021 "fallback unwind plan");
2023 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
2024 m_cfa = old_cfa;
2025 m_afa = old_afa;
2026 return false;
2027 }
2028
2029 if (old_caller_pc_value == new_caller_pc_value &&
2030 m_cfa == old_cfa &&
2031 m_afa == old_afa) {
2032 UNWIND_LOG(log, "fallback unwind plan got the same values for this frame "
2033 "CFA and caller frame pc, not using");
2035 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
2036 return false;
2037 }
2038
2039 UNWIND_LOG(log,
2040 "trying to unwind from this function with the UnwindPlan '{0}' "
2041 "because UnwindPlan '{1}' failed.",
2042 m_fallback_unwind_plan_sp->GetSourceName(),
2043 original_full_unwind_plan_sp->GetSourceName());
2044
2045 // We've copied the fallback unwind plan into the full - now clear the
2046 // fallback.
2049 }
2050
2051 return true;
2052}
2053
2055 if (m_fallback_unwind_plan_sp == nullptr)
2056 return false;
2057
2058 if (m_full_unwind_plan_sp == nullptr)
2059 return false;
2060
2062 m_full_unwind_plan_sp->GetSourceName() ==
2063 m_fallback_unwind_plan_sp->GetSourceName()) {
2064 return false;
2065 }
2066
2067 const UnwindPlan::Row *active_row =
2068 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
2069
2070 if (active_row &&
2071 active_row->GetCFAValue().GetValueType() !=
2073 addr_t new_cfa;
2074 ProcessSP process_sp = m_thread.GetProcess();
2075 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
2076 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
2077 active_row->GetCFAValue(), new_cfa) ||
2078 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
2080 "failed to get cfa with fallback unwindplan");
2082 return false;
2083 }
2084
2086 active_row->GetAFAValue(), m_afa);
2087
2090
2091 m_registers.clear();
2092
2093 m_cfa = new_cfa;
2094
2096
2098 "switched unconditionally to the fallback unwindplan {0}",
2099 m_full_unwind_plan_sp->GetSourceName());
2100 return true;
2101 }
2102 return false;
2103}
2104
2106 std::shared_ptr<const UnwindPlan> unwind_plan) {
2107 if (unwind_plan->GetUnwindPlanForSignalTrap() != eLazyBoolYes) {
2108 // Unwind plan does not indicate trap handler. Do nothing. We may
2109 // already be flagged as trap handler flag due to the symbol being
2110 // in the trap handler symbol list, and that should take precedence.
2111 return;
2112 } else if (m_frame_type != eNormalFrame) {
2113 // If this is already a trap handler frame, nothing to do.
2114 // If this is a skip or debug or invalid frame, don't override that.
2115 return;
2116 }
2117
2119
2120 Log *log = GetLog(LLDBLog::Unwind);
2121 UNWIND_LOG(log, "This frame is marked as a trap handler via its UnwindPlan");
2122
2124 // We backed up the pc by 1 to compute the symbol context, but
2125 // now need to undo that because the pc of the trap handler
2126 // frame may in fact be the first instruction of a signal return
2127 // trampoline, rather than the instruction after a call. This
2128 // happens on systems where the signal handler dispatch code, rather
2129 // than calling the handler and being returned to, jumps to the
2130 // handler after pushing the address of a return trampoline on the
2131 // stack -- on these systems, when the handler returns, control will
2132 // be transferred to the return trampoline, so that's the best
2133 // symbol we can present in the callstack.
2134 UNWIND_LOG(log,
2135 "Resetting current offset and re-doing symbol lookup; old "
2136 "symbol was {0}",
2139
2140 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
2141
2142 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
2143
2144 ExecutionContext exe_ctx(m_thread.shared_from_this());
2145 Process *process = exe_ctx.GetProcessPtr();
2146 Target *target = &process->GetTarget();
2147
2148 if (m_sym_ctx_valid) {
2149 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
2150 m_current_offset = m_current_pc.GetLoadAddress(target) -
2151 m_start_pc.GetLoadAddress(target);
2152 }
2153 }
2154}
2155
2157 lldb::RegisterKind row_register_kind, const UnwindPlan::Row::FAValue &fa,
2158 addr_t &address) {
2159 RegisterValue reg_value;
2160
2161 address = LLDB_INVALID_ADDRESS;
2162 addr_t cfa_reg_contents;
2163 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2164
2165 Log *log = GetLog(LLDBLog::Unwind);
2166 switch (fa.GetValueType()) {
2168 UNWIND_LOG(log, "CFA value via dereferencing reg");
2169 RegisterNumber regnum_to_deref(m_thread, row_register_kind,
2170 fa.GetRegisterNumber());
2171 addr_t reg_to_deref_contents;
2172 if (ReadGPRValue(regnum_to_deref, reg_to_deref_contents)) {
2173 const RegisterInfo *reg_info =
2175 RegisterValue reg_value;
2176 if (reg_info) {
2178 reg_info, reg_to_deref_contents, reg_info->byte_size, reg_value);
2179 if (error.Success()) {
2180 address = reg_value.GetAsUInt64();
2181 UNWIND_LOG(log,
2182 "CFA value via dereferencing reg {0} ({1}): reg has val "
2183 "{2:x}, CFA value is {3:x}",
2184 regnum_to_deref.GetName(),
2185 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2186 reg_to_deref_contents, address);
2187 return true;
2188 } else {
2189 UNWIND_LOG(
2190 log,
2191 "Tried to deref reg {0} ({1}) [{2:x}] but memory read failed.",
2192 regnum_to_deref.GetName(),
2193 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2194 reg_to_deref_contents);
2195 }
2196 }
2197 }
2198 break;
2199 }
2201 UNWIND_LOG(log, "CFA value via register plus offset");
2202 RegisterNumber cfa_reg(m_thread, row_register_kind,
2203 fa.GetRegisterNumber());
2204 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
2205 if (!CallFrameAddressIsValid(abi_sp, cfa_reg_contents)) {
2206 UNWIND_LOG(
2207 log,
2208 "Got an invalid CFA register value - reg {0} ({1}), value {2:x}",
2209 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2210 cfa_reg_contents);
2211 return false;
2212 }
2213 address = cfa_reg_contents + fa.GetOffset();
2214 UNWIND_LOG(
2215 log,
2216 "CFA is {0:x}: Register {1} ({2}) contents are {3:x}, offset is {4}",
2217 address, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2218 cfa_reg_contents, fa.GetOffset());
2219 return true;
2220 }
2221 UNWIND_LOG(log, "unable to read CFA register {0} ({1})", cfa_reg.GetName(),
2222 cfa_reg.GetAsKind(eRegisterKindLLDB));
2223 break;
2224 }
2226 UNWIND_LOG(log, "CFA value via DWARF expression");
2227 ExecutionContext exe_ctx(m_thread.shared_from_this());
2228 Process *process = exe_ctx.GetProcessPtr();
2231 process->GetByteOrder(),
2232 process->GetAddressByteSize());
2233 ModuleSP opcode_ctx;
2234 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
2236 row_register_kind);
2237 llvm::Expected<Value> result =
2238 dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr);
2239 if (result) {
2240 address = result->GetScalar().ULongLong();
2241 UNWIND_LOG(log, "CFA value set by DWARF expression is {0:x}", address);
2242 return true;
2243 }
2244 LLDB_LOG_ERROR(log, result.takeError(),
2245 "Failed to set CFA value via DWARF expression: {0}");
2246 break;
2247 }
2249 UNWIND_LOG(log, "CFA value via heuristic search");
2250 Process &process = *m_thread.GetProcess();
2251 lldb::addr_t return_address_hint = GetReturnAddressHint(fa.GetOffset());
2252 if (return_address_hint == LLDB_INVALID_ADDRESS)
2253 return false;
2254 const unsigned max_iterations = 256;
2255 for (unsigned i = 0; i < max_iterations; ++i) {
2256 lldb::addr_t candidate_addr =
2257 return_address_hint + i * process.GetAddressByteSize();
2258 llvm::Expected<lldb::addr_t> candidate =
2259 process.ReadPointerFromMemory(candidate_addr);
2260 if (!candidate) {
2261 LLDB_LOG_ERROR(log, candidate.takeError(),
2262 "Cannot read memory at {1:x}: {0}", candidate_addr);
2263 return false;
2264 }
2265 Address addr;
2266 uint32_t permissions;
2267 if (process.GetLoadAddressPermissions(*candidate, permissions) &&
2268 permissions & lldb::ePermissionsExecutable) {
2269 address = candidate_addr;
2270 UNWIND_LOG(log, "Heuristically found CFA: {0:x}", address);
2271 return true;
2272 }
2273 }
2274 UNWIND_LOG(log, "No suitable CFA found");
2275 break;
2276 }
2278 address = fa.GetConstant();
2279 UNWIND_LOG(log, "CFA value set by constant is {0:x}", address);
2280 return true;
2281 }
2282 default:
2283 return false;
2284 }
2285 return false;
2286}
2287
2289 addr_t hint;
2291 return LLDB_INVALID_ADDRESS;
2292 if (!m_sym_ctx.module_sp || !m_sym_ctx.symbol)
2293 return LLDB_INVALID_ADDRESS;
2294 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2295 hint = abi_sp->FixCodeAddress(hint);
2296
2297 hint += plan_offset;
2298
2299 if (auto next = GetNextFrame()) {
2300 if (!next->m_sym_ctx.module_sp || !next->m_sym_ctx.symbol)
2301 return LLDB_INVALID_ADDRESS;
2302 if (auto expected_size =
2303 next->m_sym_ctx.module_sp->GetSymbolFile()->GetParameterStackSize(
2304 *next->m_sym_ctx.symbol))
2305 hint += *expected_size;
2306 else {
2307 LLDB_LOG_ERRORV(GetLog(LLDBLog::Unwind), expected_size.takeError(),
2308 "Could not retrieve parameter size: {0}");
2309 return LLDB_INVALID_ADDRESS;
2310 }
2311 }
2312 return hint;
2313}
2314
2315// Retrieve a general purpose register value for THIS frame, as saved by the
2316// NEXT frame, i.e. the frame that
2317// this frame called. e.g.
2318//
2319// foo () { }
2320// bar () { foo (); }
2321// main () { bar (); }
2322//
2323// stopped in foo() so
2324// frame 0 - foo
2325// frame 1 - bar
2326// frame 2 - main
2327// and this RegisterContext is for frame 1 (bar) - if we want to get the pc
2328// value for frame 1, we need to ask
2329// where frame 0 (the "next" frame) saved that and retrieve the value.
2330
2332 uint32_t regnum, addr_t &value) {
2333 if (!IsValid())
2334 return false;
2335
2336 uint32_t lldb_regnum;
2337 if (register_kind == eRegisterKindLLDB) {
2338 lldb_regnum = regnum;
2339 } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2340 register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
2341 return false;
2342 }
2343
2344 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
2345 assert(reg_info);
2346 if (!reg_info) {
2347 UNWIND_LOG(
2349 "Could not find RegisterInfo definition for lldb register number {0}",
2350 lldb_regnum);
2351 return false;
2352 }
2353
2354 uint32_t generic_regnum = LLDB_INVALID_REGNUM;
2355 if (register_kind == eRegisterKindGeneric)
2356 generic_regnum = regnum;
2357 else
2358 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2359 register_kind, regnum, eRegisterKindGeneric, generic_regnum);
2360 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2361
2362 RegisterValue reg_value;
2363 // if this is frame 0 (currently executing frame), get the requested reg
2364 // contents from the actual thread registers
2365 if (IsFrameZero()) {
2366 if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
2367 value = reg_value.GetAsUInt64();
2368 if (abi_sp && generic_regnum != LLDB_INVALID_REGNUM) {
2369 if (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2370 generic_regnum == LLDB_REGNUM_GENERIC_RA)
2371 value = abi_sp->FixCodeAddress(value);
2372 }
2373 return true;
2374 }
2375 return false;
2376 }
2377
2378 bool pc_register = false;
2379 if (generic_regnum != LLDB_INVALID_REGNUM &&
2380 (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2381 generic_regnum == LLDB_REGNUM_GENERIC_RA))
2382 pc_register = true;
2383
2385 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2386 lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
2387 return false;
2388 }
2389 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
2390 value = reg_value.GetAsUInt64();
2391 if (pc_register) {
2392 if (ABISP abi_sp = m_thread.GetProcess()->GetABI()) {
2393 value = abi_sp->FixCodeAddress(value);
2394 }
2395 }
2396 return true;
2397 }
2398 return false;
2399}
2400
2402 addr_t &value) {
2403 return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
2404 value);
2405}
2406
2407// Find the value of a register in THIS frame
2408
2410 RegisterValue &value) {
2411 if (!IsValid())
2412 return false;
2413
2414 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2415 Log *log = GetLog(LLDBLog::Unwind);
2416 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2417 lldb_regnum);
2418
2419 // If this is the 0th frame, hand this over to the live register context
2420 if (IsFrameZero()) {
2422 "passing along to the live register context for reg {0}",
2423 lldb_regnum);
2424 return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
2425 }
2426
2427 bool is_pc_regnum = false;
2430 is_pc_regnum = true;
2431 }
2432
2434 // Find out where the NEXT frame saved THIS frame's register contents
2435 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2436 lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
2437 return false;
2438
2439 bool result = ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
2440 if (result) {
2441 if (is_pc_regnum && value.GetType() == RegisterValue::eTypeUInt64) {
2442 addr_t reg_value = value.GetAsUInt64(LLDB_INVALID_ADDRESS);
2443 if (reg_value != LLDB_INVALID_ADDRESS) {
2444 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2445 value = abi_sp->FixCodeAddress(reg_value);
2446 }
2447 }
2448 }
2449 return result;
2450}
2451
2453 const RegisterValue &value) {
2454 if (!IsValid())
2455 return false;
2456
2457 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2458 Log *log = GetLog(LLDBLog::Unwind);
2459 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2460 lldb_regnum);
2461
2462 // If this is the 0th frame, hand this over to the live register context
2463 if (IsFrameZero()) {
2465 "passing along to the live register context for reg {0}",
2466 lldb_regnum);
2467 return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
2468 }
2469
2471 // Find out where the NEXT frame saved THIS frame's register contents
2472 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2473 lldb_regnum, regloc, m_frame_number - 1, false))
2474 return false;
2475
2476 return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
2477}
2478
2479// Don't need to implement this one
2481 lldb::WritableDataBufferSP &data_sp) {
2482 return false;
2483}
2484
2485// Don't need to implement this one
2487 const lldb::DataBufferSP &data_sp) {
2488 return false;
2489}
2490
2491// Retrieve the pc value for THIS from
2492
2494 if (!IsValid()) {
2495 return false;
2496 }
2497 if (m_cfa == LLDB_INVALID_ADDRESS) {
2498 return false;
2499 }
2500 cfa = m_cfa;
2501 return true;
2502}
2503
2506 if (m_frame_number == 0)
2507 return regctx;
2508 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
2509}
2510
2515
2516// Retrieve the address of the start of the function of THIS frame
2517
2519 if (!IsValid())
2520 return false;
2521
2522 if (!m_start_pc.IsValid()) {
2523 bool read_successfully = ReadPC (start_pc);
2524 if (read_successfully)
2525 {
2526 ProcessSP process_sp (m_thread.GetProcess());
2527 if (process_sp)
2528 {
2529 if (ABISP abi_sp = process_sp->GetABI())
2530 start_pc = abi_sp->FixCodeAddress(start_pc);
2531 }
2532 }
2533 return read_successfully;
2534 }
2535 start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2536 return true;
2537}
2538
2539// Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2540
2542 if (!IsValid())
2543 return false;
2544
2545 bool above_trap_handler = false;
2546 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2548 above_trap_handler = true;
2549
2551 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2552 // indicates the end of a stack walk.
2553 // On the currently executing frame (or such a frame interrupted
2554 // asynchronously by sigtramp et al) this may occur if code has jumped
2555 // through a NULL pointer -- we want to be able to unwind past that frame
2556 // to help find the bug.
2557
2558 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2559 pc = abi_sp->FixCodeAddress(pc);
2560
2561 return !(m_all_registers_available == false &&
2562 above_trap_handler == false && (pc == 0 || pc == 1));
2563 } else {
2564 return false;
2565 }
2566}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:421
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
static ConstString GetSymbolOrFunctionName(const SymbolContext &sym_ctx)
#define UNWIND_LOG_VERBOSE(log,...)
static bool IsClangOutlinedFunction(const SymbolContext &sym_ctx)
Identify a clang outlined function by symbol name.
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:1029
bool Slide(int64_t offset)
Definition Address.h:446
bool ResolveFunctionScope(lldb_private::SymbolContext &sym_ctx)
Resolve this address to its containing function.
Definition Address.cpp:268
bool IsValid() const
Check if the object state is valid.
Definition Address.h:355
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
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
"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:724
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:369
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:2845
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3969
llvm::Expected< lldb::addr_t > ReadPointerFromMemory(lldb::addr_t vm_addr)
Definition Process.cpp:2551
uint32_t GetAddressByteSize() const
Definition Process.cpp:3973
virtual DynamicLoader * GetDynamicLoader()
Get the dynamic loader plug-in for this process.
Definition Process.cpp:3134
const lldb::ABISP & GetABI()
Definition Process.cpp:1529
Target & GetTarget()
Get the target object pointer for this module.
Definition Process.h:1270
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 IsTrapHandler(lldb_private::Process *process) const
Identify a trap handler from signal-frame CFI at m_current_pc or its symbol name provided by platform...
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)
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
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:615
lldb::PlatformSP GetPlatform()
Definition Target.h:2008
const ArchSpec & GetArchitecture() const
Definition Target.h:1325
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:50
void SetValueType(ValueType value_type)
Definition Value.h:90
#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:338
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:45
union lldb_private::UnwindLLDB::ConcreteRegisterLocation::@112231307016025255352221255122100277032134020214 location
struct lldb_private::UnwindLLDB::ConcreteRegisterLocation::@112231307016025255352221255122100277032134020214::@342316333304072166237122265045271116073153235374 reg_plus_offset