diff mbox

[v1,1/5] seq_file: provide an analogue of print_hex_dump()

Message ID 1404919470-26668-2-git-send-email-andriy.shevchenko@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Andy Shevchenko July 9, 2014, 3:24 p.m. UTC
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>
---
 fs/seq_file.c            | 35 +++++++++++++++++++++++++++++++++++
 include/linux/seq_file.h |  4 ++++
 2 files changed, 39 insertions(+)

Comments

Marek Vasut July 9, 2014, 8:39 p.m. UTC | #1
On Wednesday, July 09, 2014 at 05:24:26 PM, Andy Shevchenko wrote:
> 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>
> ---
>  fs/seq_file.c            | 35 +++++++++++++++++++++++++++++++++++
>  include/linux/seq_file.h |  4 ++++
>  2 files changed, 39 insertions(+)
> 
> diff --git a/fs/seq_file.c b/fs/seq_file.c
> index 3857b72..fec4a6b 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,40 @@ void seq_pad(struct seq_file *m, char c)
>  }
>  EXPORT_SYMBOL(seq_pad);
> 
> +/* Analogue of print_hex_dump() */
> +void 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;
> +	unsigned char linebuf[32 * 3 + 2 + 32 + 1];
> +
> +	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:
> +			seq_printf(m, "%s%p: %s\n", prefix_str, ptr + i, 
linebuf);
> +			break;
> +		case DUMP_PREFIX_OFFSET:
> +			seq_printf(m, "%s%.8x: %s\n", prefix_str, i, linebuf);
> +			break;
> +		default:
> +			seq_printf(m, "%s%s\n", prefix_str, linebuf);
> +			break;
> +		}
> +	}
> +}
> +EXPORT_SYMBOL(seq_hex_dump);

The above function looks like almost verbatim copy of print_hex_dump(). The only 
difference I can spot is that it's calling seq_printf() instead of printk(). Can 
you not instead generalize print_hex_dump() and based on it's invocation, make 
it call either seq_printf() or printk() ?

Best regards,
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Joe Perches July 9, 2014, 9:21 p.m. UTC | #2
On Wed, 2014-07-09 at 22:39 +0200, Marek Vasut wrote:
> The above function looks like almost verbatim copy of print_hex_dump(). The only 
> difference I can spot is that it's calling seq_printf() instead of printk(). Can 
> you not instead generalize print_hex_dump() and based on it's invocation, make 
> it call either seq_printf() or printk() ?

How do you propose doing that given any seq_<foo> call
requires a struct seq_file * and print_hex_dump needs
a KERN_<LEVEL>.

Is there an actual value to it?



--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Marek Vasut July 10, 2014, 7:58 a.m. UTC | #3
On Wednesday, July 09, 2014 at 11:21:08 PM, Joe Perches wrote:
> On Wed, 2014-07-09 at 22:39 +0200, Marek Vasut wrote:
> > The above function looks like almost verbatim copy of print_hex_dump().
> > The only difference I can spot is that it's calling seq_printf() instead
> > of printk(). Can you not instead generalize print_hex_dump() and based
> > on it's invocation, make it call either seq_printf() or printk() ?
> 
> How do you propose doing that given any seq_<foo> call
> requires a struct seq_file * and print_hex_dump needs
> a KERN_<LEVEL>.

I can imagine a rather nasty way, I can't say I would like it myself tho. The 
general idea would be to pull out the entire switch {} statement into a separate 
functions , one for printk() and one for seq_printf() cases. Then, have a 
generic do_hex_dump() call which would take as an argument a pointer to either 
of those functions and a void * to either the seq_file or level . Finally, there 
would have to be a wrapper to call the do_hex_dump() with the correct function 
pointer and it's associated arg.

Nasty? Yes ... Ineffective? Most likely.

> Is there an actual value to it?

Reducing the code duplication, but I wonder if there is a smarter solution than 
the horrid one above.

Best regards,
Marek Vasut
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andy Shevchenko July 10, 2014, 9:50 a.m. UTC | #4
On Thu, 2014-07-10 at 09:58 +0200, Marek Vasut wrote:
> On Wednesday, July 09, 2014 at 11:21:08 PM, Joe Perches wrote:
> > On Wed, 2014-07-09 at 22:39 +0200, Marek Vasut wrote:
> > > The above function looks like almost verbatim copy of print_hex_dump().
> > > The only difference I can spot is that it's calling seq_printf() instead
> > > of printk(). Can you not instead generalize print_hex_dump() and based
> > > on it's invocation, make it call either seq_printf() or printk() ?
> > 
> > How do you propose doing that given any seq_<foo> call
> > requires a struct seq_file * and print_hex_dump needs
> > a KERN_<LEVEL>.
> 
> I can imagine a rather nasty way, I can't say I would like it myself tho. The 
> general idea would be to pull out the entire switch {} statement into a separate 
> functions , one for printk() and one for seq_printf() cases. Then, have a 
> generic do_hex_dump() call which would take as an argument a pointer to either 
> of those functions and a void * to either the seq_file or level . Finally, there 
> would have to be a wrapper to call the do_hex_dump() with the correct function 
> pointer and it's associated arg.
> 
> Nasty? Yes ... Ineffective? Most likely.

It looks not good idea, yeah.

> > Is there an actual value to it?
> 
> Reducing the code duplication, but I wonder if there is a smarter solution than 
> the horrid one above.

I have considered to modify hex_dump_to_buffer() to return how many
bytes it actually proceed to the buffer. In that case we can directly
print to m->buf like other seq_<foo> calls do.

But I still have doubts about it. Any opinion?
Joe Perches July 10, 2014, 10:01 a.m. UTC | #5
On Thu, 2014-07-10 at 12:50 +0300, Andy Shevchenko wrote:
> I have considered to modify hex_dump_to_buffer() to return how many
> bytes it actually proceed to the buffer. In that case we can directly
> print to m->buf like other seq_<foo> calls do.
> 
> But I still have doubts about it. Any opinion?

Simpler is better.


--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/seq_file.c b/fs/seq_file.c
index 3857b72..fec4a6b 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,40 @@  void seq_pad(struct seq_file *m, char c)
 }
 EXPORT_SYMBOL(seq_pad);
 
+/* Analogue of print_hex_dump() */
+void 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;
+	unsigned char linebuf[32 * 3 + 2 + 32 + 1];
+
+	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:
+			seq_printf(m, "%s%p: %s\n", prefix_str, ptr + i, linebuf);
+			break;
+		case DUMP_PREFIX_OFFSET:
+			seq_printf(m, "%s%.8x: %s\n", prefix_str, i, linebuf);
+			break;
+		default:
+			seq_printf(m, "%s%s\n", prefix_str, linebuf);
+			break;
+		}
+	}
+}
+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..6a8be4c 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);
 
+void 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,