From a55a918d4fe1fe73cc5be3190929148987483905 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 25 Jul 2023 18:50:21 +0530 Subject: [PATCH] Improve stack trace formatting --- tools/tui/loop/api.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tools/tui/loop/api.go b/tools/tui/loop/api.go index 33863e489..cc9e32dae 100644 --- a/tools/tui/loop/api.go +++ b/tools/tui/loop/api.go @@ -6,7 +6,7 @@ import ( "encoding/base64" "fmt" "os" - "runtime/debug" + "runtime" "strings" "time" @@ -251,11 +251,20 @@ func (self *Loop) DebugPrintln(args ...any) { func (self *Loop) Run() (err error) { defer func() { if r := recover(); r != nil { - stack := utils.Splitlines(string(debug.Stack())) + pcs := make([]uintptr, 256) + n := runtime.Callers(2, pcs) + frames := runtime.CallersFrames(pcs[:n]) err = fmt.Errorf("Paniced: %s", r) - fmt.Fprintf(os.Stderr, "\r\nPaniced with error: %s\r\nStacktrace:\r\n", r) - for _, line := range stack { - fmt.Fprintf(os.Stderr, "%s\r\n", line) + fmt.Fprintf(os.Stderr, "\r\nPaniced with error: %s\r\nStacktrace (most recent call first):\r\n", r) + found_first_frame := false + for frame, more := frames.Next(); more; frame, more = frames.Next() { + if !found_first_frame { + if strings.HasPrefix(frame.Function, "runtime.") { + continue + } + found_first_frame = true + } + fmt.Fprintf(os.Stderr, "%s\r\n\t%s:%d\r\n", frame.Function, frame.File, frame.Line) } if self.terminal_options.alternate_screen { term, err := tty.OpenControllingTerm(tty.SetRaw)