bpftrace Dynamic kernel tracing
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
bpftrace -l '<pattern>'
bpftrace -lv '<probe>'
bpftrace -e '<program>'
bpftrace <file.bt>
bpftrace -p <pid> -e '<program>'
bpftrace -c '<cmd>' -e
'<program>'
@[comm] = count()
Ctrl+CConcepts
- Probe kernel or user event
- Filter / predicate /
- Action { ... } block
- Map @name
- Output printed on Ctrl+C
Running
bpftrace -e '<program>'
bpftrace <file.bt>
bpftrace -l '<pattern>'
bpftrace -lv '<probe>'
bpftrace -p <pid> -e '<program>'
bpftrace -c '<cmd>' -e
'<program>'
bpftrace -f json -e '<program>'
bpftrace --unsafe -e '<program>'
Probes
kprobe:<func>
kretprobe:<func>
tracepoint:<cat>:<name>
uprobe:<path>:<func>
begin { ... } / end { ... }
uretprobe:<path>:<func>
usdt:<path>:<prov>:<name>
profile:hz:<rate>
interval:s:<n>
fentry:<func>
kprobe:tcp_*Built-ins
pid / tidcommarg0 ... arg5retvalnsecsargs.<field>
funccurtaskMaps and aggregation
@ = count()@[<key>] = count()
@[<key>] = sum(<n>)
@[<key>] = avg(<n>)
@ = hist(<n>)
@ = lhist(<n>, <min>, <max>,
<step>)
@[<k1>, <k2>] = count()
@start[tid] = nsecs
delete(@<map>, <key>)
clear(@<map>)
print(@<map>, <n>)
len(@<map>)
has_key(@<map>, <key>)
Output and functions
printf("<fmt>\n", <args>)
str(<ptr>)
exit()kstack(<n>)
ustack(<n>)
system("<cmd>")
Language
probe /<predicate>/ { ... }
$x = <value>;
if (<cond>) { ... } else { ... }
(struct <name> *)<ptr>
$p.<field>
for ($kv : @<map>) { ... }
#include <linux/<header>.h>
Scripts and config
#!/usr/bin/env bpftrace
config = { <name>=<value> }
BPFTRACE_MAX_STRLEN=<n>
Recipes
t:raw_syscalls:sys_enter { @[comm] = count();}# Ctrl+C prints the counts
t:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args.filename));}
kprobe:vfs_read { @start[tid] = nsecs; }kretprobe:vfs_read /has_key(@start, tid)/ { @us = hist((nsecs - @start[tid]) / 1000); delete(@start, tid);}end { clear(@start); }
profile:hz:99 /pid == <pid>/ { @[ustack(8)] = count();}# kernel side: @[kstack(8)]



