Skip to content

Commit

Permalink
RISCV64: Fix 'bt' output when no ra on the stack top
Browse files Browse the repository at this point in the history
Same as the Linux commit f766f77a74f5 ("riscv/stacktrace: Fix
stack output without ra on the stack top").

When a function doesn't have a callee, then it will not
push ra into the stack, such as lkdtm functions, so
correct the FP of the second frame and use pt_regs to get
the right PC of the second frame.

Before this patch, the `bt -f` outputs only the first frame with
the wrong PC and FP of next frame:
```
crash> bt -f
PID: 1        TASK: ff600000000e0000  CPU: 1    COMMAND: "sh"
 #0 [ff20000000013cf0] lkdtm_EXCEPTION at ffffffff805303c0
    [PC: ffffffff805303c0 RA: ff20000000013d10 SP: ff20000000013cf0 SIZE: 16] <- wrong next PC
    ff20000000013cf0: 0000000000000001 ff20000000013d10 <- next FP
    ff20000000013d00: ff20000000013d40
crash>
```
After this patch, the `bt` outputs the full frames:
```
crash> bt
PID: 1        TASK: ff600000000e0000  CPU: 1    COMMAND: "sh"
 #0 [ff20000000013cf0] lkdtm_EXCEPTION at ffffffff805303c0
 crash-utility#1 [ff20000000013d00] lkdtm_do_action at ffffffff8052fe36
 crash-utility#2 [ff20000000013d10] direct_entry at ffffffff80530018
 crash-utility#3 [ff20000000013d40] full_proxy_write at ffffffff80305044
 crash-utility#4 [ff20000000013d80] vfs_write at ffffffff801b68b4
 crash-utility#5 [ff20000000013e30] ksys_write at ffffffff801b6c4a
 crash-utility#6 [ff20000000013e80] __riscv_sys_write at ffffffff801b6cc4
 crash-utility#7 [ff20000000013e90] do_trap_ecall_u at ffffffff80836798
crash>
```

Acked-by: Kazuhito Hagio <[email protected]>
Signed-off-by: Song Shuai <[email protected]>
  • Loading branch information
Song Shuai authored and liutgnu committed Dec 1, 2024
1 parent e714205 commit 468b4e2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions riscv64.c
Original file line number Diff line number Diff line change
Expand Up @@ -747,11 +747,14 @@ riscv64_back_trace_cmd(struct bt_info *bt)
{
struct riscv64_unwind_frame current, previous;
struct stackframe curr_frame;
struct riscv64_register * regs;
int level = 0;

if (bt->flags & BT_REGS_NOT_FOUND)
return;

regs = (struct riscv64_register *) bt->machdep;

current.pc = bt->instptr;
current.sp = bt->stkptr;
current.fp = bt->frameptr;
Expand Down Expand Up @@ -788,8 +791,16 @@ riscv64_back_trace_cmd(struct bt_info *bt)
sizeof(curr_frame), "get stack frame", RETURN_ON_ERROR))
return;

previous.pc = curr_frame.ra;
previous.fp = curr_frame.fp;
/* correct PC and FP of the second frame when the first frame has no callee */

if (regs && (regs->regs[RISCV64_REGS_EPC] == current.pc) && curr_frame.fp & 0x7){
previous.pc = regs->regs[RISCV64_REGS_RA];
previous.fp = curr_frame.ra;
} else {
previous.pc = curr_frame.ra;
previous.fp = curr_frame.fp;
}

previous.sp = current.fp;

riscv64_dump_backtrace_entry(bt, symbol, &current, &previous, level++);
Expand Down

0 comments on commit 468b4e2

Please sign in to comment.