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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use std::fmt;
use std::str::FromStr;

use uuid::Uuid;

use symbolic_common::{Arch, Error, ErrorKind, ObjectKind, Result};

use object::{FatObject, Object, ObjectId};

pub enum BreakpadRecord<'input> {
    Module(BreakpadModuleRecord<'input>),
    File(BreakpadFileRecord<'input>),
    Function(BreakpadFuncRecord<'input>),
    Line(BreakpadLineRecord),
    Public(BreakpadPublicRecord<'input>),
    Info(&'input [u8]),
    Stack,
}

pub struct BreakpadModuleRecord<'input> {
    pub arch: Arch,
    pub uuid: Uuid,
    pub name: &'input [u8],
}

impl<'input> fmt::Debug for BreakpadModuleRecord<'input> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BreakpadModuleRecord")
            .field("arch", &self.arch)
            .field("uuid", &self.uuid)
            .field("name", &String::from_utf8_lossy(self.name))
            .finish()
    }
}

pub struct BreakpadFileRecord<'input> {
    pub id: u64,
    pub name: &'input [u8],
}

impl<'input> fmt::Debug for BreakpadFileRecord<'input> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BreakpadFileRecord")
            .field("id", &self.id)
            .field("name", &String::from_utf8_lossy(self.name))
            .finish()
    }
}

#[derive(Debug)]
pub struct BreakpadLineRecord {
    pub address: u64,
    pub line: u64,
    pub file_id: u64,
}

pub struct BreakpadFuncRecord<'input> {
    pub address: u64,
    pub size: u64,
    pub name: &'input [u8],
    pub lines: Vec<BreakpadLineRecord>,
}

impl<'input> fmt::Debug for BreakpadFuncRecord<'input> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BreakpadFuncRecord")
            .field("address", &self.address)
            .field("size", &self.size)
            .field("name", &String::from_utf8_lossy(self.name))
            .field("lines", &self.lines)
            .finish()
    }
}

pub struct BreakpadPublicRecord<'input> {
    pub address: u64,
    pub size: u64,
    pub name: &'input [u8],
}

impl<'input> fmt::Debug for BreakpadPublicRecord<'input> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("BreakpadPublicRecord")
            .field("address", &self.address)
            .field("size", &self.size)
            .field("name", &String::from_utf8_lossy(self.name))
            .finish()
    }
}

/// Provides access to information in a breakpad file.
#[derive(Debug)]
pub(crate) struct BreakpadSym {
    id: ObjectId,
    arch: Arch,
}

impl BreakpadSym {
    /// Parses a breakpad file header.
    ///
    /// Example:
    /// ```
    /// MODULE mac x86_64 13DA2547B1D53AF99F55ED66AF0C7AF70 Electron Framework
    /// ```
    pub fn parse(bytes: &[u8]) -> Result<BreakpadSym> {
        let mut words = bytes.splitn(5, |b| *b == b' ');

        match words.next() {
            Some(b"MODULE") => (),
            _ => return Err(ErrorKind::BadBreakpadSym("Invalid breakpad magic").into()),
        };

        // Operating system not needed
        words.next();

        let arch = match words.next() {
            Some(word) => String::from_utf8_lossy(word),
            None => return Err(ErrorKind::BadBreakpadSym("Missing breakpad arch").into()),
        };

        let uuid_hex = match words.next() {
            Some(word) => String::from_utf8_lossy(word),
            None => return Err(ErrorKind::BadBreakpadSym("Missing breakpad uuid").into()),
        };

        let id = match ObjectId::parse(&uuid_hex[0..33]) {
            Ok(uuid) => uuid,
            Err(_) => return Err(ErrorKind::Parse("Invalid breakpad uuid").into()),
        };

        Ok(BreakpadSym {
            id: id,
            arch: Arch::from_breakpad(arch.as_ref()),
        })
    }

    pub fn id(&self) -> ObjectId {
        self.id
    }

    pub fn arch(&self) -> Arch {
        self.arch
    }
}

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
enum IterState {
    Started,
    Reading,
    Function,
}

pub struct BreakpadRecords<'data> {
    lines: Box<Iterator<Item = &'data [u8]> + 'data>,
    state: IterState,
}

impl<'data> BreakpadRecords<'data> {
    fn from_bytes(bytes: &'data [u8]) -> BreakpadRecords<'data> {
        BreakpadRecords {
            lines: Box::new(bytes.split(|b| *b == b'\n')),
            state: IterState::Started,
        }
    }

    fn parse(&mut self, line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
        let mut words = line.splitn(2, |b| *b == b' ');
        let magic = words.next().unwrap_or(b"");
        let record = words.next().unwrap_or(b"");

        match magic {
            b"MODULE" => {
                if self.state != IterState::Started {
                    return Err(ErrorKind::BadBreakpadSym("unexpected module header").into());
                }

                self.state = IterState::Reading;
                parse_module(record)
            }
            b"FILE" => {
                self.state = IterState::Reading;
                parse_file(record)
            }
            b"FUNC" => {
                self.state = IterState::Function;
                parse_func(record)
            }
            b"STACK" => {
                self.state = IterState::Reading;
                parse_stack(record)
            }
            b"PUBLIC" => {
                self.state = IterState::Reading;
                parse_public(record)
            }
            b"INFO" => {
                self.state = IterState::Reading;
                parse_info(record)
            }
            _ => {
                if self.state == IterState::Function {
                    // Pass the whole line down as there is no magic
                    parse_line(line)
                } else {
                    // No known magic and we don't expect a line record
                    Err(ErrorKind::BadBreakpadSym("unexpected line record").into())
                }
            }
        }
    }
}

impl<'data> Iterator for BreakpadRecords<'data> {
    type Item = Result<BreakpadRecord<'data>>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(next) = self.lines.next() {
            let mut len = next.len();
            while len > 0 && next[len - 1] == b'\r' {
                len -= 1;
            }

            if len > 0 {
                return Some(self.parse(&next[0..len]));
            }
        }

        None
    }
}

pub trait BreakpadData {
    fn has_breakpad_data(&self) -> bool;
    fn breakpad_records<'input>(&'input self) -> BreakpadRecords<'input>;
}

impl<'data> BreakpadData for Object<'data> {
    fn has_breakpad_data(&self) -> bool {
        self.kind() == ObjectKind::Breakpad
    }

    fn breakpad_records<'input>(&'input self) -> BreakpadRecords<'input> {
        BreakpadRecords::from_bytes(self.as_bytes())
    }
}

impl<'data> BreakpadData for FatObject<'data> {
    fn has_breakpad_data(&self) -> bool {
        self.kind() == ObjectKind::Breakpad
    }

    fn breakpad_records<'input>(&'input self) -> BreakpadRecords<'input> {
        BreakpadRecords::from_bytes(self.as_bytes())
    }
}

/// Parses a breakpad MODULE record
///
/// Syntax: "MODULE operatingsystem architecture id name"
/// Example: "MODULE Linux x86 D3096ED481217FD4C16B29CD9BC208BA0 firefox-bin"
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#module-records
fn parse_module(line: &[u8]) -> Result<BreakpadRecord> {
    // if self.module.is_some() {
    //     return Err(ErrorKind::BadBreakpadSym("Multiple MODULE records not supported").into());
    // }

    let mut record = line.splitn(4, |b| *b == b' ');

    // Skip "os" field
    record.next();

    let arch = match record.next() {
        Some(word) => String::from_utf8_lossy(word),
        None => return Err(ErrorKind::BadBreakpadSym("missing module arch").into()),
    };

    let uuid_hex = match record.next() {
        Some(word) => String::from_utf8_lossy(word),
        None => return Err(ErrorKind::BadBreakpadSym("missing module uuid").into()),
    };

    let uuid = match Uuid::parse_str(&uuid_hex[0..32]) {
        Ok(uuid) => uuid,
        Err(_) => return Err(ErrorKind::Parse("invalid breakpad uuid").into()),
    };

    let name = match record.next() {
        Some(word) => word,
        None => return Err(ErrorKind::BadBreakpadSym("missing module name").into()),
    };

    Ok(BreakpadRecord::Module(BreakpadModuleRecord {
        name: name,
        arch: Arch::from_breakpad(&arch),
        uuid: uuid,
    }))
}

/// Parses a breakpad FILE record
///
/// Syntax: "FILE number name"
/// Example: "FILE 2 /home/jimb/mc/in/browser/app/nsBrowserApp.cpp"
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#file-records
fn parse_file<'data>(line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
    let mut record = line.splitn(2, |b| *b == b' ');

    let id = match record.next() {
        Some(text) => u64::from_str(&String::from_utf8_lossy(text))
            .map_err(|e| Error::with_chain(e, "invalid file id"))?,
        None => return Err(ErrorKind::Parse("missing file ID").into()),
    };

    let name = match record.next() {
        Some(text) => text,
        None => return Err(ErrorKind::Parse("missing file name").into()),
    };

    Ok(BreakpadRecord::File(BreakpadFileRecord {
        id: id,
        name: name,
    }))
}

/// Parses a breakpad FUNC record
///
/// Syntax: "FUNC [m] address size parameter_size name"
/// Example: "FUNC m c184 30 0 nsQueryInterfaceWithError::operator()(nsID const&, void**) const"
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#func-records
fn parse_func<'data>(line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
    // Strip the optional "m" parameter; it has no meaning to us
    let line = if line.starts_with(b"m ") {
        &line[2..]
    } else {
        line
    };
    let mut record = line.splitn(4, |b| *b == b' ');

    let address = match record.next() {
        Some(text) => u64::from_str_radix(&String::from_utf8_lossy(text), 16)
            .map_err(|e| Error::with_chain(e, "invalid function address"))?,
        None => return Err(ErrorKind::Parse("missing function address").into()),
    };

    let size = match record.next() {
        Some(text) => u64::from_str_radix(&String::from_utf8_lossy(text), 16)
            .map_err(|e| Error::with_chain(e, "invalid function size"))?,
        None => return Err(ErrorKind::Parse("missing function size").into()),
    };

    // Skip the parameter_size field
    record.next();

    let name = match record.next() {
        Some(text) => text,
        None => return Err(ErrorKind::Parse("missing function name").into()),
    };

    Ok(BreakpadRecord::Function(BreakpadFuncRecord {
        address: address,
        size: size,
        name: name,
        lines: vec![],
    }))
}

/// Parses a breakpad STACK record
///
/// Can either be a STACK WIN record...
/// Syntax: "STACK WIN type rva code_size prologue_size epilogue_size parameter_size saved_register_size local_size max_stack_size has_program_string program_string_OR_allocates_base_pointer"
/// Example: "STACK WIN 4 2170 14 1 0 0 0 0 0 1 $eip 4 + ^ = $esp $ebp 8 + = $ebp $ebp ^ ="
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#stack-win-records
///
/// ... or a STACK CFI record
/// Syntax: "STACK CFI INIT address size register1: expression1 register2: expression2 ..."
/// Example: "STACK CFI INIT 804c4b0 40 .cfa: $esp 4 + $eip: .cfa 4 - ^"
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#stack-cfi-records
fn parse_stack<'data>(_line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
    // Ignored
    Ok(BreakpadRecord::Stack)
}

/// Parses a breakpad PUBLIC record
///
/// Syntax: "PUBLIC [m] address parameter_size name"
/// Example: "PUBLIC m 2160 0 Public2_1"
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#public-records
fn parse_public<'data>(line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
    // Strip the optional "m" parameter; it has no meaning to us
    let line = if line.starts_with(b"m ") {
        &line[2..]
    } else {
        line
    };
    let mut record = line.splitn(4, |b| *b == b' ');

    let address = match record.next() {
        Some(text) => u64::from_str_radix(&String::from_utf8_lossy(text), 16)
            .map_err(|e| Error::with_chain(e, "invalid symbol address"))?,
        None => return Err(ErrorKind::Parse("missing symbol address").into()),
    };

    // Skip the parameter_size field
    record.next();

    let name = match record.next() {
        Some(text) => text,
        None => return Err(ErrorKind::Parse("missing function name").into()),
    };

    Ok(BreakpadRecord::Public(BreakpadPublicRecord {
        address: address,
        size: 0, // will be computed with the next PUBLIC record
        name: name,
    }))
}

/// Parses a breakpad INFO record
///
/// Syntax: "INFO text"
/// Example: "INFO CODE_ID C22813AC7D101E2FF2598697023E1F28"
/// no documentation available
fn parse_info<'data>(line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
    Ok(BreakpadRecord::Info(line))
}

/// Parses a breakpad line record (after funcs)
///
/// Syntax: "address size line filenum"
/// Example: "c184 7 59 4"
/// see https://github.com/google/breakpad/blob/master/docs/symbol_files.md#line-records
fn parse_line<'data>(line: &'data [u8]) -> Result<BreakpadRecord<'data>> {
    let mut record = line.splitn(4, |b| *b == b' ');

    let address = match record.next() {
        Some(text) => u64::from_str_radix(&String::from_utf8_lossy(text), 16)
            .map_err(|e| Error::with_chain(e, "invalid line address"))?,
        None => return Err(ErrorKind::Parse("Missing line address").into()),
    };

    // Skip the size field
    record.next();

    let line_number = match record.next() {
        Some(text) => u64::from_str(&String::from_utf8_lossy(text))
            .map_err(|e| Error::with_chain(e, "invalid line number"))?,
        None => return Err(ErrorKind::Parse("Missing line number").into()),
    };

    let file_id = match record.next() {
        Some(text) => u64::from_str(&String::from_utf8_lossy(text))
            .map_err(|e| Error::with_chain(e, "invalid line file id"))?,
        None => return Err(ErrorKind::Parse("missing line file id").into()),
    };

    Ok(BreakpadRecord::Line(BreakpadLineRecord {
        address: address,
        line: line_number,
        file_id: file_id,
    }))
}