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)