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)
| 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. |
| 1247 | func 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 | // |
no test coverage detected
searching dependent graphs…