Save and log file paths on sys_open and sys_close

toolchain_update
Jiajie Chen 6 years ago
parent 6c988c4bfd
commit 81fde731d0

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

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

@ -157,7 +157,7 @@ pub fn enlarge_heap(heap: &mut Heap) {
let page = alloc_frame().unwrap(); let page = alloc_frame().unwrap();
let va = KERNEL_OFFSET + 0xe0000000 + page; let va = KERNEL_OFFSET + 0xe0000000 + page;
if addr_len > 0 { 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 { if *addr - PAGE_SIZE == va {
*len += PAGE_SIZE; *len += PAGE_SIZE;
*addr -= PAGE_SIZE; *addr -= PAGE_SIZE;

@ -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)? 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)); let fd = proc.add_file(FileLike::File(file));
Ok(fd) 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 { pub fn sys_close(fd: usize) -> SysResult {
info!("close: fd: {:?}", fd); info!("close: fd: {:?}", fd);
let mut proc = process(); let mut proc = process();
// for debugging
if cfg!(debug_assertions) {
debug!("files before close {:#?}", proc.files);
}
proc.files.remove(&fd).ok_or(SysError::EBADF)?; proc.files.remove(&fd).ok_or(SysError::EBADF)?;
Ok(0) Ok(0)
} }

Loading…
Cancel
Save