MCPcopy
hub / github.com/cilium/ebpf / findTargetInKernel

Function findTargetInKernel

prog.go:1247–1271  ·  view source on GitHub ↗

findTargetInKernel attempts to find a named type in the current kernel. target will point at the found type after a successful call. Searches both vmlinux and any loaded modules. Returns a non-nil handle if the type was found in a module, [btf.ErrNotFound] if the type wasn't found or if BTF is not

(typeName string, target *btf.Type, cache *btf.Cache)

Source from the content-addressed store, hash-verified

1245// Returns a non-nil handle if the type was found in a module, [btf.ErrNotFound]
1246// if the type wasn't found or if BTF is not enabled.
1247func findTargetInKernel(typeName string, target *btf.Type, cache *btf.Cache) (*btf.Spec, *btf.Handle, error) {
1248 kernelSpec, err := cache.Kernel()
1249 if err != nil {
1250 return nil, nil, fmt.Errorf("load kernel spec: %w (%w)", btf.ErrNotFound, err)
1251 }
1252
1253 err = kernelSpec.TypeByName(typeName, target)
1254 if errors.Is(err, btf.ErrNotFound) {
1255 spec, module, err := findTargetInModule(typeName, target, cache)
1256 if err != nil {
1257 // EPERM may be returned when we do not have CAP_SYS_ADMIN.
1258 // Wrap error with btf.ErrNotFound so callers can handle it accordingly.
1259 if errors.Is(err, unix.EPERM) {
1260 return spec, nil, fmt.Errorf("find target in modules: %w (%w)", btf.ErrNotFound, err)
1261 }
1262
1263 return nil, nil, fmt.Errorf("find target in modules: %w", err)
1264 }
1265 return spec, module, nil
1266 }
1267 if err != nil {
1268 return nil, nil, fmt.Errorf("find target in vmlinux: %w", err)
1269 }
1270 return kernelSpec, nil, err
1271}
1272
1273// findTargetInModule attempts to find a named type in any loaded module.
1274//

Callers 5

helpers_test.goFile · 0.85
structOpsFindTargetFunction · 0.85
newProgramWithOptionsFunction · 0.85
fixupKfuncsFunction · 0.85

Calls 4

findTargetInModuleFunction · 0.85
TypeByNameMethod · 0.80
KernelMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…