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

Function LoadKernelSpec

btf/kernel.go:26–70  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

24//
25// Consider using [Cache] instead.
26func 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//

Callers 4

LoadKernelModuleSpecFunction · 0.85
kernelMethod · 0.85
TestLoadKernelSpecFunction · 0.85
vmlinuxSpecFunction · 0.85

Calls 9

MmapFunction · 0.92
MunmapFunction · 0.92
LoadSplitSpecFromReaderFunction · 0.85
loadRawSpecFunction · 0.85
findVMLinuxFunction · 0.85
LoadSpecFromReaderFunction · 0.85
OpenMethod · 0.80
CloseMethod · 0.65
SizeMethod · 0.65

Tested by 2

TestLoadKernelSpecFunction · 0.68
vmlinuxSpecFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…