From 9dc73427fbb196f0283b7046e9775577ac1244ad Mon Sep 17 00:00:00 2001 From: grdddj Date: Wed, 20 Jul 2022 11:18:37 +0200 Subject: [PATCH] feat(rust): add print! macro for logging into terminal --- core/Makefile | 2 +- core/SConscript.unix | 2 +- core/embed/rust/Cargo.toml | 1 + core/embed/rust/build.rs | 3 +++ core/embed/rust/src/micropython/macros.rs | 24 +++++++++++++++++++++++ core/embed/rust/src/micropython/mod.rs | 1 + core/embed/rust/src/micropython/print.rs | 16 +++++++++++++++ 7 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 core/embed/rust/src/micropython/print.rs diff --git a/core/Makefile b/core/Makefile index 6db288ea11..915aeebb61 100644 --- a/core/Makefile +++ b/core/Makefile @@ -158,7 +158,7 @@ build_firmware: templates build_cross ## build firmware with frozen modules $(SCONS) CFLAGS="$(CFLAGS)" PRODUCTION="$(PRODUCTION)" TREZOR_MODEL="$(TREZOR_MODEL)" PYOPT="$(PYOPT)" BITCOIN_ONLY="$(BITCOIN_ONLY)" UI2="$(UI2)" $(FIRMWARE_BUILD_DIR)/firmware.bin build_unix: templates ## build unix port - $(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) TREZOR_MODEL="$(TREZOR_MODEL)" BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" UI2="$(UI2)" + $(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) TREZOR_MODEL="$(TREZOR_MODEL)" PYOPT="0" BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" UI2="$(UI2)" build_unix_frozen: templates build_cross ## build unix port with frozen modules $(SCONS) CFLAGS="$(CFLAGS)" $(UNIX_BUILD_DIR)/trezor-emu-core $(UNIX_PORT_OPTS) TREZOR_MODEL="$(TREZOR_MODEL)" PYOPT="$(PYOPT)" BITCOIN_ONLY="$(BITCOIN_ONLY)" TREZOR_EMULATOR_ASAN="$(ADDRESS_SANITIZER)" UI2="$(UI2)" TREZOR_MEMPERF="$(TREZOR_MEMPERF)" TREZOR_EMULATOR_FROZEN=1 diff --git a/core/SConscript.unix b/core/SConscript.unix index c25358d8cf..ae42ec6722 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -671,7 +671,7 @@ def cargo_build(): if UI2: features.append('ui') if PYOPT == '0': - features.append('ui_debug') + features.append('debug') return f'cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}"' diff --git a/core/embed/rust/Cargo.toml b/core/embed/rust/Cargo.toml index fe0295ad3f..f7e1318d58 100644 --- a/core/embed/rust/Cargo.toml +++ b/core/embed/rust/Cargo.toml @@ -18,6 +18,7 @@ ui_debug = [] buttons = [] touch = [] clippy = [] +debug = ["ui_debug"] test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug"] [lib] diff --git a/core/embed/rust/build.rs b/core/embed/rust/build.rs index 0bf727fe51..ba551c86f3 100644 --- a/core/embed/rust/build.rs +++ b/core/embed/rust/build.rs @@ -196,6 +196,9 @@ fn generate_micropython_bindings() { // time .allowlist_function("mp_hal_ticks_ms") .allowlist_function("mp_hal_delay_ms") + // debug + .allowlist_function("mp_print_strn") + .allowlist_var("mp_plat_print") // typ .allowlist_var("mp_type_type") // module diff --git a/core/embed/rust/src/micropython/macros.rs b/core/embed/rust/src/micropython/macros.rs index 498fcbcd8b..557666c8e0 100644 --- a/core/embed/rust/src/micropython/macros.rs +++ b/core/embed/rust/src/micropython/macros.rs @@ -204,3 +204,27 @@ macro_rules! obj_module { obj_module!($($key => $val),*) }); } + +/// Print arbitrary amounts of slices into a terminal. +/// Does not include a newline at the end. +/// Does not do anything when not in debugging mode. +#[allow(unused_macros)] // Should be used only for debugging purposes +macro_rules! print { + ($($string:expr),+) => { + #[cfg(feature = "debug")] + { + $(crate::micropython::print::print($string);)+ + } + } +} + +/// Print arbitrary amounts of slices into a terminal. +/// Includes a newline at the end. +/// Does not do anything when not in debugging mode. +#[allow(unused_macros)] // Should be used only for debugging purposes +macro_rules! println { + ($($string:expr),+) => { + // Just delegating to print! and adding a newline + print!($($string),+, "\n"); + } +} diff --git a/core/embed/rust/src/micropython/mod.rs b/core/embed/rust/src/micropython/mod.rs index 68659e8004..0ebe3afae6 100644 --- a/core/embed/rust/src/micropython/mod.rs +++ b/core/embed/rust/src/micropython/mod.rs @@ -12,6 +12,7 @@ pub mod list; pub mod map; pub mod module; pub mod obj; +pub mod print; pub mod qstr; pub mod runtime; pub mod time; diff --git a/core/embed/rust/src/micropython/print.rs b/core/embed/rust/src/micropython/print.rs new file mode 100644 index 0000000000..b8f47673d1 --- /dev/null +++ b/core/embed/rust/src/micropython/print.rs @@ -0,0 +1,16 @@ +use super::ffi; + +/// Print a slice into emulator console. +/// Is being '\0' terminated automatically. +pub fn print(to_log: &str) { + unsafe { + ffi::mp_print_strn( + &ffi::mp_plat_print, + to_log.as_ptr() as _, + to_log.len(), + 0, + '\0' as _, + 0, + ); + } +}