Skip to contentSkip to conversation

Make grub_error() more verbose

PATCH In review Leo Sandoval via Grub-devel 4 participants 10 messages Sep 23, 2025 – Sep 29, 2025

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 Make grub_error() more verbose

From: Peter Jones <[email protected]>

---
 grub-core/kern/err.c | 13 +++++++++++--
 include/grub/err.h   |  8 ++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)
grub-core/kern/err.c +11 -2
index 53c734de70..aebfe0cf83 100644
@@ -33,15 +33,24 @@ static struct grub_error_saved grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
 static int grub_error_stack_pos;
 static int grub_error_stack_assert;
 
+#ifdef grub_error
+#undef grub_error
+#endif
+
 grub_err_t
-grub_error (grub_err_t n, const char *fmt, ...)
+grub_error (grub_err_t n, const char *file, const int line, const char *fmt, ...)
 {
   va_list ap;
+  int m;
 
   grub_errno = n;
 
+  m = grub_snprintf (grub_errmsg, sizeof (grub_errmsg), "%s:%d:", file, line);
+  if (m < 0)
+    m = 0;
+
   va_start (ap, fmt);
-  grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
+  grub_vsnprintf (grub_errmsg + m, sizeof (grub_errmsg) - m, _(fmt), ap);
   va_end (ap);
 
   return n;
include/grub/err.h +6 -2
index 202fa8a7ae..7530f58b29 100644
@@ -88,8 +88,12 @@ struct grub_error_saved
 extern grub_err_t EXPORT_VAR(grub_errno);
 extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];
 
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file, const int line, const char *fmt, ...)
+	__attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__ ((noreturn));
 void EXPORT_FUNC(grub_error_push) (void);
 int EXPORT_FUNC(grub_error_pop) (void);
Signature · 7 lines
2.50.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH v2 2/2 Include function name on debug and error print functions

With the following change, we see standard (grub_dprintf) and error (grub_error) logs with the function name embedded (see below) into the log which is particular useful when debugging

    commands/efi/tpm.c:grub_tpm_measure:281:tpm: log_event, pcr = 8, size = 0xb,

Including one more field on the print log impacts the binary sizes and in turn their respective distro packages; for Fedora rpm packages the increase is 20k approximately.

---
 grub-core/kern/err.c  | 4 ++--
 grub-core/kern/misc.c | 4 ++--
 include/grub/err.h    | 6 +++---
 include/grub/misc.h   | 5 +++--
 4 files changed, 10 insertions(+), 9 deletions(-)
grub-core/kern/err.c +2 -2
index aebfe0cf83..ba04b57fb7 100644
@@ -38,14 +38,14 @@ static int grub_error_stack_assert;
 #endif
 
 grub_err_t
-grub_error (grub_err_t n, const char *file, const int line, const char *fmt, ...)
+grub_error (grub_err_t n, const char *file, const char *function, const int line, const char *fmt, ...)
 {
   va_list ap;
   int m;
 
   grub_errno = n;
 
-  m = grub_snprintf (grub_errmsg, sizeof (grub_errmsg), "%s:%d:", file, line);
+  m = grub_snprintf (grub_errmsg, sizeof (grub_errmsg), "%s:%s:%d:", file, function, line);
   if (m < 0)
     m = 0;
 
grub-core/kern/misc.c +2 -2
index 258f918935..b9ac86c186 100644
@@ -231,14 +231,14 @@ grub_debug_enabled (const char * condition)
 }
 
 void
-grub_real_dprintf (const char *file, const int line, const char *condition,
+grub_real_dprintf (const char *file, const char *function, const int line, const char *condition,
 		   const char *fmt, ...)
 {
   va_list args;
 
   if (grub_debug_enabled (condition))
     {
-      grub_printf ("%s:%d:%s: ", file, line, condition);
+      grub_printf ("%s:%s:%d:%s: ", file, function, line, condition);
       va_start (args, fmt);
       grub_vprintf (fmt, args);
       va_end (args);
include/grub/err.h +3 -3
index 7530f58b29..6379a6baf8 100644
@@ -88,10 +88,10 @@ struct grub_error_saved
 extern grub_err_t EXPORT_VAR(grub_errno);
 extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];
 
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file, const int line, const char *fmt, ...)
-	__attribute__ ((format (GNU_PRINTF, 4, 5)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file, const char *function, const int line, const char *fmt, ...)
+	__attribute__ ((format (GNU_PRINTF, 5, 6)));
 
-#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
 
 
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__ ((noreturn));
include/grub/misc.h +3 -2
index 9522d7305a..0d2bab7dd7 100644
@@ -35,7 +35,7 @@
 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
 #define COMPILE_TIME_ASSERT(cond) switch (0) { case 1: case !(cond): ; }
 
-#define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __LINE__, condition, __VA_ARGS__)
+#define grub_dprintf(condition, ...) grub_real_dprintf(GRUB_FILE, __FUNCTION__, __LINE__, condition, __VA_ARGS__)
 
 void *EXPORT_FUNC(grub_memmove) (void *dest, const void *src, grub_size_t n);
 char *EXPORT_FUNC(grub_strcpy) (char *dest, const char *src);
@@ -413,9 +413,10 @@ grub_puts (const char *s)
 int EXPORT_FUNC(grub_puts_) (const char *s);
 int EXPORT_FUNC(grub_debug_enabled) (const char *condition);
 void EXPORT_FUNC(grub_real_dprintf) (const char *file,
+				     const char *function,
                                      const int line,
                                      const char *condition,
-                                     const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 4, 5)));
+                                     const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 5, 6)));
 int EXPORT_FUNC(grub_printf) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
 int EXPORT_FUNC(grub_printf_) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
 int EXPORT_FUNC(grub_vprintf) (const char *fmt, va_list args);
Signature · 7 lines
2.50.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw
In reply to Leo Sandoval via Grub-devel

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

Regards
Vladimir 'phcoder' Serbinenko

Quoted 68 lines · Leo Sandoval via Grub-devel

From: Peter Jones <[email protected]>

---
 grub-core/kern/err.c | 13 +++++++++++--
 include/grub/err.h   |  8 ++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)
grub-core/kern/err.c +11 -2
index 53c734de70..aebfe0cf83 100644
@@ -33,15 +33,24 @@ static struct grub_error_saved
 grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
 static int grub_error_stack_pos;
 static int grub_error_stack_assert;
 
+#ifdef grub_error
+#undef grub_error
+#endif
+
 grub_err_t
-grub_error (grub_err_t n, const char *fmt, ...)
+grub_error (grub_err_t n, const char *file, const int line, const char
 *fmt, ...)
 {
   va_list ap;
+  int m;
 
   grub_errno = n;
 
+  m = grub_snprintf (grub_errmsg, sizeof (grub_errmsg), "%s:%d:", file,
 line);
+  if (m < 0)
+    m = 0;
+
   va_start (ap, fmt);
-  grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
+  grub_vsnprintf (grub_errmsg + m, sizeof (grub_errmsg) - m, _(fmt), ap);
   va_end (ap);
   return n;
include/grub/err.h +6 -2
index 202fa8a7ae..7530f58b29 100644
@@ -88,8 +88,12 @@ struct grub_error_saved
 extern grub_err_t EXPORT_VAR(grub_errno);
 extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];
 
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file, const
 int line, const char *fmt, ...)
+       __attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__, fmt,
 ##__VA_ARGS__)
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__

((noreturn));
void EXPORT_FUNC(grub_error_push) (void);
int EXPORT_FUNC(grub_error_pop) (void);
--
2.50.1

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

running this in both grub versions

$ grub2-mkimage -O i386-pc -p /tmp -o core.img biosdisk part_msdos ext2

core.img file increases from 34538 to 35132 bytes, so the increase is about 0.6K.

Regards

Quoted 73 lines

Vladimir 'phcoder' Serbinenko

Quoted 68 lines · Leo Sandoval via Grub-devel

From: Peter Jones <[email protected]>

---
 grub-core/kern/err.c | 13 +++++++++++--
 include/grub/err.h   |  8 ++++++--
 2 files changed, 17 insertions(+), 4 deletions(-)
grub-core/kern/err.c +11 -2
index 53c734de70..aebfe0cf83 100644
@@ -33,15 +33,24 @@ static struct grub_error_saved
 grub_error_stack_items[GRUB_ERROR_STACK_SIZE];
 static int grub_error_stack_pos;
 static int grub_error_stack_assert;
 
+#ifdef grub_error
+#undef grub_error
+#endif
+
 grub_err_t
-grub_error (grub_err_t n, const char *fmt, ...)
+grub_error (grub_err_t n, const char *file, const int line, const char
 *fmt, ...)
 {
   va_list ap;
+  int m;
 
   grub_errno = n;
 
+  m = grub_snprintf (grub_errmsg, sizeof (grub_errmsg), "%s:%d:", file,
 line);
+  if (m < 0)
+    m = 0;
+
   va_start (ap, fmt);
-  grub_vsnprintf (grub_errmsg, sizeof (grub_errmsg), _(fmt), ap);
+  grub_vsnprintf (grub_errmsg + m, sizeof (grub_errmsg) - m, _(fmt), ap);
   va_end (ap);
   return n;
include/grub/err.h +6 -2
index 202fa8a7ae..7530f58b29 100644
@@ -88,8 +88,12 @@ struct grub_error_saved
 extern grub_err_t EXPORT_VAR(grub_errno);
 extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];
 
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file,
 const int line, const char *fmt, ...)
+       __attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__, fmt,
 ##__VA_ARGS__)
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__

((noreturn));
void EXPORT_FUNC(grub_error_push) (void);
int EXPORT_FUNC(grub_error_pop) (void);
--
2.50.1

Quoted 6 lines
Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#5 lore raw
Quoted 9 lines · Leo Sandoval
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

Quoted 7 lines

running this in both grub versions

$ grub2-mkimage -O i386-pc -p /tmp -o core.img biosdisk part_msdos ext2

core.img file increases from 34538 to 35132 bytes, so the increase is about 0.6K.

0.6K out of 31K is a lot. Is there a way to decrease this overhead?

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

extern grub_err_t EXPORT_VAR(grub_errno); extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];

include/grub/err.h +1 -2
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file,

const int line, const char *fmt, ...)

include/grub/err.h +3 -0
+       __attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__, fmt,

##__VA_ARGS__)

grub-core/kern/err.c +2 -0
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__

((noreturn));
void EXPORT_FUNC(grub_error_push) (void);
int EXPORT_FUNC(grub_error_pop) (void);
--
2.50.1

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#6 lore raw
Quoted 15 lines · Vladimir 'phcoder' Serbinenko
Quoted 11 lines · Leo Sandoval
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

running this in both grub versions

Quoted 8 lines
Quoted 5 lines
$ grub2-mkimage -O i386-pc -p /tmp -o core.img biosdisk part_msdos ext2

core.img file increases from 34538 to 35132 bytes, so the increase is about 0.6K.

0.6K out of 31K is a lot. Is there a way to decrease this overhead?

I forgot to indicate that this increase includes this patch and the other in the series, which includes the function name also. In general, this 0.6K increase includes the file:function:line_number on the logs.

No idea how to decrease it. Any suggestion to try?

Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines

extern grub_err_t EXPORT_VAR(grub_errno); extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];

include/grub/err.h +1 -2
-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file,

const int line, const char *fmt, ...)

include/grub/err.h +3 -0
+       __attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__,

fmt, ##__VA_ARGS__)

grub-core/kern/err.c +2 -0
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__

((noreturn));
void EXPORT_FUNC(grub_error_push) (void);
int EXPORT_FUNC(grub_error_pop) (void);
--
2.50.1

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#7 lore raw

How large is the i386 pc user base, do you think including a conditional compile to only add for uefi 64-bit covers most cases?

Just a thought.

Thanks,
Andrew

Quoted 67 lines · Leo Sandoval via Grub-devel
Quoted 16 lines · Vladimir 'phcoder' Serbinenko

Le jeu. 25 sept. 2025, 21:04, Leo Sandoval <[email protected]> a écrit :

Quoted 11 lines
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

running this in both grub versions

Quoted 8 lines
Quoted 5 lines
$ grub2-mkimage -O i386-pc -p /tmp -o core.img biosdisk part_msdos ext2

core.img file increases from 34538 to 35132 bytes, so the increase is about 0.6K.

0.6K out of 31K is a lot. Is there a way to decrease this overhead?

I forgot to indicate that this increase includes this patch and the other in the series, which includes the function name also. In general, this 0.6K increase includes the file:function:line_number on the logs.

No idea how to decrease it. Any suggestion to try?

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines

extern grub_err_t EXPORT_VAR(grub_errno); extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];

-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)

(quoted diff) +1 -1
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file,

const int line, const char *fmt, ...)

include/grub/err.h +3 -0
+       __attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__,

fmt, ##__VA_ARGS__)

grub-core/kern/err.c +2 -0
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__

((noreturn));
void EXPORT_FUNC(grub_error_push) (void);
int EXPORT_FUNC(grub_error_pop) (void);
--
2.50.1

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________

Quoted 4 lines

Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#8 lore raw
Quoted 3 lines · Andrew Hamilton

How large is the i386 pc user base, do you think including a conditional compile to only add for uefi 64-bit covers most cases?

Great idea, thanks! I will bump then the series' version including the related changes

Quoted 81 lines

Just a thought.

Thanks,
Andrew

Quoted 71 lines · Leo Sandoval via Grub-devel
Quoted 16 lines · Vladimir 'phcoder' Serbinenko

Le jeu. 25 sept. 2025, 21:04, Leo Sandoval <[email protected]> a écrit :

Quoted 11 lines
Quoted 3 lines · Vladimir 'phcoder' Serbinenko

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

running this in both grub versions

Quoted 8 lines
Quoted 5 lines
$ grub2-mkimage -O i386-pc -p /tmp -o core.img biosdisk part_msdos ext2

core.img file increases from 34538 to 35132 bytes, so the increase is about 0.6K.

0.6K out of 31K is a lot. Is there a way to decrease this overhead?

I forgot to indicate that this increase includes this patch and the other in the series, which includes the function name also. In general, this 0.6K increase includes the file:function:line_number on the logs.

No idea how to decrease it. Any suggestion to try?

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines

extern grub_err_t EXPORT_VAR(grub_errno); extern char EXPORT_VAR(grub_errmsg)[GRUB_MAX_ERRMSG];

-grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *fmt, ...)

(quoted diff) +1 -1
-    __attribute__ ((format (GNU_PRINTF, 2, 3)));
+grub_err_t EXPORT_FUNC(grub_error) (grub_err_t n, const char *file,

const int line, const char *fmt, ...)

include/grub/err.h +3 -0
+       __attribute__ ((format (GNU_PRINTF, 4, 5)));
+
+#define grub_error(n, fmt, ...) grub_error (n, __FILE__, __LINE__,

fmt, ##__VA_ARGS__)

grub-core/kern/err.c +2 -0
+
+
 void EXPORT_FUNC(grub_fatal) (const char *fmt, ...) __attribute__

((noreturn));
void EXPORT_FUNC(grub_error_push) (void);
int EXPORT_FUNC(grub_error_pop) (void);
--
2.50.1

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________

Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#9 lore raw
In reply to Vladimir 'phcoder' Serbinenko
Quoted 2 lines · Vladimir 'phcoder' Serbinenko

What is the influence on core.img size on i386-pc? Are we still within our promises for supporting 31K gaps with simple config?

Vladimir, long time ago we stated that this setup is not recommended [1] and I think we should not care about it because simply it makes GRUB maintainers and developers live more difficult. Additionally, IIRC we at least once talked about that at FOSDEM during GRUB Project Status Update presentation. So, I am against making conditional builds or other tricks to support outdated configs. I think we should take Leo's patch as is.

Daniel

[1] https://www.gnu.org/software/grub/manual/grub/grub.html#BIOS-installation

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel