Skip to content

print.rs: Allow printing in the kernel crate #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub use crate::types::{CStr, Mode};
/// [`PAGE_SHIFT`]: ../../../include/asm-generic/page.h
pub const PAGE_SIZE: usize = 1 << bindings::PAGE_SHIFT;

/// Prefix to appear before log messages printed from within the kernel crate.
const __LOG_PREFIX: &[u8] = b"rust_kernel\0";

/// The top level entrypoint to implementing a kernel module.
///
/// For any teardown or cleanup operations, your type may implement [`Drop`].
Expand Down
20 changes: 11 additions & 9 deletions rust/kernel/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,21 @@ macro_rules! print_macro (
($format_string:path, false, $fmt:expr) => (
// SAFETY: This hidden macro should only be called by the documented
// printing macros which ensure the format string is one of the fixed
// ones. All `__MODULE_NAME`s are null-terminated as they are generated
// by the `module!` proc macro.
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
// by the `module!` proc macro or fixed values defined in a kernel
// crate.
unsafe {
kernel::print::call_printk(
$crate::print::call_printk(
&$format_string,
crate::__MODULE_NAME,
crate::__LOG_PREFIX,
$fmt.as_bytes(),
);
}
);

// Without extra arguments: no need to format anything (`CONT` case).
($format_string:path, true, $fmt:expr) => (
kernel::print::call_printk_cont(
$crate::print::call_printk_cont(
$fmt.as_bytes(),
);
);
Expand All @@ -245,12 +246,13 @@ macro_rules! print_macro (
//
// SAFETY: This hidden macro should only be called by the documented
// printing macros which ensure the format string is one of the fixed
// ones. All `__MODULE_NAME`s are null-terminated as they are generated
// by the `module!` proc macro.
// ones. All `__LOG_PREFIX`s are null-terminated as they are generated
// by the `module!` proc macro or fixed values defined in a kernel
// crate.
unsafe {
kernel::print::format_and_call::<$cont>(
$crate::print::format_and_call::<$cont>(
&$format_string,
crate::__MODULE_NAME,
crate::__LOG_PREFIX,
format_args!($fmt, $($arg)*),
);
}
Expand Down
2 changes: 1 addition & 1 deletion rust/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
/// The module name.
///
/// Used by the printing macros, e.g. [`info!`].
const __MODULE_NAME: &[u8] = b\"{name}\\0\";
const __LOG_PREFIX: &[u8] = b\"{name}\\0\";

static mut __MOD: Option<{type_}> = None;

Expand Down