From c1d2ca5048659c88263cf8ac2ee7c5806f332295 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Thu, 10 Nov 2022 22:35:41 +0100 Subject: [PATCH] pe: debugging tools --- README.md | 2 +- ports/sh/Makefile | 1 + ports/sh/debug.c | 70 +++++++++++++++++++++++++++++++++++++++++ ports/sh/debug.h | 40 +++++++++++++++++++++++ ports/sh/main.c | 27 ++++++---------- ports/sh/mpconfigport.h | 6 ++++ py/gc.c | 4 +-- py/malloc.c | 2 +- 8 files changed, 130 insertions(+), 22 deletions(-) create mode 100644 ports/sh/debug.c create mode 100644 ports/sh/debug.h diff --git a/README.md b/README.md index 5d5eef107..7e5f4c48a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ This is a MicroPython port for fx-CG 50, fx-9860G III and related CASIO calculat **Build instructions** -Requires the [fxSDK](/Lephenixnoir/fxsdk). Go to `ports/fxcg50` or `ports/fx9860g3` and run `make`. If it doesn't build, first try to use the `dev` branches for [fxSDK](/Lephenixnoir/fxSDK), [gint](/Lephenixnoir/gint), [fxlibc](/Lephenixnoir/fxlib) and [JustUI](/Lephenixnoir/JustUI). +Requires the [fxSDK](/Lephenixnoir/fxsdk). Go to `ports/fxcg50` or `ports/fx9860g3` and run `make`. If it doesn't build, first try to use the `dev` branches for [fxSDK](/Lephenixnoir/fxSDK), [gint](/Lephenixnoir/gint), [fxlibc](/Lephenixnoir/fxlib) and [JustUI](/Lephenixnoir/JustUI). Rebuilds don't always work especially when checking out other commits (maybe my fault), so try to delete `build` if you think that's related. Most of the code is in `ports/sh` and is shared between the platforms. diff --git a/ports/sh/Makefile b/ports/sh/Makefile index fe5d641c9..61cc8857a 100644 --- a/ports/sh/Makefile +++ b/ports/sh/Makefile @@ -12,6 +12,7 @@ LIBS += -nostdlib -Wl,--no-warn-rwx-segments $(SH_LDFLAGS) -Wl,-Map=build/map SRC_C = \ ports/sh/main.c \ ports/sh/console.c \ + ports/sh/debug.c \ ports/sh/keymap.c \ ports/sh/modcasioplot.c \ ports/sh/modgint.c \ diff --git a/ports/sh/debug.c b/ports/sh/debug.c new file mode 100644 index 000000000..530034cdd --- /dev/null +++ b/ports/sh/debug.c @@ -0,0 +1,70 @@ +#include "debug.h" +#include +#include +#include +#include +#include +#include + +#if PE_DEBUG + +void pe_debug_init(void) +{ + usb_interface_t const *intf[] = { &usb_ff_bulk, NULL }; + usb_open(intf, GINT_CALL_NULL); + + dclear(C_BLACK); + dtext_opt(DWIDTH/2, DHEIGHT/2 - 3, C_WHITE, C_NONE, DTEXT_MIDDLE, + DTEXT_BOTTOM, "Waiting for USB connection..."); + dtext_opt(DWIDTH/2, DHEIGHT/2 + 3, C_WHITE, C_NONE, DTEXT_MIDDLE, + DTEXT_TOP, "% fxlink -iqw"); + dupdate(); + + while(!usb_is_open()) sleep(); +} + +void pe_debug_printf(char const *fmt, ...) +{ + char str_default[256]; + + va_list args; + va_start(args, fmt); + char *str; + vasprintf(&str, fmt, args); + va_end(args); + + if(str == NULL) { + va_start(args, fmt); + str = str_default; + vsnprintf(str, sizeof str_default, fmt, args); + va_end(args); + } + + if(usb_is_open()) + usb_fxlink_text(str, 0); +} + +/* This function is used in MicroPython. */ +void DEBUG_printf(char const *fmt, ...) +__attribute__((alias("pe_debug_printf"))); + +void pe_debug_kmalloc(void) +{ + kmalloc_gint_stats_t *s; + + s = kmalloc_get_gint_stats(kmalloc_get_arena("_uram")); + pe_debug_printf("[_uram] used=%d free=%d\n", + s->used_memory, s->free_memory); + + s = kmalloc_get_gint_stats(kmalloc_get_arena("_ostk")); + pe_debug_printf("[_ostk] used=%d free=%d\n", + s->used_memory, s->free_memory); +} + +void pe_debug_screenshot(void) +{ + if(usb_is_open()) + usb_fxlink_screenshot(true); +} + +#endif /* PE_DEBUG */ diff --git a/ports/sh/debug.h b/ports/sh/debug.h new file mode 100644 index 000000000..5e2dedf9d --- /dev/null +++ b/ports/sh/debug.h @@ -0,0 +1,40 @@ +//---------------------------------------------------------------------------// +// ____ PythonExtra // +//.-'`_ o `;__, A community port of MicroPython for CASIO calculators. // +//.-'` `---` ' License: MIT (except some files; see LICENSE) // +//---------------------------------------------------------------------------// +// pe.debug: Debugging utilities +// +// Most of the debugging occurs via USB. This module also includes screenshots +// and other developer-only utilities. The interface always exists but it's +// completely no-oped if PE_DEBUG isn't set. +//--- + +#ifndef __PYTHONEXTRA_DEBUG_H +#define __PYTHONEXTRA_DEBUG_H + +/* PE_DEBUG is set in mpconfigport.h. */ +#include "mpconfigport.h" + +/* Initialize debugging resources (mostly the USB connection). */ +void pe_debug_init(void); + +/* Print to the debug stream. This function is also called DEBUG_printf in + MicroPython code. */ +void pe_debug_printf(char const *fmt, ...); + +/* Print information about allocation status. */ +void pe_debug_kmalloc(void); + +/* Take a screenshot. */ +void pe_debug_screenshot(void); + +#if !PE_DEBUG +#define PE_DEBUG_NOOP do {} while(0) +#define pe_debug_init(...) PE_DEBUG_NOOP +#define pe_debug_printf(...) PE_DEBUG_NOOP +#define pe_debug_kmalloc(...) PE_DEBUG_NOOP +#define pe_debug_screenshot(...) PE_DEBUG_NOOP +#endif + +#endif /* __PYTHONEXTRA_DEBUG_H */ diff --git a/ports/sh/main.c b/ports/sh/main.c index 4322449cf..8b03e619a 100644 --- a/ports/sh/main.c +++ b/ports/sh/main.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -30,16 +31,10 @@ #include #include +#include "mpconfigport.h" #include "console.h" #include "widget_shell.h" - -/* TODO: Expand debug configuration to not be hardcoded */ -#define DEBUG 0 - -#if DEBUG -#include -#include -#endif +#include "debug.h" #ifdef FX9860G extern bopti_image_t const img_fkeys_main; @@ -128,13 +123,10 @@ static bool async_filter(key_event_t ev) return true; } - int main(int argc, char **argv) { -#if DEBUG - usb_interface_t const *intf[] = { &usb_ff_bulk, NULL }; - usb_open(intf, GINT_CALL_NULL); -#endif + pe_debug_init(); + pe_debug_kmalloc(); //=== Init sequence ===// @@ -272,10 +264,10 @@ int main(int argc, char **argv) continue; int key = e.key.key; -#if DEBUG - if(usb_is_open() && key == KEY_SQUARE && !e.key.shift && e.key.alpha) - usb_fxlink_screenshot(true); -#endif + if(key == KEY_SQUARE && !e.key.shift && e.key.alpha) + pe_debug_screenshot(); + if(key == KEY_TAN) + pe_debug_kmalloc(); if(key == KEY_F1) { jscene_show_and_focus(scene, fileselect); @@ -289,7 +281,6 @@ int main(int argc, char **argv) //=== Deinitialization ===// - // Deinitialise the runtime. gc_sweep_all(); mp_deinit(); console_destroy(pe_shell_console); diff --git a/ports/sh/mpconfigport.h b/ports/sh/mpconfigport.h index 43afcd596..a5f151e77 100644 --- a/ports/sh/mpconfigport.h +++ b/ports/sh/mpconfigport.h @@ -8,6 +8,12 @@ #include #include +/* Debugging options: PythonExtra debug tools (pretty much required for any + other one), MicroPython's verbose logging. */ +/* PythonExtra's main debug flag */ +#define PE_DEBUG (0) +#define MICROPY_DEBUG_VERBOSE (0) + /* General feature set selection Other options: BASIC_FEATURES, EXTRA_FEATURES, FULL_FEATURES, EVERYTHING */ #define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_CORE_FEATURES) diff --git a/py/gc.c b/py/gc.c index 120213783..30e2e5922 100644 --- a/py/gc.c +++ b/py/gc.c @@ -183,11 +183,11 @@ void gc_init(void *start, void *end) { #endif DEBUG_printf("GC layout:\n"); - DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_alloc_table_start), MP_STATE_MEM(gc_alloc_table_byte_len), MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB); + DEBUG_printf(" alloc table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(area.gc_alloc_table_start), MP_STATE_MEM(area.gc_alloc_table_byte_len), MP_STATE_MEM(area.gc_alloc_table_byte_len) * BLOCKS_PER_ATB); #if MICROPY_ENABLE_FINALISER DEBUG_printf(" finaliser table at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_finaliser_table_start), gc_finaliser_table_byte_len, gc_finaliser_table_byte_len * BLOCKS_PER_FTB); #endif - DEBUG_printf(" pool at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_pool_start), gc_pool_block_len * BYTES_PER_BLOCK, gc_pool_block_len); +// DEBUG_printf(" pool at %p, length " UINT_FMT " bytes, " UINT_FMT " blocks\n", MP_STATE_MEM(gc_pool_start), gc_pool_block_len * BYTES_PER_BLOCK, gc_pool_block_len); } #if MICROPY_GC_SPLIT_HEAP diff --git a/py/malloc.c b/py/malloc.c index efdff7539..ddf139e38 100644 --- a/py/malloc.c +++ b/py/malloc.c @@ -185,7 +185,7 @@ void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move) #if MICROPY_MALLOC_USES_ALLOCATED_SIZE DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr); #else - DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, new_num_bytes, new_ptr); + DEBUG_printf("realloc %p, %d : %p\n", ptr, new_num_bytes, new_ptr); #endif return new_ptr; }