kernel - Refactor the kernel message buffer code
[dragonfly.git] / sbin / dmesg / dmesg.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
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
27 * SUCH DAMAGE.
1de703da
MD
28 *
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 $
984263bc
MD
32 */
33
984263bc
MD
34#include <sys/types.h>
35#include <sys/msgbuf.h>
36#include <sys/sysctl.h>
37
38#include <err.h>
39#include <fcntl.h>
40#include <kvm.h>
41#include <locale.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <unistd.h>
45#include <vis.h>
46#include <sys/syslog.h>
47
48struct nlist nl[] = {
49#define X_MSGBUF 0
f44d99cd
CP
50 { "_msgbufp", 0, 0, 0, 0 },
51 { NULL, 0, 0, 0, 0 },
984263bc
MD
52};
53
d08d23b2
MD
54static void dumpbuf(char *bp, size_t bufpos, size_t buflen,
55 int *newl, int *skip, int *pri);
9a76cd75 56void usage(void);
984263bc
MD
57
58#define KREAD(addr, var) \
59 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
60
d08d23b2
MD
61#define INCRBUFSIZE 65536
62
63int all_opt;
64
984263bc 65int
b5744197 66main(int argc, char **argv)
984263bc 67{
d08d23b2 68 int newl, skip;
984263bc
MD
69 struct msgbuf *bufp, cur;
70 char *bp, *memf, *nlistf;
71 kvm_t *kd;
d08d23b2 72 int ch;
f5c1f6e0 73 int clear = 0;
8af0d623 74 int pri = 0;
d08d23b2 75 int tailmode = 0;
a53014fb 76 int kno;
f44d99cd 77 size_t buflen, bufpos;
984263bc 78
7cee7052 79 setlocale(LC_CTYPE, "");
984263bc 80 memf = nlistf = NULL;
a53014fb 81 while ((ch = getopt(argc, argv, "acfM:N:n:")) != -1) {
984263bc
MD
82 switch(ch) {
83 case 'a':
d08d23b2 84 all_opt++;
984263bc 85 break;
f5c1f6e0
EA
86 case 'c':
87 clear = 1;
88 break;
d08d23b2 89 case 'f':
595fda35 90 ++tailmode;
d08d23b2 91 break;
984263bc
MD
92 case 'M':
93 memf = optarg;
94 break;
95 case 'N':
96 nlistf = optarg;
97 break;
a53014fb
MD
98 case 'n':
99 kno = strtol(optarg, NULL, 0);
100 asprintf(&memf, "/var/crash/vmcore.%d", kno);
101 asprintf(&nlistf, "/var/crash/kern.%d", kno);
102 break;
984263bc
MD
103 case '?':
104 default:
105 usage();
106 }
d08d23b2 107 }
984263bc
MD
108 argc -= optind;
109 argv += optind;
110
d08d23b2
MD
111 newl = 0;
112 skip = 0;
113 pri = 0;
114
115 if (memf == NULL && nlistf == NULL && tailmode == 0) {
984263bc 116 /* Running kernel. Use sysctl. */
5608ef17 117 buflen = 0;
984263bc
MD
118 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
119 err(1, "sysctl kern.msgbuf");
120 if ((bp = malloc(buflen)) == NULL)
121 errx(1, "malloc failed");
122 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
123 err(1, "sysctl kern.msgbuf");
124 /* We get a dewrapped buffer using sysctl. */
125 bufpos = 0;
d08d23b2 126 dumpbuf(bp, bufpos, buflen, &newl, &skip, &pri);
984263bc 127 } else {
5608ef17
MD
128 u_int rindex;
129 u_int xindex;
130 u_int ri;
131 u_int xi;
132 u_int n;
133
984263bc
MD
134 /* Read in kernel message buffer, do sanity checks. */
135 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
136 if (kd == NULL)
137 exit (1);
138 if (kvm_nlist(kd, nl) == -1)
139 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
140 if (nl[X_MSGBUF].n_type == 0)
141 errx(1, "%s: msgbufp not found",
142 nlistf ? nlistf : "namelist");
d08d23b2
MD
143 bp = malloc(INCRBUFSIZE);
144 if (!bp)
145 errx(1, "malloc failed");
146 if (KREAD(nl[X_MSGBUF].n_value, bufp))
147 errx(1, "kvm_read: %s", kvm_geterr(kd));
148 if (KREAD((long)bufp, cur))
984263bc 149 errx(1, "kvm_read: %s", kvm_geterr(kd));
5608ef17 150 if (cur.msg_magic != MSG_MAGIC && cur.msg_magic != MSG_OMAGIC)
984263bc
MD
151 errx(1, "kernel message buffer has different magic "
152 "number");
d08d23b2
MD
153
154 /*
5608ef17
MD
155 * NOTE: current algorithm is compatible with both old and
156 * new msgbuf structures. The new structure doesn't
157 * modulo the indexes (so we do), and adds a separate
158 * log index which we don't access here.
d08d23b2 159 */
5608ef17 160
d08d23b2 161 rindex = cur.msg_bufr;
d08d23b2
MD
162
163 for (;;) {
5608ef17
MD
164 /*
165 * Calculate index for dump and do sanity clipping.
166 */
167 xindex = cur.msg_bufx;
168 n = xindex - rindex;
169 if (n > cur.msg_size - 1024) {
170 rindex = xindex - cur.msg_size + 2048;
171 n = xindex - rindex;
172 }
173 ri = rindex % cur.msg_size;
174 xi = xindex % cur.msg_size;
175
176 if (ri < xi)
177 buflen = xi - ri;
d08d23b2 178 else
5608ef17
MD
179 buflen = cur.msg_size - ri;
180 if (buflen > n)
181 buflen = n;
d08d23b2
MD
182 if (buflen > INCRBUFSIZE)
183 buflen = INCRBUFSIZE;
5608ef17
MD
184
185 if (kvm_read(kd, (long)cur.msg_ptr + ri,
d08d23b2
MD
186 bp, buflen) != (ssize_t)buflen) {
187 errx(1, "kvm_read: %s", kvm_geterr(kd));
188 }
189 if (buflen)
190 dumpbuf(bp, 0, buflen, &newl, &skip, &pri);
5608ef17
MD
191 ri = (ri + buflen) % cur.msg_size;
192 n = n - buflen;
d08d23b2 193 rindex += buflen;
5608ef17 194 if ((int)n <= 0) {
d08d23b2
MD
195 if (tailmode == 0)
196 break;
80f63443 197 fflush(stdout);
595fda35
MD
198 if (tailmode == 1)
199 sleep(1);
d08d23b2
MD
200 }
201 if (KREAD((long)bufp, cur))
202 errx(1, "kvm_read: %s", kvm_geterr(kd));
203 }
984263bc 204 kvm_close(kd);
984263bc 205 }
d08d23b2
MD
206 if (!newl)
207 putchar('\n');
208 if (clear) {
209 if (sysctlbyname("kern.msgbuf_clear", NULL, NULL,
210 &clear, sizeof(int)) != 0) {
211 err(1, "sysctl kern.msgbuf_clear");
212 }
213 }
214 return(0);
215}
216
217static
218void
219dumpbuf(char *bp, size_t bufpos, size_t buflen,
220 int *newl, int *skip, int *pri)
221{
222 int ch;
223 char *p, *ep;
224 char buf[5];
984263bc
MD
225
226 /*
227 * The message buffer is circular. If the buffer has wrapped, the
228 * write pointer points to the oldest data. Otherwise, the write
229 * pointer points to \0's following the data. Read the entire
230 * buffer starting at the write pointer and ignore nulls so that
231 * we effectively start at the oldest data.
232 */
233 p = bp + bufpos;
234 ep = (bufpos == 0 ? bp + buflen : p);
984263bc
MD
235 do {
236 if (p == bp + buflen)
237 p = bp;
238 ch = *p;
239 /* Skip "\n<.*>" syslog sequences. */
d08d23b2 240 if (*skip) {
984263bc 241 if (ch == '\n') {
d08d23b2
MD
242 *skip = 0;
243 *newl = 1;
984263bc 244 } if (ch == '>') {
5608ef17 245 if (LOG_FAC(*pri) == LOG_KERN)
d08d23b2 246 *newl = *skip = 0;
984263bc 247 } else if (ch >= '0' && ch <= '9') {
d08d23b2
MD
248 *pri *= 10;
249 *pri += ch - '0';
984263bc
MD
250 }
251 continue;
252 }
5608ef17 253 if (*newl && ch == '<' && all_opt == 0) {
d08d23b2
MD
254 *pri = 0;
255 *skip = 1;
984263bc
MD
256 continue;
257 }
258 if (ch == '\0')
259 continue;
5608ef17 260 *newl = (ch == '\n');
7cee7052 261 vis(buf, ch, 0, 0);
984263bc 262 if (buf[1] == 0)
7cee7052 263 putchar(buf[0]);
984263bc 264 else
7cee7052 265 printf("%s", buf);
984263bc 266 } while (++p != ep);
984263bc
MD
267}
268
269void
b5744197 270usage(void)
984263bc 271{
f5c1f6e0 272 fprintf(stderr, "usage: dmesg [-ac] [-M core] [-N system]\n");
984263bc
MD
273 exit(1);
274}