Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add strace feature #1385

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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