Home | History | Annotate | Line # | Download | only in dmesg
dmesg.c revision 1.33.2.1
      1  1.33.2.1  christos /*	$NetBSD: dmesg.c,v 1.33.2.1 2019/06/10 22:05:32 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.33.2.1  christos __RCSID("$NetBSD: dmesg.c,v 1.33.2.1 2019/06/10 22:05:32 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.33.2.1  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.33.2.1  christos #include <langinfo.h>
     63  1.33.2.1  christos #include <locale.h>
     64  1.33.2.1  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.33.2.1  christos static const char *radix;
     72  1.33.2.1  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.33.2.1  christos 
     78  1.33.2.1  christos static const char *
     79  1.33.2.1  christos fmtydhmsf(char *b, size_t l, intmax_t t, long nsec, int ht)
     80  1.33.2.1  christos {
     81  1.33.2.1  christos 	intmax_t s, m, h;
     82  1.33.2.1  christos 	int z;
     83  1.33.2.1  christos 	int prec;
     84  1.33.2.1  christos 	size_t o;
     85  1.33.2.1  christos 
     86  1.33.2.1  christos 	s = t % 60;
     87  1.33.2.1  christos 	t /= 60;
     88  1.33.2.1  christos 
     89  1.33.2.1  christos 	m = t % 60;
     90  1.33.2.1  christos 	t /= 60;
     91  1.33.2.1  christos 
     92  1.33.2.1  christos 	h = t;
     93  1.33.2.1  christos 
     94  1.33.2.1  christos 	z = 0;
     95  1.33.2.1  christos 	o = 0;
     96  1.33.2.1  christos 
     97  1.33.2.1  christos #define APPENDFMT(fmt, ...)  \
     98  1.33.2.1  christos     do { \
     99  1.33.2.1  christos 	    z = snprintf(b + o, l - o, fmt, __VA_ARGS__); \
    100  1.33.2.1  christos 	    if (z == -1) \
    101  1.33.2.1  christos 		    return b; \
    102  1.33.2.1  christos 	    o += (size_t)z; \
    103  1.33.2.1  christos 	    if (o >= l) \
    104  1.33.2.1  christos 		    return b; \
    105  1.33.2.1  christos     } while (/*CONSTCOND*/0)
    106  1.33.2.1  christos 
    107  1.33.2.1  christos #define APPEND(a) \
    108  1.33.2.1  christos     do if (a) \
    109  1.33.2.1  christos 	APPENDFMT("%jd%c", a, toupper((unsigned char)__STRING(a)[0])); \
    110  1.33.2.1  christos     while (/*CONSTCOND*/0)
    111  1.33.2.1  christos #define APPENDS(a, pr, ms) \
    112  1.33.2.1  christos     APPENDFMT("%jd%s%.*ld%c", a, radix, pr, ms, \
    113  1.33.2.1  christos 	toupper((unsigned char)__STRING(a)[0]))
    114  1.33.2.1  christos 
    115  1.33.2.1  christos 	APPENDFMT("%s", "P");
    116  1.33.2.1  christos 	APPENDFMT("%s", "T");
    117  1.33.2.1  christos 	APPEND(h);
    118  1.33.2.1  christos 	APPEND(m);
    119  1.33.2.1  christos 	if (nsec)
    120  1.33.2.1  christos 		nsec = (nsec + 500000) / 1000000;	/* now milliseconds */
    121  1.33.2.1  christos 	prec = 3;
    122  1.33.2.1  christos 	if (nsec && ht == 2) {
    123  1.33.2.1  christos 		while (prec > 0 && (nsec % 10) == 0)
    124  1.33.2.1  christos 			--prec, nsec /= 10;
    125  1.33.2.1  christos 	}
    126  1.33.2.1  christos 	if (nsec || ht > 2)
    127  1.33.2.1  christos 		APPENDS(s, prec, nsec);
    128  1.33.2.1  christos 	else
    129  1.33.2.1  christos 		APPEND(s);
    130  1.33.2.1  christos 	return b;
    131  1.33.2.1  christos }
    132  1.33.2.1  christos 
    133  1.33.2.1  christos static void
    134  1.33.2.1  christos pnsec(long nsec, long fsec, int scale)
    135  1.33.2.1  christos {
    136  1.33.2.1  christos 	if (scale > 6)
    137  1.33.2.1  christos 		printf("%6.6ld", (nsec + 499) / 1000);
    138  1.33.2.1  christos 	else
    139  1.33.2.1  christos 		printf("%*.*ld%.*s", scale, scale, fsec, 6 - scale, "000000");
    140  1.33.2.1  christos }
    141      1.24       dsl #endif
    142       1.6       cgd 
    143       1.6       cgd int
    144      1.18    simonb main(int argc, char *argv[])
    145       1.1       cgd {
    146      1.17    simonb 	struct kern_msgbuf cur;
    147      1.28  christos 	int ch, newl, log, i;
    148      1.28  christos 	size_t tstamp, size;
    149      1.20    simonb 	char *p, *bufdata;
    150      1.28  christos 	char buf[5];
    151      1.20    simonb #ifndef SMALL
    152      1.28  christos 	char tbuf[64];
    153      1.20    simonb 	char *memf, *nlistf;
    154  1.33.2.1  christos 	struct timespec boottime;
    155      1.31  christos 	struct timespec lasttime;
    156      1.31  christos 	intmax_t sec;
    157      1.33       kre 	long nsec, fsec;
    158      1.33       kre 	int scale;
    159      1.31  christos 	int deltas, quiet, humantime;
    160      1.33       kre 	bool frac;
    161  1.33.2.1  christos 
    162      1.28  christos 	static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
    163      1.28  christos 	size = sizeof(boottime);
    164      1.28  christos 
    165  1.33.2.1  christos 	(void)setlocale(LC_ALL, "");
    166  1.33.2.1  christos 	radix = nl_langinfo(RADIXCHAR);
    167  1.33.2.1  christos 	if (radix == NULL)
    168  1.33.2.1  christos 		radix = ".";	/* could also select "," */
    169  1.33.2.1  christos 
    170      1.28  christos 	boottime.tv_sec = 0;
    171  1.33.2.1  christos 	boottime.tv_nsec = 0;
    172      1.31  christos 	lasttime.tv_sec = 0;
    173      1.31  christos 	lasttime.tv_nsec = 0;
    174      1.31  christos 	deltas = quiet = humantime = 0;
    175      1.28  christos 
    176  1.33.2.1  christos 	(void)sysctl(bmib, 2, &boottime, &size, NULL, 0);
    177       1.1       cgd 
    178       1.6       cgd 	memf = nlistf = NULL;
    179      1.31  christos 	while ((ch = getopt(argc, argv, "dM:N:tT")) != -1)
    180       1.1       cgd 		switch(ch) {
    181      1.31  christos 		case 'd':
    182      1.31  christos 			deltas = 1;
    183      1.31  christos 			break;
    184       1.1       cgd 		case 'M':
    185       1.6       cgd 			memf = optarg;
    186       1.1       cgd 			break;
    187       1.1       cgd 		case 'N':
    188       1.6       cgd 			nlistf = optarg;
    189       1.1       cgd 			break;
    190      1.31  christos 		case 't':
    191      1.31  christos 			quiet = 1;
    192      1.28  christos 			break;
    193      1.31  christos 		case 'T':
    194  1.33.2.1  christos 			humantime++;
    195      1.28  christos 			break;
    196       1.1       cgd 		case '?':
    197       1.1       cgd 		default:
    198       1.1       cgd 			usage();
    199       1.1       cgd 		}
    200       1.1       cgd 	argc -= optind;
    201       1.1       cgd 	argv += optind;
    202      1.31  christos 	if (quiet && humantime)
    203      1.31  christos 		err(EXIT_FAILURE, "-t cannot be used with -T");
    204       1.1       cgd 
    205      1.19    simonb 	if (memf == NULL) {
    206      1.20    simonb #endif
    207      1.28  christos 		static const int mmib[2] = { CTL_KERN, KERN_MSGBUF };
    208      1.17    simonb 
    209      1.28  christos 		if (sysctl(mmib, 2, NULL, &size, NULL, 0) == -1 ||
    210      1.24       dsl 		    (bufdata = malloc(size)) == NULL ||
    211      1.28  christos 		    sysctl(mmib, 2, bufdata, &size, NULL, 0) == -1)
    212      1.17    simonb 			err(1, "can't get msgbuf");
    213      1.17    simonb 
    214      1.17    simonb 		/* make a dummy struct msgbuf for the display logic */
    215      1.24       dsl 		cur.msg_bufx = 0;
    216      1.24       dsl 		cur.msg_bufs = size;
    217      1.20    simonb #ifndef SMALL
    218      1.17    simonb 	} else {
    219      1.17    simonb 		kvm_t *kd;
    220      1.17    simonb 		struct kern_msgbuf *bufp;
    221      1.17    simonb 
    222      1.17    simonb 		/*
    223      1.17    simonb 		 * Read in message buffer header and data, and do sanity
    224      1.17    simonb 		 * checks.
    225      1.17    simonb 		 */
    226      1.17    simonb 		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
    227      1.17    simonb 		if (kd == NULL)
    228      1.17    simonb 			exit (1);
    229      1.17    simonb 		if (kvm_nlist(kd, nl) == -1)
    230      1.17    simonb 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    231      1.17    simonb 		if (nl[X_MSGBUF].n_type == 0)
    232      1.17    simonb 			errx(1, "%s: msgbufp not found", nlistf ? nlistf :
    233      1.17    simonb 			    "namelist");
    234      1.17    simonb 		if (KREAD(nl[X_MSGBUF].n_value, bufp))
    235      1.17    simonb 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    236      1.17    simonb 			    nl[X_MSGBUF].n_value);
    237      1.17    simonb 		if (kvm_read(kd, (long)bufp, &cur,
    238      1.17    simonb 		    offsetof(struct kern_msgbuf, msg_bufc)) !=
    239      1.17    simonb 		    offsetof(struct kern_msgbuf, msg_bufc))
    240      1.17    simonb 			errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
    241      1.17    simonb 			    (unsigned long)bufp);
    242      1.17    simonb 		if (cur.msg_magic != MSG_MAGIC)
    243      1.17    simonb 			errx(1, "magic number incorrect");
    244      1.17    simonb 		bufdata = malloc(cur.msg_bufs);
    245      1.17    simonb 		if (bufdata == NULL)
    246      1.17    simonb 			errx(1, "couldn't allocate space for buffer data");
    247      1.17    simonb 		if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
    248      1.17    simonb 		    cur.msg_bufs) != cur.msg_bufs)
    249      1.17    simonb 			errx(1, "kvm_read: %s", kvm_geterr(kd));
    250      1.17    simonb 		kvm_close(kd);
    251      1.17    simonb 		if (cur.msg_bufx >= cur.msg_bufs)
    252      1.17    simonb 			cur.msg_bufx = 0;
    253      1.17    simonb 	}
    254      1.20    simonb #endif
    255       1.1       cgd 
    256       1.1       cgd 	/*
    257      1.14     enami 	 * The message buffer is circular; start at the write pointer
    258      1.14     enami 	 * (which points the oldest character), and go to the write
    259      1.14     enami 	 * pointer - 1 (which points the newest character).  I.e, loop
    260      1.14     enami 	 * over cur.msg_bufs times.  Unused area is skipped since it
    261      1.14     enami 	 * contains nul.
    262       1.1       cgd 	 */
    263      1.33       kre #ifndef SMALL
    264      1.33       kre 	frac = false;
    265      1.33       kre 	scale = 0;
    266      1.33       kre #endif
    267      1.28  christos 	for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
    268      1.14     enami 	    i < cur.msg_bufs; i++, p++) {
    269      1.33       kre 
    270      1.24       dsl #ifndef SMALL
    271      1.12       leo 		if (p == bufdata + cur.msg_bufs)
    272      1.12       leo 			p = bufdata;
    273      1.28  christos #define ADDC(c)				\
    274      1.33       kre     do {				\
    275      1.28  christos 	if (tstamp < sizeof(tbuf) - 1)	\
    276      1.28  christos 		tbuf[tstamp++] = (c);	\
    277      1.33       kre 	if (frac)			\
    278      1.33       kre 		scale++;		\
    279      1.33       kre     } while (/*CONSTCOND*/0)
    280      1.28  christos #else
    281      1.28  christos #define ADDC(c)
    282      1.24       dsl #endif
    283       1.1       cgd 		ch = *p;
    284      1.30  christos 		if (ch == '\0')
    285      1.30  christos 			continue;
    286       1.1       cgd 		/* Skip "\n<.*>" syslog sequences. */
    287      1.28  christos 		/* Gather timestamp sequences */
    288      1.28  christos 		if (newl) {
    289      1.33       kre #ifndef SMALL
    290      1.33       kre 			int j;
    291      1.33       kre #endif
    292      1.33       kre 
    293      1.28  christos 			switch (ch) {
    294      1.33       kre #ifndef SMALL
    295  1.33.2.1  christos 			case '[':
    296      1.33       kre 				frac = false;
    297      1.33       kre 				scale = 0;
    298      1.28  christos 				ADDC(ch);
    299      1.28  christos 				continue;
    300  1.33.2.1  christos #endif
    301      1.28  christos 			case '<':
    302      1.28  christos 				log = 1;
    303      1.28  christos 				continue;
    304      1.28  christos 			case '>':
    305      1.29  christos 				log = 0;
    306      1.28  christos 				continue;
    307      1.33       kre #ifndef SMALL
    308  1.33.2.1  christos 			case ']':
    309      1.33       kre 				frac = false;
    310      1.28  christos 				ADDC(ch);
    311      1.28  christos 				ADDC('\0');
    312      1.28  christos 				tstamp = 0;
    313      1.33       kre 				sec = fsec = 0;
    314      1.33       kre 				switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
    315      1.33       kre 				case EOF:
    316      1.33       kre 				case 0:
    317      1.33       kre 					/*???*/
    318      1.33       kre 					continue;
    319      1.33       kre 				case 1:
    320      1.33       kre 					fsec = 0;
    321      1.33       kre 					break;
    322      1.33       kre 				case 2:
    323      1.33       kre 					break;
    324      1.33       kre 				default:
    325      1.33       kre 					/* Help */
    326      1.33       kre 					continue;
    327      1.33       kre 				}
    328      1.33       kre 
    329      1.33       kre 				for (nsec = fsec, j = 9 - scale; --j >= 0; )
    330      1.33       kre 					nsec *= 10;
    331      1.31  christos 				if (!quiet || deltas)
    332      1.31  christos 					printf("[");
    333  1.33.2.1  christos 				if (humantime == 1) {
    334      1.28  christos 					time_t t;
    335      1.28  christos 					struct tm tm;
    336      1.33       kre 
    337      1.28  christos 					t = boottime.tv_sec + sec;
    338  1.33.2.1  christos 					if (nsec + boottime.tv_nsec >=
    339  1.33.2.1  christos 					    ( 1L		/* 1 second */
    340  1.33.2.1  christos 						 * 1000L	/* ms */
    341  1.33.2.1  christos 						 * 1000L	/* us */
    342  1.33.2.1  christos 						 * 1000L	/* ns */ ))
    343  1.33.2.1  christos 							t++;
    344  1.33.2.1  christos 
    345      1.28  christos 					if (localtime_r(&t, &tm) != NULL) {
    346      1.28  christos 						strftime(tbuf, sizeof(tbuf),
    347      1.31  christos 						    "%a %b %e %H:%M:%S %Z %Y",
    348  1.33.2.1  christos 						    &tm);
    349      1.31  christos 						printf("%s", tbuf);
    350      1.28  christos 					}
    351  1.33.2.1  christos 				} else if (humantime > 1) {
    352  1.33.2.1  christos 					const char *fp = fmtydhmsf(tbuf,
    353  1.33.2.1  christos 					    sizeof(tbuf), sec, fsec, humantime);
    354  1.33.2.1  christos 					if (fp) {
    355  1.33.2.1  christos 						printf("%s", fp);
    356  1.33.2.1  christos 					}
    357      1.31  christos 				} else if (!quiet) {
    358  1.33.2.1  christos 					printf(" %5jd%s", sec, radix);
    359  1.33.2.1  christos 					pnsec(nsec, fsec, scale);
    360      1.31  christos 				}
    361      1.31  christos 				if (deltas) {
    362      1.31  christos 					struct timespec nt = { sec, nsec };
    363      1.31  christos 					struct timespec dt;
    364      1.33       kre 
    365      1.31  christos 					timespecsub(&nt, &lasttime, &dt);
    366      1.31  christos 					if (humantime || !quiet)
    367      1.31  christos 						printf(" ");
    368  1.33.2.1  christos 					printf("<% 4jd%s%6.6ld>",
    369  1.33.2.1  christos 					    (intmax_t)dt.tv_sec, radix,
    370  1.33.2.1  christos 					    (dt.tv_nsec+499) / 1000);
    371      1.31  christos 					lasttime = nt;
    372      1.31  christos 				}
    373      1.31  christos 				if (!quiet || deltas)
    374      1.31  christos 					printf("] ");
    375      1.28  christos 				continue;
    376  1.33.2.1  christos #endif
    377      1.28  christos 			case ' ':
    378      1.28  christos 				if (!tstamp)
    379  1.33.2.1  christos 					continue;
    380      1.28  christos 				/*FALLTHROUGH*/
    381      1.28  christos 			default:
    382  1.33.2.1  christos #ifndef SMALL
    383      1.28  christos 				if (tstamp) {
    384      1.28  christos 				    ADDC(ch);
    385      1.33       kre 				    if (ch == '.')
    386      1.33       kre 					frac = true;
    387      1.28  christos 				    continue;
    388      1.28  christos 				}
    389  1.33.2.1  christos #endif
    390      1.28  christos 				if (log)
    391      1.28  christos 					continue;
    392      1.28  christos 				break;
    393      1.28  christos 			}
    394       1.1       cgd 		}
    395       1.6       cgd 		newl = ch == '\n';
    396      1.21  augustss 		(void)vis(buf, ch, VIS_NOSLASH, 0);
    397      1.24       dsl #ifndef SMALL
    398       1.6       cgd 		if (buf[1] == 0)
    399       1.6       cgd 			(void)putchar(buf[0]);
    400       1.6       cgd 		else
    401      1.24       dsl #endif
    402       1.6       cgd 			(void)printf("%s", buf);
    403      1.12       leo 	}
    404       1.1       cgd 	if (!newl)
    405       1.1       cgd 		(void)putchar('\n');
    406      1.28  christos 	return EXIT_SUCCESS;
    407       1.1       cgd }
    408       1.1       cgd 
    409      1.24       dsl #ifndef SMALL
    410      1.27     joerg static void
    411      1.18    simonb usage(void)
    412       1.1       cgd {
    413      1.15     enami 
    414      1.32       wiz 	(void)fprintf(stderr, "Usage: %s [-dTt] [-M core] [-N system]\n",
    415      1.28  christos 		getprogname());
    416      1.28  christos 	exit(EXIT_FAILURE);
    417       1.1       cgd }
    418      1.24       dsl #endif
    419