Skip to content

Instantly share code, notes, and snippets.

@grantseltzer grantseltzer/test.go
Created May 11, 2020

Embed
What would you like to do?
package main
//go:noinline
func parseMe(a int, b bool, c float32) {
// some code
}
func main() {
parseMe(1, true, 96.69)
}
package main
import (
"fmt"
"log"
"os"
"os/signal"
"github.com/iovisor/gobpf/bcc"
)
const eBPF_Program = `
#include <uapi/linux/ptrace.h>
BPF_PERF_OUTPUT(events);
inline int get_arguments(struct pt_regs *ctx) {
void* stackAddr = (void*)ctx->sp;
long argument1;
bpf_probe_read(&argument1, sizeof(argument1), stackAddr+8);
events.perf_submit(ctx, &argument1, sizeof(argument1));
char argument2;
bpf_probe_read(&argument2, sizeof(argument2), stackAddr+16);
events.perf_submit(ctx, &argument2, sizeof(argument2));
float argument3;
bpf_probe_read(&argument3, sizeof(argument3), stackAddr+17);
events.perf_submit(ctx, &argument3, sizeof(argument3));
}
`
func main() {
bpfModule := bcc.NewModule(eBPF_Program, []string{})
uprobeFd, err := bpfModule.LoadUprobe("get_arguments")
if err != nil {
log.Fatal(err)
}
err = bpfModule.AttachUprobe(os.Args[1], "main.parseMe", 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
You can’t perform that action at this time.