00. 目录
01. 命令概述
dmesg命令被用于检查和控制内核的环形缓冲区。kernel会将开机信息存储在ring buffer中。您若是开机时来不及查看信息,可利用dmesg来查看。开机信息保存在/var/log/dmesg
文件里。
02. 命令格式
dmesg [选项]
- 1
03. 常用选项
选项:
-C, --clear 清除内核环形缓冲区(ring butter)
-c, --read-clear 读取并清除所有消息
-D, --console-off 禁止向终端打印消息
-d, --show-delta 显示打印消息之间的时间差
-e, --reltime 以易读格式显示本地时间和时间差
-E, --console-on 启用向终端打印消息
-F, --file <文件> 用 文件 代替内核日志缓冲区
-f, --facility <列表> 将输出限制为定义的设施
-H, --human 易读格式输出
-k, --kernel 显示内核消息
-L, --color 显示彩色消息
-l, --level <列表> 限制输出级别
-n, --console-level <级别> 设置打印到终端的消息级别
-P, --nopager 不将输出通过管道传递给分页程序
-r, --raw 打印原生消息缓冲区
-S, --syslog 强制使用 syslog(2) 而非 /dev/kmsg
-s, --buffer-size <大小> 查询内核环形缓冲区所用的缓冲区大小
-T, --ctime 显示易读的时间戳(如果您使用了
SUSPEND/RESUME 则可能不准)
-t, --notime 不打印消息时间戳
-u, --userspace 显示用户空间消息
-w, --follow 等待新消息
-x, --decode 将设施和级别解码为可读的字符串
-h, --help 显示此帮助并退出
-V, --version 输出版本信息并退出
支持的日志设施:
kern - 内核消息
user - 随机的用户级消息
mail - 邮件系统
daemon - 系统守护进程
auth - 安全/认证消息
syslog - syslogd 内部生成的消息
lpr - 行打印机子系统
news - 网络新闻子系统
支持的日志级别(优先级):
emerg - 系统无法使用
alert - 操作必须立即执行
crit - 紧急条件
err - 错误条件
warn - 警告条件
notice - 正常但重要的条件
info - 信息
debug - 调试级别的消息
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
04. 参考示例
4.1 列出加载到内核中的所有驱动
dmesg
命令设备故障的诊断是非常重要的。在dmesg
命令的帮助下进行硬件的连接或断开连接操作时,我们可以看到硬件的检测或者断开连接的信息。dmesg
命令在多数基于Linux和Unix的操作系统中都可以使用。
[root@itcast ~]# dmesg
- 1
4.2 列出所有被检测到的硬件
要显示所有被内核检测到的硬盘设备,你可以使用grep命令搜索sda
关键词
[root@itcast ~]# dmesg | grep sda
[ 3.051454] sd 0:0:0:0: [sda] 209715200 512-byte logical blocks: (107 GB/100 GiB)
[ 3.051559] sd 0:0:0:0: [sda] Write Protect is off
[ 3.051562] sd 0:0:0:0: [sda] Mode Sense: 61 00 00 00
[ 3.051700] sd 0:0:0:0: [sda] Cache data unavailable
[ 3.051702] sd 0:0:0:0: [sda] Assuming drive cache: write through
[ 3.053408] sda: sda1 sda2 sda3
[ 3.054299] sd 0:0:0:0: [sda] Attached SCSI disk
[ 3.558354] XFS (sda3): Mounting V5 Filesystem
[ 4.188185] XFS (sda3): Ending clean mount
[ 12.636995] Adding 4194300k swap on /dev/sda2. Priority:-1 extents:1 across:4194300k FS
[ 12.922553] XFS (sda1): Mounting V5 Filesystem
[ 14.708567] XFS (sda1): Ending clean mount
[root@itcast ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
4.3 只输出dmesg命令的前10行日志
[root@itcast ~]# dmesg | head
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 3.10.0-862.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Fri Apr 20 16:44:24 UTC 2018
[ 0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.10.0-862.el7.x86_64 root=UUID=6fa6fd5d-0115-4ec3-b993-19322c836a32 ro rhgb quiet LANG=zh_CN.UTF-8
[ 0.000000] Disabled fast string operations
[ 0.000000] e820: BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ebff] usable
[ 0.000000] BIOS-e820: [mem 0x000000000009ec00-0x000000000009ffff] reserved
[ 0.000000] BIOS-e820: [mem 0x00000000000dc000-0x00000000000fffff] reserved
[root@itcast ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4.4 只输出dmesg命令最后10行日志
[root@itcast ~]# dmesg | tail
[117804.072174] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[117804.078051] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[117804.079860] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[117804.079873] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[117831.955328] psmouse serio1: VMMouse at isa0060/serio1/input0 lost synchronization, throwing 2 bytes away.
[117833.738722] psmouse serio1: resync failed, issuing reconnect request
[124926.680233] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[124926.693764] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
[124932.700536] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
[124932.701799] IPv6: ADDRCONF(NETDEV_CHANGE): ens33: link becomes ready
[root@itcast ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4.5 搜索包含特定字符串的被检测到的硬件
由于‘dmesg’命令的输出实在太长了,在其中搜索某个特定的字符串是非常困难的。因此,有必要过滤出一些包含‘usb’ ‘dma’ ‘tty’ ‘memory’等字符串的日志行。grep 命令 的‘-i’选项表示忽略大小写。
[root@itcast ~]# dmesg | grep -i usb
[root@itcast ~]# dmesg | grep -i dma
[root@itcast ~]# dmesg | grep -i tty
[root@itcast ~]# dmesg | grep -i memory
- 1
- 2
- 3
- 4
4.6 清空dmesg缓冲区日志
[root@itcast ~]# dmesg -c
- 1
我们可以使用如下命令来清空dmesg的日志。该命令会清空dmesg环形缓冲区中的日志。但是你依然可以查看存储在‘/var/log/dmesg’文件中的日志。你连接任何的设备都会产生dmesg日志输出。
4.7 实时监控dmesg日志输出
[root@itcast ~]# tail -f /var/log/dmesg
[ 14.708567] XFS (sda1): Ending clean mount
[ 14.880408] floppy0: no floppy controllers found
[ 14.880489] work still pending
[ 14.988537] device-mapper: uevent: version 1.0.3
[ 14.988785] device-mapper: ioctl: 4.37.0-ioctl (2017-09-20) initialised: [email protected]
[ 15.604238] RPC: Registered named UNIX socket transport module.
[ 15.604240] RPC: Registered udp transport module.
[ 15.604241] RPC: Registered tcp transport module.
[ 15.604242] RPC: Registered tcp NFSv4.1 backchannel transport module.
[ 15.783627] type=1305 audit(1563324893.915:3): audit_pid=581 old=0 auid=4294967295 ses=4294967295 res=1
//或者
[root@itcast ~]# watch "dmesg |tail"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
05. 附录
参考:https://linux.cn/article-3587-1.html
~]# watch “dmesg |tail”
## 05. 附录
参考:[【Linux】一步一步学Linux系列教程汇总](http://iyenn.com/rec/1676766.html)
参考:https://linux.cn/article-3587-1.html
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
基本概念
OpenHarmony LiteOS-M提供异常接管调测手段,帮助开发者定位分析问题。异常接管是操作系统对运行期间发生的异常情况进行处理的一系列动作,例如打印异常发生时异常类型、发生异常时的系统状态、当前函数的调用栈信息、CPU现场信息、任务调用堆栈等信息。
运行机制
栈帧用于保存函数调用过程中的函数参数、变量、返回值等信息。调用函数时,会创建子函数的栈帧,同时将函数入参、局部变量、寄存器入栈。栈帧从高地址向低地址生长。以ARM32 CPU架构为例,每个栈帧中都会保存PC、LR、SP和FP寄存器的历史值。LR链接寄存器(Link Register)指向函数的返回地址,FP帧指针寄存器(Frame Point)指向当前函数的父函数的栈帧起始地址。利用FP寄存器可以得到父函数的栈帧,从栈帧中获取父函数的FP,就可以得到祖父函数的栈帧,以此类推,可以追溯程序调用栈,得到函数间的调用关系。
当系统发生异常时,系统打印异常函数的栈帧中保存的寄存器内容,以及父函数、祖父函数的栈帧中的LR链接寄存器、FP帧指针寄存器内容,用户就可以据此追溯函数间的调用关系,定位异常原因。
堆栈分析原理如下图所示,实际堆栈信息根据不同CPU架构有所差异,此处仅做示意。
图1 堆栈分析原理示意图
图中不同颜色的寄存器表示不同的函数。可以看到函数调用过程中,寄存器的保存。通过FP寄存器,栈回溯到异常函数的父函数,继续按照规律对栈进行解析,推出函数调用关系,方便用户定位问题。
接口说明
OpenHarmony LiteOS-M内核的回溯栈模块提供以下接口,接口详细信息可以查看API参考。
表1 回溯栈模块接口
class="table-box">接口名 | 功能 |
---|---|
LOS_BackTrace | 打印调用处的函数调用栈关系。 |
LOS_RecordLR | 在无法打印的场景,用该接口获取调用处的函数调用栈关系。 |
评论记录:
回复评论: