pe: debugging tools

This commit is contained in:
Lephenixnoir 2022-11-10 22:35:41 +01:00
parent 524562e8bc
commit c1d2ca5048
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
8 changed files with 130 additions and 22 deletions

View File

@ -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.

View File

@ -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 \

70
ports/sh/debug.c Normal file
View File

@ -0,0 +1,70 @@
#include "debug.h"
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#include <gint/kmalloc.h>
#include <gint/display.h>
#include <gint/cpu.h>
#include <stdio.h>
#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 */

40
ports/sh/debug.h Normal file
View File

@ -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 */

View File

@ -16,6 +16,7 @@
#include <gint/display.h>
#include <gint/drivers/keydev.h>
#include <gint/keyboard.h>
#include <gint/kmalloc.h>
#include <gint/fs.h>
#include <justui/jscene.h>
@ -30,16 +31,10 @@
#include <stdlib.h>
#include <string.h>
#include "mpconfigport.h"
#include "console.h"
#include "widget_shell.h"
/* TODO: Expand debug configuration to not be hardcoded */
#define DEBUG 0
#if DEBUG
#include <gint/usb.h>
#include <gint/usb-ff-bulk.h>
#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);

View File

@ -8,6 +8,12 @@
#include <stdint.h>
#include <alloca.h>
/* 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)

View File

@ -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

View File

@ -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;
}