xref: /linux/rust/kernel/tracepoint.rs (revision 7f4f3b14e8079ecde096bd734af10e30d40c27b7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 // Copyright (C) 2024 Google LLC.
4 
5 //! Logic for tracepoints.
6 
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                 };
30 
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             }
38 
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     )*}
47 }
48 
49 pub use declare_trace;
50