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 fn rust_fmt_argument(buf: *mut c_char, end: *mut c_char, ptr: *const c_void) -> *mut c_char {
23 // SAFETY: The C contract guarantees that `buf` is valid if it's less than `end`.
24 let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
25 let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
31 /// Public but hidden since it should only be used from public macros.
33 pub mod format_strings {
36 /// The length we copy from the `KERN_*` kernel prefixes.
37 const LENGTH_PREFIX: usize = 2;
39 /// The length of the fixed format strings.
40 pub const LENGTH: usize = 10;
42 /// Generates a fixed format string for the kernel's [`_printk`].
44 /// The format string is always the same for a given level, i.e. for a
45 /// given `prefix`, which are the kernel's `KERN_*` constants.
47 /// [`_printk`]: ../../../../include/linux/printk.h
48 const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
49 // Ensure the `KERN_*` macros are what we expect.
50 assert!(prefix[0] == b'\x01');
52 assert!(prefix[1] == b'c');
54 assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
56 assert!(prefix[2] == b'\x00');
58 let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
65 prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
70 // Generate the format strings at compile-time.
72 // This avoids the compiler generating the contents on the fly in the stack.
74 // Furthermore, `static` instead of `const` is used to share the strings
75 // for all the kernel.
76 pub static EMERG: [u8; LENGTH] = generate(false, bindings::KERN_EMERG);
77 pub static INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
80 /// Prints a message via the kernel's [`_printk`].
82 /// Public but hidden since it should only be used from public macros.
86 /// The format string must be one of the ones in [`format_strings`], and
87 /// the module name must be null-terminated.
89 /// [`_printk`]: ../../../../include/linux/_printk.h
91 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
92 pub unsafe fn call_printk(
93 format_string: &[u8; format_strings::LENGTH],
95 args: fmt::Arguments<'_>,
97 // `_printk` does not seem to fail in any path.
101 format_string.as_ptr() as _,
102 module_name.as_ptr(),
103 &args as *const _ as *const c_void,
108 /// Performs formatting and forwards the string to [`call_printk`].
110 /// Public but hidden since it should only be used from public macros.
114 #[allow(clippy::crate_in_macro_def)]
115 macro_rules! print_macro (
116 // The non-continuation cases (most of them, e.g. `INFO`).
117 ($format_string:path, $($arg:tt)+) => (
118 // To remain sound, `arg`s must be expanded outside the `unsafe` block.
119 // Typically one would use a `let` binding for that; however, `format_args!`
120 // takes borrows on the arguments, but does not extend the scope of temporaries.
121 // Therefore, a `match` expression is used to keep them around, since
122 // the scrutinee is kept until the end of the `match`.
123 match format_args!($($arg)+) {
124 // SAFETY: This hidden macro should only be called by the documented
125 // printing macros which ensure the format string is one of the fixed
126 // ones. All `__LOG_PREFIX`s are null-terminated as they are generated
127 // by the `module!` proc macro or fixed values defined in a kernel
130 $crate::print::call_printk(
140 /// Stub for doctests
143 macro_rules! print_macro (
144 ($format_string:path, $e:expr, $($arg:tt)+) => (
149 // We could use a macro to generate these macros. However, doing so ends
150 // up being a bit ugly: it requires the dollar token trick to escape `$` as
151 // well as playing with the `doc` attribute. Furthermore, they cannot be easily
152 // imported in the prelude due to [1]. So, for the moment, we just write them
153 // manually, like in the C side; while keeping most of the logic in another
154 // macro, i.e. [`print_macro`].
156 // [1]: https://github.com/rust-lang/rust/issues/52234
158 /// Prints an emergency-level message (level 0).
160 /// Use this level if the system is unusable.
162 /// Equivalent to the kernel's [`pr_emerg`] macro.
164 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
165 /// `alloc::format!` for information about the formatting syntax.
167 /// [`pr_emerg`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_emerg
168 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
173 /// pr_emerg!("hello {}\n", "there");
176 macro_rules! pr_emerg (
178 $crate::print_macro!($crate::print::format_strings::EMERG, $($arg)*)
182 /// Prints an info-level message (level 6).
184 /// Use this level for informational messages.
186 /// Equivalent to the kernel's [`pr_info`] macro.
188 /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and
189 /// `alloc::format!` for information about the formatting syntax.
191 /// [`pr_info`]: https://www.kernel.org/doc/html/latest/core-api/printk-basics.html#c.pr_info
192 /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html
197 /// pr_info!("hello {}\n", "there");
200 #[doc(alias = "print")]
201 macro_rules! pr_info (
203 $crate::print_macro!($crate::print::format_strings::INFO, $($arg)*)