Skip to content

Commit

Permalink
selftest: add log-callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
geyslan committed Feb 6, 2023
1 parent 56803f4 commit 761bb03
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions selftest/log-callbacks/Makefile
9 changes: 9 additions & 0 deletions selftest/log-callbacks/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/aquasecurity/libbpfgo/selftest/log-callbacks

go 1.18

require github.com/aquasecurity/libbpfgo v0.2.1-libbpf-0.4.0

require golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect

replace github.com/aquasecurity/libbpfgo => ../../
12 changes: 12 additions & 0 deletions selftest/log-callbacks/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
11 changes: 11 additions & 0 deletions selftest/log-callbacks/main.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//+build ignore
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>

SEC("kprobe/sys_mmap")
int kprobe__sys_mmap(struct pt_regs *ctx)
{
return 0;
}

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

import "C"

import (
"os"
"strings"

"fmt"

bpf "github.com/aquasecurity/libbpfgo"
)

var logOutput []string

func log(level int, msg string, keyValues ...interface{}) {
logOutput = append(logOutput, msg)
}

func main() {
filterMatch := "found program 'kprobe__sys_mmap'"
bpf.SetLoggerCbs(bpf.Callbacks{
Log: log, // use log() as a handler for libbpf outputs that are not excluded by LogFilters
LogFilters: []func(libLevel int, msg string) bool{
func(libLevel int, msg string) bool {
// filter all output but containing "found program 'kprobe__sys_mmap'"
return !strings.Contains(msg, filterMatch)
},
},
})

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

if len(logOutput) != 1 {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Log ouput should contain only one output matching the string: %s", filterMatch))
fmt.Fprintln(os.Stderr, fmt.Sprintf("Log ouput: %v", logOutput))
os.Exit(-1)
}
}
1 change: 1 addition & 0 deletions selftest/log-callbacks/run.sh

0 comments on commit 761bb03

Please sign in to comment.