blob: 6670b84dee3bf977cd78f69e7bee9217d3cbcb11 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00003 *
Rob Landley7c7b0d72006-06-13 18:31:04 +00004 * dmesg - display/control kernel ring buffer.
Eric Andersene77ae3a1999-10-19 20:03:34 +00005 *
Rob Landley446129a2006-07-27 16:40:55 +00006 * Copyright 2006 Rob Landley <rob@landley.net>
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00007 * Copyright 2006 Bernhard Reutner-Fischer <rep.nop@aon.at>
Eric Andersen2afcbe42003-03-07 17:33:40 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersencc8ed391999-10-05 16:24:54 +000010 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010011//config:config DMESG
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "dmesg (3.7 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config: default y
Denys Vlasenkodd898c92016-11-23 11:46:32 +010014//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: dmesg is used to examine or control the kernel ring buffer. When the
16//config: Linux kernel prints messages to the system log, they are stored in
17//config: the kernel ring buffer. You can use dmesg to print the kernel's ring
18//config: buffer, clear the kernel ring buffer, change the size of the kernel
19//config: ring buffer, and change the priority level at which kernel messages
20//config: are also logged to the system console. Enable this option if you
21//config: wish to enable the 'dmesg' utility.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010022//config:
23//config:config FEATURE_DMESG_PRETTY
Denys Vlasenkof5604222017-01-10 14:58:54 +010024//config: bool "Pretty output"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010025//config: default y
26//config: depends on DMESG
27//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020028//config: If you wish to scrub the syslog level from the output, say 'Y' here.
29//config: The syslog level is a string prefixed to every line with the form
30//config: "<#>".
Denys Vlasenkodd898c92016-11-23 11:46:32 +010031//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020032//config: With this option you will see:
33//config: # dmesg
34//config: Linux version 2.6.17.4 .....
35//config: BIOS-provided physical RAM map:
36//config: BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
Denys Vlasenkodd898c92016-11-23 11:46:32 +010037//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020038//config: Without this option you will see:
39//config: # dmesg
40//config: <5>Linux version 2.6.17.4 .....
41//config: <6>BIOS-provided physical RAM map:
42//config: <6> BIOS-e820: 0000000000000000 - 000000000009f000 (usable)
Denys Vlasenkodd898c92016-11-23 11:46:32 +010043
44//applet:IF_DMESG(APPLET(dmesg, BB_DIR_BIN, BB_SUID_DROP))
45
46//kbuild:lib-$(CONFIG_DMESG) += dmesg.o
Pere Orga5bc8c002011-04-11 03:29:49 +020047
48//usage:#define dmesg_trivial_usage
Denys Vlasenkoe2b92152021-06-14 20:47:20 +020049//usage: "[-cr] [-n LEVEL] [-s SIZE]"
Pere Orga5bc8c002011-04-11 03:29:49 +020050//usage:#define dmesg_full_usage "\n\n"
51//usage: "Print or control the kernel ring buffer\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020052//usage: "\n -c Clear ring buffer after printing"
53//usage: "\n -n LEVEL Set console logging level"
54//usage: "\n -s SIZE Buffer size"
Peter Korsgaard864d1b72015-08-24 15:54:49 +020055//usage: "\n -r Print raw message buffer"
Pere Orga5bc8c002011-04-11 03:29:49 +020056
Eric Andersen85e5e722003-07-22 08:56:55 +000057#include <sys/klog.h>
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000058#include "libbb.h"
Eric Andersene76c3b02001-04-05 03:14:39 +000059
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000060int dmesg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061int dmesg_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000062{
Denys Vlasenko874201f2009-07-10 14:14:14 +020063 int len, level;
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000064 char *buf;
Denys Vlasenko874201f2009-07-10 14:14:14 +020065 unsigned opts;
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000066 enum {
Denys Vlasenko874201f2009-07-10 14:14:14 +020067 OPT_c = 1 << 0,
68 OPT_s = 1 << 1,
Peter Korsgaard864d1b72015-08-24 15:54:49 +020069 OPT_n = 1 << 2,
70 OPT_r = 1 << 3
Bernhard Reutner-Fischerf02efd12008-05-19 10:28:32 +000071 };
Erik Andersen1266a131999-12-29 22:19:46 +000072
Denys Vlasenko237bedd2016-07-06 21:58:02 +020073 opts = getopt32(argv, "cs:+n:+r", &len, &level);
Denys Vlasenko874201f2009-07-10 14:14:14 +020074 if (opts & OPT_n) {
75 if (klogctl(8, NULL, (long) level))
James Byrne69374872019-07-02 11:35:03 +020076 bb_simple_perror_msg_and_die("klogctl");
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000077 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000079
Denys Vlasenko874201f2009-07-10 14:14:14 +020080 if (!(opts & OPT_s))
81 len = klogctl(10, NULL, 0); /* read ring buffer size */
82 if (len < 16*1024)
83 len = 16*1024;
84 if (len > 16*1024*1024)
85 len = 16*1024*1024;
86
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000087 buf = xmalloc(len);
Denys Vlasenko874201f2009-07-10 14:14:14 +020088 len = klogctl(3 + (opts & OPT_c), buf, len); /* read ring buffer */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000089 if (len < 0)
James Byrne69374872019-07-02 11:35:03 +020090 bb_simple_perror_msg_and_die("klogctl");
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000091 if (len == 0)
92 return EXIT_SUCCESS;
93
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000094
Peter Korsgaard864d1b72015-08-24 15:54:49 +020095 if (ENABLE_FEATURE_DMESG_PRETTY && !(opts & OPT_r)) {
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +000096 int last = '\n';
97 int in = 0;
98
Peter Korsgaard3917fa32013-01-06 00:07:17 +010099 /* Skip <[0-9]+> at the start of lines */
Denys Vlasenkof04ca742010-10-19 23:08:33 +0200100 while (1) {
101 if (last == '\n' && buf[in] == '<') {
Peter Korsgaard3917fa32013-01-06 00:07:17 +0100102 while (buf[in++] != '>' && in < len)
103 ;
104 } else {
105 last = buf[in++];
106 putchar(last);
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000107 }
Denys Vlasenkof04ca742010-10-19 23:08:33 +0200108 if (in >= len)
109 break;
110 }
111 /* Make sure we end with a newline */
Denis Vlasenko7e3a5f52007-11-16 20:18:54 +0000112 if (last != '\n')
113 bb_putchar('\n');
114 } else {
115 full_write(STDOUT_FILENO, buf, len);
116 if (buf[len-1] != '\n')
117 bb_putchar('\n');
118 }
119
120 if (ENABLE_FEATURE_CLEAN_UP) free(buf);
121
122 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000123}