| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 |
|
| 9 | use core::{
|
| 10 | ffi::{c_char, c_void},
|
| 11 | fmt,
|
| 12 | };
|
| 13 |
|
| 14 | use crate::str::RawFormatter;
|
| 15 |
|
| 16 | |
| 17 | #[expect(clippy::missing_safety_doc)]
|
| 18 | #[no_mangle]
|
| 19 | unsafe extern "C" fn rust_fmt_argument(
|
| 20 | buf: *mut c_char,
|
| 21 | end: *mut c_char,
|
| 22 | ptr: *const c_void,
|
| 23 | ) -> *mut c_char {
|
| 24 | use fmt::Write;
|
| 25 | |
| 26 | let mut w = unsafe { RawFormatter::from_ptrs(buf.cast(), end.cast()) };
|
| 27 | |
| 28 | let _ = w.write_fmt(unsafe { *(ptr as *const fmt::Arguments<'_>) });
|
| 29 | w.pos().cast()
|
| 30 | }
|
| 31 |
|
| 32 | |
| 33 | |
| 34 | |
| 35 | #[doc(hidden)]
|
| 36 | pub mod format_strings {
|
| 37 | |
| 38 | const LENGTH_PREFIX: usize = 2;
|
| 39 |
|
| 40 | |
| 41 | pub const LENGTH: usize = 10;
|
| 42 |
|
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | const fn generate(is_cont: bool, prefix: &[u8; 3]) -> [u8; LENGTH] {
|
| 50 | |
| 51 | assert!(prefix[0] == b'\x01');
|
| 52 | if is_cont {
|
| 53 | assert!(prefix[1] == b'c');
|
| 54 | } else {
|
| 55 | assert!(prefix[1] >= b'0' && prefix[1] <= b'7');
|
| 56 | }
|
| 57 | assert!(prefix[2] == b'\x00');
|
| 58 |
|
| 59 | let suffix: &[u8; LENGTH - LENGTH_PREFIX] = if is_cont {
|
| 60 | b"%pA\0\0\0\0\0"
|
| 61 | } else {
|
| 62 | b"%s: %pA\0"
|
| 63 | };
|
| 64 |
|
| 65 | [
|
| 66 | prefix[0], prefix[1], suffix[0], suffix[1], suffix[2], suffix[3], suffix[4], suffix[5],
|
| 67 | suffix[6], suffix[7],
|
| 68 | ]
|
| 69 | }
|
| 70 |
|
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 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);
|
| 86 | }
|
| 87 |
|
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | #[doc(hidden)]
|
| 99 | #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
|
| 100 | pub unsafe fn call_printk(
|
| 101 | format_string: &[u8; format_strings::LENGTH],
|
| 102 | module_name: &[u8],
|
| 103 | args: fmt::Arguments<'_>,
|
| 104 | ) {
|
| 105 | |
| 106 | #[cfg(CONFIG_PRINTK)]
|
| 107 | |
| 108 | unsafe {
|
| 109 | bindings::_printk(
|
| 110 | format_string.as_ptr() as _,
|
| 111 | module_name.as_ptr(),
|
| 112 | &args as *const _ as *const c_void,
|
| 113 | );
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | #[doc(hidden)]
|
| 123 | #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
|
| 124 | pub fn call_printk_cont(args: fmt::Arguments<'_>) {
|
| 125 | |
| 126 | |
| 127 | |
| 128 | #[cfg(CONFIG_PRINTK)]
|
| 129 | unsafe {
|
| 130 | bindings::_printk(
|
| 131 | format_strings::CONT.as_ptr() as _,
|
| 132 | &args as *const _ as *const c_void,
|
| 133 | );
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | |
| 138 | |
| 139 | |
| 140 | #[doc(hidden)]
|
| 141 | #[cfg(not(testlib))]
|
| 142 | #[macro_export]
|
| 143 | #[expect(clippy::crate_in_macro_def)]
|
| 144 | macro_rules! print_macro (
|
| 145 | |
| 146 | ($format_string:path, false, $($arg:tt)+) => (
|
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | match format_args!($($arg)+) {
|
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | args => unsafe {
|
| 159 | $crate::print::call_printk(
|
| 160 | &$format_string,
|
| 161 | crate::__LOG_PREFIX,
|
| 162 | args,
|
| 163 | );
|
| 164 | }
|
| 165 | }
|
| 166 | );
|
| 167 |
|
| 168 | |
| 169 | ($format_string:path, true, $($arg:tt)+) => (
|
| 170 | $crate::print::call_printk_cont(
|
| 171 | format_args!($($arg)+),
|
| 172 | );
|
| 173 | );
|
| 174 | );
|
| 175 |
|
| 176 | |
| 177 | #[cfg(testlib)]
|
| 178 | #[macro_export]
|
| 179 | macro_rules! print_macro (
|
| 180 | ($format_string:path, $e:expr, $($arg:tt)+) => (
|
| 181 | ()
|
| 182 | );
|
| 183 | );
|
| 184 |
|
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 |
|
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | #[macro_export]
|
| 212 | macro_rules! pr_emerg (
|
| 213 | ($($arg:tt)*) => (
|
| 214 | $crate::print_macro!($crate::print::format_strings::EMERG, false, $($arg)*)
|
| 215 | )
|
| 216 | );
|
| 217 |
|
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | #[macro_export]
|
| 236 | macro_rules! pr_alert (
|
| 237 | ($($arg:tt)*) => (
|
| 238 | $crate::print_macro!($crate::print::format_strings::ALERT, false, $($arg)*)
|
| 239 | )
|
| 240 | );
|
| 241 |
|
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | #[macro_export]
|
| 260 | macro_rules! pr_crit (
|
| 261 | ($($arg:tt)*) => (
|
| 262 | $crate::print_macro!($crate::print::format_strings::CRIT, false, $($arg)*)
|
| 263 | )
|
| 264 | );
|
| 265 |
|
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | #[macro_export]
|
| 284 | macro_rules! pr_err (
|
| 285 | ($($arg:tt)*) => (
|
| 286 | $crate::print_macro!($crate::print::format_strings::ERR, false, $($arg)*)
|
| 287 | )
|
| 288 | );
|
| 289 |
|
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | #[macro_export]
|
| 308 | macro_rules! pr_warn (
|
| 309 | ($($arg:tt)*) => (
|
| 310 | $crate::print_macro!($crate::print::format_strings::WARNING, false, $($arg)*)
|
| 311 | )
|
| 312 | );
|
| 313 |
|
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | #[macro_export]
|
| 332 | macro_rules! pr_notice (
|
| 333 | ($($arg:tt)*) => (
|
| 334 | $crate::print_macro!($crate::print::format_strings::NOTICE, false, $($arg)*)
|
| 335 | )
|
| 336 | );
|
| 337 |
|
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | #[macro_export]
|
| 356 | #[doc(alias = "print")]
|
| 357 | macro_rules! pr_info (
|
| 358 | ($($arg:tt)*) => (
|
| 359 | $crate::print_macro!($crate::print::format_strings::INFO, false, $($arg)*)
|
| 360 | )
|
| 361 | );
|
| 362 |
|
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | #[macro_export]
|
| 382 | #[doc(alias = "print")]
|
| 383 | macro_rules! pr_debug (
|
| 384 | ($($arg:tt)*) => (
|
| 385 | if cfg!(debug_assertions) {
|
| 386 | $crate::print_macro!($crate::print::format_strings::DEBUG, false, $($arg)*)
|
| 387 | }
|
| 388 | )
|
| 389 | );
|
| 390 |
|
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | #[macro_export]
|
| 412 | macro_rules! pr_cont (
|
| 413 | ($($arg:tt)*) => (
|
| 414 | $crate::print_macro!($crate::print::format_strings::CONT, true, $($arg)*)
|
| 415 | )
|
| 416 | );
|