Skip to content

Commit bce51a2

Browse files
committed
[trace] add 'trace' feature to guest tracing crates
- This allows the use of the 'hyperlight-guest-tracing' without having the tracing enabled - Now the user can select at compile time whether the macros enable tracing or not - Before this change, the crate using the macros from hyperlight-guest-tracing-macro needed to define a features called 'trace_guest' because the generated code would gate everything with that feature - The change makes the generation of code dependent on the 'trace' feature - Some of the variables used in the macros were prefixed with an '_' to avoid warnings when the 'trace' feature is not set Signed-off-by: Doru Blânzeanu <[email protected]>
1 parent 3c1d417 commit bce51a2

File tree

7 files changed

+56
-33
lines changed

7 files changed

+56
-33
lines changed

src/hyperlight_guest/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Provides only the essential building blocks for interacting with the host enviro
1515
anyhow = { version = "1.0.98", default-features = false }
1616
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
1717
hyperlight-common = { workspace = true }
18-
hyperlight-guest-tracing = { workspace = true }
18+
hyperlight-guest-tracing = { workspace = true, default-features = false }
1919

2020
[features]
2121
default = []
22-
trace_guest = []
22+
trace_guest = ["hyperlight-guest-tracing/trace"]

src/hyperlight_guest_bin/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ and third-party code used by our C-API needed to build a native hyperlight-guest
1717
default = ["libc", "printf"]
1818
libc = [] # compile musl libc
1919
printf = [ "libc" ] # compile printf
20-
trace_guest = ["hyperlight-common/trace_guest", "hyperlight-guest/trace_guest"]
20+
trace_guest = ["hyperlight-common/trace_guest", "hyperlight-guest/trace_guest", "hyperlight-guest-tracing/trace"]
2121
mem_profile = ["hyperlight-common/unwind_guest","hyperlight-common/mem_profile"]
2222

2323
[dependencies]
2424
hyperlight-guest = { workspace = true, default-features = false }
2525
hyperlight-common = { workspace = true, default-features = false }
26-
hyperlight-guest-tracing = { workspace = true }
26+
hyperlight-guest-tracing = { workspace = true, default-features = false }
2727
buddy_system_allocator = "0.11.0"
2828
log = { version = "0.4", default-features = false }
2929
spin = "0.10.0"

src/hyperlight_guest_tracing/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ readme.workspace = true
1010
description = """Provides the tracing functionality for the hyperlight guest."""
1111

1212
[dependencies]
13-
hyperlight-common = { workspace = true, default-features = false, features = ["trace_guest"] }
13+
hyperlight-common = { workspace = true, default-features = false }
1414
hyperlight-guest-tracing-macro = { workspace = true }
1515
spin = "0.10.0"
1616

1717
[lints]
1818
workspace = true
19+
20+
[features]
21+
default = []
22+
trace = [ "hyperlight-guest-tracing-macro/trace", "hyperlight-common/trace_guest" ]

src/hyperlight_guest_tracing/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ limitations under the License.
1818
/// Re-export the tracing macros
1919
/// This allows users to use the macros without needing to import them explicitly.
2020
pub use hyperlight_guest_tracing_macro::*;
21+
#[cfg(feature = "trace")]
2122
pub use trace::{create_trace_record, flush_trace_buffer};
2223

2324
/// Maximum length of a trace message in bytes.
@@ -68,6 +69,7 @@ pub mod invariant_tsc {
6869
}
6970
}
7071

72+
#[cfg(feature = "trace")]
7173
mod trace {
7274
// === Dependencies ===
7375
extern crate alloc;

src/hyperlight_guest_tracing_macro/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ syn = { version = "2.0.104", features = ["full"] }
1616

1717
[features]
1818
default = []
19+
trace = []
1920

2021
[lib]
2122
proc-macro = true

src/hyperlight_guest_tracing_macro/src/lib.rs

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ use syn::{ItemFn, parse_macro_input};
2727
/// }
2828
/// ```
2929
///
30-
/// This macro will create a trace record when the function is called, if the `trace_guest`
31-
/// feature is enabled.
30+
/// This macro will create a trace record when the function is called
3231
///
3332
/// The trace record will contain the function name as a string.
3433
/// Note: This macro is intended to be used with the `hyperlight_guest_tracing` crate.
@@ -45,47 +44,57 @@ pub fn trace_function(_attr: TokenStream, item: TokenStream) -> TokenStream {
4544
let fn_output = &input_fn.sig.output;
4645

4746
// Compose entry/exit messages
48-
let entry_msg = format!("> {}", fn_name_str);
49-
let exit_msg = format!("< {}", fn_name_str);
47+
let _entry_msg = format!("> {}", fn_name_str);
48+
let _exit_msg = format!("< {}", fn_name_str);
5049

5150
let expanded = match fn_output {
5251
syn::ReturnType::Default => {
5352
// No return value (unit)
53+
#[cfg(feature = "trace")]
5454
quote! {
5555
#(#fn_attrs)*
5656
#fn_vis #fn_sig {
57-
#[cfg(feature = "trace_guest")]
5857
const _: () = assert!(
59-
#entry_msg.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
58+
#_entry_msg.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
6059
"Trace message exceeds the maximum bytes length",
6160
);
62-
#[cfg(feature = "trace_guest")]
63-
::hyperlight_guest_tracing::create_trace_record(#entry_msg);
61+
::hyperlight_guest_tracing::create_trace_record(#_entry_msg);
6462
// Call the original function body
6563
#fn_block
66-
#[cfg(feature = "trace_guest")]
67-
::hyperlight_guest_tracing::create_trace_record(#exit_msg);
64+
::hyperlight_guest_tracing::create_trace_record(#_exit_msg);
65+
}
66+
}
67+
#[cfg(not(feature = "trace"))]
68+
quote! {
69+
#(#fn_attrs)*
70+
#fn_vis #fn_sig {
71+
#fn_block
6872
}
6973
}
7074
}
7175
syn::ReturnType::Type(_, _) => {
7276
// Has a return value
77+
#[cfg(feature = "trace")]
7378
quote! {
7479
#(#fn_attrs)*
7580
#fn_vis #fn_sig {
76-
#[cfg(feature = "trace_guest")]
7781
const _: () = assert!(
78-
#entry_msg.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
82+
#_entry_msg.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
7983
"Trace message exceeds the maximum bytes length",
8084
);
81-
#[cfg(feature = "trace_guest")]
82-
::hyperlight_guest_tracing::create_trace_record(#entry_msg);
85+
::hyperlight_guest_tracing::create_trace_record(#_entry_msg);
8386
let __trace_result = (|| #fn_block )();
84-
#[cfg(feature = "trace_guest")]
85-
::hyperlight_guest_tracing::create_trace_record(#exit_msg);
87+
::hyperlight_guest_tracing::create_trace_record(#_exit_msg);
8688
__trace_result
8789
}
8890
}
91+
#[cfg(not(feature = "trace"))]
92+
quote! {
93+
#(#fn_attrs)*
94+
#fn_vis #fn_sig {
95+
#fn_block
96+
}
97+
}
8998
}
9099
};
91100

@@ -202,36 +211,41 @@ pub fn trace(input: TokenStream) -> TokenStream {
202211
_ => unreachable!(),
203212
};
204213
if let Some(statement) = parsed.statement {
205-
let entry_msg = format!("+ {}", trace_message);
206-
let exit_msg = format!("- {}", trace_message);
214+
let _entry_msg = format!("+ {}", trace_message);
215+
let _exit_msg = format!("- {}", trace_message);
216+
#[cfg(feature = "trace")]
207217
let expanded = quote! {
208218
{
209-
#[cfg(feature = "trace_guest")]
210219
const _: () = assert!(
211-
#entry_msg.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
220+
#_entry_msg.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
212221
"Trace message exceeds the maximum bytes length",
213222
);
214-
#[cfg(feature = "trace_guest")]
215-
::hyperlight_guest_tracing::create_trace_record(#entry_msg);
223+
::hyperlight_guest_tracing::create_trace_record(#_entry_msg);
216224
let __trace_result = #statement;
217-
#[cfg(feature = "trace_guest")]
218-
::hyperlight_guest_tracing::create_trace_record(#exit_msg);
225+
::hyperlight_guest_tracing::create_trace_record(#_exit_msg);
219226
__trace_result
220227
}
221228
};
229+
#[cfg(not(feature = "trace"))]
230+
let expanded = quote! {
231+
#statement
232+
};
233+
222234
TokenStream::from(expanded)
223235
} else {
236+
#[cfg(feature = "trace")]
224237
let expanded = quote! {
225238
{
226-
#[cfg(feature = "trace_guest")]
227239
const _: () = assert!(
228240
#trace_message.len() <= hyperlight_guest_tracing::MAX_TRACE_MSG_LEN,
229241
"Trace message exceeds the maximum bytes length",
230242
);
231-
#[cfg(feature = "trace_guest")]
232243
::hyperlight_guest_tracing::create_trace_record(#trace_message);
233244
}
234245
};
246+
#[cfg(not(feature = "trace"))]
247+
let expanded = quote! {};
248+
235249
TokenStream::from(expanded)
236250
}
237251
}
@@ -244,12 +258,14 @@ pub fn trace(input: TokenStream) -> TokenStream {
244258
/// ```
245259
#[proc_macro]
246260
pub fn flush(_input: TokenStream) -> TokenStream {
261+
#[cfg(feature = "trace")]
247262
let expanded = quote! {
248263
{
249-
#[cfg(feature = "trace_guest")]
250264
::hyperlight_guest_tracing::flush_trace_buffer();
251265
}
252266
};
267+
#[cfg(not(feature = "trace"))]
268+
let expanded = quote! {};
253269

254270
TokenStream::from(expanded)
255271
}

src/hyperlight_host/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ executable_heap = []
132132
print_debug = []
133133
# Dumps the VM state to a file on unexpected errors or crashes. The path of the file will be printed on stdout and logged.
134134
crashdump = ["dep:chrono"]
135-
trace_guest = ["hyperlight-common/trace_guest", "dep:hyperlight-guest-tracing"]
135+
trace_guest = ["hyperlight-common/trace_guest", "hyperlight-guest-tracing/trace"]
136136
# This feature enables unwinding the guest stack from the host, in
137137
# order to produce stack traces for debugging or profiling.
138138
unwind_guest = [ "trace_guest", "dep:framehop", "dep:fallible-iterator", "hyperlight-common/unwind_guest" ]

0 commit comments

Comments
 (0)