1 // SPDX-License-Identifier: GPL-2.0
3 //! Printing facilities.
5 //! C header: [`include/linux/printk.h`](srctree/include/linux/printk.h)
7 //! Reference: <https://docs.kernel.org/core-api/printk-basics.html>
10 ffi::{c_char, c_void},
14 use crate::str::RawFormatter;
16 // Called from `vsprintf` with format specifier `%pA`.
17 #[expect(clippy::missing_safety_doc)]
19 unsafe extern "C" fn rust_fmt_argument(
25 // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
26 let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
28 let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
34 /// Public but hidden since it should only be used from public macros.
36 pub mod format_strings {
37 /// The length we copy from the `KERN_*` kernel prefixes.
38 const LENGTH_PREFIX: usize = 2;
40 /// The length of the fixed format strings.
41 pub const LENGTH: usize = 10;
43 /// Generates a fixed format string for the kernel's [`_printk`].
45 /// The format string is always the same for a given level, i.e. for a
46 /// given `prefix`, which are the kernel's `KERN_*` constants.
48 /// [`_printk`]: srctree/include/linux/printk.h
49 const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
50 // Ensure the `KERN_*` macros are what we expect.
51 assert!(prefix[0] == b'\x01');
53 assert!(prefix[1] == b'c');
55 assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
57 assert!(prefix[2] == b'\x00');
59 let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
66 prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
71 // Generate the format strings at compile-time.
73 // This avoids the compiler generating the contents on the fly in the stack.
75 // Furthermore, `static` instead of `const` is used to share the strings
76 // for all the kernel.
77 pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG);
78 pub static ALERT: [u8; LENGTH] = generate(false, bindings::KERN_ALERT);
79 pub static CRIT: [u8; LENGTH] = generate(false, bindings::KERN_CRIT);
80 pub static ERR: [u8; LENGTH] = generate(false, bindings::KERN_ERR);
81 pub static WARNING: [u8; LENGTH] = generate(false, bindings::KERN_WARNING);
82 pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE);
83 pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
84 pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG);
85 pub static CONT: [u8; LENGTH] = generate(true, bindings::KERN_CONT);
88 /// Prints a message via the kernel's [`_printk`].
90 /// Public but hidden since it should only be used from public macros.
94 /// The format string must be one of the ones in [`format_strings`], and
95 /// the module name must be null-terminated.
97 /// [`_printk`]: srctree/include/linux/_printk.h
99 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
100 pub unsafe fn call_printk(
101 format_string: &[u8; format_strings::LENGTH],
103 args: fmt::Arguments<'_>,
105 // `_printk` does not seem to fail in any path.
106 #[cfg(CONFIG_PRINTK)]
110 format_string.as_ptr() as _,
111 module_name.as_ptr(),
112 &args as *const _ as *const c_void,
117 /// Prints a message via the kernel's [`_printk`] for the `CONT` level.
119 /// Public but hidden since it should only be used from public macros.
121 /// [`_printk`]: srctree/include/linux/printk.h
123 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
124 pub fn call_printk_cont(args: fmt::Arguments<'_>) {
125 // `_printk` does not seem to fail in any path.
127 // SAFETY: The format string is fixed.
128 #[cfg(CONFIG_PRINTK)]
131 format_strings::CONT.as_ptr() as _,
132 &args as *const _ as *const c_void,
137 /// Performs formatting and forwards the string to [`call_printk`].
139 /// Public but hidden since it should only be used from public macros.
143 #[expect(clippy::crate_in_macro_def)]
144 macro_rules! print_macro (
145 // The non-continuation cases (most of them, e.g. `INFO`).
146 ($format_string:path, false, $($arg:tt)+) => (
147 // To remain sound, `arg`s must be expanded outside the `unsafe` block.
148 // Typically one would use a `let` binding for that; however, `format_args!`
149 // takes borrows on the arguments, but does not extend the scope of temporaries.
150 // Therefore, a `match` expression is used to keep them around, since
151 // the scrutinee is kept until the end of the `match`.
152 match format_args!($($arg)+) {
153 // SAFETY: This hidden macro should only be called by the documented
154 // printing macros which ensure the format string is one of the fixed
155 // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
156 // by the `module!` proc macro or fixed values defined in a kernel
159 $crate::print::call_printk(
169 ($format_string:path, true, $($arg:tt)+) => (
170 $crate::print::call_printk_cont(
171 format_args!($($arg)+),
176 /// Stub for doctests
179 macro_rules! print_macro (
180 ($format_string:path, $e:expr, $($arg:tt)+) => (
185 // We could use a macro to generate these macros. However, doing so ends
186 // up being a bit ugly: it requires the dollar token trick to escape `$` as
187 // well as playing with the `doc` attribute. Furthermore, they cannot be easily
188 // imported in the prelude due to [1]. So, for the moment, we just write them
189 // manually, like in the C side; while keeping most of the logic in another
190 // macro, i.e. [`print_macro`].
192 // [1]: https://github.com/rust-lang/rust/issues/52234
194 /// Prints an emergency-level message (level 0).
196 /// Use this level if the system is unusable.
198 /// Equivalent to the kernel's [`pr_emerg`] macro.
200 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
201 /// `alloc::format!` for information about the formatting syntax.
203 /// [`pr_emerg`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_emerg
204 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
209 /// pr_emerg!("hello {}\n", "there");
212 macro_rules! pr_emerg (
214 $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*)
218 /// Prints an alert-level message (level 1).
220 /// Use this level if action must be taken immediately.
222 /// Equivalent to the kernel's [`pr_alert`] macro.
224 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
225 /// `alloc::format!` for information about the formatting syntax.
227 /// [`pr_alert`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_alert
228 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
233 /// pr_alert!("hello {}\n", "there");
236 macro_rules! pr_alert (
238 $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*)
242 /// Prints a critical-level message (level 2).
244 /// Use this level for critical conditions.
246 /// Equivalent to the kernel's [`pr_crit`] macro.
248 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
249 /// `alloc::format!` for information about the formatting syntax.
251 /// [`pr_crit`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_crit
252 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
257 /// pr_crit!("hello {}\n", "there");
260 macro_rules! pr_crit (
262 $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*)
266 /// Prints an error-level message (level 3).
268 /// Use this level for error conditions.
270 /// Equivalent to the kernel's [`pr_err`] macro.
272 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
273 /// `alloc::format!` for information about the formatting syntax.
275 /// [`pr_err`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_err
276 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
281 /// pr_err!("hello {}\n", "there");
284 macro_rules! pr_err (
286 $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*)
290 /// Prints a warning-level message (level 4).
292 /// Use this level for warning conditions.
294 /// Equivalent to the kernel's [`pr_warn`] macro.
296 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
297 /// `alloc::format!` for information about the formatting syntax.
299 /// [`pr_warn`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_warn
300 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
305 /// pr_warn!("hello {}\n", "there");
308 macro_rules! pr_warn (
310 $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*)
314 /// Prints a notice-level message (level 5).
316 /// Use this level for normal but significant conditions.
318 /// Equivalent to the kernel's [`pr_notice`] macro.
320 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
321 /// `alloc::format!` for information about the formatting syntax.
323 /// [`pr_notice`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_notice
324 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
329 /// pr_notice!("hello {}\n", "there");
332 macro_rules! pr_notice (
334 $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*)
338 /// Prints an info-level message (level 6).
340 /// Use this level for informational messages.
342 /// Equivalent to the kernel's [`pr_info`] macro.
344 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
345 /// `alloc::format!` for information about the formatting syntax.
347 /// [`pr_info`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_info
348 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
353 /// pr_info!("hello {}\n", "there");
356 #[doc(alias = "print")]
357 macro_rules! pr_info (
359 $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*)
363 /// Prints a debug-level message (level 7).
365 /// Use this level for debug messages.
367 /// Equivalent to the kernel's [`pr_debug`] macro, except that it doesn't support dynamic debug
370 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
371 /// `alloc::format!` for information about the formatting syntax.
373 /// [`pr_debug`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_debug
374 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
379 /// pr_debug!("hello {}\n", "there");
382 #[doc(alias = "print")]
383 macro_rules! pr_debug (
385 if cfg!(debug_assertions) {
386 $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*)
391 /// Continues a previous log message in the same line.
393 /// Use only when continuing a previous `pr_*!` macro (e.g. [`pr_info!`]).
395 /// Equivalent to the kernel's [`pr_cont`] macro.
397 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
398 /// `alloc::format!` for information about the formatting syntax.
400 /// [`pr_info!`]: crate::pr_info!
401 /// [`pr_cont`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_cont
402 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
407 /// # use kernel::pr_cont;
408 /// pr_info!("hello");
409 /// pr_cont!(" {}\n", "there");
412 macro_rules! pr_cont (
414 $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)