[PATCH v4 05/12] seq_file: provide an analogue of print_hex_dump()

From: Andy Shevchenko
Date: Thu Sep 04 2014 - 10:30:59 EST


The new seq_hex_dump() is a complete analogue of print_hex_dump().

We have few users of this functionality already. It allows to reduce their
codebase.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
---
fs/seq_file.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/seq_file.h | 4 ++++
2 files changed, 54 insertions(+)

diff --git a/fs/seq_file.c b/fs/seq_file.c
index 3857b72..66c721f4 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -12,6 +12,7 @@
#include <linux/slab.h>
#include <linux/cred.h>
#include <linux/mm.h>
+#include <linux/printk.h>

#include <asm/uaccess.h>
#include <asm/page.h>
@@ -794,6 +795,55 @@ void seq_pad(struct seq_file *m, char c)
}
EXPORT_SYMBOL(seq_pad);

+/* Analogue of print_hex_dump() */
+int seq_hex_dump(struct seq_file *m, 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;
+ int ret;
+
+ if (rowsize != 16 && rowsize != 32)
+ rowsize = 16;
+
+ for (i = 0; i < len; i += rowsize) {
+ linelen = min(remaining, rowsize);
+ remaining -= rowsize;
+
+ /* Prefix string */
+ ret = seq_printf(m, "%s", prefix_str);
+ if (ret < 0)
+ return ret;
+
+ /* Counter if asked */
+ if (prefix_type == DUMP_PREFIX_ADDRESS)
+ ret = seq_printf(m, "%p: ", ptr + i);
+ else if (prefix_type == DUMP_PREFIX_OFFSET)
+ ret = seq_printf(m, "%.8x: ", i);
+ if (ret < 0)
+ return ret;
+
+ /* Hex dump */
+ ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+ m->buf + m->count, m->size - m->count,
+ ascii);
+ if (m->count + ret >= m->size) {
+ seq_set_overflow(m);
+ return -1;
+ }
+ m->count += ret;
+
+ /* EOL */
+ ret = seq_putc(m, '\n');
+ if (ret < 0)
+ return ret;
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL(seq_hex_dump);
+
struct list_head *seq_list_start(struct list_head *head, loff_t pos)
{
struct list_head *lh;
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 52e0097..5de290c 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -107,6 +107,10 @@ int seq_write(struct seq_file *seq, const void *data, size_t len);
__printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
__printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);

+int seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
+ int rowsize, int groupsize, const void *buf, size_t len,
+ bool ascii);
+
int seq_path(struct seq_file *, const struct path *, const char *);
int seq_dentry(struct seq_file *, struct dentry *, const char *);
int seq_path_root(struct seq_file *m, const struct path *path,
--
2.1.0

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/