Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/erikdubbelboer/gspt"
"github.com/jetkvm/kvm"
"github.com/jetkvm/kvm/internal/diagnostics"
"github.com/jetkvm/kvm/internal/native"
"github.com/jetkvm/kvm/internal/supervisor"
)
Expand Down Expand Up @@ -143,6 +144,13 @@ func supervise() error {
}
}

// Log full system diagnostics
diag := diagnostics.New(diagnostics.Options{Writer: logFile})
diag.LogAll("supervisor_crash")

// Ensure all data is flushed to disk before copying
_ = logFile.Sync()

createErrorDump(logFile)
os.Exit(exiterr.ExitCode())
}
Expand Down
11 changes: 11 additions & 0 deletions internal/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
package diagnostics

import (
"context"
"io"
"time"

"github.com/jetkvm/kvm/internal/logging"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -39,8 +41,11 @@ type Options struct {
type Diagnostics struct {
logger *zerolog.Logger
options Options
ctx context.Context // set during LogAll for overall timeout
}

const defaultLogAllTimeout = 10 * time.Second

// New creates a new Diagnostics instance using the default diagnostics logger.
// If opts.Writer is set, logs are written there instead of the default logger.
func New(opts Options) *Diagnostics {
Expand All @@ -61,7 +66,13 @@ func NewWithLogger(logger *zerolog.Logger, opts Options) *Diagnostics {

// LogAll runs all diagnostic checks and logs the results.
// The phase parameter distinguishes context (e.g., "crash" vs "handshake" vs "download").
// LogAll has an overall timeout of 10 seconds; individual commands have a 2-second timeout.
func (d *Diagnostics) LogAll(phase string) {
ctx, cancel := context.WithTimeout(context.Background(), defaultLogAllTimeout)
defer cancel()
d.ctx = ctx
defer func() { d.ctx = nil }()

d.logger.Error().Str("phase", phase).Msg("=== DIAGNOSTICS ===")

d.LogSystemInfo()
Expand Down
12 changes: 10 additions & 2 deletions internal/diagnostics/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ const defaultCmdTimeout = 2 * time.Second

// runCmdLog runs a command and logs its output.
func (d *Diagnostics) runCmdLog(label string, cmd string, args ...string) {
ctx, cancel := context.WithTimeout(context.Background(), defaultCmdTimeout)
parentCtx := d.ctx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use a coalescing helper (in a utils package) for this

func orBackground(ctx context.Context) context.Context {
	if ctx == nil {
		return context.Background()
	} else {
		return ctx
	}
}

if parentCtx == nil {
parentCtx = context.Background()
}
ctx, cancel := context.WithTimeout(parentCtx, defaultCmdTimeout)
defer cancel()

output, err := exec.CommandContext(ctx, cmd, args...).CombinedOutput()
Expand All @@ -26,7 +30,11 @@ func (d *Diagnostics) runCmdLog(label string, cmd string, args ...string) {

// runShellLog runs a shell command (for pipelines) and logs its output.
func (d *Diagnostics) runShellLog(label, script string) {
ctx, cancel := context.WithTimeout(context.Background(), defaultCmdTimeout)
parentCtx := d.ctx
if parentCtx == nil {
parentCtx = context.Background()
}
ctx, cancel := context.WithTimeout(parentCtx, defaultCmdTimeout)
defer cancel()

output, err := exec.CommandContext(ctx, "sh", "-c", script).CombinedOutput()
Expand Down