diff --git a/src/os/module.rs b/src/os/module.rs index 5c1767f..b2a760b 100644 --- a/src/os/module.rs +++ b/src/os/module.rs @@ -10,6 +10,7 @@ use goblin::elf::{sym, Elf, SectionHeader}; #[cfg(target_os = "linux")] use std::path::PathBuf; +/// Represents the data associated with a specific module on Linux. #[cfg(target_os = "linux")] #[derive(Debug)] pub struct ModuleEntry { @@ -17,22 +18,30 @@ pub struct ModuleEntry { pub start_addr: usize, pub data: Vec, } + /// Represents a module loaded in a Windows process. +#[cfg(target_os = "windows")] pub struct Module<'a> { /// The name of the module. pub name: &'a str, /// A reference to a slice of bytes containing the module data. - #[cfg(target_os = "windows")] pub data: &'a [u8], - #[cfg(target_os = "linux")] - pub module_info: &'a ModuleEntry, - #[cfg(target_os = "windows")] /// The PE file format representation of the module. pub pe: PE<'a>, +} - #[cfg(target_os = "linux")] +/// Represents a module loaded in a Linux process. +#[cfg(target_os = "linux")] +pub struct Module<'a> { + /// The name of the module. + pub name: &'a str, + + /// A reference to a slice of bytes containing the module info. + pub module_info: &'a ModuleEntry, + + /// The Elf file format representation of the module. pub elf: Elf<'a>, } diff --git a/src/os/process.rs b/src/os/process.rs index ee813b8..d329b36 100644 --- a/src/os/process.rs +++ b/src/os/process.rs @@ -30,20 +30,27 @@ use std::io::{Read, Seek, SeekFrom}; use std::path::{Path, PathBuf}; /// Represents a Windows process. +#[cfg(target_os = "windows")] #[derive(Debug)] pub struct Process { /// ID of the process. id: u32, - #[cfg(target_os = "windows")] /// Handle to the process. handle: HANDLE, /// A HashMap containing the name of each module and its corresponding raw data. - #[cfg(target_os = "windows")] modules: HashMap>, +} - #[cfg(target_os = "linux")] +/// Represents a Linux process. +#[cfg(target_os = "linux")] +#[derive(Debug)] +pub struct Process { + /// PID of the process. + pid: u32, + + /// A HashMap containing the name of each module and its corresponding data. modules: HashMap, } @@ -65,9 +72,9 @@ impl Process { #[cfg(target_os = "linux")] pub fn new(name: &str) -> Result { - let id = Self::get_process_id_by_name(name)?; + let pid = Self::get_process_pid_by_name(name)?; let mut process = Self { - id, + pid, modules: HashMap::new(), }; process.parse_loaded_modules()?; @@ -160,7 +167,7 @@ impl Process { #[cfg(target_os = "linux")] pub fn read_memory_raw(&self, address: usize, buffer: *mut c_void, size: usize) -> Result<()> { - let proc_mem_path = format!("/proc/{}/mem", self.id); + let proc_mem_path = format!("/proc/{}/mem", self.pid); let mut mem_file = File::open(proc_mem_path)?; // Go to the start address @@ -257,7 +264,7 @@ impl Process { } #[cfg(target_os = "linux")] - fn get_process_id_by_name(process_name: &str) -> Result { + fn get_process_pid_by_name(process_name: &str) -> Result { use std::io::{BufRead, BufReader}; for process_iter in all_processes()? { @@ -308,7 +315,7 @@ impl Process { #[cfg(target_os = "linux")] fn parse_loaded_modules(&mut self) -> Result<()> { - let process = process::Process::new(self.id as i32)?; + let process = process::Process::new(self.pid as i32)?; let mut modules_info: HashMap = HashMap::new();