]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: add a generic hexdump function for debugging
authorWilly Tarreau <w@1wt.eu>
Sun, 4 Dec 2016 23:10:57 +0000 (00:10 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 5 Jan 2017 19:12:20 +0000 (20:12 +0100)
debug_hexdump() prints to the requested output stream (typically stdout
or stderr) an hex dump of the blob passed in argument. This is useful
to help debug binary protocols.

include/common/standard.h
src/standard.c

index e16b30414fdf8f2e2fed6e2058ea16117ab1308a..68244dbe794602119183d26b09cf0fde79c3ff27 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <limits.h>
 #include <string.h>
+#include <stdio.h>
 #include <time.h>
 #include <sys/time.h>
 #include <sys/types.h>
@@ -990,6 +991,11 @@ char *env_expand(char *in);
  */
 #define fddebug(msg...) do { char *_m = NULL; memprintf(&_m, ##msg); if (_m) write(-1, _m, strlen(_m)); free(_m); } while (0)
 
+/* displays a <len> long memory block at <buf>, assuming first byte of <buf>
+ * has address <baseaddr>. The output is emitted to file <out>.
+ */
+void debug_hexdump(FILE *out, char *buf, unsigned int baseaddr, int len);
+
 /* used from everywhere just to drain results we don't want to read and which
  * recent versions of gcc increasingly and annoyingly complain about.
  */
index 079aef8f7575c62d655233b97758eb1377746365..910e1c298b303cedac3a43fa636b0d21b197a801 100644 (file)
@@ -3765,6 +3765,51 @@ int dump_text_line(struct chunk *out, const char *buf, int bsize, int len,
        return ptr;
 }
 
+/* displays a <len> long memory block at <buf>, assuming first byte of <buf>
+ * has address <baseaddr>. The output is emitted to file <out>.
+ */
+void debug_hexdump(FILE *out, char *buf, unsigned int baseaddr, int len)
+{
+       unsigned int i, j;
+       int b;
+
+       for (i = 0; i < (len + (baseaddr & 15)); i += 16) {
+               b = i - (baseaddr & 15);
+               fprintf(out, "%08x: ", i + (baseaddr & ~15));
+               for (j = 0; j < 8; j++) {
+                       if (b + j >= 0 && b + j < len)
+                               fprintf(out, "%02x ", (unsigned char)buf[b + j]);
+                       else
+                               fprintf(out, "   ");
+               }
+
+               if (b + j >= 0 && b + j < len)
+                       fputc('-', out);
+               else
+                       fputc(' ', out);
+
+               for (j = 8; j < 16; j++) {
+                       if (b + j >= 0 && b + j < len)
+                               fprintf(out, " %02x", (unsigned char)buf[b + j]);
+                       else
+                               fprintf(out, "   ");
+               }
+
+               fprintf(out, "   ");
+               for (j = 0; j < 16; j++) {
+                       if (b + j >= 0 && b + j < len) {
+                               if (isprint((unsigned char)buf[b + j]))
+                                       fputc((unsigned char)buf[b + j], out);
+                               else
+                                       fputc('.', out);
+                       }
+                       else
+                               fputc(' ', out);
+               }
+               fputc('\n', out);
+       }
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8