-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftest: add selftest for AttachKprobeOffset
- Loading branch information
1 parent
a879333
commit 1f069b9
Showing
7 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../common/Makefile |
This file contains 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
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 |
This file contains 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
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= |
This file contains 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
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"; |
This file contains 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
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") | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../common/run.sh |
This file contains 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