mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] memory_display: Print whole line at once
@ 2018-12-03  7:37 Sascha Hauer
  2018-12-03  7:37 ` [PATCH 2/3] memory_display: move prototype to include/printk.h Sascha Hauer
  2018-12-03  7:37 ` [PATCH 3/3] Add pr_memory_display Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2018-12-03  7:37 UTC (permalink / raw)
  To: Barebox List

Instead of using many printf assemble a printed line first and
print it at once. This has the purpose of being able to pick
different output functions in the next step.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/memory_display.c | 35 ++++++++++++++++++-----------------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/common/memory_display.c b/common/memory_display.c
index 30821cced4..03d418b33b 100644
--- a/common/memory_display.c
+++ b/common/memory_display.c
@@ -8,6 +8,7 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 {
 	unsigned long linebytes, i;
 	unsigned char *cp;
+	unsigned char line[sizeof("00000000: 0000 0000 0000 0000  0000 0000 0000 0000            ................")];
 
 	/* Print the lines.
 	 *
@@ -20,9 +21,9 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 		uint32_t *uip = (uint32_t *)linebuf;
 		uint16_t *usp = (uint16_t *)linebuf;
 		uint8_t *ucp = (uint8_t *)linebuf;
-		unsigned count = 52;
+		unsigned char *pos = line;
 
-		printf("%08llx:", offs);
+		pos += sprintf(pos, "%08llx:", offs);
 		linebytes = (nbytes > DISP_LINE_LEN) ? DISP_LINE_LEN : nbytes;
 
 		for (i = 0; i < linebytes; i += size) {
@@ -34,9 +35,9 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 					res = __swab64(res);
 				if (data_abort_unmask()) {
 					res = 0xffffffffffffffffULL;
-					count -= printf(" xxxxxxxxxxxxxxxx");
+					pos += sprintf(pos, " xxxxxxxxxxxxxxxx");
 				} else {
-					count -= printf(" %016llx", res);
+					pos += sprintf(pos, " %016llx", res);
 				}
 				*ullp++ = res;
 			} else if (size == 4) {
@@ -47,9 +48,9 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 					res = __swab32(res);
 				if (data_abort_unmask()) {
 					res = 0xffffffff;
-					count -= printf(" xxxxxxxx");
+					pos += sprintf(pos, " xxxxxxxx");
 				} else {
-					count -= printf(" %08x", res);
+					pos += sprintf(pos, " %08x", res);
 				}
 				*uip++ = res;
 			} else if (size == 2) {
@@ -59,12 +60,12 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 				if (swab)
 					res = __swab16(res);
 				if (i > 1 && i % 8 == 0)
-					count -= printf(" ");
+					pos += sprintf(pos, " ");
 				if (data_abort_unmask()) {
 					res = 0xffff;
-					count -= printf(" xxxx");
+					pos += sprintf(pos, " xxxx");
 				} else {
-					count -= printf(" %04x", res);
+					pos += sprintf(pos, " %04x", res);
 				}
 				*usp++ = res;
 			} else {
@@ -72,12 +73,12 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 				data_abort_mask();
 				res = *((uint8_t *)addr);
 				if (i > 1 && i % 8 == 0)
-					count -= printf(" ");
+					pos += sprintf(pos, " ");
 				if (data_abort_unmask()) {
 					res = 0xff;
-					count -= printf(" xx");
+					pos += sprintf(pos, " xx");
 				} else {
-					count -= printf(" %02x", res);
+					pos += sprintf(pos, " %02x", res);
 				}
 				*ucp++ = res;
 			}
@@ -85,19 +86,19 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 			offs += size;
 		}
 
-		while (count--)
-			putchar(' ');
+		pos += sprintf(pos, "%*s", 61 - (pos - line), "");
 
 		cp = linebuf;
 		for (i = 0; i < linebytes; i++) {
 			if ((*cp < 0x20) || (*cp > 0x7e))
-				putchar('.');
+				sprintf(pos, ".");
 			else
-				printf("%c", *cp);
+				sprintf(pos, "%c", *cp);
+			pos++;
 			cp++;
 		}
 
-		putchar('\n');
+		printf("%s\n", line);
 		nbytes -= linebytes;
 		if (ctrlc())
 			return -EINTR;
-- 
2.19.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] memory_display: move prototype to include/printk.h
  2018-12-03  7:37 [PATCH 1/3] memory_display: Print whole line at once Sascha Hauer
@ 2018-12-03  7:37 ` Sascha Hauer
  2018-12-03  7:37 ` [PATCH 3/3] Add pr_memory_display Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2018-12-03  7:37 UTC (permalink / raw)
  To: Barebox List

It's where the kernel has the print_hex_dump prototypes aswell, it's a
better match for these.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/common.h | 10 ----------
 include/printk.h | 11 +++++++++++
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/include/common.h b/include/common.h
index f52c7e430c..11d26cb3db 100644
--- a/include/common.h
+++ b/include/common.h
@@ -112,16 +112,6 @@ void shutdown_barebox(void);
 #define PAGE_ALIGN(s)	ALIGN(s, PAGE_SIZE)
 #define PAGE_ALIGN_DOWN(x) ALIGN_DOWN(x, PAGE_SIZE)
 
-int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int swab);
-
-#define DUMP_PREFIX_OFFSET 0
-static inline void print_hex_dump(const char *level, const char *prefix_str,
-		int prefix_type, int rowsize, int groupsize,
-		const void *buf, size_t len, bool ascii)
-{
-	memory_display(buf, 0, len, 4, 0);
-}
-
 int mem_parse_options(int argc, char *argv[], char *optstr, int *mode,
 		char **sourcefile, char **destfile, int *swab);
 #define RW_BUF_SIZE	(unsigned)4096
diff --git a/include/printk.h b/include/printk.h
index 858e800543..4843dadd76 100644
--- a/include/printk.h
+++ b/include/printk.h
@@ -103,6 +103,17 @@ static inline int pr_print(int level, const char *format, ...)
 	}							\
 })
 
+int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size,
+		   int swab);
+
+#define DUMP_PREFIX_OFFSET 0
+static inline void print_hex_dump(const char *level, const char *prefix_str,
+		int prefix_type, int rowsize, int groupsize,
+		const void *buf, size_t len, bool ascii)
+{
+	memory_display(buf, 0, len, 4, 0);
+}
+
 struct log_entry {
 	struct list_head list;
 	char *msg;
-- 
2.19.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] Add pr_memory_display
  2018-12-03  7:37 [PATCH 1/3] memory_display: Print whole line at once Sascha Hauer
  2018-12-03  7:37 ` [PATCH 2/3] memory_display: move prototype to include/printk.h Sascha Hauer
@ 2018-12-03  7:37 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2018-12-03  7:37 UTC (permalink / raw)
  To: Barebox List

pr_memory_display is a memory_display variant that takes a MSG_*
loglevel priority with which the hexdump is printed. Like the
normal pr_* function this is optimized out when the priority is
below the compile time priority.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/memory_display.c | 37 ++++++++++++++++++++++++++++++++-----
 include/printk.h        |  8 ++++++++
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/common/memory_display.c b/common/memory_display.c
index 03d418b33b..cd0eadf88d 100644
--- a/common/memory_display.c
+++ b/common/memory_display.c
@@ -4,11 +4,21 @@
 
 #define DISP_LINE_LEN	16
 
-int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int swab)
+
+int __pr_memory_display(int level, const void *addr, loff_t offs, unsigned nbytes,
+			int size, int swab, const char *fmt, ...)
 {
 	unsigned long linebytes, i;
 	unsigned char *cp;
 	unsigned char line[sizeof("00000000: 0000 0000 0000 0000  0000 0000 0000 0000            ................")];
+	struct va_format vaf;
+	int ret;
+	va_list args;
+
+	va_start(args, fmt);
+
+	vaf.fmt = fmt;
+	vaf.va = &args;
 
 	/* Print the lines.
 	 *
@@ -98,11 +108,28 @@ int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size, int
 			cp++;
 		}
 
-		printf("%s\n", line);
+		if (level >= MSG_EMERG)
+			pr_print(level, "%pV%s\n", &vaf, line);
+		else
+			printf("%s\n", line);
+
 		nbytes -= linebytes;
-		if (ctrlc())
-			return -EINTR;
+		if (ctrlc()) {
+			ret = -EINTR;
+			goto out;
+		}
+
 	} while (nbytes > 0);
 
-	return 0;
+	va_end(args);
+	ret = 0;
+out:
+
+	return ret;
 }
+
+int memory_display(const void *addr, loff_t offs, unsigned nbytes,
+		   int size, int swab)
+{
+	return pr_memory_display(-1, addr, offs, nbytes, size, swab);
+}
\ No newline at end of file
diff --git a/include/printk.h b/include/printk.h
index 4843dadd76..aaad07552e 100644
--- a/include/printk.h
+++ b/include/printk.h
@@ -105,6 +105,14 @@ static inline int pr_print(int level, const char *format, ...)
 
 int memory_display(const void *addr, loff_t offs, unsigned nbytes, int size,
 		   int swab);
+int __pr_memory_display(int level, const void *addr, loff_t offs, unsigned nbytes,
+			int size, int swab, const char *format, ...);
+
+#define pr_memory_display(level, addr, offs, nbytes, size, swab) \
+	({	\
+		(level) <= LOGLEVEL ? __pr_memory_display((level), (addr), \
+				(offs), (nbytes), (size), (swab), pr_fmt("")) : 0; \
+	 })
 
 #define DUMP_PREFIX_OFFSET 0
 static inline void print_hex_dump(const char *level, const char *prefix_str,
-- 
2.19.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-12-03  7:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-03  7:37 [PATCH 1/3] memory_display: Print whole line at once Sascha Hauer
2018-12-03  7:37 ` [PATCH 2/3] memory_display: move prototype to include/printk.h Sascha Hauer
2018-12-03  7:37 ` [PATCH 3/3] Add pr_memory_display Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox