~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/kernel/print.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/kernel/print.rs (Version linux-6.12-rc7) and /rust/kernel/print.rs (Version linux-4.18.20)


  1 // SPDX-License-Identifier: GPL-2.0               
  2                                                   
  3 //! Printing facilities.                          
  4 //!                                               
  5 //! C header: [`include/linux/printk.h`](srctr    
  6 //!                                               
  7 //! Reference: <https://docs.kernel.org/core-a    
  8                                                   
  9 use core::{                                       
 10     ffi::{c_char, c_void},                        
 11     fmt,                                          
 12 };                                                
 13                                                   
 14 use crate::str::RawFormatter;                     
 15                                                   
 16 // Called from `vsprintf` with format specifie    
 17 #[no_mangle]                                      
 18 unsafe extern "C" fn rust_fmt_argument(           
 19     buf: *mut c_char,                             
 20     end: *mut c_char,                             
 21     ptr: *const c_void,                           
 22 ) -> *mut c_char {                                
 23     use fmt::Write;                               
 24     // SAFETY: The C contract guarantees that     
 25     let mut w = unsafe { RawFormatter::from_pt    
 26     let _ = w.write_fmt(unsafe { *(ptr as *con    
 27     w.pos().cast()                                
 28 }                                                 
 29                                                   
 30 /// Format strings.                               
 31 ///                                               
 32 /// Public but hidden since it should only be     
 33 #[doc(hidden)]                                    
 34 pub mod format_strings {                          
 35     /// The length we copy from the `KERN_*` k    
 36     const LENGTH_PREFIX: usize = 2;               
 37                                                   
 38     /// The length of the fixed format strings    
 39     pub const LENGTH: usize = 10;                 
 40                                                   
 41     /// Generates a fixed format string for th    
 42     ///                                           
 43     /// The format string is always the same f    
 44     /// given `prefix`, which are the kernel's    
 45     ///                                           
 46     /// [`_printk`]: srctree/include/linux/pri    
 47     const fn generate(is_cont: bool, prefix: &    
 48         // Ensure the `KERN_*` macros are what    
 49         assert!(prefix[0] == b'\x01');            
 50         if is_cont {                              
 51             assert!(prefix[1] == b'c');           
 52         } else {                                  
 53             assert!(prefix[1] >= b'0' && prefi    
 54         }                                         
 55         assert!(prefix[2] == b'\x00');            
 56                                                   
 57         let suffix: &[u8; LENGTH - LENGTH_PREF    
 58             b"%pA\0\0\0\0\0"                      
 59         } else {                                  
 60             b"%s: %pA\0"                          
 61         };                                        
 62                                                   
 63         [                                         
 64             prefix[0], prefix[1], suffix[0], s    
 65             suffix[6], suffix[7],                 
 66         ]                                         
 67     }                                             
 68                                                   
 69     // Generate the format strings at compile-    
 70     //                                            
 71     // This avoids the compiler generating the    
 72     //                                            
 73     // Furthermore, `static` instead of `const    
 74     // for all the kernel.                        
 75     pub static EMERG: [u8; LENGTH] = generate(    
 76     pub static ALERT: [u8; LENGTH] = generate(    
 77     pub static CRIT: [u8; LENGTH] = generate(f    
 78     pub static ERR: [u8; LENGTH] = generate(fa    
 79     pub static WARNING: [u8; LENGTH] = generat    
 80     pub static NOTICE: [u8; LENGTH] = generate    
 81     pub static INFO: [u8; LENGTH] = generate(f    
 82     pub static DEBUG: [u8; LENGTH] = generate(    
 83     pub static CONT: [u8; LENGTH] = generate(t    
 84 }                                                 
 85                                                   
 86 /// Prints a message via the kernel's [`_print    
 87 ///                                               
 88 /// Public but hidden since it should only be     
 89 ///                                               
 90 /// # Safety                                      
 91 ///                                               
 92 /// The format string must be one of the ones     
 93 /// the module name must be null-terminated.      
 94 ///                                               
 95 /// [`_printk`]: srctree/include/linux/_printk    
 96 #[doc(hidden)]                                    
 97 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_va    
 98 pub unsafe fn call_printk(                        
 99     format_string: &[u8; format_strings::LENGT    
100     module_name: &[u8],                           
101     args: fmt::Arguments<'_>,                     
102 ) {                                               
103     // `_printk` does not seem to fail in any     
104     #[cfg(CONFIG_PRINTK)]                         
105     unsafe {                                      
106         bindings::_printk(                        
107             format_string.as_ptr() as _,          
108             module_name.as_ptr(),                 
109             &args as *const _ as *const c_void    
110         );                                        
111     }                                             
112 }                                                 
113                                                   
114 /// Prints a message via the kernel's [`_print    
115 ///                                               
116 /// Public but hidden since it should only be     
117 ///                                               
118 /// [`_printk`]: srctree/include/linux/printk.    
119 #[doc(hidden)]                                    
120 #[cfg_attr(not(CONFIG_PRINTK), allow(unused_va    
121 pub fn call_printk_cont(args: fmt::Arguments<'    
122     // `_printk` does not seem to fail in any     
123     //                                            
124     // SAFETY: The format string is fixed.        
125     #[cfg(CONFIG_PRINTK)]                         
126     unsafe {                                      
127         bindings::_printk(                        
128             format_strings::CONT.as_ptr() as _    
129             &args as *const _ as *const c_void    
130         );                                        
131     }                                             
132 }                                                 
133                                                   
134 /// Performs formatting and forwards the strin    
135 ///                                               
136 /// Public but hidden since it should only be     
137 #[doc(hidden)]                                    
138 #[cfg(not(testlib))]                              
139 #[macro_export]                                   
140 #[allow(clippy::crate_in_macro_def)]              
141 macro_rules! print_macro (                        
142     // The non-continuation cases (most of the    
143     ($format_string:path, false, $($arg:tt)+)     
144         // To remain sound, `arg`s must be exp    
145         // Typically one would use a `let` bin    
146         // takes borrows on the arguments, but    
147         // Therefore, a `match` expression is     
148         // the scrutinee is kept until the end    
149         match format_args!($($arg)+) {            
150             // SAFETY: This hidden macro shoul    
151             // printing macros which ensure th    
152             // ones. All `__LOG_PREFIX`s are n    
153             // by the `module!` proc macro or     
154             // crate.                             
155             args => unsafe {                      
156                 $crate::print::call_printk(       
157                     &$format_string,              
158                     crate::__LOG_PREFIX,          
159                     args,                         
160                 );                                
161             }                                     
162         }                                         
163     );                                            
164                                                   
165     // The `CONT` case.                           
166     ($format_string:path, true, $($arg:tt)+) =    
167         $crate::print::call_printk_cont(          
168             format_args!($($arg)+),               
169         );                                        
170     );                                            
171 );                                                
172                                                   
173 /// Stub for doctests                             
174 #[cfg(testlib)]                                   
175 #[macro_export]                                   
176 macro_rules! print_macro (                        
177     ($format_string:path, $e:expr, $($arg:tt)+    
178         ()                                        
179     );                                            
180 );                                                
181                                                   
182 // We could use a macro to generate these macr    
183 // up being a bit ugly: it requires the dollar    
184 // well as playing with the `doc` attribute. F    
185 // imported in the prelude due to [1]. So, for    
186 // manually, like in the C side; while keeping    
187 // macro, i.e. [`print_macro`].                   
188 //                                                
189 // [1]: https://github.com/rust-lang/rust/issu    
190                                                   
191 /// Prints an emergency-level message (level 0    
192 ///                                               
193 /// Use this level if the system is unusable.     
194 ///                                               
195 /// Equivalent to the kernel's [`pr_emerg`] ma    
196 ///                                               
197 /// Mimics the interface of [`std::print!`]. S    
198 /// `alloc::format!` for information about the    
199 ///                                               
200 /// [`pr_emerg`]: https://docs.kernel.org/core    
201 /// [`std::print!`]: https://doc.rust-lang.org    
202 ///                                               
203 /// # Examples                                    
204 ///                                               
205 /// ```                                           
206 /// pr_emerg!("hello {}\n", "there");             
207 /// ```                                           
208 #[macro_export]                                   
209 macro_rules! pr_emerg (                           
210     ($($arg:tt)*) => (                            
211         $crate::print_macro!($crate::print::fo    
212     )                                             
213 );                                                
214                                                   
215 /// Prints an alert-level message (level 1).      
216 ///                                               
217 /// Use this level if action must be taken imm    
218 ///                                               
219 /// Equivalent to the kernel's [`pr_alert`] ma    
220 ///                                               
221 /// Mimics the interface of [`std::print!`]. S    
222 /// `alloc::format!` for information about the    
223 ///                                               
224 /// [`pr_alert`]: https://docs.kernel.org/core    
225 /// [`std::print!`]: https://doc.rust-lang.org    
226 ///                                               
227 /// # Examples                                    
228 ///                                               
229 /// ```                                           
230 /// pr_alert!("hello {}\n", "there");             
231 /// ```                                           
232 #[macro_export]                                   
233 macro_rules! pr_alert (                           
234     ($($arg:tt)*) => (                            
235         $crate::print_macro!($crate::print::fo    
236     )                                             
237 );                                                
238                                                   
239 /// Prints a critical-level message (level 2).    
240 ///                                               
241 /// Use this level for critical conditions.       
242 ///                                               
243 /// Equivalent to the kernel's [`pr_crit`] mac    
244 ///                                               
245 /// Mimics the interface of [`std::print!`]. S    
246 /// `alloc::format!` for information about the    
247 ///                                               
248 /// [`pr_crit`]: https://docs.kernel.org/core-    
249 /// [`std::print!`]: https://doc.rust-lang.org    
250 ///                                               
251 /// # Examples                                    
252 ///                                               
253 /// ```                                           
254 /// pr_crit!("hello {}\n", "there");              
255 /// ```                                           
256 #[macro_export]                                   
257 macro_rules! pr_crit (                            
258     ($($arg:tt)*) => (                            
259         $crate::print_macro!($crate::print::fo    
260     )                                             
261 );                                                
262                                                   
263 /// Prints an error-level message (level 3).      
264 ///                                               
265 /// Use this level for error conditions.          
266 ///                                               
267 /// Equivalent to the kernel's [`pr_err`] macr    
268 ///                                               
269 /// Mimics the interface of [`std::print!`]. S    
270 /// `alloc::format!` for information about the    
271 ///                                               
272 /// [`pr_err`]: https://docs.kernel.org/core-a    
273 /// [`std::print!`]: https://doc.rust-lang.org    
274 ///                                               
275 /// # Examples                                    
276 ///                                               
277 /// ```                                           
278 /// pr_err!("hello {}\n", "there");               
279 /// ```                                           
280 #[macro_export]                                   
281 macro_rules! pr_err (                             
282     ($($arg:tt)*) => (                            
283         $crate::print_macro!($crate::print::fo    
284     )                                             
285 );                                                
286                                                   
287 /// Prints a warning-level message (level 4).     
288 ///                                               
289 /// Use this level for warning conditions.        
290 ///                                               
291 /// Equivalent to the kernel's [`pr_warn`] mac    
292 ///                                               
293 /// Mimics the interface of [`std::print!`]. S    
294 /// `alloc::format!` for information about the    
295 ///                                               
296 /// [`pr_warn`]: https://docs.kernel.org/core-    
297 /// [`std::print!`]: https://doc.rust-lang.org    
298 ///                                               
299 /// # Examples                                    
300 ///                                               
301 /// ```                                           
302 /// pr_warn!("hello {}\n", "there");              
303 /// ```                                           
304 #[macro_export]                                   
305 macro_rules! pr_warn (                            
306     ($($arg:tt)*) => (                            
307         $crate::print_macro!($crate::print::fo    
308     )                                             
309 );                                                
310                                                   
311 /// Prints a notice-level message (level 5).      
312 ///                                               
313 /// Use this level for normal but significant     
314 ///                                               
315 /// Equivalent to the kernel's [`pr_notice`] m    
316 ///                                               
317 /// Mimics the interface of [`std::print!`]. S    
318 /// `alloc::format!` for information about the    
319 ///                                               
320 /// [`pr_notice`]: https://docs.kernel.org/cor    
321 /// [`std::print!`]: https://doc.rust-lang.org    
322 ///                                               
323 /// # Examples                                    
324 ///                                               
325 /// ```                                           
326 /// pr_notice!("hello {}\n", "there");            
327 /// ```                                           
328 #[macro_export]                                   
329 macro_rules! pr_notice (                          
330     ($($arg:tt)*) => (                            
331         $crate::print_macro!($crate::print::fo    
332     )                                             
333 );                                                
334                                                   
335 /// Prints an info-level message (level 6).       
336 ///                                               
337 /// Use this level for informational messages.    
338 ///                                               
339 /// Equivalent to the kernel's [`pr_info`] mac    
340 ///                                               
341 /// Mimics the interface of [`std::print!`]. S    
342 /// `alloc::format!` for information about the    
343 ///                                               
344 /// [`pr_info`]: https://docs.kernel.org/core-    
345 /// [`std::print!`]: https://doc.rust-lang.org    
346 ///                                               
347 /// # Examples                                    
348 ///                                               
349 /// ```                                           
350 /// pr_info!("hello {}\n", "there");              
351 /// ```                                           
352 #[macro_export]                                   
353 #[doc(alias = "print")]                           
354 macro_rules! pr_info (                            
355     ($($arg:tt)*) => (                            
356         $crate::print_macro!($crate::print::fo    
357     )                                             
358 );                                                
359                                                   
360 /// Prints a debug-level message (level 7).       
361 ///                                               
362 /// Use this level for debug messages.            
363 ///                                               
364 /// Equivalent to the kernel's [`pr_debug`] ma    
365 /// yet.                                          
366 ///                                               
367 /// Mimics the interface of [`std::print!`]. S    
368 /// `alloc::format!` for information about the    
369 ///                                               
370 /// [`pr_debug`]: https://docs.kernel.org/core    
371 /// [`std::print!`]: https://doc.rust-lang.org    
372 ///                                               
373 /// # Examples                                    
374 ///                                               
375 /// ```                                           
376 /// pr_debug!("hello {}\n", "there");             
377 /// ```                                           
378 #[macro_export]                                   
379 #[doc(alias = "print")]                           
380 macro_rules! pr_debug (                           
381     ($($arg:tt)*) => (                            
382         if cfg!(debug_assertions) {               
383             $crate::print_macro!($crate::print    
384         }                                         
385     )                                             
386 );                                                
387                                                   
388 /// Continues a previous log message in the sa    
389 ///                                               
390 /// Use only when continuing a previous `pr_*!    
391 ///                                               
392 /// Equivalent to the kernel's [`pr_cont`] mac    
393 ///                                               
394 /// Mimics the interface of [`std::print!`]. S    
395 /// `alloc::format!` for information about the    
396 ///                                               
397 /// [`pr_info!`]: crate::pr_info!                 
398 /// [`pr_cont`]: https://docs.kernel.org/core-    
399 /// [`std::print!`]: https://doc.rust-lang.org    
400 ///                                               
401 /// # Examples                                    
402 ///                                               
403 /// ```                                           
404 /// # use kernel::pr_cont;                        
405 /// pr_info!("hello");                            
406 /// pr_cont!(" {}\n", "there");                   
407 /// ```                                           
408 #[macro_export]                                   
409 macro_rules! pr_cont (                            
410     ($($arg:tt)*) => (                            
411         $crate::print_macro!($crate::print::fo    
412     )                                             
413 );                                                
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php