Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

BPF Helpers

BPF helper functions are kernel-provided routines callable from BPF programs. In Whistler, call them by name in function position. Arguments are passed in registers R1--R5 and the return value is in R0.

(get-current-pid-tgid)                      ; 0 args
(probe-read-user dst size src)              ; 3 args
(get-current-comm buf-ptr buf-size)         ; 2 args
(ringbuf-reserve map-name size flags)       ; 3 args (special: map arg)

The compiler validates argument counts at compile time.

Available helpers

HelperIDArgsDescription
map-lookup-elem1--(use map-lookup instead)
map-update-elem2--(use map-update instead)
map-delete-elem3--(use map-delete instead)
probe-read43Read kernel memory (legacy)
ktime-get-ns50Monotonic clock, nanoseconds
trace-printk63Debug printf to trace_pipe
get-prandom-u3270Pseudo-random u32
get-smp-processor-id80Current CPU number
tail-call12--(use the tail-call form instead)
get-current-pid-tgid140PID in low 32, TGID in high 32
get-current-uid-gid150UID in low 32, GID in high 32
get-current-comm162Copy task comm to buffer
redirect232Redirect packet to ifindex
perf-event-output253Send data via perf event
skb-load-bytes263Load bytes from skb
get-current-task350Pointer to current task_struct
probe-read-str453Read kernel string
get-socket-cookie471Socket cookie for tracking
get-current-cgroup-id800Current cgroup v2 ID
probe-read-user1123Read user-space memory
probe-read-kernel1133Read kernel memory (modern)
probe-read-user-str1143Read user-space string
ringbuf-output1304Copy data to ring buffer
ringbuf-reserve1313Reserve ring buffer space
ringbuf-submit1322Submit ring buffer entry
ringbuf-discard1332Discard ring buffer entry
get-current-task-btf1590Current task_struct (BTF-aware)
ktime-get-coarse-ns1610Coarse monotonic clock

Map helpers (1--3) and tail-call (12) are called through dedicated Whistler forms rather than by name. The table lists them for completeness.

Example

(defprog trace-fork (:type :tracepoint
                     :section "tracepoint/sched/sched_process_fork"
                     :license "GPL")
  (let ((tgid (get-current-pid-tgid))
        (pid  (cast u32 (>> tgid 32)))
        (ts   (ktime-get-ns)))
    (setf (getmap fork-times pid) ts))
  0)