Skip to content

Commit

Permalink
Merge pull request #35 from iovisor/fix/trace-attach
Browse files Browse the repository at this point in the history
fix(cmd/tracerunner): fix signal handling in trace attach
  • Loading branch information
fntlnz authored Jan 14, 2019
2 parents 3392e93 + bb9879d commit 889b021
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/cmd/tracerunner.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -87,7 +90,32 @@ func (o *TraceRunnerOptions) Run() error {
}
}

c := exec.Command(o.bpftraceBinaryPath, programPath)
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)

signal.Notify(sigCh, os.Signal(syscall.SIGINT))

go func() {
killable := false
defer cancel()

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
}
}
}()

c := exec.CommandContext(ctx, o.bpftraceBinaryPath, programPath)
c.Stdout = os.Stdout
c.Stdin = os.Stdin
c.Stderr = os.Stderr
Expand Down

0 comments on commit 889b021

Please sign in to comment.