Skip to content

Commit 2d15c12

Browse files
committed
stm: Add optional memory debugging output.
1 parent d0691cc commit 2d15c12

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

py/malloc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#include "misc.h"
55
#include "mpconfig.h"
66

7+
#if 0 // print debugging info
8+
#define DEBUG_printf(args...) printf(args)
9+
#else // don't print debugging info
10+
#define DEBUG_printf(args...) (void)0
11+
#endif
12+
713
#if MICROPY_MEM_STATS
814
static int total_bytes_allocated = 0;
915
static int current_bytes_allocated = 0;
@@ -26,6 +32,7 @@ void *m_malloc(int num_bytes) {
2632
current_bytes_allocated += num_bytes;
2733
UPDATE_PEAK();
2834
#endif
35+
DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
2936
return ptr;
3037
}
3138

@@ -43,6 +50,7 @@ void *m_malloc0(int num_bytes) {
4350
current_bytes_allocated += num_bytes;
4451
UPDATE_PEAK();
4552
#endif
53+
DEBUG_printf("malloc0 %d : %p\n", num_bytes, ptr);
4654
return ptr;
4755
}
4856

@@ -67,6 +75,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
6775
current_bytes_allocated += diff;
6876
UPDATE_PEAK();
6977
#endif
78+
DEBUG_printf("realloc %d, %d : %p\n", old_num_bytes, new_num_bytes, ptr);
7079
return ptr;
7180
}
7281

@@ -77,6 +86,7 @@ void m_free(void *ptr, int num_bytes) {
7786
#if MICROPY_MEM_STATS
7887
current_bytes_allocated -= num_bytes;
7988
#endif
89+
DEBUG_printf("free %p, %d\n", ptr, num_bytes);
8090
}
8191

8292
int m_get_total_bytes_allocated(void) {

stm/main.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ static const char *help_text =
129129
"Specific commands for the board:\n"
130130
" pyb.info() -- print some general information\n"
131131
" pyb.gc() -- run the garbage collector\n"
132+
" pyb.repl_info(<val>) -- enable/disable printing of info after each command\n"
132133
" pyb.delay(<n>) -- wait for n milliseconds\n"
133134
" pyb.Led(<n>) -- create Led object for LED n (n=1,2)\n"
134135
" Led methods: on(), off()\n"
@@ -215,6 +216,13 @@ static mp_obj_t pyb_info(void) {
215216
return mp_const_none;
216217
}
217218

219+
static bool repl_display_debugging_info = 0;
220+
221+
static mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
222+
repl_display_debugging_info = mp_obj_get_int(o_value);
223+
return mp_const_none;
224+
}
225+
218226
#if MICROPY_HW_HAS_SDCARD
219227
// SD card test
220228
static mp_obj_t pyb_sd_test(void) {
@@ -429,15 +437,18 @@ void do_repl(void) {
429437
if (nlr_push(&nlr) == 0) {
430438
rt_call_function_0(module_fun);
431439
nlr_pop();
432-
// optional timing
433-
if (0) {
434-
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
435-
printf("(took %lu ms)\n", ticks);
436-
}
437440
} else {
438441
// uncaught exception
439442
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
440443
}
444+
445+
// display debugging info if wanted
446+
if (repl_display_debugging_info) {
447+
uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
448+
printf("took %lu ms\n", ticks);
449+
gc_collect();
450+
pyb_info();
451+
}
441452
}
442453
}
443454
}
@@ -643,6 +654,8 @@ int main(void) {
643654

644655
mp_obj_t m = mp_obj_new_module(MP_QSTR_pyb);
645656
rt_store_attr(m, MP_QSTR_info, rt_make_function_n(0, pyb_info));
657+
rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj);
658+
rt_store_attr(m, qstr_from_str("repl_info"), rt_make_function_n(1, pyb_set_repl_info));
646659
#if MICROPY_HW_HAS_SDCARD
647660
rt_store_attr(m, MP_QSTR_sd_test, rt_make_function_n(0, pyb_sd_test));
648661
#endif
@@ -651,7 +664,6 @@ int main(void) {
651664
rt_store_attr(m, MP_QSTR_source_dir, rt_make_function_n(1, pyb_source_dir));
652665
rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main));
653666
rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync));
654-
rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj);
655667
rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay));
656668
#if MICROPY_HW_HAS_SWITCH
657669
rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj);

0 commit comments

Comments
 (0)