From 2d15c1216ddad8ee603106fd86ad36baf2d40ff8 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Wed, 29 Jan 2014 20:33:20 +0000
Subject: [PATCH] stm: Add optional memory debugging output.

---
 py/malloc.c | 10 ++++++++++
 stm/main.c  | 24 ++++++++++++++++++------
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/py/malloc.c b/py/malloc.c
index 4f01dc63f..a2c55eb06 100644
--- a/py/malloc.c
+++ b/py/malloc.c
@@ -4,6 +4,12 @@
 #include "misc.h"
 #include "mpconfig.h"
 
+#if 0 // print debugging info
+#define DEBUG_printf(args...) printf(args)
+#else // don't print debugging info
+#define DEBUG_printf(args...) (void)0
+#endif
+
 #if MICROPY_MEM_STATS
 static int total_bytes_allocated = 0;
 static int current_bytes_allocated = 0;
@@ -26,6 +32,7 @@ void *m_malloc(int num_bytes) {
     current_bytes_allocated += num_bytes;
     UPDATE_PEAK();
 #endif
+    DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
     return ptr;
 }
 
@@ -43,6 +50,7 @@ void *m_malloc0(int num_bytes) {
     current_bytes_allocated += num_bytes;
     UPDATE_PEAK();
 #endif
+    DEBUG_printf("malloc0 %d : %p\n", num_bytes, ptr);
     return ptr;
 }
 
@@ -67,6 +75,7 @@ void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) {
     current_bytes_allocated += diff;
     UPDATE_PEAK();
 #endif
+    DEBUG_printf("realloc %d, %d : %p\n", old_num_bytes, new_num_bytes, ptr);
     return ptr;
 }
 
@@ -77,6 +86,7 @@ void m_free(void *ptr, int num_bytes) {
 #if MICROPY_MEM_STATS
     current_bytes_allocated -= num_bytes;
 #endif
+    DEBUG_printf("free %p, %d\n", ptr, num_bytes);
 }
 
 int m_get_total_bytes_allocated(void) {
diff --git a/stm/main.c b/stm/main.c
index 5c2d3cf17..313a868c4 100644
--- a/stm/main.c
+++ b/stm/main.c
@@ -129,6 +129,7 @@ static const char *help_text =
 "Specific commands for the board:\n"
 "    pyb.info()     -- print some general information\n"
 "    pyb.gc()       -- run the garbage collector\n"
+"    pyb.repl_info(<val>) -- enable/disable printing of info after each command\n"
 "    pyb.delay(<n>) -- wait for n milliseconds\n"
 "    pyb.Led(<n>)   -- create Led object for LED n (n=1,2)\n"
 "                      Led methods: on(), off()\n"
@@ -215,6 +216,13 @@ static mp_obj_t pyb_info(void) {
     return mp_const_none;
 }
 
+static bool repl_display_debugging_info = 0;
+
+static mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
+    repl_display_debugging_info = mp_obj_get_int(o_value);
+    return mp_const_none;
+}
+
 #if MICROPY_HW_HAS_SDCARD
 // SD card test
 static mp_obj_t pyb_sd_test(void) {
@@ -429,15 +437,18 @@ void do_repl(void) {
                 if (nlr_push(&nlr) == 0) {
                     rt_call_function_0(module_fun);
                     nlr_pop();
-                    // optional timing
-                    if (0) {
-                        uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
-                        printf("(took %lu ms)\n", ticks);
-                    }
                 } else {
                     // uncaught exception
                     mp_obj_print_exception((mp_obj_t)nlr.ret_val);
                 }
+
+                // display debugging info if wanted
+                if (repl_display_debugging_info) {
+                    uint32_t ticks = sys_tick_counter - start; // TODO implement a function that does this properly
+                    printf("took %lu ms\n", ticks);
+                    gc_collect();
+                    pyb_info();
+                }
             }
         }
     }
@@ -643,6 +654,8 @@ soft_reset:
 
         mp_obj_t m = mp_obj_new_module(MP_QSTR_pyb);
         rt_store_attr(m, MP_QSTR_info, rt_make_function_n(0, pyb_info));
+        rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj);
+        rt_store_attr(m, qstr_from_str("repl_info"), rt_make_function_n(1, pyb_set_repl_info));
 #if MICROPY_HW_HAS_SDCARD
         rt_store_attr(m, MP_QSTR_sd_test, rt_make_function_n(0, pyb_sd_test));
 #endif
@@ -651,7 +664,6 @@ soft_reset:
         rt_store_attr(m, MP_QSTR_source_dir, rt_make_function_n(1, pyb_source_dir));
         rt_store_attr(m, MP_QSTR_main, rt_make_function_n(1, pyb_main));
         rt_store_attr(m, MP_QSTR_sync, rt_make_function_n(0, pyb_sync));
-        rt_store_attr(m, MP_QSTR_gc, (mp_obj_t)&pyb_gc_obj);
         rt_store_attr(m, MP_QSTR_delay, rt_make_function_n(1, pyb_delay));
 #if MICROPY_HW_HAS_SWITCH
         rt_store_attr(m, MP_QSTR_switch, (mp_obj_t)&pyb_switch_obj);
-- 
GitLab