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 (IsTrapHandlerSymbol(process, m_sym_ctx)) {
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 (IsTrapHandlerSymbol(process, m_sym_ctx)) {
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 __kernel_rt_sigreturn),
576 // so do not decrement and recompute if the symbol we already found is a trap
577 // 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 (IsTrapHandlerSymbol(process, m_sym_ctx)) {
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 lldb_private::Process *process,
1318 PlatformSP platform_sp(process->GetTarget().GetPlatform());
1319 if (platform_sp) {
1320 const std::vector<std::string> &trap_handler_names(
1321 platform_sp->GetTrapHandlerSymbolNames());
1322 for (const std::string &name : trap_handler_names) {
1323 if ((m_sym_ctx.function &&
1324 m_sym_ctx.function->GetName().GetStringRef() == name) ||
1325 (m_sym_ctx.symbol &&
1326 m_sym_ctx.symbol->GetName().GetStringRef() == name)) {
1327 return true;
1328 }
1329 }
1330 }
1331 const std::vector<std::string> &user_specified_trap_handler_names =
1332 m_parent_unwind.GetUserSpecifiedTrapHandlerFunctionNames();
1333 for (const std::string &name : user_specified_trap_handler_names) {
1334 if ((m_sym_ctx.function &&
1335 m_sym_ctx.function->GetName().GetStringRef() == name) ||
1336 (m_sym_ctx.symbol &&
1337 m_sym_ctx.symbol->GetName().GetStringRef() == name)) {
1338 return true;
1339 }
1340 }
1341
1342 return false;
1343}
1344
1345// Search this stack frame's UnwindPlans for the AbstractRegisterLocation
1346// for this register.
1347//
1348// \param[in] lldb_regnum
1349// The register number (in the eRegisterKindLLDB register numbering)
1350// we are searching for.
1351//
1352// \param[out] kind
1353// Set to the RegisterKind of the UnwindPlan which is the basis for
1354// the returned AbstractRegisterLocation; if the location is in terms
1355// of another register number, this Kind is needed to interpret it
1356// correctly.
1357//
1358// \return
1359// An empty optional indicaTes that there was an error in processing
1360// the request.
1361//
1362// If there is no unwind rule for a volatile (caller-preserved) register,
1363// the returned AbstractRegisterLocation will be IsUndefined,
1364// indicating that we should stop searching.
1365//
1366// If there is no unwind rule for a non-volatile (callee-preserved)
1367// register, the returned AbstractRegisterLocation will be IsSame.
1368// In frame 0, IsSame means get the value from the live register context.
1369// Else it means to continue descending down the stack to more-live frames
1370// looking for a location/value.
1371//
1372// If an AbstractRegisterLocation is found in an UnwindPlan, that will
1373// be returned, with no consideration of the current ABI rules for
1374// registers. Functions using an alternate ABI calling convention
1375// will work as long as the UnwindPlans are exhaustive about what
1376// registers are volatile/non-volatile.
1377std::optional<UnwindPlan::Row::AbstractRegisterLocation>
1379 lldb::RegisterKind &kind) {
1380 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1381 Log *log = GetLog(LLDBLog::Unwind);
1382
1383 kind = eRegisterKindLLDB;
1385
1386 // First, try to find a register location via the FastUnwindPlan
1388 const UnwindPlan::Row *active_row =
1389 m_fast_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1390 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1391 UNWIND_LOG(log,
1392 "could not convert lldb regnum {0} ({1}) into {2} "
1393 "RegisterKind reg numbering scheme",
1394 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1395 return {};
1396 }
1397 kind = m_fast_unwind_plan_sp->GetRegisterKind();
1398 // The Fast UnwindPlan typically only provides fp & pc as we move up
1399 // the stack, without requiring additional parsing or memory reads.
1400 // It may mark all other registers as IsUndefined() because, indicating
1401 // that it doesn't know if they were spilled to stack or not.
1402 // If this case, for an IsUndefined register, we should continue on
1403 // to the Full UnwindPlan which may have more accurate information
1404 // about register locations of all registers.
1405 if (active_row &&
1406 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1407 unwindplan_regloc) &&
1408 !unwindplan_regloc.IsUndefined()) {
1409 UNWIND_LOG(
1410 log,
1411 "supplying caller's saved {0} ({1})'s location using FastUnwindPlan",
1412 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1413 return unwindplan_regloc;
1414 }
1415 }
1416
1417 // Second, try to find a register location via the FullUnwindPlan.
1418 bool got_new_full_unwindplan = false;
1419 if (!m_full_unwind_plan_sp) {
1421 got_new_full_unwindplan = true;
1422 }
1426
1427 const UnwindPlan::Row *active_row =
1428 m_full_unwind_plan_sp->GetRowForFunctionOffset(
1430 kind = m_full_unwind_plan_sp->GetRegisterKind();
1431
1432 if (got_new_full_unwindplan && active_row && log) {
1433 StreamString active_row_strm;
1434 ExecutionContext exe_ctx(m_thread.shared_from_this());
1435 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(), &m_thread,
1436 m_start_pc.GetLoadAddress(exe_ctx.GetTargetPtr()));
1437 UNWIND_LOG(log, "Using full unwind plan '{0}'",
1438 m_full_unwind_plan_sp->GetSourceName());
1439 UNWIND_LOG(log, "active row: {0}", active_row_strm.GetString());
1440 }
1441
1442 if (regnum.GetAsKind(kind) == LLDB_INVALID_REGNUM) {
1443 if (kind == eRegisterKindGeneric)
1444 UNWIND_LOG(log,
1445 "could not convert lldb regnum {0} ({1}) into "
1446 "eRegisterKindGeneric reg numbering scheme",
1447 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1448 else
1449 UNWIND_LOG(log,
1450 "could not convert lldb regnum {0} ({1}) into {2} "
1451 "RegisterKind reg numbering scheme",
1452 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), kind);
1453 if (active_row && active_row->GetUnspecifiedRegistersAreUndefined()) {
1454 UNWIND_LOG(
1455 log,
1456 "marking register {0} ({1}) as Undefined (volatile) in this "
1457 "stack frame because this row is UnspecifiedRegistersAreUndefined.",
1458 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1459 unwindplan_regloc.SetUndefined();
1460 return unwindplan_regloc;
1461 }
1462 return {};
1463 }
1464
1465 if (regnum.IsValid() && active_row &&
1466 active_row->GetRegisterInfo(regnum.GetAsKind(kind),
1467 unwindplan_regloc)) {
1468 UNWIND_LOG(
1469 log,
1470 "supplying caller's saved {0} ({1})'s location using {2} UnwindPlan",
1471 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1472 m_full_unwind_plan_sp->GetSourceName());
1473 return unwindplan_regloc;
1474 }
1475
1476 // When asking for the caller's pc, and did not find a register
1477 // location for PC above in the UnwindPlan. Check if we have a
1478 // Return Address register on this target.
1479 //
1480 // On a Return Address Register architecture like arm/mips/riscv,
1481 // the caller's pc is in the RA register, and will be spilled to
1482 // stack before any other function is called. If no function
1483 // has been called yet, the return address may still be in the
1484 // live RA reg.
1485 //
1486 // There's a lot of variety of what we might see in an UnwindPlan.
1487 // We may have
1488 // ra=IsSame {unncessary}
1489 // ra=StackAddr {caller's return addr spilled to stack}
1490 // or no reg location for pc or ra at all, in a frameless function -
1491 // the caller's return address is in live ra reg.
1492 //
1493 // If a function has been interrupted in a non-call way --
1494 // async signal/sigtramp, or a hardware exception / interrupt / fault --
1495 // then the "pc" and "ra" are two distinct values, and must be
1496 // handled separately. The "pc" is the pc value at the point
1497 // the function was interrupted. The "ra" is the return address
1498 // register value at that point.
1499 // The UnwindPlan for the sigtramp/trap handler will normally have
1500 // register loations for both pc and lr, and so we'll have already
1501 // fetched them above.
1502 if (pc_regnum.IsValid() && pc_regnum == regnum) {
1503 uint32_t return_address_regnum = LLDB_INVALID_REGNUM;
1504
1505 // Get the return address register number from the UnwindPlan
1506 // or the register set definition.
1507 if (m_full_unwind_plan_sp->GetReturnAddressRegister() !=
1509 return_address_regnum =
1510 m_full_unwind_plan_sp->GetReturnAddressRegister();
1511 } else {
1512 RegisterNumber arch_default_ra_regnum(m_thread, eRegisterKindGeneric,
1514 return_address_regnum = arch_default_ra_regnum.GetAsKind(kind);
1515 }
1516
1517 // This system is using a return address register.
1518 if (return_address_regnum != LLDB_INVALID_REGNUM) {
1519 RegisterNumber return_address_reg;
1520 return_address_reg.init(m_thread,
1521 m_full_unwind_plan_sp->GetRegisterKind(),
1522 return_address_regnum);
1523 UNWIND_LOG(log,
1524 "requested caller's saved PC but this UnwindPlan uses a RA "
1525 "reg; getting {0} ({1}) instead",
1526 return_address_reg.GetName(),
1527 return_address_reg.GetAsKind(eRegisterKindLLDB));
1528
1529 // Do we have a location for the ra register?
1530 if (active_row &&
1531 active_row->GetRegisterInfo(return_address_reg.GetAsKind(kind),
1532 unwindplan_regloc)) {
1533 UNWIND_LOG(log,
1534 "supplying caller's saved {0} ({1})'s location using {2} "
1535 "UnwindPlan",
1536 return_address_reg.GetName(),
1537 return_address_reg.GetAsKind(eRegisterKindLLDB),
1538 m_full_unwind_plan_sp->GetSourceName());
1539 // If we have "ra=IsSame", rewrite to "ra=InRegister(ra)" because the
1540 // calling function thinks it is fetching "pc" and if we return an
1541 // IsSame register location, it will try to read pc.
1542 if (unwindplan_regloc.IsSame())
1543 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1544 return unwindplan_regloc;
1545 } else {
1546 // No unwind rule for the return address reg on frame 0, or an
1547 // interrupted function, means that the caller's address is still in
1548 // RA reg (0th frame) or the trap handler below this one (sigtramp
1549 // etc) has a save location for the RA reg.
1550 if (BehavesLikeZerothFrame()) {
1551 unwindplan_regloc.SetInRegister(return_address_reg.GetAsKind(kind));
1552 return unwindplan_regloc;
1553 }
1554 }
1555 }
1556 }
1557 if (active_row && active_row->GetUnspecifiedRegistersAreUndefined()) {
1558 UNWIND_LOG(
1559 log,
1560 "marking register {0} ({1}) as Undefined (volatile) in this "
1561 "stack frame because this row is UnspecifiedRegistersAreUndefined.",
1562 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1563 unwindplan_regloc.SetUndefined();
1564 return unwindplan_regloc;
1565 }
1566 }
1567
1568 ExecutionContext exe_ctx(m_thread.shared_from_this());
1569 Process *process = exe_ctx.GetProcessPtr();
1570
1571 // Third, try finding a register location via the ABI
1572 // FallbackRegisterLocation.
1573 //
1574 // If the UnwindPlan failed to give us an unwind location for this
1575 // register, we may be able to fall back to some ABI-defined default. For
1576 // example, some ABIs allow to determine the caller's SP via the CFA. Also,
1577 // the ABI willset volatile registers to the undefined state.
1578 ABI *abi = process ? process->GetABI().get() : nullptr;
1579 if (abi) {
1580 const RegisterInfo *reg_info =
1582 if (reg_info &&
1583 abi->GetFallbackRegisterLocation(reg_info, unwindplan_regloc)) {
1584 if (!unwindplan_regloc.IsUndefined())
1585 UNWIND_LOG(
1586 log,
1587 "supplying caller's saved {0} ({1})'s location using ABI default",
1588 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1589 // ABI defined volatile registers with no register location
1590 // will be returned as IsUndefined, stopping the search down
1591 // the stack.
1592 return unwindplan_regloc;
1593 }
1594 }
1595
1596 // We have no AbstractRegisterLocation, and the ABI says this is a
1597 // non-volatile / callee-preserved register. Continue down the stack
1598 // or to frame 0 & the live RegisterContext.
1599 std::string unwindplan_name;
1601 unwindplan_name += "via '";
1602 unwindplan_name += m_full_unwind_plan_sp->GetSourceName();
1603 unwindplan_name += "'";
1604 }
1605 UNWIND_LOG(log, "no save location for {0} ({1}) {2}", regnum.GetName(),
1606 regnum.GetAsKind(eRegisterKindLLDB), unwindplan_name);
1607
1608 unwindplan_regloc.SetSame();
1609 return unwindplan_regloc;
1610}
1611
1612// Answer the question: Where did THIS frame save the CALLER frame ("previous"
1613// frame)'s register value?
1614
1617 uint32_t lldb_regnum,
1619 RegisterNumber regnum(m_thread, eRegisterKindLLDB, lldb_regnum);
1620 Log *log = GetLog(LLDBLog::Unwind);
1621
1622 // Have we already found this register location?
1623 if (!m_registers.empty()) {
1624 auto iterator = m_registers.find(regnum.GetAsKind(eRegisterKindLLDB));
1625 if (iterator != m_registers.end()) {
1626 regloc = iterator->second;
1627 UNWIND_LOG(log, "supplying caller's saved {0} ({1})'s location, cached",
1628 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1630 }
1631 }
1632
1633 RegisterKind abs_regkind;
1634 std::optional<UnwindPlan::Row::AbstractRegisterLocation> abs_regloc =
1635 GetAbstractRegisterLocation(lldb_regnum, abs_regkind);
1636
1637 if (!abs_regloc)
1639
1640 if (abs_regloc->IsUndefined()) {
1641 UNWIND_LOG(
1642 log, "did not supply reg location for {0} ({1}) because it is volatile",
1643 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1645 }
1646
1647 ExecutionContext exe_ctx(m_thread.shared_from_this());
1648 Process *process = exe_ctx.GetProcessPtr();
1649 // abs_regloc has valid contents about where to retrieve the register
1650 if (abs_regloc->IsUnspecified()) {
1653 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = new_regloc;
1654 UNWIND_LOG(log,
1655 "save location for {0} ({1}) is unspecified, continue searching",
1656 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1658 }
1659
1660 if (abs_regloc->IsSame()) {
1661 if (IsFrameZero()) {
1662 regloc.type =
1665 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1666 UNWIND_LOG(log,
1667 "supplying caller's register {0} ({1}) from the live "
1668 "RegisterContext at frame 0",
1669 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1671 }
1672 // PC/RA reg don't follow the usual "callee-saved aka non-volatile" versus
1673 // "caller saved aka volatile" system. A stack frame can provide its caller
1674 // return address, but if we don't find a rule for pc/RA mid-stack, we
1675 // never want to iterate further down the stack looking for it.
1676 // Defensively prevent iterating down the stack for these two.
1677 if (!BehavesLikeZerothFrame() &&
1680 UNWIND_LOG(log,
1681 "register {0} ({1}) is marked as 'IsSame' - it is a pc or "
1682 "return address reg on a frame which does not have all "
1683 "registers available -- treat as if we have no information",
1684 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1686 }
1687
1690 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1691 UNWIND_LOG(log,
1692 "supplying caller's register {0} ({1}) value is unmodified in "
1693 "this frame",
1694 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1696 }
1697
1698 if (abs_regloc->IsCFAPlusOffset()) {
1699 int offset = abs_regloc->GetOffset();
1701 regloc.location.inferred_value = m_cfa + offset;
1702 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1703 UNWIND_LOG(log,
1704 "supplying caller's register {0} ({1}), value is CFA plus "
1705 "offset {2} [value is {3:x}]",
1706 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1707 regloc.location.inferred_value);
1709 }
1710
1711 if (abs_regloc->IsAtCFAPlusOffset()) {
1712 int offset = abs_regloc->GetOffset();
1713 regloc.type =
1715 regloc.location.target_memory_location = m_cfa + offset;
1716 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1717 UNWIND_LOG(log,
1718 "supplying caller's register {0} ({1}) from the stack, saved at "
1719 "CFA plus offset {2} [saved at {3:x}]",
1720 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1723 }
1724
1725 if (abs_regloc->IsAFAPlusOffset()) {
1728
1729 int offset = abs_regloc->GetOffset();
1731 regloc.location.inferred_value = m_afa + offset;
1732 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1733 UNWIND_LOG(log,
1734 "supplying caller's register {0} ({1}), value is AFA plus "
1735 "offset {2} [value is {3:x}]",
1736 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1737 regloc.location.inferred_value);
1739 }
1740
1741 if (abs_regloc->IsAtAFAPlusOffset()) {
1744
1745 int offset = abs_regloc->GetOffset();
1746 regloc.type =
1748 regloc.location.target_memory_location = m_afa + offset;
1749 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1750 UNWIND_LOG(log,
1751 "supplying caller's register {0} ({1}) from the stack, saved at "
1752 "AFA plus offset {2} [saved at {3:x}]",
1753 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB), offset,
1756 }
1757
1758 if (abs_regloc->IsInOtherRegister()) {
1759 RegisterNumber row_regnum(m_thread, abs_regkind,
1760 abs_regloc->GetRegisterNumber());
1761 if (row_regnum.GetAsKind(eRegisterKindLLDB) == LLDB_INVALID_REGNUM) {
1762 UNWIND_LOG(log,
1763 "could not supply caller's {0} ({1}) location - was saved in "
1764 "another reg but couldn't convert that regnum",
1765 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1767 }
1770 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1771 UNWIND_LOG(
1772 log,
1773 "supplying caller's register {0} ({1}), saved in register {2} ({3})",
1774 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB),
1775 row_regnum.GetName(), row_regnum.GetAsKind(eRegisterKindLLDB));
1777 }
1778
1779 if (abs_regloc->IsDWARFExpression() || abs_regloc->IsAtDWARFExpression()) {
1780 DataExtractor dwarfdata(abs_regloc->GetDWARFExpressionBytes(),
1781 abs_regloc->GetDWARFExpressionLength(),
1782 process->GetByteOrder(),
1783 process->GetAddressByteSize());
1784 ModuleSP opcode_ctx;
1785 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
1786 dwarfexpr.GetMutableExpressionAtAddress()->SetRegisterKind(abs_regkind);
1787 Value cfa_val = Scalar(m_cfa);
1789 llvm::Expected<Value> result =
1790 dwarfexpr.Evaluate(&exe_ctx, this, 0, &cfa_val, nullptr);
1791 if (!result) {
1792 LLDB_LOG_ERROR(log, result.takeError(),
1793 "DWARF expression failed to evaluate: {0}");
1794 } else {
1795 addr_t val;
1796 val = result->GetScalar().ULongLong();
1797 if (abs_regloc->IsDWARFExpression()) {
1798 regloc.type =
1800 regloc.location.inferred_value = val;
1801 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1802 UNWIND_LOG(log,
1803 "supplying caller's register {0} ({1}) via DWARF expression "
1804 "(IsDWARFExpression)",
1805 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1807 } else {
1808 regloc.type = UnwindLLDB::ConcreteRegisterLocation::
1809 eRegisterSavedAtMemoryLocation;
1810 regloc.location.target_memory_location = val;
1811 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1812 UNWIND_LOG(log,
1813 "supplying caller's register {0} ({1}) via DWARF expression "
1814 "(IsAtDWARFExpression)",
1815 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1817 }
1818 }
1819 UNWIND_LOG(log,
1820 "tried to use IsDWARFExpression or IsAtDWARFExpression for {0} "
1821 "({1}) but failed",
1822 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1824 }
1825
1826 if (abs_regloc->IsConstant()) {
1828 regloc.location.inferred_value = abs_regloc->GetConstant();
1829 m_registers[regnum.GetAsKind(eRegisterKindLLDB)] = regloc;
1830 UNWIND_LOG(log, "supplying caller's register {0} ({1}) via constant value",
1831 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1833 }
1834
1835 UNWIND_LOG(log, "no save location for {0} ({1}) in this stack frame",
1836 regnum.GetName(), regnum.GetAsKind(eRegisterKindLLDB));
1837
1838 // FIXME UnwindPlan::Row types atDWARFExpression and isDWARFExpression are
1839 // unsupported.
1840
1842}
1843
1846 return {};
1847 ProcessSP process_sp = m_thread.GetProcess();
1848 if (!process_sp)
1849 return {};
1850
1851 UnwindPlanSP arch_override_plan_sp;
1852 if (Architecture *arch = process_sp->GetTarget().GetArchitecturePlugin())
1853 arch_override_plan_sp =
1854 arch->GetArchitectureUnwindPlan(m_thread, this, m_full_unwind_plan_sp);
1855
1856 if (arch_override_plan_sp) {
1857 m_full_unwind_plan_sp = arch_override_plan_sp;
1859 m_registers.clear();
1860 if (Log *log = GetLog(LLDBLog::Unwind)) {
1861 UNWIND_LOG(
1862 log, "Replacing Full Unwindplan with Architecture UnwindPlan, '{0}'",
1863 m_full_unwind_plan_sp->GetSourceName());
1864 const UnwindPlan::Row *active_row =
1865 m_full_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
1866 if (active_row) {
1867 StreamString active_row_strm;
1868 active_row->Dump(active_row_strm, m_full_unwind_plan_sp.get(),
1869 &m_thread,
1870 m_start_pc.GetLoadAddress(&process_sp->GetTarget()));
1871 UNWIND_LOG(log, "{0}", active_row_strm.GetString());
1872 }
1873 }
1874 }
1875
1876 return {};
1877}
1878
1879// TryFallbackUnwindPlan() -- this method is a little tricky.
1880//
1881// When this is called, the frame above -- the caller frame, the "previous"
1882// frame -- is invalid or bad.
1883//
1884// Instead of stopping the stack walk here, we'll try a different UnwindPlan
1885// and see if we can get a valid frame above us.
1886//
1887// This most often happens when an unwind plan based on assembly instruction
1888// inspection is not correct -- mostly with hand-written assembly functions or
1889// functions where the stack frame is set up "out of band", e.g. the kernel
1890// saved the register context and then called an asynchronous trap handler like
1891// _sigtramp.
1892//
1893// Often in these cases, if we just do a dumb stack walk we'll get past this
1894// tricky frame and our usual techniques can continue to be used.
1895
1897 if (m_fallback_unwind_plan_sp == nullptr)
1898 return false;
1899
1900 if (m_full_unwind_plan_sp == nullptr)
1901 return false;
1902
1904 m_full_unwind_plan_sp->GetSourceName() ==
1905 m_fallback_unwind_plan_sp->GetSourceName()) {
1906 return false;
1907 }
1908
1909 // If a compiler generated unwind plan failed, trying the arch default
1910 // unwindplan isn't going to do any better.
1911 if (m_full_unwind_plan_sp->GetSourcedFromCompiler() == eLazyBoolYes)
1912 return false;
1913
1914 // Get the caller's pc value and our own CFA value. Swap in the fallback
1915 // unwind plan, re-fetch the caller's pc value and CFA value. If they're the
1916 // same, then the fallback unwind plan provides no benefit.
1917
1920
1921 addr_t old_caller_pc_value = LLDB_INVALID_ADDRESS;
1922 addr_t new_caller_pc_value = LLDB_INVALID_ADDRESS;
1925 regloc) ==
1927 const RegisterInfo *reg_info =
1929 if (reg_info) {
1930 RegisterValue reg_value;
1931 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
1932 old_caller_pc_value = reg_value.GetAsUInt64();
1933 if (ProcessSP process_sp = m_thread.GetProcess()) {
1934 if (ABISP abi_sp = process_sp->GetABI())
1935 old_caller_pc_value = abi_sp->FixCodeAddress(old_caller_pc_value);
1936 }
1937 }
1938 }
1939 }
1940
1941 // This is a tricky wrinkle! If SavedLocationForRegister() detects a really
1942 // impossible register location for the full unwind plan, it may call
1943 // ForceSwitchToFallbackUnwindPlan() which in turn replaces the full
1944 // unwindplan with the fallback... in short, we're done, we're using the
1945 // fallback UnwindPlan. We checked if m_fallback_unwind_plan_sp was nullptr
1946 // at the top -- the only way it became nullptr since then is via
1947 // SavedLocationForRegister().
1948 if (m_fallback_unwind_plan_sp == nullptr)
1949 return true;
1950
1951 // Switch the full UnwindPlan to be the fallback UnwindPlan. If we decide
1952 // this isn't working, we need to restore. We'll also need to save & restore
1953 // the value of the m_cfa ivar. Save is down below a bit in 'old_cfa'.
1954 std::shared_ptr<const UnwindPlan> original_full_unwind_plan_sp =
1956 addr_t old_cfa = m_cfa;
1957 addr_t old_afa = m_afa;
1958
1959 m_registers.clear();
1960
1962
1963 const UnwindPlan::Row *active_row =
1964 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(
1966
1967 Log *log = GetLog(LLDBLog::Unwind);
1968 if (active_row &&
1969 active_row->GetCFAValue().GetValueType() !=
1971 addr_t new_cfa;
1972 ProcessSP process_sp = m_thread.GetProcess();
1973 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
1974 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
1975 active_row->GetCFAValue(), new_cfa) ||
1976 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
1977 UNWIND_LOG(log, "failed to get cfa with fallback unwindplan");
1979 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
1980 return false;
1981 }
1982 m_cfa = new_cfa;
1983
1985 active_row->GetAFAValue(), m_afa);
1986
1988 regloc) ==
1990 const RegisterInfo *reg_info =
1992 if (reg_info) {
1993 RegisterValue reg_value;
1994 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info,
1995 reg_value)) {
1996 new_caller_pc_value = reg_value.GetAsUInt64();
1997 if (process_sp)
1998 new_caller_pc_value =
1999 process_sp->FixCodeAddress(new_caller_pc_value);
2000 }
2001 }
2002 }
2003
2004 if (new_caller_pc_value == LLDB_INVALID_ADDRESS) {
2005 UNWIND_LOG(log, "failed to get a pc value for the caller frame with the "
2006 "fallback unwind plan");
2008 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
2009 m_cfa = old_cfa;
2010 m_afa = old_afa;
2011 return false;
2012 }
2013
2014 if (old_caller_pc_value == new_caller_pc_value &&
2015 m_cfa == old_cfa &&
2016 m_afa == old_afa) {
2017 UNWIND_LOG(log, "fallback unwind plan got the same values for this frame "
2018 "CFA and caller frame pc, not using");
2020 m_full_unwind_plan_sp = original_full_unwind_plan_sp;
2021 return false;
2022 }
2023
2024 UNWIND_LOG(log,
2025 "trying to unwind from this function with the UnwindPlan '{0}' "
2026 "because UnwindPlan '{1}' failed.",
2027 m_fallback_unwind_plan_sp->GetSourceName(),
2028 original_full_unwind_plan_sp->GetSourceName());
2029
2030 // We've copied the fallback unwind plan into the full - now clear the
2031 // fallback.
2034 }
2035
2036 return true;
2037}
2038
2040 if (m_fallback_unwind_plan_sp == nullptr)
2041 return false;
2042
2043 if (m_full_unwind_plan_sp == nullptr)
2044 return false;
2045
2047 m_full_unwind_plan_sp->GetSourceName() ==
2048 m_fallback_unwind_plan_sp->GetSourceName()) {
2049 return false;
2050 }
2051
2052 const UnwindPlan::Row *active_row =
2053 m_fallback_unwind_plan_sp->GetRowForFunctionOffset(m_current_offset);
2054
2055 if (active_row &&
2056 active_row->GetCFAValue().GetValueType() !=
2058 addr_t new_cfa;
2059 ProcessSP process_sp = m_thread.GetProcess();
2060 ABISP abi_sp = process_sp ? process_sp->GetABI() : nullptr;
2061 if (!ReadFrameAddress(m_fallback_unwind_plan_sp->GetRegisterKind(),
2062 active_row->GetCFAValue(), new_cfa) ||
2063 !CallFrameAddressIsValid(abi_sp, new_cfa)) {
2065 "failed to get cfa with fallback unwindplan");
2067 return false;
2068 }
2069
2071 active_row->GetAFAValue(), m_afa);
2072
2075
2076 m_registers.clear();
2077
2078 m_cfa = new_cfa;
2079
2081
2083 "switched unconditionally to the fallback unwindplan {0}",
2084 m_full_unwind_plan_sp->GetSourceName());
2085 return true;
2086 }
2087 return false;
2088}
2089
2091 std::shared_ptr<const UnwindPlan> unwind_plan) {
2092 if (unwind_plan->GetUnwindPlanForSignalTrap() != eLazyBoolYes) {
2093 // Unwind plan does not indicate trap handler. Do nothing. We may
2094 // already be flagged as trap handler flag due to the symbol being
2095 // in the trap handler symbol list, and that should take precedence.
2096 return;
2097 } else if (m_frame_type != eNormalFrame) {
2098 // If this is already a trap handler frame, nothing to do.
2099 // If this is a skip or debug or invalid frame, don't override that.
2100 return;
2101 }
2102
2104
2105 Log *log = GetLog(LLDBLog::Unwind);
2106 UNWIND_LOG(log, "This frame is marked as a trap handler via its UnwindPlan");
2107
2109 // We backed up the pc by 1 to compute the symbol context, but
2110 // now need to undo that because the pc of the trap handler
2111 // frame may in fact be the first instruction of a signal return
2112 // trampoline, rather than the instruction after a call. This
2113 // happens on systems where the signal handler dispatch code, rather
2114 // than calling the handler and being returned to, jumps to the
2115 // handler after pushing the address of a return trampoline on the
2116 // stack -- on these systems, when the handler returns, control will
2117 // be transferred to the return trampoline, so that's the best
2118 // symbol we can present in the callstack.
2119 UNWIND_LOG(log,
2120 "Resetting current offset and re-doing symbol lookup; old "
2121 "symbol was {0}",
2124
2125 m_sym_ctx_valid = m_current_pc.ResolveFunctionScope(m_sym_ctx);
2126
2127 UNWIND_LOG(log, "Symbol is now {0}", GetSymbolOrFunctionName(m_sym_ctx));
2128
2129 ExecutionContext exe_ctx(m_thread.shared_from_this());
2130 Process *process = exe_ctx.GetProcessPtr();
2131 Target *target = &process->GetTarget();
2132
2133 if (m_sym_ctx_valid) {
2134 m_start_pc = m_sym_ctx.GetFunctionOrSymbolAddress();
2135 m_current_offset = m_current_pc.GetLoadAddress(target) -
2136 m_start_pc.GetLoadAddress(target);
2137 }
2138 }
2139}
2140
2142 lldb::RegisterKind row_register_kind, const UnwindPlan::Row::FAValue &fa,
2143 addr_t &address) {
2144 RegisterValue reg_value;
2145
2146 address = LLDB_INVALID_ADDRESS;
2147 addr_t cfa_reg_contents;
2148 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2149
2150 Log *log = GetLog(LLDBLog::Unwind);
2151 switch (fa.GetValueType()) {
2153 UNWIND_LOG(log, "CFA value via dereferencing reg");
2154 RegisterNumber regnum_to_deref(m_thread, row_register_kind,
2155 fa.GetRegisterNumber());
2156 addr_t reg_to_deref_contents;
2157 if (ReadGPRValue(regnum_to_deref, reg_to_deref_contents)) {
2158 const RegisterInfo *reg_info =
2160 RegisterValue reg_value;
2161 if (reg_info) {
2163 reg_info, reg_to_deref_contents, reg_info->byte_size, reg_value);
2164 if (error.Success()) {
2165 address = reg_value.GetAsUInt64();
2166 UNWIND_LOG(log,
2167 "CFA value via dereferencing reg {0} ({1}): reg has val "
2168 "{2:x}, CFA value is {3:x}",
2169 regnum_to_deref.GetName(),
2170 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2171 reg_to_deref_contents, address);
2172 return true;
2173 } else {
2174 UNWIND_LOG(
2175 log,
2176 "Tried to deref reg {0} ({1}) [{2:x}] but memory read failed.",
2177 regnum_to_deref.GetName(),
2178 regnum_to_deref.GetAsKind(eRegisterKindLLDB),
2179 reg_to_deref_contents);
2180 }
2181 }
2182 }
2183 break;
2184 }
2186 UNWIND_LOG(log, "CFA value via register plus offset");
2187 RegisterNumber cfa_reg(m_thread, row_register_kind,
2188 fa.GetRegisterNumber());
2189 if (ReadGPRValue(cfa_reg, cfa_reg_contents)) {
2190 if (!CallFrameAddressIsValid(abi_sp, cfa_reg_contents)) {
2191 UNWIND_LOG(
2192 log,
2193 "Got an invalid CFA register value - reg {0} ({1}), value {2:x}",
2194 cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2195 cfa_reg_contents);
2196 return false;
2197 }
2198 address = cfa_reg_contents + fa.GetOffset();
2199 UNWIND_LOG(
2200 log,
2201 "CFA is {0:x}: Register {1} ({2}) contents are {3:x}, offset is {4}",
2202 address, cfa_reg.GetName(), cfa_reg.GetAsKind(eRegisterKindLLDB),
2203 cfa_reg_contents, fa.GetOffset());
2204 return true;
2205 }
2206 UNWIND_LOG(log, "unable to read CFA register {0} ({1})", cfa_reg.GetName(),
2207 cfa_reg.GetAsKind(eRegisterKindLLDB));
2208 break;
2209 }
2211 UNWIND_LOG(log, "CFA value via DWARF expression");
2212 ExecutionContext exe_ctx(m_thread.shared_from_this());
2213 Process *process = exe_ctx.GetProcessPtr();
2216 process->GetByteOrder(),
2217 process->GetAddressByteSize());
2218 ModuleSP opcode_ctx;
2219 DWARFExpressionList dwarfexpr(opcode_ctx, dwarfdata, nullptr);
2221 row_register_kind);
2222 llvm::Expected<Value> result =
2223 dwarfexpr.Evaluate(&exe_ctx, this, 0, nullptr, nullptr);
2224 if (result) {
2225 address = result->GetScalar().ULongLong();
2226 UNWIND_LOG(log, "CFA value set by DWARF expression is {0:x}", address);
2227 return true;
2228 }
2229 LLDB_LOG_ERROR(log, result.takeError(),
2230 "Failed to set CFA value via DWARF expression: {0}");
2231 break;
2232 }
2234 UNWIND_LOG(log, "CFA value via heuristic search");
2235 Process &process = *m_thread.GetProcess();
2236 lldb::addr_t return_address_hint = GetReturnAddressHint(fa.GetOffset());
2237 if (return_address_hint == LLDB_INVALID_ADDRESS)
2238 return false;
2239 const unsigned max_iterations = 256;
2240 for (unsigned i = 0; i < max_iterations; ++i) {
2241 lldb::addr_t candidate_addr =
2242 return_address_hint + i * process.GetAddressByteSize();
2243 llvm::Expected<lldb::addr_t> candidate =
2244 process.ReadPointerFromMemory(candidate_addr);
2245 if (!candidate) {
2246 LLDB_LOG_ERROR(log, candidate.takeError(),
2247 "Cannot read memory at {1:x}: {0}", candidate_addr);
2248 return false;
2249 }
2250 Address addr;
2251 uint32_t permissions;
2252 if (process.GetLoadAddressPermissions(*candidate, permissions) &&
2253 permissions & lldb::ePermissionsExecutable) {
2254 address = candidate_addr;
2255 UNWIND_LOG(log, "Heuristically found CFA: {0:x}", address);
2256 return true;
2257 }
2258 }
2259 UNWIND_LOG(log, "No suitable CFA found");
2260 break;
2261 }
2263 address = fa.GetConstant();
2264 UNWIND_LOG(log, "CFA value set by constant is {0:x}", address);
2265 return true;
2266 }
2267 default:
2268 return false;
2269 }
2270 return false;
2271}
2272
2274 addr_t hint;
2276 return LLDB_INVALID_ADDRESS;
2277 if (!m_sym_ctx.module_sp || !m_sym_ctx.symbol)
2278 return LLDB_INVALID_ADDRESS;
2279 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2280 hint = abi_sp->FixCodeAddress(hint);
2281
2282 hint += plan_offset;
2283
2284 if (auto next = GetNextFrame()) {
2285 if (!next->m_sym_ctx.module_sp || !next->m_sym_ctx.symbol)
2286 return LLDB_INVALID_ADDRESS;
2287 if (auto expected_size =
2288 next->m_sym_ctx.module_sp->GetSymbolFile()->GetParameterStackSize(
2289 *next->m_sym_ctx.symbol))
2290 hint += *expected_size;
2291 else {
2292 LLDB_LOG_ERRORV(GetLog(LLDBLog::Unwind), expected_size.takeError(),
2293 "Could not retrieve parameter size: {0}");
2294 return LLDB_INVALID_ADDRESS;
2295 }
2296 }
2297 return hint;
2298}
2299
2300// Retrieve a general purpose register value for THIS frame, as saved by the
2301// NEXT frame, i.e. the frame that
2302// this frame called. e.g.
2303//
2304// foo () { }
2305// bar () { foo (); }
2306// main () { bar (); }
2307//
2308// stopped in foo() so
2309// frame 0 - foo
2310// frame 1 - bar
2311// frame 2 - main
2312// and this RegisterContext is for frame 1 (bar) - if we want to get the pc
2313// value for frame 1, we need to ask
2314// where frame 0 (the "next" frame) saved that and retrieve the value.
2315
2317 uint32_t regnum, addr_t &value) {
2318 if (!IsValid())
2319 return false;
2320
2321 uint32_t lldb_regnum;
2322 if (register_kind == eRegisterKindLLDB) {
2323 lldb_regnum = regnum;
2324 } else if (!m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2325 register_kind, regnum, eRegisterKindLLDB, lldb_regnum)) {
2326 return false;
2327 }
2328
2329 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(lldb_regnum);
2330 assert(reg_info);
2331 if (!reg_info) {
2332 UNWIND_LOG(
2334 "Could not find RegisterInfo definition for lldb register number {0}",
2335 lldb_regnum);
2336 return false;
2337 }
2338
2339 uint32_t generic_regnum = LLDB_INVALID_REGNUM;
2340 if (register_kind == eRegisterKindGeneric)
2341 generic_regnum = regnum;
2342 else
2343 m_thread.GetRegisterContext()->ConvertBetweenRegisterKinds(
2344 register_kind, regnum, eRegisterKindGeneric, generic_regnum);
2345 ABISP abi_sp = m_thread.GetProcess()->GetABI();
2346
2347 RegisterValue reg_value;
2348 // if this is frame 0 (currently executing frame), get the requested reg
2349 // contents from the actual thread registers
2350 if (IsFrameZero()) {
2351 if (m_thread.GetRegisterContext()->ReadRegister(reg_info, reg_value)) {
2352 value = reg_value.GetAsUInt64();
2353 if (abi_sp && generic_regnum != LLDB_INVALID_REGNUM) {
2354 if (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2355 generic_regnum == LLDB_REGNUM_GENERIC_RA)
2356 value = abi_sp->FixCodeAddress(value);
2357 }
2358 return true;
2359 }
2360 return false;
2361 }
2362
2363 bool pc_register = false;
2364 if (generic_regnum != LLDB_INVALID_REGNUM &&
2365 (generic_regnum == LLDB_REGNUM_GENERIC_PC ||
2366 generic_regnum == LLDB_REGNUM_GENERIC_RA))
2367 pc_register = true;
2368
2370 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2371 lldb_regnum, regloc, m_frame_number - 1, pc_register)) {
2372 return false;
2373 }
2374 if (ReadRegisterValueFromRegisterLocation(regloc, reg_info, reg_value)) {
2375 value = reg_value.GetAsUInt64();
2376 if (pc_register) {
2377 if (ABISP abi_sp = m_thread.GetProcess()->GetABI()) {
2378 value = abi_sp->FixCodeAddress(value);
2379 }
2380 }
2381 return true;
2382 }
2383 return false;
2384}
2385
2387 addr_t &value) {
2388 return ReadGPRValue(regnum.GetRegisterKind(), regnum.GetRegisterNumber(),
2389 value);
2390}
2391
2392// Find the value of a register in THIS frame
2393
2395 RegisterValue &value) {
2396 if (!IsValid())
2397 return false;
2398
2399 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2400 Log *log = GetLog(LLDBLog::Unwind);
2401 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2402 lldb_regnum);
2403
2404 // If this is the 0th frame, hand this over to the live register context
2405 if (IsFrameZero()) {
2407 "passing along to the live register context for reg {0}",
2408 lldb_regnum);
2409 return m_thread.GetRegisterContext()->ReadRegister(reg_info, value);
2410 }
2411
2412 bool is_pc_regnum = false;
2415 is_pc_regnum = true;
2416 }
2417
2419 // Find out where the NEXT frame saved THIS frame's register contents
2420 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2421 lldb_regnum, regloc, m_frame_number - 1, is_pc_regnum))
2422 return false;
2423
2424 bool result = ReadRegisterValueFromRegisterLocation(regloc, reg_info, value);
2425 if (result) {
2426 if (is_pc_regnum && value.GetType() == RegisterValue::eTypeUInt64) {
2427 addr_t reg_value = value.GetAsUInt64(LLDB_INVALID_ADDRESS);
2428 if (reg_value != LLDB_INVALID_ADDRESS) {
2429 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2430 value = abi_sp->FixCodeAddress(reg_value);
2431 }
2432 }
2433 }
2434 return result;
2435}
2436
2438 const RegisterValue &value) {
2439 if (!IsValid())
2440 return false;
2441
2442 const uint32_t lldb_regnum = reg_info->kinds[eRegisterKindLLDB];
2443 Log *log = GetLog(LLDBLog::Unwind);
2444 UNWIND_LOG_VERBOSE(log, "looking for register saved location for reg {0}",
2445 lldb_regnum);
2446
2447 // If this is the 0th frame, hand this over to the live register context
2448 if (IsFrameZero()) {
2450 "passing along to the live register context for reg {0}",
2451 lldb_regnum);
2452 return m_thread.GetRegisterContext()->WriteRegister(reg_info, value);
2453 }
2454
2456 // Find out where the NEXT frame saved THIS frame's register contents
2457 if (!m_parent_unwind.SearchForSavedLocationForRegister(
2458 lldb_regnum, regloc, m_frame_number - 1, false))
2459 return false;
2460
2461 return WriteRegisterValueToRegisterLocation(regloc, reg_info, value);
2462}
2463
2464// Don't need to implement this one
2466 lldb::WritableDataBufferSP &data_sp) {
2467 return false;
2468}
2469
2470// Don't need to implement this one
2472 const lldb::DataBufferSP &data_sp) {
2473 return false;
2474}
2475
2476// Retrieve the pc value for THIS from
2477
2479 if (!IsValid()) {
2480 return false;
2481 }
2482 if (m_cfa == LLDB_INVALID_ADDRESS) {
2483 return false;
2484 }
2485 cfa = m_cfa;
2486 return true;
2487}
2488
2491 if (m_frame_number == 0)
2492 return regctx;
2493 return m_parent_unwind.GetRegisterContextForFrameNum(m_frame_number - 1);
2494}
2495
2500
2501// Retrieve the address of the start of the function of THIS frame
2502
2504 if (!IsValid())
2505 return false;
2506
2507 if (!m_start_pc.IsValid()) {
2508 bool read_successfully = ReadPC (start_pc);
2509 if (read_successfully)
2510 {
2511 ProcessSP process_sp (m_thread.GetProcess());
2512 if (process_sp)
2513 {
2514 if (ABISP abi_sp = process_sp->GetABI())
2515 start_pc = abi_sp->FixCodeAddress(start_pc);
2516 }
2517 }
2518 return read_successfully;
2519 }
2520 start_pc = m_start_pc.GetLoadAddress(CalculateTarget().get());
2521 return true;
2522}
2523
2524// Retrieve the current pc value for THIS frame, as saved by the NEXT frame.
2525
2527 if (!IsValid())
2528 return false;
2529
2530 bool above_trap_handler = false;
2531 if (GetNextFrame().get() && GetNextFrame()->IsValid() &&
2533 above_trap_handler = true;
2534
2536 // A pc value of 0 or 1 is impossible in the middle of the stack -- it
2537 // indicates the end of a stack walk.
2538 // On the currently executing frame (or such a frame interrupted
2539 // asynchronously by sigtramp et al) this may occur if code has jumped
2540 // through a NULL pointer -- we want to be able to unwind past that frame
2541 // to help find the bug.
2542
2543 if (ABISP abi_sp = m_thread.GetProcess()->GetABI())
2544 pc = abi_sp->FixCodeAddress(pc);
2545
2546 return !(m_all_registers_available == false &&
2547 above_trap_handler == false && (pc == 0 || pc == 1));
2548 } else {
2549 return false;
2550 }
2551}
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:2836
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3960
llvm::Expected< lldb::addr_t > ReadPointerFromMemory(lldb::addr_t vm_addr)
Definition Process.cpp:2550
uint32_t GetAddressByteSize() const
Definition Process.cpp:3964
virtual DynamicLoader * GetDynamicLoader()
Get the dynamic loader plug-in for this process.
Definition Process.cpp:3125
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 ForceSwitchToFallbackUnwindPlan()
Switch to the fallback unwind plan unconditionally without any safety checks that it is providing bet...
lldb_private::UnwindLLDB::RegisterSearchResult SavedLocationForRegister(uint32_t lldb_regnum, lldb_private::UnwindLLDB::ConcreteRegisterLocation &regloc)
bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum, lldb::addr_t &value)
lldb_private::UnwindLLDB & m_parent_unwind
bool WriteRegisterValueToRegisterLocation(lldb_private::UnwindLLDB::ConcreteRegisterLocation regloc, const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value)
bool TryFallbackUnwindPlan()
If the unwind has to the caller frame has failed, try something else.
std::optional< int > m_current_offset
How far into the function we've executed.
bool ReadRegister(const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value) override
lldb::addr_t GetReturnAddressHint(int32_t plan_offset)
bool IsTrapHandlerSymbol(lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const
Determines if a SymbolContext is a trap handler or not.
uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override
Convert from a given register numbering scheme to the lldb register numbering scheme.
bool IsUnwindPlanValidForCurrentPC(std::shared_ptr< const UnwindPlan > unwind_plan_sp)
std::shared_ptr< const UnwindPlan > m_fallback_unwind_plan_sp
lldb_private::SymbolContext & m_sym_ctx
bool BehavesLikeZerothFrame() const override
Indicates that this frame is currently executing code, that the PC value is not a return-pc but an ac...
std::shared_ptr< const UnwindPlan > m_full_unwind_plan_sp
virtual Status ReadRegisterValueFromMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, RegisterValue &reg_value)
RegisterContext(Thread &thread, uint32_t concrete_frame_idx)
lldb::TargetSP CalculateTarget() override
virtual Status WriteRegisterValueToMemory(const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, const RegisterValue &reg_value)
bool SetUInt(uint64_t uint, uint32_t byte_size)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
RegisterValue::Type GetType() const
An error handling class.
Definition Status.h:118
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:612
lldb::PlatformSP GetPlatform()
Definition Target.h:2007
const ArchSpec & GetArchitecture() const
Definition Target.h:1324
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