Skip to content

Commit

Permalink
Refine kernel printing mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
RinHizakura committed Mar 10, 2024
1 parent dfd3c1e commit 65f8467
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
6 changes: 6 additions & 0 deletions os/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)+)?));
Expand Down
10 changes: 4 additions & 6 deletions os/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Drop for FsInode {

bwrite(iblock(&SB.lock(), inum), &inodes);

println!("Drop inum={}", inum);
dbg!("Release inode, inum={}", inum);
}
}

Expand All @@ -60,8 +60,6 @@ pub fn init() {
bread(1, &mut buf);

*SB.lock() = *to_struct::<SuperBlock>(&buf);

println!("nb = {:x}", SB.lock().nblocks);
}

// Seperate the first path entry from the path string
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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<FsInode> {
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. */
Expand All @@ -333,7 +332,6 @@ pub fn path_to_inode(mut path: &str) -> Option<FsInode> {
}

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 {
Expand Down
2 changes: 0 additions & 2 deletions os/src/mm/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion os/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions os/src/syscall/proc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FsInode> {
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);
Expand Down

0 comments on commit 65f8467

Please sign in to comment.