Linux: kernel magic key sysrq for kernel debugging

        In the Linux system, we may encounter a situation where a certain command of the system hangs. Usually, we will check the /proc/pid/wchan file to see what the process is in, and then further check the system log or use strace to track System calls and other methods during command execution to analyze problems. We know that the command hang usually causes problems in the kernel. If we can print the kernel information according to our needs, we may get twice the result with half the effort and quickly locate the problem.

        Fortunately, linux really provides such a way, through SysRq, and I will introduce it here today.

1. Sysrq

        SysRq (system request) is a series of keys understood by the Linux operating system kernel that can trigger a set of predefined actions. These commands are typically used when troubleshooting or recovery of a virtual machine is not possible through traditional management (for example, the VM is not responding). 

        Sysrq is called the "magic key combination". It is a debugging tool built into the Linux kernel. As long as the kernel is not completely locked, no matter what the kernel is doing, the kernel will respond to this series of key combinations. Using these key combinations, you can collect information including: System running information such as system memory usage, CPU task processing, and process running status.

        The SysRq key is useful in various situations such as confirming that the kernel is running, investigating the cause of a kernel panic, and more.

Two, enable sysrq

        To use the Magic SysRq key to open the kernel configuration option CONFIG_MAGIC_SYSRQ, the general distribution has this configuration option enabled by default:

#

# Kernel hacking

#

......

CONFIG_MAGIC_SYSRQ=y

        When running a kernel compiled with SysRq, the values ​​in the /proc/sys/kernel/sysrq file control the functions that are allowed to be called through the SysRq key (keyboard combination). The following are the possible values ​​in /proc/sys/kernel/sysrq list:

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

The values ​​in the /proc/sys/kernel/sysrq file control some functions of the SysRq key (keyboard combination).

echo "number" >/proc/sys/kernel/sysr

For example:

echo 1 >/proc/sys/kernel/sysrq ##When set to 1, enable all functions of the SysRq key.

Or you can set or read and write kernel parameters through the sysctl command:

sysctl -w kernel.sysrq = 1

        Note: The value of /proc/sys/kernel/sysrq only affects invocations via keyboard combinations. Invoking any operation via /proc/sysrq-trigger is always allowed (by a user with administrative privileges).
That is, the value of /proc/sys/kernel/sysrq only affects the keyboard combination key to trigger the kernel operation, and has no effect on the /proc/sysrq-trigger triggering the kernel operation.

The CONFIG_MAGIC_SYSRQ option is enabled in the kernel configuration option, so that after the system starts, the /proc/sysrq-trigger node will be generated for debugging.

        General usage:

sysctl -n kernel.sysrq ####Display the current sysrq value, usually 16, which means using the "sysnc" command

sysctl -w kernel.sysrq=1 ### "1" means enable all sysrq function keys.

sysctl -n kernel.sysrq ###Confirm that the sysrq function key is enabled, you can also use the following command to view

To make the SysReq configuration persistent, you can do the following to enable all SysRq commands

Add this line to /etc/sysctl.conf

kernel.sysrq = 1

or

echo "kernel.sysrq = 1" >> /etc/sysctl.conf

Reboot or update sysctl by running

sysctl -p

3. The use of SysRq

SysRq can be used in two ways:

  • The first is the keyboard combination: Alt+SysRq + command key. (affected by /proc/sys/kernel/sysrq value)
  • The second is to modify the value of the /proc/sysrq-trigger file. (not affected by /proc/sys/kernel/sysrq value)

  Mainly introduce the second method:

The value of the /proc/sysrq-trigger file is not affected by the value of /proc/sys/kernel/sysrq, so setting the value of /proc/sys/kernel/sysrq to 0 can still trigger various events of the kernel.

[root@localhost ~]# echo 0 > /proc/sys/kernel/sysrq
[root@localhost ~]# cat /proc/sys/kernel/sysrq
0

for example:

echo t > /proc/sysrq-trigger

4. Command introduction (part)

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

Key Name Function Description

b Start immediately without synchronizing or unmounting the hard drive.

c Perform a kexe restart in order to obtain a crash dump.

d Displays all locks held.

e Send the signal SIGTERM to all processes except init.

f will call oom_kill to kill the memory hot process.

g is used by kgdb on platforms ppc and sh.

h Displays help information.

i Send the signal SIGKILL to all processes except init.

k Secure Access Key (Secure Access Key, SAK) kills all programs on the current virtual terminal.

m dumps current memory information to the console.

n is used to set the real-time task as adjustable nice.

o Will shut down the system (if configured to support it).

p Print the current register and flags to the console.

q will dump a list of all running timers.

r Turn off keyboard Raw mode and set to XLATE mode.

s Attempts to synchronize all mounted filesystems.

t will dump the current list of tasks and their information to the console.

u Attempts to remount all mounted file systems read-only.

v Dump Voyager SMP processor information to the console.

w Dumps all tasks in non-interruptible (blocked) state.

x is used by the xmon (X monitor) interface on platforms ppc/powerpc.

0~9 device console log level, control the kernel information that will be printed to the console. For example: 0 only prints emergency information, such as: PANIC and OOPS information.

1、echo m > /proc/sysrq-trigger

        Dump current memory information to the host.

2、echo t > /proc/sysrq-trigger

        This command will output the execution status and call stack information of all processes in the system (including system and user)

3、echo w > /proc/sysrq-trigger

Dump tasks that are in the uninterruptible (blocked) state.

4、echo l > /proc/sysrq-trigger

Displays stack traces for all active CPUs.

5、echo p > /proc/sysrq-trigger

Dump the current registers and flags to the host.

6、echo q > /proc/sysrq-trigger

 Will dump a per-CPU list of all armed hrtimers (but not regular timer_list timers) and details about all clock event devices.

Guess you like

Origin blog.csdn.net/hhd1988/article/details/130006269