Skip to content

Commit 7af9797

Browse files
committed
link: return os.ErrNotExist when symbol doesn't exist
Some of the tracing related links currently return ErrNotSupported when trying to attach to a non-existant symbol. This is misleading. Consistently return os.ErrNotExist in such cases.
1 parent ffcb036 commit 7af9797

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

link/kprobe.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,7 @@ func tracefsProbe(typ probeType, symbol, path string, offset uint64, ret bool) (
266266
if err == nil {
267267
return nil, fmt.Errorf("trace event already exists: %s/%s", group, symbol)
268268
}
269-
// The read is expected to fail with ErrNotSupported due to a non-existing event.
270-
if err != nil && !errors.Is(err, ErrNotSupported) {
269+
if err != nil && !errors.Is(err, os.ErrNotExist) {
271270
return nil, fmt.Errorf("checking trace event %s/%s: %w", group, symbol, err)
272271
}
273272

@@ -346,7 +345,7 @@ func createTraceFSProbeEvent(typ probeType, group, symbol, path string, offset u
346345
// when trying to create a kretprobe for a missing symbol. Make sure ENOENT
347346
// is returned to the caller.
348347
if errors.Is(err, os.ErrNotExist) || errors.Is(err, unix.EINVAL) {
349-
return fmt.Errorf("kernel symbol %s not found: %w", symbol, os.ErrNotExist)
348+
return fmt.Errorf("symbol %s not found: %w", symbol, os.ErrNotExist)
350349
}
351350
if err != nil {
352351
return fmt.Errorf("writing '%s' to '%s': %w", pe, typ.EventsPath(), err)

link/perf_event.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ func unsafeStringPtr(str string) (unsafe.Pointer, error) {
207207
// group and name must be alphanumeric or underscore, as required by the kernel.
208208
func getTraceEventID(group, name string) (uint64, error) {
209209
tid, err := uint64FromFile(tracefsPath, "events", group, name, "id")
210-
if errors.Is(err, ErrNotSupported) {
211-
return 0, fmt.Errorf("trace event %s/%s: %w", group, name, ErrNotSupported)
210+
if errors.Is(err, os.ErrNotExist) {
211+
return 0, fmt.Errorf("trace event %s/%s: %w", group, name, os.ErrNotExist)
212212
}
213213
if err != nil {
214214
return 0, fmt.Errorf("reading trace event ID of %s/%s: %w", group, name, err)
@@ -219,9 +219,11 @@ func getTraceEventID(group, name string) (uint64, error) {
219219

220220
// getPMUEventType reads a Performance Monitoring Unit's type (numeric identifier)
221221
// from /sys/bus/event_source/devices/<pmu>/type.
222+
//
223+
// Returns ErrNotSupported if the pmu type is not supported.
222224
func getPMUEventType(typ probeType) (uint64, error) {
223225
et, err := uint64FromFile("/sys/bus/event_source/devices", typ.String(), "type")
224-
if errors.Is(err, ErrNotSupported) {
226+
if errors.Is(err, os.ErrNotExist) {
225227
return 0, fmt.Errorf("pmu type %s: %w", typ, ErrNotSupported)
226228
}
227229
if err != nil {
@@ -255,22 +257,13 @@ func openTracepointPerfEvent(tid uint64) (*internal.FD, error) {
255257
// and joined onto base. Returns error if base no longer prefixes the path after
256258
// joining all components.
257259
func uint64FromFile(base string, path ...string) (uint64, error) {
258-
259-
// Resolve leaf path separately for error feedback. Makes the join onto
260-
// base more readable (can't mix with variadic args).
261260
l := filepath.Join(path...)
262-
263261
p := filepath.Join(base, l)
264262
if !strings.HasPrefix(p, base) {
265263
return 0, fmt.Errorf("path '%s' attempts to escape base path '%s': %w", l, base, errInvalidInput)
266264
}
267265

268266
data, err := ioutil.ReadFile(p)
269-
if os.IsNotExist(err) {
270-
// Only echo leaf path, the base path can be prepended at the call site
271-
// if more verbosity is required.
272-
return 0, fmt.Errorf("symbol %s: %w", l, ErrNotSupported)
273-
}
274267
if err != nil {
275268
return 0, fmt.Errorf("reading file %s: %w", p, err)
276269
}

link/perf_event_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package link
22

33
import (
44
"errors"
5+
"os"
56
"testing"
67

78
"github.com/cilium/ebpf/internal/testutils"
@@ -38,8 +39,8 @@ func TestTraceReadID(t *testing.T) {
3839
}
3940

4041
_, err = uint64FromFile("/base/path/not", "../not/escaped")
41-
if !errors.Is(err, ErrNotSupported) {
42-
t.Errorf("expected error %s, got: %s", ErrNotSupported, err)
42+
if !errors.Is(err, os.ErrNotExist) {
43+
t.Errorf("expected os.ErrNotExist, got: %s", err)
4344
}
4445
}
4546

link/tracepoint_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package link
22

33
import (
44
"errors"
5+
"os"
56
"testing"
67

78
"github.com/cilium/ebpf"
89
"github.com/cilium/ebpf/asm"
9-
"github.com/cilium/ebpf/internal"
1010
"github.com/cilium/ebpf/internal/testutils"
1111
"github.com/cilium/ebpf/internal/unix"
1212

@@ -48,6 +48,19 @@ func TestTracepoint(t *testing.T) {
4848
}
4949
}
5050

51+
func TestTracepointMissing(t *testing.T) {
52+
prog, err := ebpf.NewProgram(&tracepointSpec)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
defer prog.Close()
57+
58+
_, err = Tracepoint("missing", "foobazbar", prog)
59+
if !errors.Is(err, os.ErrNotExist) {
60+
t.Error("Expected os.ErrNotExist, got", err)
61+
}
62+
}
63+
5164
func TestTracepointErrors(t *testing.T) {
5265
c := qt.New(t)
5366

@@ -72,8 +85,8 @@ func TestTraceGetEventID(t *testing.T) {
7285
}
7386

7487
_, err = getTraceEventID("totally", "bogus")
75-
if !errors.Is(err, internal.ErrNotSupported) {
76-
t.Fatal("Doesn't return ErrNotSupported")
88+
if !errors.Is(err, os.ErrNotExist) {
89+
t.Fatal("Expected os.ErrNotExist, got", err)
7790
}
7891
}
7992

0 commit comments

Comments
 (0)