mirror of
https://git.FreeBSD.org/src.git
synced 2026-06-02 11:24:32 +00:00
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
This commit is contained in:
committed by
Pouria Mousavizadeh Tehrani
parent
1569e2334a
commit
a723308555
+15
-1
@@ -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
|
||||
|
||||
+65
-5
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user