|
1 | 1 | use proc_macro2::{Ident, Span};
|
2 | 2 | use quote::quote;
|
3 |
| -use syn::{parse_quote, Abi, Attribute, Item, ItemFn, Pat, Result, Signature, Visibility}; |
| 3 | +use syn::{parse_quote, Abi, Attribute, FnArg, Item, ItemFn, Pat, Result, Signature, Visibility}; |
4 | 4 |
|
5 | 5 | fn validate_vis(vis: &Visibility) -> Result<()> {
|
6 | 6 | if !matches!(vis, Visibility::Public(_)) {
|
@@ -96,6 +96,38 @@ fn emit_func(mut func: ItemFn, sig: &ParsedSig) -> Result<ItemFn> {
|
96 | 96 | func.vis = Visibility::Inherited;
|
97 | 97 | func.attrs.clear();
|
98 | 98 |
|
| 99 | + let input_idents = sig |
| 100 | + .inputs |
| 101 | + .iter() |
| 102 | + .map(|fn_arg| match fn_arg { |
| 103 | + FnArg::Typed(pat_type) => match &*pat_type.pat { |
| 104 | + Pat::Ident(pat_ident) => &pat_ident.ident, |
| 105 | + _ => unreachable!(), |
| 106 | + }, |
| 107 | + _ => unreachable!(), |
| 108 | + }) |
| 109 | + .collect::<Vec<_>>(); |
| 110 | + let input_format = input_idents |
| 111 | + .iter() |
| 112 | + .map(|ident| format!("{ident} = {{:?}}")) |
| 113 | + .collect::<Vec<_>>() |
| 114 | + .join(", "); |
| 115 | + let strace_format = format!("{}({input_format}) = ", sig.ident); |
| 116 | + |
| 117 | + let block = func.block; |
| 118 | + func.block = parse_quote! {{ |
| 119 | + #[allow(unreachable_code)] |
| 120 | + #[allow(clippy::diverging_sub_expression)] |
| 121 | + { |
| 122 | + #[cfg(feature = "strace")] |
| 123 | + print!(#strace_format, #(#input_idents),*); |
| 124 | + let ret = #block; |
| 125 | + #[cfg(feature = "strace")] |
| 126 | + println!("{ret:?}"); |
| 127 | + ret |
| 128 | + } |
| 129 | + }}; |
| 130 | + |
99 | 131 | let func_call = quote! {
|
100 | 132 | kernel_function!(#ident(#(#args),*))
|
101 | 133 | };
|
@@ -156,8 +188,19 @@ mod tests {
|
156 | 188 | #[no_mangle]
|
157 | 189 | pub extern "C" fn sys_test(a: i8, b: i16) -> i32 {
|
158 | 190 | extern "C" fn __sys_test(a: i8, b: i16) -> i32 {
|
159 |
| - let c = i16::from(a) + b; |
160 |
| - i32::from(c) |
| 191 | + #[allow(unreachable_code)] |
| 192 | + #[allow(clippy::diverging_sub_expression)] |
| 193 | + { |
| 194 | + #[cfg(feature = "strace")] |
| 195 | + print!("sys_test(a = {:?}, b = {:?}) = ", a, b); |
| 196 | + let ret = { |
| 197 | + let c = i16::from(a) + b; |
| 198 | + i32::from(c) |
| 199 | + }; |
| 200 | + #[cfg(feature = "strace")] |
| 201 | + println!("{ret:?}"); |
| 202 | + ret |
| 203 | + } |
161 | 204 | }
|
162 | 205 |
|
163 | 206 | kernel_function!(__sys_test(a, b))
|
@@ -193,8 +236,19 @@ mod tests {
|
193 | 236 | #[no_mangle]
|
194 | 237 | pub unsafe extern "C" fn sys_test(a: i8, b: i16) -> i32 {
|
195 | 238 | unsafe extern "C" fn __sys_test(a: i8, b: i16) -> i32 {
|
196 |
| - let c = i16::from(a) + b; |
197 |
| - i32::from(c) |
| 239 | + #[allow(unreachable_code)] |
| 240 | + #[allow(clippy::diverging_sub_expression)] |
| 241 | + { |
| 242 | + #[cfg(feature = "strace")] |
| 243 | + print!("sys_test(a = {:?}, b = {:?}) = ", a, b); |
| 244 | + let ret = { |
| 245 | + let c = i16::from(a) + b; |
| 246 | + i32::from(c) |
| 247 | + }; |
| 248 | + #[cfg(feature = "strace")] |
| 249 | + println!("{ret:?}"); |
| 250 | + ret |
| 251 | + } |
198 | 252 | }
|
199 | 253 |
|
200 | 254 | unsafe { kernel_function!(__sys_test(a, b)) }
|
|
0 commit comments