linux logging框架
创始人
2024-06-02 10:36:32
0

引用

  • Linux Logging Complete Guide – devconnected

  • 内核printk原理介绍 - 知乎 (zhihu.com)

  • Message logging with printk — The Linux Kernel documentation

  • How to get printk format specifiers right — The Linux Kernel documentation

  • (24条消息) Linux pstore 实现自动“抓捕”内核崩溃日志_宋宝华的博客-CSDN博客

  • linux内核调试(三)内核崩溃日志抓取pstore - 知乎 (zhihu.com)

  • The GNU C Library

  • util-linux/dmesg.c at master · util-linux/util-linux (github.com)

  • BusyBox

  • klogd.c « sysklogd - busybox - BusyBox: The Swiss Army Knife of Embedded Linux

  • The rocket-fast Syslog Server - rsyslog

  • rsyslog/rsyslog: a Rocket-fast SYStem for LOG processing (github.com)

  • kernel - What is the difference between /proc/kmsg and /dev/kmsg? - Unix & Linux Stack Exchange

  • syslog-ng/syslog-ng: syslog-ng is an enhanced log daemon, supporting a wide range of input and output methods: syslog, unstructured text, queueing, SQL & NoSQL. (github.com)

  • Kernel logging: APIs and implementation - IBM Developer

  • syslog(2) - Linux manual page (man7.org)

  • syslog(3) - Linux manual page (man7.org)

  • dmesg(1) - Linux manual page (man7.org)

  • logrotate(8) - Linux manual page (man7.org)

  • Linux内核基础篇——printk调试 (betheme.net)


一. overall

二. 接口差异

2.1 /proc/kmsg 和 /dev/kmsg 的差异?

  • /proc/kmsg provides a root-only, read-only, consuming view of the kernel log buffer. It’s equivalent to calling syslog(2) with the SYSLOG_ACTION_READ action. As mentioned in the proc manpage,

A process must have superuser privileges to read this file, and only one process should read this file. This file should not be read if a syslog process is running which uses the syslog(2) system call facility to log kernel messages.

  • /dev/kmsg provides access to the same kernel log buffer, but in an easier-to-use fashion. Reads are tracked per open, so multiple processes can read in parallel, and entries aren’t removed from the buffer as they are read. /dev/kmsg also provides write access to the log buffer, so it can be used to add entries to the log buffer. See the /dev/kmsg documentation for details.

  • As for why both are present, and why one is in /proc (albeit not process-related) and one in dev, /proc/kmsg is an old convenience “export” of kernel internals, and /dev/kmsg is a more recent addition, designed as a usable interface to the log buffer.

2.2 系统调用 syslog()?

The syslog call serves as the input/output (I/O) and control interface to the kernel's log message ring buffer. From the syslog call, an application can read log messages (partial, in their entirety, or only new messages) as well as control the behavior of the ring buffer (clear contents, set the level of messages to be logged, enable or disable console, and so on).

  • syslog(2) vs. syslog(3)

Note that the syslog defined here ( syslog(2)) is different from the API for sending messages to the system logger ( syslog(3)). The latter allows messages to be sent to the syslog (through functions to open, close, and write to the log using a particular priority).

  • syslog(2) - 调用系统调用 syslog().

/** syslog, klogctl - read and/or clear kernel message ring buffer;* set console_loglevel*/#include         /* Definition of SYSLOG_* constants */
#include      /* Definition of SYS_* constants */
#include int syscall(SYS_syslog, int type, char *bufp, int len);/* The glibc interface */
#include int klogctl(int type, char *bufp, int len);
  • syslog(3) - 向系统logger发送日志,例如,klogd使用该组接口与syslogd对接,写入log到syslogd中。

/** closelog, openlog, syslog, vsyslog - send messages to the system logger*/#include void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);void vsyslog(int priority, const char *format, va_list ap);

2.3 glibc提供的 klogctl()?

The syslog call (called do_syslog within the kernel in ./linux/kernel/printk.c) is a relatively small function that provides the ability to read and control the kernel ring buffer. Note that in glibc 2.0, this function is called klogctl because of overuse of the term syslog, which refers to a variety of calls and applications. The prototype function (in user space) for syslog and klogctl is defined as:

int syslog( int type, char ∗bufp, int len );
int klogctl( int type, char ∗bufp, int len );

2.4 klogd和syslogd的关系与作用?

syslogd和klogd是很有意思的守护进程,syslogd是一个分发器,它将接收到的所有日志按照/etc/syslog.conf的配置策略发送到这些日志应该去的地方,当然也包括从klogd接收到的日志。klogd首先接收内核的日志,然后将之发送给syslogd。

syslogd日志记录器由两个守护进程(klogd,syslogd)和一个配置文件(syslog.conf)组成。klogd不使用配置文件,它负责截获内核消息,它既可以独立使用也可以作为syslogd的客户端运行。syslogd默认使用/etc/syslog.conf作为配置文件,负责截获应用程序消息,还可以截获klogd向其转发的内核消息。支持internet/unix domain sockets的特性使得这两个工具可以用于记录本地和远程的日志。

klogd会调用 glibc中的api,openlog 、syslog 、closelog 接口,将kernel log发送到 syslogd。

2.5 console log level?

printk() is typically used like this:

printk(KERN_INFO "Message: %s\n", arg);

where KERN_INFO is the log level (note that it’s concatenated to the format string, the log level is not a separate argument). The available log levels are:

Name

String

Alias function

KERN_EMERG

“0”

pr_emerg()

KERN_ALERT

“1”

pr_alert()

KERN_CRIT

“2”

pr_crit()

KERN_ERR

“3”

pr_err()

KERN_WARNING

“4”

pr_warn()

KERN_NOTICE

“5”

pr_notice()

KERN_INFO

“6”

pr_info()

KERN_DEBUG

“7”

pr_debug() and pr_devel() if DEBUG is defined

KERN_DEFAULT

“”

KERN_CONT

“c”

pr_cont()

The log level specifies the importance of a message. The kernel decides whether to show the message immediately (printing it to the current console) depending on its log level and the current console_loglevel (a kernel variable). If the message priority is higher (lower log level value) than the console_loglevel the message will be printed to the console.

If the log level is omitted, the message is printed with KERN_DEFAULT level.

You can check the current console_loglevel with:

    $ cat /proc/sys/kernel/printk4        4        1        7

The result shows the current, default, minimum and boot-time-default log levels.

即默认只有0-3等级的log才能输出到串口或者console。

To change the current console_loglevel simply write the desired level to /proc/sys/kernel/printk. For example, to print all messages to the console:

# echo 8 > /proc/sys/kernel/printk

Another way, using dmesg:

# dmesg -n 5

sets the console_loglevel to print KERN_WARNING (4) or more severe messages to console. See dmesg(1) for more information.

或者在启动命令行中添加配置:

通过在启动内核时传递commandline给内核的方法来修改系统默认的输出等级。例如,使用uboot引导内核时,可以在uboot传参的bootargs参数上,加上“loglevel=8”,这样在系统启动时,就打开了所有内核输出。

2.6 kmsg dump接口

kmsg dump接口主要提供给pstore和mtdoops使用,mtdoops用于在系统oops时将log buffer中的信息保存到mtd设备中,在当前内核版本中pstore也已支持将log保存到mtd中,因此pstore可以代替mtdoops的功能了,因此本文主要介绍pstore的log dump功能。

pstore的主要作用是在系统panic或oops时,将系统崩溃日志保存到backend设备中,以供调试分析。backend设备可以是重启不掉电的ram,mtd或者块设备,根据不同的配置可以选择使用不同的后端设备。同时pstore前端除了panic和oops之外,还支持通过ftrace,pmsg和console,如ftrace前端用于将ftrace信息保存到pstore中,pmsg前端用于将用户态信息保存到pstore中。

相关内容

热门资讯

Python|位运算|数组|动... 目录 1、只出现一次的数字(位运算,数组) 示例 选项代...
张岱的人物生平 张岱的人物生平张岱(414年-484年),字景山,吴郡吴县(今江苏苏州)人。南朝齐大臣。祖父张敞,东...
西游西后传演员女人物 西游西后传演员女人物西游西后传演员女人物 孙悟空 六小龄童 唐僧 徐少华 ...
名人故事中贾岛作诗内容简介 名人故事中贾岛作诗内容简介有一次,贾岛骑驴闯了官道.他正琢磨着一句诗,名叫《题李凝幽居》全诗如下:闲...
和男朋友一起优秀的文案? 和男朋友一起优秀的文案?1.希望是惟一所有的人都共同享有的好处;一无所有的人,仍拥有希望。2.生活,...
戴玉手镯的好处 戴玉手镯好还是... 戴玉手镯的好处 戴玉手镯好还是碧玺好 女人戴玉?戴玉好还是碧玺好点佩戴手镯,以和田玉手镯为佳!相嫌滑...
依然什么意思? 依然什么意思?依然(汉语词语)依然,汉语词汇。拼音:yī    rán基本解释:副词,指照往常、依旧...
高尔基的散文诗 高尔基的散文诗《海燕》、《大学》、《母亲》、《童年》这些都是比较出名的一些代表作。
心在飞扬作者简介 心在飞扬作者简介心在飞扬作者简介如下。根据相关公开资料查询,心在飞扬是一位优秀的小说作者,他的小说作...
卡什坦卡的故事赏析? 卡什坦卡的故事赏析?讲了一只小狗的故事, 我也是近来才读到这篇小说. 作家对动物的拟人描写真是惟妙...
林绍涛为简艾拿绿豆糕是哪一集 林绍涛为简艾拿绿豆糕是哪一集第三十二集。 贾宽认为是阎帅间接导致刘映霞住了院,第二天上班,他按捺不...
小爱同学是女生吗小安同学什么意... 小爱同学是女生吗小安同学什么意思 小爱同学,小安同学说你是女生。小安是男的。
内分泌失调导致脸上长斑,怎么调... 内分泌失调导致脸上长斑,怎么调理内分泌失调导致脸上长斑,怎么调理先调理内分泌,去看中医吧,另外用好的...
《魔幻仙境》刺客,骑士人物属性... 《魔幻仙境》刺客,骑士人物属性加点魔幻仙境骑士2功1体质
很喜欢她,该怎么办? 很喜欢她,该怎么办?太冷静了!! 太理智了!爱情是需要冲劲的~不要考虑着考虑那~否则缘...
言情小说作家 言情小说作家我比较喜欢匪我思存的,很虐,很悲,还有梅子黄时雨,笙离,叶萱,还有安宁的《温暖的玄》 小...
两个以名人的名字命名的风景名胜... 两个以名人的名字命名的风景名胜?快太白楼,李白。尚志公园,赵尚志。
幼儿教育的代表人物及其著作 幼儿教育的代表人物及其著作卡尔威特的《卡尔威特的教育》,小卡尔威特,他儿子成了天才后写的《小卡尔威特...
海贼王中为什么说路飞打凯多靠霸... 海贼王中为什么说路飞打凯多靠霸气升级?凯多是靠霸气升级吗?因为之前刚到时确实打不过人家因为路飞的实力...
运气不好拜财神有用吗运气不好拜... 运气不好拜财神有用吗运气不好拜财神有没有用1、运气不好拜财神有用。2、拜财神上香前先点蜡烛,照亮人神...