-
-
Save developer-guy/37ca8ece310d2034f15049b15d08743d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"github.com/iovisor/gobpf/bcc" | |
) | |
const eBPF_Program = ` | |
#include <uapi/linux/ptrace.h> | |
#include <linux/string.h> | |
BPF_PERF_OUTPUT(events); | |
inline int function_was_called(struct pt_regs *ctx) { | |
char x[29] = "Hey, the handler was called!"; | |
events.perf_submit(ctx, &x, sizeof(x)); | |
return 0; | |
} | |
` | |
func main() { | |
bpfModule := bcc.NewModule(eBPF_Program, []string{}) | |
uprobeFd, err := bpfModule.LoadUprobe("function_was_called") | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = bpfModule.AttachUprobe(os.Args[1], "main.handlerFunction", uprobeFd, -1) | |
if err != nil { | |
log.Fatal(err) | |
} | |
table := bcc.NewTable(bpfModule.TableId("events"), bpfModule) | |
channel := make(chan []byte) | |
perfMap, err := bcc.InitPerfMap(table, channel) | |
if err != nil { | |
log.Fatal(err) | |
} | |
c := make(chan os.Signal, 1) | |
signal.Notify(c, os.Interrupt) | |
go func() { | |
for { | |
value := <-channel | |
fmt.Println(string(value)) | |
} | |
}() | |
perfMap.Start() | |
<-c | |
perfMap.Stop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment