| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Use of this source code is governed by a MIT-style | |
| // license that can be found in the LICENSE file or at | |
| // https://opensource.org/licenses/MIT | |
| /// Resolves a string parameter to an `&'static InternedString` (for literals and identifiers) or | |
| /// passes dynamic expressions through unchanged. | |
| #[doc(hidden)] | |
| macro_rules! resolve_string { | |
| ($string:ident) => { | |
| $string | |
| }; | |
| ($string:literal) => { | |
| $crate::ktrace::intern_string!($string) | |
| }; | |
| (($string:expr)) => { | |
| $string | |
| }; | |
| ($string:expr) => { | |
| $string | |
| }; | |
| } | |
| /// Resolves a category literal or identifier to `&'static Category` at compile time. | |
| #[doc(hidden)] | |
| macro_rules! resolve_category { | |
| ($category:ident) => { | |
| &$category | |
| }; | |
| ($category:literal) => { | |
| const { $crate::ktrace::Category::lookup_const($category) } | |
| }; | |
| } | |
| /// Returns true if the given trace category is currently enabled. | |
| macro_rules! category_enabled { | |
| ($category:tt) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| $crate::ktrace::KTrace::category_enabled(category) | |
| }}; | |
| } | |
| #[doc(hidden)] | |
| macro_rules! __args { | |
| ($($key:tt => $val:expr),*) => { | |
| [$($crate::ktrace::Argument::new($crate::ktrace::resolve_string!($key), $val)),*] | |
| }; | |
| } | |
| #[doc(hidden)] | |
| macro_rules! __emit_event { | |
| ( | |
| @simple $event_type:ident, | |
| $context:ident, | |
| $category:tt, | |
| $label:tt | |
| $(, $key:tt => $val:expr)* | |
| ) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| $crate::ktrace::outlined(|| { | |
| $crate::ktrace::KTrace::emit_simple_event( | |
| $crate::ktrace::EventType::$event_type, | |
| $crate::ktrace::Context::$context, | |
| category, | |
| $crate::ktrace::resolve_string!($label), | |
| &$crate::ktrace::__args!($($key => $val),*), | |
| ); | |
| }); | |
| } | |
| }}; | |
| ( | |
| @now $event_type:ident, | |
| $trailing:expr, | |
| $context:ident, | |
| $category:tt, | |
| $label:tt | |
| $(, $key:tt => $val:expr)* | |
| ) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| $crate::ktrace::outlined(|| { | |
| $crate::ktrace::KTrace::emit_event_now( | |
| $crate::ktrace::EventType::$event_type, | |
| $trailing, | |
| $crate::ktrace::Context::$context, | |
| category, | |
| $crate::ktrace::resolve_string!($label), | |
| &$crate::ktrace::__args!($($key => $val),*), | |
| ); | |
| }); | |
| } | |
| }}; | |
| ( | |
| @timestamp $kind:expr, | |
| $context:ident, | |
| $category:tt, | |
| $label:tt, | |
| $ts:expr | |
| $(, $key:tt => $val:expr)* | |
| ) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| $crate::ktrace::outlined(|| { | |
| $crate::ktrace::KTrace::emit_event( | |
| $kind, | |
| $crate::ktrace::Context::$context, | |
| category, | |
| $crate::ktrace::resolve_string!($label), | |
| $ts, | |
| &$crate::ktrace::__args!($($key => $val),*), | |
| ); | |
| }); | |
| } | |
| }}; | |
| } | |
| /// Writes an instant event associated with the current thread when the category is enabled. | |
| macro_rules! instant { | |
| ($category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @simple Instant, | |
| Thread, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes an instant event associated with the current CPU when the category is enabled. | |
| macro_rules! cpu_instant { | |
| ($category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!(@simple Instant, Cpu, $category, $label $(, $key => $val)*) | |
| }; | |
| } | |
| /// Writes a duration begin event with an explicit timestamp on the current thread. | |
| macro_rules! duration_begin_timestamp { | |
| ($category:tt, $label:tt, $timestamp:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::DurationBegin, | |
| Thread, | |
| $category, | |
| $label, | |
| $timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a duration begin event associated with the current thread when the category is enabled. | |
| macro_rules! duration_begin { | |
| ($category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @simple DurationBegin, | |
| Thread, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a duration end event with an explicit timestamp on the current thread. | |
| macro_rules! duration_end_timestamp { | |
| ($category:tt, $label:tt, $timestamp:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::DurationEnd, | |
| Thread, | |
| $category, | |
| $label, | |
| $timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a duration end event associated with the current thread when the category is enabled. | |
| macro_rules! duration_end { | |
| ($category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @simple DurationEnd, | |
| Thread, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a counter event with an explicit timestamp on the current CPU. | |
| macro_rules! cpu_counter_timestamp { | |
| ( | |
| $category:tt, | |
| $label:tt, | |
| $timestamp:expr, | |
| $counter_id:expr | |
| $(, $key:tt => $val:expr)* $(,)? | |
| ) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::Counter { id: $counter_id as u64 }, | |
| Cpu, | |
| $category, | |
| $label, | |
| $timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a counter event associated with the current CPU when the category is enabled. | |
| macro_rules! cpu_counter { | |
| ($category:tt, $label:tt, $counter_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @now Counter, | |
| $counter_id as u64, | |
| Cpu, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow begin event with an explicit timestamp on the current thread. | |
| macro_rules! flow_begin_timestamp { | |
| ($category:tt, $label:tt, $timestamp:expr, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::FlowBegin { id: $flow_id as u64 }, | |
| Thread, | |
| $category, | |
| $label, | |
| $timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow begin event associated with the current thread when the category is enabled. | |
| macro_rules! flow_begin { | |
| ($category:tt, $label:tt, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @now FlowBegin, | |
| $flow_id as u64, | |
| Thread, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow begin event associated with the current CPU when the category is enabled. | |
| macro_rules! cpu_flow_begin { | |
| ($category:tt, $label:tt, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @now FlowBegin, | |
| $flow_id as u64, | |
| Cpu, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow step event with an explicit timestamp on the current thread. | |
| macro_rules! flow_step_timestamp { | |
| ($category:tt, $label:tt, $timestamp:expr, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::FlowStep { id: $flow_id as u64 }, | |
| Thread, | |
| $category, | |
| $label, | |
| $timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow step event associated with the current CPU when the category is enabled. | |
| macro_rules! cpu_flow_step { | |
| ($category:tt, $label:tt, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @now FlowStep, | |
| $flow_id as u64, | |
| Cpu, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow end event with an explicit timestamp on the current thread. | |
| macro_rules! flow_end_timestamp { | |
| ($category:tt, $label:tt, $timestamp:expr, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::FlowEnd { id: $flow_id as u64 }, | |
| Thread, | |
| $category, | |
| $label, | |
| $timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow end event associated with the current thread when the category is enabled. | |
| macro_rules! flow_end { | |
| ($category:tt, $label:tt, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @now FlowEnd, | |
| $flow_id as u64, | |
| Thread, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a flow end event associated with the current CPU when the category is enabled. | |
| macro_rules! cpu_flow_end { | |
| ($category:tt, $label:tt, $flow_id:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @now FlowEnd, | |
| $flow_id as u64, | |
| Cpu, | |
| $category, | |
| $label | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| #[doc(hidden)] | |
| macro_rules! __begin_scope_cond { | |
| ($cond:expr, $context:ident, $category:tt, $label:tt $(, $key:tt => $val:expr)*) => { | |
| <$crate::ktrace::TraceCond<{ $cond }> as $crate::ktrace::CondScopeStorage< | |
| '_, | |
| _, | |
| >>::make_scope(|| { | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| core::hint::cold_path(); | |
| Some($crate::ktrace::KTraceScope::begin( | |
| category, | |
| $crate::ktrace::resolve_string!($label), | |
| $crate::ktrace::Context::$context, | |
| || $crate::ktrace::__args!($($key => $val),*), | |
| )) | |
| } else { | |
| None | |
| } | |
| }) | |
| }; | |
| } | |
| /// Creates an `Option<KTraceScope>` guard that emits a thread duration-complete event when dropped | |
| /// or ended via `end_scope!`. | |
| macro_rules! begin_scope { | |
| ($category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::begin_scope_cond!(true, $category, $label $(, $key => $val)*) | |
| }; | |
| } | |
| /// Creates an `Option<KTraceScope>` guard associated with the current CPU. | |
| macro_rules! cpu_begin_scope { | |
| ($category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::cpu_begin_scope_cond!(true, $category, $label $(, $key => $val)*) | |
| }; | |
| } | |
| /// Gates a thread trace scope on a compile-time `bool` condition, evaluating to | |
| /// `Option<KTraceScope>` when `$cond` is `true` and zero-sized `DisabledKTraceScope` when `false`. | |
| macro_rules! begin_scope_cond { | |
| ($cond:expr, $category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__begin_scope_cond!($cond, Thread, $category, $label $(, $key => $val)*) | |
| }; | |
| } | |
| /// Gates a CPU trace scope on a compile-time `bool` condition. | |
| macro_rules! cpu_begin_scope_cond { | |
| ($cond:expr, $category:tt, $label:tt $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__begin_scope_cond!($cond, Cpu, $category, $label $(, $key => $val)*) | |
| }; | |
| } | |
| /// Ends a trace scope guard with additional completion key-value argument pairs. | |
| macro_rules! end_scope { | |
| ($scope:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::OptionalKTraceScope::end_with($scope, |scope| { | |
| $crate::ktrace::outlined(|| { | |
| let end_time = $crate::ktrace::KTrace::timestamp(); | |
| scope.end_at_with_args(end_time, &$crate::ktrace::__args!($($key => $val),*)); | |
| }); | |
| }) | |
| }; | |
| } | |
| /// Writes a duration complete event with explicit start and end timestamps on the current thread. | |
| macro_rules! complete_timestamp { | |
| ($category:tt, $label:tt, $start_ts:expr, $end_ts:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::DurationComplete { end_time: $end_ts }, | |
| Thread, | |
| $category, | |
| $label, | |
| $start_ts | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a duration complete event from `$start_timestamp` to the current timestamp on the | |
| /// current thread. | |
| macro_rules! complete { | |
| ($category:tt, $label:tt, $start_timestamp:expr $(, $key:tt => $val:expr)* $(,)?) => { | |
| $crate::ktrace::__emit_event!( | |
| @timestamp $crate::ktrace::EventKind::DurationCompleteNow, | |
| Thread, | |
| $category, | |
| $label, | |
| $start_timestamp | |
| $(, $key => $val)* | |
| ) | |
| }; | |
| } | |
| /// Writes a kernel object record when the given trace category is enabled. | |
| macro_rules! kernel_object { | |
| ($category:tt, $koid:expr, $obj_type:expr, $name:expr $(, $key:tt => $val:expr)* $(,)?) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| $crate::ktrace::kernel_object_always!($koid, $obj_type, $name $(, $key => $val)*); | |
| } | |
| }}; | |
| } | |
| /// Writes a kernel object record unconditionally when writes are enabled. | |
| macro_rules! kernel_object_always { | |
| ($koid:expr, $obj_type:expr, $name:expr $(, $key:tt => $val:expr)* $(,)?) => {{ | |
| $crate::ktrace::outlined(|| { | |
| $crate::ktrace::KTrace::emit_kernel_object( | |
| $koid as u64, | |
| $obj_type as u32, | |
| $crate::ktrace::StringRef::from($crate::ktrace::resolve_string!($name)), | |
| &$crate::ktrace::__args!($($key => $val),*), | |
| ); | |
| }); | |
| }}; | |
| } | |
| /// Writes a context switch record when the given trace category is enabled. | |
| macro_rules! context_switch { | |
| ( | |
| $category:tt, | |
| $cpu:expr, | |
| $outgoing_state:expr, | |
| $outgoing_tid:expr, | |
| $incoming_tid:expr | |
| $(, $key:tt => $val:expr)* $(,)? | |
| ) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| $crate::ktrace::outlined(move || { | |
| $crate::ktrace::KTrace::emit_context_switch( | |
| $cpu as u16, | |
| $outgoing_state as u32, | |
| $outgoing_tid as u64, | |
| $incoming_tid as u64, | |
| &$crate::ktrace::__args!($($key => $val),*), | |
| ); | |
| }); | |
| } | |
| }}; | |
| } | |
| /// Writes a thread wakeup record when the given trace category is enabled. | |
| macro_rules! thread_wakeup { | |
| ($category:tt, $cpu:expr, $incoming_tid:expr $(, $key:tt => $val:expr)* $(,)?) => {{ | |
| let category = $crate::ktrace::resolve_category!($category); | |
| if $crate::ktrace::KTrace::category_enabled(category) { | |
| $crate::ktrace::outlined(move || { | |
| $crate::ktrace::KTrace::emit_thread_wakeup( | |
| $cpu as u16, | |
| $incoming_tid as u64, | |
| &$crate::ktrace::__args!($($key => $val),*), | |
| ); | |
| }); | |
| } | |
| }}; | |
| } | |
| pub(crate) use begin_scope; | |
| pub(crate) use begin_scope_cond; | |
| pub(crate) use category_enabled; | |
| pub(crate) use complete; | |
| pub(crate) use complete_timestamp; | |
| pub(crate) use context_switch; | |
| pub(crate) use cpu_begin_scope; | |
| pub(crate) use cpu_begin_scope_cond; | |
| pub(crate) use cpu_counter; | |
| pub(crate) use cpu_counter_timestamp; | |
| pub(crate) use cpu_flow_begin; | |
| pub(crate) use cpu_flow_end; | |
| pub(crate) use cpu_flow_step; | |
| pub(crate) use cpu_instant; | |
| pub(crate) use duration_begin; | |
| pub(crate) use duration_begin_timestamp; | |
| pub(crate) use duration_end; | |
| pub(crate) use duration_end_timestamp; | |
| pub(crate) use end_scope; | |
| pub(crate) use flow_begin; | |
| pub(crate) use flow_begin_timestamp; | |
| pub(crate) use flow_end; | |
| pub(crate) use flow_end_timestamp; | |
| pub(crate) use flow_step_timestamp; | |
| pub(crate) use instant; | |
| pub(crate) use kernel_object; | |
| pub(crate) use kernel_object_always; | |
| pub(crate) use thread_wakeup; | |
| #[doc(hidden)] | |
| pub(crate) use {__args, __begin_scope_cond, __emit_event, resolve_category, resolve_string}; |