Skip to content

Commit

Permalink
Trace log sys-calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f committed Sep 26, 2023
1 parent 722d0cf commit aefa730
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions core-backend/src/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{
};
use alloc::string::{String, ToString};
use blake2_rfc::blake2b::blake2b;
use core::marker::PhantomData;
use core::{fmt, marker::PhantomData};
use gear_core::{
buffer::{RuntimeBuffer, RuntimeBufferSizeError},
costs::RuntimeCosts,
Expand All @@ -51,41 +51,44 @@ use gsys::{
HashWithValue, TwoHashesWithValue,
};

#[macro_export(local_inner_macros)]
macro_rules! syscall_args_trace {
($val:expr) => {
{
let s = ::core::stringify!($val);
if s.ends_with("_ptr") {
alloc::format!(", {} = {:#x?}", s, $val)
} else {
alloc::format!(", {} = {:?}", s, $val)
}
}
};
($val:expr, $($rest:expr),+) => {
{
let mut s = syscall_args_trace!($val);
s.push_str(&syscall_args_trace!($($rest),+));
s
const PTR_SPECIAL: u32 = u32::MAX;

struct ValueFormatter<'a>(&'a Value);

impl fmt::Display for ValueFormatter<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
Value::I32(i32) => fmt::Display::fmt(i32, f),
Value::I64(i64) => fmt::Display::fmt(i64, f),
Value::F32(f32) => fmt::Display::fmt(f32, f),
Value::F64(f64) => fmt::Display::fmt(f64, f),
}
};
}
}

macro_rules! syscall_trace {
($name:expr, $($args:expr),+) => {
{
::log::trace!(target: "syscalls", "{}{}", $name, syscall_args_trace!($($args),+));
struct ArgsFormatter<'a>(&'a [Value]);

impl fmt::Display for ArgsFormatter<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut iter = self.0.iter();

if let Some(value) = iter.next() {
write!(f, "{}", ValueFormatter(value))?;
}
};
($name:expr) => {
{
::log::trace!(target: "syscalls", "{}", $name);

for value in iter {
write!(f, ", {}", ValueFormatter(value))?;
}

Ok(())
}
}

const PTR_SPECIAL: u32 = u32::MAX;
fn function_name<T>() -> &'static str {
let s = core::any::type_name::<T>();
let pos = s.rfind("::").unwrap();
&s[pos + 2..]
}

/// Actually just wrapper around [`Value`] to implement conversions.
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -345,6 +348,8 @@ where
S: SysCall<Ext, R>,
R: Into<SysCallReturnValue>,
{
log::trace!(target: "syscalls", "{}({})", function_name::<H>(), ArgsFormatter(args));

let (ctx, args) = S::Context::from_args(args)?;
let sys_call = SysCallBuilder::build(handler, args)?;
let (gas, value) = sys_call.execute(caller, ctx)?;
Expand Down

0 comments on commit aefa730

Please sign in to comment.