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