1*fae6e9adSlinfeng use core::{
2*fae6e9adSlinfeng ffi::{c_char, c_int},
3*fae6e9adSlinfeng fmt::Write,
4*fae6e9adSlinfeng };
5*fae6e9adSlinfeng
6*fae6e9adSlinfeng use printf_compat::{format, output};
7*fae6e9adSlinfeng
8*fae6e9adSlinfeng /// 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*fae6e9adSlinfeng pub unsafe extern "C" fn printf(w: &mut impl Write, str: *const c_char, mut args: ...) -> c_int {
10*fae6e9adSlinfeng let bytes_written = format(str as _, args.as_va_list(), output::fmt_write(w));
11*fae6e9adSlinfeng bytes_written + 1
12*fae6e9adSlinfeng }
13*fae6e9adSlinfeng
14*fae6e9adSlinfeng struct TerminalOut;
15*fae6e9adSlinfeng impl Write for TerminalOut {
write_str(&mut self, s: &str) -> core::fmt::Result16*fae6e9adSlinfeng fn write_str(&mut self, s: &str) -> core::fmt::Result {
17*fae6e9adSlinfeng print!("{}", s);
18*fae6e9adSlinfeng Ok(())
19*fae6e9adSlinfeng }
20*fae6e9adSlinfeng }
21*fae6e9adSlinfeng
22*fae6e9adSlinfeng /// 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*fae6e9adSlinfeng pub fn trace_printf(fmt_ptr: u64, _fmt_len: u64, arg3: u64, arg4: u64, arg5: u64) -> u64 {
24*fae6e9adSlinfeng unsafe { printf(&mut TerminalOut, fmt_ptr as _, arg3, arg4, arg5) as u64 }
25*fae6e9adSlinfeng }
26