summary refs log tree commit diff
path: root/sys_util/src/mmap.rs
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@chromium.org>2019-02-12 17:51:26 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-02-20 08:20:02 -0800
commitb4bd00fdad094b16c785b8ead9f92d68264f5fb4 (patch)
tree540ad033c789aa08a1ffed17e9c161df331600d2 /sys_util/src/mmap.rs
parent0373b9f154b5ec1894138b7c10ad495fcce7b64f (diff)
downloadcrosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.gz
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.bz2
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.lz
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.xz
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.tar.zst
crosvm-b4bd00fdad094b16c785b8ead9f92d68264f5fb4.zip
error: Print errors using Display impl
I have been running into Debug-printed error messages too often and
needing to look up in the source code each level of nested errors to
find out from the comment on the error variant what the short name of
the variant means in human terms. Worse, many errors (like the one shown
below) already had error strings written but were being printed from the
calling code in the less helpful Debug representation anyway.

Before:
    [ERROR:src/main.rs:705] The architecture failed to build the vm: NoVarEmpty

After:
    [ERROR:src/main.rs:705] The architecture failed to build the vm: /var/empty doesn't exist, can't jail devices.

TEST=cargo check --all-features
TEST=FEATURES=test emerge-amd64-generic crosvm

Change-Id: I77122c7d6861b2d610de2fff718896918ab21e10
Reviewed-on: https://chromium-review.googlesource.com/1469225
Commit-Ready: David Tolnay <dtolnay@chromium.org>
Tested-by: David Tolnay <dtolnay@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'sys_util/src/mmap.rs')
-rw-r--r--sys_util/src/mmap.rs31
1 files changed, 26 insertions, 5 deletions
diff --git a/sys_util/src/mmap.rs b/sys_util/src/mmap.rs
index 5de433c..07815ca 100644
--- a/sys_util/src/mmap.rs
+++ b/sys_util/src/mmap.rs
@@ -6,6 +6,7 @@
 //! mmap object leaves scope.
 
 use std;
+use std::fmt::{self, Display};
 use std::io::{Read, Write};
 use std::os::unix::io::AsRawFd;
 use std::ptr::null_mut;
@@ -36,6 +37,26 @@ pub enum Error {
 }
 pub type Result<T> = std::result::Result<T, Error>;
 
+impl Display for Error {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        use self::Error::*;
+
+        match self {
+            InvalidAddress => write!(f, "requested memory out of range"),
+            InvalidOffset => write!(f, "requested offset is out of range of off_t"),
+            InvalidRange(offset, count) => write!(
+                f,
+                "requested memory range spans past the end of the region: offset={} count={}",
+                offset, count,
+            ),
+            ReadFromSource(e) => write!(f, "failed to read from the given source: {}", e),
+            SystemCallFailed(e) => write!(f, "mmap system call failed: {}", e),
+            WriteToMemory(e) => write!(f, "failed to write to memory: {}", e),
+            ReadFromMemory(e) => write!(f, "failed to read from memory: {}", e),
+        }
+    }
+}
+
 /// Wraps an anonymous shared memory mapping in the current process.
 #[derive(Debug)]
 pub struct MemoryMapping {
@@ -75,7 +96,7 @@ impl MemoryMapping {
         // return value. We only warn about an error because failure here is not fatal to the mmap.
         if unsafe { libc::madvise(addr, size, libc::MADV_DONTDUMP) } == -1 {
             warn!(
-                "failed madvise(MADV_DONTDUMP) on mmap: {:?}",
+                "failed madvise(MADV_DONTDUMP) on mmap: {}",
                 errno::Error::last()
             );
         }
@@ -123,7 +144,7 @@ impl MemoryMapping {
         // return value. We only warn about an error because failure here is not fatal to the mmap.
         if unsafe { libc::madvise(addr, size, libc::MADV_DONTDUMP) } == -1 {
             warn!(
-                "failed madvise(MADV_DONTDUMP) on mmap: {:?}",
+                "failed madvise(MADV_DONTDUMP) on mmap: {}",
                 errno::Error::last()
             );
         }
@@ -414,7 +435,7 @@ mod tests {
         if let Error::SystemCallFailed(e) = res {
             assert_eq!(e.errno(), libc::EINVAL);
         } else {
-            panic!("unexpected error: {:?}", res);
+            panic!("unexpected error: {}", res);
         }
     }
 
@@ -425,7 +446,7 @@ mod tests {
         if let Error::SystemCallFailed(e) = res {
             assert_eq!(e.errno(), libc::EBADF);
         } else {
-            panic!("unexpected error: {:?}", res);
+            panic!("unexpected error: {}", res);
         }
     }
 
@@ -485,7 +506,7 @@ mod tests {
             .unwrap_err();
         match res {
             Error::InvalidOffset => {}
-            e => panic!("unexpected error: {:?}", e),
+            e => panic!("unexpected error: {}", e),
         }
     }
 }