Skip to content

Commit 69cd0f5

Browse files
committed
Implement custom abort and fault handler for debugging
1 parent ab0b6fa commit 69cd0f5

File tree

5 files changed

+390
-43
lines changed

5 files changed

+390
-43
lines changed

src/runtime/runtime_cortexm.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package runtime
44

55
import (
6-
"device/arm"
76
"unsafe"
87
)
98

@@ -40,16 +39,6 @@ func preinit() {
4039
}
4140
}
4241

43-
func abort() {
44-
// disable all interrupts
45-
arm.DisableInterrupts()
46-
47-
// lock up forever
48-
for {
49-
arm.Asm("wfi")
50-
}
51-
}
52-
5342
// The stack layout at the moment an interrupt occurs.
5443
// Registers can be accessed if the stack pointer is cast to a pointer to this
5544
// struct.
@@ -63,35 +52,3 @@ type interruptStack struct {
6352
PC uintptr
6453
PSR uintptr
6554
}
66-
67-
// This function is called at HardFault.
68-
// Before this function is called, the stack pointer is reset to the initial
69-
// stack pointer (loaded from addres 0x0) and the previous stack pointer is
70-
// passed as an argument to this function. This allows for easy inspection of
71-
// the stack the moment a HardFault occurs, but it means that the stack will be
72-
// corrupted by this function and thus this handler must not attempt to recover.
73-
//
74-
// For details, see:
75-
// https://community.arm.com/developer/ip-products/system/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault
76-
// https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
77-
//export handleHardFault
78-
func handleHardFault(sp *interruptStack) {
79-
print("fatal error: ")
80-
if uintptr(unsafe.Pointer(sp)) < 0x20000000 {
81-
print("stack overflow")
82-
} else {
83-
// TODO: try to find the cause of the hard fault. Especially on
84-
// Cortex-M3 and higher it is possible to find more detailed information
85-
// in special status registers.
86-
print("HardFault")
87-
}
88-
print(" with sp=", sp)
89-
if uintptr(unsafe.Pointer(&sp.PC)) >= 0x20000000 {
90-
// Only print the PC if it points into memory.
91-
// It may not point into memory during a stack overflow, so check that
92-
// first before accessing the stack.
93-
print(" pc=", sp.PC)
94-
}
95-
println()
96-
abort()
97-
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// +build cortexm,!nxp
2+
3+
package runtime
4+
5+
import (
6+
"device/arm"
7+
)
8+
9+
func abort() {
10+
// disable all interrupts
11+
arm.DisableInterrupts()
12+
13+
// lock up forever
14+
for {
15+
arm.Asm("wfi")
16+
}
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build cortexm,!nxp
2+
3+
package runtime
4+
5+
import (
6+
"unsafe"
7+
)
8+
9+
// This function is called at HardFault.
10+
// Before this function is called, the stack pointer is reset to the initial
11+
// stack pointer (loaded from addres 0x0) and the previous stack pointer is
12+
// passed as an argument to this function. This allows for easy inspection of
13+
// the stack the moment a HardFault occurs, but it means that the stack will be
14+
// corrupted by this function and thus this handler must not attempt to recover.
15+
//
16+
// For details, see:
17+
// https://community.arm.com/developer/ip-products/system/f/embedded-forum/3257/debugging-a-cortex-m0-hard-fault
18+
// https://blog.feabhas.com/2013/02/developing-a-generic-hard-fault-handler-for-arm-cortex-m3cortex-m4/
19+
//export handleHardFault
20+
func handleHardFault(sp *interruptStack) {
21+
print("fatal error: ")
22+
if uintptr(unsafe.Pointer(sp)) < 0x20000000 {
23+
print("stack overflow")
24+
} else {
25+
// TODO: try to find the cause of the hard fault. Especially on
26+
// Cortex-M3 and higher it is possible to find more detailed information
27+
// in special status registers.
28+
print("HardFault")
29+
}
30+
print(" with sp=", sp)
31+
if uintptr(unsafe.Pointer(&sp.PC)) >= 0x20000000 {
32+
// Only print the PC if it points into memory.
33+
// It may not point into memory during a stack overflow, so check that
34+
// first before accessing the stack.
35+
print(" pc=", sp.PC)
36+
}
37+
println()
38+
abort()
39+
}

0 commit comments

Comments
 (0)