Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Apr 2026 18:15:05 +0000
From:      Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Cc:        Andre Albsmeier <mail@ghub.e4m.org>
Subject:   git: a7233085558d - main - dmesg(8): Add -t and -f options for converting timestamps
Message-ID:  <69f39c29.3601d.2b01cf39@gitrepo.freebsd.org>

index | next in thread | raw e-mail

The branch main has been updated by pouria:

URL: https://cgit.FreeBSD.org/src/commit/?id=a7233085558db6d2ee0251891a85a7e74dddcb7e

commit a7233085558db6d2ee0251891a85a7e74dddcb7e
Author:     Andre Albsmeier <mail@ghub.e4m.org>
AuthorDate: 2026-01-23 11:10:18 +0000
Commit:     Pouria Mousavizadeh Tehrani <pouria@FreeBSD.org>
CommitDate: 2026-04-30 18:13:37 +0000

    dmesg(8): Add -t and -f options for converting timestamps
    
    Kernel timestamps are relative to kern.boottime.
    With -t, kern.boottime is added and converted to either
    a default format or the one specified using the -f option.
    
    Signed-off-by:  Andre Albsmeier <mail@ghub.e4m.org>
    Reviewed by:    kib, pouria
    Discussed with: imp
    Pull-Request:   https://github.com/freebsd/freebsd-src/pull/1985
---
 sbin/dmesg/dmesg.8 | 16 ++++++++++++-
 sbin/dmesg/dmesg.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 80 insertions(+), 6 deletions(-)

diff --git a/sbin/dmesg/dmesg.8 b/sbin/dmesg/dmesg.8
index d84587b61475..d153f5b71cea 100644
--- a/sbin/dmesg/dmesg.8
+++ b/sbin/dmesg/dmesg.8
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd March 7, 2026
+.Dd April 28, 2026
 .Dt DMESG 8
 .Os
 .Sh NAME
@@ -34,6 +34,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl ac
+.Op Fl t Op Fl f Ar output_fmt
 .Op Fl M Ar core Op Fl N Ar system
 .Sh DESCRIPTION
 The
@@ -58,6 +59,19 @@ This includes any syslog records and
 output.
 .It Fl c
 Clear the kernel buffer after printing.
+.It Fl t
+Convert timestamps (see
+.Sx SYSCTL VARIABLES
+below) to an absolute date and time.
+.It Fl f
+If
+.Fl t
+is also specified,
+use the specified
+.Ar output_fmt
+for the conversion (see
+.Xr strftime 3
+manual page).
 .It Fl M
 Extract values associated with the name list from the specified core.
 .It Fl N
diff --git a/sbin/dmesg/dmesg.c b/sbin/dmesg/dmesg.c
index 65005a903154..f94465a55182 100644
--- a/sbin/dmesg/dmesg.c
+++ b/sbin/dmesg/dmesg.c
@@ -32,6 +32,8 @@
 #include <sys/types.h>
 #include <sys/msgbuf.h>
 #include <sys/sysctl.h>
+#include <sys/syslog.h>
+#include <sys/time.h>
 
 #include <ctype.h>
 #include <err.h>
@@ -47,7 +49,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <vis.h>
-#include <sys/syslog.h>
 
 static struct nlist nl[] = {
 #define	X_MSGBUF	0
@@ -64,18 +65,22 @@ int
 main(int argc, char *argv[])
 {
 	struct msgbuf *bufp, cur;
+	struct timeval boottime, reltime, abstime;
+	char timebuf[1024];
 	char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
+	const char *timefmt = "%d %b %T";
 	kvm_t *kd;
 	size_t buflen, bufpos;
 	long pri;
 	int ch, clear;
-	bool all;
+	bool all, timeconv;
 
 	all = false;
 	clear = false;
+	timeconv = false;
 	(void) setlocale(LC_CTYPE, "");
 	memf = nlistf = NULL;
-	while ((ch = getopt(argc, argv, "acM:N:")) != -1)
+	while ((ch = getopt(argc, argv, "actM:N:f:")) != -1)
 		switch(ch) {
 		case 'a':
 			all = true;
@@ -83,12 +88,18 @@ main(int argc, char *argv[])
 		case 'c':
 			clear = true;
 			break;
+		case 't':
+			timeconv = true;
+			break;
 		case 'M':
 			memf = optarg;
 			break;
 		case 'N':
 			nlistf = optarg;
 			break;
+		case 'f':
+			timefmt = optarg;
+			break;
 		case '?':
 		default:
 			usage();
@@ -97,6 +108,14 @@ main(int argc, char *argv[])
 	if (argc != 0)
 		usage();
 
+	if (timeconv) {
+		int mib[2] = {CTL_KERN, KERN_BOOTTIME};
+
+		size_t l = sizeof(boottime);
+		if (sysctl(mib, 2, &boottime, &l, 0, 0) < 0)
+			err(1, "sysctl kern.boottime");
+        }
+
 	if (memf == NULL) {
 		/*
 		 * Running kernel.  Use sysctl.  This gives an unwrapped buffer
@@ -188,7 +207,48 @@ main(int argc, char *argv[])
 		}
 
 		(void)strvisx(visbp, p, nextp - p, 0);
-		(void)printf("%s", visbp);
+		if (!timeconv) {
+			printf("%s", visbp);
+			continue;
+		}
+
+		if (visbp[0] != '[') {
+			printf("%s", visbp);
+			continue;
+		}
+
+		reltime.tv_usec = 0;
+		errno = 0;
+		reltime.tv_sec = strtoul(visbp + 1, &q, 10);
+		if (errno != 0) {
+			printf("%s", visbp);
+			continue;
+		}
+
+		if (*q == '.') {
+			errno = 0;
+			reltime.tv_usec = strtof(q, &q) * 1000000.0;
+			if (errno != 0) {
+				printf("%s", visbp);
+				continue;
+			}
+		}
+
+		if (*q != ']' || q[1] != ' ') {
+			printf("%s", visbp);
+			continue;
+		}
+		q++;
+
+		timeradd(&boottime, &reltime, &abstime);
+
+		if (strftime(timebuf, sizeof timebuf, timefmt,
+		    localtime(&abstime.tv_sec)) != 0) {
+			printf("[%s]%s", timebuf, q);
+		} else {
+			printf("%s", visbp);
+			continue;
+		}
 	}
 	exit(0);
 }
@@ -196,6 +256,6 @@ main(int argc, char *argv[])
 void
 usage(void)
 {
-	fprintf(stderr, "usage: dmesg [-ac] [-M core [-N system]]\n");
+	fprintf(stderr, "usage: dmesg [-ac] [-t [-f output_fmt]] [-M core [-N system]]\n");
 	exit(1);
 }


home | help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?69f39c29.3601d.2b01cf39>