From 8949cf068816a4111866c84840883ae37fd473e6 Mon Sep 17 00:00:00 2001 From: David Coles Date: Fri, 24 Jan 2025 00:21:52 -0800 Subject: [PATCH] Fix display of panic message When connected via USB serial, there is often not enough time for the output to be fully recieved before the USB device resets. Add a short 500 ms delay before we call `furi_crash`. This also fixes incorrect `printf` formatting strings with explicit length (the length is provided as the 'precision' value after the decimal rather than the 'width' value before the decimal). --- CHANGELOG.md | 2 ++ crates/rt/src/panic_handler.rs | 14 +++++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9c49464..6c19da56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - Fixed passing of crash message `__furi_crash_implementation` - Allow no argument calls to `flipperzero_sys::crash!` and `flipperzero_sys::halt!` macros - `flipperzero_sys::furi::UnsafeRecord::open` now takes a `&'static CStr` +- Fixed overread when printing panic message to console +- Added short delay in panic handler so message can be seen on USB console before `furi_crash` ### Removed diff --git a/crates/rt/src/panic_handler.rs b/crates/rt/src/panic_handler.rs index eeb4f9cd..38952328 100644 --- a/crates/rt/src/panic_handler.rs +++ b/crates/rt/src/panic_handler.rs @@ -1,7 +1,6 @@ //! Panic handler for Furi applications. //! This will print the panic info to stdout and then trigger a crash. -use core::ffi::c_char; use core::panic::PanicInfo; use flipperzero_sys as sys; @@ -21,24 +20,21 @@ pub fn panic(panic_info: &PanicInfo<'_>) -> ! { sys::__wrap_printf(c"\x1b[0;31mthread: '%s' paniced".as_ptr(), thread_name); if let Some(s) = panic_info.message().as_str() { - sys::__wrap_printf(c" at '%*s'".as_ptr(), s.len(), s.as_ptr() as *const c_char); + sys::__wrap_printf(c" at '%.*s'".as_ptr(), s.len(), s.as_ptr()); } if let Some(location) = panic_info.location() { let file = location.file(); let line = location.line(); - sys::__wrap_printf( - c", %*s:%u".as_ptr(), - file.len(), - file.as_ptr() as *const c_char, - line, - ); + sys::__wrap_printf(c", %.*s:%u".as_ptr(), file.len(), file.as_ptr(), line); } sys::__wrap_printf(c"\x1b[0m\r\n".as_ptr()); sys::furi_thread_stdout_flush(); - sys::furi_thread_yield(); // Allow console to flush + + // Ensure there's plenty of time to fully flush the console + sys::furi_delay_ms(500); sys::crash!("Rust panic") }