LoadKernelSpec returns the current kernel's BTF information. Defaults to /sys/kernel/btf/vmlinux and falls back to scanning the file system for vmlinux ELFs. Returns an error wrapping [ErrNotSupported] if BTF is not enabled. Consider using [Cache] instead.
()
| 24 | // |
| 25 | // Consider using [Cache] instead. |
| 26 | func LoadKernelSpec() (*Spec, error) { |
| 27 | if platform.IsWindows { |
| 28 | return nil, internal.ErrNotSupportedOnOS |
| 29 | } |
| 30 | |
| 31 | fh, err := os.Open("/sys/kernel/btf/vmlinux") |
| 32 | if err == nil { |
| 33 | defer fh.Close() |
| 34 | |
| 35 | info, err := fh.Stat() |
| 36 | if err != nil { |
| 37 | return nil, fmt.Errorf("stat vmlinux: %w", err) |
| 38 | } |
| 39 | |
| 40 | // NB: It's not safe to mmap arbitrary files because mmap(2) doesn't |
| 41 | // guarantee that changes made after mmap are not visible in the mapping. |
| 42 | // |
| 43 | // This is not a problem for vmlinux, since it is always a read-only file. |
| 44 | raw, err := unix.Mmap(int(fh.Fd()), 0, int(info.Size()), unix.PROT_READ, unix.MAP_PRIVATE) |
| 45 | if err != nil { |
| 46 | return LoadSplitSpecFromReader(fh, nil) |
| 47 | } |
| 48 | |
| 49 | spec, err := loadRawSpec(raw, nil) |
| 50 | if err != nil { |
| 51 | _ = unix.Munmap(raw) |
| 52 | return nil, fmt.Errorf("load vmlinux: %w", err) |
| 53 | } |
| 54 | |
| 55 | runtime.AddCleanup(spec.decoder.sharedBuf, func(b []byte) { |
| 56 | _ = unix.Munmap(b) |
| 57 | }, raw) |
| 58 | |
| 59 | return spec, nil |
| 60 | } |
| 61 | |
| 62 | file, err := findVMLinux() |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | defer file.Close() |
| 67 | |
| 68 | spec, err := LoadSpecFromReader(file) |
| 69 | return spec, err |
| 70 | } |
| 71 | |
| 72 | // LoadKernelModuleSpec returns the BTF information for the named kernel module. |
| 73 | // |
searching dependent graphs…