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