[Concept,09/35] test: Show the required size when console-record overflows

Message ID 20251210000737.180797-10-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:07 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

When the console-record buffer overflows, show both the current buffer
size and the size needed. This helps the user know what value to set
for CONFIG_CONSOLE_RECORD_OUT_SIZE.

Add a console_out_ovf field to global_data to track the number of bytes
that could not be written due to overflow.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 common/console.c                  | 16 +++++++++++-----
 include/asm-generic/global_data.h | 14 ++++++++++++++
 test/ut.c                         | 15 +++++++++++++--
 3 files changed, 38 insertions(+), 7 deletions(-)
  

Patch

diff --git a/common/console.c b/common/console.c
index c033d72486a..da1125cfa98 100644
--- a/common/console.c
+++ b/common/console.c
@@ -36,21 +36,26 @@  static void console_record_putc(const char c)
 {
 	if (!(gd->flags & GD_FLG_RECORD))
 		return;
-	if  (gd->console_out.start &&
-	     !membuf_putbyte((struct membuf *)&gd->console_out, c))
+	if (gd->console_out.start &&
+	    !membuf_putbyte((struct membuf *)&gd->console_out, c)) {
 		gd->flags |= GD_FLG_RECORD_OVF;
+		gd->console_out_ovf++;
+	}
 }
 
 static void console_record_puts(const char *s)
 {
 	if (!(gd->flags & GD_FLG_RECORD))
 		return;
-	if  (gd->console_out.start) {
+	if (gd->console_out.start) {
 		int len = strlen(s);
+		int written;
 
-		if (membuf_put((struct membuf *)&gd->console_out, s, len) !=
-		    len)
+		written = membuf_put((struct membuf *)&gd->console_out, s, len);
+		if (written != len) {
 			gd->flags |= GD_FLG_RECORD_OVF;
+			gd->console_out_ovf += len - written;
+		}
 	}
 }
 
@@ -893,6 +898,7 @@  void console_record_reset(void)
 	membuf_purge((struct membuf *)&gd->console_out);
 	membuf_purge((struct membuf *)&gd->console_in);
 	gd->flags &= ~GD_FLG_RECORD_OVF;
+	gd->console_out_ovf = 0;
 }
 
 int console_record_reset_enable(void)
diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h
index d91103dfe00..4a197a2c230 100644
--- a/include/asm-generic/global_data.h
+++ b/include/asm-generic/global_data.h
@@ -347,6 +347,12 @@  struct global_data {
 	 * This buffer is used to collect output during console recording.
 	 */
 	struct membuf console_out;
+	/**
+	 * @console_out_ovf: overflow byte count for console recording
+	 *
+	 * Number of bytes that could not be written due to buffer overflow.
+	 */
+	int console_out_ovf;
 	/**
 	 * @console_in: input buffer for console recording
 	 *
@@ -589,6 +595,14 @@  static_assert(sizeof(struct global_data) == GD_SIZE);
 #define gd_malloc_ptr()		0L
 #endif
 
+#ifdef CONFIG_CONSOLE_RECORD
+#define gd_console_out()		(&gd->console_out)
+#define gd_console_out_ovf()		gd->console_out_ovf
+#else
+#define gd_console_out()		NULL
+#define gd_console_out_ovf()		0
+#endif
+
 #if CONFIG_IS_ENABLED(UPL)
 #define gd_upl()		gd->upl
 #define gd_set_upl(_val)	gd->upl = (_val)
diff --git a/test/ut.c b/test/ut.c
index b4f2a8bf40f..94b09364687 100644
--- a/test/ut.c
+++ b/test/ut.c
@@ -8,6 +8,7 @@ 
 #include <console.h>
 #include <errno.h>
 #include <malloc.h>
+#include <membuf.h>
 #include <slre.h>
 #include <vsprintf.h>
 #ifdef CONFIG_SANDBOX
@@ -86,8 +87,18 @@  static int readline_check(struct unit_test_state *uts)
 
 	ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str));
 	if (ret == -ENOSPC) {
-		ut_fail(uts, __FILE__, __LINE__, __func__,
-			"Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE");
+		if (IS_ENABLED(CONFIG_CONSOLE_RECORD)) {
+			int cur_size = membuf_size(gd_console_out());
+
+			ut_failf(uts, __FILE__, __LINE__, __func__,
+				 "Console record buffer too small",
+				 "CONFIG_CONSOLE_RECORD_OUT_SIZE=%#x, need %#x",
+				 cur_size, cur_size + gd_console_out_ovf());
+		} else {
+			ut_fail(uts, __FILE__, __LINE__, __func__,
+				"Console record buffer too small - increase "
+				"CONFIG_CONSOLE_RECORD_OUT_SIZE");
+		}
 		return ret;
 	} else if (ret == -ENOENT) {
 		strcpy(uts->actual_str, "<no-more-output>");