@@ -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:: 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, 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 } ;
@@ -124,6 +126,7 @@ impl GuestHandle {
124126 /// sends it to the host, and then retrieves the return value.
125127 ///
126128 /// The return value is deserialized into the specified type `T`.
129+ #[ instrument( skip_all, level = "Trace" ) ]
127130 pub fn call_host_function < T : TryFrom < ReturnValue > > (
128131 & self ,
129132 function_name : & str ,
@@ -134,6 +137,7 @@ impl GuestHandle {
134137 self . get_host_return_value :: < T > ( )
135138 }
136139
140+ #[ instrument( skip_all, level = "Trace" ) ]
137141 pub fn get_host_function_details ( & self ) -> HostFunctionDetails {
138142 let peb_ptr = self . peb ( ) . unwrap ( ) ;
139143 let host_function_details_buffer =
@@ -159,24 +163,46 @@ impl GuestHandle {
159163 source_file : & str ,
160164 line : u32 ,
161165 ) {
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 ) ;
166+ // Closure to send log message to host
167+ let send_to_host = || {
168+ let guest_log_data = GuestLogData :: new (
169+ message. to_string ( ) ,
170+ source. to_string ( ) ,
171+ log_level,
172+ caller. to_string ( ) ,
173+ source_file. to_string ( ) ,
174+ line,
175+ ) ;
176+
177+ let bytes: Vec < u8 > = guest_log_data
178+ . try_into ( )
179+ . expect ( "Failed to convert GuestLogData to bytes" ) ;
180+
181+ self . push_shared_output_data ( & bytes)
182+ . expect ( "Unable to push log data to shared output data" ) ;
183+
184+ unsafe {
185+ out32 ( OutBAction :: Log as u16 , 0 ) ;
186+ }
187+ } ;
188+
189+ #[ cfg( feature = "trace_guest" ) ]
190+ if hyperlight_guest_tracing:: is_trace_enabled ( ) {
191+ // If the "trace_guest" feature is enabled and tracing is initialized, log using tracing
192+ tracing:: trace!(
193+ event = message,
194+ level = ?log_level,
195+ code. filepath = source,
196+ caller = caller,
197+ source_file = source_file,
198+ code. lineno = line,
199+ ) ;
200+ } else {
201+ send_to_host ( ) ;
202+ }
203+ #[ cfg( not( feature = "trace_guest" ) ) ]
204+ {
205+ send_to_host ( ) ;
180206 }
181207 }
182208}
0 commit comments