dmesg.c revision 1.45 1 1.45 kre /* $NetBSD: dmesg.c,v 1.45 2020/01/01 00:24:52 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.45 kre __RCSID("$NetBSD: dmesg.c,v 1.45 2020/01/01 00:24:52 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.37 christos #include <langinfo.h>
63 1.37 christos #include <locale.h>
64 1.37 christos
65 1.27 joerg static struct nlist nl[] = {
66 1.6 cgd #define X_MSGBUF 0
67 1.25 christos { .n_name = "_msgbufp" },
68 1.25 christos { .n_name = NULL },
69 1.1 cgd };
70 1.1 cgd
71 1.37 christos static const char *radix;
72 1.37 christos
73 1.27 joerg __dead static void usage(void);
74 1.1 cgd
75 1.6 cgd #define KREAD(addr, var) \
76 1.6 cgd kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
77 1.34 christos
78 1.34 christos static const char *
79 1.36 kre fmtydhmsf(char *b, size_t l, intmax_t t, long nsec, int ht)
80 1.34 christos {
81 1.38 kre intmax_t s, m, h;
82 1.34 christos int z;
83 1.35 kre int prec;
84 1.34 christos size_t o;
85 1.34 christos
86 1.34 christos s = t % 60;
87 1.34 christos t /= 60;
88 1.34 christos
89 1.34 christos m = t % 60;
90 1.34 christos t /= 60;
91 1.34 christos
92 1.37 christos h = t;
93 1.34 christos
94 1.34 christos z = 0;
95 1.34 christos o = 0;
96 1.34 christos
97 1.34 christos #define APPENDFMT(fmt, ...) \
98 1.34 christos do { \
99 1.34 christos z = snprintf(b + o, l - o, fmt, __VA_ARGS__); \
100 1.34 christos if (z == -1) \
101 1.34 christos return b; \
102 1.34 christos o += (size_t)z; \
103 1.34 christos if (o >= l) \
104 1.34 christos return b; \
105 1.34 christos } while (/*CONSTCOND*/0)
106 1.34 christos
107 1.34 christos #define APPEND(a) \
108 1.34 christos do if (a) \
109 1.40 kre APPENDFMT("%jd%c", a, toupper((unsigned char)__STRING(a)[0])); \
110 1.34 christos while (/*CONSTCOND*/0)
111 1.35 kre #define APPENDS(a, pr, ms) \
112 1.35 kre APPENDFMT("%jd%s%.*ld%c", a, radix, pr, ms, \
113 1.34 christos toupper((unsigned char)__STRING(a)[0]))
114 1.34 christos
115 1.34 christos APPENDFMT("%s", "P");
116 1.34 christos APPENDFMT("%s", "T");
117 1.34 christos APPEND(h);
118 1.34 christos APPEND(m);
119 1.34 christos if (nsec)
120 1.35 kre nsec = (nsec + 500000) / 1000000; /* now milliseconds */
121 1.35 kre prec = 3;
122 1.36 kre if (nsec && ht == 2) {
123 1.36 kre while (prec > 0 && (nsec % 10) == 0)
124 1.36 kre --prec, nsec /= 10;
125 1.36 kre }
126 1.36 kre if (nsec || ht > 2)
127 1.35 kre APPENDS(s, prec, nsec);
128 1.34 christos else
129 1.34 christos APPEND(s);
130 1.34 christos return b;
131 1.34 christos }
132 1.34 christos
133 1.34 christos static void
134 1.34 christos pnsec(long nsec, long fsec, int scale)
135 1.34 christos {
136 1.34 christos if (scale > 6)
137 1.34 christos printf("%6.6ld", (nsec + 499) / 1000);
138 1.34 christos else
139 1.34 christos printf("%*.*ld%.*s", scale, scale, fsec, 6 - scale, "000000");
140 1.34 christos }
141 1.24 dsl #endif
142 1.6 cgd
143 1.6 cgd int
144 1.18 simonb main(int argc, char *argv[])
145 1.1 cgd {
146 1.17 simonb struct kern_msgbuf cur;
147 1.28 christos int ch, newl, log, i;
148 1.28 christos size_t tstamp, size;
149 1.20 simonb char *p, *bufdata;
150 1.28 christos char buf[5];
151 1.20 simonb #ifndef SMALL
152 1.28 christos char tbuf[64];
153 1.20 simonb char *memf, *nlistf;
154 1.41 kre struct timespec boottime;
155 1.31 christos struct timespec lasttime;
156 1.31 christos intmax_t sec;
157 1.33 kre long nsec, fsec;
158 1.33 kre int scale;
159 1.31 christos int deltas, quiet, humantime;
160 1.44 tsutsui bool frac, postts;
161 1.40 kre
162 1.28 christos static const int bmib[] = { CTL_KERN, KERN_BOOTTIME };
163 1.28 christos size = sizeof(boottime);
164 1.28 christos
165 1.37 christos (void)setlocale(LC_ALL, "");
166 1.37 christos radix = nl_langinfo(RADIXCHAR);
167 1.37 christos if (radix == NULL)
168 1.37 christos radix = "."; /* could also select "," */
169 1.37 christos
170 1.28 christos boottime.tv_sec = 0;
171 1.41 kre boottime.tv_nsec = 0;
172 1.31 christos lasttime.tv_sec = 0;
173 1.31 christos lasttime.tv_nsec = 0;
174 1.31 christos deltas = quiet = humantime = 0;
175 1.28 christos
176 1.40 kre (void)sysctl(bmib, 2, &boottime, &size, NULL, 0);
177 1.1 cgd
178 1.6 cgd memf = nlistf = NULL;
179 1.31 christos while ((ch = getopt(argc, argv, "dM:N:tT")) != -1)
180 1.1 cgd switch(ch) {
181 1.31 christos case 'd':
182 1.31 christos deltas = 1;
183 1.31 christos break;
184 1.1 cgd case 'M':
185 1.6 cgd memf = optarg;
186 1.1 cgd break;
187 1.1 cgd case 'N':
188 1.6 cgd nlistf = optarg;
189 1.1 cgd break;
190 1.31 christos case 't':
191 1.31 christos quiet = 1;
192 1.28 christos break;
193 1.31 christos case 'T':
194 1.34 christos humantime++;
195 1.28 christos break;
196 1.1 cgd case '?':
197 1.1 cgd default:
198 1.1 cgd usage();
199 1.1 cgd }
200 1.1 cgd argc -= optind;
201 1.1 cgd argv += optind;
202 1.31 christos if (quiet && humantime)
203 1.31 christos err(EXIT_FAILURE, "-t cannot be used with -T");
204 1.1 cgd
205 1.19 simonb if (memf == NULL) {
206 1.20 simonb #endif
207 1.28 christos static const int mmib[2] = { CTL_KERN, KERN_MSGBUF };
208 1.17 simonb
209 1.28 christos if (sysctl(mmib, 2, NULL, &size, NULL, 0) == -1 ||
210 1.24 dsl (bufdata = malloc(size)) == NULL ||
211 1.28 christos sysctl(mmib, 2, bufdata, &size, NULL, 0) == -1)
212 1.17 simonb err(1, "can't get msgbuf");
213 1.17 simonb
214 1.17 simonb /* make a dummy struct msgbuf for the display logic */
215 1.24 dsl cur.msg_bufx = 0;
216 1.24 dsl cur.msg_bufs = size;
217 1.20 simonb #ifndef SMALL
218 1.17 simonb } else {
219 1.17 simonb kvm_t *kd;
220 1.17 simonb struct kern_msgbuf *bufp;
221 1.17 simonb
222 1.17 simonb /*
223 1.17 simonb * Read in message buffer header and data, and do sanity
224 1.17 simonb * checks.
225 1.17 simonb */
226 1.17 simonb kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg");
227 1.17 simonb if (kd == NULL)
228 1.17 simonb exit (1);
229 1.17 simonb if (kvm_nlist(kd, nl) == -1)
230 1.17 simonb errx(1, "kvm_nlist: %s", kvm_geterr(kd));
231 1.17 simonb if (nl[X_MSGBUF].n_type == 0)
232 1.17 simonb errx(1, "%s: msgbufp not found", nlistf ? nlistf :
233 1.17 simonb "namelist");
234 1.17 simonb if (KREAD(nl[X_MSGBUF].n_value, bufp))
235 1.17 simonb errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
236 1.17 simonb nl[X_MSGBUF].n_value);
237 1.17 simonb if (kvm_read(kd, (long)bufp, &cur,
238 1.17 simonb offsetof(struct kern_msgbuf, msg_bufc)) !=
239 1.17 simonb offsetof(struct kern_msgbuf, msg_bufc))
240 1.17 simonb errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd),
241 1.17 simonb (unsigned long)bufp);
242 1.17 simonb if (cur.msg_magic != MSG_MAGIC)
243 1.17 simonb errx(1, "magic number incorrect");
244 1.17 simonb bufdata = malloc(cur.msg_bufs);
245 1.17 simonb if (bufdata == NULL)
246 1.17 simonb errx(1, "couldn't allocate space for buffer data");
247 1.17 simonb if (kvm_read(kd, (long)&bufp->msg_bufc, bufdata,
248 1.17 simonb cur.msg_bufs) != cur.msg_bufs)
249 1.17 simonb errx(1, "kvm_read: %s", kvm_geterr(kd));
250 1.17 simonb kvm_close(kd);
251 1.17 simonb if (cur.msg_bufx >= cur.msg_bufs)
252 1.17 simonb cur.msg_bufx = 0;
253 1.17 simonb }
254 1.20 simonb #endif
255 1.1 cgd
256 1.1 cgd /*
257 1.14 enami * The message buffer is circular; start at the write pointer
258 1.14 enami * (which points the oldest character), and go to the write
259 1.14 enami * pointer - 1 (which points the newest character). I.e, loop
260 1.14 enami * over cur.msg_bufs times. Unused area is skipped since it
261 1.14 enami * contains nul.
262 1.1 cgd */
263 1.33 kre #ifndef SMALL
264 1.33 kre frac = false;
265 1.44 tsutsui postts = false;
266 1.33 kre scale = 0;
267 1.33 kre #endif
268 1.28 christos for (tstamp = 0, newl = 1, log = i = 0, p = bufdata + cur.msg_bufx;
269 1.14 enami i < cur.msg_bufs; i++, p++) {
270 1.33 kre
271 1.24 dsl #ifndef SMALL
272 1.12 leo if (p == bufdata + cur.msg_bufs)
273 1.12 leo p = bufdata;
274 1.28 christos #define ADDC(c) \
275 1.33 kre do { \
276 1.28 christos if (tstamp < sizeof(tbuf) - 1) \
277 1.28 christos tbuf[tstamp++] = (c); \
278 1.33 kre if (frac) \
279 1.33 kre scale++; \
280 1.33 kre } while (/*CONSTCOND*/0)
281 1.28 christos #else
282 1.28 christos #define ADDC(c)
283 1.24 dsl #endif
284 1.1 cgd ch = *p;
285 1.30 christos if (ch == '\0')
286 1.30 christos continue;
287 1.1 cgd /* Skip "\n<.*>" syslog sequences. */
288 1.28 christos /* Gather timestamp sequences */
289 1.28 christos if (newl) {
290 1.33 kre #ifndef SMALL
291 1.33 kre int j;
292 1.33 kre #endif
293 1.33 kre
294 1.28 christos switch (ch) {
295 1.42 kre #ifndef SMALL
296 1.28 christos case '[':
297 1.33 kre frac = false;
298 1.33 kre scale = 0;
299 1.28 christos ADDC(ch);
300 1.28 christos continue;
301 1.42 kre #endif
302 1.28 christos case '<':
303 1.28 christos log = 1;
304 1.28 christos continue;
305 1.28 christos case '>':
306 1.29 christos log = 0;
307 1.28 christos continue;
308 1.42 kre #ifndef SMALL
309 1.28 christos case ']':
310 1.33 kre frac = false;
311 1.28 christos ADDC(ch);
312 1.28 christos ADDC('\0');
313 1.28 christos tstamp = 0;
314 1.44 tsutsui postts = true;
315 1.33 kre sec = fsec = 0;
316 1.33 kre switch (sscanf(tbuf, "[%jd.%ld]", &sec, &fsec)){
317 1.33 kre case EOF:
318 1.33 kre case 0:
319 1.33 kre /*???*/
320 1.33 kre continue;
321 1.33 kre case 1:
322 1.33 kre fsec = 0;
323 1.33 kre break;
324 1.33 kre case 2:
325 1.33 kre break;
326 1.33 kre default:
327 1.33 kre /* Help */
328 1.33 kre continue;
329 1.33 kre }
330 1.33 kre
331 1.33 kre for (nsec = fsec, j = 9 - scale; --j >= 0; )
332 1.33 kre nsec *= 10;
333 1.31 christos if (!quiet || deltas)
334 1.31 christos printf("[");
335 1.34 christos if (humantime == 1) {
336 1.28 christos time_t t;
337 1.28 christos struct tm tm;
338 1.33 kre
339 1.28 christos t = boottime.tv_sec + sec;
340 1.41 kre if (nsec + boottime.tv_nsec >=
341 1.41 kre ( 1L /* 1 second */
342 1.41 kre * 1000L /* ms */
343 1.41 kre * 1000L /* us */
344 1.41 kre * 1000L /* ns */ ))
345 1.41 kre t++;
346 1.41 kre
347 1.28 christos if (localtime_r(&t, &tm) != NULL) {
348 1.28 christos strftime(tbuf, sizeof(tbuf),
349 1.31 christos "%a %b %e %H:%M:%S %Z %Y",
350 1.40 kre &tm);
351 1.31 christos printf("%s", tbuf);
352 1.28 christos }
353 1.34 christos } else if (humantime > 1) {
354 1.34 christos const char *fp = fmtydhmsf(tbuf,
355 1.36 kre sizeof(tbuf), sec, fsec, humantime);
356 1.34 christos if (fp) {
357 1.34 christos printf("%s", fp);
358 1.34 christos }
359 1.31 christos } else if (!quiet) {
360 1.39 kre printf(" %5jd%s", sec, radix);
361 1.34 christos pnsec(nsec, fsec, scale);
362 1.31 christos }
363 1.31 christos if (deltas) {
364 1.31 christos struct timespec nt = { sec, nsec };
365 1.31 christos struct timespec dt;
366 1.33 kre
367 1.31 christos timespecsub(&nt, &lasttime, &dt);
368 1.31 christos if (humantime || !quiet)
369 1.31 christos printf(" ");
370 1.39 kre printf("<% 4jd%s%6.6ld>",
371 1.39 kre (intmax_t)dt.tv_sec, radix,
372 1.39 kre (dt.tv_nsec+499) / 1000);
373 1.31 christos lasttime = nt;
374 1.31 christos }
375 1.31 christos if (!quiet || deltas)
376 1.31 christos printf("] ");
377 1.42 kre continue;
378 1.28 christos #endif
379 1.28 christos case ' ':
380 1.45 kre #ifndef SMALL
381 1.44 tsutsui if (!tstamp && postts) {
382 1.44 tsutsui postts = false;
383 1.45 kre #else
384 1.45 kre if (!tstamp) {
385 1.45 kre #endif
386 1.40 kre continue;
387 1.44 tsutsui }
388 1.28 christos /*FALLTHROUGH*/
389 1.28 christos default:
390 1.42 kre #ifndef SMALL
391 1.28 christos if (tstamp) {
392 1.28 christos ADDC(ch);
393 1.33 kre if (ch == '.')
394 1.33 kre frac = true;
395 1.28 christos continue;
396 1.28 christos }
397 1.42 kre #endif
398 1.28 christos if (log)
399 1.28 christos continue;
400 1.28 christos break;
401 1.28 christos }
402 1.1 cgd }
403 1.6 cgd newl = ch == '\n';
404 1.21 augustss (void)vis(buf, ch, VIS_NOSLASH, 0);
405 1.24 dsl #ifndef SMALL
406 1.6 cgd if (buf[1] == 0)
407 1.6 cgd (void)putchar(buf[0]);
408 1.6 cgd else
409 1.24 dsl #endif
410 1.6 cgd (void)printf("%s", buf);
411 1.12 leo }
412 1.1 cgd if (!newl)
413 1.1 cgd (void)putchar('\n');
414 1.28 christos return EXIT_SUCCESS;
415 1.1 cgd }
416 1.1 cgd
417 1.24 dsl #ifndef SMALL
418 1.27 joerg static void
419 1.18 simonb usage(void)
420 1.1 cgd {
421 1.15 enami
422 1.32 wiz (void)fprintf(stderr, "Usage: %s [-dTt] [-M core] [-N system]\n",
423 1.28 christos getprogname());
424 1.28 christos exit(EXIT_FAILURE);
425 1.1 cgd }
426 1.24 dsl #endif
427