improved runtime error handling

This commit is contained in:
Bernd Herzog 2023-03-18 22:41:26 +01:00
parent ed1c2e1c03
commit b83f43793c
6 changed files with 75 additions and 28 deletions

View File

@ -29,13 +29,14 @@
#include "ui_painter.hpp"
#include "portapack.hpp"
#include "ui_font_fixed_8x16.hpp"
#include "string_format.hpp"
void runtime_error(LED);
std::string number_to_hex_string(uint32_t);
void draw_line(int32_t, const char *, regarm_t);
static bool error_shown = false;
extern void draw_guru_meditation_header(uint8_t source, const char *hint) {
void draw_guru_meditation_header(uint8_t source, const char *hint) {
ui::Painter painter;
ui::Style style_default {
.font = ui::font::fixed_8x16,
@ -81,7 +82,7 @@ void draw_guru_meditation(uint8_t source, const char *hint) {
runtime_error(hackrf::one::led_tx);
}
void draw_guru_meditation(uint8_t source, const char *hint, struct extctx *ctxp) {
void draw_guru_meditation(uint8_t source, const char *hint, struct extctx *ctxp, uint32_t cfsr = 0) {
if(error_shown == false)
{
error_shown = true;
@ -102,6 +103,10 @@ void draw_guru_meditation(uint8_t source, const char *hint, struct extctx *ctxp)
// to see whats causing the fault.
draw_line(80 + i++ * 20, "lr:", ctxp->lr_thd);
draw_line(80 + i++ * 20, "pc:", ctxp->pc);
// see SCB_CFSR_* in libopencm3/cm3/scb.h for details
if (cfsr != 0)
draw_line(80 + i++ * 20, "cfsr:", (void *)cfsr);
}
}
@ -121,23 +126,7 @@ void draw_line(int32_t y_offset, const char *label, regarm_t value){
};
painter.draw_string({ 15, y_offset }, style_default, label);
painter.draw_string({ 15 + 8*8, y_offset }, style_default, number_to_hex_string((uint32_t)value));
}
std::string number_to_hex_string(uint32_t number){
char str[16];
char* p = &str[16];
do {
p--;
uint32_t digit = number % 16;
number /= 16;
*p = digit>=10 ? 'A' + (digit-10) : '0' + digit;
} while ( number > 0 );
p--;
*p = 'x';
p--;
*p = '0';
return std::string(p, &str[16]-p);
painter.draw_string({ 15 + 8*8, y_offset }, style_default, to_string_hex((uint32_t)value, 8));
}
void runtime_error(LED led) {
@ -198,6 +187,11 @@ CH_IRQ_HANDLER(HardFaultVector) {
ctxp = (struct extctx *)__get_MSP();
port_disable();
auto stack_space_left = get_free_stack_space();
if (stack_space_left < 16)
draw_guru_meditation(CORTEX_M0, "Stack Overflow", ctxp);
draw_guru_meditation(CORTEX_M0, "Hard Fault", ctxp);
CH_IRQ_EPILOGUE();

View File

@ -26,6 +26,18 @@
#include "hackrf_gpio.hpp"
extern void draw_guru_meditation(uint8_t, const char *);
extern void draw_guru_meditation(uint8_t, const char *, struct extctx *);
extern void draw_guru_meditation(uint8_t, const char *, struct extctx *, uint32_t);
extern uint32_t __process_stack_base__;
extern uint32_t __process_stack_end__;
#define CRT0_STACKS_FILL_PATTERN 0x55555555
inline uint32_t get_free_stack_space(){
uint32_t *p;
for (p = &__process_stack_base__; *p == CRT0_STACKS_FILL_PATTERN && p < &__process_stack_end__; p++);
auto stack_space_left = p - &__process_stack_base__;
return stack_space_left;
}
#endif/*__DEBUG_H__*/

View File

@ -145,7 +145,11 @@ void EventDispatcher::dispatch(const eventmask_t events) {
if (shared_memory.bb_data.data[0] == 0)
draw_guru_meditation(CORTEX_M4, shared_memory.m4_panic_msg);
else
draw_guru_meditation(CORTEX_M4, shared_memory.m4_panic_msg, (struct extctx *)&shared_memory.bb_data.data[4]);
draw_guru_meditation(
CORTEX_M4,
shared_memory.m4_panic_msg,
(struct extctx *)&shared_memory.bb_data.data[8],
*(uint32_t *)&shared_memory.bb_data.data[4]);
}
if( events & EVT_MASK_APPLICATION ) {

View File

@ -33,7 +33,8 @@ void write_m4_panic_msg(const char *panic_message, struct extctx *ctxp) {
}
else {
shared_memory.bb_data.data[0] = 1;
memcpy(&shared_memory.bb_data.data[4], ctxp, sizeof(struct extctx));
*((uint32_t *)&shared_memory.bb_data.data[4]) = SCB->CFSR;
memcpy(&shared_memory.bb_data.data[8], ctxp, sizeof(struct extctx));
}
for(size_t i=0; i<sizeof(shared_memory.m4_panic_msg); i++) {
@ -54,6 +55,10 @@ void port_halt(void) {
dbg_panic_msg = "system halted";
write_m4_panic_msg(dbg_panic_msg, nullptr);
while (true) {
HALT_IF_DEBUGGING();
}
}
#endif
@ -85,7 +90,6 @@ CH_IRQ_HANDLER(HardFaultVector) {
#if CH_DBG_ENABLED
regarm_t _saved_lr;
asm volatile ("mov %0, lr" : "=r" (_saved_lr) : : "memory");
CH_IRQ_PROLOGUE();
struct extctx *ctxp;
port_lock_from_isr();
@ -95,12 +99,18 @@ CH_IRQ_HANDLER(HardFaultVector) {
else
ctxp = (struct extctx *)__get_MSP();
write_m4_panic_msg("Hard Fault", ctxp);
volatile uint32_t stack_space_left = get_free_stack_space();
if (stack_space_left < 16)
write_m4_panic_msg("Stack Overflow", ctxp);
else write_m4_panic_msg("Hard Fault", ctxp);
port_disable();
while (true);
CH_IRQ_EPILOGUE();
while (true) {
HALT_IF_DEBUGGING();
}
#else
chSysHalt();
#endif

View File

@ -22,4 +22,25 @@
#ifndef __DEBUG_H__
#define __DEBUG_H__
#include <ch.h>
extern uint32_t __process_stack_base__;
extern uint32_t __process_stack_end__;
#define CRT0_STACKS_FILL_PATTERN 0x55555555
inline uint32_t get_free_stack_space(){
uint32_t *p;
for (p = &__process_stack_base__; *p == CRT0_STACKS_FILL_PATTERN && p < &__process_stack_end__; p++);
auto stack_space_left = p - &__process_stack_base__;
return stack_space_left;
}
#define HALT_IF_DEBUGGING() \
do { \
if ((*(volatile uint32_t *)0xE000EDF0) & (1 << 0)) { \
__asm__ __volatile__("bkpt 1"); \
} \
} while (0)
#endif/*__DEBUG_H__*/

View File

@ -26,11 +26,17 @@
#include <ch.h>
void* operator new(size_t size) {
return chHeapAlloc(0x0, size);
void *p = chHeapAlloc(0x0, size);
if (p == nullptr)
chDbgPanic("Out of Memory");
return p;
}
void* operator new[](size_t size) {
return chHeapAlloc(0x0, size);
void *p = chHeapAlloc(0x0, size);
if (p == nullptr)
chDbgPanic("Out of Memory");
return p;
}
void operator delete(void* p) noexcept {