1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

feat(core/rust): introduce dbg_print and improve errors printing

[no changelog]
This commit is contained in:
cepetr 2024-06-11 13:37:10 +02:00 committed by cepetr
parent 5fc3c6e617
commit f5203011c5
4 changed files with 67 additions and 15 deletions

View File

@ -0,0 +1,33 @@
use ufmt;
#[cfg(feature = "micropython")]
use crate::micropython;
pub struct DebugConsole;
impl ufmt::uWrite for DebugConsole {
type Error = core::convert::Infallible;
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
#[cfg(feature = "micropython")]
micropython::print::print(s);
// TODO: add alternative if micropython is not available
Ok(())
}
}
#[macro_export]
macro_rules! dbg_println {
($($args:tt)*) => {
ufmt::uwriteln!($crate::debug::DebugConsole, $($args)*).ok();
};
}
#[macro_export]
macro_rules! dbg_print {
($($args:tt)*) => {
ufmt::uwrite!($crate::debug::DebugConsole, $($args)*).ok();
};
}

View File

@ -39,6 +39,10 @@ pub mod translations;
pub mod ui; pub mod ui;
pub mod strutil; pub mod strutil;
#[cfg(feature = "debug")]
#[macro_use]
pub mod debug;
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
#[cfg(not(test))] #[cfg(not(test))]
#[panic_handler] #[panic_handler]
@ -48,13 +52,8 @@ pub mod strutil;
fn panic_debug(panic_info: &core::panic::PanicInfo) -> ! { fn panic_debug(panic_info: &core::panic::PanicInfo) -> ! {
// Filling at least the file and line information, if available. // Filling at least the file and line information, if available.
// TODO: find out how to display message from panic_info.message() // TODO: find out how to display message from panic_info.message()
if let Some(location) = panic_info.location() { if let Some(location) = panic_info.location() {
let file = location.file(); trezorhal::fatal_error::__fatal_error("", "rs", location.file(), location.line(), "");
print!(file);
print!(":");
println!(inttostr!(location.line()));
trezorhal::fatal_error::__fatal_error("", "rs", file, location.line(), "");
} else { } else {
trezorhal::fatal_error::__fatal_error("", "rs", "", 0, ""); trezorhal::fatal_error::__fatal_error("", "rs", "", 0, "");
} }

View File

@ -10,6 +10,9 @@ use crate::ui::{
ui_features::{ModelUI, UIFeaturesCommon}, ui_features::{ModelUI, UIFeaturesCommon},
}; };
#[cfg(feature = "debug")]
use crate::dbg_println;
fn shutdown() -> ! { fn shutdown() -> ! {
unsafe { ffi::trezor_shutdown() } unsafe { ffi::trezor_shutdown() }
} }
@ -32,6 +35,23 @@ pub fn error_shutdown(title: &str, msg: &str, footer: &str) -> ! {
/// In debug mode, also prints the error message to the console. /// In debug mode, also prints the error message to the console.
#[inline(never)] // saves few kilobytes of flash #[inline(never)] // saves few kilobytes of flash
pub fn __fatal_error(_expr: &str, msg: &str, _file: &str, _line: u32, _func: &str) -> ! { pub fn __fatal_error(_expr: &str, msg: &str, _file: &str, _line: u32, _func: &str) -> ! {
#[cfg(feature = "debug")]
{
dbg_println!("=== FATAL ERROR");
if _line != 0 {
dbg_println!("Location: {}:{}", _file, _line);
}
if !_expr.is_empty() {
dbg_println!("Expression: {}", _expr);
}
if !msg.is_empty() {
dbg_println!("Message: {}", msg);
}
dbg_println!("===");
}
error_shutdown("INTERNAL_ERROR", msg, "PLEASE VISIT\nTREZOR.IO/RSOD"); error_shutdown("INTERNAL_ERROR", msg, "PLEASE VISIT\nTREZOR.IO/RSOD");
} }

View File

@ -66,15 +66,15 @@ where
}; };
} }
// This function enables nested invocations of `run_with_bumps()`, /// This function enables nested invocations of `run_with_bumps()`,
// which is necessary when the application needs to display a /// which is necessary when the application needs to display a
// fatal error message and subsequently terminate. /// fatal error message and subsequently terminate.
// ///
// SAFETY: /// # Safety
// This function must be invoked exclusively in failure scenarios /// This function must be invoked exclusively in failure scenarios
// where the application is required to display a fatal error /// where the application is required to display a fatal error
// message and then shut down. It is safe to call this function /// message and then shut down. It is safe to call this function
// only under these specific conditions. /// only under these specific conditions.
pub unsafe fn unlock_bumps_on_failure() { pub unsafe fn unlock_bumps_on_failure() {
// The application is single-threaded, so we can safely use a // The application is single-threaded, so we can safely use a
// static variable as a lock against nested calls. // static variable as a lock against nested calls.