1994-05-26 06:35:07 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-05-02 07:08:52 +00:00
|
|
|
#if 0
|
1994-05-26 06:35:07 +00:00
|
|
|
#ifndef lint
|
1996-09-21 08:11:22 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 06:35:07 +00:00
|
|
|
"@(#) Copyright (c) 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1996-09-21 08:11:22 +00:00
|
|
|
static const char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93";
|
1994-05-26 06:35:07 +00:00
|
|
|
#endif /* not lint */
|
2003-05-02 07:08:52 +00:00
|
|
|
#endif
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2001-07-03 19:49:31 +00:00
|
|
|
#include <sys/types.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <sys/msgbuf.h>
|
2001-07-03 19:49:31 +00:00
|
|
|
#include <sys/sysctl.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
|
1996-06-19 01:49:01 +00:00
|
|
|
#include <err.h>
|
2004-02-08 23:08:53 +00:00
|
|
|
#include <errno.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
2004-02-08 23:08:53 +00:00
|
|
|
#include <limits.h>
|
1996-09-21 08:11:22 +00:00
|
|
|
#include <locale.h>
|
2002-08-18 17:57:08 +00:00
|
|
|
#include <nlist.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-02-05 21:07:50 +00:00
|
|
|
#include <string.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <vis.h>
|
2001-02-26 08:09:51 +00:00
|
|
|
#include <sys/syslog.h>
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2005-01-17 13:56:46 +00:00
|
|
|
char s_msgbufp[] = "_msgbufp";
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
struct nlist nl[] = {
|
|
|
|
#define X_MSGBUF 0
|
2005-01-17 13:56:46 +00:00
|
|
|
{ s_msgbufp, 0, 0, 0, 0 },
|
|
|
|
{ NULL, 0, 0, 0, 0 },
|
1994-05-26 06:35:07 +00:00
|
|
|
};
|
|
|
|
|
2002-03-19 13:29:39 +00:00
|
|
|
void usage(void) __dead2;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
|
|
|
#define KREAD(addr, var) \
|
|
|
|
kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
|
|
|
|
|
|
|
|
int
|
2002-03-19 13:29:39 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
|
|
|
struct msgbuf *bufp, cur;
|
2004-02-08 23:08:53 +00:00
|
|
|
char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
|
1994-05-26 06:35:07 +00:00
|
|
|
kvm_t *kd;
|
2003-05-02 07:08:52 +00:00
|
|
|
size_t buflen, bufpos;
|
2004-02-08 23:08:53 +00:00
|
|
|
long pri;
|
|
|
|
int all, ch;
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-02-05 21:07:50 +00:00
|
|
|
all = 0;
|
1995-10-23 23:36:55 +00:00
|
|
|
(void) setlocale(LC_CTYPE, "");
|
1994-05-26 06:35:07 +00:00
|
|
|
memf = nlistf = NULL;
|
2000-12-17 19:05:17 +00:00
|
|
|
while ((ch = getopt(argc, argv, "aM:N:")) != -1)
|
1994-05-26 06:35:07 +00:00
|
|
|
switch(ch) {
|
2000-12-17 19:05:17 +00:00
|
|
|
case 'a':
|
|
|
|
all++;
|
|
|
|
break;
|
1994-05-26 06:35:07 +00:00
|
|
|
case 'M':
|
|
|
|
memf = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
nlistf = optarg;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
2004-10-13 20:33:18 +00:00
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-03-26 09:24:45 +00:00
|
|
|
if (memf == NULL) {
|
2004-02-08 23:08:53 +00:00
|
|
|
/*
|
|
|
|
* Running kernel. Use sysctl. This gives an unwrapped
|
|
|
|
* buffer as a side effect.
|
|
|
|
*/
|
2001-07-03 19:49:31 +00:00
|
|
|
if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
|
|
|
|
err(1, "sysctl kern.msgbuf");
|
2004-02-08 23:08:53 +00:00
|
|
|
if ((bp = malloc(buflen + 2)) == NULL)
|
2001-07-03 19:49:31 +00:00
|
|
|
errx(1, "malloc failed");
|
|
|
|
if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
|
|
|
|
err(1, "sysctl kern.msgbuf");
|
|
|
|
} else {
|
2004-02-08 23:08:53 +00:00
|
|
|
/* Read in kernel message buffer and do sanity checks. */
|
2001-07-03 19:49:31 +00:00
|
|
|
kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
|
|
|
|
if (kd == NULL)
|
|
|
|
exit (1);
|
|
|
|
if (kvm_nlist(kd, nl) == -1)
|
|
|
|
errx(1, "kvm_nlist: %s", kvm_geterr(kd));
|
|
|
|
if (nl[X_MSGBUF].n_type == 0)
|
|
|
|
errx(1, "%s: msgbufp not found",
|
|
|
|
nlistf ? nlistf : "namelist");
|
|
|
|
if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
|
|
|
|
errx(1, "kvm_read: %s", kvm_geterr(kd));
|
|
|
|
if (cur.msg_magic != MSG_MAGIC)
|
|
|
|
errx(1, "kernel message buffer has different magic "
|
|
|
|
"number");
|
2004-02-08 23:08:53 +00:00
|
|
|
if ((bp = malloc(cur.msg_size + 2)) == NULL)
|
2001-07-03 19:49:31 +00:00
|
|
|
errx(1, "malloc failed");
|
2004-02-08 23:08:53 +00:00
|
|
|
|
2004-02-05 21:07:50 +00:00
|
|
|
/* Unwrap the circular buffer to start from the oldest data. */
|
|
|
|
bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
|
|
|
|
if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
|
2005-01-17 13:56:46 +00:00
|
|
|
cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos))
|
2004-02-05 21:07:50 +00:00
|
|
|
errx(1, "kvm_read: %s", kvm_geterr(kd));
|
|
|
|
if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
|
2005-01-17 13:56:46 +00:00
|
|
|
&bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos)
|
2001-07-03 19:49:31 +00:00
|
|
|
errx(1, "kvm_read: %s", kvm_geterr(kd));
|
|
|
|
kvm_close(kd);
|
|
|
|
buflen = cur.msg_size;
|
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
|
2004-02-08 23:08:53 +00:00
|
|
|
/*
|
|
|
|
* Ensure that the buffer ends with a newline and a \0 to avoid
|
|
|
|
* complications below. We left space above.
|
|
|
|
*/
|
|
|
|
if (buflen == 0 || bp[buflen - 1] != '\n')
|
|
|
|
bp[buflen++] = '\n';
|
|
|
|
bp[buflen] = '\0';
|
|
|
|
|
|
|
|
if ((visbp = malloc(4 * buflen + 1)) == NULL)
|
|
|
|
errx(1, "malloc failed");
|
|
|
|
|
1994-05-26 06:35:07 +00:00
|
|
|
/*
|
2004-02-05 21:07:50 +00:00
|
|
|
* The message buffer is circular, but has been unwrapped so that
|
|
|
|
* the oldest data comes first. The data will be preceded by \0's
|
|
|
|
* if the message buffer was not full.
|
1994-05-26 06:35:07 +00:00
|
|
|
*/
|
2004-02-05 21:07:50 +00:00
|
|
|
p = bp;
|
|
|
|
ep = &bp[buflen];
|
|
|
|
if (*p == '\0') {
|
|
|
|
/* Strip leading \0's */
|
2004-02-08 23:08:53 +00:00
|
|
|
while (*p == '\0')
|
2004-02-05 21:07:50 +00:00
|
|
|
p++;
|
|
|
|
} else if (!all) {
|
|
|
|
/* Skip the first line, since it is probably incomplete. */
|
2004-10-03 15:48:32 +00:00
|
|
|
p = memchr(p, '\n', ep - p);
|
|
|
|
p++;
|
2004-02-05 21:07:50 +00:00
|
|
|
}
|
2004-02-08 23:08:53 +00:00
|
|
|
for (; p < ep; p = nextp) {
|
2004-10-03 15:48:32 +00:00
|
|
|
nextp = memchr(p, '\n', ep - p);
|
|
|
|
nextp++;
|
2004-02-08 23:08:53 +00:00
|
|
|
|
2004-02-05 21:07:50 +00:00
|
|
|
/* Skip ^<[0-9]+> syslog sequences. */
|
|
|
|
if (*p == '<') {
|
2004-02-08 23:08:53 +00:00
|
|
|
errno = 0;
|
|
|
|
pri = strtol(p + 1, &q, 10);
|
|
|
|
if (*q == '>' && pri >= 0 && pri < INT_MAX &&
|
|
|
|
errno == 0) {
|
2004-02-05 21:07:50 +00:00
|
|
|
if (LOG_FAC(pri) != LOG_KERN && !all)
|
|
|
|
continue;
|
|
|
|
p = q + 1;
|
2000-12-17 19:05:17 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
}
|
2004-02-08 23:08:53 +00:00
|
|
|
|
|
|
|
(void)strvisx(visbp, p, nextp - p, 0);
|
|
|
|
(void)printf("%s", visbp);
|
2004-02-05 21:07:50 +00:00
|
|
|
}
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-19 13:29:39 +00:00
|
|
|
usage(void)
|
1994-05-26 06:35:07 +00:00
|
|
|
{
|
2004-03-26 09:24:45 +00:00
|
|
|
(void)fprintf(stderr, "usage: dmesg [-a] [-M core [-N system]]\n");
|
1994-05-26 06:35:07 +00:00
|
|
|
exit(1);
|
|
|
|
}
|