gdb_command/
error.rs

1//! A custom GdbCommand error.
2use core::fmt;
3use core::result;
4use std::{error, io};
5
6#[derive(Debug)]
7/// A custom GdbCommand error
8pub enum Error {
9    /// Gdb output parsing error
10    ParseOutput(String),
11    /// No executable/core found to run under gdb.
12    NoFile(String),
13    /// An IO based error
14    IO(io::Error),
15    /// Error parsing stack trace
16    StacktraceParse(String),
17    /// Error parsing siginfo
18    SiginfoParse(String),
19    /// Error parsing mapped files
20    MappedFilesParse(String),
21    /// Error parsing memory object.
22    MemoryObjectParse(String),
23    /// An ParseInt based error
24    IntParse(std::num::ParseIntError),
25    /// GDB launch error.
26    Gdb(String),
27}
28
29impl error::Error for Error {
30    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
31        match *self {
32            Error::IO(ref io) => Some(io),
33            Error::IntParse(ref pr) => Some(pr),
34            Error::ParseOutput(_) => None,
35            Error::NoFile(_) => None,
36            Error::StacktraceParse(_) => None,
37            Error::SiginfoParse(_) => None,
38            Error::MappedFilesParse(_) => None,
39            Error::MemoryObjectParse(_) => None,
40            Error::Gdb(_) => None,
41        }
42    }
43}
44
45impl From<io::Error> for Error {
46    fn from(err: io::Error) -> Error {
47        Error::IO(err)
48    }
49}
50
51impl From<std::num::ParseIntError> for Error {
52    fn from(err: std::num::ParseIntError) -> Error {
53        Error::IntParse(err)
54    }
55}
56
57impl fmt::Display for Error {
58    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
59        match *self {
60            Error::IO(ref err) => write!(fmt, "{err}"),
61            Error::IntParse(ref err) => write!(fmt, "{err}"),
62            Error::ParseOutput(ref msg) => write!(fmt, "Gdb parsing output error: {msg}"),
63            Error::NoFile(ref msg) => write!(fmt, "File not found: {msg}"),
64            Error::StacktraceParse(ref msg) => write!(fmt, "Error parsing stack trace: {msg}"),
65            Error::SiginfoParse(ref msg) => write!(fmt, "Error parsing siginfo: {msg}"),
66            Error::MappedFilesParse(ref msg) => write!(fmt, "Error parsing mapped files: {msg}"),
67            Error::MemoryObjectParse(ref msg) => {
68                write!(fmt, "Error parsing memory object: {msg}")
69            }
70            Error::Gdb(ref msg) => write!(fmt, "Failed to launch GDB: {msg}"),
71        }
72    }
73}
74
75/// GdbCommand Result
76pub type Result<T> = result::Result<T, Error>;