| 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 INFO: [u8; LENGTH] = generate(false, bindings::KERN_INFO);
|
| 78 | }
|
| 79 |
|
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[doc(hidden)]
|
| 91 | #[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
|
| 92 | pub unsafe fn call_printk(
|
| 93 | format_string: &[u8; format_strings::LENGTH],
|
| 94 | module_name: &[u8],
|
| 95 | args: fmt::Arguments<'_>,
|
| 96 | ) {
|
| 97 | |
| 98 | #[cfg(CONFIG_PRINTK)]
|
| 99 | unsafe {
|
| 100 | bindings::_printk(
|
| 101 | format_string.as_ptr() as _,
|
| 102 | module_name.as_ptr(),
|
| 103 | &args as *const _ as *const c_void,
|
| 104 | );
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | |
| 109 | |
| 110 | |
| 111 | #[doc(hidden)]
|
| 112 | #[cfg(not(testlib))]
|
| 113 | #[macro_export]
|
| 114 | #[allow(clippy::crate_in_macro_def)]
|
| 115 | macro_rules! print_macro (
|
| 116 | |
| 117 | ($format_string:path, $($arg:tt)+) => (
|
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | unsafe {
|
| 124 | $crate::print::call_printk(
|
| 125 | &$format_string,
|
| 126 | crate::__LOG_PREFIX,
|
| 127 | format_args!($($arg)+),
|
| 128 | );
|
| 129 | }
|
| 130 | );
|
| 131 | );
|
| 132 |
|
| 133 | |
| 134 | #[cfg(testlib)]
|
| 135 | #[macro_export]
|
| 136 | macro_rules! print_macro (
|
| 137 | ($format_string:path, $e:expr, $($arg:tt)+) => (
|
| 138 | ()
|
| 139 | );
|
| 140 | );
|
| 141 |
|
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 |
|
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | #[macro_export]
|
| 169 | macro_rules! pr_emerg (
|
| 170 | ($($arg:tt)*) => (
|
| 171 | $crate::print_macro!($crate::print::format_strings::EMERG, $($arg)*)
|
| 172 | )
|
| 173 | );
|
| 174 |
|
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | #[macro_export]
|
| 193 | #[doc(alias = "print")]
|
| 194 | macro_rules! pr_info (
|
| 195 | ($($arg:tt)*) => (
|
| 196 | $crate::print_macro!($crate::print::format_strings::INFO, $($arg)*)
|
| 197 | )
|
| 198 | );
|