Skip to content

Commit

Permalink
link: test - attach k(ret)probe to optimized kernel symbols containin…
Browse files Browse the repository at this point in the history
…g dots

These are all local symbols that visible in /proc/kallsyms and traceable
by kprobes. Make sure the library supports attaching to these.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Mar 30, 2022
1 parent 0814bf4 commit 5f13ae3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
63 changes: 46 additions & 17 deletions link/kprobe_test.go
Expand Up @@ -13,43 +13,72 @@ import (
"github.com/cilium/ebpf/internal/unix"
)

// Kernel symbol that should be present on all tested kernels.
// Global symbol, present on all tested kernels.
var ksym = "vprintk"

func TestKprobe(t *testing.T) {
c := qt.New(t)
// Collection of various symbols present in all tested kernels.
// Compiler optimizations result in different names for these symbols.
var symTests = []string{
"async_resume.cold", // marked with 'cold' gcc attribute, unlikely to be executed
"echo_char.isra.0", // function optimized by -fipa-sra
"get_buffer.constprop.0", // optimized function with constant operands
"unregister_kprobes.part.0", // function body that was split and partially inlined
}

func TestKprobe(t *testing.T) {
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")

k, err := Kprobe(ksym, prog, nil)
c.Assert(err, qt.IsNil)
defer k.Close()
for _, tt := range symTests {
t.Run(tt, func(t *testing.T) {
k, err := Kprobe(tt, prog, nil)
if err != nil {
t.Fatal(err)
}
defer k.Close()
})
}

testLink(t, k, prog)
c := qt.New(t)

k, err = Kprobe("bogus", prog, nil)
c.Assert(errors.Is(err, os.ErrNotExist), qt.IsTrue, qt.Commentf("got error: %s", err))
k, err := Kprobe("bogus", prog, nil)
c.Assert(err, qt.ErrorIs, os.ErrNotExist, qt.Commentf("got error: %s", err))
if k != nil {
k.Close()
}

k, err = Kprobe(ksym, prog, nil)
c.Assert(err, qt.IsNil)
defer k.Close()

testLink(t, k, prog)
}

func TestKretprobe(t *testing.T) {
c := qt.New(t)

prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")

k, err := Kretprobe(ksym, prog, nil)
c.Assert(err, qt.IsNil)
defer k.Close()
for _, tt := range symTests {
t.Run(tt, func(t *testing.T) {
k, err := Kretprobe(tt, prog, nil)
if err != nil {
t.Fatal(err)
}
defer k.Close()
})
}

testLink(t, k, prog)
c := qt.New(t)

k, err = Kretprobe("bogus", prog, nil)
c.Assert(errors.Is(err, os.ErrNotExist), qt.IsTrue, qt.Commentf("got error: %s", err))
k, err := Kretprobe("bogus", prog, nil)
c.Assert(err, qt.ErrorIs, os.ErrNotExist, qt.Commentf("got error: %s", err))
if k != nil {
k.Close()
}

k, err = Kretprobe(ksym, prog, nil)
c.Assert(err, qt.IsNil)
defer k.Close()

testLink(t, k, prog)
}

func TestKprobeErrors(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions link/link_test.go
Expand Up @@ -97,7 +97,7 @@ func testLink(t *testing.T, link Link, prog *ebpf.Program) {
}
defer os.RemoveAll(tmp)

t.Run("pinning", func(t *testing.T) {
t.Run("link/pinning", func(t *testing.T) {
path := filepath.Join(tmp, "link")
err = link.Pin(path)
testutils.SkipIfNotSupported(t, err)
Expand All @@ -123,7 +123,7 @@ func testLink(t *testing.T, link Link, prog *ebpf.Program) {
}
})

t.Run("update", func(t *testing.T) {
t.Run("link/update", func(t *testing.T) {
err := link.Update(prog)
testutils.SkipIfNotSupported(t, err)
if err != nil {
Expand All @@ -142,7 +142,7 @@ func testLink(t *testing.T, link Link, prog *ebpf.Program) {
}()
})

t.Run("link_info", func(t *testing.T) {
t.Run("link/info", func(t *testing.T) {
info, err := link.Info()
testutils.SkipIfNotSupported(t, err)
if err != nil {
Expand Down

0 comments on commit 5f13ae3

Please sign in to comment.