mapped_lib.rs (1191B) - raw


      1 use std::error::Error;
      2 
      3 use procfs::process::{MMPermissions, MMapPath};
      4 
      5 #[derive(Debug)]
      6 pub(crate) struct MappedRegion {
      7     pub start: u64,
      8     pub end: u64,
      9     pub perms: MMPermissions,
     10 }
     11 
     12 #[derive(Debug)]
     13 pub(crate) struct MappedLib {
     14     name: String,
     15     pub regions: Vec<MappedRegion>,
     16 }
     17 
     18 impl MappedLib {
     19     pub fn new(name: String) -> Self {
     20         Self {
     21             name,
     22             regions: Vec::new(),
     23         }
     24     }
     25 
     26     pub fn search(&mut self) -> Result<&Self, Box<dyn Error>> {
     27         procfs::process::Process::myself()?.maps()?.iter().for_each(|map| {
     28             let pathname = &map.pathname;
     29 
     30             if let MMapPath::Path(path_buffer) = pathname {
     31                 let path = path_buffer.to_string_lossy();
     32 
     33                 if path.contains(&self.name) {
     34                     self.regions.push(MappedRegion {
     35                         start: map.address.0,
     36                         end: map.address.1,
     37                         perms: map.perms,
     38                     });
     39                 }
     40             }
     41         });
     42 
     43         if self.regions.is_empty() {
     44             return Err(format!("No regions found for {}", self.name).into());
     45         }
     46 
     47         Ok(self)
     48     }
     49 }