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