Skip to content

add defmt::panic!#266

Merged
japaric merged 20 commits intomainfrom
panic
Nov 23, 2020
Merged

add defmt::panic!#266
japaric merged 20 commits intomainfrom
panic

Conversation

@japaric
Copy link
Copy Markdown
Member

@japaric japaric commented Nov 17, 2020

closes #136
this implements defmt::panic! which calls defmt::error! and then "panics"

by default, defmt::panic! will "panic" by calling core::panic!. This preserves the panicking behavior (e.g. abort on panic, reset on panic, etc.) but can result in the panic message being printed twice -- this is the case if you use panic_probe (+print-defmt)

to prevent printing the panic message twice, the panicking behavior of defmt::panic can be overridden using the #[defmt::panic_handler] attribute, which works about the same as #[core::panic_handler] but expects a fn() -> ! function (note: no argument)

the idea is that end users will replicate their #[panic_handler] panicking behavior, and omit the 'print the panic message' part, in the #[defmt::panic_handler] function, for example:

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    print(info); // e.g. using defmt
    reset()
}

#[defmt::panic_handler]
fn defmt_panic() -> ! {
    reset()
}

we can do the above for all new users of app-template so they don't run into the 'panic message printed twice':

// src/lib.rs
use panic_probe as _; // "print-defmt" is enabled

#[defmt::panic_handler]
fn panic() -> ! {
    cortex_m::asm::udf()
}

TODO

FIXME

  • probe-run renders assert_ne as a color-diff but shouldn't

Copy link
Copy Markdown
Contributor

@jonas-schievink jonas-schievink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

Comment thread book/src/panic.md Outdated
Comment thread macros/src/lib.rs

let block = &f.block;
quote!(
#[export_name = "_defmt_panic"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc #252

Co-authored-by: Jonas Schievink <jonas.schievink@ferrous-systems.com>
@japaric japaric merged commit e537f96 into main Nov 23, 2020
@japaric japaric deleted the panic branch November 23, 2020 13:59
@japaric japaric mentioned this pull request Nov 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

defmt::{panic, assert, assert_eq}

2 participants