bpftrace Standard Library (pre-release)
This includes builtins, functions, macros, and map value functions.
The boundaries for the first three are blurred, by design, to allow for more flexible usage and are grouped below as "Helpers".
For example pid and pid() are equivalent; both yielding the process id.
Basically all functions or macros that don't have arguments or have default arguments can be invoked with or without the call syntax.
async helpers are asynchronous, which can lead to unexpected behaviour. See the Invocation Mode section for more information.
compile time helpers are evaluated at compile time, a static value will be compiled into the program.
unsafe helpers can have dangerous side effects and should be used with care, the --unsafe flag is required for use.
Helpers
abs_path
Resolves a relative path or filename to an absolute path.
string abs_path(string path, uint64 max_path_depth)
If path is empty, the returned string is empty.
Example:
tracepoint:syscalls:sys_enter_openat {
print(abs_path(str(args.filename), 32));
}
See also cwd.
arch
Returns the architecture bpftrace was built for.
string arch()string arch
compile time
Name of the architecture bpftrace was built for. One of: x86_64, arm64,
arm, s390x, ppc64le, ppc64, mips64, riscv64, loongarch64.
Note this is the architecture of the bpftrace binary itself, which is not
necessarily what uname -m reports: a 32 bit arm build running on an arm64
kernel yields arm.
assert
Terminates the script with an error code if a condition is false.
void assert(bool condition, string message)
assert_str
Checks that this value is string-like.
void assert_str(any expression)
bswap
Reverses the byte order of an integer.
uint8 bswap(uint8 n)uint16 bswap(uint16 n)uint32 bswap(uint32 n)uint64 bswap(uint64 n)
bswap reverses the order of the bytes in integer n. In case of 8 bit integers, n is returned without being modified.
The return type is an unsigned integer of the same width as n.
buf
Reads bytes from an address into a buffer.
buffer buf(void * data, [int64 length])
buf reads length amount of bytes from address data.
The maximum value of length is limited to the BPFTRACE_MAX_STRLEN variable.
For arrays the length is optional, it is automatically inferred from the signature.
buf is address space aware and will call the correct helper based on the address space associated with data.
The buffer object returned by buf can safely be printed as a hex encoded string with the %r format specifier.
Bytes with values >=32 and <=126 are printed using their ASCII character, other bytes are printed in hex form (e.g. \x00). The %rx format specifier can be used to print everything in hex form, including ASCII characters. The similar %rh format specifier prints everything in hex form without \x and with spaces between bytes (e.g. 0a fe).
interval:s:1 {
printf("%r\n", buf(kaddr("avenrun"), 8));
}
\x00\x03\x00\x00\x00\x00\x00\x00
\xc2\x02\x00\x00\x00\x00\x00\x00
cat
Prints the contents of a file to standard output.
void cat(string namefmt, [...args])
async
Dump the contents of the named file to stdout.
cat supports the same format string and arguments that printf does.
If the file cannot be opened or read an error is printed to stderr.
tracepoint:syscalls:sys_enter_execve {
cat("/proc/%d/maps", pid);
}
55f683ebd000-55f683ec1000 r--p 00000000 08:01 1843399 /usr/bin/ls
55f683ec1000-55f683ed6000 r-xp 00004000 08:01 1843399 /usr/bin/ls
55f683ed6000-55f683edf000 r--p 00019000 08:01 1843399 /usr/bin/ls
55f683edf000-55f683ee2000 rw-p 00021000 08:01 1843399 /usr/bin/ls
55f683ee2000-55f683ee3000 rw-p 00000000 00:00 0
cgroup
Returns the current process's cgroup ID.
uint64 cgroup()uint64 cgroup
Only works with cgroupv2.
This utilizes the BPF helper bpf_get_current_cgroup_id.
cgroup_path
Converts a cgroup ID to its path.
cgroup_path_t cgroup_path(int cgroupid, string filter)
This is done asynchronously in userspace when the cgroup_path value is printed,
therefore it can resolve to a different value if the cgroup id gets reassigned.
This also means that the returned value can only be used for printing.
A string literal may be passed as an optional second argument to filter cgroup hierarchies in which the cgroup id is looked up by a wildcard expression (cgroup2 is always represented by "unified", regardless of where it is mounted).
The currently mounted hierarchy at /sys/fs/cgroup is used to do the lookup. If the cgroup with the given id isn’t present here (e.g. when running in a Docker container), the cgroup path won’t be found (unlike when looking up the cgroup path of a process via /proc/.../cgroup).
BEGIN {
$cgroup_path = cgroup_path(3436);
print($cgroup_path);
print($cgroup_path); /* This may print a different path */
printf("%s %s", $cgroup_path, $cgroup_path); /* This may print two different paths */
}
cgroupid
Returns the cgroup v2 ID for a cgroup path.
uint64 cgroupid(const string path)
compile time
BEGIN {
print(cgroupid("/sys/fs/cgroup/system.slice"));
}
clear
Clear all keys/values from a map.
void clear(map m)
async
Use delete(m) to remove all keys/values synchronously.
interval:ms:100 {
@[rand % 10] = count();
}
interval:s:10 {
print(@);
clear(@);
}
comm
Returns the name of the current thread or a specified process.
string comm()string commstring comm(uint32 pid)
This uses the BPF helper bpf_get_current_comm for comm() or the kfunc
bpf_task_from_pid for comm(pid).
config
Returns the current bpftrace configuration.
Record config()Record config
Returns a Record containing the current bpftrace configuration settings. List of config variables
begin {
printf("max_strlen: %d, stack_mode: %s\n", config.max_strlen, config.stack_mode);
}
container_of
Finds the containing object from a pointer to one of its members.
Container* container_of(Member* ptr, Type type, Identifier member)
cpid
Returns the child process ID when bpftrace was started with -c.
uint32 cpid()uint32 cpid
If there is no child process, a runtime warning will be issued and the
return value will be zero. This warning can be avoided by using has_cpid
to check if cpid has a value, prior to referencing cpid.
cpu
Returns the ID of the CPU running the current BPF program.
uint32 cpu()uint32 cpu
The BPF program corresponds to the probe's action body.
This utilizes the BPF helper bpf_get_smp_processor_id.
curtask
Returns a pointer to the current task's task_struct.
struct task_struct * curtask()struct task_struct * curtask
This utilizes the BPF helper bpf_get_current_task_btf.
cwd
Get the current working directory.
string cwd()string cwdstring cwd(max_path_depth)
default_str_length
Returns the default unbounded length.
uint64 default_str_length()uint64 default_str_length
delete
Removes one key and its value from a map.
bool delete(map m, mapkey k)bool delete(map m)- deprecated
bool delete(mapkey k)
For scalar maps (e.g. no explicit keys), the key is omitted.
For maps with explicit keys, omitting the key deletes all of the map's keys/values and returns false if some entries couldn't be deleted.
Unlike clear, the deletion happens in-kernel during the call rather than asynchronously, so it can't be dropped. If other probes update the map at the same time (e.g. on other CPUs), entries they add while delete runs may remain afterwards.
Note that because the deletion happens in-kernel, a print of the map issued earlier in the same probe will also see an empty map, as printing happens asynchronously.
For map keys that are composed of multiple values (e.g. @mymap[3, "hello"] = 1 - remember these values are represented as a tuple) the syntax would be: delete(@mymap, (3, "hello"));.
If deletion fails (e.g. the key doesn’t exist) the function returns false (0).
If the return value of delete isn't used, you will get a compile-time warning, whether or not the deletion succeeds.
Use _ = delete(...) to discard it without a warning.
@a[1] = 1;
delete(@a, 1); // warning (return value is discarded)
if (delete(@a, 2)) { // no warning (return value is used)
// ...
}
$did_delete = delete(@a, 2); // no warning (return value is used)
_ = delete(@a, 2); // no warning (return value is explicitly discarded)
The, now deprecated, API (supported in version <= 0.21.x) of passing map arguments with the key is still supported:
e.g. delete(@mymap[3, "hello"]);.
kprobe:dummy {
@scalar = 1;
delete(@scalar); // ok
@single["hello"] = 1;
delete(@single, "hello"); // ok
@associative[1,2] = 1;
delete(@associative, (1,2)); // ok
delete(@associative); // ok, deletes all keys/values
delete(@associative, 1); // error
// deprecated but ok
delete(@single["hello"]);
delete(@associative[1, 2]);
}