Skip to main content

kvmtrace/
error.rs

1//! The one error type the crate returns.
2
3use std::io;
4
5/// Everything that can go wrong while tracing.
6///
7/// Most variants carry enough context to say *which* guest address or
8/// ioctl failed, because "something failed" is useless at three in the
9/// morning when a 40 GB trace stops halfway.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum Error {
13    /// A KVM ioctl failed.
14    #[error("KVM ioctl {what} failed: {source}")]
15    Kvm {
16        /// Which operation was attempted, such as `KVM_GET_SREGS`.
17        what: &'static str,
18        /// The errno KVM returned.
19        source: kvm_ioctls::Error,
20    },
21    /// The guest-physical address is not backed by any memory region we know about
22    /// (it may be MMIO, or a region the VMM never registered).
23    #[error("guest-physical address {0:#x} is not backed by RAM")]
24    Unmapped(u64),
25    /// The instruction bytes at a linear address could not be fetched.
26    #[error("could not fetch instruction bytes at linear address {0:#x}")]
27    Fetch(u64),
28    /// Writing a trace file failed.
29    #[error("trace output: {0}")]
30    Io(#[from] io::Error),
31    /// A trace file is malformed.
32    #[error("malformed trace: {0}")]
33    Format(String),
34}
35
36impl Error {
37    /// Wraps a failed KVM ioctl, for use with `map_err`:
38    ///
39    /// ```no_run
40    /// # fn f(vcpu: &mut kvm_ioctls::VcpuFd) -> kvmtrace::Result<()> {
41    /// let exit = vcpu.run().map_err(kvmtrace::Error::kvm("KVM_RUN"))?;
42    /// # Ok(()) }
43    /// ```
44    pub fn kvm(what: &'static str) -> impl FnOnce(kvm_ioctls::Error) -> Error {
45        move |source| Error::Kvm { what, source }
46    }
47}
48
49/// Shorthand for `std::result::Result<T, kvmtrace::Error>`.
50pub type Result<T> = std::result::Result<T, Error>;