Skip to content

Commit 3760710

Browse files
andy-shevtorvalds
authored andcommitted
seq_file: provide an analogue of print_hex_dump()
This introduces a new helper and switches current users to use it. All patches are compiled tested. kmemleak is tested via its own test suite. This patch (of 6): 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@linux.intel.com> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Joe Perches <joe@perches.com> Cc: Tadeusz Struk <tadeusz.struk@intel.com> Cc: Helge Deller <deller@gmx.de> Cc: Ingo Tuchscherer <ingo.tuchscherer@de.ibm.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Vladimir Kondratiev <qca_vkondrat@qca.qualcomm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 40f705a commit 3760710

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

fs/seq_file.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <linux/slab.h>
1313
#include <linux/cred.h>
1414
#include <linux/mm.h>
15+
#include <linux/printk.h>
1516

1617
#include <asm/uaccess.h>
1718
#include <asm/page.h>
@@ -773,6 +774,47 @@ void seq_pad(struct seq_file *m, char c)
773774
}
774775
EXPORT_SYMBOL(seq_pad);
775776

777+
/* A complete analogue of print_hex_dump() */
778+
void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
779+
int rowsize, int groupsize, const void *buf, size_t len,
780+
bool ascii)
781+
{
782+
const u8 *ptr = buf;
783+
int i, linelen, remaining = len;
784+
int ret;
785+
786+
if (rowsize != 16 && rowsize != 32)
787+
rowsize = 16;
788+
789+
for (i = 0; i < len && !seq_has_overflowed(m); i += rowsize) {
790+
linelen = min(remaining, rowsize);
791+
remaining -= rowsize;
792+
793+
switch (prefix_type) {
794+
case DUMP_PREFIX_ADDRESS:
795+
seq_printf(m, "%s%p: ", prefix_str, ptr + i);
796+
break;
797+
case DUMP_PREFIX_OFFSET:
798+
seq_printf(m, "%s%.8x: ", prefix_str, i);
799+
break;
800+
default:
801+
seq_printf(m, "%s", prefix_str);
802+
break;
803+
}
804+
805+
ret = hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
806+
m->buf + m->count, m->size - m->count,
807+
ascii);
808+
if (ret >= m->size - m->count) {
809+
seq_set_overflow(m);
810+
} else {
811+
m->count += ret;
812+
seq_putc(m, '\n');
813+
}
814+
}
815+
}
816+
EXPORT_SYMBOL(seq_hex_dump);
817+
776818
struct list_head *seq_list_start(struct list_head *head, loff_t pos)
777819
{
778820
struct list_head *lh;

include/linux/seq_file.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ int seq_write(struct seq_file *seq, const void *data, size_t len);
122122
__printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
123123
__printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
124124

125+
void seq_hex_dump(struct seq_file *m, const char *prefix_str, int prefix_type,
126+
int rowsize, int groupsize, const void *buf, size_t len,
127+
bool ascii);
128+
125129
int seq_path(struct seq_file *, const struct path *, const char *);
126130
int seq_file_path(struct seq_file *, struct file *, const char *);
127131
int seq_dentry(struct seq_file *, struct dentry *, const char *);

0 commit comments

Comments
 (0)