Skip to content

Latest commit

 

History

History
73 lines (56 loc) · 3.14 KB

bpftrace-oneliners.md

File metadata and controls

73 lines (56 loc) · 3.14 KB

+

# args
sudo cat /sys/kernel/tracing/events/syscalls/sys_enter_write/format
bpftrace -lv 'kfunc:foo'

# exposed headers
# /usr/src/linux-headers-6.1.0-35-common/include/linux/sched.h 
# => #include <linux/sched.h>

# Trace libc calls 
# - /!\ need to scope glob, use dynamic syms: ~/bin/libctrace.sh
bpftrace -e '
BEGIN { @start = nsecs; } 
uprobe:/usr/lib64/libc-2.33.so:* /@start != 0 && pid == cpid/ { 
    printf("%-08d %-04d %s\n", tid, (nsecs - @start) / 1000000, func); 
}' -c '/bin/sleep 2'

# Trace user functions
bpftrace -e 'uprobe:/bin/bash:readline {
    printf("%s\n", ustack(perf, 10));
}'

# C++
readelf -Ws /foo | grep methodName
#69: 00000000004007ee    30 FUNC    WEAK   DEFAULT   14 _ZN6BigApp10methodNameEv
bpftrace -e 'uprobe:/foo:_ZN6BigApp10methodNameEv { printf("1"); }'
bpftrace -e 'uprobe:/foo:"function signature" { printf("1"); }'
# Files opened by process
bpftrace -e 'tracepoint:syscalls:sys_enter_open { printf("%s %s\n", comm, str(args->filename)); }'

# Syscall count by program
bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'

# Read bytes by process:
bpftrace -e 'tracepoint:syscalls:sys_exit_read /args->ret/ { @[comm] = sum(args->ret); }'

# Read size distribution by process:
bpftrace -e 'tracepoint:syscalls:sys_exit_read { @[comm] = hist(args->ret); }'

# Show per-second syscall rates:
bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @ = count(); } interval:s:1 { print(@); clear(@); }'

# Trace disk size by process
bpftrace -e 'tracepoint:block:block_rq_issue { printf("%d %s %d\n", pid, comm, args->bytes); }'

# Count page faults by process
bpftrace -e 'software:faults:1 { @[comm] = count(); }'

# Count LLC cache misses by process name and PID (uses PMCs):
bpftrace -e 'hardware:cache-misses:1000000 { @[comm, pid] = count(); }'

# Profile user-level stacks at 99 Hertz, for PID 189:
bpftrace -e 'profile:hz:99 /pid == 189/ { @[ustack] = count(); }'

# Files opened, for processes in the root cgroup-v2
bpftrace -e 'tracepoint:syscalls:sys_enter_openat /cgroup == cgroupid("/sys/fs/cgroup/unified/mycg")/ { printf("%s\n", str(args->filename)); }'