Skip to content

Commit 322dd79

Browse files
committed
fix(pkg/btf): fix FindBTFStruct to return first found btf type.
In case of multiple matches, the code expects the first one found with correct type to be used. Signed-off-by: Federico Di Pierro <[email protected]>
1 parent 1f4e0c0 commit 322dd79

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pkg/btf/btf.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package btf
77

88
import (
9+
"errors"
910
"fmt"
1011
"os"
1112
"path"
@@ -109,6 +110,30 @@ func FindBTFStruct(name string) (*btf.Struct, error) {
109110
}
110111

111112
err = spec.TypeByName(name, &ty)
113+
if err != nil && errors.Is(err, btf.ErrMultipleMatches) {
114+
logger.GetLogger().Warn("Multiple matches found in BTF spec; using first one", "struct", name)
115+
// In case of multiple matches, fallback at using first available
116+
// NOTE: we can skip all error checking here
117+
// as it was already done for us by spec.TypeByName()
118+
typeInterface := reflect.TypeOf((*btf.Type)(nil)).Elem()
119+
typValue := reflect.ValueOf(&ty)
120+
typPtr := typValue.Elem()
121+
types, _ := spec.AnyTypesByName(name)
122+
wanted := typPtr.Type()
123+
if wanted == typeInterface {
124+
// This is *Type. Unwrap the value's type.
125+
wanted = typPtr.Elem().Type()
126+
}
127+
for _, typ := range types {
128+
if reflect.TypeOf(typ) != wanted {
129+
continue
130+
}
131+
typPtr.Set(reflect.ValueOf(typ))
132+
// reset err
133+
err = nil
134+
break
135+
}
136+
}
112137
return ty, err
113138
}
114139

pkg/sensors/tracing/generic.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package tracing
77

88
import (
9-
"errors"
109
"fmt"
1110
"strings"
1211

@@ -74,7 +73,7 @@ func resolveBTFArg(hook string, arg *v1alpha1.KProbeArg, tp bool) (*ebtf.Type, [
7473
// - real argument value
7574
if hasCurrentTaskSource(arg) {
7675
st, err := btf.FindBTFStruct("task_struct")
77-
if err != nil && !errors.Is(err, ebtf.ErrMultipleMatches) {
76+
if err != nil {
7877
return nil, [api.MaxBTFArgDepth]api.ConfigBTFArg{}, err
7978
}
8079
ty = ebtf.Type(st)

0 commit comments

Comments
 (0)