test: Show the required size when console-record overflows

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>
This commit is contained in:
Simon Glass
2025-12-08 15:11:40 -07:00
parent 8f8c862a7a
commit c17d68b0fb
3 changed files with 38 additions and 7 deletions

View File

@@ -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)

View File

@@ -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
*
@@ -585,6 +591,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)

View File

@@ -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>");