From: Daniel Walton Date: Wed, 21 Oct 2015 01:46:55 +0000 (+0000) Subject: Add zlog_hexdump() for debugging X-Git-Tag: frr-2.0-rc1~1223^2 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=99a6c6cdb509a4b6b1a51d77dd0cf2a306571c16;p=matthieu%2Ffrr.git Add zlog_hexdump() for debugging Signed-off-by: Daniel Walton Reviewed-by: Donald Sharp Ticket: CM-7931 --- diff --git a/lib/log.c b/lib/log.c index cf575a3c43..936c34657e 100644 --- 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); +} diff --git a/lib/log.h b/lib/log.h index 28fc3736fb..4fcd44a713 100644 --- 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 */