Skip to contentSkip to conversation

] riscv: add backtrace support

PATCH In review Ben Dooks 2 participants 2 messages Sep 5, 2023 – Mar 28, 2024 RISC-V

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

PATCH v2 1/1 ] riscv: add backtrace support

When debugging, it is useful to have a backtrace to find out what is in the call stack as the previous function (RA) may not have been the culprit.

Since this adds size to the build, do not add it by default and avoid putting it in the SPL build if not needed.

---
v2:
- back to codethink email as sifive account is now gone
- add option to build SPL with frame pointer (as suggested by Bo Gan <[email protected]>

---
 arch/riscv/Kconfig          | 20 ++++++++++++++++++++
 arch/riscv/Makefile         |  4 ++++
 arch/riscv/cpu/start.S      |  1 +
 arch/riscv/lib/interrupts.c | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 60 insertions(+)
arch/riscv/Kconfig +20 -0
index 6771d8d919..a446166631 100644
@@ -111,6 +111,26 @@ config ARCH_RV64I
 
 endchoice
 
+config FRAMEPOINTER
+	bool "Build with frame pointer for stack unwinding"
+	help
+	  Choose this option to use the frame pointer so the stack can be
+	  unwound if needed. This is useful for tracing where faults came
+	  from as the source may be several functions back
+
+	  If you say Y here, then the code size will be increased due to
+	  having to store the fp.
+
+config SPL_FRAMEPOINTER
+	bool "Build SPL with frame pointer for stack unwinding"
+	help
+	  Choose this option to use the frame pointer so the stack can be
+	  unwound if needed. This is useful for tracing where faults came
+	  from as the source may be several functions back
+
+	  If you say Y here, then the code size will be increased due to
+	  having to store the fp.
+
 choice
 	prompt "Code Model"
 	default CMODEL_MEDLOW
arch/riscv/Makefile +4 -0
index 4963b5109b..0cb60c7c7e 100644
@@ -45,6 +45,10 @@ endif
 ARCH_FLAGS = -march=$(RISCV_MARCH) -mabi=$(ABI) \
 	     -mcmodel=$(CMODEL)
 
+ifeq ($(CONFIG_$(SPL_)FRAMEPOINTER),y)
+	ARCH_FLAGS += -fno-omit-frame-pointer
+endif
+
 PLATFORM_CPPFLAGS	+= $(ARCH_FLAGS)
 CFLAGS_EFI		+= $(ARCH_FLAGS)
 
arch/riscv/cpu/start.S +1 -0
index 30cf674370..53de424378 100644
@@ -419,6 +419,7 @@ call_board_init_r:
  */
 	mv	a0, s3			/* gd_t */
 	mv	a1, s4			/* dest_addr */
+	mv	s0, zero		/* fp == NULL */
 
 /*
  * jump to it ...
arch/riscv/lib/interrupts.c +35 -0
index e966afa7e3..db3d7e294b 100644
@@ -53,6 +53,40 @@ static void show_regs(struct pt_regs *regs)
 #endif
 }
 
+#if defined(CONFIG_FRAMEPOINTER) || defined(CONFIG_SPL_FRAMEPOINTER)
+static void show_backtrace(struct pt_regs *regs)
+{
+	uintptr_t *fp = (uintptr_t *)regs->s0;
+	unsigned count = 0;
+	ulong ra;
+
+	printf("backtrace:\n");
+
+	/* there are a few entry points where the s0 register is
+	 * set to gd, so to avoid changing those, just abort if
+	 * the value is the same */
+	while (fp != NULL && fp != (uintptr_t *)gd) {
+		ra = fp[-1];
+		printf("backtrace %2d: FP: " REG_FMT " RA: " REG_FMT,
+		       count, (ulong)fp, ra);
+
+		if (gd && gd->flags & GD_FLG_RELOC)
+			printf(" - RA: " REG_FMT " reloc adjusted\n",
+			ra - gd->reloc_off);
+		else
+			printf("\n");
+
+		fp = (uintptr_t *)fp[-2];
+		count++;
+	}
+}
+#else
+static void show_backtrace(struct pt_regs *regs)
+{
+	printf("No backtrace support enabled\n");
+}
+#endif
+
 /**
  * instr_len() - get instruction length
  *
@@ -119,6 +153,7 @@ static void _exit_trap(ulong code, ulong epc, ulong tval, struct pt_regs *regs)
 		       epc - gd->reloc_off, regs->ra - gd->reloc_off);
 
 	show_regs(regs);
+	show_backtrace(regs);
 	show_code(epc);
 	show_efi_loaded_images(epc);
 	panic("\n");
Signature · 1 line
2.40.1
Tested-by on v2 1/1
#1 lore raw
Quoted 13 lines · Ben Dooks

When debugging, it is useful to have a backtrace to find out what is in the call stack as the previous function (RA) may not have been the culprit.

Since this adds size to the build, do not add it by default and avoid putting it in the SPL build if not needed.

---
v2:
- back to codethink email as sifive account is now gone
- add option to build SPL with frame pointer (as suggested by Bo Gan <[email protected]>

Quoted 6 lines
---
 arch/riscv/Kconfig          | 20 ++++++++++++++++++++
 arch/riscv/Makefile         |  4 ++++
 arch/riscv/cpu/start.S      |  1 +
 arch/riscv/lib/interrupts.c | 35 +++++++++++++++++++++++++++++++++++
 4 files changed, 60 insertions(+)