From: Andrew Ballance <andrewjballance@gmail.com>
To: jbaron@akamai.com, jim.cromie@gmail.com,
daniel.almeida@collabora.com, acourbot@nvidia.com,
ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com,
gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org,
a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
dakr@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org,
rostedt@goodmis.org, andrewjballance@gmail.com
Cc: viresh.kumar@linaro.org, lina+kernel@asahilina.net,
tamird@gmail.com, jubalh@iodoru.org,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2 4/4] rust: device: add support for dynamic debug to dev_dbg!
Date: Fri, 20 Jun 2025 16:05:33 -0500 [thread overview]
Message-ID: <20250620210533.400889-5-andrewjballance@gmail.com> (raw)
In-Reply-To: <20250620210533.400889-1-andrewjballance@gmail.com>
adds support for dynamic debug for the dev_dbg macro.
Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
rust/kernel/device.rs | 105 +++++++++++++++++++++++++++++++++++++++++-
1 file changed, 103 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs
index dea06b79ecb5..829b7ca7bb7b 100644
--- a/rust/kernel/device.rs
+++ b/rust/kernel/device.rs
@@ -536,11 +536,14 @@ macro_rules! dev_info {
///
/// This level should be used for debug messages.
///
-/// Equivalent to the kernel's `dev_dbg` macro, except that it doesn't support dynamic debug yet.
+/// Equivalent to the kernel's `dev_dbg` macro.
+///
+/// This has support for [`dynamic debug`].
///
/// Mimics the interface of [`std::print!`]. More information about the syntax is available from
/// [`core::fmt`] and [`std::format!`].
///
+/// [`dynamic debug`]: https://docs.kernel.org/admin-guide/dynamic-debug-howto.html
/// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
/// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html
///
@@ -555,5 +558,103 @@ macro_rules! dev_info {
/// ```
#[macro_export]
macro_rules! dev_dbg {
- ($($f:tt)*) => { $crate::dev_printk!(pr_dbg, $($f)*); }
+ ($($f:tt)*) => {
+ #[cfg(any(DYNAMIC_DEBUG_MODULE, CONFIG_DYNAMIC_DEBUG))]
+ { $crate::dynamic_dev_dbg_unlikely!($($f)*); }
+
+ #[cfg(not(any(DYNAMIC_DEBUG_MODULE, CONFIG_DYNAMIC_DEBUG)))]
+ { $crate::dev_printk!(pr_dbg, $($f)*); }
+ }
+}
+
+/// this contains all of the code that is used for dynamic debug for dev_dbg!
+/// this is public but hidden. This code should only be called
+/// by the `pr_debug!` or `dev_dbg!` macros.
+#[cfg(CONFIG_DYNAMIC_DEBUG_CORE)]
+#[doc(hidden)]
+pub mod dynamic_debug {
+
+ use super::Device;
+ use kernel::c_str;
+ use kernel::print::dynamic_debug::_Ddebug;
+
+ /// a wrapper function around the c function `__dynamic_dev_dbg`.
+ /// # Safety
+ /// - descriptor must be a valid pointer to a `static mut` _Ddebug
+ pub unsafe fn dynamic_dev_dbg(
+ descriptor: *mut _Ddebug,
+ dev: &Device,
+ args: core::fmt::Arguments<'_>,
+ ) {
+ // SAFETY:
+ // - "%pA" is null terminated and is the format for rust printing
+ // - dev.as_raw() is a valid pointer to struct device
+ unsafe {
+ bindings::__dynamic_dev_dbg(
+ &raw mut (*descriptor).inner,
+ dev.as_raw(),
+ c_str!("%pA").as_char_ptr(),
+ (&raw const args).cast::<ffi::c_void>(),
+ )
+ };
+ }
+
+ /// macro for dynamic debug equivalent to the C `dev_dbg` macro
+ #[doc(hidden)]
+ #[macro_export]
+ macro_rules! dynamic_dev_dbg_unlikely {
+ ($dev:expr, $($f:tt)*) => {{
+ use $crate::c_str;
+ use $crate::str::CStr;
+ use $crate::print::dynamic_debug::{_ddebug, _Ddebug};
+
+ const MOD_NAME: &CStr = c_str!(module_path!());
+ // right now rust does not have a function! macro so hard code this to be
+ // the name of the macro that is printing
+ // TODO:
+ // replace this once either a function! macro exists
+ // or core::any::type_name becomes const
+ const FN_NAME: &CStr = c_str!("dev_dbg!");
+ const FILE_NAME: &CStr = c_str!(file!());
+ const MESSAGE: &CStr = c_str!(stringify!($($f)*));
+ const LINE: u32 = line!();
+
+ #[used]
+ #[unsafe(link_section = "__dyndbg")]
+ static mut DEBUG_INFO: _Ddebug =
+ _Ddebug::new_unlikely(MOD_NAME, FN_NAME, FILE_NAME, MESSAGE, LINE);
+
+ // SAFETY:
+ // - this is reading from a `static mut` variable
+ // - key.dd_key_false is a valid static key
+ let should_print: bool = unsafe {
+ #[cfg(CONFIG_JUMP_LABEL)]
+ {
+ ::kernel::jump_label::static_branch_unlikely!(
+ DEBUG_INFO,
+ _Ddebug,
+ inner.key.dd_key_false
+ )
+ }
+ #[cfg(not(CONFIG_JUMP_LABEL))]
+ {
+ // gets the _DPRINTK_FLAGS_PRINT bit
+ // use pointers to avoid undefined behavior
+ let ptr: *const _ddebug = &raw const DEBUG_INFO.inner;
+ (*ptr).flags() & 1 != 0
+ }
+ };
+
+ if should_print {
+ // SAFETY: `&mut DEBUG_INFO` is a valid pointer to a static mut _Ddebug
+ unsafe {
+ $crate::device::dynamic_debug::dynamic_dev_dbg(
+ &raw mut DEBUG_INFO,
+ $dev,
+ format_args!($($f)*)
+ );
+ }
+ }
+ }};
+ }
}
--
2.49.0
prev parent reply other threads:[~2025-06-20 21:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-20 21:05 [RFC PATCH v2 0/4] rust: add support for dynamic debug Andrew Ballance
2025-06-20 21:05 ` [RFC PATCH v2 1/4] rust: jump label: add support for nested arguments Andrew Ballance
2025-06-20 21:05 ` [RFC PATCH v2 2/4] rust: jump label: add STATIC_KEY_INIT_FALSE Andrew Ballance
2025-06-20 21:05 ` [RFC PATCH v2 3/4] rust: print: add support for dynamic debug to pr_debug! Andrew Ballance
2025-06-20 21:05 ` Andrew Ballance [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250620210533.400889-5-andrewjballance@gmail.com \
--to=andrewjballance@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=gary@garyguo.net \
--cc=gregkh@linuxfoundation.org \
--cc=jbaron@akamai.com \
--cc=jim.cromie@gmail.com \
--cc=jubalh@iodoru.org \
--cc=lina+kernel@asahilina.net \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rafael@kernel.org \
--cc=rostedt@goodmis.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@gmail.com \
--cc=tmgross@umich.edu \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).