Skip to content

Commit fef0214

Browse files
adamrkojeda
authored andcommitted
rust: allow printing in the kernel crate
The `pr_*` print macros previously didn't compile when used from the `kernel` crate because they refer to the path `kernel` instead of using the `$crate` variable and require a `const __MODULE_NAME` to be defined in any crate in which they are used. This fixes both issues and changes the `__MODULE_NAME` to `__LOG_PREFIX` to reflect the fact that the print macro may be called from a module or a kernel library. Signed-off-by: Adam Bratschi-Kaye <[email protected]> [normalized title] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 43f76a4 commit fef0214

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

rust/kernel/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub use crate::types::{CStr, Mode};
7070
/// [`PAGE_SHIFT`]: ../../../include/asm-generic/page.h
7171
pub const PAGE_SIZE: usize = 1 << bindings::PAGE_SHIFT;
7272

73+
/// Prefix to appear before log messages printed from within the kernel crate.
74+
const __LOG_PREFIX: &[u8] = b"rust_kernel\0";
75+
7376
/// The top level entrypoint to implementing a kernel module.
7477
///
7578
/// For any teardown or cleanup operations, your type may implement [`Drop`].

rust/kernel/print.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,21 @@ macro_rules! print_macro (
209209
($format_string:path, false, $fmt:expr) => (
210210
// SAFETY: This hidden macro should only be called by the documented
211211
// printing macros which ensure the format string is one of the fixed
212-
// ones. All `__MODULE_NAME`s are null-terminated as they are generated
213-
// by the `module!` proc macro.
212+
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
213+
// by the `module!` proc macro or fixed values defined in a kernel
214+
// crate.
214215
unsafe {
215-
kernel::print::call_printk(
216+
$crate::print::call_printk(
216217
&$format_string,
217-
crate::__MODULE_NAME,
218+
crate::__LOG_PREFIX,
218219
$fmt.as_bytes(),
219220
);
220221
}
221222
);
222223

223224
// Without extra arguments: no need to format anything (`CONT` case).
224225
($format_string:path, true, $fmt:expr) => (
225-
kernel::print::call_printk_cont(
226+
$crate::print::call_printk_cont(
226227
$fmt.as_bytes(),
227228
);
228229
);
@@ -245,12 +246,13 @@ macro_rules! print_macro (
245246
//
246247
// SAFETY: This hidden macro should only be called by the documented
247248
// printing macros which ensure the format string is one of the fixed
248-
// ones. All `__MODULE_NAME`s are null-terminated as they are generated
249-
// by the `module!` proc macro.
249+
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
250+
// by the `module!` proc macro or fixed values defined in a kernel
251+
// crate.
250252
unsafe {
251-
kernel::print::format_and_call::<$cont>(
253+
$crate::print::format_and_call::<$cont>(
252254
&$format_string,
253-
crate::__MODULE_NAME,
255+
crate::__LOG_PREFIX,
254256
format_args!($fmt, $($arg)*),
255257
);
256258
}

rust/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
587587
/// The module name.
588588
///
589589
/// Used by the printing macros, e.g. [`info!`].
590-
const __MODULE_NAME: &[u8] = b\"{name}\\0\";
590+
const __LOG_PREFIX: &[u8] = b\"{name}\\0\";
591591
592592
static mut __MOD: Option<{type_}> = None;
593593

0 commit comments

Comments
 (0)