Skip to content

Commit 2333a6a

Browse files
committed
feat(core): log GC info in case of OOM
Enabled on debug firmware and non-frozen emulator. [no changelog]
1 parent 2e72a8c commit 2333a6a

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

core/SConscript.firmware

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,17 @@ ui.init_ui(TREZOR_MODEL, "firmware", RUST_UI_FEATURES)
395395

396396
SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_MICROPYTHON_SPEED
397397

398+
if PYOPT == '0':
399+
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=1"
400+
else:
401+
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=0"
402+
398403
env = Environment(
399404
ENV=os.environ,
400-
CFLAGS=f"{ARGUMENTS.get('CFLAGS', '')} -DPRODUCTION={int(PRODUCTION)} -DPYOPT={PYOPT} -DBOOTLOADER_QA={int(BOOTLOADER_QA)} -DBITCOIN_ONLY={BITCOIN_ONLY}",
405+
CFLAGS=(
406+
f"{ARGUMENTS.get('CFLAGS', '')} -DPRODUCTION={int(PRODUCTION)} -DPYOPT={PYOPT} "
407+
f"-DBOOTLOADER_QA={int(BOOTLOADER_QA)} -DBITCOIN_ONLY={BITCOIN_ONLY} {DEBUG_FLAGS}"
408+
),
401409
CPPDEFINES_IMPLICIT=[],
402410
CPPDEFPREFIX="-D'",
403411
CPPDEFSUFFIX="'",

core/SConscript.unix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,13 @@ ui.init_ui(TREZOR_MODEL, "firmware", RUST_UI_FEATURES)
422422
SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_UNIX
423423

424424
if PYOPT == '0' or not FROZEN:
425-
STATIC="-DSTATIC="
425+
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=1 -DSTATIC="
426426
else:
427-
STATIC=""
427+
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=0"
428428

429429
env = Environment(
430430
ENV=os.environ,
431-
CFLAGS=ARGUMENTS.get("CFLAGS", "") + f" -DCONFIDENTIAL= -DPYOPT={PYOPT} -DBITCOIN_ONLY={BITCOIN_ONLY} {STATIC}",
431+
CFLAGS=ARGUMENTS.get("CFLAGS", "") + f" -DCONFIDENTIAL= -DPYOPT={PYOPT} -DBITCOIN_ONLY={BITCOIN_ONLY} {DEBUG_FLAGS}",
432432
CPPDEFPREFIX="-D'",
433433
CPPDEFSUFFIX="'",
434434
)

core/embed/upymod/modtrezorutils/modtrezorutils.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <trezor_model.h>
2121
#include <trezor_rtl.h>
2222

23+
#if MICROPY_OOM_CALLBACK
24+
#include <py/gc.h>
25+
#endif
2326
#include "py/objstr.h"
2427
#include "py/runtime.h"
2528

@@ -243,7 +246,8 @@ STATIC mp_obj_t mod_trezorutils_sd_hotswap_enabled(void) {
243246
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_sd_hotswap_enabled_obj,
244247
mod_trezorutils_sd_hotswap_enabled);
245248

246-
#if !PYOPT && LOG_STACK_USAGE
249+
#if !PYOPT
250+
#if LOG_STACK_USAGE
247251
/// def zero_unused_stack() -> None:
248252
/// """
249253
/// Zero unused stack memory.
@@ -275,7 +279,27 @@ STATIC mp_obj_t mod_trezorutils_estimate_unused_stack(void) {
275279
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_estimate_unused_stack_obj,
276280
mod_trezorutils_estimate_unused_stack);
277281

278-
#endif // !PYOPT && LOG_STACK_USAGE
282+
#endif // LOG_STACK_USAGE
283+
284+
#if MICROPY_OOM_CALLBACK
285+
static void gc_oom_callback(void) {
286+
gc_dump_info();
287+
}
288+
289+
/// if __debug__:
290+
/// def enable_oom_dump() -> None:
291+
/// """
292+
/// Dump GC info in case of an OOM.
293+
/// """
294+
STATIC mp_obj_t mod_trezorutils_enable_oom_dump(void) {
295+
gc_set_oom_callback(gc_oom_callback);
296+
return mp_const_none;
297+
}
298+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_enable_oom_dump_obj,
299+
mod_trezorutils_enable_oom_dump);
300+
#endif // MICROPY_OOM_CALLBACK
301+
302+
#endif // !PYOPT
279303

280304
/// def reboot_to_bootloader(
281305
/// boot_command : int = 0,
@@ -482,11 +506,17 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
482506
MP_ROM_PTR(&mod_trezorutils_unit_packaging_obj)},
483507
{MP_ROM_QSTR(MP_QSTR_unit_btconly),
484508
MP_ROM_PTR(&mod_trezorutils_unit_btconly_obj)},
485-
#if !PYOPT && LOG_STACK_USAGE
509+
#if !PYOPT
510+
#if LOG_STACK_USAGE
486511
{MP_ROM_QSTR(MP_QSTR_zero_unused_stack),
487512
MP_ROM_PTR(&mod_trezorutils_zero_unused_stack_obj)},
488513
{MP_ROM_QSTR(MP_QSTR_estimate_unused_stack),
489514
MP_ROM_PTR(&mod_trezorutils_estimate_unused_stack_obj)},
515+
#endif
516+
#if MICROPY_OOM_CALLBACK
517+
{MP_ROM_QSTR(MP_QSTR_enable_oom_dump),
518+
MP_ROM_PTR(&mod_trezorutils_enable_oom_dump_obj)},
519+
#endif
490520
#endif
491521
{MP_ROM_QSTR(MP_QSTR_sd_hotswap_enabled),
492522
MP_ROM_PTR(&mod_trezorutils_sd_hotswap_enabled_obj)},

core/mocks/generated/trezorutils.pyi

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ def estimate_unused_stack() -> int:
100100
"""
101101
Estimate unused stack size.
102102
"""
103+
if __debug__:
104+
def enable_oom_dump() -> None:
105+
"""
106+
Dump GC info in case of an OOM.
107+
"""
103108

104109

105110
# upymod/modtrezorutils/modtrezorutils.c

core/src/trezor/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def presize_module(modname: str, size: int) -> None:
125125
if __debug__:
126126
from ubinascii import hexlify
127127

128+
try:
129+
from trezorutils import enable_oom_dump
130+
131+
enable_oom_dump()
132+
except ImportError:
133+
pass
134+
128135
def mem_dump(filename: str) -> None:
129136
from micropython import mem_info
130137

vendor/micropython

0 commit comments

Comments
 (0)