github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/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/testutils"
     8  )
     9  
    10  func TestFreplace(t *testing.T) {
    11  	testutils.SkipOnOldKernel(t, "5.10", "freplace")
    12  
    13  	file := testutils.NativeFile(t, "../testdata/freplace-%s.elf")
    14  	spec, err := ebpf.LoadCollectionSpec(file)
    15  	if err != nil {
    16  		t.Fatal("Can't parse ELF:", err)
    17  	}
    18  
    19  	target, err := ebpf.NewProgram(spec.Programs["sched_process_exec"])
    20  	testutils.SkipIfNotSupported(t, err)
    21  	if err != nil {
    22  		t.Fatal("Can't create target program:", err)
    23  	}
    24  	defer target.Close()
    25  
    26  	// Test attachment specified at load time
    27  	spec.Programs["replacement"].AttachTarget = target
    28  	replacement, err := ebpf.NewProgram(spec.Programs["replacement"])
    29  	testutils.SkipIfNotSupported(t, err)
    30  	if err != nil {
    31  		t.Fatal("Can't create replacement program:", err)
    32  	}
    33  	defer replacement.Close()
    34  
    35  	freplace, err := AttachFreplace(nil, "", replacement)
    36  	testutils.SkipIfNotSupported(t, err)
    37  	if err != nil {
    38  		t.Fatal("Can't create freplace:", err)
    39  	}
    40  
    41  	testLink(t, freplace, replacement)
    42  }
    43  
    44  func TestFentryFexit(t *testing.T) {
    45  	testutils.SkipOnOldKernel(t, "5.5", "fentry")
    46  
    47  	spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "../testdata/fentry_fexit-%s.elf"))
    48  	if err != nil {
    49  		t.Fatal("Can't parse ELF:", err)
    50  	}
    51  
    52  	target, err := ebpf.NewProgram(spec.Programs["target"])
    53  	testutils.SkipIfNotSupported(t, err)
    54  	if err != nil {
    55  		t.Fatal("Can't create target program:", err)
    56  	}
    57  	defer target.Close()
    58  
    59  	for _, name := range []string{"trace_on_entry", "trace_on_exit"} {
    60  		progSpec := spec.Programs[name]
    61  		t.Run(name, func(t *testing.T) {
    62  			progSpec.AttachTarget = target
    63  
    64  			prog, err := ebpf.NewProgram(progSpec)
    65  			if err != nil {
    66  				t.Fatal(err)
    67  			}
    68  			defer prog.Close()
    69  
    70  			t.Run("link", func(t *testing.T) {
    71  				testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING")
    72  
    73  				tracingLink, err := AttachTracing(TracingOptions{
    74  					Program: prog,
    75  				})
    76  				if err != nil {
    77  					t.Fatal("Can't attach tracing:", err)
    78  				}
    79  				defer tracingLink.Close()
    80  
    81  				testLink(t, tracingLink, prog)
    82  			})
    83  
    84  		})
    85  	}
    86  }
    87  
    88  func TestTracing(t *testing.T) {
    89  	testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING")
    90  
    91  	tests := []struct {
    92  		name                             string
    93  		attachTo                         string
    94  		programType                      ebpf.ProgramType
    95  		programAttachType, attachTypeOpt ebpf.AttachType
    96  		cookie                           uint64
    97  	}{
    98  		{
    99  			name:              "AttachTraceFEntry",
   100  			attachTo:          "inet_dgram_connect",
   101  			programType:       ebpf.Tracing,
   102  			programAttachType: ebpf.AttachTraceFEntry,
   103  		},
   104  		{
   105  			name:              "AttachTraceFEntry",
   106  			attachTo:          "inet_dgram_connect",
   107  			programType:       ebpf.Tracing,
   108  			programAttachType: ebpf.AttachTraceFEntry,
   109  			attachTypeOpt:     ebpf.AttachTraceFEntry,
   110  			cookie:            1,
   111  		},
   112  		{
   113  			name:              "AttachTraceFEntry",
   114  			attachTo:          "inet_dgram_connect",
   115  			programType:       ebpf.Tracing,
   116  			programAttachType: ebpf.AttachTraceFEntry,
   117  		},
   118  		{
   119  			name:              "AttachTraceFExit",
   120  			attachTo:          "inet_dgram_connect",
   121  			programType:       ebpf.Tracing,
   122  			programAttachType: ebpf.AttachTraceFExit,
   123  		},
   124  		{
   125  			name:              "AttachModifyReturn",
   126  			attachTo:          "bpf_modify_return_test",
   127  			programType:       ebpf.Tracing,
   128  			programAttachType: ebpf.AttachModifyReturn,
   129  		},
   130  		{
   131  			name:              "AttachTraceRawTp",
   132  			attachTo:          "kfree_skb",
   133  			programType:       ebpf.Tracing,
   134  			programAttachType: ebpf.AttachTraceRawTp,
   135  		},
   136  	}
   137  
   138  	for _, tt := range tests {
   139  		t.Run(tt.name, func(t *testing.T) {
   140  			prog := mustLoadProgram(t, tt.programType, tt.programAttachType, tt.attachTo)
   141  
   142  			opts := TracingOptions{Program: prog, AttachType: tt.attachTypeOpt, Cookie: tt.cookie}
   143  			link, err := AttachTracing(opts)
   144  			testutils.SkipIfNotSupported(t, err)
   145  			if err != nil {
   146  				t.Fatal(err)
   147  			}
   148  			testLink(t, link, prog)
   149  			if err = link.Close(); err != nil {
   150  				t.Fatal(err)
   151  			}
   152  		})
   153  	}
   154  }
   155  
   156  func TestLSM(t *testing.T) {
   157  	testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING")
   158  
   159  	prog := mustLoadProgram(t, ebpf.LSM, ebpf.AttachLSMMac, "file_mprotect")
   160  
   161  	link, err := AttachLSM(LSMOptions{Program: prog})
   162  	testutils.SkipIfNotSupported(t, err)
   163  	if err != nil {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	testLink(t, link, prog)
   168  }