@@ -30,13 +30,15 @@ use hyperlight_common::flatbuffer_wrappers::guest_log_level::LogLevel;
30
30
use hyperlight_common:: flatbuffer_wrappers:: host_function_details:: HostFunctionDetails ;
31
31
use hyperlight_common:: flatbuffer_wrappers:: util:: estimate_flatbuffer_capacity;
32
32
use hyperlight_common:: outb:: OutBAction ;
33
+ use tracing:: { Span , instrument} ;
33
34
34
35
use super :: handle:: GuestHandle ;
35
36
use crate :: error:: { HyperlightGuestError , Result } ;
36
37
use crate :: exit:: out32;
37
38
38
39
impl GuestHandle {
39
40
/// Get user memory region as bytes.
41
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
40
42
pub fn read_n_bytes_from_user_memory ( & self , num : u64 ) -> Result < Vec < u8 > > {
41
43
let peb_ptr = self . peb ( ) . unwrap ( ) ;
42
44
let user_memory_region_ptr = unsafe { ( * peb_ptr) . init_data . ptr as * mut u8 } ;
@@ -65,6 +67,7 @@ impl GuestHandle {
65
67
///
66
68
/// When calling `call_host_function<T>`, this function is called
67
69
/// internally to get the return value.
70
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
68
71
pub fn get_host_return_value < T : TryFrom < ReturnValue > > ( & self ) -> Result < T > {
69
72
let return_value = self
70
73
. try_pop_shared_input_data_into :: < ReturnValue > ( )
@@ -85,6 +88,7 @@ impl GuestHandle {
85
88
///
86
89
/// Note: The function return value must be obtained by calling
87
90
/// `get_host_return_value`.
91
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
88
92
pub fn call_host_function_without_returning_result (
89
93
& self ,
90
94
function_name : & str ,
@@ -118,6 +122,7 @@ impl GuestHandle {
118
122
/// sends it to the host, and then retrieves the return value.
119
123
///
120
124
/// The return value is deserialized into the specified type `T`.
125
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
121
126
pub fn call_host_function < T : TryFrom < ReturnValue > > (
122
127
& self ,
123
128
function_name : & str ,
@@ -128,6 +133,7 @@ impl GuestHandle {
128
133
self . get_host_return_value :: < T > ( )
129
134
}
130
135
136
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
131
137
pub fn get_host_function_details ( & self ) -> HostFunctionDetails {
132
138
let peb_ptr = self . peb ( ) . unwrap ( ) ;
133
139
let host_function_details_buffer =
@@ -144,6 +150,7 @@ impl GuestHandle {
144
150
}
145
151
146
152
/// Write an error to the shared output data buffer.
153
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
147
154
pub fn write_error ( & self , error_code : ErrorCode , message : Option < & str > ) {
148
155
let guest_error: GuestError = GuestError :: new (
149
156
error_code,
@@ -159,6 +166,7 @@ impl GuestHandle {
159
166
}
160
167
161
168
/// Log a message with the specified log level, source, caller, source file, and line number.
169
+ #[ instrument( skip_all, parent = Span :: current( ) , level= "Trace" ) ]
162
170
pub fn log_message (
163
171
& self ,
164
172
log_level : LogLevel ,
@@ -168,24 +176,46 @@ impl GuestHandle {
168
176
source_file : & str ,
169
177
line : u32 ,
170
178
) {
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 ( ) ;
189
219
}
190
220
}
191
221
}
0 commit comments