From a608e87ff9daa417b70546d2f766937227573b8b Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 4 May 2023 00:39:17 +0330 Subject: [PATCH] LibWasm: Show wasi function arguments and result in debug mode --- Userland/Libraries/LibWasm/WASI/Wasi.cpp | 87 +++++++++++++++++++++++- 1 file changed, 85 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWasm/WASI/Wasi.cpp b/Userland/Libraries/LibWasm/WASI/Wasi.cpp index 68a2ac96846..d01708b0178 100644 --- a/Userland/Libraries/LibWasm/WASI/Wasi.cpp +++ b/Userland/Libraries/LibWasm/WASI/Wasi.cpp @@ -850,8 +850,7 @@ struct InvocationOf { .template operator()(MakeIndexSequence()); auto result = args.apply_as_args([&](auto&&... impl_args) { return (self.*impl)(configuration, impl_args...); }); - dbgln_if(WASI_DEBUG, "WASI: Called {}", function_name); - + dbgln_if(WASI_DEBUG, "WASI: {}({}) = {}", function_name, arguments, result); if (result.is_error()) return Wasm::Trap { DeprecatedString::formatted("Invalid call to {}() = {}", function_name, result.error()) }; @@ -1027,3 +1026,87 @@ Errno errno_value_from_errno(int value) } } } + +namespace AK { +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Value const& value) + { + return value.value().visit( + [&](Wasm::Reference const&) { + return Formatter::format(builder, "({}) &r"sv, Wasm::ValueType::kind_name(value.type().kind())); + }, + [&](auto const& v) { + return Formatter::format(builder, "({}) {}"sv, Wasm::ValueType::kind_name(value.type().kind()), v); + }); + } +}; + +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Wasi::Errno const& value) + { + return Formatter::format(builder, "{}"sv, to_underlying(value)); + } +}; + +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder&, Empty) + { + return {}; + } +}; + +template +struct Formatter> : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Wasi::Result const& value) + { + if (value.is_error()) + return Formatter::format(builder, "Error({})"sv, *value.error()); + + return Formatter::format(builder, "Ok({})"sv, *value.result()); + } +}; + +template T> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, T const& value) + { + return Formatter::format(builder, "size={}, count={}"sv, value.size, value.count); + } +}; + +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Wasi::FDStat const&) + { + return Formatter::format(builder, "(rights)"sv); + } +}; + +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Wasi::FileStat const& value) + { + return Formatter::format(builder, "dev={}, ino={}, ft={}, nlink={}, size={}, atim={}, mtim={}, ctim={}"sv, + value.dev, value.ino, to_underlying(value.filetype), value.nlink, value.size, value.atim, value.mtim, value.ctim); + } +}; + +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Wasi::PreStat const& value) + { + return Formatter::format(builder, "length={}"sv, value.dir.pr_name_len); + } +}; + +template<> +struct Formatter : AK::Formatter { + ErrorOr format(FormatBuilder& builder, Wasm::Wasi::SockRecvResult const& value) + { + return Formatter::format(builder, "size={}"sv, value.size); + } +}; +}