From 65f84678a498e827e6af08c170b175304aaba961 Mon Sep 17 00:00:00 2001 From: RinHizakura Date: Sun, 10 Mar 2024 22:45:19 +0800 Subject: [PATCH] Refine kernel printing mechanism --- os/src/console.rs | 6 ++++++ os/src/fs/mod.rs | 10 ++++------ os/src/mm/mapping.rs | 2 -- os/src/syscall/mod.rs | 2 +- os/src/syscall/proc.rs | 6 +++--- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/os/src/console.rs b/os/src/console.rs index 4de7f50..9337b82 100644 --- a/os/src/console.rs +++ b/os/src/console.rs @@ -28,6 +28,12 @@ macro_rules! println { } } +macro_rules! dbg { + ($fmt: literal $(, $($arg: tt)+)?) => { + $crate::console::print(format_args!(concat!("\x1b[0;32m", $fmt, "\x1b[0m\n") $(, $($arg)+)?)); + } +} + macro_rules! info { ($fmt: literal $(, $($arg: tt)+)?) => { $crate::console::print(format_args!(concat!("\x1b[1;94m", $fmt, "\x1b[0m\n") $(, $($arg)+)?)); diff --git a/os/src/fs/mod.rs b/os/src/fs/mod.rs index 6ad47df..a4fc9d0 100644 --- a/os/src/fs/mod.rs +++ b/os/src/fs/mod.rs @@ -50,7 +50,7 @@ impl Drop for FsInode { bwrite(iblock(&SB.lock(), inum), &inodes); - println!("Drop inum={}", inum); + dbg!("Release inode, inum={}", inum); } } @@ -60,8 +60,6 @@ pub fn init() { bread(1, &mut buf); *SB.lock() = *to_struct::(&buf); - - println!("nb = {:x}", SB.lock().nblocks); } // Seperate the first path entry from the path string @@ -81,7 +79,7 @@ fn parse_path<'a>(path: &'a str) -> Option<(&'a str, &'a str)> { // Find the corresponding inode by inode number pub fn find_inode(inum: u32) -> FsInode { - println!("Find inum={}", inum); + dbg!("Get inode, inum={}", inum); let mut inodes = vec![0; BLKSZ]; bread(iblock(&SB.lock(), inum), &inodes); @@ -110,6 +108,7 @@ pub fn alloc_inode(typ: u16, major: u16, minor: u16, nlink: u16) -> u32 { if inode_ptr.is_free() { inode_ptr.init(typ, major, minor, nlink); bwrite(iblock, &inodes); + dbg!("Alloc inode, inum={}", inum); return inum; } } @@ -320,7 +319,7 @@ pub fn dirlink(fsinode: &mut FsInode, name: &str, inum: u32) -> bool { // Find the corresponding inode by the path pub fn path_to_inode(mut path: &str) -> Option { - info!("Traslate path {} to inode", path); + dbg!("Traslate path {} to inode", path); /* FIXME: We only support to use the absolute path which * starting from root now. Allow relative path in the future. */ @@ -333,7 +332,6 @@ pub fn path_to_inode(mut path: &str) -> Option { } while let Some((path_head, path_tail)) = parse_path(path) { - println!("parse_path: head={} / tail={}", path_head, path_tail); /* This inode is corresponded to a directory, but we want to find * a file under it. This is an invalid request. */ if inode.inner.typ != T_DIR { diff --git a/os/src/mm/mapping.rs b/os/src/mm/mapping.rs index 138e0de..7b5c186 100644 --- a/os/src/mm/mapping.rs +++ b/os/src/mm/mapping.rs @@ -201,14 +201,12 @@ impl Mapping { while !success && total < len { let va = align_down!(addr, PAGE_SIZE); - println!("va {:x}", va); let pa = self.walk(va as u64); // Unable to find the corresponding physical address if pa.is_none() { break; } let pa = pa.unwrap() as usize; - println!("va {:x} -> pa {:x}", va, pa); let mut ptr = (pa + (addr - va)) as *const u8; let mut n = (PAGE_SIZE - (addr - va)).min(len - total); while n > 0 { diff --git a/os/src/syscall/mod.rs b/os/src/syscall/mod.rs index 3421b2a..886a4d6 100644 --- a/os/src/syscall/mod.rs +++ b/os/src/syscall/mod.rs @@ -17,7 +17,7 @@ pub fn syscall_handler() { // a7 is the number of syscall let syscall_num = unsafe { (*frame).get_a(7) }; - warning!("SYSCALL {}", syscall_num); + info!("receive SYSCALL={}", syscall_num); let result = match syscall_num { SYS_OPEN => proc::sys_open() as usize, diff --git a/os/src/syscall/proc.rs b/os/src/syscall/proc.rs index ccf8490..cf8ddc3 100644 --- a/os/src/syscall/proc.rs +++ b/os/src/syscall/proc.rs @@ -44,10 +44,10 @@ fn path_to_parent_file(path: &str) -> Option<(&str, &str)> { } fn create(path: &str, typ: u16, major: u16, minor: u16) -> Option { - let (path, file) = path_to_parent_file(path)?; - println!("parent = {}, file = {}", path, file); + let (parent, file) = path_to_parent_file(path)?; + dbg!("Create file {} under {}", file, parent); - let mut parent_inode = path_to_inode(path)?; + let mut parent_inode = path_to_inode(parent)?; if let Some(file_inode) = dirlookup(&parent_inode, file) { // The inode for the file already exists return Some(file_inode);