aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/util/debug.rs
diff options
context:
space:
mode:
authorKeegan McAllister <kmcallister@mozilla.com>2013-08-28 16:35:37 -0700
committerKeegan McAllister <kmcallister@mozilla.com>2013-09-03 13:10:51 -0700
commitfc3afc5059f9d8121a38c85079112ec672f3dcf1 (patch)
treeaf986678c932f4b30c74908ac45b45d8703cc2c6 /src/components/util/debug.rs
parent317add552832fc8315e4a6ed0bdb095a0371f3a8 (diff)
downloadservo-fc3afc5059f9d8121a38c85079112ec672f3dcf1.tar.gz
servo-fc3afc5059f9d8121a38c85079112ec672f3dcf1.zip
Add a hexdump function for debugging
Diffstat (limited to 'src/components/util/debug.rs')
-rw-r--r--src/components/util/debug.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/components/util/debug.rs b/src/components/util/debug.rs
new file mode 100644
index 00000000000..1c4549fc27a
--- /dev/null
+++ b/src/components/util/debug.rs
@@ -0,0 +1,31 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+use std::io;
+use std::vec::raw::buf_as_slice;
+use std::cast::transmute;
+use std::sys::size_of;
+
+fn hexdump_slice(buf: &[u8]) {
+ let stderr = io::stderr();
+ stderr.write_str(" ");
+ for (i, &v) in buf.iter().enumerate() {
+ stderr.write_str(fmt!("%02X ", v as uint));
+ match i % 16 {
+ 15 => stderr.write_str("\n "),
+ 7 => stderr.write_str(" "),
+ _ => ()
+ }
+ stderr.flush();
+ }
+ stderr.write_char('\n');
+}
+
+pub fn hexdump<T>(obj: &T) {
+ unsafe {
+ let buf: *u8 = transmute(obj);
+ debug!("dumping at %p", buf);
+ buf_as_slice(buf, size_of::<T>(), hexdump_slice);
+ }
+}