github.com/cilium/ebpf@v0.10.0/link/tracing_test.go (about) 1 package link 2 3 import ( 4 "testing" 5 6 "github.com/cilium/ebpf" 7 "github.com/cilium/ebpf/internal" 8 "github.com/cilium/ebpf/internal/testutils" 9 ) 10 11 func TestFreplace(t *testing.T) { 12 testutils.SkipOnOldKernel(t, "5.10", "freplace") 13 14 testutils.Files(t, testutils.Glob(t, "../testdata/freplace-*.elf"), func(t *testing.T, file string) { 15 spec, err := ebpf.LoadCollectionSpec(file) 16 if err != nil { 17 t.Fatal("Can't parse ELF:", err) 18 } 19 20 if spec.ByteOrder != internal.NativeEndian { 21 return 22 } 23 24 target, err := ebpf.NewProgram(spec.Programs["sched_process_exec"]) 25 testutils.SkipIfNotSupported(t, err) 26 if err != nil { 27 t.Fatal("Can't create target program:", err) 28 } 29 defer target.Close() 30 31 // Test attachment specified at load time 32 spec.Programs["replacement"].AttachTarget = target 33 replacement, err := ebpf.NewProgram(spec.Programs["replacement"]) 34 testutils.SkipIfNotSupported(t, err) 35 if err != nil { 36 t.Fatal("Can't create replacement program:", err) 37 } 38 defer replacement.Close() 39 40 freplace, err := AttachFreplace(nil, "", replacement) 41 testutils.SkipIfNotSupported(t, err) 42 if err != nil { 43 t.Fatal("Can't create freplace:", err) 44 } 45 46 testLink(t, freplace, replacement) 47 }) 48 } 49 50 func TestTracing(t *testing.T) { 51 testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING") 52 53 tests := []struct { 54 name string 55 attachTo string 56 programType ebpf.ProgramType 57 attachType ebpf.AttachType 58 }{ 59 { 60 name: "AttachTraceFEntry", 61 attachTo: "inet_dgram_connect", 62 programType: ebpf.Tracing, 63 attachType: ebpf.AttachTraceFEntry, 64 }, 65 { 66 name: "AttachTraceFExit", 67 attachTo: "inet_dgram_connect", 68 programType: ebpf.Tracing, 69 attachType: ebpf.AttachTraceFExit, 70 }, 71 { 72 name: "AttachModifyReturn", 73 attachTo: "bpf_modify_return_test", 74 programType: ebpf.Tracing, 75 attachType: ebpf.AttachModifyReturn, 76 }, 77 { 78 name: "AttachTraceRawTp", 79 attachTo: "kfree_skb", 80 programType: ebpf.Tracing, 81 attachType: ebpf.AttachTraceRawTp, 82 }, 83 } 84 85 for _, tt := range tests { 86 t.Run(tt.name, func(t *testing.T) { 87 prog := mustLoadProgram(t, tt.programType, tt.attachType, tt.attachTo) 88 89 link, err := AttachTracing(TracingOptions{Program: prog}) 90 testutils.SkipIfNotSupported(t, err) 91 if err != nil { 92 t.Fatal(err) 93 } 94 95 testLink(t, link, prog) 96 }) 97 } 98 } 99 100 func TestLSM(t *testing.T) { 101 testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING") 102 103 prog := mustLoadProgram(t, ebpf.LSM, ebpf.AttachLSMMac, "file_mprotect") 104 105 link, err := AttachLSM(LSMOptions{Program: prog}) 106 testutils.SkipIfNotSupported(t, err) 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 testLink(t, link, prog) 112 }