2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char copyright[] =
33 "@(#) Copyright (c) 1991, 1993\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static const char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
45 #include <sys/msgbuf.h>
46 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
64 static struct nlist nl[] = {
66 { "_msgbufp", 0, 0, 0, 0 },
70 void usage(void) __dead2;
72 #define KREAD(addr, var) \
73 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
76 main(int argc, char *argv[])
78 struct msgbuf *bufp, cur;
79 char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp;
81 size_t buflen, bufpos;
88 (void) setlocale(LC_CTYPE, "");
90 while ((ch = getopt(argc, argv, "acM:N:")) != -1)
114 * Running kernel. Use sysctl. This gives an unwrapped
115 * buffer as a side effect.
117 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
118 err(1, "sysctl kern.msgbuf");
119 if ((bp = malloc(buflen + 2)) == NULL)
120 errx(1, "malloc failed");
121 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
122 err(1, "sysctl kern.msgbuf");
124 if (sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof(int)))
125 err(1, "sysctl kern.msgbuf_clear");
127 /* Read in kernel message buffer and do sanity checks. */
128 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
131 if (kvm_nlist(kd, nl) == -1)
132 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
133 if (nl[X_MSGBUF].n_type == 0)
134 errx(1, "%s: msgbufp not found",
135 nlistf ? nlistf : "namelist");
136 if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur))
137 errx(1, "kvm_read: %s", kvm_geterr(kd));
138 if (cur.msg_magic != MSG_MAGIC)
139 errx(1, "kernel message buffer has different magic "
141 if ((bp = malloc(cur.msg_size + 2)) == NULL)
142 errx(1, "malloc failed");
144 /* Unwrap the circular buffer to start from the oldest data. */
145 bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq);
146 if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp,
147 cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos))
148 errx(1, "kvm_read: %s", kvm_geterr(kd));
149 if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr,
150 &bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos)
151 errx(1, "kvm_read: %s", kvm_geterr(kd));
153 buflen = cur.msg_size;
157 * Ensure that the buffer ends with a newline and a \0 to avoid
158 * complications below. We left space above.
160 if (buflen == 0 || bp[buflen - 1] != '\n')
164 if ((visbp = malloc(4 * buflen + 1)) == NULL)
165 errx(1, "malloc failed");
168 * The message buffer is circular, but has been unwrapped so that
169 * the oldest data comes first. The data will be preceded by \0's
170 * if the message buffer was not full.
175 /* Strip leading \0's */
179 /* Skip the first line, since it is probably incomplete. */
180 p = memchr(p, '\n', ep - p);
183 for (; p < ep; p = nextp) {
184 nextp = memchr(p, '\n', ep - p);
187 /* Skip ^<[0-9]+> syslog sequences. */
188 if (*p == '<' && isdigit(*(p+1))) {
190 pri = strtol(p + 1, &q, 10);
191 if (*q == '>' && pri >= 0 && pri < INT_MAX &&
193 if (LOG_FAC(pri) != LOG_KERN && !all)
199 (void)strvisx(visbp, p, nextp - p, 0);
200 (void)printf("%s", visbp);
208 fprintf(stderr, "usage: dmesg [-ac] [-M core [-N system]]\n");