Skip to content

Commit

Permalink
Merge pull request #1385 from hermit-os/strace
Browse files Browse the repository at this point in the history
feat: add strace feature
  • Loading branch information
mkroening authored Sep 16, 2024
2 parents 2a6f779 + a573adb commit d78e94b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 20 deletions.
27 changes: 12 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,28 @@ harness = false
[features]
default = ["pci", "pci-ids", "acpi", "fsgsbase", "smp", "tcp", "dhcpv4", "fuse", "vsock"]
acpi = []
dhcpv4 = [
"smoltcp",
"smoltcp/proto-dhcpv4",
"smoltcp/socket-dhcpv4",
]
common-os = []
dhcpv4 = ["smoltcp", "smoltcp/proto-dhcpv4", "smoltcp/socket-dhcpv4"]
dns = ["smoltcp", "smoltcp/socket-dns"]
fs = ["fuse"]
fuse = ["pci", "dep:fuse-abi", "fuse-abi/num_enum"]
vsock = ["pci"]
fsgsbase = []
fuse = ["pci", "dep:fuse-abi", "fuse-abi/num_enum"]
gem-net = ["tcp", "dep:tock-registers"]
idle-poll = []
mmap = []
newlib = []
nostd = []
pci = ["virtio/pci"]
rtl8139 = ["tcp", "pci"]
semihosting = ["dep:semihosting"]
shell = ["simple-shell"]
smp = []
strace = []
tcp = ["smoltcp", "smoltcp/socket-tcp"]
udp = ["smoltcp", "smoltcp/socket-udp"]
dns = ["smoltcp", "smoltcp/socket-dns"]
trace = []
udp = ["smoltcp", "smoltcp/socket-udp"]
vga = []
common-os = []
nostd = []
semihosting = ["dep:semihosting"]
shell = ["simple-shell"]
idle-poll = []
mmap = []
vsock = ["pci"]

[dependencies]
hermit-macro = { path = "hermit-macro" }
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 d78e94b

Please sign in to comment.