1 // SPDX-License-Identifier: GPL-2.0
3 //! Printing facilities.
5 //! C header: [`include/linux/printk.h`](../../../../include/linux/printk.h)
7 //! Reference: <https://www.kernel.org/doc/html/latest/core-api/printk-basics.html>
10 ffi::{c_char, c_void},
14 use crate::str::RawFormatter;
19 // Called from `vsprintf` with format specifier `%pA`.
21 unsafe extern "C" fn rust_fmt_argument(
27 // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
28 let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
29 let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
35 /// Public but hidden since it should only be used from public macros.
37 pub mod format_strings {
40 /// The length we copy from the `KERN_*` kernel prefixes.
41 const LENGTH_PREFIX: usize = 2;
43 /// The length of the fixed format strings.
44 pub const LENGTH: usize = 10;
46 /// Generates a fixed format string for the kernel's [`_printk`].
48 /// The format string is always the same for a given level, i.e. for a
49 /// given `prefix`, which are the kernel's `KERN_*` constants.
51 /// [`_printk`]: ../../../../include/linux/printk.h
52 const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
53 // Ensure the `KERN_*` macros are what we expect.
54 assert!(prefix[0] == b'\x01');
56 assert!(prefix[1] == b'c');
58 assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
60 assert!(prefix[2] == b'\x00');
62 let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
69 prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
74 // Generate the format strings at compile-time.
76 // This avoids the compiler generating the contents on the fly in the stack.
78 // Furthermore, `static` instead of `const` is used to share the strings
79 // for all the kernel.
80 pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG);
81 pub static ALERT: [u8; LENGTH] = generate(false, bindings::KERN_ALERT);
82 pub static CRIT: [u8; LENGTH] = generate(false, bindings::KERN_CRIT);
83 pub static ERR: [u8; LENGTH] = generate(false, bindings::KERN_ERR);
84 pub static WARNING: [u8; LENGTH] = generate(false, bindings::KERN_WARNING);
85 pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE);
86 pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
87 pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG);
88 pub static CONT: [u8; LENGTH] = generate(true, bindings::KERN_CONT);
91 /// Prints a message via the kernel's [`_printk`].
93 /// Public but hidden since it should only be used from public macros.
97 /// The format string must be one of the ones in [`format_strings`], and
98 /// the module name must be null-terminated.
100 /// [`_printk`]: ../../../../include/linux/_printk.h
102 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
103 pub unsafe fn call_printk(
104 format_string: &[u8; format_strings::LENGTH],
106 args: fmt::Arguments<'_>,
108 // `_printk` does not seem to fail in any path.
109 #[cfg(CONFIG_PRINTK)]
112 format_string.as_ptr() as _,
113 module_name.as_ptr(),
114 &args as *const _ as *const c_void,
119 /// Prints a message via the kernel's [`_printk`] for the `CONT` level.
121 /// Public but hidden since it should only be used from public macros.
123 /// [`_printk`]: ../../../../include/linux/printk.h
125 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
126 pub fn call_printk_cont(args: fmt::Arguments<'_>) {
127 // `_printk` does not seem to fail in any path.
129 // SAFETY: The format string is fixed.
130 #[cfg(CONFIG_PRINTK)]
133 format_strings::CONT.as_ptr() as _,
134 &args as *const _ as *const c_void,
139 /// Performs formatting and forwards the string to [`call_printk`].
141 /// Public but hidden since it should only be used from public macros.
145 #[allow(clippy::crate_in_macro_def)]
146 macro_rules! print_macro (
147 // The non-continuation cases (most of them, e.g. `INFO`).
148 ($format_string:path, false, $($arg:tt)+) => (
149 // To remain sound, `arg`s must be expanded outside the `unsafe` block.
150 // Typically one would use a `let` binding for that; however, `format_args!`
151 // takes borrows on the arguments, but does not extend the scope of temporaries.
152 // Therefore, a `match` expression is used to keep them around, since
153 // the scrutinee is kept until the end of the `match`.
154 match format_args!($($arg)+) {
155 // SAFETY: This hidden macro should only be called by the documented
156 // printing macros which ensure the format string is one of the fixed
157 // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
158 // by the `module!` proc macro or fixed values defined in a kernel
161 $crate::print::call_printk(
171 ($format_string:path, true, $($arg:tt)+) => (
172 $crate::print::call_printk_cont(
173 format_args!($($arg)+),
178 /// Stub for doctests
181 macro_rules! print_macro (
182 ($format_string:path, $e:expr, $($arg:tt)+) => (
187 // We could use a macro to generate these macros. However, doing so ends
188 // up being a bit ugly: it requires the dollar token trick to escape `$` as
189 // well as playing with the `doc` attribute. Furthermore, they cannot be easily
190 // imported in the prelude due to [1]. So, for the moment, we just write them
191 // manually, like in the C side; while keeping most of the logic in another
192 // macro, i.e. [`print_macro`].
194 // [1]: https://github.com/rust-lang/rust/issues/52234
196 /// Prints an emergency-level message (level 0).
198 /// Use this level if the system is unusable.
200 /// Equivalent to the kernel's [`pr_emerg`] macro.
202 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
203 /// `alloc::format!` for information about the formatting syntax.
205 /// [`pr_emerg`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_emerg
206 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
211 /// pr_emerg!("hello {}\n", "there");
214 macro_rules! pr_emerg (
216 $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*)
220 /// Prints an alert-level message (level 1).
222 /// Use this level if action must be taken immediately.
224 /// Equivalent to the kernel's [`pr_alert`] macro.
226 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
227 /// `alloc::format!` for information about the formatting syntax.
229 /// [`pr_alert`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_alert
230 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
235 /// pr_alert!("hello {}\n", "there");
238 macro_rules! pr_alert (
240 $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*)
244 /// Prints a critical-level message (level 2).
246 /// Use this level for critical conditions.
248 /// Equivalent to the kernel's [`pr_crit`] macro.
250 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
251 /// `alloc::format!` for information about the formatting syntax.
253 /// [`pr_crit`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_crit
254 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
259 /// pr_crit!("hello {}\n", "there");
262 macro_rules! pr_crit (
264 $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*)
268 /// Prints an error-level message (level 3).
270 /// Use this level for error conditions.
272 /// Equivalent to the kernel's [`pr_err`] macro.
274 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
275 /// `alloc::format!` for information about the formatting syntax.
277 /// [`pr_err`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_err
278 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
283 /// pr_err!("hello {}\n", "there");
286 macro_rules! pr_err (
288 $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*)
292 /// Prints a warning-level message (level 4).
294 /// Use this level for warning conditions.
296 /// Equivalent to the kernel's [`pr_warn`] macro.
298 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
299 /// `alloc::format!` for information about the formatting syntax.
301 /// [`pr_warn`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_warn
302 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
307 /// pr_warn!("hello {}\n", "there");
310 macro_rules! pr_warn (
312 $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*)
316 /// Prints a notice-level message (level 5).
318 /// Use this level for normal but significant conditions.
320 /// Equivalent to the kernel's [`pr_notice`] macro.
322 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
323 /// `alloc::format!` for information about the formatting syntax.
325 /// [`pr_notice`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_notice
326 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
331 /// pr_notice!("hello {}\n", "there");
334 macro_rules! pr_notice (
336 $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*)
340 /// Prints an info-level message (level 6).
342 /// Use this level for informational messages.
344 /// Equivalent to the kernel's [`pr_info`] macro.
346 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
347 /// `alloc::format!` for information about the formatting syntax.
349 /// [`pr_info`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_info
350 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
355 /// pr_info!("hello {}\n", "there");
358 #[doc(alias = "print")]
359 macro_rules! pr_info (
361 $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*)
365 /// Prints a debug-level message (level 7).
367 /// Use this level for debug messages.
369 /// Equivalent to the kernel's [`pr_debug`] macro, except that it doesn't support dynamic debug
372 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
373 /// `alloc::format!` for information about the formatting syntax.
375 /// [`pr_debug`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_debug
376 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
381 /// pr_debug!("hello {}\n", "there");
384 #[doc(alias = "print")]
385 macro_rules! pr_debug (
387 if cfg!(debug_assertions) {
388 $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*)
393 /// Continues a previous log message in the same line.
395 /// Use only when continuing a previous `pr_*!` macro (e.g. [`pr_info!`]).
397 /// Equivalent to the kernel's [`pr_cont`] macro.
399 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
400 /// `alloc::format!` for information about the formatting syntax.
402 /// [`pr_cont`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_cont
403 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
408 /// # use kernel::pr_cont;
409 /// pr_info!("hello");
410 /// pr_cont!(" {}\n", "there");
413 macro_rules! pr_cont (
415 $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)