diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-06 16:33:02 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-06 16:33:02 +0200 |
| commit | e91e05e585940fa48767510b671ac6e9d8e4184d (patch) | |
| tree | 0c9c2d46e9fb759386ecfe47ca8faa6b5b70a47f | |
| parent | e2322eeb63efc492bbaf499427afd3592c4836be (diff) | |
refactor: remove dead tracepoint attach abstractions (task 383)
| -rw-r--r-- | internal/ior.go | 55 | ||||
| -rw-r--r-- | internal/ior_test.go | 108 |
2 files changed, 0 insertions, 163 deletions
diff --git a/internal/ior.go b/internal/ior.go index 7b411f2..2a5d1b9 100644 --- a/internal/ior.go +++ b/internal/ior.go @@ -27,14 +27,6 @@ import ( bpf "github.com/aquasecurity/libbpfgo" ) -type tracepointProgram interface { - attachTracepoint(category, name string) (tracepointLink, error) -} - -type tracepointLink interface { - Destroy() error -} - var ( runTraceFn = runTrace runTraceWithContextFn = runTraceWithContext @@ -46,10 +38,6 @@ var ( errRootPrivilegesRequired = errors.New("tracing requires root privileges (run with sudo)") ) -type tracepointModule interface { - getProgram(progName string) (tracepointProgram, error) -} - type libbpfTracepointProgram struct { prog *bpf.BPFProg } @@ -58,22 +46,10 @@ func (p libbpfTracepointProgram) AttachTracepoint(category, name string) (probem return p.prog.AttachTracepoint(category, name) } -func (p libbpfTracepointProgram) attachTracepoint(category, name string) (tracepointLink, error) { - return p.AttachTracepoint(category, name) -} - type libbpfTracepointModule struct { module *bpf.Module } -func (m libbpfTracepointModule) getProgram(progName string) (tracepointProgram, error) { - prog, err := m.module.GetProgram(progName) - if err != nil { - return nil, err - } - return libbpfTracepointProgram{prog: prog}, nil -} - func (m libbpfTracepointModule) GetProgram(progName string) (probemanager.Program, error) { prog, err := m.module.GetProgram(progName) if err != nil { @@ -82,37 +58,6 @@ func (m libbpfTracepointModule) GetProgram(progName string) (probemanager.Progra return libbpfTracepointProgram{prog: prog}, nil } -func attachTracepointsWith(module tracepointModule, shouldAttach func(string) bool, tracepointNames []string, verbose bool) error { - logln := func(...any) {} - logf := func(string, ...any) {} - if verbose { - logln = func(args ...any) { _, _ = fmt.Println(args...) } - logf = func(format string, args ...any) { _, _ = fmt.Printf(format, args...) } - } - - for _, name := range tracepointNames { - if !shouldAttach(name) { - continue - } - logln("Attaching tracepoint", name) - - prog, err := module.getProgram(fmt.Sprintf("handle_%s", name)) - if err != nil { - return fmt.Errorf("failed to get BPF program handle_%s: %w", name, err) - } - logln("Attached prog handle_", name) - - if _, err = prog.attachTracepoint("syscalls", name); err != nil { - // OK, older Kernel versions may not have this tracepoint! - logf("Failed to attach to %s tracepoint: %v, kernel version may be too old, skipping", name, err) - continue - } - logln("Attached tracepoint ", name) - } - - return nil -} - // Run is the main entry point for the ior binary. func Run() error { flags.PrintVersion() diff --git a/internal/ior_test.go b/internal/ior_test.go deleted file mode 100644 index 43e8091..0000000 --- a/internal/ior_test.go +++ /dev/null @@ -1,108 +0,0 @@ -package internal - -import ( - "errors" - "strings" - "testing" -) - -type fakeTracepointProgram struct { - attachCalls int - attachErr error -} - -type fakeTracepointLink struct{} - -func (fakeTracepointLink) Destroy() error { return nil } - -func (p *fakeTracepointProgram) attachTracepoint(_, _ string) (tracepointLink, error) { - p.attachCalls++ - if p.attachErr != nil { - return nil, p.attachErr - } - return fakeTracepointLink{}, nil -} - -type fakeTracepointModule struct { - getProgramCalls []string - getProgramErrs map[string]error - programs map[string]*fakeTracepointProgram -} - -func (m *fakeTracepointModule) getProgram(progName string) (tracepointProgram, error) { - m.getProgramCalls = append(m.getProgramCalls, progName) - if err, ok := m.getProgramErrs[progName]; ok { - return nil, err - } - if prog, ok := m.programs[progName]; ok { - return prog, nil - } - return nil, errors.New("missing program") -} - -func TestAttachTracepointsWithSkipsFilteredTracepoints(t *testing.T) { - module := &fakeTracepointModule{ - programs: map[string]*fakeTracepointProgram{ - "handle_sys_enter_read": {}, - "handle_sys_enter_write": {}, - }, - getProgramErrs: map[string]error{}, - } - - err := attachTracepointsWith(module, func(tracepoint string) bool { - return tracepoint == "sys_enter_read" - }, []string{"sys_enter_read", "sys_enter_write"}, false) - if err != nil { - t.Fatalf("attachTracepointsWith returned error: %v", err) - } - - if len(module.getProgramCalls) != 1 || module.getProgramCalls[0] != "handle_sys_enter_read" { - t.Fatalf("getProgram calls = %v, want only handle_sys_enter_read", module.getProgramCalls) - } - - if module.programs["handle_sys_enter_read"].attachCalls != 1 { - t.Fatalf("read attach calls = %d, want 1", module.programs["handle_sys_enter_read"].attachCalls) - } - if module.programs["handle_sys_enter_write"].attachCalls != 0 { - t.Fatalf("write attach calls = %d, want 0", module.programs["handle_sys_enter_write"].attachCalls) - } -} - -func TestAttachTracepointsWithReturnsErrorWhenProgramMissing(t *testing.T) { - module := &fakeTracepointModule{ - programs: map[string]*fakeTracepointProgram{}, - getProgramErrs: map[string]error{ - "handle_sys_enter_read": errors.New("not found"), - }, - } - - err := attachTracepointsWith(module, func(string) bool { return true }, []string{"sys_enter_read"}, false) - if err == nil { - t.Fatal("attachTracepointsWith returned nil error, want non-nil") - } - if !strings.Contains(err.Error(), "handle_sys_enter_read") { - t.Fatalf("error %q does not mention handle_sys_enter_read", err) - } -} - -func TestAttachTracepointsWithAttachFailureContinues(t *testing.T) { - module := &fakeTracepointModule{ - programs: map[string]*fakeTracepointProgram{ - "handle_sys_enter_read": {attachErr: errors.New("no tracepoint")}, - "handle_sys_enter_write": {}, - }, - getProgramErrs: map[string]error{}, - } - - err := attachTracepointsWith(module, func(string) bool { return true }, []string{"sys_enter_read", "sys_enter_write"}, false) - if err != nil { - t.Fatalf("attachTracepointsWith returned error: %v", err) - } - - if module.programs["handle_sys_enter_read"].attachCalls != 1 { - t.Fatalf("read attach calls = %d, want 1", module.programs["handle_sys_enter_read"].attachCalls) - } - if module.programs["handle_sys_enter_write"].attachCalls != 1 { - t.Fatalf("write attach calls = %d, want 1", module.programs["handle_sys_enter_write"].attachCalls) - } -} |
