| 1 |
package tracefs |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/rand" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"runtime" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
"syscall" |
| 13 |
|
| 14 |
"github.com/cilium/ebpf/internal" |
| 15 |
"github.com/cilium/ebpf/internal/linux" |
| 16 |
"github.com/cilium/ebpf/internal/mountinfo" |
| 17 |
"github.com/cilium/ebpf/internal/platform" |
| 18 |
"github.com/cilium/ebpf/internal/unix" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
ErrInvalidInput = errors.New("invalid input") |
| 23 |
|
| 24 |
ErrInvalidMaxActive = errors.New("can only set maxactive on kretprobes") |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
type ProbeType uint8 |
| 30 |
|
| 31 |
const ( |
| 32 |
Kprobe ProbeType = iota |
| 33 |
Uprobe |
| 34 |
) |
| 35 |
|
| 36 |
func (pt ProbeType) eventsFile() (*os.File, error) { |
| 37 |
path, err := sanitizeTracefsPath(fmt.Sprintf("%s_events", pt.String())) |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
|
| 42 |
return os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0666) |
| 43 |
} |
| 44 |
|
| 45 |
type ProbeArgs struct { |
| 46 |
Type ProbeType |
| 47 |
Symbol, Group, Path string |
| 48 |
Offset, RefCtrOffset, Cookie uint64 |
| 49 |
Pid, RetprobeMaxActive int |
| 50 |
Ret bool |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func RandomGroup(prefix string) (string, error) { |
| 58 |
if !validIdentifier(prefix) { |
| 59 |
return "", fmt.Errorf("prefix '%s' must be alphanumeric or underscore: %w", prefix, ErrInvalidInput) |
| 60 |
} |
| 61 |
|
| 62 |
b := make([]byte, 8) |
| 63 |
if _, err := rand.Read(b); err != nil { |
| 64 |
return "", fmt.Errorf("reading random bytes: %w", err) |
| 65 |
} |
| 66 |
|
| 67 |
group := fmt.Sprintf("%s_%x", prefix, b) |
| 68 |
if len(group) > 63 { |
| 69 |
return "", fmt.Errorf("group name '%s' cannot be longer than 63 characters: %w", group, ErrInvalidInput) |
| 70 |
} |
| 71 |
|
| 72 |
return group, nil |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
func validIdentifier(s string) bool { |
| 82 |
if len(s) < 1 { |
| 83 |
return false |
| 84 |
} |
| 85 |
for i, c := range []byte(s) { |
| 86 |
switch { |
| 87 |
case c >= 'a' && c <= 'z': |
| 88 |
case c >= 'A' && c <= 'Z': |
| 89 |
case c == '_': |
| 90 |
case i > 0 && (c == '-' || c >= '0' && c <= '9'): |
| 91 |
|
| 92 |
default: |
| 93 |
return false |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return true |
| 98 |
} |
| 99 |
|
| 100 |
func sanitizeTracefsPath(path ...string) (string, error) { |
| 101 |
base, err := getTracefsPath() |
| 102 |
if err != nil { |
| 103 |
return "", err |
| 104 |
} |
| 105 |
l := filepath.Join(path...) |
| 106 |
p := filepath.Join(base, l) |
| 107 |
if !strings.HasPrefix(p, base) { |
| 108 |
return "", fmt.Errorf("path '%s' attempts to escape base path '%s': %w", l, base, ErrInvalidInput) |
| 109 |
} |
| 110 |
return p, nil |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
var getTracefsPath = sync.OnceValues(func() (string, error) { |
| 128 |
if !platform.IsLinux { |
| 129 |
return "", fmt.Errorf("tracefs: %w", internal.ErrNotSupportedOnOS) |
| 130 |
} |
| 131 |
|
| 132 |
if entries, err := mountinfo.Read(); err == nil { |
| 133 |
if path := findTracefsInEntries(entries); path != "" { |
| 134 |
return path, nil |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
for _, p := range []struct { |
| 139 |
path string |
| 140 |
fsType int64 |
| 141 |
}{ |
| 142 |
{"/sys/kernel/tracing", unix.TRACEFS_MAGIC}, |
| 143 |
{"/sys/kernel/debug/tracing", unix.TRACEFS_MAGIC}, |
| 144 |
|
| 145 |
{"/sys/kernel/debug/tracing", unix.DEBUGFS_MAGIC}, |
| 146 |
} { |
| 147 |
if fsType, err := linux.FSType(p.path); err == nil && fsType == p.fsType { |
| 148 |
return p.path, nil |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return "", errors.New("neither debugfs nor tracefs are mounted") |
| 153 |
}) |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
func findTracefsInEntries(entries []mountinfo.Entry) string { |
| 160 |
for _, e := range entries { |
| 161 |
if e.FSType == "tracefs" && e.Root == "/" { |
| 162 |
return e.MountPoint |
| 163 |
} |
| 164 |
} |
| 165 |
for _, e := range entries { |
| 166 |
if e.FSType != "debugfs" || e.Root != "/" { |
| 167 |
continue |
| 168 |
} |
| 169 |
tracing := filepath.Join(e.MountPoint, "tracing") |
| 170 |
if info, err := os.Stat(tracing); err == nil && info.IsDir() { |
| 171 |
return tracing |
| 172 |
} |
| 173 |
} |
| 174 |
return "" |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
func sanitizeIdentifier(s string) string { |
| 181 |
var skip bool |
| 182 |
return strings.Map(func(c rune) rune { |
| 183 |
switch { |
| 184 |
case c >= 'a' && c <= 'z', |
| 185 |
c >= 'A' && c <= 'Z', |
| 186 |
c >= '0' && c <= '9': |
| 187 |
skip = false |
| 188 |
return c |
| 189 |
|
| 190 |
case skip: |
| 191 |
return -1 |
| 192 |
|
| 193 |
default: |
| 194 |
skip = true |
| 195 |
return '_' |
| 196 |
} |
| 197 |
}, s) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
func EventID(group, name string) (uint64, error) { |
| 203 |
if !validIdentifier(group) { |
| 204 |
return 0, fmt.Errorf("invalid tracefs group: %q", group) |
| 205 |
} |
| 206 |
|
| 207 |
if !validIdentifier(name) { |
| 208 |
return 0, fmt.Errorf("invalid tracefs name: %q", name) |
| 209 |
} |
| 210 |
|
| 211 |
path, err := sanitizeTracefsPath("events", group, name, "id") |
| 212 |
if err != nil { |
| 213 |
return 0, err |
| 214 |
} |
| 215 |
tid, err := internal.ReadUint64FromFile("%d\n", path) |
| 216 |
if errors.Is(err, os.ErrNotExist) { |
| 217 |
return 0, err |
| 218 |
} |
| 219 |
if err != nil { |
| 220 |
return 0, fmt.Errorf("reading trace event ID of %s/%s: %w", group, name, err) |
| 221 |
} |
| 222 |
|
| 223 |
return tid, nil |
| 224 |
} |
| 225 |
|
| 226 |
func probePrefix(ret bool, maxActive int) string { |
| 227 |
if ret { |
| 228 |
if maxActive > 0 { |
| 229 |
return fmt.Sprintf("r%d", maxActive) |
| 230 |
} |
| 231 |
return "r" |
| 232 |
} |
| 233 |
return "p" |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
type Event struct { |
| 238 |
typ ProbeType |
| 239 |
group, name string |
| 240 |
|
| 241 |
id uint64 |
| 242 |
|
| 243 |
cleanup runtime.Cleanup |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
func NewEvent(args ProbeArgs) (*Event, error) { |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
eventName := sanitizeIdentifier(args.Symbol) |
| 259 |
_, err := EventID(args.Group, eventName) |
| 260 |
if err == nil { |
| 261 |
return nil, fmt.Errorf("trace event %s/%s: %w", args.Group, eventName, os.ErrExist) |
| 262 |
} |
| 263 |
if errors.Is(err, unix.EINVAL) { |
| 264 |
return nil, fmt.Errorf("trace event %s/%s: %w (unknown symbol?)", args.Group, eventName, err) |
| 265 |
} |
| 266 |
if !errors.Is(err, os.ErrNotExist) { |
| 267 |
return nil, fmt.Errorf("checking trace event %s/%s: %w", args.Group, eventName, err) |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
f, err := args.Type.eventsFile() |
| 272 |
if err != nil { |
| 273 |
return nil, err |
| 274 |
} |
| 275 |
defer f.Close() |
| 276 |
|
| 277 |
var pe, token string |
| 278 |
switch args.Type { |
| 279 |
case Kprobe: |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
if args.RetprobeMaxActive != 0 && !args.Ret { |
| 295 |
return nil, ErrInvalidMaxActive |
| 296 |
} |
| 297 |
token = KprobeToken(args) |
| 298 |
pe = fmt.Sprintf("%s:%s/%s %s", probePrefix(args.Ret, args.RetprobeMaxActive), args.Group, eventName, token) |
| 299 |
case Uprobe: |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if args.RetprobeMaxActive != 0 { |
| 311 |
return nil, ErrInvalidMaxActive |
| 312 |
} |
| 313 |
token = UprobeToken(args) |
| 314 |
pe = fmt.Sprintf("%s:%s/%s %s", probePrefix(args.Ret, 0), args.Group, eventName, token) |
| 315 |
} |
| 316 |
_, err = f.WriteString(pe) |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
if errors.Is(err, os.ErrNotExist) { |
| 321 |
return nil, fmt.Errorf("token %s: not found: %w", token, err) |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if errors.Is(err, syscall.EILSEQ) { |
| 327 |
return nil, fmt.Errorf("token %s: bad insn boundary: %w", token, os.ErrNotExist) |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
if errors.Is(err, syscall.ERANGE) { |
| 332 |
return nil, fmt.Errorf("token %s: offset too big: %w", token, os.ErrNotExist) |
| 333 |
} |
| 334 |
|
| 335 |
if err != nil { |
| 336 |
return nil, fmt.Errorf("token %s: writing '%s': %w", token, pe, err) |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
tid, err := EventID(args.Group, eventName) |
| 341 |
if args.RetprobeMaxActive != 0 && errors.Is(err, os.ErrNotExist) { |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
event := fmt.Sprintf("kprobes/r_%s_%d", args.Symbol, args.Offset) |
| 347 |
if err := removeEvent(args.Type, event); err != nil { |
| 348 |
return nil, fmt.Errorf("failed to remove spurious maxactive event: %s", err) |
| 349 |
} |
| 350 |
|
| 351 |
return nil, &internal.UnsupportedFeatureError{ |
| 352 |
MinimumVersion: internal.Version{4, 12}, |
| 353 |
Name: "trace event with non-default maxactive", |
| 354 |
} |
| 355 |
} |
| 356 |
if err != nil { |
| 357 |
return nil, fmt.Errorf("get trace event id: %w", err) |
| 358 |
} |
| 359 |
|
| 360 |
evt := &Event{typ: args.Type, group: args.Group, name: eventName, id: tid} |
| 361 |
evt.cleanup = runtime.AddCleanup(evt, func(*byte) { |
| 362 |
_ = removeEvent(args.Type, fmt.Sprintf("%s/%s", args.Group, eventName)) |
| 363 |
}, nil) |
| 364 |
|
| 365 |
return evt, nil |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
func (evt *Event) Close() error { |
| 372 |
if evt.id == 0 { |
| 373 |
return os.ErrClosed |
| 374 |
} |
| 375 |
|
| 376 |
evt.id = 0 |
| 377 |
evt.cleanup.Stop() |
| 378 |
pe := fmt.Sprintf("%s/%s", evt.group, evt.name) |
| 379 |
return removeEvent(evt.typ, pe) |
| 380 |
} |
| 381 |
|
| 382 |
func removeEvent(typ ProbeType, pe string) error { |
| 383 |
f, err := typ.eventsFile() |
| 384 |
if err != nil { |
| 385 |
return err |
| 386 |
} |
| 387 |
defer f.Close() |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if _, err = f.WriteString("-:" + pe); err != nil { |
| 392 |
return fmt.Errorf("remove event %q from %s: %w", pe, f.Name(), err) |
| 393 |
} |
| 394 |
|
| 395 |
return nil |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
func (evt *Event) ID() uint64 { |
| 400 |
return evt.id |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
func (evt *Event) Group() string { |
| 405 |
return evt.group |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
func KprobeToken(args ProbeArgs) string { |
| 410 |
po := args.Symbol |
| 411 |
|
| 412 |
if args.Offset != 0 { |
| 413 |
po += fmt.Sprintf("+%#x", args.Offset) |
| 414 |
} |
| 415 |
|
| 416 |
return po |
| 417 |
} |