Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 1ed0804

Browse files
committed
add eprintln macro
1 parent b9a4c2d commit 1ed0804

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ const INCLUDED_VARS: &[&str] = &[
3939
"FS_USERNS_MOUNT",
4040
"FS_RENAME_DOES_D_MOVE",
4141
"BINDINGS_GFP_KERNEL",
42+
"KERN_EMERG",
43+
"KERN_ALERT",
44+
"KERN_CRIT",
45+
"KERN_ERR",
46+
"KERN_WARNING",
47+
"KERN_NOTICE",
4248
"KERN_INFO",
49+
"KERN_DEBUG",
4350
"VERIFY_WRITE",
4451
"LINUX_VERSION_CODE",
4552
"SEEK_SET",

hello-world/Kbuild

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ helloworld-objs := hello_world.rust.o
44
CARGO ?= cargo
55

66
export c_flags
7+
export KDIR
78

89
$(src)/target/x86_64-linux-kernel/debug/libhello_world.a: cargo_will_determine_dependencies
910
cd $(src); $(CARGO) build -Z build-std=core,alloc --target=x86_64-linux-kernel

hello-world/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use alloc::borrow::ToOwned;
66
use alloc::string::String;
77

88
use linux_kernel_module::println;
9+
use linux_kernel_module::eprintln;
910

1011
struct HelloWorldModule {
1112
message: String,
@@ -24,6 +25,7 @@ impl Drop for HelloWorldModule {
2425
fn drop(&mut self) {
2526
println!("My message is {}", self.message);
2627
println!("Goodbye kernel module!");
28+
eprintln!("Module unloaded");
2729
}
2830
}
2931

src/printk.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ use core::fmt;
44
use crate::bindings;
55
use crate::c_types::c_int;
66

7+
pub use crate::bindings::{
8+
KERN_ALERT, KERN_CRIT, KERN_DEBUG, KERN_EMERG, KERN_ERR, KERN_INFO, KERN_NOTICE, KERN_WARNING,
9+
};
10+
711
#[doc(hidden)]
8-
pub fn printk(s: &[u8]) {
12+
pub fn printk(s: &[u8], level: &'static [u8; 3usize]) {
913
// Don't copy the trailing NUL from `KERN_INFO`.
1014
let mut fmt_str = [0; bindings::KERN_INFO.len() - 1 + b"%.*s\0".len()];
1115
fmt_str[..bindings::KERN_INFO.len() - 1]
12-
.copy_from_slice(&bindings::KERN_INFO[..bindings::KERN_INFO.len() - 1]);
16+
.copy_from_slice(&level[..bindings::KERN_INFO.len() - 1]);
1317
fmt_str[bindings::KERN_INFO.len() - 1..].copy_from_slice(b"%.*s\0");
1418

1519
// TODO: I believe printk never fails
@@ -56,15 +60,36 @@ impl fmt::Write for LogLineWriter {
5660
#[macro_export]
5761
macro_rules! println {
5862
() => ({
59-
$crate::printk::printk("\n".as_bytes());
63+
$crate::printk::printk("\n".as_bytes(), $crate::printk::KERN_INFO);
64+
});
65+
($fmt:expr) => ({
66+
$crate::printk::printk(concat!($fmt, "\n").as_bytes(), $crate::printk::KERN_INFO);
67+
});
68+
($fmt:expr, $($arg:tt)*) => ({
69+
use ::core::fmt;
70+
let mut writer = $crate::printk::LogLineWriter::new();
71+
let _ = fmt::write(&mut writer, format_args!(concat!($fmt, "\n"), $($arg)*)).unwrap();
72+
$crate::printk::printk(writer.as_bytes(), $crate::printk::KERN_INFO);
73+
});
74+
}
75+
76+
/// [`eprintln!`] functions the same as it does in `std`, except instead of
77+
/// printing to `stdout`, it writes to the kernel console at the `KERN_ERR`
78+
/// level.
79+
///
80+
/// [`eprintln!`]: https://doc.rust-lang.org/stable/std/macro.eprintln.html
81+
#[macro_export]
82+
macro_rules! eprintln {
83+
() => ({
84+
$crate::printk::printk("\n".as_bytes(), $crate::printk::KERN_ERR);
6085
});
6186
($fmt:expr) => ({
62-
$crate::printk::printk(concat!($fmt, "\n").as_bytes());
87+
$crate::printk::printk(concat!($fmt, "\n").as_bytes(), $crate::printk::KERN_ERR);
6388
});
6489
($fmt:expr, $($arg:tt)*) => ({
6590
use ::core::fmt;
6691
let mut writer = $crate::printk::LogLineWriter::new();
6792
let _ = fmt::write(&mut writer, format_args!(concat!($fmt, "\n"), $($arg)*)).unwrap();
68-
$crate::printk::printk(writer.as_bytes());
93+
$crate::printk::printk(writer.as_bytes(), $crate::printk::KERN_ERR);
6994
});
7095
}

0 commit comments

Comments
 (0)