Skip to content

Commit

Permalink
Berry debug.gcdebug() to enable GC debugging (#19936)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored Nov 5, 2023
1 parent 3207d54 commit 625b204
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
## [13.2.0.2]
### Added
- Scripter TCP client (#19914)
- Berry ``debug.gcdebug()`` to enable GC debugging

### Breaking Changed

Expand Down
16 changes: 16 additions & 0 deletions lib/libesp32/berry/src/be_debuglib.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ static int m_codedump(bvm *vm)
be_return_nil(vm);
}

static int m_gcdebug(bvm *vm) {
int argc = be_top(vm);
if (argc >= 1 && be_isbool(vm, 1)) {
if (be_tobool(vm, 1)) {
comp_set_gc_debug(vm);
} else {
comp_clear_gc_debug(vm);
}
}
be_pushbool(vm, comp_is_gc_debug(vm));
be_return(vm);
}

static int m_traceback(bvm *vm)
{
be_tracestack(vm);
Expand Down Expand Up @@ -223,6 +236,7 @@ be_native_module_attr_table(debug) {
be_native_module_function("varname", m_varname),
be_native_module_function("upvname", m_upvname)
#endif
be_native_module_function("gcdebug", m_gcdebug)
};

be_define_native_module(debug, NULL);
Expand All @@ -242,6 +256,8 @@ module debug (scope: global, depend: BE_USE_DEBUG_MODULE) {
allocs, func(m_allocs)
frees, func(m_frees)
reallocs, func(m_reallocs)
// GC debug mode
gcdebug, func(m_gcdebug)
}
@const_object_info_end */
#include "../generate/be_fixed_debug.h"
Expand Down
8 changes: 1 addition & 7 deletions lib/libesp32/berry/src/be_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,15 +545,9 @@ static void reset_fixedlist(bvm *vm)

void be_gc_auto(bvm *vm)
{
#if BE_USE_DEBUG_GC
if (vm->gc.status & GC_PAUSE) { /* force gc each time it's possible */
if (vm->gc.status & GC_PAUSE && (BE_USE_DEBUG_GC || vm->gc.usage > vm->gc.threshold || comp_is_gc_debug(vm))) {
be_gc_collect(vm);
}
#else
if (vm->gc.status & GC_PAUSE && vm->gc.usage > vm->gc.threshold) {
be_gc_collect(vm);
}
#endif
}

size_t be_gc_memcount(bvm *vm)
Expand Down
6 changes: 3 additions & 3 deletions lib/libesp32/berry/src/be_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ BERRY_API void* be_realloc(bvm *vm, void *ptr, size_t old_size, size_t new_size)
vm->counter_mem_free++;
#endif
if (ptr == NULL) { return NULL; } /* safeguard */
#if BE_USE_DEBUG_GC
memset(ptr, 0xFF, old_size); /* fill the structure with invalid pointers */
#endif
if (BE_USE_DEBUG_GC || comp_is_gc_debug(vm)) {
memset(ptr, 0xFF, old_size); /* fill the structure with invalid pointers */
}
free_from_pool(vm, ptr, old_size);
break; /* early exit */
}
Expand Down
12 changes: 6 additions & 6 deletions lib/libesp32/berry/src/be_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ void be_gcstrtab(bvm *vm)
}
}
}
#if BE_USE_DEBUG_GC == 0
if (tab->count < size >> 2 && size > 8) {
resize(vm, size >> 1);
if (BE_USE_DEBUG_GC || comp_is_gc_debug(vm)) {
resize(vm, tab->count + 4);
} else {
if (tab->count < size >> 2 && size > 8) {
resize(vm, size >> 1);
}
}
#else
resize(vm, tab->count + 4);
#endif
}

uint32_t be_strhash(const bstring *s)
Expand Down
15 changes: 10 additions & 5 deletions lib/libesp32/berry/src/be_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
#define comp_set_named_gbl(vm) ((vm)->compopt |= (1<<COMP_NAMED_GBL))
#define comp_clear_named_gbl(vm) ((vm)->compopt &= ~(1<<COMP_NAMED_GBL))

#define comp_is_strict(vm) ((vm)->compopt & (1<<COMP_STRICT))
#define comp_set_strict(vm) ((vm)->compopt |= (1<<COMP_STRICT))
#define comp_clear_strict(vm) ((vm)->compopt &= ~(1<<COMP_STRICT))
#define comp_is_strict(vm) ((vm)->compopt & (1<<COMP_STRICT))
#define comp_set_strict(vm) ((vm)->compopt |= (1<<COMP_STRICT))
#define comp_clear_strict(vm) ((vm)->compopt &= ~(1<<COMP_STRICT))

#define comp_is_gc_debug(vm) ((vm)->compopt & (1<<COMP_GC_DEBUG))
#define comp_set_gc_debug(vm) ((vm)->compopt |= (1<<COMP_GC_DEBUG))
#define comp_clear_gc_debug(vm) ((vm)->compopt &= ~(1<<COMP_GC_DEBUG))

/* Compilation options */
typedef enum {
COMP_NAMED_GBL = 0x00, /* compile with named globals */
COMP_STRICT = 0x01, /* compile with named globals */
COMP_NAMED_GBL = 0x00, /* compile with named globals */
COMP_STRICT = 0x01, /* compile with named globals */
COMP_GC_DEBUG = 0x02, /* compile with gc debug */
} compoptmask;

typedef struct {
Expand Down

0 comments on commit 625b204

Please sign in to comment.