46968b6
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
46968b6
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
46968b6
Date: Sat, 23 Nov 2019 14:57:41 +0100
46968b6
Subject: [PATCH] New --with-debug-timestamps configure flag to prepend debug
46968b6
 traces with absolute and relative timestamp
46968b6
MIME-Version: 1.0
46968b6
Content-Type: text/plain; charset=UTF-8
46968b6
Content-Transfer-Encoding: 8bit
46968b6
46968b6
Signed-off-by: Renaud M├ętrich <rmetrich@redhat.com>
46968b6
---
46968b6
 configure.ac          | 18 ++++++++++++++++++
46968b6
 grub-core/kern/misc.c | 20 ++++++++++++++++++++
46968b6
 config.h.in           |  1 +
46968b6
 3 files changed, 39 insertions(+)
46968b6
46968b6
diff --git a/configure.ac b/configure.ac
13985b0
index 907477a585c..d5d2a28b4ef 100644
46968b6
--- a/configure.ac
46968b6
+++ b/configure.ac
46968b6
@@ -1613,6 +1613,17 @@ else
46968b6
 fi
46968b6
 AC_SUBST([BOOT_TIME_STATS])
46968b6
 
46968b6
+AC_ARG_WITH([debug-timestamps],
46968b6
+	   AS_HELP_STRING([--with-debug-timestamps],
46968b6
+                          [prepend debug traces with absolute and relative timestamps]))
46968b6
+
46968b6
+if test x$with_debug_timestamps = xyes; then
46968b6
+  DEBUG_WITH_TIMESTAMPS=1
46968b6
+else
46968b6
+  DEBUG_WITH_TIMESTAMPS=0
46968b6
+fi
46968b6
+AC_SUBST([DEBUG_WITH_TIMESTAMPS])
46968b6
+
46968b6
 AC_ARG_ENABLE([grub-emu-sdl],
46968b6
 	      [AS_HELP_STRING([--enable-grub-emu-sdl],
46968b6
                              [build and install the `grub-emu' debugging utility with SDL support (default=guessed)])])
46968b6
@@ -2200,6 +2211,7 @@ AM_CONDITIONAL([COND_APPLE_LINKER], [test x$TARGET_APPLE_LINKER = x1])
46968b6
 AM_CONDITIONAL([COND_ENABLE_EFIEMU], [test x$enable_efiemu = xyes])
46968b6
 AM_CONDITIONAL([COND_ENABLE_CACHE_STATS], [test x$DISK_CACHE_STATS = x1])
46968b6
 AM_CONDITIONAL([COND_ENABLE_BOOT_TIME_STATS], [test x$BOOT_TIME_STATS = x1])
46968b6
+AM_CONDITIONAL([COND_DEBUG_WITH_TIMESTAMPS], [test x$DEBUG_WITH_TIMESTAMPS = x1])
46968b6
 
46968b6
 AM_CONDITIONAL([COND_HAVE_CXX], [test x$HAVE_CXX = xyes])
46968b6
 
46968b6
@@ -2295,6 +2307,12 @@ else
46968b6
 echo With boot time statistics: No
46968b6
 fi
46968b6
 
46968b6
+if [ x"$with_debug_timestamps" = xyes ]; then
46968b6
+echo Debug traces with timestamps: Yes
46968b6
+else
46968b6
+echo Debug traces with timestamps: No
46968b6
+fi
46968b6
+
46968b6
 if [ x"$efiemu_excuse" = x ]; then
46968b6
 echo efiemu runtime: Yes
46968b6
 else
46968b6
diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c
46968b6
index 578bf51a5fc..9f54b6b7d2d 100644
46968b6
--- a/grub-core/kern/misc.c
46968b6
+++ b/grub-core/kern/misc.c
46968b6
@@ -25,6 +25,9 @@
46968b6
 #include <grub/env.h>
46968b6
 #include <grub/i18n.h>
46968b6
 #include <grub/backtrace.h>
46968b6
+#if DEBUG_WITH_TIMESTAMPS
46968b6
+#include <grub/time.h>
46968b6
+#endif
46968b6
 
46968b6
 union printf_arg
46968b6
 {
46968b6
@@ -192,9 +195,26 @@ grub_real_dprintf (const char *file, const int line, const char *condition,
46968b6
 		   const char *fmt, ...)
46968b6
 {
46968b6
   va_list args;
46968b6
+#if DEBUG_WITH_TIMESTAMPS
46968b6
+  static long unsigned int last_time = 0;
46968b6
+  static int last_had_cr = 1;
46968b6
+#endif
46968b6
 
46968b6
   if (grub_debug_enabled (condition))
46968b6
     {
46968b6
+#if DEBUG_WITH_TIMESTAMPS
46968b6
+      /* Don't print timestamp if last printed message isn't terminated yet */
46968b6
+      if (last_had_cr) {
46968b6
+        long unsigned int tmabs = (long unsigned int) grub_get_time_ms();
46968b6
+        long unsigned int tmrel = tmabs - last_time;
46968b6
+        last_time = tmabs;
46968b6
+        grub_printf ("%3lu.%03lus +%2lu.%03lus ", tmabs / 1000, tmabs % 1000, tmrel / 1000, tmrel % 1000);
46968b6
+      }
46968b6
+      if (fmt[grub_strlen(fmt)-1] == '\n')
46968b6
+        last_had_cr = 1;
46968b6
+      else
46968b6
+        last_had_cr = 0;
46968b6
+#endif
46968b6
       grub_printf ("%s:%d: ", file, line);
46968b6
       va_start (args, fmt);
46968b6
       grub_vprintf (fmt, args);
46968b6
diff --git a/config.h.in b/config.h.in
46968b6
index c7e316f0f1f..c80e3e0aba3 100644
46968b6
--- a/config.h.in
46968b6
+++ b/config.h.in
46968b6
@@ -12,6 +12,7 @@
46968b6
 /* Define to 1 to enable disk cache statistics.  */
46968b6
 #define DISK_CACHE_STATS @DISK_CACHE_STATS@
46968b6
 #define BOOT_TIME_STATS @BOOT_TIME_STATS@
46968b6
+#define DEBUG_WITH_TIMESTAMPS @DEBUG_WITH_TIMESTAMPS@
46968b6
 
46968b6
 /* We don't need those.  */
46968b6
 #define MINILZO_CFG_SKIP_LZO_PTR 1