From ab5d43f0d9201750842eed4e2af88e0136932ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Fri, 28 Jun 2024 22:28:55 +0200 Subject: [PATCH 1/4] Add an impl Format for PanicInfo --- CHANGELOG.md | 2 ++ defmt/src/impls/core_/mod.rs | 1 + defmt/src/impls/core_/panic.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 defmt/src/impls/core_/panic.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 2888ede91..cb107ea90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - [#845]: `decoder`: fix println!() records being printed with formatting - [#843]: `defmt`: Sort IDs of log msgs by severity to allow runtime filtering by severity - [#822]: `CI`: Run `cargo semver-checks` on every PR +- [#856]: `defmt`: Add a `Format` impl for `PanicInfo` and related types. [#859]: https://github.com/knurling-rs/defmt/pull/859 [#858]: https://github.com/knurling-rs/defmt/pull/858 @@ -28,6 +29,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). [#845]: https://github.com/knurling-rs/defmt/pull/845 [#843]: https://github.com/knurling-rs/defmt/pull/843 [#822]: https://github.com/knurling-rs/defmt/pull/822 +[#856]: https://github.com/knurling-rs/defmt/pull/856 ## [v0.3.8] - 2024-05-17 diff --git a/defmt/src/impls/core_/mod.rs b/defmt/src/impls/core_/mod.rs index c9b3c7706..b005ca577 100644 --- a/defmt/src/impls/core_/mod.rs +++ b/defmt/src/impls/core_/mod.rs @@ -12,6 +12,7 @@ mod cell; mod net; mod num; mod ops; +mod panic; mod ptr; mod slice; diff --git a/defmt/src/impls/core_/panic.rs b/defmt/src/impls/core_/panic.rs new file mode 100644 index 000000000..885c3106c --- /dev/null +++ b/defmt/src/impls/core_/panic.rs @@ -0,0 +1,28 @@ +use core::panic; + +use super::*; + +impl<'a> Format for panic::PanicInfo<'a> { + fn format(&self, f: Formatter) { + crate::write!(f, "panicked at {}:", self.location()); + // TODO: consider supporting self.message() once stabilized, or add a crate feature for + // conditional support + + #[allow(deprecated)] // Deprecated on nightly + if let Some(msg) = self.payload().downcast_ref::<&'static str>() { + crate::write!(f, "\n{=str}", msg); + } + } +} + +impl<'a> Format for panic::Location<'a> { + fn format(&self, f: Formatter) { + crate::write!( + f, + "{=str}:{=u32}:{=u32}", + self.file(), + self.line(), + self.column() + ); + } +} From 75099f96c838f85346f63fa00c165692aedc646b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Tue, 30 Jul 2024 12:29:06 +0200 Subject: [PATCH 2/4] Remove faulty downcast of PanicInfo::payload --- defmt/src/impls/core_/panic.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/defmt/src/impls/core_/panic.rs b/defmt/src/impls/core_/panic.rs index 885c3106c..7912582c2 100644 --- a/defmt/src/impls/core_/panic.rs +++ b/defmt/src/impls/core_/panic.rs @@ -4,14 +4,9 @@ use super::*; impl<'a> Format for panic::PanicInfo<'a> { fn format(&self, f: Formatter) { - crate::write!(f, "panicked at {}:", self.location()); + crate::write!(f, "panicked at {}", self.location()); // TODO: consider supporting self.message() once stabilized, or add a crate feature for // conditional support - - #[allow(deprecated)] // Deprecated on nightly - if let Some(msg) = self.payload().downcast_ref::<&'static str>() { - crate::write!(f, "\n{=str}", msg); - } } } From 447ac116080a23c26e2d42a5648649cca1080b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Tue, 30 Jul 2024 16:48:03 +0200 Subject: [PATCH 3/4] Omit panic location if None is returned from PanicInfo::location --- defmt/src/impls/core_/panic.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/defmt/src/impls/core_/panic.rs b/defmt/src/impls/core_/panic.rs index 7912582c2..78a56d74f 100644 --- a/defmt/src/impls/core_/panic.rs +++ b/defmt/src/impls/core_/panic.rs @@ -4,7 +4,11 @@ use super::*; impl<'a> Format for panic::PanicInfo<'a> { fn format(&self, f: Formatter) { - crate::write!(f, "panicked at {}", self.location()); + if let Some(location) = self.location() { + crate::write!(f, "panicked at {}", location); + } else { + crate::write!(f, "panicked"); + } // TODO: consider supporting self.message() once stabilized, or add a crate feature for // conditional support } From b90793e23c5070c9c99bb062765791dcac5bc0b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Flemstr=C3=B6m?= Date: Tue, 30 Jul 2024 16:48:27 +0200 Subject: [PATCH 4/4] Add snapshot test for formatting PanicInfo --- firmware/qemu/src/bin/panic_info.out | 1 + firmware/qemu/src/bin/panic_info.rs | 23 +++++++++++++++++++++++ xtask/src/snapshot.rs | 1 + 3 files changed, 25 insertions(+) create mode 100644 firmware/qemu/src/bin/panic_info.out create mode 100644 firmware/qemu/src/bin/panic_info.rs diff --git a/firmware/qemu/src/bin/panic_info.out b/firmware/qemu/src/bin/panic_info.out new file mode 100644 index 000000000..4f76222f7 --- /dev/null +++ b/firmware/qemu/src/bin/panic_info.out @@ -0,0 +1 @@ +INFO PanicInfo: panicked at qemu/src/bin/panic_info.rs:14:5 diff --git a/firmware/qemu/src/bin/panic_info.rs b/firmware/qemu/src/bin/panic_info.rs new file mode 100644 index 000000000..f260cd998 --- /dev/null +++ b/firmware/qemu/src/bin/panic_info.rs @@ -0,0 +1,23 @@ +#![no_std] +#![no_main] + +use core::panic; +use cortex_m_semihosting::debug; + +use defmt_semihosting as _; // global logger + +#[cortex_m_rt::entry] +fn main() -> ! { + // Note: this test is a bit brittle in that the line/column number of the following panic is + // included in the test snapshot. Hence, be mindful to update the snapshot if you want to + // add any additional code to this file above the following line! + panic!("aaah!") +} + +#[panic_handler] +fn panic(panic_info: &panic::PanicInfo) -> ! { + defmt::info!("PanicInfo: {=?}", panic_info); + loop { + debug::exit(debug::EXIT_SUCCESS) + } +} diff --git a/xtask/src/snapshot.rs b/xtask/src/snapshot.rs index f19e41810..bfcf950d3 100644 --- a/xtask/src/snapshot.rs +++ b/xtask/src/snapshot.rs @@ -26,6 +26,7 @@ pub(crate) fn all_snapshot_tests() -> Vec<&'static str> { "hints_inner", "dbg", "net", + "panic_info", ]; const NIGHTLY_SNAPSHOT_TESTS: &[&str] = &["alloc"];