1 /* vi: set sw=4 ts=4: */
4 * dmesg - display/control kernel ring buffer.
6 * Copyright 2006 Rob Landley <rob@landley.net>
7 * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at>
9 * Licensed under GPLv2, see file LICENSE in this source tree.
12 //config: bool "dmesg (3.5 kb)"
14 //config: select PLATFORM_LINUX
16 //config: dmesg is used to examine or control the kernel ring buffer. When the
17 //config: Linux kernel prints messages to the system log, they are stored in
18 //config: the kernel ring buffer. You can use dmesg to print the kernel's ring
19 //config: buffer, clear the kernel ring buffer, change the size of the kernel
20 //config: ring buffer, and change the priority level at which kernel messages
21 //config: are also logged to the system console. Enable this option if you
22 //config: wish to enable the 'dmesg' utility.
24 //config:config FEATURE_DMESG_PRETTY
25 //config: bool "Pretty output"
27 //config: depends on DMESG
29 //config: If you wish to scrub the syslog level from the output, say 'Y' here.
30 //config: The syslog level is a string prefixed to every line with the form
33 //config: With this option you will see:
35 //config: Linux version 2.6.17.4 .....
36 //config: BIOS-provided physical RAM map:
37 //config: BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
39 //config: Without this option you will see:
41 //config: <5>Linux version 2.6.17.4 .....
42 //config: <6>BIOS-provided physical RAM map:
43 //config: <6> BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
45 //applet:IF_DMESG(APPLET(dmesg, BB_DIR_BIN, BB_SUID_DROP))
47 //kbuild:lib-$(CONFIG_DMESG) += dmesg.o
49 //usage:#define dmesg_trivial_usage
50 //usage: "[-c] [-n LEVEL] [-s SIZE]"
51 //usage:#define dmesg_full_usage "\n\n"
52 //usage: "Print or control the kernel ring buffer\n"
53 //usage: "\n -c Clear ring buffer after printing"
54 //usage: "\n -n LEVEL Set console logging level"
55 //usage: "\n -s SIZE Buffer size"
56 //usage: "\n -r Print raw message buffer"
61 int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
62 int dmesg_main(int argc UNUSED_PARAM, char **argv)
74 opts = getopt32(argv, "cs:+n:+r", &len, &level);
76 if (klogctl(8, NULL, (long) level))
77 bb_perror_msg_and_die("klogctl");
82 len = klogctl(10, NULL, 0); /* read ring buffer size */
85 if (len > 16*1024*1024)
89 len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
91 bb_perror_msg_and_die("klogctl");
96 if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
100 /* Skip <[0-9]+> at the start of lines */
102 if (last == '\n' && buf[in] == '<') {
103 while (buf[in++] != '>' && in < len)
112 /* Make sure we end with a newline */
116 full_write(STDOUT_FILENO, buf, len);
117 if (buf[len-1] != '\n')
121 if (ENABLE_FEATURE_CLEAN_UP) free(buf);