Linux bpftrace Command
bpftrace is an advanced tracing tool based on eBPF (Extended Berkeley Packet Filter), which allows developers to dynamically observe and analyze the running state of a Linux system without modifying kernel code.
eBPF is a revolutionary technology in the Linux kernel that provides a safe virtual machine environment for running user-defined code in the kernel. Built on top of eBPF, bpftrace provides a simpler, higher-level abstraction layer.
Core advantages of bpftrace
Real-time system observation
- No need to restart the system or applications
- Extremely low performance overhead
- Can observe kernel and user-space programs
Flexible probing capabilities
- Supports multiple probe point types: function entry/exit, timers, hardware events, etc.
- Can trace system calls, network events, disk I/O, etc.
Simple scripting language
- AWK-like syntax with a gentle learning curve
- Rich set of built-in functions and variables
- Supports conditional filtering and aggregate statistics
bpftrace installation and configuration
Installation Method
Example
Ubuntu/Debian
sudo
apt
install
bpftrace
CentOS/RHEL
sudo
yum install
bpftrace
Compiling from source
git clone
https:
//
github.com
/
iovisor
/
bpftrace.git
mkdir
bpftrace
/
build
&
amp;
&
amp;
cd
bpftrace
/
build
cmake ..
make
sudo
make
install
Verify the installation
sudo bpftrace -e 'BEGIN { printf("Hello, bpftrace!n"); }'
bpftrace basic syntax
A bpftrace program consists of probes and associated actions; the basic structure is as follows:
probe /filter/ {
action
}
Probe types
| Probe types | Description | Example |
|---|---|---|
kprobe |
Kernel function entry | kprobe:vfs_read |
kretprobe |
Kernel function return | kretprobe:vfs_read |
uprobe |
User-space function entry | uprobe:/bin/bash:readline |
tracepoint |
Kernel static tracepoints | tracepoint:syscalls:sys_enter_open |
interval |
Timer triggers | interval:s:5 |
software |
Software events | software:faults:major |
Common built-in variables
pid: current process IDtid: current thread IDcomm: current process namensecs: nanosecond timestamparg0-argN: function argumentsretval: function return value
Practical bpftrace examples
1. Tracing system calls
Example
Count the number of open system calls
sudo
bpftrace
-e
'tracepoint:syscalls:sys_enter_open { @[comm] = count(); }'
2. Analyzing function execution time
Example
Measure the execution time of vfs_read
sudo
bpftrace
-e
'
kprobe:vfs_read { @start[tid] = nsecs; }
kretprobe:vfs_read /@start[tid]/ {
@times = hist(nsecs - @start[tid]);
delete(@start[tid]);
}'
3. Monitoring file access by a process
Example
Trace files opened by a specified process
sudo
bpftrace
-e
'tracepoint:syscalls:sys_enter_openat /pid == 1234/ { printf("%s -> %sn", comm, str(args->filename)); }'
4. Counting TCP connections
Example
Count TCP connections per process
sudo
bpftrace
-e
'kprobe:tcp_connect { @[comm] = count(); }'
Advanced bpftrace features
1. Map functionality
bpftrace provides several built-in map types for data aggregation:
Example
Statistical histogram
@
hist = hist
(
nsecs
)
;
Compute an average
@
avg = avg
(
nsecs
)
;
Count unique values
@
unique = count
(
)
;
2. Conditional filtering
Example
Trace only read calls from a specific process
tracepoint:syscalls:sys_enter_read
/
pid ==
1234
/
{
printf
(
"PID %d reading %d bytesn"
, pid, args-
count
)
;
}
3. Combining multiple probes
Example
Trace the entire process from socket creation to connection
kprobe:sock_alloc
{
@
socket
[
tid
]
=
1
;
}
kprobe:tcp_connect
/@
socket
[
tid
]
/
{
printf
(
"socket %d connecting to %s:%dn"
, args-
sock-
__sk_common.skc_dport,
ntop
(
args-
sock-
__sk_common.skc_daddr
)
,
args-
sock-
__sk_common.skc_dport
)
;
delete
(
@
socket
[
tid
]
)
;
}
bpftrace best practices
- Limit the tracing scope: Filter by PID or command name to reduce system overhead
- Avoid excessive printing: Too many printf calls will affect performance
- Use aggregation: Use aggregate functions such as count() and sum() whenever possible
- Clean up resources: Long-running scripts should periodically clean up map data
- Security considerations: bpftrace requires root privileges, so run unknown scripts with caution
Comparison of bpftrace with other tools
| Tool | Advantages | Disadvantages |
|---|---|---|
| bpftrace | Flexible, high-performance, and easy to use | Requires root privileges |
| strace | Simple, no compilation required | High performance overhead |
| perf | Full-featured, low overhead | Steep learning curve |
| SystemTap | Powerful features | Requires compilation, complex configuration |
Recommended learning resources
bpftrace is a powerful tool for system performance analysis and troubleshooting. By practicing these examples and mastering its core concepts, you will be able to understand and optimize the runtime behavior of Linux systems more deeply.
Linux Command Reference