]> git.proxmox.com Git - mirror_ubuntu-kernels.git/commitdiff
seq_buf: Add printing formatted hex dumps
authorPiotr Maziarz <piotrx.maziarz@linux.intel.com>
Thu, 7 Nov 2019 12:45:37 +0000 (13:45 +0100)
committerSteven Rostedt (VMware) <rostedt@goodmis.org>
Thu, 14 Nov 2019 18:15:12 +0000 (13:15 -0500)
Provided function is an analogue of print_hex_dump().

Implementing this function in seq_buf allows using for multiple
purposes (e.g. for tracing) and therefore prevents from code duplication
in every layer that uses seq_buf.

print_hex_dump() is an essential part of logging data to dmesg. Adding
similar capability for other purposes is beneficial to all users.

Example usage:
seq_buf_hex_dump(seq, "", DUMP_PREFIX_OFFSET, 16, 4, buf,
 ARRAY_SIZE(buf), true);
Example output:
0000000000000000 ffffff10 ffffff32 ffff3210  ........2....2..
00000010ffff3210 83d00437 c0700000 00000000  .2..7.....p.....
0000002002010004 0000000f 0000000f 00004002  .............@..
0000003000000fff 00000000                    ........

Link: http://lkml.kernel.org/r/1573130738-29390-1-git-send-email-piotrx.maziarz@linux.intel.com
Signed-off-by: Piotr Maziarz <piotrx.maziarz@linux.intel.com>
Signed-off-by: Cezary Rojewski <cezary.rojewski@intel.com>
Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
include/linux/seq_buf.h
lib/seq_buf.c

index aa5deb041c25d4796c8efa021de3c40d17483913..fb0205d87d3c1d80995147207ed7b24929c99a91 100644 (file)
@@ -125,6 +125,9 @@ extern int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len);
 extern int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
                              unsigned int len);
 extern int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc);
+extern int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str,
+                           int prefix_type, int rowsize, int groupsize,
+                           const void *buf, size_t len, bool ascii);
 
 #ifdef CONFIG_BINARY_PRINTF
 extern int
index bd807f545a9d71f4d6c4d87a9348cdba8a9986f1..4e865d42ab03cc8d8e541c86815a9acd81abdca3 100644 (file)
@@ -328,3 +328,65 @@ int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
        s->readpos += cnt;
        return cnt;
 }
+
+/**
+ * seq_buf_hex_dump - print formatted hex dump into the sequence buffer
+ * @s: seq_buf descriptor
+ * @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
+ * @groupsize: 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
+ *
+ * Function is an analogue of print_hex_dump() and thus has similar interface.
+ *
+ * linebuf size is maximal length for one line.
+ * 32 * 3 - maximum bytes per line, each printed into 2 chars + 1 for
+ *     separating space
+ * 2 - spaces separating hex dump and ascii representation
+ * 32 - ascii representation
+ * 1 - terminating '\0'
+ *
+ * Returns zero on success, -1 on overflow
+ */
+int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, int prefix_type,
+                    int rowsize, int groupsize,
+                    const void *buf, size_t len, bool ascii)
+{
+       const u8 *ptr = buf;
+       int i, linelen, remaining = len;
+       unsigned char linebuf[32 * 3 + 2 + 32 + 1];
+       int ret;
+
+       if (rowsize != 16 && rowsize != 32)
+               rowsize = 16;
+
+       for (i = 0; i < len; i += rowsize) {
+               linelen = min(remaining, rowsize);
+               remaining -= rowsize;
+
+               hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+                                  linebuf, sizeof(linebuf), ascii);
+
+               switch (prefix_type) {
+               case DUMP_PREFIX_ADDRESS:
+                       ret = seq_buf_printf(s, "%s%p: %s\n",
+                              prefix_str, ptr + i, linebuf);
+                       break;
+               case DUMP_PREFIX_OFFSET:
+                       ret = seq_buf_printf(s, "%s%.8x: %s\n",
+                                            prefix_str, i, linebuf);
+                       break;
+               default:
+                       ret = seq_buf_printf(s, "%s%s\n", prefix_str, linebuf);
+                       break;
+               }
+               if (ret)
+                       return ret;
+       }
+       return 0;
+}