From 7e098d58697ef94e899f2aff9bbdf6d23e5a8c71 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Thu, 6 Feb 2025 22:41:52 -0600 Subject: [PATCH] add js debug functions;remove tracy to hooks --- meson.build | 2 - scripts/modules/js.js | 3 +- source/jsffi.c | 122 ++++++++++++------------------------------ source/qjs_tracy.c | 34 ++++++++++++ source/script.c | 1 - 5 files changed, 68 insertions(+), 94 deletions(-) diff --git a/meson.build b/meson.build index 28d732ef..7059daa1 100644 --- a/meson.build +++ b/meson.build @@ -9,14 +9,12 @@ if not get_option('editor') add_project_arguments('-DNEDITOR', language:'c') endif -# Grab the nearest tag (e.g., "0.6.1") git_tag_cmd = run_command('git', 'describe', '--tags', '--abbrev=0', check: false) prosperon_version = 'unknown' if git_tag_cmd.returncode() == 0 prosperon_version = git_tag_cmd.stdout().strip() endif -# Grab short commit hash (e.g., "de1c9a1") git_commit_cmd = run_command('git', 'rev-parse', '--short', 'HEAD', check: false) prosperon_commit = 'unknown' if git_commit_cmd.returncode() == 0 diff --git a/scripts/modules/js.js b/scripts/modules/js.js index 265ddb58..480b0853 100644 --- a/scripts/modules/js.js +++ b/scripts/modules/js.js @@ -1,6 +1,5 @@ var js = this -js.dump_mem[prosperon.DOC] = "Return a string summarizing memory usage in the QuickJS runtime." js.dump_shapes[prosperon.DOC] = "Return a debug string describing the internal shape hierarchy used by QuickJS." js.dump_atoms[prosperon.DOC] = "Return a debug string describing all currently registered atoms in the QuickJS runtime." js.calc_mem[prosperon.DOC] = "Return the approximate memory usage of a single JS value." @@ -12,4 +11,4 @@ js.memstate[prosperon.DOC] = "Return simpler memory usage (malloc sizes, etc.) f js.gc[prosperon.DOC] = "Force a full garbage collection immediately." js.eval[prosperon.DOC] = "Evaluate the given JavaScript source string with an optional filename, returning the result." -return js \ No newline at end of file +return js diff --git a/source/jsffi.c b/source/jsffi.c index ec15235d..8f1395df 100644 --- a/source/jsffi.c +++ b/source/jsffi.c @@ -1128,7 +1128,6 @@ void SDL_Renderer_free(JSRuntime *rt, SDL_Renderer *r) void SDL_Surface_free(JSRuntime *rt, SDL_Surface *s) { if (s->flags & SDL_SURFACE_PREALLOCATED) free(s->pixels); - TracyCFreeN(s,"texture memory"); SDL_DestroySurface(s); } @@ -1202,18 +1201,15 @@ QJSCLASS(SDL_Renderer) QJSCLASS(SDL_Camera) void SDL_Texture_free(JSRuntime *rt, SDL_Texture *t){ - TracyCFreeN(t, "vram"); SDL_DestroyTexture(t); } QJSCLASS(SDL_Texture, - TracyCAllocN(n, n->w*n->h*4, "vram"); JS_SetProperty(js, j, width_atom, number2js(js,n->w)); JS_SetProperty(js,j,height_atom,number2js(js,n->h)); ) QJSCLASS(SDL_Surface, - TracyCAllocN(n, n->pitch*n->h, "texture memory"); JS_SetProperty(js, j, width_atom, number2js(js,n->w)); JS_SetProperty(js,j,height_atom,number2js(js,n->h)); ) @@ -6560,9 +6556,7 @@ JSValue js_os_sys(JSContext *js, JSValue self, int argc, JSValue *argv) } JSC_CCALL(os_exit, exit(js2number(js,argv[0]));) -JSC_CCALL(os_gc, - return JS_RunGC(JS_GetRuntime(js), js) -) +JSC_CCALL(os_gc, JS_RunGC(JS_GetRuntime(js)) ) JSC_CCALL(os_now, return number2js(js, (double)SDL_GetTicksNS()/1000000000.0)) JSC_CCALL(os_mem_limit, JS_SetMemoryLimit(JS_GetRuntime(js), js2number(js,argv[0]))) JSC_CCALL(os_gc_threshold, JS_SetGCThreshold(JS_GetRuntime(js), js2number(js,argv[0]))) @@ -6581,22 +6575,11 @@ static JSValue tmp2js(JSContext *js,FILE *tmp) } JSC_CCALL(os_dump_atoms, - FILE *tmp = tmpfile(); - quickjs_set_dumpout(tmp); - JS_PrintAtoms(JS_GetRuntime(js)); - ret = tmp2js(js,tmp); + return js_dump_atoms(js); ) JSC_CCALL(os_dump_shapes, - FILE *tmp = tmpfile(); - quickjs_set_dumpout(tmp); - JS_PrintShapes(JS_GetRuntime(js)); - size_t size = ftell(tmp); - rewind(tmp); - char buffer[size]; - fgets(buffer, sizeof(char)*size, tmp); - fclose(tmp); - ret = JS_NewString(js,buffer); + return js_dump_shapes(js); ) JSC_CCALL(os_calc_mem, @@ -6654,43 +6637,7 @@ JSC_CCALL(os_rusage, #endif ) -JSC_CCALL(os_mem, - JSMemoryUsage jsmem; - JS_ComputeMemoryUsage(JS_GetRuntime(js), &jsmem); - ret = JS_NewObject(js); - JSJMEMRET(malloc_size) - JSJMEMRET(malloc_limit) - JSJMEMRET(memory_used_size) - JSJMEMRET(memory_used_count) - JSJMEMRET(atom_count) - JSJMEMRET(atom_size) - JSJMEMRET(str_count) - JSJMEMRET(str_size) - JSJMEMRET(obj_count) - JSJMEMRET(prop_count) - JSJMEMRET(prop_size) - JSJMEMRET(shape_count) - JSJMEMRET(shape_size) - JSJMEMRET(js_func_count) - JSJMEMRET(js_func_size) - JSJMEMRET(js_func_code_size) - JSJMEMRET(js_func_pc2line_count) - JSJMEMRET(js_func_pc2line_size) - JSJMEMRET(c_func_count) - JSJMEMRET(array_count) - JSJMEMRET(fast_array_count) - JSJMEMRET(fast_array_elements) - JSJMEMRET(binary_object_count) - JSJMEMRET(binary_object_size) -) - -JSC_CCALL(os_dump_mem, - FILE *tmp = tmpfile(); - JSMemoryUsage mem = {0}; - JS_DumpMemoryUsage(tmp, &mem, JS_GetRuntime(js)); - ret = tmp2js(js,tmp); -) - +JSC_CCALL(os_mem, return js_get_memory_usage(js)) JSC_CCALL(os_value_id, return number2js(js,(intptr_t)JS_VALUE_GET_PTR(self)); ) @@ -7291,10 +7238,38 @@ static const JSCFunctionListEntry js_os_funcs[] = { MIST_FUNC_DEF(os, system, 1), }; +JSC_CCALL(js_dump_class, return js_get_object_class_distribution(js)) +JSC_CCALL(js_dump_type_overheads, return js_get_object_type_overheads(js)) +JSC_CCALL(js_dump_objects, return js_dump_objects(js)) + +JSValue cycle_fn = JS_UNDEFINED; + +void cycle_hook_call(JSContext *js, JSValue v) +{ + JS_FreeValue(js,JS_Call(js,cycle_fn,JS_UNDEFINED,1,&v)); +} + +JSC_CCALL(js_cycle_hook, + if (JS_IsUndefined(argv[0])) + js_debug_sethook(js,NULL,JS_HOOK_CYCLE); + else { + printf("SETTING DEBUG HOOK\n"); + JS_FreeValue(js,cycle_fn); + cycle_fn = JS_DupValue(js,argv[0]); + js_debug_sethook(js,cycle_hook_call, JS_HOOK_CYCLE); + } +) + +JSC_CCALL(js_stack_info, return js_dump_stack_info()) + static const JSCFunctionListEntry js_js_funcs[] = { - MIST_FUNC_DEF(os, dump_mem, 0), + MIST_FUNC_DEF(js, cycle_hook,1), MIST_FUNC_DEF(os, dump_shapes, 0), MIST_FUNC_DEF(os, dump_atoms,0), + MIST_FUNC_DEF(js, dump_class, 0), + MIST_FUNC_DEF(js, dump_objects, 0), + MIST_FUNC_DEF(js, dump_type_overheads, 0), + MIST_FUNC_DEF(js, stack_info, 0), MIST_FUNC_DEF(os, calc_mem, 1), MIST_FUNC_DEF(os, mem, 1), MIST_FUNC_DEF(os, mem_limit, 1), @@ -7872,34 +7847,3 @@ void ffi_load(JSContext *js, int argc, char **argv) { JS_FreeValue(js,globalThis); } -/* - -#if defined(TRACY_ENABLE) && !defined(_WIN32) - const char *ccname = get_func_name(ctx,func_obj); - const char *file = ""; - TracyCZoneCtx tracy_ctx = ___tracy_emit_zone_begin_alloc(___tracy_alloc_srcloc(1, file, strlen(file), ccname, strlen(ccname), (int)ccname), 1); - JS_FreeCString(ctx,ccname); -#endif - -#ifdef TRACY_ENABLE - ___tracy_emit_zone_end(tracy_ctx); -#endif - -#ifdef TRACY_ENABLE - const char *fn_src = JS_AtomToCString(caller_ctx, js_fn_filename(caller_ctx,func_obj)); - const char *js_func_name = get_func_name(caller_ctx, func_obj); - const char *fn_name; - if (!js_func_name || js_func_name[0] == '\0') - fn_name = ""; - else - fn_name = js_func_name; - - uint64_t srcloc; - srcloc = ___tracy_alloc_srcloc(js_fn_linenum(caller_ctx,func_obj), fn_src, strlen(fn_src), fn_name, strlen(fn_name), (int)fn_src); - - TracyCZoneCtx tracy_ctx = ___tracy_emit_zone_begin_alloc(srcloc,1); - JS_FreeCString(caller_ctx,js_func_name); - JS_FreeCString(caller_ctx,fn_src); -#endif - -*/ diff --git a/source/qjs_tracy.c b/source/qjs_tracy.c index 93891a66..082f1ab0 100644 --- a/source/qjs_tracy.c +++ b/source/qjs_tracy.c @@ -4,6 +4,7 @@ #include #include "render.h" #include +#include "stb_ds.h" static JSValue js_tracy_fiber_enter(JSContext *js, JSValue self, int argc, JSValue *argv) { @@ -482,6 +483,38 @@ static JSValue js_tracy_image(JSContext *js, JSValue self, int argc, JSValue *ar #endif +static TracyCZoneCtx *tracy_stack; + +void tracy_call_hook(JSContext *js, JSValue fn) +{ + js_debug debug; + js_debug_info(js,fn,&debug); + + uint64_t srcloc; + srcloc = ___tracy_alloc_srcloc(debug.line, debug.source, debug.srclen, debug.name, strlen(debug.name), (int)debug.source); + + arrput(tracy_stack, ___tracy_emit_zone_begin_alloc(srcloc,1)); +} + +void tracy_end_hook(JSContext *js, JSValue fn) +{ + if (arrlen(tracy_stack) >0) + ___tracy_emit_zone_end(arrpop(tracy_stack)); +} + +JSValue js_tracy_level(JSContext *js, JSValue selff, int argc, JSValue *argv) +{ + int32_t level; + JS_ToInt32(js,&level, argv[0]); + if (JS_IsUndefined(argv[0]) || level == 0) { + js_debug_sethook(js,NULL,JS_HOOK_CALL); + js_debug_sethook(js,NULL,JS_HOOK_RET); + } else { + js_debug_sethook(js, tracy_call_hook, JS_HOOK_CALL); + js_debug_sethook(js, tracy_end_hook, JS_HOOK_RET); + } +} + static const JSCFunctionListEntry js_tracy_funcs[] = { JS_CFUNC_DEF("fiber", 1, js_tracy_fiber_enter), JS_CFUNC_DEF("fiber_leave", 1, js_tracy_fiber_leave), @@ -494,6 +527,7 @@ static const JSCFunctionListEntry js_tracy_funcs[] = { JS_CFUNC_DEF("message", 1, js_tracy_message), JS_CFUNC_DEF("plot", 2, js_tracy_plot), JS_CFUNC_DEF("image", 3, js_tracy_image), + JS_CFUNC_DEF("level", 1, js_tracy_level), }; JSValue js_tracy_use(JSContext *js) diff --git a/source/script.c b/source/script.c index 37afdb29..b89636db 100644 --- a/source/script.c +++ b/source/script.c @@ -158,7 +158,6 @@ void script_startup(int argc, char **argv) { JS_AddIntrinsicJSON(js); JS_AddIntrinsicMapSet(js); JS_AddIntrinsicTypedArrays(js); - JS_AddIntrinsicProxy(js); JS_AddIntrinsicBigInt(js); JS_AddIntrinsicBigFloat(js); JS_AddIntrinsicBigDecimal(js);