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