Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add defmt::panic! #266

Merged
merged 20 commits into from
Nov 23, 2020
Merged

add defmt::panic! #266

merged 20 commits into from
Nov 23, 2020

Conversation

japaric
Copy link
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

  • API docs
  • book docs
  • todo! & unimplemented!
  • unreachable!
  • assert!
  • assert_eq! assert_ne!
  • debug_assert, debug_assert_eq, debug_assert_ne
  • snapshot tests
  • assert_eq! & probe-run integration (colored diff) -> https:/knurling-rs/probe-run/tree/color-diff
  • prelude didn't work you can't override core's prelude items using a glob import

FIXME

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

Copy link
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!

book/src/panic.md Outdated Show resolved Hide resolved

let block = &f.block;
quote!(
#[export_name = "_defmt_panic"]
Copy link
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 <[email protected]>
@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