7b5ede72951111434fb32ead2df23cb62c63fe86
[dragonfly.git] / sbin / dmesg / dmesg.c
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.
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.
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.
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 $
32  */
33
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
48 struct nlist nl[] = {
49 #define X_MSGBUF        0
50         { "_msgbufp",   0, 0, 0, 0 },
51         { NULL,         0, 0, 0, 0 },
52 };
53
54 static void dumpbuf(char *bp, size_t bufpos, size_t buflen,
55                     int *newl, int *skip, int *pri);
56 void usage(void);
57
58 #define KREAD(addr, var) \
59         kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
60
61 #define INCRBUFSIZE     65536
62
63 int all_opt;
64
65 int
66 main(int argc, char **argv)
67 {
68         int newl, skip;
69         struct msgbuf *bufp, cur;
70         char *bp, *memf, *nlistf;
71         kvm_t *kd;
72         int ch;
73         int clear = 0;
74         int pri = 0;
75         int tailmode = 0;
76         int kno;
77         size_t buflen, bufpos;
78
79         setlocale(LC_CTYPE, "");
80         memf = nlistf = NULL;
81         while ((ch = getopt(argc, argv, "acfM:N:n:")) != -1) {
82                 switch(ch) {
83                 case 'a':
84                         all_opt++;
85                         break;
86                 case 'c':
87                         clear = 1;
88                         break;
89                 case 'f':
90                         ++tailmode;
91                         break;
92                 case 'M':
93                         memf = optarg;
94                         break;
95                 case 'N':
96                         nlistf = optarg;
97                         break;
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;
103                 case '?':
104                 default:
105                         usage();
106                 }
107         }
108         argc -= optind;
109         argv += optind;
110
111         newl = 1;
112         skip = 0;
113         pri = 0;
114
115         if (memf == NULL && nlistf == NULL && tailmode == 0) {
116                 /* Running kernel. Use sysctl. */
117                 buflen = 0;
118                 if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1)
119                         err(1, "sysctl kern.msgbuf");
120                 if (buflen == 0)        /* can happen if msgbuf was cleared */
121                         buflen = 1;
122
123                 if ((bp = malloc(buflen)) == NULL)
124                         errx(1, "malloc failed");
125                 if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1)
126                         err(1, "sysctl kern.msgbuf");
127                 /* We get a dewrapped buffer using sysctl. */
128                 bufpos = 0;
129                 dumpbuf(bp, bufpos, buflen, &newl, &skip, &pri);
130         } else {
131                 u_int rindex;
132                 u_int xindex;
133                 u_int ri;
134                 u_int xi;
135                 u_int n;
136
137                 /* Read in kernel message buffer, do sanity checks. */
138                 kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
139                 if (kd == NULL)
140                         exit (1);
141                 if (kvm_nlist(kd, nl) == -1)
142                         errx(1, "kvm_nlist: %s", kvm_geterr(kd));
143                 if (nl[X_MSGBUF].n_type == 0)
144                         errx(1, "%s: msgbufp not found",
145                             nlistf ? nlistf : "namelist");
146                 bp = malloc(INCRBUFSIZE);
147                 if (!bp)
148                         errx(1, "malloc failed");
149                 if (KREAD(nl[X_MSGBUF].n_value, bufp))
150                         errx(1, "kvm_read: %s", kvm_geterr(kd));
151                 if (KREAD((long)bufp, cur))
152                         errx(1, "kvm_read: %s", kvm_geterr(kd));
153                 if (cur.msg_magic != MSG_MAGIC && cur.msg_magic != MSG_OMAGIC)
154                         errx(1, "kernel message buffer has different magic "
155                             "number");
156
157                 /*
158                  * NOTE: current algorithm is compatible with both old and
159                  *       new msgbuf structures.  The new structure doesn't
160                  *       modulo the indexes (so we do), and adds a separate
161                  *       log index which we don't access here.
162                  */
163
164                 rindex = cur.msg_bufr;
165
166                 for (;;) {
167                         /*
168                          * Calculate index for dump and do sanity clipping.
169                          */
170                         xindex = cur.msg_bufx;
171                         n = xindex - rindex;
172                         if (n > cur.msg_size - 1024) {
173                                 rindex = xindex - cur.msg_size + 2048;
174                                 n = xindex - rindex;
175                         }
176                         ri = rindex % cur.msg_size;
177                         xi = xindex % cur.msg_size;
178
179                         if (ri < xi)
180                                 buflen = xi - ri;
181                         else
182                                 buflen = cur.msg_size - ri;
183                         if (buflen > n)
184                                 buflen = n;
185                         if (buflen > INCRBUFSIZE)
186                                 buflen = INCRBUFSIZE;
187
188                         if (kvm_read(kd, (long)cur.msg_ptr + ri,
189                                      bp, buflen) != (ssize_t)buflen) {
190                                 errx(1, "kvm_read: %s", kvm_geterr(kd));
191                         }
192                         if (buflen)
193                                 dumpbuf(bp, 0, buflen, &newl, &skip, &pri);
194                         ri = (ri + buflen) % cur.msg_size;
195                         n = n - buflen;
196                         rindex += buflen;
197                         if ((int)n <= 0) {
198                                 if (tailmode == 0)
199                                         break;
200                                 fflush(stdout);
201                                 if (tailmode == 1)
202                                         sleep(1);
203                         }
204                         if (KREAD((long)bufp, cur))
205                                 errx(1, "kvm_read: %s", kvm_geterr(kd));
206                 }
207                 kvm_close(kd);
208         }
209         if (!newl)
210                 putchar('\n');
211         if (clear) {
212                 if (sysctlbyname("kern.msgbuf_clear", NULL, NULL,
213                                  &clear, sizeof(int)) != 0) {
214                         err(1, "sysctl kern.msgbuf_clear");
215                 }
216         }
217         return(0);
218 }
219
220 static
221 void
222 dumpbuf(char *bp, size_t bufpos, size_t buflen,
223         int *newl, int *skip, int *pri)
224 {
225         int ch;
226         char *p, *ep;
227         char buf[5];
228
229         /*
230          * The message buffer is circular.  If the buffer has wrapped, the
231          * write pointer points to the oldest data.  Otherwise, the write
232          * pointer points to \0's following the data.  Read the entire
233          * buffer starting at the write pointer and ignore nulls so that
234          * we effectively start at the oldest data.
235          */
236         p = bp + bufpos;
237         ep = (bufpos == 0 ? bp + buflen : p);
238         do {
239                 if (p == bp + buflen)
240                         p = bp;
241                 ch = *p;
242                 /* Skip "\n<.*>" syslog sequences. */
243                 if (*skip) {
244                         if (ch == '\n') {
245                                 *skip = 0;
246                                 *newl = 1;
247                         } if (ch == '>') {
248                                 if (LOG_FAC(*pri) == LOG_KERN)
249                                         *newl = *skip = 0;
250                         } else if (ch >= '0' && ch <= '9') {
251                                 *pri *= 10;
252                                 *pri += ch - '0';
253                         }
254                         continue;
255                 }
256                 if (*newl && ch == '<' && all_opt == 0) {
257                         *pri = 0;
258                         *skip = 1;
259                         continue;
260                 }
261                 if (ch == '\0')
262                         continue;
263                 *newl = (ch == '\n');
264                 vis(buf, ch, 0, 0);
265                 if (buf[1] == 0)
266                         putchar(buf[0]);
267                 else
268                         printf("%s", buf);
269         } while (++p != ep);
270 }
271
272 void
273 usage(void)
274 {
275         fprintf(stderr, "usage: dmesg [-ac] [-M core] [-N system]\n");
276         exit(1);
277 }