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`.
18 unsafe extern "C" fn rust_fmt_argument(
24 // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
25 let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
26 let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
32 /// Public but hidden since it should only be used from public macros.
34 pub mod format_strings {
35 /// The length we copy from the `KERN_*` kernel prefixes.
36 const LENGTH_PREFIX: usize = 2;
38 /// The length of the fixed format strings.
39 pub const LENGTH: usize = 10;
41 /// Generates a fixed format string for the kernel's [`_printk`].
43 /// The format string is always the same for a given level, i.e. for a
44 /// given `prefix`, which are the kernel's `KERN_*` constants.
46 /// [`_printk`]: srctree/include/linux/printk.h
47 const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
48 // Ensure the `KERN_*` macros are what we expect.
49 assert!(prefix[0] == b'\x01');
51 assert!(prefix[1] == b'c');
53 assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
55 assert!(prefix[2] == b'\x00');
57 let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
64 prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
69 // Generate the format strings at compile-time.
71 // This avoids the compiler generating the contents on the fly in the stack.
73 // Furthermore, `static` instead of `const` is used to share the strings
74 // for all the kernel.
75 pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG);
76 pub static ALERT: [u8; LENGTH] = generate(false, bindings::KERN_ALERT);
77 pub static CRIT: [u8; LENGTH] = generate(false, bindings::KERN_CRIT);
78 pub static ERR: [u8; LENGTH] = generate(false, bindings::KERN_ERR);
79 pub static WARNING: [u8; LENGTH] = generate(false, bindings::KERN_WARNING);
80 pub static NOTICE: [u8; LENGTH] = generate(false, bindings::KERN_NOTICE);
81 pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
82 pub static DEBUG: [u8; LENGTH] = generate(false, bindings::KERN_DEBUG);
83 pub static CONT: [u8; LENGTH] = generate(true, bindings::KERN_CONT);
86 /// Prints a message via the kernel's [`_printk`].
88 /// Public but hidden since it should only be used from public macros.
92 /// The format string must be one of the ones in [`format_strings`], and
93 /// the module name must be null-terminated.
95 /// [`_printk`]: srctree/include/linux/_printk.h
97 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
98 pub unsafe fn call_printk(
99 format_string: &[u8; format_strings::LENGTH],
101 args: fmt::Arguments<'_>,
103 // `_printk` does not seem to fail in any path.
104 #[cfg(CONFIG_PRINTK)]
107 format_string.as_ptr() as _,
108 module_name.as_ptr(),
109 &args as *const _ as *const c_void,
114 /// Prints a message via the kernel's [`_printk`] for the `CONT` level.
116 /// Public but hidden since it should only be used from public macros.
118 /// [`_printk`]: srctree/include/linux/printk.h
120 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
121 pub fn call_printk_cont(args: fmt::Arguments<'_>) {
122 // `_printk` does not seem to fail in any path.
124 // SAFETY: The format string is fixed.
125 #[cfg(CONFIG_PRINTK)]
128 format_strings::CONT.as_ptr() as _,
129 &args as *const _ as *const c_void,
134 /// Performs formatting and forwards the string to [`call_printk`].
136 /// Public but hidden since it should only be used from public macros.
140 #[allow(clippy::crate_in_macro_def)]
141 macro_rules! print_macro (
142 // The non-continuation cases (most of them, e.g. `INFO`).
143 ($format_string:path, false, $($arg:tt)+) => (
144 // To remain sound, `arg`s must be expanded outside the `unsafe` block.
145 // Typically one would use a `let` binding for that; however, `format_args!`
146 // takes borrows on the arguments, but does not extend the scope of temporaries.
147 // Therefore, a `match` expression is used to keep them around, since
148 // the scrutinee is kept until the end of the `match`.
149 match format_args!($($arg)+) {
150 // SAFETY: This hidden macro should only be called by the documented
151 // printing macros which ensure the format string is one of the fixed
152 // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
153 // by the `module!` proc macro or fixed values defined in a kernel
156 $crate::print::call_printk(
166 ($format_string:path, true, $($arg:tt)+) => (
167 $crate::print::call_printk_cont(
168 format_args!($($arg)+),
173 /// Stub for doctests
176 macro_rules! print_macro (
177 ($format_string:path, $e:expr, $($arg:tt)+) => (
182 // We could use a macro to generate these macros. However, doing so ends
183 // up being a bit ugly: it requires the dollar token trick to escape `$` as
184 // well as playing with the `doc` attribute. Furthermore, they cannot be easily
185 // imported in the prelude due to [1]. So, for the moment, we just write them
186 // manually, like in the C side; while keeping most of the logic in another
187 // macro, i.e. [`print_macro`].
189 // [1]: https://github.com/rust-lang/rust/issues/52234
191 /// Prints an emergency-level message (level 0).
193 /// Use this level if the system is unusable.
195 /// Equivalent to the kernel's [`pr_emerg`] macro.
197 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
198 /// `alloc::format!` for information about the formatting syntax.
200 /// [`pr_emerg`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_emerg
201 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
206 /// pr_emerg!("hello {}\n", "there");
209 macro_rules! pr_emerg (
211 $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*)
215 /// Prints an alert-level message (level 1).
217 /// Use this level if action must be taken immediately.
219 /// Equivalent to the kernel's [`pr_alert`] macro.
221 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
222 /// `alloc::format!` for information about the formatting syntax.
224 /// [`pr_alert`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_alert
225 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
230 /// pr_alert!("hello {}\n", "there");
233 macro_rules! pr_alert (
235 $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*)
239 /// Prints a critical-level message (level 2).
241 /// Use this level for critical conditions.
243 /// Equivalent to the kernel's [`pr_crit`] macro.
245 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
246 /// `alloc::format!` for information about the formatting syntax.
248 /// [`pr_crit`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_crit
249 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
254 /// pr_crit!("hello {}\n", "there");
257 macro_rules! pr_crit (
259 $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*)
263 /// Prints an error-level message (level 3).
265 /// Use this level for error conditions.
267 /// Equivalent to the kernel's [`pr_err`] macro.
269 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
270 /// `alloc::format!` for information about the formatting syntax.
272 /// [`pr_err`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_err
273 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
278 /// pr_err!("hello {}\n", "there");
281 macro_rules! pr_err (
283 $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*)
287 /// Prints a warning-level message (level 4).
289 /// Use this level for warning conditions.
291 /// Equivalent to the kernel's [`pr_warn`] macro.
293 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
294 /// `alloc::format!` for information about the formatting syntax.
296 /// [`pr_warn`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_warn
297 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
302 /// pr_warn!("hello {}\n", "there");
305 macro_rules! pr_warn (
307 $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*)
311 /// Prints a notice-level message (level 5).
313 /// Use this level for normal but significant conditions.
315 /// Equivalent to the kernel's [`pr_notice`] macro.
317 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
318 /// `alloc::format!` for information about the formatting syntax.
320 /// [`pr_notice`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_notice
321 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
326 /// pr_notice!("hello {}\n", "there");
329 macro_rules! pr_notice (
331 $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*)
335 /// Prints an info-level message (level 6).
337 /// Use this level for informational messages.
339 /// Equivalent to the kernel's [`pr_info`] macro.
341 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
342 /// `alloc::format!` for information about the formatting syntax.
344 /// [`pr_info`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_info
345 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
350 /// pr_info!("hello {}\n", "there");
353 #[doc(alias = "print")]
354 macro_rules! pr_info (
356 $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*)
360 /// Prints a debug-level message (level 7).
362 /// Use this level for debug messages.
364 /// Equivalent to the kernel's [`pr_debug`] macro, except that it doesn't support dynamic debug
367 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
368 /// `alloc::format!` for information about the formatting syntax.
370 /// [`pr_debug`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_debug
371 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
376 /// pr_debug!("hello {}\n", "there");
379 #[doc(alias = "print")]
380 macro_rules! pr_debug (
382 if cfg!(debug_assertions) {
383 $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*)
388 /// Continues a previous log message in the same line.
390 /// Use only when continuing a previous `pr_*!` macro (e.g. [`pr_info!`]).
392 /// Equivalent to the kernel's [`pr_cont`] macro.
394 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
395 /// `alloc::format!` for information about the formatting syntax.
397 /// [`pr_info!`]: crate::pr_info!
398 /// [`pr_cont`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr_cont
399 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
404 /// # use kernel::pr_cont;
405 /// pr_info!("hello");
406 /// pr_cont!(" {}\n", "there");
409 macro_rules! pr_cont (
411 $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)