Skip to content

Commit

Permalink
feat: add strace feature
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Sep 16, 2024
1 parent 6b69206 commit a573adb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ rtl8139 = ["tcp", "pci"]
semihosting = ["dep:semihosting"]
shell = ["simple-shell"]
smp = []
strace = []
tcp = ["smoltcp", "smoltcp/socket-tcp"]
trace = []
udp = ["smoltcp", "smoltcp/socket-udp"]
Expand Down
64 changes: 59 additions & 5 deletions hermit-macro/src/system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{parse_quote, Abi, Attribute, Item, ItemFn, Pat, Result, Signature, Visibility};
use syn::{parse_quote, Abi, Attribute, FnArg, Item, ItemFn, Pat, Result, Signature, Visibility};

fn validate_vis(vis: &Visibility) -> Result<()> {
if !matches!(vis, Visibility::Public(_)) {
Expand Down Expand Up @@ -96,6 +96,38 @@ fn emit_func(mut func: ItemFn, sig: &ParsedSig) -> Result<ItemFn> {
func.vis = Visibility::Inherited;
func.attrs.clear();

let input_idents = sig
.inputs
.iter()
.map(|fn_arg| match fn_arg {
FnArg::Typed(pat_type) => match &*pat_type.pat {
Pat::Ident(pat_ident) => &pat_ident.ident,
_ => unreachable!(),
},
_ => unreachable!(),
})
.collect::<Vec<_>>();
let input_format = input_idents
.iter()
.map(|ident| format!("{ident} = {{:?}}"))
.collect::<Vec<_>>()
.join(", ");
let strace_format = format!("{}({input_format}) = ", sig.ident);

let block = func.block;
func.block = parse_quote! {{
#[allow(unreachable_code)]
#[allow(clippy::diverging_sub_expression)]
{
#[cfg(feature = "strace")]
print!(#strace_format, #(#input_idents),*);
let ret = #block;
#[cfg(feature = "strace")]
println!("{ret:?}");
ret
}
}};

let func_call = quote! {
kernel_function!(#ident(#(#args),*))
};
Expand Down Expand Up @@ -156,8 +188,19 @@ mod tests {
#[no_mangle]
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 {
extern "C" fn __sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
#[allow(unreachable_code)]
#[allow(clippy::diverging_sub_expression)]
{
#[cfg(feature = "strace")]
print!("sys_test(a = {:?}, b = {:?}) = ", a, b);
let ret = {
let c = i16::from(a) + b;
i32::from(c)
};
#[cfg(feature = "strace")]
println!("{ret:?}");
ret
}
}

kernel_function!(__sys_test(a, b))
Expand Down Expand Up @@ -193,8 +236,19 @@ mod tests {
#[no_mangle]
pub unsafe extern "C" fn sys_test(a: i8, b: i16) -> i32 {
unsafe extern "C" fn __sys_test(a: i8, b: i16) -> i32 {
let c = i16::from(a) + b;
i32::from(c)
#[allow(unreachable_code)]
#[allow(clippy::diverging_sub_expression)]
{
#[cfg(feature = "strace")]
print!("sys_test(a = {:?}, b = {:?}) = ", a, b);
let ret = {
let c = i16::from(a) + b;
i32::from(c)
};
#[cfg(feature = "strace")]
println!("{ret:?}");
ret
}
}

unsafe { kernel_function!(__sys_test(a, b)) }
Expand Down

0 comments on commit a573adb

Please sign in to comment.