Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux bpftrace Command

Linux bpftrace Command

Linux Command Reference Linux Command Reference


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

Flexible probing capabilities

Simple scripting language


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


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

  1. Limit the tracing scope: Filter by PID or command name to reduce system overhead
  2. Avoid excessive printing: Too many printf calls will affect performance
  3. Use aggregation: Use aggregate functions such as count() and sum() whenever possible
  4. Clean up resources: Long-running scripts should periodically clean up map data
  5. 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

  1. bpftrace official documentation
  2. bpftrace one-liner tutorial
  3. bpftrace example repository

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 Linux Command Reference