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

feat(rust): add print! macro for logging into terminal

This commit is contained in:
grdddj 2022-07-20 11:18:37 +02:00 committed by Jiří Musil
parent 8bfbe9da01
commit 9dc73427fb
7 changed files with 47 additions and 2 deletions

View File

@ -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 $(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 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 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 $(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

View File

@ -671,7 +671,7 @@ def cargo_build():
if UI2: if UI2:
features.append('ui') features.append('ui')
if PYOPT == '0': 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)}"' return f'cd embed/rust; cargo build --profile {RUST_PROFILE} --target-dir=../../build/unix/rust --no-default-features --features "{" ".join(features)}"'

View File

@ -18,6 +18,7 @@ ui_debug = []
buttons = [] buttons = []
touch = [] touch = []
clippy = [] clippy = []
debug = ["ui_debug"]
test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug"] test = ["cc", "glob", "micropython", "protobuf", "ui", "ui_debug"]
[lib] [lib]

View File

@ -196,6 +196,9 @@ fn generate_micropython_bindings() {
// time // time
.allowlist_function("mp_hal_ticks_ms") .allowlist_function("mp_hal_ticks_ms")
.allowlist_function("mp_hal_delay_ms") .allowlist_function("mp_hal_delay_ms")
// debug
.allowlist_function("mp_print_strn")
.allowlist_var("mp_plat_print")
// typ // typ
.allowlist_var("mp_type_type") .allowlist_var("mp_type_type")
// module // module

View File

@ -204,3 +204,27 @@ macro_rules! obj_module {
obj_module!($($key => $val),*) 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");
}
}

View File

@ -12,6 +12,7 @@ pub mod list;
pub mod map; pub mod map;
pub mod module; pub mod module;
pub mod obj; pub mod obj;
pub mod print;
pub mod qstr; pub mod qstr;
pub mod runtime; pub mod runtime;
pub mod time; pub mod time;

View File

@ -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,
);
}
}