Skip to content

Add ability to call user function from panic #256

@DusterTheFirst

Description

@DusterTheFirst

Panics are, of course, fatal errors, so it makes sense to halt the processor, but a lot of the time, panics are unexpected when they happen (They should get caught or prevented before actual deployment, but things always have a way of going not as planned) and leave the pico in an undefined state. Since the pico does not shut down and stays in the state it was before the panic, it can be useful to reset the pico's outputs or provide other functionality to safely enter the panicked state.

I have little expertise with C and all its linker magic/tools, but I was envisioning a feature much like the #[panic_handler] attribute of rust that would be called in place of the provided panic function.

If achieving this with a macro or some linker tomfoolery is not feasible or elegant, it could also be possible to let the user provide a function pointer that will be called first thing the panic function is entered. Something like:

// leaving out the implementation details of how the user would provide their function
// we could just use a simple setter function
void (*panic_handler)(const char *fmt, ...);

void __attribute__((noreturn)) __printflike(1, 0) panic(const char *fmt, ...) {
    if (panic_handler) {
        panic_handler(/* pass it the args  */);
    }

    // usual panic code
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions