[][src]Function solana_rbpf::helpers::bpf_trace_printf

pub fn bpf_trace_printf(
    unused1: u64,
    unused2: u64,
    arg3: u64,
    arg4: u64,
    arg5: u64
) -> u64

Prints its last three arguments to standard output. The first two arguments are unused. Returns the number of bytes written.

By ignoring the first two arguments, it creates a helper that will have a behavior similar to the one of the equivalent helper bpf_trace_printk() from Linux kernel.

Examples

use solana_rbpf::helpers;

let res = helpers::bpf_trace_printf(0, 0, 1, 15, 32);
assert_eq!(res as usize, "bpf_trace_printf: 0x1, 0xf, 0x20\n".len());

This will print bpf_trace_printf: 0x1, 0xf, 0x20.

The eBPF code needed to perform the call in this example would be nearly identical to the code obtained by compiling the following code from C to eBPF with clang:

#include <linux/bpf.h>
#include "path/to/linux/samples/bpf/bpf_helpers.h"

int main(struct __sk_buff *skb)
{
    // Only %d %u %x %ld %lu %lx %lld %llu %llx %p %s conversion specifiers allowed.
    // See <https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/kernel/trace/bpf_trace.c>.
    char *fmt = "bpf_trace_printk %llx, %llx, %llx\n";
    return bpf_trace_printk(fmt, sizeof(fmt), 1, 15, 32);
}

This would equally print the three numbers in /sys/kernel/debug/tracing file each time the program is run.