diff --git a/src/hyperlight_guest/src/exit.rs b/src/hyperlight_guest/src/exit.rs index cc91aad9e..d7fe2f7a3 100644 --- a/src/hyperlight_guest/src/exit.rs +++ b/src/hyperlight_guest/src/exit.rs @@ -22,7 +22,7 @@ use tracing::instrument; /// Halt the execution of the guest and returns control to the host. #[inline(never)] -#[instrument(skip_all, level = "Trace")] +#[instrument(skip_all, level = "Info")] pub fn halt() { #[cfg(feature = "trace_guest")] { @@ -123,7 +123,7 @@ pub(crate) fn outb(port: u16, data: &[u8]) { } /// OUT function for sending a 32-bit value to the host. -#[instrument(skip_all, level = "Trace")] +#[instrument(skip_all, level = "Info")] pub(crate) unsafe fn out32(port: u16, val: u32) { #[cfg(feature = "trace_guest")] { diff --git a/src/hyperlight_guest/src/guest_handle/host_comm.rs b/src/hyperlight_guest/src/guest_handle/host_comm.rs index 8ae22fb49..be54e72c3 100644 --- a/src/hyperlight_guest/src/guest_handle/host_comm.rs +++ b/src/hyperlight_guest/src/guest_handle/host_comm.rs @@ -67,6 +67,7 @@ impl GuestHandle { /// /// When calling `call_host_function`, this function is called /// internally to get the return value. + #[instrument(skip_all, level = "Trace")] pub fn get_host_return_value>(&self) -> Result { let inner = self .try_pop_shared_input_data_into::() @@ -93,6 +94,7 @@ impl GuestHandle { /// /// Note: The function return value must be obtained by calling /// `get_host_return_value`. + #[instrument(skip_all, level = "Trace")] pub fn call_host_function_without_returning_result( &self, function_name: &str, @@ -126,7 +128,7 @@ impl GuestHandle { /// sends it to the host, and then retrieves the return value. /// /// The return value is deserialized into the specified type `T`. - #[instrument(skip_all, level = "Trace")] + #[instrument(skip_all, level = "Info")] pub fn call_host_function>( &self, function_name: &str, @@ -154,6 +156,7 @@ impl GuestHandle { } /// Log a message with the specified log level, source, caller, source file, and line number. + #[instrument(skip_all, level = "Trace")] pub fn log_message( &self, log_level: LogLevel, diff --git a/src/hyperlight_guest/src/guest_handle/io.rs b/src/hyperlight_guest/src/guest_handle/io.rs index a494033dd..716a97c63 100644 --- a/src/hyperlight_guest/src/guest_handle/io.rs +++ b/src/hyperlight_guest/src/guest_handle/io.rs @@ -20,14 +20,14 @@ use core::any::type_name; use core::slice::from_raw_parts_mut; use hyperlight_common::flatbuffer_wrappers::guest_error::ErrorCode; -use tracing::{Span, instrument}; +use tracing::instrument; use super::handle::GuestHandle; use crate::error::{HyperlightGuestError, Result}; impl GuestHandle { /// Pops the top element from the shared input data buffer and returns it as a T - #[instrument(skip_all, parent = Span::current(), level= "Trace")] + #[instrument(skip_all, level = "Trace")] pub fn try_pop_shared_input_data_into(&self) -> Result where T: for<'a> TryFrom<&'a [u8]>, @@ -89,7 +89,7 @@ impl GuestHandle { } /// Pushes the given data onto the shared output data buffer. - #[instrument(skip_all, parent = Span::current(), level= "Trace")] + #[instrument(skip_all, level = "Trace")] pub fn push_shared_output_data(&self, data: &[u8]) -> Result<()> { let peb_ptr = self.peb().unwrap(); let output_stack_size = unsafe { (*peb_ptr).output_stack.size as usize }; diff --git a/src/hyperlight_guest_bin/src/guest_function/call.rs b/src/hyperlight_guest_bin/src/guest_function/call.rs index b5bba301b..05b435d47 100644 --- a/src/hyperlight_guest_bin/src/guest_function/call.rs +++ b/src/hyperlight_guest_bin/src/guest_function/call.rs @@ -23,13 +23,13 @@ use hyperlight_common::flatbuffer_wrappers::function_types::{FunctionCallResult, use hyperlight_common::flatbuffer_wrappers::guest_error::{ErrorCode, GuestError}; use hyperlight_guest::error::{HyperlightGuestError, Result}; use hyperlight_guest::exit::halt; -use tracing::{Span, instrument}; +use tracing::instrument; use crate::{GUEST_HANDLE, REGISTERED_GUEST_FUNCTIONS}; type GuestFunc = fn(&FunctionCall) -> Result>; -#[instrument(skip_all, parent = Span::current(), level= "Trace")] +#[instrument(skip_all, level = "Info")] pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result> { // Validate this is a Guest Function Call if function_call.function_call_type() != FunctionCallType::Guest { @@ -85,7 +85,7 @@ pub(crate) fn call_guest_function(function_call: FunctionCall) -> Result // This function may panic, as we have no other ways of dealing with errors at this level #[unsafe(no_mangle)] #[inline(never)] -#[instrument(skip_all, parent = Span::current(), level= "Trace")] +#[instrument(skip_all, level = "Trace")] fn internal_dispatch_function() { let handle = unsafe { GUEST_HANDLE }; @@ -119,7 +119,7 @@ fn internal_dispatch_function() { // This is implemented as a separate function to make sure that epilogue in the internal_dispatch_function is called before the halt() // which if it were included in the internal_dispatch_function cause the epilogue to not be called because the halt() would not return // when running in the hypervisor. -#[instrument(skip_all, parent = Span::current(), level= "Trace")] +#[instrument(skip_all, level = "Info")] pub(crate) extern "C" fn dispatch_function() { // The hyperlight host likes to use one partition and reset it in // various ways; if that has happened, there might stale TLB diff --git a/src/hyperlight_guest_bin/src/lib.rs b/src/hyperlight_guest_bin/src/lib.rs index 240e29ac9..5ae057429 100644 --- a/src/hyperlight_guest_bin/src/lib.rs +++ b/src/hyperlight_guest_bin/src/lib.rs @@ -236,7 +236,7 @@ pub extern "C" fn entrypoint(peb_address: u64, seed: u64, ops: u64, max_log_leve // It is important that all the tracing events are produced after the tracing is initialized. #[cfg(feature = "trace_guest")] if max_log_level != LevelFilter::Off { - hyperlight_guest_tracing::init_guest_tracing(guest_start_tsc); + hyperlight_guest_tracing::init_guest_tracing(guest_start_tsc, max_log_level); } hyperlight_main(); diff --git a/src/hyperlight_guest_bin/src/paging.rs b/src/hyperlight_guest_bin/src/paging.rs index 4ee3d827a..49c4da841 100644 --- a/src/hyperlight_guest_bin/src/paging.rs +++ b/src/hyperlight_guest_bin/src/paging.rs @@ -17,7 +17,7 @@ limitations under the License. use alloc::alloc::Layout; use core::arch::asm; -use tracing::{Span, instrument}; +use tracing::instrument; use crate::OS_PAGE_SIZE; @@ -63,7 +63,7 @@ struct MapResponse { /// as such do not use concurrently with any other page table operations /// - TLB invalidation is not performed, /// if previously-unmapped ranges are not being mapped, TLB invalidation may need to be performed afterwards. -#[instrument(skip_all, parent = Span::current(), level= "Trace")] +#[instrument(skip_all, level = "Trace")] pub unsafe fn map_region(phys_base: u64, virt_base: *mut u8, len: u64) { let mut pml4_base: u64; unsafe { diff --git a/src/hyperlight_guest_tracing/Cargo.toml b/src/hyperlight_guest_tracing/Cargo.toml index 5109aba85..a0bc60a51 100644 --- a/src/hyperlight_guest_tracing/Cargo.toml +++ b/src/hyperlight_guest_tracing/Cargo.toml @@ -13,7 +13,7 @@ description = """Provides the tracing functionality for the hyperlight guest.""" heapless = { version = "0.9.1", features = ["serde"] } hyperlight-common = { workspace = true, default-features = false } spin = "0.10.0" -tracing = { version = "0.1.41", default-features = false, features = ["attributes"] } +tracing = { version = "0.1.41", default-features = false, features = ["attributes", "log"] } tracing-core = { version = "0.1.34", default-features = false } [lints] diff --git a/src/hyperlight_guest_tracing/src/lib.rs b/src/hyperlight_guest_tracing/src/lib.rs index ded4e91aa..b134c638d 100644 --- a/src/hyperlight_guest_tracing/src/lib.rs +++ b/src/hyperlight_guest_tracing/src/lib.rs @@ -146,6 +146,7 @@ mod trace { use alloc::sync::{Arc, Weak}; use spin::Mutex; + use tracing::log::LevelFilter; use super::*; use crate::state::GuestState; @@ -155,12 +156,12 @@ mod trace { static GUEST_STATE: spin::Once>> = spin::Once::new(); /// Initialize the guest tracing subscriber as global default. - pub fn init_guest_tracing(guest_start_tsc: u64) { + pub fn init_guest_tracing(guest_start_tsc: u64, max_log_level: LevelFilter) { // Set as global default if not already set. if tracing_core::dispatcher::has_been_set() { return; } - let sub = GuestSubscriber::new(guest_start_tsc); + let sub = GuestSubscriber::new(guest_start_tsc, max_log_level); let state = sub.state(); // Store state Weak to use later at runtime GUEST_STATE.call_once(|| Arc::downgrade(state)); diff --git a/src/hyperlight_guest_tracing/src/subscriber.rs b/src/hyperlight_guest_tracing/src/subscriber.rs index fca202bb0..aca9466d9 100644 --- a/src/hyperlight_guest_tracing/src/subscriber.rs +++ b/src/hyperlight_guest_tracing/src/subscriber.rs @@ -20,7 +20,7 @@ use alloc::sync::Arc; use spin::Mutex; use tracing_core::span::{Attributes, Id, Record}; use tracing_core::subscriber::Subscriber; -use tracing_core::{Event, Metadata}; +use tracing_core::{Event, LevelFilter, Metadata}; use crate::state::GuestState; @@ -30,22 +30,42 @@ pub(crate) struct GuestSubscriber { /// Protected by a Mutex for inner mutability /// A reference to this state is stored in a static variable state: Arc>, + /// Maximum log level to record + max_log_level: LevelFilter, +} + +/// Converts a `tracing::log::LevelFilter` to a `tracing_core::LevelFilter` +/// Used to check if an event should be recorded based on the maximum log level +fn convert_level_filter(filter: tracing::log::LevelFilter) -> tracing_core::LevelFilter { + match filter { + tracing::log::LevelFilter::Off => tracing_core::LevelFilter::OFF, + tracing::log::LevelFilter::Error => tracing_core::LevelFilter::ERROR, + tracing::log::LevelFilter::Warn => tracing_core::LevelFilter::WARN, + tracing::log::LevelFilter::Info => tracing_core::LevelFilter::INFO, + tracing::log::LevelFilter::Debug => tracing_core::LevelFilter::DEBUG, + tracing::log::LevelFilter::Trace => tracing_core::LevelFilter::TRACE, + } } impl GuestSubscriber { - pub(crate) fn new(guest_start_tsc: u64) -> Self { + /// Creates a new `GuestSubscriber` with the given guest start TSC and maximum log level + pub(crate) fn new(guest_start_tsc: u64, max_log_level: tracing::log::LevelFilter) -> Self { Self { state: Arc::new(Mutex::new(GuestState::new(guest_start_tsc))), + max_log_level: convert_level_filter(max_log_level), } } + /// Returns a reference to the internal state of the subscriber + /// This is used to access the spans and events collected by the subscriber pub(crate) fn state(&self) -> &Arc> { &self.state } } impl Subscriber for GuestSubscriber { - fn enabled(&self, _md: &Metadata<'_>) -> bool { - true + fn enabled(&self, md: &Metadata<'_>) -> bool { + // Check if the metadata level is less than or equal to the maximum log level filter + self.max_log_level >= *md.level() } fn new_span(&self, attrs: &Attributes<'_>) -> Id { diff --git a/src/hyperlight_host/src/sandbox/trace/context.rs b/src/hyperlight_host/src/sandbox/trace/context.rs index f17a466ef..85c7f16ad 100644 --- a/src/hyperlight_host/src/sandbox/trace/context.rs +++ b/src/hyperlight_host/src/sandbox/trace/context.rs @@ -131,7 +131,7 @@ impl TraceContext { let current_ctx = Span::current().context(); - let span = tracing::trace_span!("call-to-guest"); + let span = tracing::info_span!("call-to-guest"); let _ = span.set_parent(current_ctx); let entered = span.entered(); @@ -324,7 +324,7 @@ impl TraceContext { } pub fn new_host_trace(&mut self, ctx: Context) { - let span = tracing::trace_span!("call-to-host"); + let span = tracing::info_span!("call-to-host"); let _ = span.set_parent(ctx); let entered = span.entered(); self.host_spans.push(entered); diff --git a/src/tests/rust_guests/dummyguest/Cargo.lock b/src/tests/rust_guests/dummyguest/Cargo.lock index 8844a2bb2..be3650f17 100644 --- a/src/tests/rust_guests/dummyguest/Cargo.lock +++ b/src/tests/rust_guests/dummyguest/Cargo.lock @@ -85,9 +85,9 @@ dependencies = [ [[package]] name = "heapless" -version = "0.8.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +checksum = "b1edcd5a338e64688fbdcb7531a846cfd3476a54784dcb918a0844682bc7ada5" dependencies = [ "hash32", "serde", @@ -302,6 +302,7 @@ version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", diff --git a/src/tests/rust_guests/simpleguest/Cargo.lock b/src/tests/rust_guests/simpleguest/Cargo.lock index 5edd3cc30..c63d57295 100644 --- a/src/tests/rust_guests/simpleguest/Cargo.lock +++ b/src/tests/rust_guests/simpleguest/Cargo.lock @@ -77,9 +77,9 @@ dependencies = [ [[package]] name = "heapless" -version = "0.8.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +checksum = "b1edcd5a338e64688fbdcb7531a846cfd3476a54784dcb918a0844682bc7ada5" dependencies = [ "hash32", "serde", @@ -306,6 +306,7 @@ version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core", diff --git a/src/tests/rust_guests/witguest/Cargo.lock b/src/tests/rust_guests/witguest/Cargo.lock index 6172a003c..f9d274838 100644 --- a/src/tests/rust_guests/witguest/Cargo.lock +++ b/src/tests/rust_guests/witguest/Cargo.lock @@ -193,9 +193,9 @@ dependencies = [ [[package]] name = "heapless" -version = "0.8.0" +version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +checksum = "b1edcd5a338e64688fbdcb7531a846cfd3476a54784dcb918a0844682bc7ada5" dependencies = [ "hash32", "serde", @@ -532,9 +532,9 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "syn" -version = "2.0.107" +version = "2.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2a26dbd934e5451d21ef060c018dae56fc073894c5a7896f882928a76e6d081b" +checksum = "da58917d35242480a05c2897064da0a80589a2a0476c9a3f2fdc83b53502e917" dependencies = [ "proc-macro2", "quote", @@ -547,6 +547,7 @@ version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ + "log", "pin-project-lite", "tracing-attributes", "tracing-core",