Add a hexdump function
authorRonald G. Minnich <rminnich@google.com>
Sat, 16 Nov 2013 21:48:49 +0000 (13:48 -0800)
committerBarret Rhoden <brho@cs.berkeley.edu>
Thu, 16 Jan 2014 19:40:06 +0000 (11:40 -0800)
Very handy when things go wrong.

Signed-off-by: Ronald G. Minnich <rminnich@google.com>
kern/include/kdebug.h
kern/src/Kbuild
kern/src/hexdump.c [new file with mode: 0644]

index 684b44a..907aaf6 100644 (file)
@@ -27,5 +27,5 @@ void __print_func_entry(const char *func, const char *file);
 void __print_func_exit(const char *func, const char *file);
 #define print_func_entry() __print_func_entry(__FUNCTION__, __FILE__)
 #define print_func_exit() __print_func_exit(__FUNCTION__, __FILE__)
-
+void hexdump(void *v, int length);
 #endif /* ROS_KERN_KDEBUG_H */
index ba85ea5..e784dc4 100644 (file)
@@ -12,6 +12,7 @@ obj-y                                         += event.o
 obj-y                                          += ext2fs.o
 obj-y                                          += frontend.o
 obj-y                                          += hashtable.o
+obj-y                                          += hexdump.o
 obj-y                                          += init.o
 obj-y                                          += kdebug.o
 obj-y                                          += kfs.o
diff --git a/kern/src/hexdump.c b/kern/src/hexdump.c
new file mode 100644 (file)
index 0000000..45f0822
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but without any warranty; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <kdebug.h>
+#include <kmalloc.h>
+#include <string.h>
+#include <assert.h>
+#include <smp.h>
+
+static int isprint(int c)
+{
+       return (c >= 32 && c <= 126);
+}
+
+void hexdump(void *v, int length)
+{
+        int i;
+        uint8_t *m = v;
+       uintptr_t memory = (uintptr_t)v;
+        int all_zero = 0;
+
+        for (i = 0; i < length; i += 16) {
+                int j;
+
+                all_zero++;
+                for (j = 0; j < 16; j++) {
+                        if(m[i+j] != 0) {
+                                all_zero = 0;
+                                break;
+                        }
+                }
+
+                if (all_zero < 2) {
+                        printk("%08lx:", memory + i);
+                        for (j = 0; j < 16; j++)
+                                printk(" %02x", m[i+j]);
+                        printk("  ");
+                        for (j = 0; j < 16; j++)
+                                printk("%c",
+                                       isprint(m[i+j]) ? m[i+j] : '.');
+                        printk("\n");
+                } else if (all_zero == 2) {
+                        printk("...\n");
+                }
+        }
+}
+
+