1 /* $NetBSD: dmesg.c,v 1.25 2006/10/16 02:43:19 christos Exp $ */
3 * Copyright (c) 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 1991, 1993\
34 The Regents of the University of California. All rights reserved.");
39 static char sccsid
[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93";
41 __RCSID("$NetBSD: dmesg.c,v 1.25 2006/10/16 02:43:19 christos Exp $");
45 #include <sys/param.h>
46 #include <sys/msgbuf.h>
47 #include <sys/sysctl.h>
62 { .n_name
= "_msgbufp" },
68 #define KREAD(addr, var) \
69 kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var)
73 main(int argc
, char *argv
[])
75 struct kern_msgbuf cur
;
76 int ch
, newl
, skip
, i
;
85 while ((ch
= getopt(argc
, argv
, "M:N:")) != -1)
106 mib
[1] = KERN_MSGBUF
;
108 if (sysctl(mib
, 2, NULL
, &size
, NULL
, 0) == -1 ||
109 (bufdata
= malloc(size
)) == NULL
||
110 sysctl(mib
, 2, bufdata
, &size
, NULL
, 0) == -1)
111 err(1, "can't get msgbuf");
113 /* make a dummy struct msgbuf for the display logic */
119 struct kern_msgbuf
*bufp
;
122 * Read in message buffer header and data, and do sanity
125 kd
= kvm_open(nlistf
, memf
, NULL
, O_RDONLY
, "dmesg");
128 if (kvm_nlist(kd
, nl
) == -1)
129 errx(1, "kvm_nlist: %s", kvm_geterr(kd
));
130 if (nl
[X_MSGBUF
].n_type
== 0)
131 errx(1, "%s: msgbufp not found", nlistf
? nlistf
:
133 if (KREAD(nl
[X_MSGBUF
].n_value
, bufp
))
134 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd
),
135 nl
[X_MSGBUF
].n_value
);
136 if (kvm_read(kd
, (long)bufp
, &cur
,
137 offsetof(struct kern_msgbuf
, msg_bufc
)) !=
138 offsetof(struct kern_msgbuf
, msg_bufc
))
139 errx(1, "kvm_read: %s (0x%lx)", kvm_geterr(kd
),
140 (unsigned long)bufp
);
141 if (cur
.msg_magic
!= MSG_MAGIC
)
142 errx(1, "magic number incorrect");
143 bufdata
= malloc(cur
.msg_bufs
);
145 errx(1, "couldn't allocate space for buffer data");
146 if (kvm_read(kd
, (long)&bufp
->msg_bufc
, bufdata
,
147 cur
.msg_bufs
) != cur
.msg_bufs
)
148 errx(1, "kvm_read: %s", kvm_geterr(kd
));
150 if (cur
.msg_bufx
>= cur
.msg_bufs
)
156 * The message buffer is circular; start at the write pointer
157 * (which points the oldest character), and go to the write
158 * pointer - 1 (which points the newest character). I.e, loop
159 * over cur.msg_bufs times. Unused area is skipped since it
162 for (newl
= skip
= i
= 0, p
= bufdata
+ cur
.msg_bufx
;
163 i
< cur
.msg_bufs
; i
++, p
++) {
165 if (p
== bufdata
+ cur
.msg_bufs
)
169 /* Skip "\n<.*>" syslog sequences. */
175 if (newl
&& ch
== '<') {
182 (void)vis(buf
, ch
, VIS_NOSLASH
, 0);
185 (void)putchar(buf
[0]);
188 (void)printf("%s", buf
);
200 (void)fprintf(stderr
, "usage: dmesg [-M core] [-N system]\n");