diff --git a/arch/arm/src/armv7-m/Kconfig b/arch/arm/src/armv7-m/Kconfig index 5dc99f3e00..b1ca99f116 100644 --- a/arch/arm/src/armv7-m/Kconfig +++ b/arch/arm/src/armv7-m/Kconfig @@ -116,6 +116,16 @@ config ARMV7M_STACKCHECK CFLAGS when you compile. This addition to your CFLAGS should probably be added to the definition of the CFFLAGS in your board Make.defs file. +config ARMV7M_STACKCHECK_BREAKPOINT + bool "Breakpoint on stack overflow" + default n + depends on ARMV7M_STACKCHECK + ---help--- + If enabled, a hard-coded breakpoint will be inserted to the stack + overflow trap. This is useful to stop the execution of the program + and diagnose the issue before the hardfault handler is called (and + context information is lost). + config ARMV7M_ITMSYSLOG bool "ITM SYSLOG support" default n diff --git a/arch/arm/src/armv7-m/arm_stackcheck.c b/arch/arm/src/armv7-m/arm_stackcheck.c index 968a190c26..d7167a9c55 100644 --- a/arch/arm/src/armv7-m/arm_stackcheck.c +++ b/arch/arm/src/armv7-m/arm_stackcheck.c @@ -69,6 +69,14 @@ void __stack_overflow_trap(void) uint32_t regval; +#ifdef CONFIG_ARMV7M_STACKCHECK_BREAKPOINT + regval = getreg32(NVIC_DHCSR); + if (regval & NVIC_DHCSR_C_DEBUGEN) + { + __asm("bkpt 1"); + } +#endif + /* force hard fault */ regval = getreg32(NVIC_INTCTRL); diff --git a/arch/arm/src/armv8-m/Kconfig b/arch/arm/src/armv8-m/Kconfig index c6c77cfc2a..550509281c 100644 --- a/arch/arm/src/armv8-m/Kconfig +++ b/arch/arm/src/armv8-m/Kconfig @@ -115,6 +115,16 @@ config ARMV8M_STACKCHECK_HARDWARE endchoice +config ARMV8M_STACKCHECK_BREAKPOINT + bool "Breakpoint on stack overflow" + default n + depends on ARMV8M_STACKCHECK + ---help--- + If enabled, a hard-coded breakpoint will be inserted to the stack + overflow trap. This is useful to stop the execution of the program + and diagnose the issue before the hardfault handler is called (and + context information is lost). + config ARMV8M_ITMSYSLOG bool "ITM SYSLOG support" default n diff --git a/arch/arm/src/armv8-m/arm_stackcheck.c b/arch/arm/src/armv8-m/arm_stackcheck.c index ddc85e26ee..cbe107ca00 100644 --- a/arch/arm/src/armv8-m/arm_stackcheck.c +++ b/arch/arm/src/armv8-m/arm_stackcheck.c @@ -69,6 +69,14 @@ void __stack_overflow_trap(void) uint32_t regval; +#ifdef CONFIG_ARMV8M_STACKCHECK_BREAKPOINT + regval = getreg32(NVIC_DHCSR); + if (regval & NVIC_DHCSR_C_DEBUGEN) + { + __asm("bkpt 1"); + } +#endif + /* force hard fault */ regval = getreg32(NVIC_INTCTRL);