udbg 0.3.1

cross-platform library for binary debugging and memory hacking
Documentation
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//!
//! Utilities for accessing symbols in pdb file
//!

use anyhow::Context;
use pdb::{FallibleIterator, ItemIter, MemberType, SymbolData, TypeData, TypeIndex, PDB};

use spin::Mutex;
use std::sync::Arc;
use std::{fs::File, io::ErrorKind};

use crate::{pe, prelude::*, shell::udbg_ui};

fn to_field_info(m: MemberType) -> FieldInfo {
    FieldInfo {
        offset: m.offset as u32,
        type_id: m.field_type.0,
        name: m.name.to_string().into(),
    }
}

pub struct PdbFile {
    // use box to ensure the reference in `finder` unchange
    db: Box<PDB<'static, File>>,
    ti: pdb::TypeInformation<'static>,
    finder: pdb::ItemFinder<'static, pdb::TypeIndex>,
}

impl PdbFile {
    pub fn load(path: &str, pe: Option<&pe::PeHelper>) -> anyhow::Result<Self> {
        let mut db = Box::new(
            pdb::PDB::open(File::open(path).with_context(|| format!("open file {path}"))?)
                .with_context(|| format!("open pdb {path}"))?,
        );
        let pi = db.pdb_information().context("PDBInformation")?;
        if let Some(pe) = pe {
            if !pe
                .debug_data
                .and_then(|d| d.codeview_pdb70_debug_info)
                .map(|d| unsafe {
                    let (g1, g2, g3, g4): (u32, u16, u16, [u8; 8]) =
                        core::mem::transmute(d.signature);
                    let (d1, d2, d3, d4) = pi.guid.as_fields();
                    g1 == d1 && g2 == d2 && g3 == d3 && &g4 == d4
                })
                .unwrap_or(true)
            {
                anyhow::bail!("Signature not matched");
            }
        }

        let ti = db.type_information().context("ItemInformation")?;
        let pti: *const _ = &ti;
        unsafe {
            let finder = pti.as_ref().unwrap().finder();
            // let ii = db.id_information().context("IdInformation")?;
            Ok(Self {
                db,
                ti,
                finder: core::mem::transmute(finder),
            })
        }
    }

    pub fn global(&mut self) -> anyhow::Result<SymbolMap> {
        let pdb = &mut self.db;
        let address_map = pdb.address_map().context("address_map failed")?;
        let dbi = pdb
            .debug_information()
            .context("debug_information failed")?;
        let mut modules = dbi.modules().context("get modules failed")?;

        let mut result = SymbolMap::default();
        let mut push_symbol = |sym: SymbolData| {
            match sym {
                SymbolData::Public(p) => {
                    let rva = p.offset.to_rva(&address_map).unwrap_or_default().0;
                    // println!("0x{:x} {}", rva, p.name);
                    let mut flags = SymbolFlags::NONE;
                    if p.function {
                        flags |= SymbolFlags::FUNCTION;
                    }
                    result.entry(rva as usize).or_insert_with(|| Symbol {
                        offset: rva,
                        name: p.name.to_string().into(),
                        type_id: 0,
                        flags: flags.bits(),
                        len: SYM_NOLEN,
                    });
                }
                SymbolData::Procedure(p) => {
                    let rva = p.offset.to_rva(&address_map).unwrap_or_default().0;
                    // println!("Proc: {} {} {:x} {:x}", p.name.to_string(), p.global, rva, p.len);
                    result.entry(rva as usize).or_insert_with(|| Symbol {
                        offset: rva,
                        name: p.name.to_string().into(),
                        type_id: p.type_index.0,
                        flags: SymbolFlags::FUNCTION.bits(),
                        len: p.len,
                    });
                }
                SymbolData::Data(p) => {
                    // if let Err(_) = std::panic::catch_unwind(|| {
                    let rva = p.offset.to_rva(&address_map).unwrap_or_default().0;
                    result.entry(rva as usize).or_insert_with(|| Symbol {
                        offset: rva,
                        name: p.name.to_string().into(),
                        flags: SymbolFlags::NONE.bits(),
                        len: 0,
                        type_id: p.type_index.0,
                    });
                    // }) { error!("push_symbol error"); }
                }
                _ => {}
            }
        };
        while let Ok(Some(module)) = modules.next() {
            if let Ok(Some(i)) = pdb.module_info(&module) {
                if let Ok(mut syms) = i.symbols() {
                    while let Ok(Some(symbol)) = syms.next() {
                        symbol.parse().map(|sym| push_symbol(sym)).ok();
                    }
                } else {
                    error!("get symbols failed: {}", module.module_name());
                }
            }
            // println!("module name: {}, object file name: {}", module.module_name(), module.object_file_name());
        }

        let symbol_table = pdb.global_symbols().context("global_symbols failed")?;
        let mut symbols = symbol_table.iter();
        while let Ok(Some(symbol)) = symbols.next() {
            symbol.parse().map(|sym| push_symbol(sym)).ok();
        }
        Ok(result)
    }

    pub fn td2ti(&mut self, id: u32, data: TypeData, name: Option<&str>) -> Option<TypeInfo> {
        let (tn, kind) = match data {
            TypeData::Procedure(p) => (
                "".into(),
                TypeKind::Proc {
                    args_tid: p.argument_list.0,
                    return_tid: p.return_type.map(|i| i.0).unwrap_or_default(),
                },
            ),
            TypeData::Class(cls) => {
                // if cls.fields.is_none() { return None; }
                (
                    cls.name.to_string(),
                    TypeKind::Class {
                        fields: cls.fields.map(|x| x.0),
                        vtable: cls.vtable_shape.map(|x| x.0),
                        derive: cls.derived_from.map(|x| x.0),
                        size: cls.size as _,
                    },
                )
            }
            TypeData::Nested(n) => (n.name.to_string(), TypeKind::Nested),
            // TypeData::BaseClass(cls) => {
            //     ("BaseClass".into(), TypeKind::Class {tid: cls.base_class.0.into(), vtable: None, size: 0})
            // }
            TypeData::Array(a) => (
                "".into(),
                TypeKind::Array {
                    tid: a.element_type.0,
                    dimensions: a.dimensions.clone(),
                },
            ),
            TypeData::Bitfield(b) => (
                "".into(),
                TypeKind::Bitfield {
                    tid: b.underlying_type.0,
                    len: b.length,
                    pos: b.position,
                },
            ),
            TypeData::Union(cls) => (cls.name.to_string(), TypeKind::Union),
            // TypeData::Enumerate(cls) => {
            //     (cls.name.to_string(), TypeKind::Enum)
            // }
            TypeData::Enumeration(cls) => (cls.name.to_string(), TypeKind::Enum),
            TypeData::Pointer(pt) => (
                "".into(),
                TypeKind::Pointer {
                    tid: pt.underlying_type.0,
                },
            ),
            TypeData::Primitive(p) => (
                format!("{:?}", p.kind).into(),
                TypeKind::Primitive {
                    pointer: p.indirection.is_some(),
                },
            ),
            TypeData::Modifier(m) => {
                if name.is_none() {
                    let id = m.underlying_type.0;
                    let x = self.find_type(id).and_then(|x| x.parse().ok())?;
                    return self.td2ti(id, x, None);
                } else {
                    return None;
                }
            }
            _ => return None,
        };
        if let Some(pattern) = name.and_then(|n| glob::Pattern::new(n).ok()) {
            let options = glob::MatchOptions {
                case_sensitive: true,
                ..Default::default()
            };
            if !pattern.matches_with(&tn, options) {
                return None;
            }
        }
        Some(TypeInfo {
            id,
            name: tn.into(),
            kind,
        })
    }

    pub fn get_type(&mut self, id: u32) -> Option<TypeInfo> {
        let x = self.find_type(id)?.parse().ok()?;
        self.td2ti(id, x, None)
    }

    pub fn find_type_info(&mut self, name: &str) -> Vec<TypeInfo> {
        let mut type_iter: ItemIter<'static, TypeIndex> =
            unsafe { core::mem::transmute(self.ti.iter()) };
        let mut cache = vec![];
        while let Some(typ) = type_iter.next().ok().flatten() {
            self.finder.update(&type_iter);
            let id = typ.index().0;
            if let Some(r) = typ
                .parse()
                .ok()
                .and_then(|ty| self.td2ti(id, ty, Some(name)))
            {
                let canbreak = match &r.kind {
                    TypeKind::Class { fields, .. } => fields.is_some(),
                    _ => true,
                };
                cache.push(r);
                if canbreak {
                    break;
                }
            }
        }
        cache
    }

    pub fn find_type(&mut self, id: u32) -> Option<pdb::Item<'static, TypeIndex>> {
        self.finder.find(id.into()).ok().or_else(|| {
            let mut type_iter: ItemIter<'static, TypeIndex> =
                unsafe { core::mem::transmute(self.ti.iter()) };
            while let Some(item) = type_iter.next().ok()? {
                self.finder.update(&type_iter);
                if item.index().0 == id {
                    return Some(item);
                }
            }
            None
        })
    }

    pub fn field_list(&mut self, type_id: u32) -> Option<pdb::FieldList> {
        let id = match self.find_type(type_id)?.parse().ok()? {
            TypeData::Class(cls) => cls.fields,
            TypeData::Enumeration(cls) => Some(cls.fields),
            TypeData::FieldList(data) => return Some(data),
            _ => None,
        };
        match self.find_type(id?.0)?.parse().ok()? {
            TypeData::FieldList(data) => Some(data),
            _ => {
                // println!("find field list or parse failed");
                None
            }
        }
    }

    pub fn get_field_list(&mut self, type_id: u32) -> Vec<FieldInfo> {
        let mut result = vec![];
        if let Some(fl) = self.field_list(type_id) {
            for field in fl.fields {
                match field {
                    TypeData::Member(m) => {
                        result.push(to_field_info(m));
                    }
                    TypeData::Enumerate(cls) => {
                        result.push(FieldInfo {
                            type_id: 0,
                            offset: 0,
                            name: cls.name.to_string().into(),
                        });
                    }
                    _ => continue,
                }
            }
        }
        result
    }

    pub fn find_field(&mut self, id: u32, name: &str) -> Result<FieldInfo, String> {
        for field in self.field_list(id).ok_or("no field list")?.fields {
            match field {
                TypeData::Member(m) => {
                    if m.name.to_string() == name {
                        return Ok(to_field_info(m));
                    }
                }
                _ => continue,
            }
        }
        Err("".into())
    }

    pub fn get_field(&mut self, id: u32, index: usize) -> Option<FieldInfo> {
        match self.field_list(id)?.fields.get(index)? {
            TypeData::Member(m) => Some(to_field_info(m.clone())),
            _ => None,
        }
    }
}

pub struct PDBData {
    pub file: Mutex<PdbFile>,
    pub path: Arc<str>,
    pub global: Mutex<Option<Arc<SymbolMap>>>,
}

impl PDBData {
    pub fn load(path: &str, pe: Option<&pe::PeHelper>) -> anyhow::Result<PDBData> {
        Ok(Self {
            file: PdbFile::load(path, pe)?.into(),
            path: path.into(),
            global: None.into(),
        })
    }
}

impl SymbolFile for PDBData {
    fn path(&self) -> &str {
        self.path.as_ref()
    }

    fn find_type(&self, name: &str) -> Vec<TypeInfo> {
        self.file.lock().find_type_info(name)
    }

    fn get_type(&self, tid: u32) -> Option<TypeInfo> {
        self.file.lock().get_type(tid)
    }

    fn get_field_list(&self, tid: u32) -> Vec<FieldInfo> {
        self.file.lock().get_field_list(tid)
    }

    fn get_field(&self, tid: u32, index: usize) -> Option<FieldInfo> {
        self.file.lock().get_field(tid, index)
    }

    fn find_field(&self, id: u32, name: &str) -> Option<FieldInfo> {
        self.file.lock().find_field(id, name).ok()
    }

    fn global(&self) -> anyhow::Result<Arc<SymbolMap>> {
        let result = self.global.lock().clone();
        match result {
            Some(r) => Ok(r.clone()),
            None => {
                let r = Arc::new(self.file.lock().global()?);
                *self.global.lock() = r.clone().into();
                Ok(r)
            }
        }
    }
}

impl pe::PeHelper<'_> {
    pub fn find_pdb(&self, path: &str) -> anyhow::Result<Arc<PDBData>> {
        use std::path::Path;

        let fullpath = Path::new(path);
        let pdbpath = self.get_pdb_path().and_then(|p| p.to_str().ok());
        let mut paths = vec![];

        if let Some(pdbpath) = pdbpath {
            let pdbpath = Path::new(pdbpath);
            let pdbname = pdbpath.file_name().context("pdbname")?;
            // 1. the pdb's full path
            if pdbpath.is_absolute() {
                paths.push(pdbpath.to_path_buf());
            }
            // 2. dir(module) + pdb's name
            paths.push(fullpath.with_file_name(pdbname));
            // 3. the cached pdb path
            udbg_ui().base().symcache.as_ref().map(|cache| {
                if let Some(pdb_sig) = self.get_pdb_signature() {
                    paths.push(cache.join(pdbname).join(pdb_sig).join(pdbname));
                }
            });
        }
        // 4. the same pdb path to dll
        paths.push(fullpath.with_extension("pdb"));

        let mut err = None;
        for p in paths.iter() {
            if p.exists() {
                match PDBData::load(&p.to_string_lossy(), self.into()) {
                    Ok(pdb) => return Ok(pdb.into()),
                    Err(e) => err = Some(e),
                }
            }
        }

        Err(err
            .map(Into::into)
            .unwrap_or_else(|| std::io::Error::from(ErrorKind::NotFound).into()))
    }
}

impl SymbolsData {
    pub fn load_from_pdb(&self, path: &str) -> anyhow::Result<SymbolMap> {
        let mut pdb = PdbFile::load(path, None)?;
        pdb.global()
    }
}