debug: fix print stack for wasm

This commit is contained in:
visualfc
2025-07-01 11:28:48 +08:00
parent f0728c4fe0
commit 013f2ef00d
4 changed files with 52 additions and 7 deletions

View File

@@ -0,0 +1,31 @@
#ifdef __EMSCRIPTEN__
//emscripten
#include <emscripten.h>
void llgo_print_stack(int skip) {
EM_ASM({
function getStackTrace() {
var stack;
try {
throw new Error();
} catch (e) {
stack = e.stack;
}
return stack;
}
var stack = getStackTrace();
if (stack) {
var frames = stack.split('\n').slice($0);
frames.forEach(function(frame) {
console.log(frame.trim());
});
}
}, skip);
}
#else
// wasi
void llgo_print_stack(int skip){
}
#endif

View File

@@ -44,3 +44,12 @@ func StackTrace(skip int, fn func(fr *Frame) bool) {
return 1 return 1
}) })
} }
func PrintStack(skip int) {
StackTrace(skip+1, func(fr *Frame) bool {
var info Info
Addrinfo(unsafe.Pointer(fr.PC), &info)
c.Fprintf(c.Stderr, c.Str("[0x%08X %s+0x%x, SP = 0x%x]\n"), fr.PC, fr.Name, fr.Offset, fr.SP)
return true
})
}

View File

@@ -6,6 +6,10 @@ import (
c "github.com/goplus/llgo/runtime/internal/clite" c "github.com/goplus/llgo/runtime/internal/clite"
) )
const (
LLGoFiles = "_wrap/debug_wasm.c"
)
type Info struct { type Info struct {
Fname *c.Char Fname *c.Char
Fbase c.Pointer Fbase c.Pointer
@@ -31,3 +35,10 @@ type Frame struct {
func StackTrace(skip int, fn func(fr *Frame) bool) { func StackTrace(skip int, fn func(fr *Frame) bool) {
panic("not implemented") panic("not implemented")
} }
func PrintStack(skip int) {
print_stack(c.Int(skip + 4))
}
//go:linkname print_stack C.llgo_print_stack
func print_stack(skip c.Int)

View File

@@ -62,13 +62,7 @@ func Rethrow(link *Defer) {
if ptr := excepKey.Get(); ptr != nil { if ptr := excepKey.Get(); ptr != nil {
if link == nil { if link == nil {
TracePanic(*(*any)(ptr)) TracePanic(*(*any)(ptr))
debug.StackTrace(0, func(fr *debug.Frame) bool { debug.PrintStack(2)
var info debug.Info
debug.Addrinfo(unsafe.Pointer(fr.PC), &info)
c.Fprintf(c.Stderr, c.Str("[0x%08X %s+0x%x, SP = 0x%x]\n"), fr.PC, fr.Name, fr.Offset, fr.SP)
return true
})
c.Free(ptr) c.Free(ptr)
c.Exit(2) c.Exit(2)
} else { } else {