1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
package btf_test
import (
"fmt"
"testing"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal/testutils"
)
func TestHandleIterator(t *testing.T) {
// There is no guarantee that there is a BTF ID allocated, but loading a module
// triggers loading vmlinux.
// See https://github.com/torvalds/linux/commit/5329722057d41aebc31e391907a501feaa42f7d9
testutils.SkipOnOldKernel(t, "5.11", "vmlinux BTF ID")
it := new(btf.HandleIterator)
defer it.Handle.Close()
if !it.Next() {
t.Fatalf("No BTF loaded")
}
if it.Handle == nil {
t.Fatal("Next doesn't assign handle")
}
prev := it.ID
for it.Next() {
// Iterate all loaded BTF.
if it.Handle == nil {
t.Fatal("Next doesn't assign handle")
}
if it.ID == prev {
t.Fatal("Iterator doesn't advance ID")
}
prev = it.ID
}
if err := it.Err(); err != nil {
t.Fatal("Iteration returned an error:", err)
}
if it.Handle != nil {
t.Fatal("Next doesn't clean up handle on last iteration")
}
if prev != it.ID {
t.Fatal("Next changes ID on last iteration")
}
}
func TestParseModuleSplitSpec(t *testing.T) {
// See TestNewHandleFromID for reasoning.
testutils.SkipOnOldKernel(t, "5.11", "vmlinux BTF ID")
module, err := btf.FindHandle(func(info *btf.HandleInfo) bool {
if info.IsModule() {
t.Log("Using module", info.Name)
return true
}
return false
})
if err != nil {
t.Fatal(err)
}
defer module.Close()
vmlinux, err := btf.FindHandle(func(info *btf.HandleInfo) bool {
return info.IsVmlinux()
})
if err != nil {
t.Fatal(err)
}
defer vmlinux.Close()
base, err := vmlinux.Spec(nil)
if err != nil {
t.Fatal(err)
}
_, err = module.Spec(base)
if err != nil {
t.Fatal("Parse module BTF:", err)
}
}
func ExampleHandleIterator() {
it := new(btf.HandleIterator)
defer it.Handle.Close()
for it.Next() {
info, err := it.Handle.Info()
if err != nil {
panic(err)
}
fmt.Printf("Found handle with ID %d and name %s\n", it.ID, info.Name)
}
if err := it.Err(); err != nil {
panic(err)
}
}
|