1 use core::{
2 ffi::{c_char, c_int},
3 fmt::Write,
4 };
5
6 use printf_compat::{format, output};
7
8 /// Printf according to the format string, function will return the number of bytes written(including '\0')
printf(w: &mut impl Write, str: *const c_char, mut args: ...) -> c_int9 pub unsafe extern "C" fn printf(w: &mut impl Write, str: *const c_char, mut args: ...) -> c_int {
10 let bytes_written = format(str as _, args.as_va_list(), output::fmt_write(w));
11 bytes_written + 1
12 }
13
14 struct TerminalOut;
15 impl Write for TerminalOut {
write_str(&mut self, s: &str) -> core::fmt::Result16 fn write_str(&mut self, s: &str) -> core::fmt::Result {
17 print!("{}", s);
18 Ok(())
19 }
20 }
21
22 /// See https://ebpf-docs.dylanreimerink.nl/linux/helper-function/bpf_trace_printk/
trace_printf(fmt_ptr: u64, _fmt_len: u64, arg3: u64, arg4: u64, arg5: u64) -> u6423 pub fn trace_printf(fmt_ptr: u64, _fmt_len: u64, arg3: u64, arg4: u64, arg5: u64) -> u64 {
24 unsafe { printf(&mut TerminalOut, fmt_ptr as _, arg3, arg4, arg5) as u64 }
25 }
26