Merge pull request #1181 from visualfc/trace

debug: fix print stack for wasm
This commit is contained in:
xushiwei
2025-07-01 13:22:42 +08:00
committed by GitHub
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
})
}
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"
)
const (
LLGoFiles = "_wrap/debug_wasm.c"
)
type Info struct {
Fname *c.Char
Fbase c.Pointer
@@ -31,3 +35,10 @@ type Frame struct {
func StackTrace(skip int, fn func(fr *Frame) bool) {
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 link == nil {
TracePanic(*(*any)(ptr))
debug.StackTrace(0, func(fr *debug.Frame) bool {
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
})
debug.PrintStack(2)
c.Free(ptr)
c.Exit(2)
} else {