aya-log-ebpf: allow macros in expr position

This is load-bearing in aya-template.
reviewable/pr1339/r1
Tamir Duberstein 6 days ago
parent 222b2cb88f
commit aa47acc507
No known key found for this signature in database

@ -152,34 +152,36 @@ pub(crate) fn log(args: LogArgs, level_expr: Option<TokenStream>) -> Result<Toke
let len = Ident::new("len", Span::mixed_site()); let len = Ident::new("len", Span::mixed_site());
let record = Ident::new("record", Span::mixed_site()); let record = Ident::new("record", Span::mixed_site());
Ok(quote! { Ok(quote! {
let #level = #level_expr; {
if ::aya_log_ebpf::macro_support::level_enabled(#level) { let #level = #level_expr;
match ::aya_log_ebpf::macro_support::AYA_LOG_BUF.get_ptr_mut(0).and_then(|ptr| unsafe { ptr.as_mut() }) { if ::aya_log_ebpf::macro_support::level_enabled(#level) {
None => {}, match ::aya_log_ebpf::macro_support::AYA_LOG_BUF.get_ptr_mut(0).and_then(|ptr| unsafe { ptr.as_mut() }) {
Some(::aya_log_ebpf::macro_support::LogBuf { buf: #buf }) => { None => {},
// Silence unused variable warning; we may need ctx in the future. Some(::aya_log_ebpf::macro_support::LogBuf { buf: #buf }) => {
let _ = #ctx; // Silence unused variable warning; we may need ctx in the future.
let _: Option<()> = (|| { let _ = #ctx;
let #size = ::aya_log_ebpf::macro_support::write_record_header( let _: Option<()> = (|| {
#buf, let #size = ::aya_log_ebpf::macro_support::write_record_header(
#target, #buf,
#level, #target,
module_path!(), #level,
file!(), module_path!(),
line!(), file!(),
#num_args, line!(),
)?; #num_args,
let mut #size = #size.get(); )?;
#( let mut #size = #size.get();
{ #(
let #buf = #buf.get_mut(#size..)?; {
let #len = ::aya_log_ebpf::macro_support::WriteToBuf::write(#values_iter, #buf)?; let #buf = #buf.get_mut(#size..)?;
#size += #len.get(); let #len = ::aya_log_ebpf::macro_support::WriteToBuf::write(#values_iter, #buf)?;
} #size += #len.get();
)* }
let #record = #buf.get(..#size)?; )*
Result::<_, i64>::ok(::aya_log_ebpf::macro_support::AYA_LOGS.output(#record, 0)) let #record = #buf.get(..#size)?;
})(); Result::<_, i64>::ok(::aya_log_ebpf::macro_support::AYA_LOGS.output(#record, 0))
})();
}
} }
} }
} }

@ -83,7 +83,8 @@ pub fn test_log(ctx: ProbeContext) {
let no_copy = NoCopy {}; let no_copy = NoCopy {};
debug!(&ctx, "{:x}", no_copy.consume()); // Check usage in expression position.
let () = debug!(&ctx, "{:x}", no_copy.consume());
} }
} }

Loading…
Cancel
Save