1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 00:16:17 +04:00

Save and log file paths on sys_open and sys_close

This commit is contained in:
Jiajie Chen 2019-05-02 15:07:06 +08:00
parent 6c988c4bfd
commit 81fde731d0
4 changed files with 60 additions and 5 deletions

View File

@ -1,6 +1,7 @@
//! File handle for process
use alloc::{string::String, sync::Arc};
use core::fmt;
use rcore_fs::vfs::{FsError, INode, Metadata, PollStatus, Result};
@ -9,6 +10,10 @@ pub struct FileHandle {
inode: Arc<INode>,
offset: u64,
options: OpenOptions,
// for debugging
#[cfg(debug_assertions)]
path: String,
}
#[derive(Debug, Clone)]
@ -28,11 +33,29 @@ pub enum SeekFrom {
impl FileHandle {
pub fn new(inode: Arc<INode>, options: OpenOptions) -> Self {
FileHandle {
#[cfg(debug_assertions)]
return FileHandle {
inode,
offset: 0,
options,
path: String::from("unknown"),
};
#[cfg(not(debug_assertions))]
return FileHandle {
inode,
offset: 0,
options,
};
}
#[cfg(debug_assertions)]
pub fn set_path(&mut self, path: &str) {
self.path = String::from(path);
}
#[cfg(not(debug_assertions))]
pub fn set_path(&mut self, _path: &str) {
unreachable!()
}
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
@ -121,3 +144,22 @@ impl FileHandle {
self.inode.clone()
}
}
impl fmt::Debug for FileHandle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// for debugging
#[cfg(debug_assertions)]
return f
.debug_struct("FileHandle")
.field("offset", &self.offset)
.field("options", &self.options)
.field("path", &self.path)
.finish();
#[cfg(not(debug_assertions))]
return f
.debug_struct("FileHandle")
.field("offset", &self.offset)
.field("options", &self.options)
.finish();
}
}

View File

@ -53,7 +53,7 @@ impl FileLike {
impl fmt::Debug for FileLike {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
FileLike::File(_) => write!(f, "File"),
FileLike::File(file) => write!(f, "File {:?}", file),
FileLike::Socket(_) => write!(f, "Socket"),
}
}

View File

@ -157,7 +157,7 @@ pub fn enlarge_heap(heap: &mut Heap) {
let page = alloc_frame().unwrap();
let va = KERNEL_OFFSET + 0xe0000000 + page;
if addr_len > 0 {
let (ref mut addr, ref mut len) = addrs[addr_len-1];
let (ref mut addr, ref mut len) = addrs[addr_len - 1];
if *addr - PAGE_SIZE == va {
*len += PAGE_SIZE;
*addr -= PAGE_SIZE;

View File

@ -273,7 +273,14 @@ pub fn sys_openat(dir_fd: usize, path: *const u8, flags: usize, mode: usize) ->
proc.lookup_inode_at(dir_fd, &path, true)?
};
let file = FileHandle::new(inode, flags.to_options());
let mut file = FileHandle::new(inode, flags.to_options());
// for debugging
if cfg!(debug_assertions) {
file.set_path(&path);
debug!("files before open {:#?}", proc.files);
}
let fd = proc.add_file(FileLike::File(file));
Ok(fd)
}
@ -281,6 +288,12 @@ pub fn sys_openat(dir_fd: usize, path: *const u8, flags: usize, mode: usize) ->
pub fn sys_close(fd: usize) -> SysResult {
info!("close: fd: {:?}", fd);
let mut proc = process();
// for debugging
if cfg!(debug_assertions) {
debug!("files before close {:#?}", proc.files);
}
proc.files.remove(&fd).ok_or(SysError::EBADF)?;
Ok(0)
}