@@ -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 return_value = self
7073 . try_pop_shared_input_data_into :: < ReturnValue > ( )
@@ -85,6 +88,7 @@ impl GuestHandle {
8588 ///
8689 /// Note: The function return value must be obtained by calling
8790 /// `get_host_return_value`.
91+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
8892 pub fn call_host_function_without_returning_result (
8993 & self ,
9094 function_name : & str ,
@@ -118,6 +122,7 @@ impl GuestHandle {
118122 /// sends it to the host, and then retrieves the return value.
119123 ///
120124 /// The return value is deserialized into the specified type `T`.
125+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
121126 pub fn call_host_function < T : TryFrom < ReturnValue > > (
122127 & self ,
123128 function_name : & str ,
@@ -128,6 +133,7 @@ impl GuestHandle {
128133 self . get_host_return_value :: < T > ( )
129134 }
130135
136+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
131137 pub fn get_host_function_details ( & self ) -> HostFunctionDetails {
132138 let peb_ptr = self . peb ( ) . unwrap ( ) ;
133139 let host_function_details_buffer =
@@ -144,6 +150,7 @@ impl GuestHandle {
144150 }
145151
146152 /// Write an error to the shared output data buffer.
153+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
147154 pub fn write_error ( & self , error_code : ErrorCode , message : Option < & str > ) {
148155 let guest_error: GuestError = GuestError :: new (
149156 error_code,
@@ -159,6 +166,7 @@ impl GuestHandle {
159166 }
160167
161168 /// Log a message with the specified log level, source, caller, source file, and line number.
169+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
162170 pub fn log_message (
163171 & self ,
164172 log_level : LogLevel ,
@@ -168,24 +176,46 @@ impl GuestHandle {
168176 source_file : & str ,
169177 line : u32 ,
170178 ) {
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 ) ;
179+ // Closure to send log message to host
180+ let send_to_host = || {
181+ let guest_log_data = GuestLogData :: new (
182+ message. to_string ( ) ,
183+ source. to_string ( ) ,
184+ log_level,
185+ caller. to_string ( ) ,
186+ source_file. to_string ( ) ,
187+ line,
188+ ) ;
189+
190+ let bytes: Vec < u8 > = guest_log_data
191+ . try_into ( )
192+ . expect ( "Failed to convert GuestLogData to bytes" ) ;
193+
194+ self . push_shared_output_data ( & bytes)
195+ . expect ( "Unable to push log data to shared output data" ) ;
196+
197+ unsafe {
198+ out32 ( OutBAction :: Log as u16 , 0 ) ;
199+ }
200+ } ;
201+
202+ #[ cfg( feature = "trace_guest" ) ]
203+ if hyperlight_guest_tracing:: is_trace_enabled ( ) {
204+ // If the "trace_guest" feature is enabled and tracing is initialized, log using tracing
205+ tracing:: trace!(
206+ event = message,
207+ level = ?log_level,
208+ code. filepath = source,
209+ caller = caller,
210+ source_file = source_file,
211+ code. lineno = line,
212+ ) ;
213+ } else {
214+ send_to_host ( ) ;
215+ }
216+ #[ cfg( not( feature = "trace_guest" ) ) ]
217+ {
218+ send_to_host ( ) ;
189219 }
190220 }
191221}
0 commit comments