]> git.puffer.fish Git - matthieu/frr.git/commitdiff
Add zlog_hexdump() for debugging
authorDaniel Walton <dwalton@cumulusnetworks.com>
Wed, 21 Oct 2015 01:46:55 +0000 (01:46 +0000)
committerDaniel Walton <dwalton@cumulusnetworks.com>
Wed, 21 Oct 2015 01:46:55 +0000 (01:46 +0000)
Signed-off-by: Daniel Walton <dwalton@cumulusnetworks.com>
Reviewed-by: Donald Sharp <sharpd@cumulusnetworks.com>
Ticket: CM-7931

lib/log.c
lib/log.h

index cf575a3c43243e289965663712490166949c3b0e..936c34657e4eb0c21be8ea2eee2e09fb42f4e883 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -982,3 +982,45 @@ proto_redistnum(int afi, const char *s)
     }
   return -1;
 }
+
+void
+zlog_hexdump (void *mem, unsigned int len) {
+  unsigned long i = 0;
+  unsigned int j = 0;
+  unsigned int columns = 8;
+  char buf[(len * 4) + ((len/4) * 20) + 30];
+  char *s = buf;
+
+  for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++)
+    {
+      /* print offset */
+      if (i % columns == 0)
+        s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i);
+
+      /* print hex data */
+      if (i < len)
+        s += sprintf(s, "%02x ", 0xFF & ((char*)mem)[i]);
+
+      /* end of block, just aligning for ASCII dump */
+      else
+        s += sprintf(s, "   ");
+
+      /* print ASCII dump */
+      if (i % columns == (columns - 1))
+        {
+          for (j = i - (columns - 1); j <= i; j++)
+            {
+              if (j >= len) /* end of block, not really printing */
+                s += sprintf(s, " ");
+
+              else if(isprint(((char*)mem)[j])) /* printable char */
+                s += sprintf(s, "%x", 0xFF & ((char*)mem)[j]);
+
+              else /* other char */
+                s += sprintf(s, ".");
+            }
+          s += sprintf(s, "\n");
+        }
+    }
+    zlog_debug("\n%s", buf);
+}
index 28fc3736fb2582c415f3272bfb423550b52d35d4..4fcd44a7133e366d118f374882a2023ff5d143ed 100644 (file)
--- a/lib/log.h
+++ b/lib/log.h
@@ -175,6 +175,8 @@ extern void zlog_backtrace_sigsafe(int priority, void *program_counter);
 extern size_t quagga_timestamp(int timestamp_precision /* # subsecond digits */,
                               char *buf, size_t buflen);
 
+extern void zlog_hexdump(void *mem, unsigned int len);
+
 /* structure useful for avoiding repeated rendering of the same timestamp */
 struct timestamp_control {
    size_t len;         /* length of rendered timestamp */