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 * 3. 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
29 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)dmesg.c 8.1 (Berkeley) 6/5/93
31 * $FreeBSD: src/sbin/dmesg/dmesg.c,v 1.11.2.3 2001/08/08 22:32:15 obrien Exp $
34 #include <sys/types.h>
35 #include <sys/msgbuf.h>
36 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
50 { "_msgbufp", 0, 0, 0, 0 },
54 static void dumpbuf(char *bp, size_t bufpos, size_t buflen,
55 int *newl, int *skip, int *pri);
58 #define KREAD(addr, var) \
59 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
61 #define INCRBUFSIZE 65536
66 main(int argc, char **argv)
69 struct msgbuf *bufp, cur;
70 char *bp, *memf, *nlistf;
77 size_t buflen, bufpos;
79 setlocale(LC_CTYPE, "");
81 while ((ch = getopt(argc, argv, "acfM:N:")) != -1) {
110 if (memf == NULL && nlistf == NULL && tailmode == 0) {
111 /* Running kernel. Use sysctl. */
112 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
113 err(1, "sysctl kern.msgbuf");
114 buflen += 4096; /* add some slop */
115 if ((bp = malloc(buflen)) == NULL)
116 errx(1, "malloc failed");
117 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
118 err(1, "sysctl kern.msgbuf");
119 /* We get a dewrapped buffer using sysctl. */
121 dumpbuf(bp, bufpos, buflen, &newl, &skip, &pri);
123 /* Read in kernel message buffer, do sanity checks. */
124 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
127 if (kvm_nlist(kd, nl) == -1)
128 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
129 if (nl[X_MSGBUF].n_type == 0)
130 errx(1, "%s: msgbufp not found",
131 nlistf ? nlistf : "namelist");
132 bp = malloc(INCRBUFSIZE);
134 errx(1, "malloc failed");
135 if (KREAD(nl[X_MSGBUF].n_value, bufp))
136 errx(1, "kvm_read: %s", kvm_geterr(kd));
137 if (KREAD((long)bufp, cur))
138 errx(1, "kvm_read: %s", kvm_geterr(kd));
139 if (cur.msg_magic != MSG_MAGIC)
140 errx(1, "kernel message buffer has different magic "
144 * Start point. Use rindex == bufx as our end-of-buffer
145 * indication but for the initial rindex from the kernel
146 * this means the buffer has cycled and is 100% full.
148 rindex = cur.msg_bufr;
149 if (rindex == cur.msg_bufx) {
150 if (++rindex >= cur.msg_size)
155 if (cur.msg_bufx >= rindex)
156 buflen = cur.msg_bufx - rindex;
158 buflen = cur.msg_size - rindex;
159 if (buflen > INCRBUFSIZE)
160 buflen = INCRBUFSIZE;
161 if (kvm_read(kd, (long)cur.msg_ptr + rindex,
162 bp, buflen) != (ssize_t)buflen) {
163 errx(1, "kvm_read: %s", kvm_geterr(kd));
166 dumpbuf(bp, 0, buflen, &newl, &skip, &pri);
168 if (rindex >= cur.msg_size)
170 if (rindex == cur.msg_bufx) {
177 if (KREAD((long)bufp, cur))
178 errx(1, "kvm_read: %s", kvm_geterr(kd));
185 if (sysctlbyname("kern.msgbuf_clear", NULL, NULL,
186 &clear, sizeof(int)) != 0) {
187 err(1, "sysctl kern.msgbuf_clear");
195 dumpbuf(char *bp, size_t bufpos, size_t buflen,
196 int *newl, int *skip, int *pri)
203 * The message buffer is circular. If the buffer has wrapped, the
204 * write pointer points to the oldest data. Otherwise, the write
205 * pointer points to \0's following the data. Read the entire
206 * buffer starting at the write pointer and ignore nulls so that
207 * we effectively start at the oldest data.
210 ep = (bufpos == 0 ? bp + buflen : p);
212 if (p == bp + buflen)
215 /* Skip "\n<.*>" syslog sequences. */
221 if (LOG_FAC(*pri) == LOG_KERN || all_opt)
223 } else if (ch >= '0' && ch <= '9') {
229 if (*newl && ch == '<') {
248 fprintf(stderr, "usage: dmesg [-ac] [-M core] [-N system]\n");