From 2dac99d40b209686fe23ffc03e4b5e773d552f43 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Wed, 29 Nov 2023 16:25:18 +0100 Subject: [PATCH 1/3] fix(cmd/gno): log panic information in gno test --- gnovm/cmd/gno/test.go | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 38615c88c4e..c399d34467f 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -9,6 +9,7 @@ import ( "log" "os" "path/filepath" + "runtime" "sort" "strings" "text/template" @@ -439,8 +440,13 @@ func runTestFiles( printRuntimeMetrics bool, runFlag string, io commands.IO, -) error { - var errs error +) (errs error) { + defer func() { + if r := recover(); r != nil { + trace := smallStacktrace() + errs = multierr.Append(fmt.Errorf("panic: %v\ngno machine: %v\nstracktrace:\n%v", r, m.String(), trace), errs) + } + }() testFuncs := &testFuncs{ PackageName: pkgName, @@ -535,6 +541,34 @@ func runTestFiles( return errs } +/* + smallStacktrace sample result: + +prog.go:11 main.main +proc.go:250 runtime.main +asm_amd64.s:1598 runtime.goexit +*/ +func smallStacktrace() string { + var buf bytes.Buffer + pc := make([]uintptr, 100) + pc = pc[:runtime.Callers(2, pc)] + frames := runtime.CallersFrames(pc) + for { + f, more := frames.Next() + + if idx := strings.LastIndexByte(f.Function, '/'); idx >= 0 { + f.Function = f.Function[idx+1:] + } + + fmt.Fprintf(&buf, "%-25s %s\n", fmt.Sprintf("%s:%d", filepath.Base(f.File), f.Line), f.Function) + + if !more { + return buf.String() + } + } + return buf.String() +} + // mirror of stdlibs/testing.Report type report struct { Name string From a6a71413c73f599c8a3bd48cb8ab9b7ad5619dc8 Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Thu, 21 Dec 2023 13:02:44 +0100 Subject: [PATCH 2/3] improve smallStacktrace --- gnovm/cmd/gno/test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index c399d34467f..1fd2ffc4872 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -549,6 +549,8 @@ proc.go:250 runtime.main asm_amd64.s:1598 runtime.goexit */ func smallStacktrace() string { + const unicodeEllipsis = "\u2026" + var buf bytes.Buffer pc := make([]uintptr, 100) pc = pc[:runtime.Callers(2, pc)] @@ -560,7 +562,13 @@ func smallStacktrace() string { f.Function = f.Function[idx+1:] } - fmt.Fprintf(&buf, "%-25s %s\n", fmt.Sprintf("%s:%d", filepath.Base(f.File), f.Line), f.Function) + // trim full path to at most 30 characters + fullPath := fmt.Sprintf("%s:%-4d", f.File, f.Line) + if len(fullPath) > 30 { + fullPath = unicodeEllipsis + fullPath[len(fullPath)-29:] + } + + fmt.Fprintf(&buf, "%30s %s\n", fullPath, f.Function) if !more { return buf.String() From cfd9737d2c7cd4fb6c0463e12950647fba94a3cd Mon Sep 17 00:00:00 2001 From: Morgan Bazalgette Date: Mon, 15 Jan 2024 23:15:45 +0100 Subject: [PATCH 3/3] change to use normal stacktraces --- gnovm/cmd/gno/test.go | 41 ++--------------------------------------- 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 1fd2ffc4872..5664e4bb28d 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -9,7 +9,7 @@ import ( "log" "os" "path/filepath" - "runtime" + "runtime/debug" "sort" "strings" "text/template" @@ -443,8 +443,7 @@ func runTestFiles( ) (errs error) { defer func() { if r := recover(); r != nil { - trace := smallStacktrace() - errs = multierr.Append(fmt.Errorf("panic: %v\ngno machine: %v\nstracktrace:\n%v", r, m.String(), trace), errs) + errs = multierr.Append(fmt.Errorf("panic: %v\nstack:\n%v\ngno machine: %v", r, string(debug.Stack()), m.String()), errs) } }() @@ -541,42 +540,6 @@ func runTestFiles( return errs } -/* - smallStacktrace sample result: - -prog.go:11 main.main -proc.go:250 runtime.main -asm_amd64.s:1598 runtime.goexit -*/ -func smallStacktrace() string { - const unicodeEllipsis = "\u2026" - - var buf bytes.Buffer - pc := make([]uintptr, 100) - pc = pc[:runtime.Callers(2, pc)] - frames := runtime.CallersFrames(pc) - for { - f, more := frames.Next() - - if idx := strings.LastIndexByte(f.Function, '/'); idx >= 0 { - f.Function = f.Function[idx+1:] - } - - // trim full path to at most 30 characters - fullPath := fmt.Sprintf("%s:%-4d", f.File, f.Line) - if len(fullPath) > 30 { - fullPath = unicodeEllipsis + fullPath[len(fullPath)-29:] - } - - fmt.Fprintf(&buf, "%30s %s\n", fullPath, f.Function) - - if !more { - return buf.String() - } - } - return buf.String() -} - // mirror of stdlibs/testing.Report type report struct { Name string