From: Randy Dunlap <randy.dunlap@oracle.com>
To: Christoph Lameter <clameter@sgi.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-kernel@vger.kernel.org, hugh@veritas.com
Subject: [PATCH 1/3] hexdump: more output formatting
Date: Wed, 30 May 2007 14:34:28 -0700 [thread overview]
Message-ID: <20070530143428.2f20446a.randy.dunlap@oracle.com> (raw)
In-Reply-To: <Pine.LNX.4.64.0705241533510.32401@schroedinger.engr.sgi.com>
From: Randy Dunlap <randy.dunlap@oracle.com>
Add a prefix string parameter. Callers are responsible for any
string length/alignment that they want to see in the output. I.e.,
callers should pad strings to achieve alignment if they want that.
Add rowsize parameter. This is the number of raw data bytes
to be printed per line. Must be 16 or 32.
Add a group_size parameter. This allows callers to dump values
as 1-byte, 2-byte, 4-byte, or 8-byte numbers. Default is
1-byte numbers. If the total length is not an even multiple
of group_size, 1-byte numbers are printed.
Add an "ascii" output parameter. This causes ASCII data output
following the hex data output.
Clean up some doc examples.
Signed-off-by: Randy Dunlap <randy.dunlap@oracle.com>
---
include/linux/kernel.h | 10 ++--
lib/hexdump.c | 120 +++++++++++++++++++++++++++++++++++++------------
2 files changed, 99 insertions(+), 31 deletions(-)
--- linux-2622-rc2mm1-slub.orig/include/linux/kernel.h
+++ linux-2622-rc2mm1-slub/include/linux/kernel.h
@@ -218,10 +218,12 @@ enum {
DUMP_PREFIX_ADDRESS,
DUMP_PREFIX_OFFSET
};
-extern void hex_dump_to_buffer(const void *buf, size_t len, char *linebuf,
- size_t linebuflen);
-extern void print_hex_dump(const char *level, int prefix_type,
- void *buf, size_t len);
+extern void hex_dump_to_buffer(const void *buf, size_t len,
+ int rowsize, int group_size,
+ char *linebuf, size_t linebuflen, bool ascii);
+extern void print_hex_dump(const char *level, const char *prefix_str,
+ int prefix_type, int rowsize, int group_size,
+ void *buf, size_t len, bool ascii);
#define hex_asc(x) "0123456789abcdef"[x]
#ifdef DEBUG
--- linux-2622-rc2mm1-slub.orig/lib/hexdump.c
+++ linux-2622-rc2mm1-slub/lib/hexdump.c
@@ -16,42 +16,92 @@
* hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
* @buf: data blob to dump
* @len: number of bytes in the @buf
+ * @rowsize: number of bytes to print per line; must be 16 or 32
+ * @group_size: number of bytes to print at a time (1, 2, 4, 8; default = 1)
* @linebuf: where to put the converted data
* @linebuflen: total size of @linebuf, including space for terminating NUL
+ * @ascii: include ASCII after the hex output
*
* hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
- * 16 bytes of input data converted to hex + ASCII output.
+ * 16 or 32 bytes of input data converted to hex + ASCII output.
*
* Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
* to a hex + ASCII dump at the supplied memory location.
* The converted output is always NUL-terminated.
*
* E.g.:
- * hex_dump_to_buffer(frame->data, frame->len, linebuf, sizeof(linebuf));
+ * hex_dump_to_buffer(frame->data, frame->len, 1, linebuf, sizeof(linebuf));
*
* example output buffer:
- * 40414243 44454647 48494a4b 4c4d4e4f @ABCDEFGHIJKLMNO
+ * 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
*/
-void hex_dump_to_buffer(const void *buf, size_t len, char *linebuf,
- size_t linebuflen)
+void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
+ int group_size, char *linebuf, size_t linebuflen,
+ bool ascii)
{
const u8 *ptr = buf;
u8 ch;
int j, lx = 0;
- for (j = 0; (j < 16) && (j < len) && (lx + 3) < linebuflen; j++) {
- if (j && !(j % 4))
+ if (rowsize != 16 && rowsize != 32)
+ rowsize = 16;
+
+ if (!len)
+ goto nil;
+ if (len > rowsize) /* limit to one line at a time */
+ len = rowsize;
+ if ((len % group_size) != 0) /* no mixed size output */
+ group_size = 1;
+
+ switch (group_size) {
+ case 8: {
+ const u64 *ptr8 = buf;
+ int ngroups = len / group_size;
+
+ for (j = 0; j < ngroups; j++)
+ lx += scnprintf(linebuf + lx, linebuflen - lx,
+ "%16.16llx ", (unsigned long long)*(ptr8 + j));
+ break;
+ }
+
+ case 4: {
+ const u32 *ptr4 = buf;
+ int ngroups = len / group_size;
+
+ for (j = 0; j < ngroups; j++)
+ lx += scnprintf(linebuf + lx, linebuflen - lx,
+ "%8.8x ", *(ptr4 + j));
+ break;
+ }
+
+ case 2: {
+ const u16 *ptr2 = buf;
+ int ngroups = len / group_size;
+
+ for (j = 0; j < ngroups; j++)
+ lx += scnprintf(linebuf + lx, linebuflen - lx,
+ "%4.4x ", *(ptr2 + j));
+ break;
+ }
+
+ default:
+ for (j = 0; (j < rowsize) && (j < len) && (lx + 4) < linebuflen;
+ j++) {
+ ch = ptr[j];
+ linebuf[lx++] = hex_asc(ch >> 4);
+ linebuf[lx++] = hex_asc(ch & 0x0f);
linebuf[lx++] = ' ';
- ch = ptr[j];
- linebuf[lx++] = hex_asc(ch >> 4);
- linebuf[lx++] = hex_asc(ch & 0x0f);
+ }
+ break;
}
- if ((lx + 2) < linebuflen) {
- linebuf[lx++] = ' ';
+ if (!ascii)
+ goto nil;
+
+ if ((lx + 1) < linebuflen)
linebuf[lx++] = ' ';
- }
- for (j = 0; (j < 16) && (j < len) && (lx + 2) < linebuflen; j++)
+ for (j = 0; (j < rowsize) && (j < len) && (lx + 2) < linebuflen; j++)
linebuf[lx++] = isprint(ptr[j]) ? ptr[j] : '.';
+nil:
linebuf[lx++] = '\0';
}
EXPORT_SYMBOL(hex_dump_to_buffer);
@@ -59,44 +109,60 @@ EXPORT_SYMBOL(hex_dump_to_buffer);
/**
* print_hex_dump - print a text hex dump to syslog for a binary blob of data
* @level: kernel log level (e.g. KERN_DEBUG)
+ * @prefix_str: string to prefix each line with;
+ * caller supplies trailing spaces for alignment if desired
* @prefix_type: controls whether prefix of an offset, address, or none
* is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
+ * @rowsize: number of bytes to print per line; must be 16 or 32
+ * @group_size: number of bytes to print at a time (1, 2, 4, 8; default = 1)
* @buf: data blob to dump
* @len: number of bytes in the @buf
+ * @ascii: include ASCII after the hex output
*
* Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
* to the kernel log at the specified kernel log level, with an optional
* leading prefix.
*
+ * print_hex_dump() works on one "line" of output at a time, i.e.,
+ * 16 or 32 bytes of input data converted to hex + ASCII output.
+ * print_hex_dump() iterates over the entire input @buf, breaking it into
+ * "line size" chunks to format and print.
+ *
* E.g.:
* print_hex_dump(KERN_DEBUG, DUMP_PREFIX_ADDRESS, frame->data, frame->len);
*
- * Example output using %DUMP_PREFIX_OFFSET:
- * 0009ab42: 40414243 44454647 48494a4b 4c4d4e4f @ABCDEFGHIJKLMNO
- * Example output using %DUMP_PREFIX_ADDRESS:
- * ffffffff88089af0: 70717273 74757677 78797a7b 7c7d7e7f pqrstuvwxyz{|}~.
+ * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
+ * 0009ab42: 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
+ * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
+ * ffffffff88089af0: 73727170 77767574-7b7a7978 7f7e7d7c pqrstuvwxyz{|}~.
*/
-void print_hex_dump(const char *level, int prefix_type, void *buf, size_t len)
+void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
+ int rowsize, int group_size,
+ void *buf, size_t len, bool ascii)
{
u8 *ptr = buf;
int i, linelen, remaining = len;
- unsigned char linebuf[100];
+ unsigned char linebuf[200];
+
+ if (rowsize != 16 && rowsize != 32)
+ rowsize = 16;
- for (i = 0; i < len; i += 16) {
- linelen = min(remaining, 16);
- remaining -= 16;
- hex_dump_to_buffer(ptr + i, linelen, linebuf, sizeof(linebuf));
+ for (i = 0; i < len; i += rowsize) {
+ linelen = min(remaining, rowsize);
+ remaining -= rowsize;
+ hex_dump_to_buffer(ptr + i, linelen, rowsize, group_size,
+ linebuf, sizeof(linebuf), ascii);
switch (prefix_type) {
case DUMP_PREFIX_ADDRESS:
- printk("%s%*p: %s\n", level,
+ printk("%s%s%*p: %s\n", level, prefix_str,
(int)(2 * sizeof(void *)), ptr + i, linebuf);
break;
case DUMP_PREFIX_OFFSET:
- printk("%s%.8x: %s\n", level, i, linebuf);
+ printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
break;
default:
- printk("%s%s\n", level, linebuf);
+ printk("%s%s%s\n", level, prefix_str, linebuf);
break;
}
}
next prev parent reply other threads:[~2007-05-30 21:37 UTC|newest]
Thread overview: 104+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-23 7:42 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 7:48 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 9:22 ` 2.6.22-rc2-mm1 Rafael J. Wysocki
2007-05-23 14:47 ` 2.6.22-rc2-mm1 Alan Stern
2007-05-23 15:54 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 16:09 ` 2.6.22-rc2-mm1 Oleg Nesterov
2007-05-23 17:00 ` 2.6.22-rc2-mm1 Alan Stern
2007-05-23 16:21 ` 2.6.22-rc2-mm1 Oleg Nesterov
2007-05-23 18:41 ` 2.6.22-rc2-mm1 Alan Stern
2007-05-23 9:47 ` 2.6.22-rc2-mm1 Michal Piotrowski
2007-05-23 17:18 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-23 21:05 ` 2.6.22-rc2-mm1 Michal Piotrowski
2007-05-23 22:01 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 22:18 ` 2.6.22-rc2-mm1 Michal Piotrowski
2007-05-23 22:27 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-23 22:37 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 23:36 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-24 1:21 ` 2.6.22-rc2-mm1 Randy Dunlap
2007-05-24 2:43 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-24 3:00 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-24 3:26 ` 2.6.22-rc2-mm1 Randy Dunlap
2007-05-24 7:31 ` 2.6.22-rc2-mm1 Ingo Molnar
2007-05-24 16:40 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-24 21:20 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-24 21:29 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-24 21:55 ` 2.6.22-rc2-mm1 Randy Dunlap
2007-05-24 22:35 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-24 22:53 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-30 21:34 ` Randy Dunlap [this message]
2007-05-30 21:42 ` [PATCH 1/3] hexdump: more output formatting Christoph Lameter
2007-05-30 21:45 ` Randy Dunlap
2007-05-31 1:45 ` [PATCH 1/3 v2] " Randy Dunlap
2007-05-30 21:56 ` [PATCH 1/3] " Satyam Sharma
2007-05-30 22:03 ` Randy Dunlap
2007-05-30 22:11 ` Satyam Sharma
2007-05-30 22:18 ` Christoph Lameter
2007-05-30 22:41 ` Satyam Sharma
2007-05-30 22:44 ` Randy Dunlap
2007-05-30 22:48 ` Satyam Sharma
2007-05-30 22:59 ` Randy Dunlap
2007-05-30 22:25 ` Randy Dunlap
2007-05-30 22:36 ` Jesper Juhl
2007-05-30 23:04 ` Randy Dunlap
2007-05-30 23:07 ` Jesper Juhl
2007-05-30 21:34 ` [PATCH 2/3 -mm] slub: use lib/hexdump Randy Dunlap
2007-05-30 21:45 ` Christoph Lameter
2007-05-30 21:48 ` Randy Dunlap
2007-05-30 21:51 ` Christoph Lameter
2007-05-30 21:54 ` Randy Dunlap
2007-05-30 22:03 ` Christoph Lameter
2007-05-30 22:06 ` Randy Dunlap
2007-05-31 1:39 ` Randy Dunlap
2007-05-23 22:24 ` 2.6.22-rc2-mm1 Christoph Lameter
2007-05-23 12:01 ` 2.6.22-rc2-mm1 Gabriel C
2007-05-23 16:01 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 15:02 ` 2.6.22-rc2-mm1 William Lee Irwin III
2007-05-23 15:28 ` 2.6.22-rc2-mm1 William Lee Irwin III
2007-05-23 16:08 ` 2.6.22-rc2-mm1 William Lee Irwin III
2007-05-23 16:29 ` 2.6.22-rc2-mm1 William Lee Irwin III
2007-05-23 17:27 ` 2.6.22-rc2-mm1 William Lee Irwin III
2007-05-23 23:17 ` 2.6.22-rc2-mm1 Zan Lynx
2007-05-23 23:27 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-23 23:40 ` 2.6.22-rc2-mm1 Jiri Kosina
2007-05-24 3:28 ` 2.6.22-rc2-mm1 Dmitry Torokhov
2007-05-24 7:28 ` 2.6.22-rc2-mm1 Jiri Kosina
2007-05-30 14:08 ` [PATCH] Input: i8042 - cleanup of debug code (was Re: 2.6.22-rc2-mm1) Jiri Kosina
2007-05-30 14:27 ` Dmitry Torokhov
2007-05-30 14:30 ` Jiri Kosina
[not found] ` <1180058760.7001.6.camel@oberon.rnd.esoft.com>
2007-05-25 7:23 ` 2.6.22-rc2-mm1 Jiri Kosina
2007-05-23 23:50 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-24 10:36 ` rmmod e1000 hangs (Was Re: 2.6.22-rc2-mm1) Jeremy Fitzhardinge
2007-05-24 10:47 ` Herbert Xu
2007-05-24 10:54 ` Herbert Xu
2007-05-24 14:44 ` Kok, Auke
2007-05-25 12:54 ` Herbert Xu
2007-05-25 13:04 ` Herbert Xu
2007-05-25 13:32 ` Herbert Xu
2007-05-25 22:12 ` Kok, Auke
2007-05-25 23:48 ` Jeff Garzik
2007-05-26 0:10 ` Herbert Xu
2007-05-25 21:20 ` idle=poll burns my box [was Re: 2.6.22-rc2-mm1] J.A. Magallón
2007-05-25 21:52 ` Andrew Morton
2007-05-26 15:59 ` 2.6.22-rc2-mm1 Tilman Schmidt
2007-05-26 16:01 ` 2.6.22-rc2-mm1 Andrew Morton
2007-05-27 22:16 ` 2.6.22-rc2-mm1 Tilman Schmidt
2007-05-27 22:41 ` 2.6.22-rc2-mm1 Kay Sievers
2007-05-28 17:22 ` 2.6.22-rc2-mm1 Cornelia Huck
2007-05-29 7:56 ` 2.6.22-rc2-mm1 Kay Sievers
2007-05-29 7:25 ` 2.6.22-rc2-mm1 Cornelia Huck
2007-05-29 14:43 ` 2.6.22-rc2-mm1 Matt Mackall
2007-05-29 16:55 ` 2.6.22-rc2-mm1 Tilman Schmidt
2007-05-29 17:25 ` 2.6.22-rc2-mm1 Cornelia Huck
2007-06-01 12:38 ` 2.6.22-rc2-mm1 Greg KH
2007-07-03 8:50 ` 2.6.22-rc2-mm1 Cornelia Huck
2007-07-12 6:00 ` 2.6.22-rc2-mm1 Greg KH
2007-05-28 10:27 ` 2.6.22-rc2-mm1 - a different BUG: at mm/slab.c:777 __find_general_cachep() Valdis.Kletnieks
2007-05-28 10:43 ` Pekka Enberg
2007-05-28 11:12 ` Valdis.Kletnieks
2007-05-29 4:22 ` 2.6.22-rc2-mm1: SLUB Randy Dunlap
2007-05-29 17:13 ` Christoph Lameter
2007-05-29 18:13 ` Randy Dunlap
2007-05-29 18:30 ` Christoph Lameter
2007-05-29 18:32 ` Christoph Lameter
2007-05-29 18:59 ` Randy Dunlap
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20070530143428.2f20446a.randy.dunlap@oracle.com \
--to=randy.dunlap@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=clameter@sgi.com \
--cc=hugh@veritas.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.