sunsili 发表于 2023-6-7 14:48:24

【Linux命令】诊断系统故障命令dmesg详解

本帖最后由 sunsili 于 2023-7-1 22:34 编辑

【Linux命令】诊断系统故障命令dmesg详解

前言

本文讲解诊断系统故障命令dmesg
dmesg命令简介
dmesg 命令用于显示系统开机信息,可用于诊断系统故障。内核会将系统开机信息存储在ring buffer中,可以使用dmesg命令来查看,开机信息保存在/var/log/dmesg文件中。
dmesg用法
查看帮助
dmesg -h
Usage:
dmesg

Display or control the kernel ring buffer.

Options:
-C, --clear               clear the kernel ring buffer
-c, --read-clear            read and clear all messages
-D, --console-off         disable printing messages to console
-E, --console-on            enable printing messages to console
-F, --file <file>         use the file instead of the kernel log buffer
-f, --facility <list>       restrict output to defined facilities
-H, --human               human readable output
-k, --kernel                display kernel messages
-L, --color[=<when>]      colorize messages (auto, always or never)
                               colors are enabled by default
-l, --level <list>          restrict output to defined levels
-n, --console-level <level> set level of messages printed to console
-P, --nopager               do not pipe output into a pager
-p, --force-prefix          force timestamp output on each line of multi-line messages
-r, --raw                   print the raw message buffer
-S, --syslog                force to use syslog(2) rather than /dev/kmsg
-s, --buffer-size <size>    buffer size to query the kernel ring buffer
-u, --userspace             display userspace messages
-w, --follow                wait for new messages
-x, --decode                decode facility and level to readable string
-d, --show-delta            show time delta between printed messages
-e, --reltime               show local time and time delta in readable format
-T, --ctime               show human-readable timestamp (may be inaccurate!)
-t, --notime                don't show any timestamp with messages
   --time-format <format>show timestamp using the given format:
                              
Suspending/resume will make ctime and iso timestamps inaccurate.

-h, --help                  display this help
-V, --version               display version

Supported log facilities:
    kern - kernel messages
    user - random user-level messages
    mail - mail system
daemon - system daemons
    auth - security/authorization messages
syslog - messages generated internally by syslogd
   lpr - line printer subsystem
    news - network news subsystem

Supported log levels (priorities):
   emerg - system is unusable
   alert - action must be taken immediately
    crit - critical conditions
   err - error conditions
    warn - warning conditions
notice - normal but significant condition
    info - informational
   debug - debug-level messages

For more details see dmesg(1).

dmesg帮助信息解释
命令语法dmesg
选项说明-c#显示信息后,清除ring buffer中的内容-s<缓冲区大小>#默认值为8196,刚好等于ring buffer的大小-n#设置记录信息的层级-D#禁用打印消息到控制台-E#启用打印消息到控制台-h#打印帮助文本并退出-k#打印内核消息-n#设置将消息记录到控制台的级别-r#打印原始消息缓冲区-s#使用多少大小的缓冲区来查询内核环缓冲区。默认情况下为16392-T#打印人类可读时间戳-t#不打印内核的时间戳-u#打印用户空间消息-V#输出版本信息并退出-x#将设施和级别(优先级)编号解码为可读的前缀
应用举例

在openwrt上查看开机信息dmesg
[    0.000000] Linux version 5.4.143 (*******) (gcc version 8.4.0 (OpenWrt GCC 8.4.0 r16279-5cc                                                                                                                              0535800)) #0 Tue Aug 31 22:20:08 2021
[    0.000000] Board has DDR2
[    0.000000] Analog PMU set to hw control
[    0.000000] Digital PMU set to hw control
[    0.000000] SoC Type: MediaTek MT7628AN ver:1 eco:2
[    0.000000] printk: bootconsole enabled
[    0.000000] CPU0 revision is: 00019655 (MIPS 24KEc)
[    0.000000] MIPS: machine is Jotale JS76x8 (32M)
[    0.000000] Initrd not found or empty - disabling initrd
[    0.000000] Primary instruction cache 64kB, VIPT, 4-way, linesize 32 bytes.
[    0.000000] Primary data cache 32kB, 4-way, PIPT, no aliases, linesize 32 bytes
[    0.000000] Zone ranges:
[    0.000000]   Normal   
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0:
[    0.000000] Initmem setup node 0
[    0.000000] On node 0 totalpages: 32768
[    0.000000]   Normal zone: 288 pages used for memmap
[    0.000000]   Normal zone: 0 pages reserved
[    0.000000]   Normal zone: 32768 pages, LIFO batch:7
[    0.000000] pcpu-alloc: s0 r0 d32768 u32768 alloc=1*32768
[    0.000000] pcpu-alloc: 0
[    0.000000] Built 1 zonelists, mobility grouping on.Total pages: 32480
[    0.000000] Kernel command line: console=ttyS0,115200 rootfstype=squashfs,jffs2
[    0.000000] Dentry cache hash table entries: 16384 (order: 4, 65536 bytes, linear)
[    0.000000] Inode-cache hash table entries: 8192 (order: 3, 32768 bytes, linear)
[    0.000000] Writing ErrCtl register=00000000
[    0.000000] Readback ErrCtl register=00000000
[    0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
*******
[   11.641746] usbcore: registered new interface driver cdc_subset
[   11.689505] PPP generic driver version 2.4.2
[   11.703356] usbcore: registered new interface driver qmi_wwan
[   11.730537] rndis_host 1-1:1.0 eth1: register 'rndis_host' at usb-101c0000.ehci-1, RNDIS device, ac:4                                                                                                                              b:b3:b9:eb:e5
[   11.741397] usbcore: registered new interface driver rndis_host
[   11.789009] kmodloader: done loading kernel modules from /etc/modules.d/*
[   11.855712] urngd: v1.0.2 started.
[   12.515084] random: crng init done
[   12.518593] random: 1 urandom warning(s) missed due to ratelimiting
[   19.978363] rt3050-esw 10110000.esw: link changed 0x00
[   22.202128] br-lan: port 1(eth0.1) entered blocking state
[   22.207696] br-lan: port 1(eth0.1) entered disabled state
[   22.213547] device eth0.1 entered promiscuous mode
[   22.218473] device eth0 entered promiscuous mode
[   22.255696] br-lan: port 1(eth0.1) entered blocking state
[   22.261264] br-lan: port 1(eth0.1) entered forwarding state
[   23.004559] rt3050-esw 10110000.esw: link changed 0x01

这样打印信息不方便查找关键信息

查看前25行开机信息
dmesg | head -n 25
[    0.000000] Linux version 5.4.143 (fan@FAN-THINK-PC) (gcc version 8.4.0 (OpenWrt GCC 8.4.0 r16279-5cc0535800)) #0 Tue Aug 31 22:20:08 2021
[    0.000000] Board has DDR2
[    0.000000] Analog PMU set to hw control
[    0.000000] Digital PMU set to hw control
[    0.000000] SoC Type: MediaTek MT7628AN ver:1 eco:2
[    0.000000] printk: bootconsole enabled
[    0.000000] CPU0 revision is: 00019655 (MIPS 24KEc)
[    0.000000] MIPS: machine is Jotale JS76x8 (32M)
[    0.000000] Initrd not found or empty - disabling initrd
[    0.000000] Primary instruction cache 64kB, VIPT, 4-way, linesize 32 bytes.
[    0.000000] Primary data cache 32kB, 4-way, PIPT, no aliases, linesize 32 bytes
[    0.000000] Zone ranges:
[    0.000000]   Normal   
[    0.000000] Movable zone start for each node
[    0.000000] Early memory node ranges
[    0.000000]   node   0:
[    0.000000] Initmem setup node 0
[    0.000000] On node 0 totalpages: 32768
[    0.000000]   Normal zone: 288 pages used for memmap
[    0.000000]   Normal zone: 0 pages reserved
[    0.000000]   Normal zone: 32768 pages, LIFO batch:7
[    0.000000] pcpu-alloc: s0 r0 d32768 u32768 alloc=1*32768
[    0.000000] pcpu-alloc: 0
[    0.000000] Built 1 zonelists, mobility grouping on.Total pages: 32480
[    0.000000] Kernel command line: console=ttyS0,115200 rootfstype=squashfs,jffs2

查看后25行开机信息

dmesg | tail -n 25
[   11.352236] GACT probability on
[   11.361732] Mirror/redirect action on
[   11.395872] u32 classifier
[   11.398710]   input device check on
[   11.402442]   Actions configured
[   11.463279] usbcore: registered new interface driver cdc_wdm
[   11.613359] xt_time: kernel timezone is -0000
[   11.626466] usbcore: registered new interface driver cdc_ether
[   11.641746] usbcore: registered new interface driver cdc_subset
[   11.689505] PPP generic driver version 2.4.2
[   11.703356] usbcore: registered new interface driver qmi_wwan
[   11.730537] rndis_host 1-1:1.0 eth1: register 'rndis_host' at usb-101c0000.ehci-1, RNDIS device, ac:4b:b3:b9:eb:e5
[   11.741397] usbcore: registered new interface driver rndis_host
[   11.789009] kmodloader: done loading kernel modules from /etc/modules.d/*
[   11.855712] urngd: v1.0.2 started.
[   12.515084] random: crng init done
[   12.518593] random: 1 urandom warning(s) missed due to ratelimiting
[   19.978363] rt3050-esw 10110000.esw: link changed 0x00
[   22.202128] br-lan: port 1(eth0.1) entered blocking state
[   22.207696] br-lan: port 1(eth0.1) entered disabled state
[   22.213547] device eth0.1 entered promiscuous mode
[   22.218473] device eth0 entered promiscuous mode
[   22.255696] br-lan: port 1(eth0.1) entered blocking state
[   22.261264] br-lan: port 1(eth0.1) entered forwarding state
[   23.004559] rt3050-esw 10110000.esw: link changed 0x01


查看与内存相关的开机信息
dmesg | grep -i memory
[    0.000000] Early memory node ranges
[    0.000000] Memory: 122688K/131072K available (4436K kernel code, 192K rwdata, 1016K rodata, 1192K init, 193K bss, 8384K reserved, 0K cma-reserved)
[    0.526438] Freeing unused kernel memory: 1192K
[    0.531064] This architecture does not have kernel memory protection.


查看与USB相关的开机信息
dmesg | grep -i usb
[    1.884358] usbcore: registered new interface driver usbfs
[    1.890096] usbcore: registered new interface driver hub
[    1.895617] usbcore: registered new device driver usb
[    1.907868] ehci_hcd: USB 2.0 'Enhanced' Host Controller (EHCI) Driver
[    1.939711] phy phy-10120000.usbphy.0: remote usb device wakeup disabled
[    1.946525] phy phy-10120000.usbphy.0: UTMI 16bit 30MHz
[    1.957713] ehci-platform 101c0000.ehci: new USB bus registered, assigned bus number 1
[    1.984466] ehci-platform 101c0000.ehci: USB 2.0 started, EHCI 1.00
[    1.991901] hub 1-0:1.0: USB hub found
[    4.628468] usb 1-1: new high-speed USB device number 2 using ehci-platform
[   11.463279] usbcore: registered new interface driver cdc_wdm
[   11.626466] usbcore: registered new interface driver cdc_ether
[   11.641746] usbcore: registered new interface driver cdc_subset
[   11.703356] usbcore: registered new interface driver qmi_wwan
[   11.730537] rndis_host 1-1:1.0 eth1: register 'rndis_host' at usb-101c0000.ehci-1, RNDIS device, ac:4b:b3:b9:eb:e5
[   11.741397] usbcore: registered new interface driver rndis_host
页: [1]
查看完整版本: 【Linux命令】诊断系统故障命令dmesg详解