linux内核中的sysrq

一、介绍

Sysrq被称为”魔术组合键”, 是内建于Linux内核的调试工具。只要内核没有完全锁住(还可以响应中断),不管内核在做什么事情,使用这些组合键都可以搜集包括系统内存使用、CPU任务处理、进程运行状态等系统运行信息和进行系统关机、内核奔溃异常模拟等操作。

详细信息可以查看内核文档:Documentation/admin-guide/sysrq.rst

二、sysrq的实现和使用

首先sysrq的功能实现是通过注册一个proc sysctl控制键实现功能的(关于proc sysctl的相关说明和实现参考我的另一篇博客Linux内核proc sysctl_systcl-CSDN博客)。

/****kernel/sysctl.c****/
#ifdef CONFIG_MAGIC_SYSRQ
static int sysrq_sysctl_handler(struct ctl_table *table, int write,
                void *buffer, size_t *lenp, loff_t *ppos)
{
    int tmp, ret;

    tmp = sysrq_mask();

    ret = __do_proc_dointvec(&tmp, table, write, buffer,
                   lenp, ppos, NULL, NULL);
    if (ret || !write)
        return ret;

    if (write)
        sysrq_toggle_support(tmp);

    return 0;
}
#endif

...

#ifdef CONFIG_MAGIC_SYSRQ
    {
        .procname   = "sysrq",
        .data       = NULL,
        .maxlen     = sizeof (int),
        .mode       = 0644,
        .proc_handler   = sysrq_sysctl_handler,
    },
#endif

这里可以看到,在打开CONFIG_MAGIC_SYSRQ配置后,内核将注册一个sysrq的一个用于运行时配置内核参数的命令。可以通过以下节点查看sysrq的支持情况和控制sysrq响应的命令键值:

# cat /proc/sys/kernel/sysrq

配置内核时,需要对“Magic SysRq key(CONFIG_Magic_SysRq)”说“是”。当运行在中编译了SysRq的内核时,/proc/sys/kernel/sysrq控制允许通过sysrq键调用的函数。此文件中的默认值由CONFIG_MAGIC_SYSRQ_default_ENABLE配置符号设置,该符号本身默认为1。以下是/proc/sys/kernel/sysrq中可能的值列表:

   -  0 - disable sysrq completely          /***为0时,完全禁用sysrq***/
   -  1 - enable all functions of sysrq     /***为1时,启用sysrq的所有功能***/
   - >1 - bitmask of allowed sysrq functions (see below for detailed function
     description)::                         /***大于1时,启用sysrq的部分功能***/

          2 =   0x2 - enable control of console logging level    /***为2时,启用控制台日志记录级别的控制***/
          4 =   0x4 - enable control of keyboard (SAK, unraw)    /***为1时,启用键盘控制***/
          8 =   0x8 - enable debugging dumps of processes etc.   /***为1时,启用进程等的调试转储***/
         16 =  0x10 - enable sync command                        /***为1时,启用同步命令***/
         32 =  0x20 - enable remount read-only                   /***为1时,启用只读重新装载***/
         64 =  0x40 - enable signalling of processes (term, kill, oom-kill)    /***为1时,启用进程信号(term、kill、oom-kill)***/
        128 =  0x80 - allow reboot/poweroff                      /***为1时,允许重新启动/关闭电源***/
        256 = 0x100 - allow nicing of all RT tasks               /***为1时,允许记录所有RT任务***/

注:非0时,值越小功能越多

可以通过以下方法进行重新设置

# echo "number" >/proc/sys/kernel/sysrq

在sysrq可以使用的情况下,都可以通过以下节点来实现sysrq的调试功能,这里将始终允许(具有管理员权限的用户)通过`/proc/sysrq触发器`调用任何操作。

# echo funckey > /proc/sysrq-trigger

sysrq常用调试功能说明如下代码说明

/****Documentation/admin-guide/sysrq.rst****/
=========== ===================================================================
Command     Function
=========== ===================================================================
``b``       Will immediately reboot the system without syncing or unmounting
            your disks.

``c``       Will perform a system crash and 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 the oom killer to kill a memory hog process, but do not
        panic if nothing can be killed.

``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 uninterruptible (blocked) state.

``x``       Used by xmon interface on ppc/powerpc platforms.
            Show global PMU Registers on sparc64.
            Dump all TLB entries on MIPS.

``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.)
=========== ===================================================================

功能说明如下表所示:

No 命令功能
1 ``b``将立即重新启动系统,而不同步或卸载磁盘。
2 ``c“”将执行系统崩溃,如果配置了,则将执行崩溃转储。
3 ``d``显示所有已持有的锁。
4 ``e``向除init之外的所有进程发送SIGTERM。
5 ``f会打电话给oom杀手来杀死一个占用内存的过程,但如果什么都杀不了,不要惊慌。
6 ``g``由kgdb(内核调试器)使用
7 ``h``将显示帮助(实际上,除了此处列出的任何其他键都将显示帮助。但是`h``很容易记住:-)
8 ``i``向除init之外的所有进程发送SIGKILL。
9 ``j``强行“解冻它”-文件系统被FIFREEZE ioctl冻结。
10 ``k``安全访问密钥(SAK)杀死当前虚拟控制台上的所有程序。注意:请参阅下面SAK部分的重要注释。
11 ``l``显示所有活动CPU的堆栈回溯。
12 ``m``将把当前内存信息转储到控制台。
13 ``n``用于使RT任务变得很好
14 ``o``将关闭您的系统(如果配置和支持)。
15 ``p``将把当前寄存器和标志转储到控制台。
16 ``q ``将转储所有已武装hrtimers(但不是常规timer_list timers)的每个CPU列表以及有关所有时钟事件设备的详细信息。
17 ``r``关闭键盘原始模式并将其设置为XLATE。
18 ``s``将尝试同步所有已安装的文件系统。
19 ``t``将当前任务及其信息的列表转储到控制台。
20 ``u``将尝试以只读方式重新装载所有已装载的文件系统。
21 ``v``强制恢复帧缓冲区控制台
22 ``v``导致ETM缓冲区转储[ARM特定]
23 ``w ``转储处于不间断(阻止)状态的任务。
24 ``x``由ppc/powerpc平台上的xmon接口使用。在sparc64上显示全局PMU寄存器。转储MIPS上的所有TLB条目。
25 ``y ``显示全局CPU寄存器[SSPARC-64特定]
26 ``z ``转储ftrace缓冲区
27 ``0``-`9``设置控制台日志级别,控制哪些内核消息将打印到控制台。(例如,“0”将使其成为只有像PANIC或OOPS这样的紧急消息才能到达您的控制台。

猜你喜欢

转载自blog.csdn.net/tombaby_come/article/details/134038949
今日推荐