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
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::fmt;
use std::io::{self, BufRead, BufReader, Read};

use chrono::{DateTime, FixedOffset, Utc};
use lazy_static::lazy_static;
use regex::Regex;
#[cfg(feature = "with_serde")]
use serde::{Serialize, Serializer};
use uuid::Uuid;

lazy_static! {
    static ref KEY_VALUE_RE: Regex = Regex::new(
        r#"(?x)
        ^\s*(.*?)\s*:\s*(.*?)\s*$
    "#
    )
    .unwrap();
    static ref THREAD_RE: Regex = Regex::new(
        r#"(?x)
        ^Thread\ ([0-9]+)(\ Crashed)?:\s*(.+?)?\s*$
    "#
    )
    .unwrap();
    static ref THREAD_NAME_RE: Regex = Regex::new(
        r#"(?x)
        ^Thread\ ([0-9]+)\ name:\s*(.+?)
        (?:\s+Dispatch\ queue:\s*(.*?))?\s*$
    "#
    )
    .unwrap();
    static ref THREAD_STATE_RE: Regex = Regex::new(
        r#"(?x)
        ^Thread\ ([0-9]+)\ crashed\ with\ .*?\ Thread\ State:\s*$
    "#
    )
    .unwrap();
    static ref REGISTER_RE: Regex = Regex::new(
        r#"(?x)
        \s*
        ([a-z0-9]+):\s+
        (0x[0-9a-fA-F]+)\s*
    "#
    )
    .unwrap();
    static ref FRAME_RE: Regex = Regex::new(
        r#"(?x)
        ^
            [0-9]+ \s+
            (\S+) \s+
            (0x[0-9a-fA-F]+)\s+
            (.*?)
            (?:\ (?:\+\ [0-9]+|\((.*?):([0-9]+)\)))?
            \s*
        $
    "#
    )
    .unwrap();
    static ref BINARY_IMAGE_RE: Regex = Regex::new(
        r#"(?x)
        ^
            \s*
            (0x[0-9a-fA-F]+) \s*
            -
            \s*
            (0x[0-9a-fA-F]+) \s+
            \+?(.+)\s+
            (\S+?)\s+
            (?:\(([^)]+?)\))?\s+
            <([^>]+?)>\s+
            (.*?)
        $
    "#
    )
    .unwrap();
}

/// A newtype for addresses.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Addr(u64);

#[cfg(feature = "with_serde")]
impl Serialize for Addr {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        format!("0x{:x}", self.0).serialize(serializer)
    }
}

/// Holds a parsed apple crash report.
#[derive(Debug, Default)]
#[cfg_attr(feature = "with_serde", derive(Serialize))]
pub struct AppleCrashReport {
    /// The unique crash ID.
    pub incident_identifier: Uuid,
    /// The timestamp of the crash.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub timestamp: Option<DateTime<Utc>>,
    /// The architecture of the crash (might require further parsing)
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub code_type: Option<String>,
    /// The path to the application.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub path: Option<String>,
    /// Optional application specific crash information as string.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub application_specific_information: Option<String>,
    /// Optional syslog info
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub filtered_syslog: Option<String>,
    /// The internal report version.
    pub report_version: u32,
    /// Extra metdata.
    pub metadata: BTreeMap<String, String>,
    /// A list of threads.
    pub threads: Vec<Thread>,
    /// A list of referenced binary images.
    pub binary_images: Vec<BinaryImage>,
}

/// A single binary image in the crash.
#[derive(Debug)]
#[cfg_attr(feature = "with_serde", derive(Serialize))]
pub struct BinaryImage {
    /// The address of the image,
    pub addr: Addr,
    /// The size of the image,
    pub size: u64,
    /// The unique ID of the image,
    pub uuid: Uuid,
    /// The architecture of the image,
    pub arch: String,
    /// The version of the image if available. This might require further parsing.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub version: Option<String>,
    /// The short name of the image.
    pub name: String,
    /// The full path of the image.
    pub path: String,
}

/// Represents a single frame.
#[derive(Debug)]
#[cfg_attr(feature = "with_serde", derive(Serialize))]
pub struct Frame {
    /// The module of the frame.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub module: Option<String>,
    /// The symbol of the frame if available.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub symbol: Option<String>,
    /// The filename of the frame if available.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub filename: Option<String>,
    /// The line number of the frame if available.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub lineno: Option<u32>,
    //// The instruction address of the frame.
    pub instruction_addr: Addr,
}

/// A single thread in the crash.
#[derive(Debug)]
#[cfg_attr(feature = "with_serde", derive(Serialize))]
pub struct Thread {
    /// The ID (index) of the thread.
    pub id: u64,
    /// The name of the thread if available.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub name: Option<String>,
    /// The name of the dispatch queue
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub dispatch_queue: Option<String>,
    /// `true` if this thread crashed.
    pub crashed: bool,
    /// The list of frames
    pub frames: Vec<Frame>,
    /// A dump of all the registers of the thread if available.
    #[cfg_attr(feature = "with_serde", serde(skip_serializing_if = "Option::is_none"))]
    pub registers: Option<BTreeMap<String, Addr>>,
}

enum ParsingState {
    Root,
    Thread,
    BinaryImages,
    ThreadState,
    FilteredSyslog,
    ApplicationSpecificInformation,
}

/// Represents a parsing error.
#[derive(Debug)]
pub enum ParseError {
    Io(io::Error),
    InvalidIncidentIdentifier(uuid::parser::ParseError),
    InvalidReportVersion(std::num::ParseIntError),
    InvalidTimestamp(chrono::ParseError),
}

impl std::error::Error for ParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            ParseError::Io(ref err) => Some(err),
            ParseError::InvalidIncidentIdentifier(ref err) => Some(err),
            ParseError::InvalidReportVersion(ref err) => Some(err),
            ParseError::InvalidTimestamp(ref err) => Some(err),
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ParseError::Io(..) => write!(f, "io error during parsing"),
            ParseError::InvalidIncidentIdentifier(..) => write!(f, "invalid incident identifier"),
            ParseError::InvalidReportVersion(..) => write!(f, "invalid report version"),
            ParseError::InvalidTimestamp(..) => write!(f, "invalid timestamp"),
        }
    }
}

impl std::str::FromStr for AppleCrashReport {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<AppleCrashReport, ParseError> {
        AppleCrashReport::from_line_iter(s.lines().map(|x| Ok(Cow::Borrowed(x))))
    }
}

impl AppleCrashReport {
    /// Consumes a reader and parses it.
    pub fn from_reader<R: Read>(r: R) -> Result<AppleCrashReport, ParseError> {
        let reader = BufReader::new(r);
        AppleCrashReport::from_line_iter(reader.lines().map(|x| x.map(Cow::Owned)))
    }

    #[allow(clippy::cyclomatic_complexity)]
    fn from_line_iter<'a, I: Iterator<Item = Result<Cow<'a, str>, io::Error>>>(
        iter: I,
    ) -> Result<AppleCrashReport, ParseError> {
        let mut state = ParsingState::Root;
        let mut thread = None;
        let mut thread_names = BTreeMap::new();
        let mut registers = BTreeMap::new();
        let mut application_specific_information = String::new();
        let mut filtered_syslog = String::new();

        let mut rv = AppleCrashReport::default();

        for line in iter {
            let line = line.map_err(ParseError::Io)?;
            let line = line.trim_end();

            if line.starts_with("Binary Images:") {
                state = ParsingState::BinaryImages;
                continue;
            } else if line.starts_with("Application Specific Information:") {
                state = ParsingState::ApplicationSpecificInformation;
                continue;
            } else if line.starts_with("Filtered syslog:") {
                state = ParsingState::FilteredSyslog;
                continue;
            } else if THREAD_STATE_RE.is_match(&line) {
                state = ParsingState::ThreadState;
                continue;
            } else if let Some(caps) = THREAD_RE.captures(&line) {
                if let Some(thread) = thread.take() {
                    rv.threads.push(thread);
                }
                thread = Some(Thread {
                    id: caps[1].parse().unwrap(),
                    name: caps.get(3).map(|m| m.as_str().to_string()),
                    dispatch_queue: None,
                    frames: vec![],
                    crashed: caps.get(2).is_some(),
                    registers: None,
                });
                state = ParsingState::Thread;
                continue;
            } else if let Some(caps) = THREAD_NAME_RE.captures(&line) {
                thread_names.insert(
                    caps[1].parse::<u64>().unwrap(),
                    (
                        caps[2].to_string(),
                        caps.get(3).map(|x| x.as_str().to_string()),
                    ),
                );
                state = ParsingState::Root;
                continue;
            }

            state = match state {
                ParsingState::Root => {
                    if let Some(caps) = KEY_VALUE_RE.captures(&line) {
                        match &caps[1] {
                            "Incident Identifier" => {
                                rv.incident_identifier = caps[2]
                                    .parse()
                                    .map_err(ParseError::InvalidIncidentIdentifier)?;
                            }
                            "Report Version" => {
                                rv.report_version =
                                    caps[2].parse().map_err(ParseError::InvalidReportVersion)?;
                            }
                            "Path" => {
                                rv.path = Some(caps[2].to_string());
                            }
                            "Code Type" => {
                                rv.code_type = Some(caps[2].to_string());
                            }
                            "Date/Time" => {
                                let timestamp = DateTime::<FixedOffset>::parse_from_str(
                                    &caps[2],
                                    "%Y-%m-%d %H:%M:%S %z",
                                )
                                .map_err(ParseError::InvalidTimestamp)?;
                                rv.timestamp = Some(timestamp.with_timezone(&Utc));
                            }
                            "Crashed Thread" => {}
                            _ => {
                                rv.metadata.insert(caps[1].to_string(), caps[2].to_string());
                            }
                        }
                    }
                    ParsingState::Root
                }
                ParsingState::ThreadState => {
                    if line.is_empty() {
                        ParsingState::Root
                    } else {
                        for caps in REGISTER_RE.captures_iter(&line) {
                            registers.insert(
                                caps[1].to_string(),
                                Addr(u64::from_str_radix(&caps[2][2..], 16).unwrap()),
                            );
                        }
                        ParsingState::ThreadState
                    }
                }
                ParsingState::Thread => {
                    if let Some(caps) = FRAME_RE.captures(&line) {
                        thread.as_mut().unwrap().frames.push(Frame {
                            module: if &caps[1] == "???" {
                                None
                            } else {
                                Some(caps[1].to_string())
                            },
                            symbol: caps.get(3).and_then(|x| {
                                if x.as_str().starts_with("0x")
                                    && u64::from_str_radix(&x.as_str()[2..], 16).is_ok()
                                {
                                    None
                                } else {
                                    Some(x.as_str().to_string())
                                }
                            }),
                            filename: caps.get(4).map(|x| x.as_str().to_string()),
                            lineno: caps.get(5).map(|x| x.as_str().parse().unwrap()),
                            instruction_addr: Addr(u64::from_str_radix(&caps[2][2..], 16).unwrap()),
                        });
                        ParsingState::Thread
                    } else {
                        ParsingState::Root
                    }
                }
                ParsingState::BinaryImages => {
                    if line.is_empty() {
                        ParsingState::BinaryImages
                    } else if let Some(caps) = BINARY_IMAGE_RE.captures(&line) {
                        let addr = u64::from_str_radix(&caps[1][2..], 16).unwrap();
                        rv.binary_images.push(BinaryImage {
                            addr: Addr(addr),
                            size: u64::from_str_radix(&caps[2][2..], 16).unwrap() - addr,
                            uuid: caps[6]
                                .parse()
                                .map_err(ParseError::InvalidIncidentIdentifier)?,
                            arch: caps[4].to_string(),
                            version: caps.get(5).map(|x| x.as_str().to_string()),
                            name: caps[3].to_string(),
                            path: caps[7].to_string(),
                        });
                        ParsingState::BinaryImages
                    } else {
                        ParsingState::Root
                    }
                }
                ParsingState::ApplicationSpecificInformation => {
                    if !application_specific_information.is_empty() {
                        application_specific_information.push('\n');
                    }
                    application_specific_information.push_str(line);
                    ParsingState::ApplicationSpecificInformation
                }
                ParsingState::FilteredSyslog => {
                    if !filtered_syslog.is_empty() {
                        filtered_syslog.push('\n');
                    }
                    filtered_syslog.push_str(line);
                    ParsingState::FilteredSyslog
                }
            }
        }

        if let Some(thread) = thread.take() {
            rv.threads.push(thread);
        }

        for thread in rv.threads.iter_mut() {
            if let Some((name, dispatch_queue)) = thread_names.remove(&thread.id) {
                thread.name = Some(name);
                thread.dispatch_queue = dispatch_queue;
            }
        }

        if !registers.is_empty() {
            for thread in rv.threads.iter_mut() {
                if thread.crashed {
                    thread.registers = Some(registers);
                    break;
                }
            }
        }

        if !application_specific_information.is_empty() {
            if application_specific_information.ends_with('\n') {
                application_specific_information
                    .truncate(application_specific_information.len() - 1);
            }
            rv.application_specific_information = Some(application_specific_information);
        }
        if !filtered_syslog.is_empty() {
            if filtered_syslog.ends_with('\n') {
                filtered_syslog.truncate(filtered_syslog.len() - 1);
            }
            rv.filtered_syslog = Some(filtered_syslog);
        }

        Ok(rv)
    }
}