mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/5] console: provide veprintf and eputchar helpers
@ 2024-07-31  7:14 Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 2/5] KASan: report: print shadow memory state to stderr Ahmad Fatoum
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Ahmad Fatoum @ 2024-07-31  7:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

For easy printing to stderr, provide helpers that can be used instead of
vprintf and eputchar, respectively. To make them generally usable, also
provide stubs for PBL.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/console_common.c | 19 ++++++++++++++-----
 include/stdio.h         | 10 +++++++++-
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/common/console_common.c b/common/console_common.c
index 0113a6413850..a8527eee1e5a 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -361,23 +361,32 @@ EXPORT_SYMBOL(of_console_get_by_alias);
 
 #endif /* !CONFIG_CONSOLE_NONE */
 
-int dprintf(int file, const char *fmt, ...)
+int vdprintf(int file, const char *fmt, va_list args)
 {
-	va_list args;
 	char printbuffer[CFG_PBSIZE];
 
-	va_start(args, fmt);
-
 	/*
 	 * For this to work, printbuffer must be larger than
 	 * anything we ever want to print.
 	 */
 	vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
-	va_end(args);
 
 	/* Print the string */
 	return dputs(file, printbuffer);
 }
+EXPORT_SYMBOL(vdprintf);
+
+int dprintf(int file, const char *fmt, ...)
+{
+	va_list args;
+	int i;
+
+	va_start(args, fmt);
+	i = vdprintf(file, fmt, args);
+	va_end(args);
+
+	return i;
+}
 EXPORT_SYMBOL(dprintf);
 
 int dputs(int fd, const char *s)
diff --git a/include/stdio.h b/include/stdio.h
index d53f29039376..095e9b0a1d42 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -95,14 +95,22 @@ static inline void putchar(char c)
  * FILE based functions
  */
 
-/* stderr */
+#ifdef __PBL__
+#define eprintf			printf
+#define veprintf		vprintf
+#define eputchar		putchar
+#else
 #define eprintf(fmt,args...)	dprintf(STDERR_FILENO, fmt ,##args)
+#define veprintf(fmt,args)	vdprintf(STDERR_FILENO, fmt, args)
+#define eputchar(ch)		dputc(STDERR_FILENO, ch)
+#endif
 
 #define STDIN_FILENO		0
 #define STDOUT_FILENO		1
 #define STDERR_FILENO		2
 #define MAX_FILES	128
 
+int vdprintf(int fd, const char *fmt, va_list args) ;
 int dprintf(int file, const char *fmt, ...) __attribute__ ((format(__printf__, 2, 3)));
 int dputs(int file, const char *s);
 int dputc(int file, const char c);
-- 
2.39.2




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

* [PATCH 2/5] KASan: report: print shadow memory state to stderr
  2024-07-31  7:14 [PATCH 1/5] console: provide veprintf and eputchar helpers Ahmad Fatoum
@ 2024-07-31  7:14 ` Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 3/5] ARM: print exception reports " Ahmad Fatoum
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ahmad Fatoum @ 2024-07-31  7:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

The hexdump is currently printed to stdout via printk in
print_hex_dump. KASAN reports are special: They should be printed to
stderr, but they should not be logged as that would involve
reallocation. Therefore instead of calling print_hex_dump, call eprintf
with the %*ph format specifier instead. This allows us to consolidate
the code some more.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 lib/kasan/Kconfig  | 1 +
 lib/kasan/report.c | 8 ++------
 2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/lib/kasan/Kconfig b/lib/kasan/Kconfig
index e96638304cd8..895a62d88439 100644
--- a/lib/kasan/Kconfig
+++ b/lib/kasan/Kconfig
@@ -11,6 +11,7 @@ config KASAN
 	depends on (HAVE_ARCH_KASAN && CC_HAS_KASAN_GENERIC)
 	depends on MALLOC_TLSF
 	select CONSTRUCTORS
+	select PRINTF_HEXSTR
 	help
 	  Enables KASAN (KernelAddressSANitizer) - runtime memory debugger,
 	  designed to find out-of-bounds accesses and use-after-free bugs.
diff --git a/lib/kasan/report.c b/lib/kasan/report.c
index a9050546e7a6..b7844e93553c 100644
--- a/lib/kasan/report.c
+++ b/lib/kasan/report.c
@@ -116,20 +116,16 @@ static void print_shadow_for_address(const void *addr)
 
 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
-		char buffer[4 + (BITS_PER_LONG/8)*2];
 		char shadow_buf[SHADOW_BYTES_PER_ROW];
 
-		snprintf(buffer, sizeof(buffer),
-			(i == 0) ? ">%px: " : " %px: ", kaddr);
 		/*
 		 * We should not pass a shadow pointer to generic
 		 * function, because generic functions may try to
 		 * access kasan mapping for the passed address.
 		 */
 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
-		print_hex_dump(KERN_ERR, buffer,
-			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
-			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
+		eprintf("%c%px: %*ph\n", (i == 0) ? '>' : ' ', kaddr,
+			SHADOW_BYTES_PER_ROW, shadow_buf);
 
 		if (row_is_guilty(shadow_row, shadow))
 			eprintf("%*c\n",
-- 
2.39.2




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

* [PATCH 3/5] ARM: print exception reports to stderr
  2024-07-31  7:14 [PATCH 1/5] console: provide veprintf and eputchar helpers Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 2/5] KASan: report: print shadow memory state to stderr Ahmad Fatoum
@ 2024-07-31  7:14 ` Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 4/5] common: print panic " Ahmad Fatoum
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Ahmad Fatoum @ 2024-07-31  7:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We already print stack traces to stderr, but exception cause and
register dump is printed to stdout still. Fix that by using eprintf
instead of printf.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/arm/cpu/interrupts_32.c | 26 +++++++++++++-------------
 arch/arm/cpu/interrupts_64.c | 24 ++++++++++++------------
 2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/arch/arm/cpu/interrupts_32.c b/arch/arm/cpu/interrupts_32.c
index 468dcdd30e93..623efb3966f0 100644
--- a/arch/arm/cpu/interrupts_32.c
+++ b/arch/arm/cpu/interrupts_32.c
@@ -42,21 +42,21 @@ void show_regs (struct pt_regs *regs)
 
 	flags = condition_codes (regs);
 
-	printf ("pc : [<%08lx>]    lr : [<%08lx>]\n"
+	eprintf("pc : [<%08lx>]    lr : [<%08lx>]\n"
 		"sp : %08lx  ip : %08lx  fp : %08lx\n",
 		instruction_pointer (regs),
 		regs->ARM_lr, regs->ARM_sp, regs->ARM_ip, regs->ARM_fp);
-	printf ("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
+	eprintf("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
 		regs->ARM_r10, regs->ARM_r9, regs->ARM_r8);
-	printf ("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
+	eprintf("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
 		regs->ARM_r7, regs->ARM_r6, regs->ARM_r5, regs->ARM_r4);
-	printf ("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
+	eprintf("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
 		regs->ARM_r3, regs->ARM_r2, regs->ARM_r1, regs->ARM_r0);
-	printf ("Flags: %c%c%c%c",
+	eprintf("Flags: %c%c%c%c",
 		flags & PSR_N_BIT ? 'N' : 'n',
 		flags & PSR_Z_BIT ? 'Z' : 'z',
 		flags & PSR_C_BIT ? 'C' : 'c', flags & PSR_V_BIT ? 'V' : 'v');
-	printf ("  IRQs %s  FIQs %s  Mode %s%s\n",
+	eprintf("  IRQs %s  FIQs %s  Mode %s%s\n",
 		interrupts_enabled (regs) ? "on" : "off",
 		fast_interrupts_enabled (regs) ? "on" : "off",
 		processor_modes[processor_mode (regs)],
@@ -79,7 +79,7 @@ static void __noreturn do_exception(struct pt_regs *pt_regs)
  */
 void do_undefined_instruction (struct pt_regs *pt_regs)
 {
-	printf ("undefined instruction\n");
+	eprintf("undefined instruction\n");
 	do_exception(pt_regs);
 }
 
@@ -92,7 +92,7 @@ void do_undefined_instruction (struct pt_regs *pt_regs)
  */
 void do_software_interrupt (struct pt_regs *pt_regs)
 {
-	printf ("software interrupt\n");
+	eprintf("software interrupt\n");
 	do_exception(pt_regs);
 }
 
@@ -104,7 +104,7 @@ void do_software_interrupt (struct pt_regs *pt_regs)
  */
 void do_prefetch_abort (struct pt_regs *pt_regs)
 {
-	printf ("prefetch abort\n");
+	eprintf("prefetch abort\n");
 	do_exception(pt_regs);
 }
 
@@ -136,8 +136,8 @@ void do_data_abort (struct pt_regs *pt_regs)
 
 	asm volatile ("mrc     p15, 0, %0, c6, c0, 0" : "=r" (far) : : "cc");
 
-	printf("unable to handle %s at address 0x%08x\n",
-	       data_abort_reason(far), far);
+	eprintf("unable to handle %s at address 0x%08x\n",
+		data_abort_reason(far), far);
 
 	do_exception(pt_regs);
 }
@@ -150,7 +150,7 @@ void do_data_abort (struct pt_regs *pt_regs)
  */
 void do_fiq (struct pt_regs *pt_regs)
 {
-	printf ("fast interrupt request\n");
+	eprintf("fast interrupt request\n");
 	do_exception(pt_regs);
 }
 
@@ -162,7 +162,7 @@ void do_fiq (struct pt_regs *pt_regs)
  */
 void do_irq (struct pt_regs *pt_regs)
 {
-	printf ("interrupt request\n");
+	eprintf("interrupt request\n");
 	do_exception(pt_regs);
 }
 
diff --git a/arch/arm/cpu/interrupts_64.c b/arch/arm/cpu/interrupts_64.c
index 6262ba8872db..8933cfeb4288 100644
--- a/arch/arm/cpu/interrupts_64.c
+++ b/arch/arm/cpu/interrupts_64.c
@@ -76,12 +76,12 @@ void show_regs(struct pt_regs *regs)
 {
 	int i;
 
-	printf("elr: %016lx lr : %016lx\n", regs->elr, regs->regs[30]);
+	eprintf("elr: %016lx lr : %016lx\n", regs->elr, regs->regs[30]);
 
 	for (i = 0; i < 29; i += 2)
-		printf("x%-2d: %016lx x%-2d: %016lx\n",
+		eprintf("x%-2d: %016lx x%-2d: %016lx\n",
 			i, regs->regs[i], i + 1, regs->regs[i + 1]);
-	printf("\n");
+	eprintf("\n");
 }
 
 static void __noreturn do_exception(struct pt_regs *pt_regs)
@@ -101,7 +101,7 @@ static void __noreturn do_exception(struct pt_regs *pt_regs)
  */
 void do_fiq(struct pt_regs *pt_regs)
 {
-	printf ("fast interrupt request\n");
+	eprintf("fast interrupt request\n");
 	do_exception(pt_regs);
 }
 
@@ -113,31 +113,31 @@ void do_fiq(struct pt_regs *pt_regs)
  */
 void do_irq(struct pt_regs *pt_regs)
 {
-	printf ("interrupt request\n");
+	eprintf("interrupt request\n");
 	do_exception(pt_regs);
 }
 
 void do_bad_sync(struct pt_regs *pt_regs)
 {
-	printf("bad sync\n");
+	eprintf("bad sync\n");
 	do_exception(pt_regs);
 }
 
 void do_bad_irq(struct pt_regs *pt_regs)
 {
-	printf("bad irq\n");
+	eprintf("bad irq\n");
 	do_exception(pt_regs);
 }
 
 void do_bad_fiq(struct pt_regs *pt_regs)
 {
-	printf("bad fiq\n");
+	eprintf("bad fiq\n");
 	do_exception(pt_regs);
 }
 
 void do_bad_error(struct pt_regs *pt_regs)
 {
-	printf("bad error\n");
+	eprintf("bad error\n");
 	do_exception(pt_regs);
 }
 
@@ -174,15 +174,15 @@ void do_sync(struct pt_regs *pt_regs, unsigned int esr, unsigned long far)
 		extra = data_abort_reason(far);
 	}
 
-	printf("%s%s exception (ESR 0x%08x) at 0x%016lx\n", extra ?: "",
-	       esr_get_class_string(esr), esr, far);
+	eprintf("%s%s exception (ESR 0x%08x) at 0x%016lx\n", extra ?: "",
+		esr_get_class_string(esr), esr, far);
 	do_exception(pt_regs);
 }
 
 
 void do_error(struct pt_regs *pt_regs)
 {
-	printf("error exception\n");
+	eprintf("error exception\n");
 	do_exception(pt_regs);
 }
 
-- 
2.39.2




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

* [PATCH 4/5] common: print panic to stderr
  2024-07-31  7:14 [PATCH 1/5] console: provide veprintf and eputchar helpers Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 2/5] KASan: report: print shadow memory state to stderr Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 3/5] ARM: print exception reports " Ahmad Fatoum
@ 2024-07-31  7:14 ` Ahmad Fatoum
  2024-07-31  7:14 ` [PATCH 5/5] RISC-V: stacktrace: output stack traces to standard error Ahmad Fatoum
  2024-08-01 11:33 ` [PATCH 1/5] console: provide veprintf and eputchar helpers Sascha Hauer
  4 siblings, 0 replies; 6+ messages in thread
From: Ahmad Fatoum @ 2024-07-31  7:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Stack trace is already printed to stderr, so have the panic string end
up there too.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/misc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/common/misc.c b/common/misc.c
index 7bc92c0f9518..cf92d7a48b38 100644
--- a/common/misc.c
+++ b/common/misc.c
@@ -285,8 +285,8 @@ BAREBOX_MAGICVAR(global.of.kernel.add_machine_compatible, "Additional machine/bo
 
 static void __noreturn do_panic(bool stacktrace, const char *fmt, va_list ap)
 {
-	vprintf(fmt, ap);
-	putchar('\n');
+	veprintf(fmt, ap);
+	eputchar('\n');
 
 	if (stacktrace)
 		dump_stack();
-- 
2.39.2




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

* [PATCH 5/5] RISC-V: stacktrace: output stack traces to standard error
  2024-07-31  7:14 [PATCH 1/5] console: provide veprintf and eputchar helpers Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2024-07-31  7:14 ` [PATCH 4/5] common: print panic " Ahmad Fatoum
@ 2024-07-31  7:14 ` Ahmad Fatoum
  2024-08-01 11:33 ` [PATCH 1/5] console: provide veprintf and eputchar helpers Sascha Hauer
  4 siblings, 0 replies; 6+ messages in thread
From: Ahmad Fatoum @ 2024-07-31  7:14 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

KASAN splats are currently split between standard output and standard
error, which looks ok, when both are enabled on the same consoles, but
is difficult to follow when mapping standard out/err to different
consoles. Let's move all stack trace dumping to standard error. This is
already the case for ARM.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 arch/riscv/lib/stacktrace.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/lib/stacktrace.c b/arch/riscv/lib/stacktrace.c
index 663938019ee6..b678ad7d5241 100644
--- a/arch/riscv/lib/stacktrace.c
+++ b/arch/riscv/lib/stacktrace.c
@@ -22,9 +22,9 @@ struct stackframe {
 static void dump_backtrace_entry(unsigned long where, unsigned long from)
 {
 #ifdef CONFIG_KALLSYMS
-	printf("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from);
+	eprintf("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from);
 #else
-	printf("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
+	eprintf("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
 #endif
 }
 
@@ -60,7 +60,7 @@ void unwind_backtrace(const struct pt_regs *regs)
 		frame.ra = (unsigned long)unwind_backtrace;
 	}
 
-	printf("Call trace:\n");
+	eprintf("Call trace:\n");
 	for (;;) {
 		unsigned long where = frame.ra;
 		int ret;
-- 
2.39.2




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

* Re: [PATCH 1/5] console: provide veprintf and eputchar helpers
  2024-07-31  7:14 [PATCH 1/5] console: provide veprintf and eputchar helpers Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2024-07-31  7:14 ` [PATCH 5/5] RISC-V: stacktrace: output stack traces to standard error Ahmad Fatoum
@ 2024-08-01 11:33 ` Sascha Hauer
  4 siblings, 0 replies; 6+ messages in thread
From: Sascha Hauer @ 2024-08-01 11:33 UTC (permalink / raw)
  To: barebox, Ahmad Fatoum


On Wed, 31 Jul 2024 09:14:12 +0200, Ahmad Fatoum wrote:
> For easy printing to stderr, provide helpers that can be used instead of
> vprintf and eputchar, respectively. To make them generally usable, also
> provide stubs for PBL.
> 
> 

Applied, thanks!

[1/5] console: provide veprintf and eputchar helpers
      https://git.pengutronix.de/cgit/barebox/commit/?id=8c404f92aab1 (link may not be stable)
[2/5] KASan: report: print shadow memory state to stderr
      https://git.pengutronix.de/cgit/barebox/commit/?id=c161590a335e (link may not be stable)
[3/5] ARM: print exception reports to stderr
      https://git.pengutronix.de/cgit/barebox/commit/?id=ef90a8380113 (link may not be stable)
[4/5] common: print panic to stderr
      https://git.pengutronix.de/cgit/barebox/commit/?id=3cf509ab1e9f (link may not be stable)
[5/5] RISC-V: stacktrace: output stack traces to standard error
      https://git.pengutronix.de/cgit/barebox/commit/?id=3d58b9f44f1a (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




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

end of thread, other threads:[~2024-08-01 11:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-31  7:14 [PATCH 1/5] console: provide veprintf and eputchar helpers Ahmad Fatoum
2024-07-31  7:14 ` [PATCH 2/5] KASan: report: print shadow memory state to stderr Ahmad Fatoum
2024-07-31  7:14 ` [PATCH 3/5] ARM: print exception reports " Ahmad Fatoum
2024-07-31  7:14 ` [PATCH 4/5] common: print panic " Ahmad Fatoum
2024-07-31  7:14 ` [PATCH 5/5] RISC-V: stacktrace: output stack traces to standard error Ahmad Fatoum
2024-08-01 11:33 ` [PATCH 1/5] console: provide veprintf and eputchar helpers Sascha Hauer

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