Skip to content

Commit

Permalink
Fix display of panic message
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dcoles committed Jan 26, 2025
1 parent c43bc7e commit 8949cf0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 5 additions & 9 deletions crates/rt/src/panic_handler.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
}
Expand Down

0 comments on commit 8949cf0

Please sign in to comment.