Skip to content

Commit 0bc6093

Browse files
committed
feat(core): dump GC arena on OOM
Enabled for debug firmware and non-frozen emulator. [no changelog]
1 parent f46221b commit 0bc6093

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

core/SConscript.firmware

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ ui.init_ui(TREZOR_MODEL, "firmware", RUST_UI_FEATURES)
396396
SOURCE_QSTR = SOURCE_MOD + SOURCE_MICROPYTHON + SOURCE_MICROPYTHON_SPEED
397397

398398
if PYOPT == '0':
399-
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=1"
399+
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=1 -DSTATIC="
400400
else:
401401
DEBUG_FLAGS = "-DMICROPY_OOM_CALLBACK=0"
402402

core/embed/rust/librust.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "librust_qstr.h"
44

5-
#ifdef TREZOR_EMULATOR
5+
#if !PYOPT
66
mp_obj_t protobuf_debug_msg_type();
77
mp_obj_t protobuf_debug_msg_def_type();
88
#endif
@@ -11,6 +11,6 @@ extern mp_obj_module_t mp_module_trezorproto;
1111
extern mp_obj_module_t mp_module_trezorui_api;
1212
extern mp_obj_module_t mp_module_trezortranslate;
1313

14-
#ifdef TREZOR_EMULATOR
14+
#if !PYOPT
1515
mp_obj_t ui_debug_layout_type();
1616
#endif

core/embed/upymod/modtrezorutils/modtrezorutils-meminfo.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
#if !TREZOR_EMULATOR || PYOPT
20+
#if PYOPT
2121
#define MEMINFO_DICT_ENTRIES /* empty */
2222

2323
#else
@@ -38,6 +38,14 @@
3838
#include "embed/rust/librust.h"
3939
#include "embed/upymod/trezorobj.h"
4040

41+
#if !TREZOR_EMULATOR
42+
#define fopen(path, mode) &mp_plat_print
43+
#define fprintf mp_printf
44+
#define fflush(f)
45+
#define fclose(f)
46+
#define FILE const mp_print_t
47+
#endif
48+
4149
#define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / MP_BYTES_PER_OBJ_WORD)
4250
#define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK)
4351

@@ -199,7 +207,7 @@ void dump_short(FILE *out, mp_const_obj_t value) {
199207

200208
} else if (mp_obj_is_small_int(value)) {
201209
static char num_buf[100];
202-
snprintf(num_buf, 100, "%ld", MP_OBJ_SMALL_INT_VALUE(value));
210+
snprintf(num_buf, 100, INT_FMT, MP_OBJ_SMALL_INT_VALUE(value));
203211
print_type(out, "smallint", num_buf, NULL, true);
204212

205213
} else if (!VERIFY_PTR(value)) {
@@ -709,14 +717,14 @@ void dump_qstrdata(FILE *out) {
709717
}
710718
}
711719

712-
/// def meminfo(filename: str) -> None:
713-
/// """Dumps map of micropython GC arena to a file.
714-
/// The JSON file can be decoded by analyze-memory-dump.py
715-
/// Only available in the emulator.
716-
/// """
717-
STATIC mp_obj_t mod_trezorutils_meminfo(mp_obj_t filename) {
718-
size_t fn_len;
719-
FILE *out = fopen(mp_obj_str_get_data(filename, &fn_len), "w");
720+
static void dump_meminfo_json(FILE *out) {
721+
if (out == NULL) {
722+
#if TREZOR_EMULATOR
723+
out = stdout;
724+
#else
725+
out = &mp_plat_print;
726+
#endif
727+
}
720728
fprintf(out, "[");
721729

722730
// void **ptrs = (void **)(void *)&mp_state_ctx;
@@ -779,6 +787,19 @@ STATIC mp_obj_t mod_trezorutils_meminfo(mp_obj_t filename) {
779787
}
780788

781789
gc_dump_alloc_table();
790+
}
791+
792+
/// def meminfo(filename: str | None) -> None:
793+
/// """Dumps map of micropython GC arena to a file.
794+
/// The JSON file can be decoded by analyze-memory-dump.py
795+
/// """
796+
STATIC mp_obj_t mod_trezorutils_meminfo(mp_obj_t filename) {
797+
size_t fn_len;
798+
FILE *out = (filename == mp_const_none)
799+
? NULL
800+
: fopen(mp_obj_str_get_data(filename, &fn_len), "w");
801+
(void)fn_len;
802+
dump_meminfo_json(out);
782803
return mp_const_none;
783804
}
784805
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_meminfo_obj,

core/embed/upymod/modtrezorutils/modtrezorutils.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_estimate_unused_stack_obj,
284284
#if MICROPY_OOM_CALLBACK
285285
static void gc_oom_callback(void) {
286286
gc_dump_info();
287+
#if BLOCK_ON_VCP
288+
dump_meminfo_json(NULL); // dump to stdout
289+
#endif
287290
}
288291

289292
/// if __debug__:

0 commit comments

Comments
 (0)