|
17 | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 | */ |
19 | 19 |
|
20 | | -#if !TREZOR_EMULATOR || PYOPT |
| 20 | +#if PYOPT |
21 | 21 | #define MEMINFO_DICT_ENTRIES /* empty */ |
22 | 22 |
|
23 | 23 | #else |
|
38 | 38 | #include "embed/rust/librust.h" |
39 | 39 | #include "embed/upymod/trezorobj.h" |
40 | 40 |
|
| 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 | + |
41 | 49 | #define WORDS_PER_BLOCK ((MICROPY_BYTES_PER_GC_BLOCK) / MP_BYTES_PER_OBJ_WORD) |
42 | 50 | #define BYTES_PER_BLOCK (MICROPY_BYTES_PER_GC_BLOCK) |
43 | 51 |
|
@@ -199,7 +207,7 @@ void dump_short(FILE *out, mp_const_obj_t value) { |
199 | 207 |
|
200 | 208 | } else if (mp_obj_is_small_int(value)) { |
201 | 209 | 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)); |
203 | 211 | print_type(out, "smallint", num_buf, NULL, true); |
204 | 212 |
|
205 | 213 | } else if (!VERIFY_PTR(value)) { |
@@ -709,14 +717,14 @@ void dump_qstrdata(FILE *out) { |
709 | 717 | } |
710 | 718 | } |
711 | 719 |
|
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 | + } |
720 | 728 | fprintf(out, "["); |
721 | 729 |
|
722 | 730 | // void **ptrs = (void **)(void *)&mp_state_ctx; |
@@ -779,6 +787,19 @@ STATIC mp_obj_t mod_trezorutils_meminfo(mp_obj_t filename) { |
779 | 787 | } |
780 | 788 |
|
781 | 789 | 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); |
782 | 803 | return mp_const_none; |
783 | 804 | } |
784 | 805 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_meminfo_obj, |
|
0 commit comments