From 77526f6728faad43f25359d107419081640b2b18 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 3 Mar 2020 15:57:10 +0100 Subject: [PATCH] MINOR: tools: add new function dump_addr_and_bytes() This function dumps bytes from in hex form into buffer enclosed in brackets after the address itself, formatted on 14 chars including the "0x" prefix. This is meant to be used as a prefix for code areas. For example: "0x7f10b6557690 [48 c7 c0 0f 00 00 00 0f]: " It relies on may_access() to know if the bytes are dumpable, otherwise "--" is emitted. An optional prefix is supported. (cherry picked from commit 762fb3ec8e05ca7a61b28e8a876c194c9db591ef) Signed-off-by: Willy Tarreau --- include/common/standard.h | 1 + src/standard.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index ef4bf93..e8b6917 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -1496,6 +1496,7 @@ int dump_text(struct buffer *out, const char *buf, int bsize); int dump_binary(struct buffer *out, const char *buf, int bsize); int dump_text_line(struct buffer *out, const char *buf, int bsize, int len, int *line, int ptr); +void dump_addr_and_bytes(struct buffer *buf, const char *pfx, const void *addr, int n); void dump_hex(struct buffer *out, const char *pfx, const void *buf, int len, int unsafe); int may_access(const void *ptr); diff --git a/src/standard.c b/src/standard.c index b85cbab..a75ab88 100644 --- a/src/standard.c +++ b/src/standard.c @@ -4199,6 +4199,31 @@ void dump_hex(struct buffer *out, const char *pfx, const void *buf, int len, int } } +/* dumps followed by bytes from in hex form into buffer + * enclosed in brackets after the address itself, formatted on 14 chars + * including the "0x" prefix. This is meant to be used as a prefix for code + * areas. For example: + * "0x7f10b6557690 [48 c7 c0 0f 00 00 00 0f]" + * It relies on may_access() to know if the bytes are dumpable, otherwise "--" + * is emitted. A NULL will be considered empty. + */ +void dump_addr_and_bytes(struct buffer *buf, const char *pfx, const void *addr, int n) +{ + int ok = 0; + int i; + + chunk_appendf(buf, "%s%#14lx [", pfx ? pfx : "", (long)addr); + + for (i = 0; i < n; i++) { + if (i == 0 || (((long)(addr + i) ^ (long)(addr)) & 4096)) + ok = may_access(addr + i); + if (ok) + chunk_appendf(buf, "%02x%s", ((uint8_t*)addr)[i], (i. The format is : * <2 spaces> <70 chars max> <\n> * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are -- 1.7.10.4