diff --git a/pkg/cmd/tracerunner.go b/pkg/cmd/tracerunner.go index 140fcd88..0900138a 100644 --- a/pkg/cmd/tracerunner.go +++ b/pkg/cmd/tracerunner.go @@ -1,13 +1,16 @@ package cmd import ( + "context" "fmt" "io" "io/ioutil" "os" "os/exec" + "os/signal" "path" "strings" + "syscall" "github.com/fntlnz/mountinfo" "github.com/spf13/cobra" @@ -87,7 +90,29 @@ func (o *TraceRunnerOptions) Run() error { } } - c := exec.Command(o.bpftraceBinaryPath, programPath) + fmt.Println("if you have maps to print, send a SIGINT using Ctrl-C, if you want to interrupt the execution send SIGINT two times") + ctx, cancel := context.WithCancel(context.Background()) + sigCh := make(chan os.Signal, 1) + + signal.Notify(sigCh, os.Signal(syscall.SIGINT)) + + go func() { + killable := false + defer cancel() + M: + select { + case <-ctx.Done(): + return + case <-sigCh: + if !killable { + killable = true + goto M + } + return + } + }() + + c := exec.CommandContext(ctx, o.bpftraceBinaryPath, programPath) c.Stdout = os.Stdout c.Stdin = os.Stdin c.Stderr = os.Stderr