@@ -30,13 +30,15 @@ use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
3030use hyperlight_common:: flatbuffer_wrappers:: host_function_details:: HostFunctionDetails ;
3131use hyperlight_common:: flatbuffer_wrappers:: util:: estimate_flatbuffer_capacity;
3232use hyperlight_common:: outb:: OutBAction ;
33+ use tracing:: { Span , instrument} ;
3334
3435use super :: handle:: GuestHandle ;
3536use crate :: error:: { HyperlightGuestError , Result } ;
3637use crate :: exit:: out32;
3738
3839impl GuestHandle {
3940 /// Get user memory region as bytes.
41+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
4042 pub fn read_n_bytes_from_user_memory ( & self , num : u64 ) -> Result < Vec < u8 > > {
4143 let peb_ptr = self . peb ( ) . unwrap ( ) ;
4244 let user_memory_region_ptr = unsafe { ( * peb_ptr) . init_data . ptr as * mut u8 } ;
@@ -65,6 +67,7 @@ impl GuestHandle {
6567 ///
6668 /// When calling `call_host_function<T>`, this function is called
6769 /// internally to get the return value.
70+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
6871 pub fn get_host_return_value < T : TryFrom < ReturnValue > > ( & self ) -> Result < T > {
6972 let inner = self
7073 . try_pop_shared_input_data_into :: < FunctionCallResult > ( )
@@ -91,6 +94,7 @@ impl GuestHandle {
9194 ///
9295 /// Note: The function return value must be obtained by calling
9396 /// `get_host_return_value`.
97+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
9498 pub fn call_host_function_without_returning_result (
9599 & self ,
96100 function_name : & str ,
@@ -124,6 +128,7 @@ impl GuestHandle {
124128 /// sends it to the host, and then retrieves the return value.
125129 ///
126130 /// The return value is deserialized into the specified type `T`.
131+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
127132 pub fn call_host_function < T : TryFrom < ReturnValue > > (
128133 & self ,
129134 function_name : & str ,
@@ -134,6 +139,7 @@ impl GuestHandle {
134139 self . get_host_return_value :: < T > ( )
135140 }
136141
142+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
137143 pub fn get_host_function_details ( & self ) -> HostFunctionDetails {
138144 let peb_ptr = self . peb ( ) . unwrap ( ) ;
139145 let host_function_details_buffer =
@@ -150,6 +156,7 @@ impl GuestHandle {
150156 }
151157
152158 /// Log a message with the specified log level, source, caller, source file, and line number.
159+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
153160 pub fn log_message (
154161 & self ,
155162 log_level : LogLevel ,
@@ -159,24 +166,46 @@ impl GuestHandle {
159166 source_file : & str ,
160167 line : u32 ,
161168 ) {
162- let guest_log_data = GuestLogData :: new (
163- message. to_string ( ) ,
164- source. to_string ( ) ,
165- log_level,
166- caller. to_string ( ) ,
167- source_file. to_string ( ) ,
168- line,
169- ) ;
170-
171- let bytes: Vec < u8 > = guest_log_data
172- . try_into ( )
173- . expect ( "Failed to convert GuestLogData to bytes" ) ;
174-
175- self . push_shared_output_data ( & bytes)
176- . expect ( "Unable to push log data to shared output data" ) ;
177-
178- unsafe {
179- out32 ( OutBAction :: Log as u16 , 0 ) ;
169+ // Closure to send log message to host
170+ let send_to_host = || {
171+ let guest_log_data = GuestLogData :: new (
172+ message. to_string ( ) ,
173+ source. to_string ( ) ,
174+ log_level,
175+ caller. to_string ( ) ,
176+ source_file. to_string ( ) ,
177+ line,
178+ ) ;
179+
180+ let bytes: Vec < u8 > = guest_log_data
181+ . try_into ( )
182+ . expect ( "Failed to convert GuestLogData to bytes" ) ;
183+
184+ self . push_shared_output_data ( & bytes)
185+ . expect ( "Unable to push log data to shared output data" ) ;
186+
187+ unsafe {
188+ out32 ( OutBAction :: Log as u16 , 0 ) ;
189+ }
190+ } ;
191+
192+ #[ cfg( feature = "trace_guest" ) ]
193+ if hyperlight_guest_tracing:: is_trace_enabled ( ) {
194+ // If the "trace_guest" feature is enabled and tracing is initialized, log using tracing
195+ tracing:: trace!(
196+ event = message,
197+ level = ?log_level,
198+ code. filepath = source,
199+ caller = caller,
200+ source_file = source_file,
201+ code. lineno = line,
202+ ) ;
203+ } else {
204+ send_to_host ( ) ;
205+ }
206+ #[ cfg( not( feature = "trace_guest" ) ) ]
207+ {
208+ send_to_host ( ) ;
180209 }
181210 }
182211}
0 commit comments