1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::fmt;

use crate::error;

/// `File` struct represents unit (segment) in proccess address space.
#[derive(Clone, Default, Debug)]
pub struct File {
    /// Start address of objfile
    pub start: u64,
    /// End address of objfile
    pub end: u64,
    /// Offset in file.
    pub offset: u64,
    /// Full path to binary module.
    pub name: String,
}

impl File {
    /// Constructs Mapped file from components.
    ///
    /// # Arguments
    ///
    /// * `start` - linear address of module load.
    ///
    /// * `end` - linear address of module end.
    ///
    /// * `offset` - offset in file.
    ///
    ///* `fname` - full path to binary module.
    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
        )
    }
}

/// `MappedFiles` all mapped files in process.
pub type MappedFiles = Vec<File>;

pub trait MappedFilesExt {
    /// Construct `MappedFiels` from string
    ///
    /// # Arguments
    ///
    /// * 'mapping' - gdb output string with mapped files
    fn from_gdb<T: AsRef<str>>(mapping: T) -> error::Result<MappedFiles>;

    /// Determine which file contains the address
    ///
    /// # Arguments
    ///
    /// * 'addr' - given address
    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);
        }

        // Split mapped files info in columns.
        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.to_string()
                )));
            }

            // Get index for module name. Different gdb versions have varying
            // number of fields.
            name_idx = name_idx.max(filevec.len() - 1);

            info.push(filevec);
        }

        let mut files = MappedFiles::new();

        // Parse and collect mapped files info.
        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 as u64) && (x.end > addr as u64))
            .cloned()
    }
}