11558Srgrimes/*-
21558Srgrimes * Copyright (c) 1991, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114515Sobrien#if 0
311558Srgrimes#ifndef lint
3218439Sbdestatic const char copyright[] =
331558Srgrimes"@(#) Copyright (c) 1991, 1993\n\
341558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351558Srgrimes#endif /* not lint */
361558Srgrimes
371558Srgrimes#ifndef lint
3818439Sbdestatic const char sccsid[] = "@(#)dmesg.c	8.1 (Berkeley) 6/5/93";
39114515Sobrien#endif /* not lint */
4018439Sbde#endif
41114515Sobrien#include <sys/cdefs.h>
42114515Sobrien__FBSDID("$FreeBSD$");
431558Srgrimes
4479154Stmm#include <sys/types.h>
451558Srgrimes#include <sys/msgbuf.h>
4679154Stmm#include <sys/sysctl.h>
471558Srgrimes
48156076Sdwmalone#include <ctype.h>
4916497Salex#include <err.h>
50125610Siedowse#include <errno.h>
511558Srgrimes#include <fcntl.h>
521558Srgrimes#include <kvm.h>
53125610Siedowse#include <limits.h>
5418439Sbde#include <locale.h>
55102069Sbde#include <nlist.h>
561558Srgrimes#include <stdio.h>
571558Srgrimes#include <stdlib.h>
58125497Siedowse#include <string.h>
591558Srgrimes#include <unistd.h>
601558Srgrimes#include <vis.h>
6173076Sru#include <sys/syslog.h>
621558Srgrimes
63140383Sdelphijchar s_msgbufp[] = "_msgbufp";
64140383Sdelphij
651558Srgrimesstruct nlist nl[] = {
661558Srgrimes#define	X_MSGBUF	0
67140383Sdelphij	{ s_msgbufp, 0, 0, 0, 0 },
68140383Sdelphij	{ NULL, 0, 0, 0, 0 },
691558Srgrimes};
701558Srgrimes
7192697Simpvoid usage(void) __dead2;
721558Srgrimes
731558Srgrimes#define	KREAD(addr, var) \
741558Srgrimes	kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
751558Srgrimes
761558Srgrimesint
7792697Simpmain(int argc, char *argv[])
781558Srgrimes{
791558Srgrimes	struct msgbuf *bufp, cur;
80125610Siedowse	char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
811558Srgrimes	kvm_t *kd;
82125497Siedowse	size_t buflen, bufpos;
83125610Siedowse	long pri;
84125610Siedowse	int all, ch;
851558Srgrimes
86125497Siedowse	all = 0;
8711749Sache	(void) setlocale(LC_CTYPE, "");
881558Srgrimes	memf = nlistf = NULL;
8970123Sphk	while ((ch = getopt(argc, argv, "aM:N:")) != -1)
901558Srgrimes		switch(ch) {
9170123Sphk		case 'a':
9270123Sphk			all++;
9370123Sphk			break;
941558Srgrimes		case 'M':
951558Srgrimes			memf = optarg;
961558Srgrimes			break;
971558Srgrimes		case 'N':
981558Srgrimes			nlistf = optarg;
991558Srgrimes			break;
1001558Srgrimes		case '?':
1011558Srgrimes		default:
1021558Srgrimes			usage();
1031558Srgrimes		}
1041558Srgrimes	argc -= optind;
105136491Sschweikh	if (argc != 0)
106136491Sschweikh		usage();
1071558Srgrimes
108127448Sru	if (memf == NULL) {
109125610Siedowse		/*
110125610Siedowse		 * Running kernel.  Use sysctl.  This gives an unwrapped
111125610Siedowse		 * buffer as a side effect.
112125610Siedowse		 */
11379154Stmm		if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
11479154Stmm			err(1, "sysctl kern.msgbuf");
115125610Siedowse		if ((bp = malloc(buflen + 2)) == NULL)
11679154Stmm			errx(1, "malloc failed");
11779154Stmm		if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
11879154Stmm			err(1, "sysctl kern.msgbuf");
11979154Stmm	} else {
120125610Siedowse		/* Read in kernel message buffer and do sanity checks. */
12179154Stmm		kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
12279154Stmm		if (kd == NULL)
12379154Stmm			exit (1);
12479154Stmm		if (kvm_nlist(kd, nl) == -1)
12579154Stmm			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
12679154Stmm		if (nl[X_MSGBUF].n_type == 0)
12779154Stmm			errx(1, "%s: msgbufp not found",
12879154Stmm			    nlistf ? nlistf : "namelist");
12979154Stmm		if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
13079154Stmm			errx(1, "kvm_read: %s", kvm_geterr(kd));
13179154Stmm		if (cur.msg_magic != MSG_MAGIC)
13279154Stmm			errx(1, "kernel message buffer has different magic "
13379154Stmm			    "number");
134125610Siedowse		if ((bp = malloc(cur.msg_size + 2)) == NULL)
13579154Stmm			errx(1, "malloc failed");
136125610Siedowse
137125497Siedowse		/* Unwrap the circular buffer to start from the oldest data. */
138125497Siedowse		bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
139125497Siedowse		if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
140140383Sdelphij		    cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos))
14179154Stmm			errx(1, "kvm_read: %s", kvm_geterr(kd));
142125497Siedowse		if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
143140383Sdelphij		    &bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos)
144125497Siedowse			errx(1, "kvm_read: %s", kvm_geterr(kd));
14579154Stmm		kvm_close(kd);
14679154Stmm		buflen = cur.msg_size;
14779154Stmm	}
1481558Srgrimes
1491558Srgrimes	/*
150125610Siedowse	 * Ensure that the buffer ends with a newline and a \0 to avoid
151125610Siedowse	 * complications below.  We left space above.
152125610Siedowse	 */
153125610Siedowse	if (buflen == 0 || bp[buflen - 1] != '\n')
154125610Siedowse		bp[buflen++] = '\n';
155125610Siedowse	bp[buflen] = '\0';
156125610Siedowse
157125610Siedowse	if ((visbp = malloc(4 * buflen + 1)) == NULL)
158125610Siedowse		errx(1, "malloc failed");
159125610Siedowse
160125610Siedowse	/*
161125497Siedowse	 * The message buffer is circular, but has been unwrapped so that
162125497Siedowse	 * the oldest data comes first.  The data will be preceded by \0's
163125497Siedowse	 * if the message buffer was not full.
1641558Srgrimes	 */
165125497Siedowse	p = bp;
166125497Siedowse	ep = &bp[buflen];
167125497Siedowse	if (*p == '\0') {
168125497Siedowse		/* Strip leading \0's */
169125610Siedowse		while (*p == '\0')
170125497Siedowse			p++;
171125497Siedowse	} else if (!all) {
172125497Siedowse		/* Skip the first line, since it is probably incomplete. */
173136092Sstefanf		p = memchr(p, '\n', ep - p);
174136092Sstefanf		p++;
175125497Siedowse	}
176125610Siedowse	for (; p < ep; p = nextp) {
177136092Sstefanf		nextp = memchr(p, '\n', ep - p);
178136092Sstefanf		nextp++;
179125610Siedowse
180125497Siedowse		/* Skip ^<[0-9]+> syslog sequences. */
181156076Sdwmalone		if (*p == '<' && isdigit(*(p+1))) {
182125610Siedowse			errno = 0;
183125610Siedowse			pri = strtol(p + 1, &q, 10);
184125610Siedowse			if (*q == '>' && pri >= 0 && pri < INT_MAX &&
185125610Siedowse			    errno == 0) {
186125497Siedowse				if (LOG_FAC(pri) != LOG_KERN && !all)
187125497Siedowse					continue;
188125497Siedowse				p = q + 1;
18970123Sphk			}
1901558Srgrimes		}
191125610Siedowse
192125610Siedowse		(void)strvisx(visbp, p, nextp - p, 0);
193125610Siedowse		(void)printf("%s", visbp);
194125497Siedowse	}
1951558Srgrimes	exit(0);
1961558Srgrimes}
1971558Srgrimes
1981558Srgrimesvoid
19992697Simpusage(void)
2001558Srgrimes{
201127448Sru	(void)fprintf(stderr, "usage: dmesg [-a] [-M core [-N system]]\n");
2021558Srgrimes	exit(1);
2031558Srgrimes}
204