11558Srgrimes/*- 21558Srgrimes * Copyright (c) 1991, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 4. Neither the name of the University nor the names of its contributors 141558Srgrimes * may be used to endorse or promote products derived from this software 151558Srgrimes * without specific prior written permission. 161558Srgrimes * 171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271558Srgrimes * SUCH DAMAGE. 281558Srgrimes */ 291558Srgrimes 30114515Sobrien#if 0 311558Srgrimes#ifndef lint 3218439Sbdestatic const char copyright[] = 331558Srgrimes"@(#) Copyright (c) 1991, 1993\n\ 341558Srgrimes The Regents of the University of California. All rights reserved.\n"; 351558Srgrimes#endif /* not lint */ 361558Srgrimes 371558Srgrimes#ifndef lint 3818439Sbdestatic const char sccsid[] = "@(#)dmesg.c 8.1 (Berkeley) 6/5/93"; 39114515Sobrien#endif /* not lint */ 4018439Sbde#endif 41114515Sobrien#include <sys/cdefs.h> 42114515Sobrien__FBSDID("$FreeBSD$"); 431558Srgrimes 4479154Stmm#include <sys/types.h> 451558Srgrimes#include <sys/msgbuf.h> 4679154Stmm#include <sys/sysctl.h> 471558Srgrimes 48156076Sdwmalone#include <ctype.h> 4916497Salex#include <err.h> 50125610Siedowse#include <errno.h> 511558Srgrimes#include <fcntl.h> 521558Srgrimes#include <kvm.h> 53125610Siedowse#include <limits.h> 5418439Sbde#include <locale.h> 55102069Sbde#include <nlist.h> 561558Srgrimes#include <stdio.h> 57250430Seadler#include <stdbool.h> 581558Srgrimes#include <stdlib.h> 59125497Siedowse#include <string.h> 601558Srgrimes#include <unistd.h> 611558Srgrimes#include <vis.h> 6273076Sru#include <sys/syslog.h> 631558Srgrimes 64227081Sedstatic struct nlist nl[] = { 651558Srgrimes#define X_MSGBUF 0 66227081Sed { "_msgbufp", 0, 0, 0, 0 }, 67140383Sdelphij { NULL, 0, 0, 0, 0 }, 681558Srgrimes}; 691558Srgrimes 7092697Simpvoid usage(void) __dead2; 711558Srgrimes 721558Srgrimes#define KREAD(addr, var) \ 731558Srgrimes kvm_read(kd, addr, &var, sizeof(var)) != sizeof(var) 741558Srgrimes 751558Srgrimesint 7692697Simpmain(int argc, char *argv[]) 771558Srgrimes{ 781558Srgrimes struct msgbuf *bufp, cur; 79125610Siedowse char *bp, *ep, *memf, *nextp, *nlistf, *p, *q, *visbp; 801558Srgrimes kvm_t *kd; 81125497Siedowse size_t buflen, bufpos; 82125610Siedowse long pri; 83250430Seadler int ch, clear; 84250430Seadler bool all; 851558Srgrimes 86250430Seadler all = false; 87250430Seadler clear = false; 8811749Sache (void) setlocale(LC_CTYPE, ""); 891558Srgrimes memf = nlistf = NULL; 90250430Seadler while ((ch = getopt(argc, argv, "acM:N:")) != -1) 911558Srgrimes switch(ch) { 9270123Sphk case 'a': 93250430Seadler all = true; 9470123Sphk break; 95250430Seadler case 'c': 96250430Seadler clear = true; 97250430Seadler break; 981558Srgrimes case 'M': 991558Srgrimes memf = optarg; 1001558Srgrimes break; 1011558Srgrimes case 'N': 1021558Srgrimes nlistf = optarg; 1031558Srgrimes break; 1041558Srgrimes case '?': 1051558Srgrimes default: 1061558Srgrimes usage(); 1071558Srgrimes } 1081558Srgrimes argc -= optind; 109136491Sschweikh if (argc != 0) 110136491Sschweikh usage(); 1111558Srgrimes 112127448Sru if (memf == NULL) { 113125610Siedowse /* 114125610Siedowse * Running kernel. Use sysctl. This gives an unwrapped 115125610Siedowse * buffer as a side effect. 116125610Siedowse */ 11779154Stmm if (sysctlbyname("kern.msgbuf", NULL, &buflen, NULL, 0) == -1) 11879154Stmm err(1, "sysctl kern.msgbuf"); 119288498Svangyzen /* Allocate extra room for growth between the sysctl calls. */ 120288498Svangyzen buflen += buflen/8; 121288498Svangyzen /* Allocate more than sysctl sees, for room to append \n\0. */ 122125610Siedowse if ((bp = malloc(buflen + 2)) == NULL) 12379154Stmm errx(1, "malloc failed"); 12479154Stmm if (sysctlbyname("kern.msgbuf", bp, &buflen, NULL, 0) == -1) 12579154Stmm err(1, "sysctl kern.msgbuf"); 126251618Sflo if (clear) 127251618Sflo if (sysctlbyname("kern.msgbuf_clear", NULL, NULL, &clear, sizeof(int))) 128251618Sflo err(1, "sysctl kern.msgbuf_clear"); 12979154Stmm } else { 130125610Siedowse /* Read in kernel message buffer and do sanity checks. */ 13179154Stmm kd = kvm_open(nlistf, memf, NULL, O_RDONLY, "dmesg"); 13279154Stmm if (kd == NULL) 13379154Stmm exit (1); 13479154Stmm if (kvm_nlist(kd, nl) == -1) 13579154Stmm errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 13679154Stmm if (nl[X_MSGBUF].n_type == 0) 13779154Stmm errx(1, "%s: msgbufp not found", 13879154Stmm nlistf ? nlistf : "namelist"); 13979154Stmm if (KREAD(nl[X_MSGBUF].n_value, bufp) || KREAD((long)bufp, cur)) 14079154Stmm errx(1, "kvm_read: %s", kvm_geterr(kd)); 14179154Stmm if (cur.msg_magic != MSG_MAGIC) 14279154Stmm errx(1, "kernel message buffer has different magic " 14379154Stmm "number"); 144125610Siedowse if ((bp = malloc(cur.msg_size + 2)) == NULL) 14579154Stmm errx(1, "malloc failed"); 146125610Siedowse 147125497Siedowse /* Unwrap the circular buffer to start from the oldest data. */ 148125497Siedowse bufpos = MSGBUF_SEQ_TO_POS(&cur, cur.msg_wseq); 149125497Siedowse if (kvm_read(kd, (long)&cur.msg_ptr[bufpos], bp, 150140383Sdelphij cur.msg_size - bufpos) != (ssize_t)(cur.msg_size - bufpos)) 15179154Stmm errx(1, "kvm_read: %s", kvm_geterr(kd)); 152125497Siedowse if (bufpos != 0 && kvm_read(kd, (long)cur.msg_ptr, 153140383Sdelphij &bp[cur.msg_size - bufpos], bufpos) != (ssize_t)bufpos) 154125497Siedowse errx(1, "kvm_read: %s", kvm_geterr(kd)); 15579154Stmm kvm_close(kd); 15679154Stmm buflen = cur.msg_size; 15779154Stmm } 1581558Srgrimes 1591558Srgrimes /* 160125610Siedowse * Ensure that the buffer ends with a newline and a \0 to avoid 161125610Siedowse * complications below. We left space above. 162125610Siedowse */ 163125610Siedowse if (buflen == 0 || bp[buflen - 1] != '\n') 164125610Siedowse bp[buflen++] = '\n'; 165125610Siedowse bp[buflen] = '\0'; 166125610Siedowse 167125610Siedowse if ((visbp = malloc(4 * buflen + 1)) == NULL) 168125610Siedowse errx(1, "malloc failed"); 169125610Siedowse 170125610Siedowse /* 171125497Siedowse * The message buffer is circular, but has been unwrapped so that 172125497Siedowse * the oldest data comes first. The data will be preceded by \0's 173125497Siedowse * if the message buffer was not full. 1741558Srgrimes */ 175125497Siedowse p = bp; 176125497Siedowse ep = &bp[buflen]; 177125497Siedowse if (*p == '\0') { 178125497Siedowse /* Strip leading \0's */ 179125610Siedowse while (*p == '\0') 180125497Siedowse p++; 181125497Siedowse } else if (!all) { 182125497Siedowse /* Skip the first line, since it is probably incomplete. */ 183136092Sstefanf p = memchr(p, '\n', ep - p); 184136092Sstefanf p++; 185125497Siedowse } 186125610Siedowse for (; p < ep; p = nextp) { 187136092Sstefanf nextp = memchr(p, '\n', ep - p); 188136092Sstefanf nextp++; 189125610Siedowse 190125497Siedowse /* Skip ^<[0-9]+> syslog sequences. */ 191156076Sdwmalone if (*p == '<' && isdigit(*(p+1))) { 192125610Siedowse errno = 0; 193125610Siedowse pri = strtol(p + 1, &q, 10); 194125610Siedowse if (*q == '>' && pri >= 0 && pri < INT_MAX && 195125610Siedowse errno == 0) { 196125497Siedowse if (LOG_FAC(pri) != LOG_KERN && !all) 197125497Siedowse continue; 198125497Siedowse p = q + 1; 19970123Sphk } 2001558Srgrimes } 201125610Siedowse 202125610Siedowse (void)strvisx(visbp, p, nextp - p, 0); 203125610Siedowse (void)printf("%s", visbp); 204125497Siedowse } 2051558Srgrimes exit(0); 2061558Srgrimes} 2071558Srgrimes 2081558Srgrimesvoid 20992697Simpusage(void) 2101558Srgrimes{ 211250430Seadler fprintf(stderr, "usage: dmesg [-ac] [-M core [-N system]]\n"); 2121558Srgrimes exit(1); 2131558Srgrimes} 214