Feat: crash log (#7)
Merged #6 - crash log (db0e1be9a02f70927124f9f3563df5e9dee3a764) - perf(watchdog): limit buffer size and optimize crash substring search (https://github.com/versenilvis/IRIS/pull/7/commits/7725434fedf8b32929dc0d71e1256fda13cfdfca) - refactor(crash): use dynamic buffer for stack trace capture (https://github.com/versenilvis/IRIS/pull/7/commits/07809a4330a1236471e8dae0e83afd4323a4c83a)
This commit is contained in:
+129
-2
@@ -1,8 +1,12 @@
|
||||
package root
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
@@ -14,6 +18,7 @@ import (
|
||||
_ "github.com/versenilvis/iris/commands/runner"
|
||||
_ "github.com/versenilvis/iris/commands/search"
|
||||
_ "github.com/versenilvis/iris/commands/view"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -23,6 +28,15 @@ var (
|
||||
Long: `IRIS (a.k.a Intelligent Real-time Input Suggestion) is a shell auto-autocompletion tool.
|
||||
It works exactly like coding editor suggestion menu drop down.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
WriteCrashLog(r)
|
||||
restoreTerminal()
|
||||
printCrashNotice()
|
||||
startRescueShell()
|
||||
os.Exit(2)
|
||||
}
|
||||
}()
|
||||
if pidStr := os.Getenv("IRIS_PID"); pidStr != "" {
|
||||
if pid, err := strconv.Atoi(pidStr); err == nil && pid > 0 {
|
||||
_ = syscall.Kill(pid, syscall.SIGUSR1)
|
||||
@@ -49,13 +63,117 @@ func init() {
|
||||
rootCmd.PersistentFlags().BoolVarP(&debugMode, "debug", "d", false, "enable debug logging to iris.log")
|
||||
}
|
||||
|
||||
func debugLog(format string, a ...interface{}) {
|
||||
func debugLog(format string, a ...any) {
|
||||
if debugLogger != nil {
|
||||
_, _ = fmt.Fprintf(debugLogger, format+"\n", a...)
|
||||
}
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
// runWatchdog spawns the watchdog parent process
|
||||
func runWatchdog() {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
runOriginal()
|
||||
return
|
||||
}
|
||||
|
||||
// save original terminal settings in parent process
|
||||
watchdogOldState, errState := term.MakeRaw(int(os.Stdin.Fd()))
|
||||
if errState == nil {
|
||||
_ = term.Restore(int(os.Stdin.Fd()), watchdogOldState)
|
||||
}
|
||||
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
runOriginal()
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(context.Background(), exe, os.Args[1:]...)
|
||||
cmd.Env = append(os.Environ(), "IRIS_IS_CHILD=true")
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = w
|
||||
|
||||
err = cmd.Start()
|
||||
if err != nil {
|
||||
runOriginal()
|
||||
return
|
||||
}
|
||||
|
||||
_ = w.Close()
|
||||
|
||||
// copy child stderr to both our buffer and the real stderr, filtering out panics
|
||||
var stderrBuf bytes.Buffer
|
||||
origStderr := os.Stderr
|
||||
tempBuf := make([]byte, 1024)
|
||||
suppress := false
|
||||
for {
|
||||
n, errRead := r.Read(tempBuf)
|
||||
if n > 0 {
|
||||
_, _ = stderrBuf.Write(tempBuf[:n])
|
||||
if stderrBuf.Len() > 64*1024 {
|
||||
// discard oldest bytes to avoid memory leak
|
||||
over := stderrBuf.Len() - 64*1024
|
||||
_ = stderrBuf.Next(over)
|
||||
}
|
||||
if !suppress {
|
||||
currentContent := stderrBuf.Bytes()
|
||||
searchStart := 0
|
||||
if len(currentContent) > n+12 {
|
||||
searchStart = len(currentContent) - (n + 12)
|
||||
}
|
||||
searchSlice := currentContent[searchStart:]
|
||||
idxPanic := bytes.Index(searchSlice, []byte("panic:"))
|
||||
idxFatal := bytes.Index(searchSlice, []byte("fatal error:"))
|
||||
triggerIdx := -1
|
||||
if idxPanic != -1 {
|
||||
triggerIdx = searchStart + idxPanic
|
||||
} else if idxFatal != -1 {
|
||||
triggerIdx = searchStart + idxFatal
|
||||
}
|
||||
|
||||
if triggerIdx != -1 {
|
||||
suppress = true
|
||||
printedLen := len(currentContent) - n
|
||||
if triggerIdx > printedLen {
|
||||
_, _ = origStderr.Write(currentContent[printedLen:triggerIdx])
|
||||
}
|
||||
} else {
|
||||
_, _ = origStderr.Write(tempBuf[:n])
|
||||
}
|
||||
}
|
||||
}
|
||||
if errRead != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// check if child exited abnormally or crashed
|
||||
errWait := cmd.Wait()
|
||||
if errWait != nil {
|
||||
content := stderrBuf.Bytes()
|
||||
if bytes.Contains(content, []byte("panic:")) || bytes.Contains(content, []byte("fatal error:")) {
|
||||
WriteCrashLog(string(content))
|
||||
// restore terminal state if watchdog saved it
|
||||
if watchdogOldState != nil {
|
||||
_ = term.Restore(int(os.Stdin.Fd()), watchdogOldState)
|
||||
}
|
||||
printCrashNotice()
|
||||
startRescueShell()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(errWait, &exitErr) {
|
||||
os.Exit(exitErr.ExitCode())
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// runOriginal runs the normal command execution
|
||||
func runOriginal() {
|
||||
if os.Getenv("IRIS_RELOADED") == "true" {
|
||||
fmt.Printf("\r\033[K\033[35m[IRIS] reloading...\033[0m\n")
|
||||
_ = os.Unsetenv("IRIS_RELOADED")
|
||||
@@ -66,3 +184,12 @@ func Execute() {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if os.Getenv("IRIS_IS_CHILD") != "true" {
|
||||
runWatchdog()
|
||||
return
|
||||
}
|
||||
|
||||
runOriginal()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user