Skip to content

Commit

Permalink
Add panic handler using semihosting to vm app
Browse files Browse the repository at this point in the history
  • Loading branch information
bigspider committed Sep 18, 2024
1 parent 2ef8083 commit a7b1bf9
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions vm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#![no_std]
#![no_main]
#![feature(panic_info_message)]

mod app_ui;
mod handlers;
Expand All @@ -34,8 +35,6 @@ use ledger_device_sdk::io::{ApduHeader, Comm, Event, Reply, StatusWords};
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_device_sdk::ui::gadgets::display_pending_review;

ledger_device_sdk::set_panic!(ledger_device_sdk::exiting_panic);

// Required for using String, Vec, format!...
extern crate alloc;

Expand All @@ -61,6 +60,31 @@ macro_rules! println {
});
}

// Print panic message to the console. Uses the `print!` macro defined above,
// therefore it only works when running on Speculos.
fn handle_panic(info: &core::panic::PanicInfo) -> ! {
let message = if let Some(location) = info.location() {
alloc::format!(
"Panic occurred in file '{}' at line {}: {:?}",
location.file(),
location.line(),
info.message().unwrap_or(&format_args!("no message"))
)
} else {
alloc::format!(
"Panic occurred: {}",
info.message().unwrap_or(&format_args!("no message"))
)
};
println!("{}", message);

let mut comm = ledger_device_sdk::io::Comm::new();
comm.reply(ledger_device_sdk::io::StatusWords::Panic);
ledger_device_sdk::exit_app(0x01);
}

ledger_device_sdk::set_panic!(handle_panic);

// Application status words.
#[repr(u16)]
pub enum AppSW {
Expand Down

0 comments on commit a7b1bf9

Please sign in to comment.