From nobody Thu Apr 30 18:15:05 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4g62QQ2Vrkz6byvW for ; Thu, 30 Apr 2026 18:15:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R13" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4g62QQ1WhSz40P2 for ; Thu, 30 Apr 2026 18:15:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1777572910; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dpNHNimbzSocpRUchTRa+NejjyCwPVR2+0cDiwOdI94=; b=mENYIQk0zw2b5b0urkXBMj6zZ2BOqed+Z54a8q8Flif/niTjJTDa3fW3j/vMfvGzlrA2w5 HvBz36sFiHZ5EfUQ5GQdMJB8vjB646xOwYvSR0NBkvnhYP87ULQTsNOmVoOxMTrnxEO6E/ 3HV3U+G8KWdiZ5qrWn+2/43jqkhM+7ov9Ru5gBlyCfI93FAayXBqf16+gtCpt15hHJjXkl RwXamjOVQuEykRPvTJoWXZ5c8In7MNk3VVXYofMBE1lGVpzoEpwLoQfg+IqnJvZP+rYNag XMPudf6R0s3seLBY99d7Z7Uv5/gi3xfDyi7LNjP9ORLE0unPNN9HnfHsRozoiA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1777572910; a=rsa-sha256; cv=none; b=XMog6oouCwkgqPSg7eakFzqL9H30TH7w/l+obIYRfijTuQ6MGcyXL2D8V0fvseBVz0Blwm hh3eA5ZZdZWJIBIWGRNIZ59tyg+rsD1R+NMk3L7aejcAtohM4KWHvY1QS1bntQLAOYlCBj ta3au7bCTylXeSLp2yFXk5hWiUgDFES/gSJE1EIVFcfDclfVjJxYg8tzAH44OVp/xjp9T5 z/ywdBGl3HkYIZIT17V4eNGqsrEO9kAcujBvUL8OuVKjtOWpFVLTrT1d3dcbQ2LOKcL5hk Lyd42pgqOvQI3j2InKERtjzEJGWHswqWXmmulQGU6uPOvZDz5v0sLJt/4xsOZA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1777572910; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=dpNHNimbzSocpRUchTRa+NejjyCwPVR2+0cDiwOdI94=; b=NpCozP4DIsSB5e3rDcV34PAzL/E6+WWidxu5sXfTpjHstw+7lWut8E3D87O/YSgN8KSQbk 8j9Ckrtow2h29pIPvOwsdlMTU3CgfeMbucbwxvX/4NTyNFtbDsoZt/+zTX/kLhfmcOVqJQ mW3nX+KJm7giTOIx1WdEavHA6DpteOS8KgapY4YuTY86sCnHSMmsa01pXVcwgT7BnFYGnH xrHlYGkAacz5MpeDpwt400bfNgb7AYFhKwGXmFRG9KtBgjE+3qpoZu1eIbt4l9mVw1MeLb eOR1WP3hRcqvphJHN+OHrFSsi0udbuVuX825dC1tmuPH6L2QEY4nKpBismSLAg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4g62QQ0vswz7Y2 for ; Thu, 30 Apr 2026 18:15:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3601d by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Thu, 30 Apr 2026 18:15:05 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Andre Albsmeier From: Pouria Mousavizadeh Tehrani Subject: git: a7233085558d - main - dmesg(8): Add -t and -f options for converting timestamps List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pouria X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a7233085558db6d2ee0251891a85a7e74dddcb7e Auto-Submitted: auto-generated Date: Thu, 30 Apr 2026 18:15:05 +0000 Message-Id: <69f39c29.3601d.2b01cf39@gitrepo.freebsd.org> The branch main has been updated by pouria: URL: https://cgit.FreeBSD.org/src/commit/?id=a7233085558db6d2ee0251891a85a7e74dddcb7e commit a7233085558db6d2ee0251891a85a7e74dddcb7e Author: Andre Albsmeier AuthorDate: 2026-01-23 11:10:18 +0000 Commit: Pouria Mousavizadeh Tehrani 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 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 #include #include +#include +#include #include #include @@ -47,7 +49,6 @@ #include #include #include -#include 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); }