logo
pub struct BpfTracePrintf {}
Expand description

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 syscall that will have a behavior similar to the one of the equivalent syscall bpf_trace_printk() from Linux kernel.

Examples

use solana_rbpf::syscalls::{BpfTracePrintf, Result};
use solana_rbpf::memory_region::{MemoryRegion, MemoryMapping};
use solana_rbpf::vm::{Config, SyscallObject};
use solana_rbpf::user_error::UserError;

let mut result: Result = Ok(0);
let config = Config::default();
let memory_mapping = MemoryMapping::new::<UserError>(vec![], &config).unwrap();
BpfTracePrintf::call(&mut BpfTracePrintf {}, 0, 0, 1, 15, 32, &memory_mapping, &mut result);
assert_eq!(result.unwrap() as usize, "BpfTracePrintf: 0x1, 0xf, 0x20\n".len());

This will print BpfTracePrintf: 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_syscalls.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.

Trait Implementations

Call the syscall function

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.