From b23e1ac165a3cb90e1f2fc31a58ad387141bd1ce Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Mon, 14 Jan 2019 16:31:18 +0100 Subject: [PATCH 1/2] fix(cmd/tracerunner): fix signal handling in trace attach Signed-off-by: Lorenzo Fontana --- pkg/cmd/tracerunner.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) 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 From bb9879dab9900ccd80de6e27bc68cae6bce58b3c Mon Sep 17 00:00:00 2001 From: Lorenzo Fontana Date: Mon, 14 Jan 2019 19:08:31 +0100 Subject: [PATCH 2/2] fix(cmd/tracerunner): better printing for attach Signed-off-by: Lorenzo Fontana --- pkg/cmd/tracerunner.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/tracerunner.go b/pkg/cmd/tracerunner.go index 0900138a..8203ba12 100644 --- a/pkg/cmd/tracerunner.go +++ b/pkg/cmd/tracerunner.go @@ -90,7 +90,7 @@ func (o *TraceRunnerOptions) Run() error { } } - 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") + fmt.Println("if your program has 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) @@ -99,16 +99,19 @@ func (o *TraceRunnerOptions) Run() error { go func() { killable := false defer cancel() - M: - select { - case <-ctx.Done(): - return - case <-sigCh: - if !killable { - killable = true - goto M + + for { + select { + case <-ctx.Done(): + return + case <-sigCh: + if !killable { + killable = true + fmt.Println("\nfirst SIGINT received, now if your program had maps and did not free them it should print them out") + continue + } + return } - return } }()