Linux 6.13-rc7
[linux.git] / rust / kernel / tracepoint.rs
blobc6e80aa99e8e1400e425d133c57001349986a670
1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (C) 2024 Google LLC.
5 //! Logic for tracepoints.
7 /// Declare the Rust entry point for a tracepoint.
8 ///
9 /// This macro generates an unsafe function that calls into C, and its safety requirements will be
10 /// whatever the relevant C code requires. To document these safety requirements, you may add
11 /// doc-comments when invoking the macro.
12 #[macro_export]
13 macro_rules! declare_trace {
14     ($($(#[$attr:meta])* $pub:vis unsafe fn $name:ident($($argname:ident : $argtyp:ty),* $(,)?);)*) => {$(
15         $( #[$attr] )*
16         #[inline(always)]
17         $pub unsafe fn $name($($argname : $argtyp),*) {
18             #[cfg(CONFIG_TRACEPOINTS)]
19             {
20                 // SAFETY: It's always okay to query the static key for a tracepoint.
21                 let should_trace = unsafe {
22                     $crate::macros::paste! {
23                         $crate::jump_label::static_branch_unlikely!(
24                             $crate::bindings::[< __tracepoint_ $name >],
25                             $crate::bindings::tracepoint,
26                             key
27                         )
28                     }
29                 };
31                 if should_trace {
32                     $crate::macros::paste! {
33                         // SAFETY: The caller guarantees that it is okay to call this tracepoint.
34                         unsafe { $crate::bindings::[< rust_do_trace_ $name >]($($argname),*) };
35                     }
36                 }
37             }
39             #[cfg(not(CONFIG_TRACEPOINTS))]
40             {
41                 // If tracepoints are disabled, insert a trivial use of each argument
42                 // to avoid unused argument warnings.
43                 $( let _unused = $argname; )*
44             }
45         }
46     )*}
49 pub use declare_trace;