dmesg.c revision 1.36 1 1.36 kre /* $NetBSD: dmesg.c,v 1.36 2018/09/19 22:58:03 kre Exp $ */
2 1.1 cgd /*-
3 1.6 cgd * Copyright (c) 1991, 1993
4 1.6 cgd * The Regents of the University of California. All rights reserved.
5 1.1 cgd *
6 1.1 cgd * Redistribution and use in source and binary forms, with or without
7 1.1 cgd * modification, are permitted provided that the following conditions
8 1.1 cgd * are met:
9 1.1 cgd * 1. Redistributions of source code must retain the above copyright
10 1.1 cgd * notice, this list of conditions and the following disclaimer.
11 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer in the
13 1.1 cgd * documentation and/or other materials provided with the distribution.
14 1.22 agc * 3. Neither the name of the University nor the names of its contributors
15 1.1 cgd * may be used to endorse or promote products derived from this software
16 1.1 cgd * without specific prior written permission.
17 1.1 cgd *
18 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 cgd * SUCH DAMAGE.
29 1.1 cgd */
30 1.1 cgd
31 1.11 lukem #include <sys/cdefs.h>
32 1.1 cgd #ifndef lint
33 1.26 lukem __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
34 1.26 lukem The Regents of the University of California. All rights reserved.");
35 1.1 cgd #endif /* not lint */
36 1.1 cgd
37 1.1 cgd #ifndef lint
38 1.8 cgd #if 0
39 1.6 cgd static char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93";
40 1.8 cgd #else
41 1.36 kre __RCSID("$NetBSD: dmesg.c,v 1.36 2018/09/19 22:58:03 kre Exp $");
42 1.8 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.17 simonb #include <sys/param.h>
46 1.1 cgd #include <sys/msgbuf.h>
47 1.17 simonb #include <sys/sysctl.h>
48 1.6 cgd
49 1.7 cgd #include <err.h>
50 1.34 christos #include <ctype.h>
51 1.6 cgd #include <fcntl.h>
52 1.28 christos #include <time.h>
53 1.6 cgd #include <kvm.h>
54 1.1 cgd #include <nlist.h>
55 1.6 cgd #include <stdio.h>
56 1.17 simonb #include <stddef.h>
57 1.1 cgd #include <stdlib.h>
58 1.5 jtc #include <unistd.h>
59 1.6 cgd #include <vis.h>
60 1.1 cgd
61 1.20 simonb #ifndef SMALL
62 1.27 joerg static struct nlist nl[] = {
63 1.6 cgd #define X_MSGBUF 0
64 1.25 christos { .n_name = "_msgbufp" },
65 1.25 christos { .n_name = NULL },
66 1.1 cgd };
67 1.1 cgd
68 1.27 joerg __dead static void usage(void);
69 1.1 cgd
70 1.6 cgd #define KREAD(addr, var) \
71 1.6 cgd kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
72 1.34 christos
73 1.34 christos static const char *
74 1.36 kre fmtydhmsf(char *b, size_t l, intmax_t t, long nsec, int ht)
75 1.34 christos {
76 1.35 kre intmax_t s, m, h, d, M, y;
77 1.34 christos int z;
78 1.35 kre int prec;
79 1.34 christos size_t o;
80 1.34 christos
81 1.34 christos s = t % 60;
82 1.34 christos t /= 60;
83 1.34 christos
84 1.34 christos m = t % 60;
85 1.34 christos t /= 60;
86 1.34 christos
87 1.34 christos h = t % 24;
88 1.34 christos t /= 24;
89 1.34 christos
90 1.34 christos d = t % 30;
91 1.34 christos t /= 30;
92 1.34 christos
93 1.34 christos M = t % 12;
94 1.34 christos t /= 12;
95 1.34 christos
96 1.34 christos y = t;
97 1.34 christos
98 1.34 christos z = 0;
99 1.34 christos o = 0;
100 1.34 christos
101 1.34 christos #define APPENDFMT(fmt, ...) \
102 1.34 christos do { \
103 1.34 christos z = snprintf(b + o, l - o, fmt, __VA_ARGS__); \
104 1.34 christos if (z == -1) \
105 1.34 christos return b; \
106 1.34 christos o += (size_t)z; \
107 1.34 christos if (o >= l) \
108 1.34 christos return b; \
109 1.34 christos } while (/*CONSTCOND*/0)
110 1.34 christos
111 1.34 christos #define APPEND(a) \
112 1.34 christos do if (a) \
113 1.35 kre APPENDFMT("%jd%c", a, toupper((unsigned char)__STRING(a)[0])); \
114 1.34 christos while (/*CONSTCOND*/0)
115 1.35 kre #define APPENDS(a, pr, ms) \
116 1.35 kre APPENDFMT("%jd%s%.*ld%c", a, radix, pr, ms, \
117 1.34 christos toupper((unsigned char)__STRING(a)[0]))
118 1.34 christos
119 1.34 christos APPENDFMT("%s", "P");
120 1.34 christos APPEND(y);
121 1.34 christos APPEND(M);
122 1.34 christos APPEND(d);
123 1.34 christos APPENDFMT("%s", "T");
124 1.34 christos APPEND(h);
125 1.34 christos APPEND(m);
126 1.34 christos if (nsec)
127 1.35 kre nsec = (nsec + 500000) / 1000000; /* now milliseconds */
128 1.35 kre prec = 3;
129 1.36 kre if (nsec && ht == 2) {
130 1.36 kre while (prec > 0 && (nsec % 10) == 0)
131 1.36 kre --prec, nsec /= 10;
132 1.36 kre }
133 1.36 kre if (nsec || ht > 2)
134 1.35 kre APPENDS(s, prec, nsec);
135 1.34 christos else
136 1.34 christos APPEND(s);
137 1.34 christos return b;
138 1.34 christos }
139 1.34 christos
140 1.34 christos static void
141 1.34 christos pnsec(long nsec, long fsec, int scale)
142 1.34 christos {
143 1.34 christos if (scale > 6)
144 1.34 christos printf("%6.6ld", (nsec + 499) / 1000);
145 1.34 christos else
146 1.34 christos printf("%*.*ld%.*s", scale, scale, fsec, 6 - scale, "000000");
147 1.34 christos }
148 1.24 dsl #endif
149 1.6 cgd
150 1.6 cgd int
151 1.18 simonb main(int argc, char *argv[])
152 1.1 cgd {
153 1.17 simonb struct kern_msgbuf cur;
154 1.28 christos int ch, newl, log, i;
155 1.28 christos size_t tstamp, size;
156 1.20 simonb char *p, *bufdata;
157 1.28 christos char buf[5];
158 1.20 simonb #ifndef SMALL
159 1.28 christos char tbuf[64];
160 1.20 simonb char *memf, *nlistf;
161 1.28 christos struct timeval boottime;
162 1.31 christos struct timespec lasttime;
163 1.31 christos intmax_t sec;
164 1.33 kre long nsec, fsec;
165 1.33 kre int scale;
166 1.31 christos int deltas, quiet, humantime;
167 1.33 kre bool frac;
168 1.28 christos
169 1.28 christos static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
170 1.28 christos size = sizeof(boottime);
171 1.28 christos
172 1.28 christos boottime.tv_sec = 0;
173 1.28 christos boottime.tv_usec = 0;
174 1.31 christos lasttime.tv_sec = 0;
175 1.31 christos lasttime.tv_nsec = 0;
176 1.31 christos deltas = quiet = humantime = 0;
177 1.28 christos
178 1.28 christos (void)sysctl(bmib, 2, &boottime, &size, NULL, 0);
179 1.1 cgd
180 1.6 cgd memf = nlistf = NULL;
181 1.31 christos while ((ch = getopt(argc, argv, "dM:N:tT")) != -1)
182 1.1 cgd switch(ch) {
183 1.31 christos case 'd':
184 1.31 christos deltas = 1;
185 1.31 christos break;
186 1.1 cgd case 'M':
187 1.6 cgd memf = optarg;
188 1.1 cgd break;
189 1.1 cgd case 'N':
190 1.6 cgd nlistf = optarg;
191 1.1 cgd break;
192 1.31 christos case 't':
193 1.31 christos quiet = 1;
194 1.28 christos break;
195 1.31 christos case 'T':
196 1.34 christos humantime++;
197 1.28 christos break;
198 1.1 cgd case '?':
199 1.1 cgd default:
200 1.1 cgd usage();
201 1.1 cgd }
202 1.1 cgd argc -= optind;
203 1.1 cgd argv += optind;
204 1.31 christos if (quiet && humantime)
205 1.31 christos err(EXIT_FAILURE, "-t cannot be used with -T");
206 1.1 cgd
207 1.19 simonb if (memf == NULL) {
208 1.20 simonb #endif
209 1.28 christos static const int mmib[2] = { CTL_KERN, KERN_MSGBUF };
210 1.17 simonb
211 1.28 christos if (sysctl(mmib, 2, NULL, &size, NULL, 0) == -1 ||
212 1.24 dsl (bufdata = malloc(size)) == NULL ||
213 1.28 christos sysctl(mmib, 2, bufdata, &size, NULL, 0) == -1)
214 1.17 simonb err(1, "can't get msgbuf");
215 1.17 simonb
216 1.17 simonb /* make a dummy struct msgbuf for the display logic */
217 1.24 dsl cur.msg_bufx = 0;
218 1.24 dsl cur.msg_bufs = size;
219 1.20 simonb #ifndef SMALL
220 1.17 simonb } else {
221 1.17 simonb kvm_t *kd;
222 1.17 simonb struct kern_msgbuf *bufp;
223 1.17 simonb
224 1.17 simonb /*
225 1.17 simonb * Read in message buffer header and data, and do sanity
226 1.17 simonb * checks.
227 1.17 simonb */
228 1.17 simonb kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
229 1.17 simonb if (kd == NULL)
230 1.17 simonb exit (1);
231 1.17 simonb if (kvm_nlist(kd, nl) == -1)
232 1.17 simonb errx(1, "kvm_nlist: %s", kvm_geterr(kd));
233 1.17 simonb if (nl[X_MSGBUF].n_type == 0)
234 1.17 simonb errx(1, "%s: msgbufp not found", nlistf ? nlistf :
235 1.17 simonb "namelist");
236 1.17 simonb if (KREAD(nl[X_MSGBUF].n_value, bufp))
237 1.17 simonb errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
238 1.17 simonb nl[X_MSGBUF].n_value);
239 1.17 simonb if (kvm_read(kd, (long)bufp, &cur,
240 1.17 simonb offsetof(struct kern_msgbuf, msg_bufc)) !=
241 1.17 simonb offsetof(struct kern_msgbuf, msg_bufc))
242 1.17 simonb errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
243 1.17 simonb (unsigned long)bufp);
244 1.17 simonb if (cur.msg_magic != MSG_MAGIC)
245 1.17 simonb errx(1, "magic number incorrect");
246 1.17 simonb bufdata = malloc(cur.msg_bufs);
247 1.17 simonb if (bufdata == NULL)
248 1.17 simonb errx(1, "couldn't allocate space for buffer data");
249 1.17 simonb if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
250 1.17 simonb cur.msg_bufs) != cur.msg_bufs)
251 1.17 simonb errx(1, "kvm_read: %s", kvm_geterr(kd));
252 1.17 simonb kvm_close(kd);
253 1.17 simonb if (cur.msg_bufx >= cur.msg_bufs)
254 1.17 simonb cur.msg_bufx = 0;
255 1.17 simonb }
256 1.20 simonb #endif
257 1.1 cgd
258 1.1 cgd /*
259 1.14 enami * The message buffer is circular; start at the write pointer
260 1.14 enami * (which points the oldest character), and go to the write
261 1.14 enami * pointer - 1 (which points the newest character). I.e, loop
262 1.14 enami * over cur.msg_bufs times. Unused area is skipped since it
263 1.14 enami * contains nul.
264 1.1 cgd */
265 1.33 kre #ifndef SMALL
266 1.33 kre frac = false;
267 1.33 kre scale = 0;
268 1.33 kre #endif
269 1.28 christos for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
270 1.14 enami i < cur.msg_bufs; i++, p++) {
271 1.33 kre
272 1.24 dsl #ifndef SMALL
273 1.12 leo if (p == bufdata + cur.msg_bufs)
274 1.12 leo p = bufdata;
275 1.28 christos #define ADDC(c) \
276 1.33 kre do { \
277 1.28 christos if (tstamp < sizeof(tbuf) - 1) \
278 1.28 christos tbuf[tstamp++] = (c); \
279 1.33 kre if (frac) \
280 1.33 kre scale++; \
281 1.33 kre } while (/*CONSTCOND*/0)
282 1.28 christos #else
283 1.28 christos #define ADDC(c)
284 1.24 dsl #endif
285 1.1 cgd ch = *p;
286 1.30 christos if (ch == '\0')
287 1.30 christos continue;
288 1.1 cgd /* Skip "\n<.*>" syslog sequences. */
289 1.28 christos /* Gather timestamp sequences */
290 1.28 christos if (newl) {
291 1.33 kre #ifndef SMALL
292 1.33 kre int j;
293 1.33 kre #endif
294 1.33 kre
295 1.28 christos switch (ch) {
296 1.28 christos case '[':
297 1.33 kre #ifndef SMALL
298 1.33 kre frac = false;
299 1.33 kre scale = 0;
300 1.33 kre #endif
301 1.28 christos ADDC(ch);
302 1.28 christos continue;
303 1.28 christos case '<':
304 1.28 christos log = 1;
305 1.28 christos continue;
306 1.28 christos case '>':
307 1.29 christos log = 0;
308 1.28 christos continue;
309 1.28 christos case ']':
310 1.33 kre #ifndef SMALL
311 1.33 kre frac = false;
312 1.33 kre #endif
313 1.28 christos ADDC(ch);
314 1.28 christos ADDC('\0');
315 1.28 christos tstamp = 0;
316 1.28 christos #ifndef SMALL
317 1.33 kre sec = fsec = 0;
318 1.33 kre switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
319 1.33 kre case EOF:
320 1.33 kre case 0:
321 1.33 kre /*???*/
322 1.33 kre continue;
323 1.33 kre case 1:
324 1.33 kre fsec = 0;
325 1.33 kre break;
326 1.33 kre case 2:
327 1.33 kre break;
328 1.33 kre default:
329 1.33 kre /* Help */
330 1.33 kre continue;
331 1.33 kre }
332 1.33 kre
333 1.33 kre for (nsec = fsec, j = 9 - scale; --j >= 0; )
334 1.33 kre nsec *= 10;
335 1.31 christos if (!quiet || deltas)
336 1.31 christos printf("[");
337 1.34 christos if (humantime == 1) {
338 1.28 christos time_t t;
339 1.28 christos struct tm tm;
340 1.33 kre
341 1.28 christos t = boottime.tv_sec + sec;
342 1.28 christos if (localtime_r(&t, &tm) != NULL) {
343 1.28 christos strftime(tbuf, sizeof(tbuf),
344 1.31 christos "%a %b %e %H:%M:%S %Z %Y",
345 1.28 christos &tm);
346 1.31 christos printf("%s", tbuf);
347 1.28 christos }
348 1.34 christos } else if (humantime > 1) {
349 1.34 christos const char *fp = fmtydhmsf(tbuf,
350 1.36 kre sizeof(tbuf), sec, fsec, humantime);
351 1.34 christos if (fp) {
352 1.34 christos printf("%s", fp);
353 1.34 christos }
354 1.31 christos } else if (!quiet) {
355 1.34 christos printf(" %5jd.", sec);
356 1.34 christos pnsec(nsec, fsec, scale);
357 1.31 christos }
358 1.31 christos if (deltas) {
359 1.31 christos struct timespec nt = { sec, nsec };
360 1.31 christos struct timespec dt;
361 1.33 kre
362 1.31 christos timespecsub(&nt, &lasttime, &dt);
363 1.31 christos if (humantime || !quiet)
364 1.31 christos printf(" ");
365 1.31 christos printf("<% 4jd.%06ld>", (intmax_t)
366 1.33 kre dt.tv_sec, (dt.tv_nsec+499) / 1000);
367 1.31 christos lasttime = nt;
368 1.31 christos }
369 1.31 christos if (!quiet || deltas)
370 1.31 christos printf("] ");
371 1.28 christos #endif
372 1.28 christos continue;
373 1.28 christos case ' ':
374 1.28 christos if (!tstamp)
375 1.28 christos continue;
376 1.28 christos /*FALLTHROUGH*/
377 1.28 christos default:
378 1.28 christos if (tstamp) {
379 1.28 christos ADDC(ch);
380 1.33 kre #ifndef SMALL
381 1.33 kre if (ch == '.')
382 1.33 kre frac = true;
383 1.33 kre #endif
384 1.28 christos continue;
385 1.28 christos }
386 1.28 christos if (log)
387 1.28 christos continue;
388 1.28 christos break;
389 1.28 christos }
390 1.29 christos newl = 0;
391 1.1 cgd }
392 1.6 cgd newl = ch == '\n';
393 1.21 augustss (void)vis(buf, ch, VIS_NOSLASH, 0);
394 1.24 dsl #ifndef SMALL
395 1.6 cgd if (buf[1] == 0)
396 1.6 cgd (void)putchar(buf[0]);
397 1.6 cgd else
398 1.24 dsl #endif
399 1.6 cgd (void)printf("%s", buf);
400 1.12 leo }
401 1.1 cgd if (!newl)
402 1.1 cgd (void)putchar('\n');
403 1.28 christos return EXIT_SUCCESS;
404 1.1 cgd }
405 1.1 cgd
406 1.24 dsl #ifndef SMALL
407 1.27 joerg static void
408 1.18 simonb usage(void)
409 1.1 cgd {
410 1.15 enami
411 1.32 wiz (void)fprintf(stderr, "Usage: %s [-dTt] [-M core] [-N system]\n",
412 1.28 christos getprogname());
413 1.28 christos exit(EXIT_FAILURE);
414 1.1 cgd }
415 1.24 dsl #endif
416