077c9d
commit 35e3fbc4512c880fccb35b8e3abd132d4be18480
077c9d
Author: Florian Weimer <fweimer@redhat.com>
077c9d
Date:   Mon Nov 19 15:35:03 2018 +0100
077c9d
077c9d
    support: Print timestamps in timeout handler
077c9d
    
077c9d
    This is sometimes useful to determine if a test truly got stuck, or if
077c9d
    it was making progress (logging information to standard output) and
077c9d
    was merely slow to finish.
077c9d
077c9d
diff --git a/support/support_test_main.c b/support/support_test_main.c
077c9d
index 23429779aca85613..fa3c2e06dee5ae0f 100644
077c9d
--- a/support/support_test_main.c
077c9d
+++ b/support/support_test_main.c
077c9d
@@ -30,6 +30,7 @@
077c9d
 #include <string.h>
077c9d
 #include <sys/param.h>
077c9d
 #include <sys/resource.h>
077c9d
+#include <sys/time.h>
077c9d
 #include <sys/types.h>
077c9d
 #include <sys/wait.h>
077c9d
 #include <time.h>
077c9d
@@ -86,6 +87,19 @@ static pid_t test_pid;
077c9d
 /* The cleanup handler passed to test_main.  */
077c9d
 static void (*cleanup_function) (void);
077c9d
 
077c9d
+static void
077c9d
+print_timestamp (const char *what, struct timeval tv)
077c9d
+{
077c9d
+  struct tm tm;
077c9d
+  if (gmtime_r (&tv.tv_sec, &tm) == NULL)
077c9d
+    printf ("%s: %lld.%06d\n",
077c9d
+            what, (long long int) tv.tv_sec, (int) tv.tv_usec);
077c9d
+  else
077c9d
+    printf ("%s: %04d-%02d-%02dT%02d:%02d:%02d.%06d\n",
077c9d
+            what, 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
077c9d
+            tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec);
077c9d
+}
077c9d
+
077c9d
 /* Timeout handler.  We kill the child and exit with an error.  */
077c9d
 static void
077c9d
 __attribute__ ((noreturn))
077c9d
@@ -94,6 +108,13 @@ signal_handler (int sig)
077c9d
   int killed;
077c9d
   int status;
077c9d
 
077c9d
+  /* Do this first to avoid further interference from the
077c9d
+     subprocess.  */
077c9d
+  struct timeval now;
077c9d
+  bool now_available = gettimeofday (&now, NULL) == 0;
077c9d
+  struct stat64 st;
077c9d
+  bool st_available = fstat64 (STDOUT_FILENO, &st) == 0 && st.st_mtime != 0;
077c9d
+
077c9d
   assert (test_pid > 1);
077c9d
   /* Kill the whole process group.  */
077c9d
   kill (-test_pid, SIGKILL);
077c9d
@@ -144,6 +165,13 @@ signal_handler (int sig)
077c9d
     printf ("Timed out: killed the child process but it exited %d\n",
077c9d
             WEXITSTATUS (status));
077c9d
 
077c9d
+  if (now_available)
077c9d
+    print_timestamp ("Termination time", now);
077c9d
+  if (st_available)
077c9d
+    print_timestamp ("Last write to standard output",
077c9d
+                     (struct timeval) { st.st_mtim.tv_sec,
077c9d
+                         st.st_mtim.tv_nsec / 1000 });
077c9d
+
077c9d
   /* Exit with an error.  */
077c9d
   exit (1);
077c9d
 }