use std::fmt;
use crate::error;
#[derive(Clone, Default, Debug)]
pub struct File {
pub start: u64,
pub end: u64,
pub offset: u64,
pub name: String,
}
impl File {
pub fn new(start: u64, end: u64, offset: u64, fname: &str) -> Self {
File {
start,
end,
offset,
name: String::from(fname),
}
}
}
impl fmt::Display for File {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"File {{ Start: 0x{:x}, End: 0x{:x}, offset: 0x{:x}, path: {} }}",
self.start, self.end, self.offset, self.name
)
}
}
pub type MappedFiles = Vec<File>;
pub trait MappedFilesExt {
fn from_gdb<T: AsRef<str>>(mapping: T) -> error::Result<MappedFiles>;
fn find(&self, addr: u64) -> Option<File>;
}
impl MappedFilesExt for MappedFiles {
fn from_gdb<T: AsRef<str>>(mapping: T) -> error::Result<MappedFiles> {
let mut hlp = mapping
.as_ref()
.lines()
.map(|s| s.trim().to_string())
.collect::<Vec<String>>();
if let Some(pos) = hlp.iter().position(|x| x.contains("Start Addr")) {
hlp.drain(0..pos + 1);
}
let mut info = Vec::new();
let mut name_idx = 0;
for x in hlp.iter() {
let filevec = x
.split_whitespace()
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect::<Vec<String>>();
if filevec.len() < 4 {
return Err(error::Error::MappedFilesParse(format!(
"Expected at least 4 columns in {x}"
)));
}
name_idx = name_idx.max(filevec.len() - 1);
info.push(filevec);
}
let mut files = MappedFiles::new();
for x in info.iter() {
let f = File {
start: u64::from_str_radix(x[0].get(2..).unwrap_or(&x[0]), 16)?,
end: u64::from_str_radix(x[1].get(2..).unwrap_or(&x[1]), 16)?,
offset: u64::from_str_radix(x[3].get(2..).unwrap_or(&x[3]), 16)?,
name: x.get(name_idx).unwrap_or(&String::new()).clone(),
};
files.push(f);
}
Ok(files)
}
fn find(&self, addr: u64) -> Option<File> {
self.iter()
.find(|&x| (x.start <= addr) && (x.end > addr))
.cloned()
}
}