| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
|
| 10 |
"github.com/cilium/ebpf" |
| 11 |
"github.com/cilium/ebpf/features" |
| 12 |
"github.com/cilium/ebpf/internal/sys" |
| 13 |
"github.com/cilium/ebpf/internal/unix" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type KprobeMultiOptions struct { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
Symbols []string |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
Addresses []uintptr |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
Cookies []uint64 |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
Session bool |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func KprobeMulti(prog *ebpf.Program, opts KprobeMultiOptions) (Link, error) { |
| 59 |
return kprobeMulti(prog, opts, 0) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func KretprobeMulti(prog *ebpf.Program, opts KprobeMultiOptions) (Link, error) { |
| 71 |
return kprobeMulti(prog, opts, sys.BPF_F_KPROBE_MULTI_RETURN) |
| 72 |
} |
| 73 |
|
| 74 |
func kprobeMulti(prog *ebpf.Program, opts KprobeMultiOptions, flags uint32) (Link, error) { |
| 75 |
if prog == nil { |
| 76 |
return nil, errors.New("cannot attach a nil program") |
| 77 |
} |
| 78 |
|
| 79 |
syms := uint32(len(opts.Symbols)) |
| 80 |
addrs := uint32(len(opts.Addresses)) |
| 81 |
cookies := uint32(len(opts.Cookies)) |
| 82 |
|
| 83 |
if syms == 0 && addrs == 0 { |
| 84 |
return nil, fmt.Errorf("one of Symbols or Addresses is required: %w", errInvalidInput) |
| 85 |
} |
| 86 |
if syms != 0 && addrs != 0 { |
| 87 |
return nil, fmt.Errorf("fields Symbols and Addresses are mutually exclusive: %w", errInvalidInput) |
| 88 |
} |
| 89 |
if cookies > 0 && cookies != syms && cookies != addrs { |
| 90 |
return nil, fmt.Errorf("field Cookies must be exactly Symbols or Addresses in length: %w", errInvalidInput) |
| 91 |
} |
| 92 |
|
| 93 |
attachType := sys.BPF_TRACE_KPROBE_MULTI |
| 94 |
if opts.Session { |
| 95 |
attachType = sys.BPF_TRACE_KPROBE_SESSION |
| 96 |
} |
| 97 |
|
| 98 |
attr := &sys.LinkCreateKprobeMultiAttr{ |
| 99 |
ProgFd: uint32(prog.FD()), |
| 100 |
AttachType: attachType, |
| 101 |
KprobeMultiFlags: flags, |
| 102 |
} |
| 103 |
|
| 104 |
switch { |
| 105 |
case syms != 0: |
| 106 |
attr.Count = syms |
| 107 |
attr.Syms = sys.NewStringSlicePointer(opts.Symbols) |
| 108 |
|
| 109 |
case addrs != 0: |
| 110 |
attr.Count = addrs |
| 111 |
attr.Addrs = sys.SlicePointer(opts.Addresses) |
| 112 |
} |
| 113 |
|
| 114 |
if cookies != 0 { |
| 115 |
attr.Cookies = sys.SlicePointer(opts.Cookies) |
| 116 |
} |
| 117 |
|
| 118 |
fd, err := sys.LinkCreateKprobeMulti(attr) |
| 119 |
if err == nil { |
| 120 |
return &kprobeMultiLink{RawLink{fd, ""}}, nil |
| 121 |
} |
| 122 |
|
| 123 |
if errors.Is(err, unix.ESRCH) { |
| 124 |
return nil, fmt.Errorf("couldn't find one or more symbols: %w", os.ErrNotExist) |
| 125 |
} |
| 126 |
|
| 127 |
if opts.Session { |
| 128 |
if haveFeatErr := features.HaveBPFLinkKprobeSession(); haveFeatErr != nil { |
| 129 |
return nil, haveFeatErr |
| 130 |
} |
| 131 |
} else { |
| 132 |
if haveFeatErr := features.HaveBPFLinkKprobeMulti(); haveFeatErr != nil { |
| 133 |
return nil, haveFeatErr |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
if errors.Is(err, unix.EINVAL) { |
| 140 |
return nil, fmt.Errorf("%w (missing kernel symbol or prog's AttachType not %s?)", err, ebpf.AttachType(attachType)) |
| 141 |
} |
| 142 |
|
| 143 |
return nil, err |
| 144 |
} |
| 145 |
|
| 146 |
type kprobeMultiLink struct { |
| 147 |
RawLink |
| 148 |
} |
| 149 |
|
| 150 |
var _ Link = (*kprobeMultiLink)(nil) |
| 151 |
|
| 152 |
func (kml *kprobeMultiLink) Update(_ *ebpf.Program) error { |
| 153 |
return fmt.Errorf("update kprobe_multi: %w", ErrNotSupported) |
| 154 |
} |
| 155 |
|
| 156 |
func (kml *kprobeMultiLink) Info() (*Info, error) { |
| 157 |
var info sys.KprobeMultiLinkInfo |
| 158 |
if err := sys.ObjInfo(kml.fd, &info); err != nil { |
| 159 |
return nil, fmt.Errorf("kprobe multi link info: %w", err) |
| 160 |
} |
| 161 |
var addrs = make([]uint64, info.Count) |
| 162 |
var cookies = make([]uint64, info.Count) |
| 163 |
info = sys.KprobeMultiLinkInfo{ |
| 164 |
Addrs: sys.SlicePointer(addrs), |
| 165 |
Cookies: sys.SlicePointer(cookies), |
| 166 |
Count: uint32(len(addrs)), |
| 167 |
} |
| 168 |
if err := sys.ObjInfo(kml.fd, &info); err != nil { |
| 169 |
return nil, fmt.Errorf("kprobe multi link info: %w", err) |
| 170 |
} |
| 171 |
if info.Addrs.IsNil() { |
| 172 |
addrs = nil |
| 173 |
} |
| 174 |
if info.Cookies.IsNil() { |
| 175 |
cookies = nil |
| 176 |
} |
| 177 |
extra := &KprobeMultiInfo{ |
| 178 |
Count: info.Count, |
| 179 |
Flags: info.Flags, |
| 180 |
Missed: info.Missed, |
| 181 |
addrs: addrs, |
| 182 |
cookies: cookies, |
| 183 |
} |
| 184 |
|
| 185 |
return &Info{ |
| 186 |
info.Type, |
| 187 |
info.Id, |
| 188 |
ebpf.ProgramID(info.ProgId), |
| 189 |
extra, |
| 190 |
}, nil |
| 191 |
} |