foniod / redbpf

Rust library for building and running BPF/eBPF modules
Apache License 2.0
1.71k stars 136 forks source link

printk macro why not use {} #330

Closed Sherlock-Holo closed 2 years ago

Sherlock-Holo commented 2 years ago

in rust world, format!, println! or log::info!, all of them use {}, why printk! use %llu %d?

GermanCoding commented 2 years ago

Well for once, those functions all use std::fmt internally, which we do not have access to as eBPF programs work without stdlib, so you can't just proxy.

Next, you have to recall that in eBPF, we do not have access to any (standard) memory allocation mechanisms (only limited size stack and some map trickery), so working with arbitrary size data is not possible. You would have to write a very limited emulation of the format! macro.

What printk! does internally, it just calls the kernel's printk function (plus some safety checks). And %llu, %d etc all happen to be the syntax used by the kernel: https://www.kernel.org/doc/Documentation/printk-formats.txt