1 use core::fmt::{self, Debug, Display}; 2 3 use crate::protocol::{PacketParseError, ResponseWriterError}; 4 use crate::util::managed_vec::CapacityError; 5 6 /// An error which may occur during a GDB debugging session. 7 #[derive(Debug)] 8 #[non_exhaustive] 9 pub enum GdbStubError<T, C> { 10 /// Connection Error while reading request. 11 ConnectionRead(C), 12 /// Connection Error while writing response. 13 ConnectionWrite(ResponseWriterError<C>), 14 /// Client nack'd the last packet, but `gdbstub` doesn't implement 15 /// re-transmission. 16 ClientSentNack, 17 /// GdbStub was not provided with a packet buffer in `no_std` mode 18 /// (missing call to `with_packet_buffer`) 19 MissingPacketBuffer, 20 /// Packet cannot fit in the provided packet buffer. 21 PacketBufferOverlow, 22 /// Could not parse the packet into a valid command. 23 PacketParse(PacketParseError), 24 /// GDB client sent an unexpected packet. 25 PacketUnexpected, 26 /// GDB client sent a packet with too much data for the given target. 27 TargetMismatch, 28 /// Target threw a fatal error. 29 TargetError(T), 30 /// Target didn't report any active threads. 31 NoActiveThreads, 32 /// Resuming with a signal is not implemented yet. Consider opening a PR? 33 ResumeWithSignalUnimplemented, 34 /// Internal - A non-fatal error occurred (with errno-style error code) 35 #[doc(hidden)] 36 NonFatalError(u8), 37 } 38 39 impl<T, C> From<ResponseWriterError<C>> for GdbStubError<T, C> { from(e: ResponseWriterError<C>) -> Self40 fn from(e: ResponseWriterError<C>) -> Self { 41 GdbStubError::ConnectionWrite(e) 42 } 43 } 44 45 impl<A, T, C> From<CapacityError<A>> for GdbStubError<T, C> { from(_: CapacityError<A>) -> Self46 fn from(_: CapacityError<A>) -> Self { 47 GdbStubError::PacketBufferOverlow 48 } 49 } 50 51 impl<T, C> Display for GdbStubError<T, C> 52 where 53 C: Debug, 54 T: Debug, 55 { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 57 use self::GdbStubError::*; 58 match self { 59 ConnectionRead(e) => write!(f, "Connection Error while reading request: {:?}", e), 60 ConnectionWrite(e) => write!(f, "Connection Error while writing response: {:?}", e), 61 ClientSentNack => write!(f, "Client nack'd the last packet, but `gdbstub` doesn't implement re-transmission."), 62 MissingPacketBuffer => write!(f, "GdbStub was not provided with a packet buffer in `no_std` mode (missing call to `with_packet_buffer`)"), 63 PacketBufferOverlow => write!(f, "Packet too big for provided buffer!"), 64 PacketParse(e) => write!(f, "Could not parse the packet into a valid command: {:?}", e), 65 PacketUnexpected => write!(f, "Client sent an unexpected packet."), 66 TargetMismatch => write!(f, "GDB client sent a packet with too much data for the given target."), 67 TargetError(e) => write!(f, "Target threw a fatal error: {:?}", e), 68 NoActiveThreads => write!(f, "Target didn't report any active threads."), 69 ResumeWithSignalUnimplemented => write!(f, "Resuming with a signal is not implemented yet. Consider opening a PR?"), 70 NonFatalError(_) => write!(f, "Internal - A non-fatal error occurred (with errno-style error code)"), 71 } 72 } 73 } 74 75 #[cfg(feature = "std")] 76 impl<T, C> std::error::Error for GdbStubError<T, C> 77 where 78 C: Debug, 79 T: Debug, 80 { 81 } 82