Skip to content

Helper function bpf_get_attach_cookie

v5.15

Definition

Copyright (c) 2015 The Libbpf Authors. All rights reserved.

Get bpf_cookie value provided (optionally) during the program attachment. It might be different for each individual attachment, even if BPF program itself is the same. Expects BPF program context ctx as a first argument.

Supported for the following program types:

  • kprobe/uprobe
  • tracepoint
  • perf_event
  • fentry/fexit/fmod_ret
  • LSM

Returns

Value specified by user at BPF link creation/attachment time or 0, if it was not specified.

static __u64 (* const bpf_get_attach_cookie)(void *ctx) = (void *) 174;

Usage

This is useful for cases when the same BPF program is used for attaching and processing invocation of different tracepoints/kprobes/uprobes in a generic fashion, but such that each invocation is distinguished from each other (e.g., BPF program can look up additional information associated with a specific kernel function without having to rely on function IP lookups). This enables new use cases to be implemented simply and efficiently that previously were possible only through code generation (and thus multiple instances of almost identical BPF program) or compilation at runtime (BCC-style) on target hosts (even more expensive resource-wise). For uprobes it is not even possible in some cases to know function IP before hand (e.g., when attaching to shared library without process ID filtering, in which case base load address is not known for a library).

Program types

This helper call can be used in the following program types:

Example

Docs could be improved

This part of the docs is incomplete, contributions are very welcome