Skip to content

[lldb] Implement RISCV function unwinding using instruction emulation #147434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1899,4 +1899,24 @@ RISCVSingleStepBreakpointLocationsPredictor::HandleAtomicSequence(
return bp_addrs;
}

bool EmulateInstructionRISCV::CreateFunctionEntryUnwind(
UnwindPlan &unwind_plan) {
unwind_plan.Clear();
unwind_plan.SetRegisterKind(eRegisterKindLLDB);

UnwindPlan::Row row;

// Our previous Call Frame Address is the stack pointer
row.GetCFAValue().SetIsRegisterPlusOffset(gpr_sp_riscv, 0);
row.SetRegisterLocationToSame(gpr_fp_riscv, /*must_replace=*/false);

unwind_plan.AppendRow(std::move(row));
unwind_plan.SetSourceName("EmulateInstructionRISCV");
unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolYes);
unwind_plan.SetUnwindPlanForSignalTrap(eLazyBoolNo);
unwind_plan.SetReturnAddressRegister(gpr_ra_riscv);
return true;
}

} // namespace lldb_private
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ class EmulateInstructionRISCV : public EmulateInstruction {

static bool SupportsThisInstructionType(InstructionType inst_type) {
switch (inst_type) {
case eInstructionTypeAny:
case eInstructionTypePCModifying:
case lldb_private::eInstructionTypeAny:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is inside a namespace lldb_private, please don't add lldb_private:: specifiers.

case lldb_private::eInstructionTypePrologueEpilogue:
return true;
case eInstructionTypePrologueEpilogue:
case eInstructionTypeAll:

case lldb_private::eInstructionTypePCModifying:
Copy link
Collaborator

@jasonmolenda jasonmolenda Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EmulateInstructionRISCV is currently used to correctly predict where a given instruction will branch to next, for instruction stepping on hardware that does not support instruction step. By changing eInstructionTypePCModifying to return false from SupportsThisInstructionType, you're going to break that use of this. Did you copy and paste this from another EmulateInstruction plugin? Modifying this plugin without a much deeper understanding of what it is doing & testing it carefully against a corpus of functions is not going to work; this isn't a minor edit.

case lldb_private::eInstructionTypeAll:
return false;
}
llvm_unreachable("Fully covered switch above!");
Expand Down Expand Up @@ -94,6 +95,8 @@ class EmulateInstructionRISCV : public EmulateInstruction {
std::optional<RegisterInfo> GetRegisterInfo(lldb::RegisterKind reg_kind,
uint32_t reg_num) override;

bool CreateFunctionEntryUnwind(UnwindPlan &unwind_plan) override;

std::optional<DecodeResult> ReadInstructionAt(lldb::addr_t addr);
std::optional<DecodeResult> Decode(uint32_t inst);
bool Execute(DecodeResult inst, bool ignore_cond);
Expand Down
Loading