| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"os" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/cilium/ebpf" |
| 11 |
"github.com/cilium/ebpf/internal/testutils" |
| 12 |
"github.com/cilium/ebpf/internal/unix" |
| 13 |
|
| 14 |
"github.com/go-quicktest/qt" |
| 15 |
) |
| 16 |
|
| 17 |
func TestTracepoint(t *testing.T) { |
| 18 |
|
| 19 |
testutils.SkipOnOldKernel(t, "4.7", "tracepoint support") |
| 20 |
|
| 21 |
prog := mustLoadProgram(t, ebpf.TracePoint, 0, "") |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
tp, err := Tracepoint("printk", "console", prog, nil) |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
|
| 30 |
if err := tp.Close(); err != nil { |
| 31 |
t.Error("closing tracepoint:", err) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func TestTracepointMissing(t *testing.T) { |
| 36 |
|
| 37 |
testutils.SkipOnOldKernel(t, "4.7", "tracepoint support") |
| 38 |
|
| 39 |
prog := mustLoadProgram(t, ebpf.TracePoint, 0, "") |
| 40 |
|
| 41 |
_, err := Tracepoint("missing", "foobazbar", prog, nil) |
| 42 |
if !errors.Is(err, os.ErrNotExist) { |
| 43 |
t.Error("Expected os.ErrNotExist, got", err) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestTracepointErrors(t *testing.T) { |
| 48 |
|
| 49 |
_, err := Tracepoint("", "", nil, nil) |
| 50 |
qt.Assert(t, qt.ErrorIs(err, errInvalidInput)) |
| 51 |
|
| 52 |
_, err = Tracepoint("_", "_", nil, nil) |
| 53 |
qt.Assert(t, qt.ErrorIs(err, errInvalidInput)) |
| 54 |
|
| 55 |
_, err = Tracepoint(".", "+", &ebpf.Program{}, nil) |
| 56 |
qt.Assert(t, qt.ErrorIs(err, errInvalidInput)) |
| 57 |
|
| 58 |
_, err = Tracepoint("foo", "bar", &ebpf.Program{}, nil) |
| 59 |
qt.Assert(t, qt.ErrorIs(err, errInvalidInput)) |
| 60 |
} |
| 61 |
|
| 62 |
func TestTracepointProgramCall(t *testing.T) { |
| 63 |
|
| 64 |
testutils.SkipOnOldKernel(t, "4.14", "syscalls tracepoint support") |
| 65 |
|
| 66 |
m, p := newUpdaterMapProg(t, ebpf.TracePoint, 0) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
tp, err := Tracepoint("syscalls", "sys_enter_getpid", p, nil) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
unix.Getpid() |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
assertMapValueGE(t, m, 0, 1) |
| 81 |
|
| 82 |
|
| 83 |
if err := tp.Close(); err != nil { |
| 84 |
t.Fatal(err) |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
if err := m.Update(uint32(0), uint32(0), ebpf.UpdateExist); err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
unix.Getpid() |
| 94 |
|
| 95 |
|
| 96 |
assertMapValue(t, m, 0, 0) |
| 97 |
} |
| 98 |
|
| 99 |
func TestTracepointInfo(t *testing.T) { |
| 100 |
testutils.SkipOnOldKernel(t, "6.6", "bpf_link_info_perf_event") |
| 101 |
|
| 102 |
prog := mustLoadProgram(t, ebpf.TracePoint, 0, "") |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
tp, err := Tracepoint("printk", "console", prog, nil) |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
defer tp.Close() |
| 111 |
|
| 112 |
info, err := tp.Info() |
| 113 |
if err != nil { |
| 114 |
t.Fatal(err) |
| 115 |
} |
| 116 |
tpInfo := info.PerfEvent().Tracepoint() |
| 117 |
qt.Assert(t, qt.Equals(tpInfo.Tracepoint, "console")) |
| 118 |
} |