嵌入式Linux驱动笔记(三十一)——SYSRQ组合键使用

你好!这里是风筝的博客,

欢迎和我一起交流。


SYSRQ组合键是Linux调试的一种手段,即使在系统死机、panic等情况,只要系统还能响应中断,那么SYSRQ就派上用场了,具体的使用情况可以参考内核文档里的详细描述:Documentation/sysrq.txt

note:SYSRQ键也就是键盘上的Print Screen键.

使用SYSRQ组合键需要在内核开启配置:CONFIG_MAGIC_SYSRQ
可以通过在系统中使用

1
cat /proc/sys/kernel/sysrq

查看sysqr是否开启,sysrq值对应:

in /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 - enable control of console logging level
4 - enable control of keyboard (SAK, unraw)
8 - enable debugging dumps of processes etc.
16 - enable sync command
32 - enable remount read-only
64 - enable signalling of processes (term, kill, oom-kill)
128 - allow reboot/poweroff
256 - allow nicing of all RT tasks

0就是关闭,1就是开启,当然也可以通过手动echo “number” >/proc/sys/kernel/sysrq设置sysrq,比如

1
    echo 1 >/proc/sys/kernel/sysrq

开启了sysrq怎么用呢?
有两种方式,如下:

一、/proc/sysrq-trigger节点

在终端输入:

1
echo <key> > /proc/sysrq-trigger

key的值在sysrq.txt也有说明,如下:

  • What are the ‘command’ keys?
    ‘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.)

比如我要重启机器,向sysrq-trigger节点写入相应的字母即可:

1
echo b > /proc/sysrq-trigger

note:无论/proc/sys/kernel/sysrq是什么值,sysrq-trigger都是有效的。

二、键盘SYSRQ组合键

在键盘上通过组合键 ALT + SYSRQ + key 的方式使用(串口serial console不适用这种方法,后面会讲)

比如我要查看当前内存信息,可以在键盘上按 ALT + SYSRQ + m ,这样即可。

那么串口该怎么用SYSRQ呢,其实sysrq.txt文档里面也有说明:

On the serial console (PC style standard serial ports only) -
You send a BREAK, then within 5 seconds a command key. Sending
BREAK twice is interpreted as a normal BREAK.

也就是使用串口时,单机键盘上的BREAK 键,在5秒内,输出key值。
比如我要查看当前内存信息,可以在键盘上按 BREAK + m ,这样即可。
note:BREAK 键也就是键盘上的Pause Break键.

那么为什么在串口(嵌入式常用串口作为调试)上不适用ALT + SYSRQ + key 的方式的方式呢?我们可以简单的看下sysrq的实现:
sysrq的实现在drivers/tty/sysrq.c

待续。。。。。。

后续:
我看到网上有些文章描述如下:
/****************************************************************************************************
那么如何产生一个SysRq键呢?

  • 在Ubuntu下,图形界面环境不能使用SysRq,需进入文本虚拟终端环境(Ctrl+Alt+F1从图形桌面切换到虚拟终端,Alt+F7可切回来),然后同时按下Alt和Print Screen键以及相应的字母键。
  • 在嵌入式设备上,通过串口工具也可以触发SysRq,如果使用SecureCRT,则同时按下Alt和Print Screen键,会出现上述HELP,然后紧接着按下某个字母。如果使用teraTerm,则点击菜单中的Control->Send Break,会出现上述HELP,然后紧接着按下某个字母。
    ***********************************************************************************************/

对于嵌入式设备这块有点扯,