Blob Blame History Raw
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
Date: Sat, 23 Nov 2019 14:57:41 +0100
Subject: [PATCH] New --with-debug-timestamps configure flag to prepend debug
 traces with absolute and relative timestamp
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Renaud M├ętrich <rmetrich@redhat.com>
---
 configure.ac          | 18 ++++++++++++++++++
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
 config.h.in           |  1 +
 3 files changed, 39 insertions(+)

diff --git a/configure.ac b/configure.ac
index a02d40a05b..ab0d326f00 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1585,6 +1585,17 @@ else
 fi
 AC_SUBST([BOOT_TIME_STATS])
 
+AC_ARG_WITH([debug-timestamps],
+	   AS_HELP_STRING([--with-debug-timestamps],
+                          [prepend debug traces with absolute and relative timestamps]))
+
+if test x$with_debug_timestamps = xyes; then
+  DEBUG_WITH_TIMESTAMPS=1
+else
+  DEBUG_WITH_TIMESTAMPS=0
+fi
+AC_SUBST([DEBUG_WITH_TIMESTAMPS])
+
 AC_ARG_ENABLE([grub-emu-sdl],
 	      [AS_HELP_STRING([--enable-grub-emu-sdl],
                              [build and install the `grub-emu' debugging utility with SDL support (default=guessed)])])
@@ -2136,6 +2147,7 @@ AM_CONDITIONAL([COND_APPLE_LINKER], [test x$TARGET_APPLE_LINKER = x1])
 AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu = xyes])
 AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS = x1])
 AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1])
+AM_CONDITIONAL([COND_DEBUG_WITH_TIMESTAMPS], [test x$DEBUG_WITH_TIMESTAMPS = x1])
 
 AM_CONDITIONAL([COND_HAVE_CXX], [test x$HAVE_CXX = xyes])
 
@@ -2231,6 +2243,12 @@ else
 echo With boot time statistics: No
 fi
 
+if [ x"$with_debug_timestamps" = xyes ]; then
+echo Debug traces with timestamps: Yes
+else
+echo Debug traces with timestamps: No
+fi
+
 if [ x"$efiemu_excuse" = x ]; then
 echo efiemu runtime: Yes
 else
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
index 578bf51a5f..9f54b6b7d2 100644
--- a/grub-core/kern/misc.c
+++ b/grub-core/kern/misc.c
@@ -25,6 +25,9 @@
 #include <grub/env.h>
 #include <grub/i18n.h>
 #include <grub/backtrace.h>
+#if DEBUG_WITH_TIMESTAMPS
+#include <grub/time.h>
+#endif
 
 union printf_arg
 {
@@ -192,9 +195,26 @@ grub_real_dprintf (const char *file, const int line, const char *condition,
 		   const char *fmt, ...)
 {
   va_list args;
+#if DEBUG_WITH_TIMESTAMPS
+  static long unsigned int last_time = 0;
+  static int last_had_cr = 1;
+#endif
 
   if (grub_debug_enabled (condition))
     {
+#if DEBUG_WITH_TIMESTAMPS
+      /* Don't print timestamp if last printed message isn't terminated yet */
+      if (last_had_cr) {
+        long unsigned int tmabs = (long unsigned int) grub_get_time_ms();
+        long unsigned int tmrel = tmabs - last_time;
+        last_time = tmabs;
+        grub_printf ("%3lu.%03lus +%2lu.%03lus ", tmabs / 1000, tmabs % 1000, tmrel / 1000, tmrel % 1000);
+      }
+      if (fmt[grub_strlen(fmt)-1] == '\n')
+        last_had_cr = 1;
+      else
+        last_had_cr = 0;
+#endif
       grub_printf ("%s:%d: ", file, line);
       va_start (args, fmt);
       grub_vprintf (fmt, args);
diff --git a/config.h.in b/config.h.in
index c7e316f0f1..c80e3e0aba 100644
--- a/config.h.in
+++ b/config.h.in
@@ -12,6 +12,7 @@
 /* Define to 1 to enable disk cache statistics.  */
 #define DISK_CACHE_STATS @DISK_CACHE_STATS@
 #define BOOT_TIME_STATS @BOOT_TIME_STATS@
+#define DEBUG_WITH_TIMESTAMPS @DEBUG_WITH_TIMESTAMPS@
 
 /* We don't need those.  */
 #define MINILZO_CFG_SKIP_LZO_PTR 1