Home | History | Annotate | Line # | Download | only in dmesg
dmesg.c revision 1.37
      1  1.37  christos /*	$NetBSD: dmesg.c,v 1.37 2018/09/19 23:02:14 christos Exp $	*/
      2   1.1       cgd /*-
      3   1.6       cgd  * Copyright (c) 1991, 1993
      4   1.6       cgd  *	The Regents of the University of California.  All rights reserved.
      5   1.1       cgd  *
      6   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      7   1.1       cgd  * modification, are permitted provided that the following conditions
      8   1.1       cgd  * are met:
      9   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     10   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     11   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     13   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     14  1.22       agc  * 3. Neither the name of the University nor the names of its contributors
     15   1.1       cgd  *    may be used to endorse or promote products derived from this software
     16   1.1       cgd  *    without specific prior written permission.
     17   1.1       cgd  *
     18   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     19   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     22   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28   1.1       cgd  * SUCH DAMAGE.
     29   1.1       cgd  */
     30   1.1       cgd 
     31  1.11     lukem #include <sys/cdefs.h>
     32   1.1       cgd #ifndef lint
     33  1.26     lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
     34  1.26     lukem  The Regents of the University of California.  All rights reserved.");
     35   1.1       cgd #endif /* not lint */
     36   1.1       cgd 
     37   1.1       cgd #ifndef lint
     38   1.8       cgd #if 0
     39   1.6       cgd static char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
     40   1.8       cgd #else
     41  1.37  christos __RCSID("$NetBSD: dmesg.c,v 1.37 2018/09/19 23:02:14 christos Exp $");
     42   1.8       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45  1.17    simonb #include <sys/param.h>
     46   1.1       cgd #include <sys/msgbuf.h>
     47  1.17    simonb #include <sys/sysctl.h>
     48   1.6       cgd 
     49   1.7       cgd #include <err.h>
     50  1.34  christos #include <ctype.h>
     51   1.6       cgd #include <fcntl.h>
     52  1.28  christos #include <time.h>
     53   1.6       cgd #include <kvm.h>
     54   1.1       cgd #include <nlist.h>
     55   1.6       cgd #include <stdio.h>
     56  1.17    simonb #include <stddef.h>
     57   1.1       cgd #include <stdlib.h>
     58   1.5       jtc #include <unistd.h>
     59   1.6       cgd #include <vis.h>
     60   1.1       cgd 
     61  1.20    simonb #ifndef SMALL
     62  1.37  christos #include <langinfo.h>
     63  1.37  christos #include <locale.h>
     64  1.37  christos 
     65  1.27     joerg static struct nlist nl[] = {
     66   1.6       cgd #define	X_MSGBUF	0
     67  1.25  christos 	{ .n_name = "_msgbufp" },
     68  1.25  christos 	{ .n_name = NULL },
     69   1.1       cgd };
     70   1.1       cgd 
     71  1.37  christos static const char *radix;
     72  1.37  christos 
     73  1.27     joerg __dead static void	usage(void);
     74   1.1       cgd 
     75   1.6       cgd #define	KREAD(addr, var) \
     76   1.6       cgd 	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
     77  1.34  christos 
     78  1.34  christos static const char *
     79  1.36       kre fmtydhmsf(char *b, size_t l, intmax_t t, long nsec, int ht)
     80  1.34  christos {
     81  1.37  christos 	intmax_t s, m, h
     82  1.37  christos #if 0
     83  1.37  christos 			, d, M, y
     84  1.37  christos #endif
     85  1.34  christos 	int z;
     86  1.35       kre 	int prec;
     87  1.34  christos 	size_t o;
     88  1.34  christos 
     89  1.34  christos 	s = t % 60;
     90  1.34  christos 	t /= 60;
     91  1.34  christos 
     92  1.34  christos 	m = t % 60;
     93  1.34  christos 	t /= 60;
     94  1.34  christos 
     95  1.37  christos #if 0
     96  1.37  christos 	/*
     97  1.37  christos 	 * This is wrong for 2 reasons, first, months do not all
     98  1.37  christos 	 * have 30 days (and yes, it matters, I think) and because when
     99  1.37  christos 	 * summer time begins(ends) "1 day" is 23 (25) hours, not 24.
    100  1.37  christos 	 *
    101  1.37  christos 	 * We would need to convert to localtime, before and after,
    102  1.37  christos 	 * and subtract in localtime format, then normalise, to produce
    103  1.37  christos 	 * a meaningfuly yYmMdD string to use here.   So just stick to
    104  1.37  christos 	 * a count of hours.  It is legit, and easy...
    105  1.37  christos 	 */
    106  1.34  christos 	h = t % 24;
    107  1.34  christos 	t /= 24;
    108  1.34  christos 
    109  1.34  christos 	d = t % 30;
    110  1.34  christos 	t /= 30;
    111  1.34  christos 
    112  1.34  christos 	M = t % 12;
    113  1.34  christos 	t /= 12;
    114  1.34  christos 
    115  1.34  christos 	y = t;
    116  1.37  christos #else
    117  1.37  christos 	h = t;
    118  1.37  christos #endif
    119  1.34  christos 
    120  1.34  christos 	z = 0;
    121  1.34  christos 	o = 0;
    122  1.34  christos 
    123  1.34  christos #define APPENDFMT(fmt, ...)  \
    124  1.34  christos     do { \
    125  1.34  christos 	    z = snprintf(b + o, l - o, fmt, __VA_ARGS__); \
    126  1.34  christos 	    if (z == -1) \
    127  1.34  christos 		    return b; \
    128  1.34  christos 	    o += (size_t)z; \
    129  1.34  christos 	    if (o >= l) \
    130  1.34  christos 		    return b; \
    131  1.34  christos     } while (/*CONSTCOND*/0)
    132  1.34  christos 
    133  1.34  christos #define APPEND(a) \
    134  1.34  christos     do if (a) \
    135  1.35       kre         APPENDFMT("%jd%c", a, toupper((unsigned char)__STRING(a)[0])); \
    136  1.34  christos     while (/*CONSTCOND*/0)
    137  1.35       kre #define APPENDS(a, pr, ms) \
    138  1.35       kre     APPENDFMT("%jd%s%.*ld%c", a, radix, pr, ms, \
    139  1.34  christos 	toupper((unsigned char)__STRING(a)[0]))
    140  1.34  christos 
    141  1.34  christos 	APPENDFMT("%s", "P");
    142  1.37  christos #if 0
    143  1.34  christos 	APPEND(y);
    144  1.34  christos 	APPEND(M);
    145  1.34  christos 	APPEND(d);
    146  1.37  christos #endif
    147  1.34  christos 	APPENDFMT("%s", "T");
    148  1.34  christos 	APPEND(h);
    149  1.34  christos 	APPEND(m);
    150  1.34  christos 	if (nsec)
    151  1.35       kre 		nsec = (nsec + 500000) / 1000000;	/* now milliseconds */
    152  1.35       kre 	prec = 3;
    153  1.36       kre 	if (nsec && ht == 2) {
    154  1.36       kre 		while (prec > 0 && (nsec % 10) == 0)
    155  1.36       kre 			--prec, nsec /= 10;
    156  1.36       kre 	}
    157  1.36       kre 	if (nsec || ht > 2)
    158  1.35       kre 		APPENDS(s, prec, nsec);
    159  1.34  christos 	else
    160  1.34  christos 		APPEND(s);
    161  1.34  christos 	return b;
    162  1.34  christos }
    163  1.34  christos 
    164  1.34  christos static void
    165  1.34  christos pnsec(long nsec, long fsec, int scale)
    166  1.34  christos {
    167  1.34  christos 	if (scale > 6)
    168  1.34  christos 		printf("%6.6ld", (nsec + 499) / 1000);
    169  1.34  christos 	else
    170  1.34  christos 		printf("%*.*ld%.*s", scale, scale, fsec, 6 - scale, "000000");
    171  1.34  christos }
    172  1.24       dsl #endif
    173   1.6       cgd 
    174   1.6       cgd int
    175  1.18    simonb main(int argc, char *argv[])
    176   1.1       cgd {
    177  1.17    simonb 	struct kern_msgbuf cur;
    178  1.28  christos 	int ch, newl, log, i;
    179  1.28  christos 	size_t tstamp, size;
    180  1.20    simonb 	char *p, *bufdata;
    181  1.28  christos 	char buf[5];
    182  1.20    simonb #ifndef SMALL
    183  1.28  christos 	char tbuf[64];
    184  1.20    simonb 	char *memf, *nlistf;
    185  1.28  christos 	struct timeval boottime;
    186  1.31  christos 	struct timespec lasttime;
    187  1.31  christos 	intmax_t sec;
    188  1.33       kre 	long nsec, fsec;
    189  1.33       kre 	int scale;
    190  1.31  christos 	int deltas, quiet, humantime;
    191  1.33       kre 	bool frac;
    192  1.28  christos 
    193  1.28  christos 	static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
    194  1.28  christos 	size = sizeof(boottime);
    195  1.28  christos 
    196  1.37  christos 	(void)setlocale(LC_ALL, "");
    197  1.37  christos 	radix = nl_langinfo(RADIXCHAR);
    198  1.37  christos 	if (radix == NULL)
    199  1.37  christos 		radix = ".";	/* could also select "," */
    200  1.37  christos 
    201  1.28  christos 	boottime.tv_sec = 0;
    202  1.28  christos 	boottime.tv_usec = 0;
    203  1.31  christos 	lasttime.tv_sec = 0;
    204  1.31  christos 	lasttime.tv_nsec = 0;
    205  1.31  christos 	deltas = quiet = humantime = 0;
    206  1.28  christos 
    207  1.28  christos         (void)sysctl(bmib, 2, &boottime, &size, NULL, 0);
    208   1.1       cgd 
    209   1.6       cgd 	memf = nlistf = NULL;
    210  1.31  christos 	while ((ch = getopt(argc, argv, "dM:N:tT")) != -1)
    211   1.1       cgd 		switch(ch) {
    212  1.31  christos 		case 'd':
    213  1.31  christos 			deltas = 1;
    214  1.31  christos 			break;
    215   1.1       cgd 		case 'M':
    216   1.6       cgd 			memf = optarg;
    217   1.1       cgd 			break;
    218   1.1       cgd 		case 'N':
    219   1.6       cgd 			nlistf = optarg;
    220   1.1       cgd 			break;
    221  1.31  christos 		case 't':
    222  1.31  christos 			quiet = 1;
    223  1.28  christos 			break;
    224  1.31  christos 		case 'T':
    225  1.34  christos 			humantime++;
    226  1.28  christos 			break;
    227   1.1       cgd 		case '?':
    228   1.1       cgd 		default:
    229   1.1       cgd 			usage();
    230   1.1       cgd 		}
    231   1.1       cgd 	argc -= optind;
    232   1.1       cgd 	argv += optind;
    233  1.31  christos 	if (quiet && humantime)
    234  1.31  christos 		err(EXIT_FAILURE, "-t cannot be used with -T");
    235   1.1       cgd 
    236  1.19    simonb 	if (memf == NULL) {
    237  1.20    simonb #endif
    238  1.28  christos 		static const int mmib[2] = { CTL_KERN, KERN_MSGBUF };
    239  1.17    simonb 
    240  1.28  christos 		if (sysctl(mmib, 2, NULL, &size, NULL, 0) == -1 ||
    241  1.24       dsl 		    (bufdata = malloc(size)) == NULL ||
    242  1.28  christos 		    sysctl(mmib, 2, bufdata, &size, NULL, 0) == -1)
    243  1.17    simonb 			err(1, "can't get msgbuf");
    244  1.17    simonb 
    245  1.17    simonb 		/* make a dummy struct msgbuf for the display logic */
    246  1.24       dsl 		cur.msg_bufx = 0;
    247  1.24       dsl 		cur.msg_bufs = size;
    248  1.20    simonb #ifndef SMALL
    249  1.17    simonb 	} else {
    250  1.17    simonb 		kvm_t *kd;
    251  1.17    simonb 		struct kern_msgbuf *bufp;
    252  1.17    simonb 
    253  1.17    simonb 		/*
    254  1.17    simonb 		 * Read in message buffer header and data, and do sanity
    255  1.17    simonb 		 * checks.
    256  1.17    simonb 		 */
    257  1.17    simonb 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
    258  1.17    simonb 		if (kd == NULL)
    259  1.17    simonb 			exit (1);
    260  1.17    simonb 		if (kvm_nlist(kd, nl) == -1)
    261  1.17    simonb 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    262  1.17    simonb 		if (nl[X_MSGBUF].n_type == 0)
    263  1.17    simonb 			errx(1, "%s: msgbufp not found", nlistf ? nlistf :
    264  1.17    simonb 			    "namelist");
    265  1.17    simonb 		if (KREAD(nl[X_MSGBUF].n_value, bufp))
    266  1.17    simonb 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    267  1.17    simonb 			    nl[X_MSGBUF].n_value);
    268  1.17    simonb 		if (kvm_read(kd, (long)bufp, &cur,
    269  1.17    simonb 		    offsetof(struct kern_msgbuf, msg_bufc)) !=
    270  1.17    simonb 		    offsetof(struct kern_msgbuf, msg_bufc))
    271  1.17    simonb 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    272  1.17    simonb 			    (unsigned long)bufp);
    273  1.17    simonb 		if (cur.msg_magic != MSG_MAGIC)
    274  1.17    simonb 			errx(1, "magic number incorrect");
    275  1.17    simonb 		bufdata = malloc(cur.msg_bufs);
    276  1.17    simonb 		if (bufdata == NULL)
    277  1.17    simonb 			errx(1, "couldn't allocate space for buffer data");
    278  1.17    simonb 		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
    279  1.17    simonb 		    cur.msg_bufs) != cur.msg_bufs)
    280  1.17    simonb 			errx(1, "kvm_read: %s", kvm_geterr(kd));
    281  1.17    simonb 		kvm_close(kd);
    282  1.17    simonb 		if (cur.msg_bufx >= cur.msg_bufs)
    283  1.17    simonb 			cur.msg_bufx = 0;
    284  1.17    simonb 	}
    285  1.20    simonb #endif
    286   1.1       cgd 
    287   1.1       cgd 	/*
    288  1.14     enami 	 * The message buffer is circular; start at the write pointer
    289  1.14     enami 	 * (which points the oldest character), and go to the write
    290  1.14     enami 	 * pointer - 1 (which points the newest character).  I.e, loop
    291  1.14     enami 	 * over cur.msg_bufs times.  Unused area is skipped since it
    292  1.14     enami 	 * contains nul.
    293   1.1       cgd 	 */
    294  1.33       kre #ifndef SMALL
    295  1.33       kre 	frac = false;
    296  1.33       kre 	scale = 0;
    297  1.33       kre #endif
    298  1.28  christos 	for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
    299  1.14     enami 	    i < cur.msg_bufs; i++, p++) {
    300  1.33       kre 
    301  1.24       dsl #ifndef SMALL
    302  1.12       leo 		if (p == bufdata + cur.msg_bufs)
    303  1.12       leo 			p = bufdata;
    304  1.28  christos #define ADDC(c)				\
    305  1.33       kre     do {				\
    306  1.28  christos 	if (tstamp < sizeof(tbuf) - 1)	\
    307  1.28  christos 		tbuf[tstamp++] = (c);	\
    308  1.33       kre 	if (frac)			\
    309  1.33       kre 		scale++;		\
    310  1.33       kre     } while (/*CONSTCOND*/0)
    311  1.28  christos #else
    312  1.28  christos #define ADDC(c)
    313  1.24       dsl #endif
    314   1.1       cgd 		ch = *p;
    315  1.30  christos 		if (ch == '\0')
    316  1.30  christos 			continue;
    317   1.1       cgd 		/* Skip "\n<.*>" syslog sequences. */
    318  1.28  christos 		/* Gather timestamp sequences */
    319  1.28  christos 		if (newl) {
    320  1.33       kre #ifndef SMALL
    321  1.33       kre 			int j;
    322  1.33       kre #endif
    323  1.33       kre 
    324  1.28  christos 			switch (ch) {
    325  1.28  christos 			case '[':
    326  1.33       kre #ifndef SMALL
    327  1.33       kre 				frac = false;
    328  1.33       kre 				scale = 0;
    329  1.33       kre #endif
    330  1.28  christos 				ADDC(ch);
    331  1.28  christos 				continue;
    332  1.28  christos 			case '<':
    333  1.28  christos 				log = 1;
    334  1.28  christos 				continue;
    335  1.28  christos 			case '>':
    336  1.29  christos 				log = 0;
    337  1.28  christos 				continue;
    338  1.28  christos 			case ']':
    339  1.33       kre #ifndef SMALL
    340  1.33       kre 				frac = false;
    341  1.33       kre #endif
    342  1.28  christos 				ADDC(ch);
    343  1.28  christos 				ADDC('\0');
    344  1.28  christos 				tstamp = 0;
    345  1.28  christos #ifndef SMALL
    346  1.33       kre 				sec = fsec = 0;
    347  1.33       kre 				switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
    348  1.33       kre 				case EOF:
    349  1.33       kre 				case 0:
    350  1.33       kre 					/*???*/
    351  1.33       kre 					continue;
    352  1.33       kre 				case 1:
    353  1.33       kre 					fsec = 0;
    354  1.33       kre 					break;
    355  1.33       kre 				case 2:
    356  1.33       kre 					break;
    357  1.33       kre 				default:
    358  1.33       kre 					/* Help */
    359  1.33       kre 					continue;
    360  1.33       kre 				}
    361  1.33       kre 
    362  1.33       kre 				for (nsec = fsec, j = 9 - scale; --j >= 0; )
    363  1.33       kre 					nsec *= 10;
    364  1.31  christos 				if (!quiet || deltas)
    365  1.31  christos 					printf("[");
    366  1.34  christos 				if (humantime == 1) {
    367  1.28  christos 					time_t t;
    368  1.28  christos 					struct tm tm;
    369  1.33       kre 
    370  1.28  christos 					t = boottime.tv_sec + sec;
    371  1.28  christos 					if (localtime_r(&t, &tm) != NULL) {
    372  1.28  christos 						strftime(tbuf, sizeof(tbuf),
    373  1.31  christos 						    "%a %b %e %H:%M:%S %Z %Y",
    374  1.28  christos 						     &tm);
    375  1.31  christos 						printf("%s", tbuf);
    376  1.28  christos 					}
    377  1.34  christos 				} else if (humantime > 1) {
    378  1.34  christos 					const char *fp = fmtydhmsf(tbuf,
    379  1.36       kre 					    sizeof(tbuf), sec, fsec, humantime);
    380  1.34  christos 					if (fp) {
    381  1.34  christos 						printf("%s", fp);
    382  1.34  christos 					}
    383  1.31  christos 				} else if (!quiet) {
    384  1.34  christos 					printf(" %5jd.", sec);
    385  1.34  christos 					pnsec(nsec, fsec, scale);
    386  1.31  christos 				}
    387  1.31  christos 				if (deltas) {
    388  1.31  christos 					struct timespec nt = { sec, nsec };
    389  1.31  christos 					struct timespec dt;
    390  1.33       kre 
    391  1.31  christos 					timespecsub(&nt, &lasttime, &dt);
    392  1.31  christos 					if (humantime || !quiet)
    393  1.31  christos 						printf(" ");
    394  1.31  christos 					printf("<% 4jd.%06ld>", (intmax_t)
    395  1.33       kre 					    dt.tv_sec, (dt.tv_nsec+499) / 1000);
    396  1.31  christos 					lasttime = nt;
    397  1.31  christos 				}
    398  1.31  christos 				if (!quiet || deltas)
    399  1.31  christos 					printf("] ");
    400  1.28  christos #endif
    401  1.28  christos 				continue;
    402  1.28  christos 			case ' ':
    403  1.28  christos 				if (!tstamp)
    404  1.28  christos 					continue;
    405  1.28  christos 				/*FALLTHROUGH*/
    406  1.28  christos 			default:
    407  1.28  christos 				if (tstamp) {
    408  1.28  christos 				    ADDC(ch);
    409  1.33       kre #ifndef SMALL
    410  1.33       kre 				    if (ch == '.')
    411  1.33       kre 					frac = true;
    412  1.33       kre #endif
    413  1.28  christos 				    continue;
    414  1.28  christos 				}
    415  1.28  christos 				if (log)
    416  1.28  christos 					continue;
    417  1.28  christos 				break;
    418  1.28  christos 			}
    419  1.29  christos 			newl = 0;
    420   1.1       cgd 		}
    421   1.6       cgd 		newl = ch == '\n';
    422  1.21  augustss 		(void)vis(buf, ch, VIS_NOSLASH, 0);
    423  1.24       dsl #ifndef SMALL
    424   1.6       cgd 		if (buf[1] == 0)
    425   1.6       cgd 			(void)putchar(buf[0]);
    426   1.6       cgd 		else
    427  1.24       dsl #endif
    428   1.6       cgd 			(void)printf("%s", buf);
    429  1.12       leo 	}
    430   1.1       cgd 	if (!newl)
    431   1.1       cgd 		(void)putchar('\n');
    432  1.28  christos 	return EXIT_SUCCESS;
    433   1.1       cgd }
    434   1.1       cgd 
    435  1.24       dsl #ifndef SMALL
    436  1.27     joerg static void
    437  1.18    simonb usage(void)
    438   1.1       cgd {
    439  1.15     enami 
    440  1.32       wiz 	(void)fprintf(stderr, "Usage: %s [-dTt] [-M core] [-N system]\n",
    441  1.28  christos 		getprogname());
    442  1.28  christos 	exit(EXIT_FAILURE);
    443   1.1       cgd }
    444  1.24       dsl #endif
    445