Skip to content

Commit

Permalink
selftest: add selftest for AttachKprobeOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldtinoco committed Jan 11, 2024
1 parent a879333 commit 1f069b9
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions selftest/tracing-by-offset/Makefile
14 changes: 14 additions & 0 deletions selftest/tracing-by-offset/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/aquasecurity/libbpfgo/selftest/tracing-by-offset

go 1.18

require (
github.com/aquasecurity/libbpfgo v0.4.7-libbpf-1.2.0-b2e29a1
github.com/aquasecurity/libbpfgo/helpers v0.4.5
)

require golang.org/x/sys v0.7.0 // indirect

replace github.com/aquasecurity/libbpfgo => ../../

replace github.com/aquasecurity/libbpfgo/helpers => ../../helpers
6 changes: 6 additions & 0 deletions selftest/tracing-by-offset/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
14 changes: 14 additions & 0 deletions selftest/tracing-by-offset/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//+build ignore

#include <vmlinux.h>

#include <bpf/bpf_helpers.h>

SEC("kprobe/sys_mmap")
int kprobe__sys_mmap(struct pt_regs *ctx)
{
bpf_printk("Hello, World!\n");
return 0;
}

char LICENSE[] SEC("license") = "Dual BSD/GPL";
70 changes: 70 additions & 0 deletions selftest/tracing-by-offset/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import "C"

import (
"os"
"runtime"
"time"

"fmt"
"syscall"

bpf "github.com/aquasecurity/libbpfgo"
"github.com/aquasecurity/libbpfgo/helpers"
)

func main() {
funcName := fmt.Sprintf("__%s_sys_mmap", ksymArch())

kst, err := helpers.NewKernelSymbolTable()
if err != nil {
fmt.Fprintln(os.Stderr, "NewKernelSymbolTable() failed: %v", err)
os.Exit(-1)
}

funcSymbol, err := kst.GetSymbolByName(funcName)
if err != nil {
fmt.Fprintln(os.Stderr, "Expected to find symbol %s, but it was not found", funcSymbol)
os.Exit(-1)
}

bpfModule, err := bpf.NewModuleFromFile("main.bpf.o")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
defer bpfModule.Close()

bpfModule.BPFLoadObject()
prog, err := bpfModule.GetProgram("kprobe__sys_mmap")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

_, err = prog.AttachKprobeOffset(funcSymbol[0].Address)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

go func() {
time.Sleep(time.Second)
syscall.Mmap(999, 999, 999, 1, 1)
syscall.Mmap(999, 999, 999, 1, 1)
}()

time.Sleep(time.Second * 2)
}

func ksymArch() string {
switch runtime.GOARCH {
case "amd64":
return "x64"
case "arm64":
return "arm64"
default:
panic("unsupported architecture")
}
}
1 change: 1 addition & 0 deletions selftest/tracing-by-offset/run.sh
2 changes: 1 addition & 1 deletion selftest/tracing/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
os.Exit(-1)
}

if sym[0].Address == 0 || sym[0].Name == "" {
if sym[0].Address == 0 && sym[0].Name == "" {
fmt.Fprintln(os.Stderr, "could not find symbol to attach to")
os.Exit(-1)
}
Expand Down

0 comments on commit 1f069b9

Please sign in to comment.