linux 自带强大调试工具sysRQ(高级片)

SysRq 是 Linux 内核提供的一种调试工具,通过启用 /proc/sys/kernel/sysrq,用户可以使用特定组合键进行系统控制,如强制同步、打印内存信息、挂起任务等。例如,'w' 键用于打印所有不可中断状态的任务,'t' 键用于显示当前任务列表。启用 SysRq 功能需要在 /etc/sysctl.conf 中设置 kernel.sysrq,并通过 /proc/sysrq-trigger 文件触发相应操作。这对于诊断和解决系统挂起等问题非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在linux系统下,我们可能会遇到系统某个命令hang住的情况,通常情况下,我们会查看/proc/pid/wchan文件,看看进程处于什么状况,然后进一步查看系统日志或者使用strace跟踪命令执行时的系统调用等等方法来分析问题。我们知道命令hang,一般问题都出在内核里, 如果我们能根据自己的需要打印内核的信息,可能会事半功倍,很快定位到问题。幸运的是linux真的有提供这样的方式,就是通过SysRq,今天在这里我就介绍一下。

1. SysRq的启用与关闭 

要想启用SysRq, 需要在配置内核时设置Magic SysRq key (CONFIG_MAGIC_SYSRQ)为Y. 对于支持SysRq的内核, /proc/sys/kernel/sysrq控制SysRq的启用与否. 如果/proc/sys/kernel/sysrq内容为0, 则SysRq被禁用; 如果/proc/sys/kernel/sysrq内容为1, 则SysRq被启用. 关于/proc/sys/kernel/sysrq的更多描述, 请参考<Kernel Source>/Documentation/sysrq.txt 
https://www.kernel.org/doc/Documentation/sysrq.txt

可通过运行命令echo "0" >/proc/sys/kernel/sysrq和echo "1" >/proc/sys/kernel/sysrq暂时启用或禁用SysRq. 如果需要永久启用或者禁用SysRqs, 则可在/etc/sysctl.conf中设置kernel.sysrq = 1 (启用SsyRq)或kernel.sysrq = 0 (禁用SysRq)。另外,/proc/sys/kernel/sysrq除了可以用来启用和禁用SysRq之外,还可以控制以下内容:
          2 =   0x2 - enable control of console logging level
          4 =   0x4 - enable control of keyboard (SAK, unraw)
          8 =   0x8 - enable debugging dumps of processes etc.
         16 =  0x10 - enable sync command
         32 =  0x20 - enable remount read-only
         64 =  0x40 - enable signalling of processes (term, kill, oom-kill)
        128 = 0x80 - allow reboot/poweroff
        256 = 0x100 - allow nicing of all RT tasks

2. SysRq组合键的功能说明

键名 功能说明
b 在没有同步或卸载硬盘的情况下立即启动。
c 为了获取崩溃转储执行kexe重启动。
d 显示被持的所有锁。
e 发送信号SIGTERM给所有进程,除了init外。
f 将调用oom_kill杀死内存热进程。
g 在平台ppc和sh上被kgdb使用。
h 显示帮助信息。
i 发送信号SIGKILL给所有的进程,除了init外。
k 安全访问密钥(Secure Access Key,SAK)杀死在当前虚拟终端上的所有程序。
m 转储当前的内存信息到控制台。
n 用于设置实时任务为可调整nice的。
o 将关闭系统(如果配置为支持)。
p 打印当前寄存器和标识到控制台。
q 将转储所有正运行定时器的列表。
r 关闭键盘Raw模式并设置为XLATE模式。
s 尝试同步所有挂接的文件系统。
t 将转储当前的任务列表和它们的信息到控制台。
u 尝试以仅读的方式重挂接所有已挂接的文件系统。
v 转储Voyager SMP处理器信息到控制台。
w 转储的所有非可中断(已阻塞)状态的任务。
x 在平台ppc/powerpc上被xmon(X监视器)接口使用。
0~9 设备控制台日志级别,控制将打印到控制台的内核信息。例如:0仅打印紧急信息,如:PANIC和OOPS信息。

3. SysRq功能键的使用

这里介绍最通用的方式,即通过/proc/sysrq-trigger,具体用法是向该文件写入相应的功能字符。
比如接着上面的hang的问题,就可以,
$ echo 'w' > /proc/sysrq-trigger
$ dmesg
比如你想查看当前系统中所有线程的信息,你可以,
$ echo 't' > /proc/sysrq-trigger
$ dmesg

手机需要打开调试sysrq的调试功能:

    echo 1 > /proc/sys/kernel/sysrq

 

对于/proc/sys/kernel/sysrq文件节点的具体使用如下:

0 - disable sysrq completely (关闭调试)
1 - enable all functions of sysrq(允许所有调试功能)
 >1 - bitmask of allowed sysrq functions (see below for detailed function description):
2 =   0x2 - enable control of console logging level
4 =   0x4 - enable control of keyboard (SAK, unraw)
8 =   0x8 - enable debugging dumps of processes etc.
16 =  0x10 - enable sync command
32 =  0x20 - enable remount read-only
64 =  0x40 - enable signalling of processes (term, kill, oom-kill)
128 =  0x80 - allow reboot/poweroff
256 = 0x100 - allow nicing of all RT tasks

 

接下来,手机就可以使用另一个文件节点/proc/sysrq-trigger  来进行调试:

 

'b' - Will immediately reboot the system without syncing or unmounting your disks.
'c' - Will perform a system crash by a NULL pointer dereference. A crashdump will be taken if configured.
'd' - Shows all locks that are held.
'e' - Send a SIGTERM to all processes, except for init.
'f' - Will call oom_kill to kill a memory hog process.
'g' - Used by kgdb (kernel debugger)
'h' - Will display help (actually any other key than those listed here will display help. but 'h' is easy to remember :-)
'i' - Send a SIGKILL to all processes, except for init.
'j' - Forcibly "Just thaw it" - filesystems frozen by the FIFREEZE ioctl.
'k' - Secure Access Key (SAK) Kills all programs on the current virtual console. NOTE: See important comments below in SAK section.
'l' - Shows a stack backtrace for all active CPUs.
'm' - Will dump current memory info to your console.
'n' - Used to make RT tasks nice-able
'o' - Will shut your system off (if configured and supported).
'p' - Will dump the current registers and flags to your console.
'q' - Will dump per CPU lists of all armed hrtimers (but NOT regular timer_list timers) and detailed information about all
clockevent devices.
'r' - Turns off keyboard raw mode and sets it to XLATE.
's' - Will attempt to sync all mounted filesystems.
't' - Will dump a list of current tasks and their information to your console.
'u' - Will attempt to remount all mounted filesystems read-only.
'v' - Forcefully restores framebuffer console 'v' - Causes ETM buffer dump [ARM-specific]
'w' - Dumps tasks that are in uninterruptable (blocked) state.
'x' - Used by xmon interface on ppc/powerpc platforms.
'y' - Show global CPU Registers [SPARC-64 specific]
'z' - Dump the ftrace buffer
'0'-'9' - Sets the console log level, controlling which kernel messages will be printed to your console. ('0', for example would make
it so that only emergency messages like PANICs or OOPSes would make it to your console.)

 

进程状态:

"R (running)", /* 0 */
"S (sleeping)", /* 1 */
"D (disk sleep)", /* 2 */
"T (stopped)", /* 4 */
"t (tracing stop)", /* 8 */
"Z (zombie)", /* 16 */
"X (dead)", /* 32 */
"x (dead)", /* 64 */
"K (wakekill)", /* 128 */
"W (waking)", /* 256 */

4. 总结

SysRq的功能非常强大,是debug linux内核问题时经常会用到的工具!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值