SysRq鍵簡介
SysRq鍵是一個魔術案件,只要在內核沒有完全卡死的情況下,內核都會相應SysRq 鍵的輸入,使用這些組合鍵都可以搜集包括系統(tǒng)內存使用、CPU任務處理、進程運行狀態(tài)等系統(tǒng)運行信息。
配置
內核配置選項中要使能CONFIG_MAGIC_SYSRQ
選項,這樣系統(tǒng)啟動之后,會生成/proc/sysrq-trigger
節(jié)點用于調試。
修改/etc/sysctl.conf
啟動SysRq。
kernel.sysrq=1
執(zhí)行下列命令,使修改生效。
sysctl -p
也可以通過寫echo "number" >/proc/sys/kernel/sysrq
節(jié)點臨時使能sysrq功能。
寫入不同的值使能不同的功能:
值 | 描述/說明 |
---|---|
0 | 完全禁用 sysrq |
1 | 啟用 sysrq 的所有功能 |
2 | 允許控制終端日志級別 |
4 | 允許控制終端日志級別 |
8 | 允許調試進程dump |
16 | 允許執(zhí)行sync命令 |
32 | 允許重新掛載文件系統(tǒng)為之讀 |
64 | 允許發(fā)送信號給進程(term,kill,oom-kill) |
128 | 允許重啟/關機 |
256 | 允許調整實時任務的優(yōu)先級 |
如何使用SysRq
echo <command key> > /proc/sysrq-trigger
命令鍵
是區(qū)分大小寫的。
具體含義如下。
命令鍵 | 功能 |
---|---|
b |
將立即重啟系統(tǒng),不會同步或者卸載磁盤。 |
c |
將執(zhí)行系統(tǒng) crash,如果配置了系統(tǒng) crashdump,將執(zhí)行 crashdump。 |
d |
顯示所有持有的鎖。 |
e |
發(fā)送 SIGTERM 信號給所有進程,除了 init 進程。 |
f |
將調用 oom killer 殺掉一個過度占用內存的進程,如果什么任務都沒殺, 也不會 panic。 |
g |
kgdb 使用(內核調試器)。 |
h |
將會顯示幫助。(實際上除了這里列舉的鍵,其他的都將顯示幫助, 但是h 容易記?。?-) |
i |
發(fā)送 SIGKILL 給所有進程,除了 init 進程。 |
j |
強制性的 “解凍它” - 用于被 FIFREEZE ioctl 操作凍住的文件系統(tǒng)。 |
k |
安全訪問秘鑰(SAK)殺掉在當前虛擬控制臺的所有程序,注意:參考 下面 SAK 節(jié)重要論述。 |
l |
顯示所有活動 cpu 的?;厮?。 |
m |
將導出當前內存信息到你的控制臺。 |
n |
用于使所有實時任務變成普通任務。 |
o |
將關閉系統(tǒng)(如果配置和支持的話)。 |
p |
將導出當前寄存器和標志位到控制臺。 |
q |
將導出每個 cpu 上所有已裝備的高精度定時器(不是完整的 time_list 文件顯示的 timers)和所有時鐘事件設備的詳細信息。 |
r |
關閉鍵盤的原始模式,設置為轉換模式。 |
s |
將嘗試同步所有的已掛載文件系統(tǒng)。 |
t |
將導出當前所有任務列表和它們的信息到控制臺。 |
u |
將嘗試重新掛載已掛載文件系統(tǒng)為只讀。 |
v |
強制恢復幀緩存控制臺。 |
v |
觸發(fā) ETM 緩存導出 [ARM 架構特有] |
w |
導出處于不可中斷狀態(tài)(阻塞)的任務。 |
x |
在 ppc/powerpc 架構上用于 xmon 接口。在 sparc64 架構上用于顯示全局的 PMU(性能監(jiān)控單元)寄存器。在 MIPS 架構上導出所有的 tlb 條目。 |
y |
顯示全局 cpu 寄存器 [SPARC-64 架構特有] |
z |
導出 ftrace 緩存信息 |
0 -9 |
設置控制臺日志級別,該級別控制什么樣的內核信息將被打印到你的 控制臺。(比如0 ,將使得只有緊急信息,像 PANICs or OOPSes 才能到你的控制臺。) |
打印內存使用信息
root@firefly:~# echo m > /proc/sysrq-trigger
[ 3100.196187] sysrq: SysRq : Show Memory
[ 3100.196675] Mem-Info:
[ 3100.196931] active_anon:48062 inactive_anon:2987 isolated_anon:0
[ 3100.196931] active_file:40449 inactive_file:53540 isolated_file:0
[ 3100.196931] unevictable:0 dirty:22 writeback:0 unstable:0
[ 3100.196931] slab_reclaimable:10518 slab_unreclaimable:6599
[ 3100.196931] mapped:44843 shmem:3710 pagetables:1568 bounce:0
[ 3100.196931] free:810482 free_pcp:497 free_cma:0
[ 3100.199998] DMA free:3241928kB min:7920kB low:9900kB high:11880kB active_anon:192248kB inactive_anon:11948kB active_file:161796kB inactive_file:214160kB unevictable:0kB isolated(anon):0kB isolated(file):0kB present:4030464kB managed:3933592kB mlocked:0kB dirty:88kB writeback:0kB mapped:179372kB shmem:14840kB slab_reclaimable:42072kB slab_unreclaimable:26396kB kernel_stack:5792kB pagetables:6272kB unstable:0kB bounce:0kB free_pcp:1988kB local_pcp:224kB free_cma:0kB writeback_tmp:0kB pages_scanned:0 all_unreclaimable? no
[ 3100.204031] lowmem_reserve[]: 0 0 0
[ 3100.204414] DMA: 272*4kB (U) 95*8kB (UM) 177*16kB (UME) 88*32kB (UME) 50*64kB (UM) 32*128kB (UM) 6*256kB (UM) 4*512kB (UM) 2*1024kB (M) 3*2048kB (UME) 785*4096kB (M) = 3241928kB
[ 3100.206208] Node 0 hugepages_total=0 hugepages_free=0 hugepages_surp=0 hugepages_size=2048kB
[ 3100.206990] 97710 total pagecache pages
[ 3100.207345] 0 pages in swap cache
[ 3100.207679] Swap cache stats: add 0, delete 0, find 0/0
[ 3100.208156] Free swap = 0kB
[ 3100.208425] Total swap = 0kB
[ 3100.208721] 1007616 pages RAM
[ 3100.209003] 0 pages HighMem/MovableOnly
[ 3100.209352] 24218 pages reserved
root@firefly:~#
打印進程列表
root@firefly:~# echo t > /proc/sysrq-trigger
[ 3357.426399] sysrq: SysRq : Show State
[ 3357.426902] Call trace:
[ 3357.427155] [ ] __switch_to+0x94/0xbc
[ 3357.427759] [ ] __schedule+0x334/0x65c
[ 3357.428249] [ ] schedule+0x70/0x84
[ 3357.428762] [ ] schedule_hrtimeout_range_clock+0x54/0xd8
[ 3357.429390] [ ] schedule_hrtimeout_range+0x38/0x48
[ 3357.430004] [ ] SyS_epoll_wait+0x2c0/0x334
[ 3357.430515] [ ] SyS_epoll_pwait+0xb4/0x104
[ 3357.431057] [ ] el0_svc_naked+0x24/0x28
[ 3357.431576] Call trace:
[ 3359.224688] runnable tasks:
[ 3359.224688] task PID tree-key switches prio wait-time sum-exec sum-sleep
[ 3359.224688] ----------------------------------------------------------------------------------------------------------
[ 3359.226942] watchdog/5 31 -8.990375 842 0 0.010418 33.639974 0.011083 /
[ 3359.227935] migration/5 32 0.000000 11 0 0.006417 0.703626 0.011083 /
...............................
[ 3359.280491] rsyslogd 620 12549.402602 31 120 3.959710 13.067249 3000146.370846 /
[ 3359.281483] gmain 649 2780.837080 4 120 1.497875 0.234791 42.333083 /
[ 3359.282473] gdbus 653 6084.558463 235 120 11.845505 88.143377 5894.875470 /
[ 3359.283464] probing-thread 657 2798.851658 1 120 0.171209 0.046375 0.000000 /
[ 3359.284458] gmain 666 13129.747017 842 120 1.635792 160.937875 ...............................
[ 3359.347891] kbase_event 1087 5737.991190 2 100 0.000000 0.015416 0.022167 /
[ 3359.348888] kworker/5:0 1415 12729.712418 6352 120 94.661328 379.266952 300531.917073 /
[ 3359.349880] kworker/5:1 1421 13139.623478 4592 120 40.804756 269.058278 218093.165092 /
[ 3359.350919]
打印 CPU 信息
root@firefly:~# echo w > /proc/sysrq-trigger
[ 3461.098259] sysrq: SysRq : Show Blocked State
[ 3461.099495] Sched Debug Version: v0.11, 4.4.194+ #15
[ 3461.099987] ktime : 3460974.551474
[ 3461.100559] sched_clk : 3461099.486338
[ 3461.101163] cpu_clk : 3461099.486629
[ 3461.101733] jiffies : 4298128273
[ 3461.102303]
[ 3461.102457] sysctl_sched
...............................
[ 3461.106084]
[ 3461.476753] task PID tree-key switches prio wait-time sum-exec sum-sleep
[ 3461.476753] ----------------------------------------------------------------------------------------------------------
[ 3461.479007] watchdog/5 31 -8.990375 868 0 0.010418 34.639809 0.011083 /
[ 3461.479999] migration/5 32 0.000000 11 0 0.006417 0.703626 0.011083 /
[ 3461.480990] ksoftirqd/5 33 12976.655002 282 120 31.262910 9.577926 3226896.156313 /
....................
[ 3461.598951] gdbus 981 5621.043245 63 120 10.429415 7.925751 240.024166 /
[ 3461.599945] kbase_event 1087 5737.991190 2 100 0.000000 0.015416 0.022167 /
[ 3461.600943] kworker/5:1 1421 13242.484431 6324 120 56.331917 371.919231 300578.331580 /
[ 3461.601934] kworker/5:0 1424 13267.128801 416 120 3.932261 25.079662 19671.560460 /
[ 3461.602964]
顯示所有活動 cpu 的?;厮?/h3>
root@firefly:~# echo l > /proc/sysrq-trigger
[ 3602.003162] sysrq: SysRq : Show backtrace of all active CPUs
[ 3602.003919] Call trace:
[ 3602.004175] [] dump_backtrace+0x0/0x220
root@firefly:~# [ 3602.004675] [] show_stack+0x24/0x30
[ 3602.005233] [] showacpu+0x68/0x84
[ 3602.005688] [] flush_smp_call_function_queue+0x84/0x134
[ 3602.006303] [] generic_smp_call_function_single_interrupt+0x18/0x20
[ 3602.007007] [] handle_IPI+0x19c/0x260
[ 3602.007490] [] gic_handle_irq+0x12c/0x184
[ 3602.007995] Exception stack(0xffffffc0f2f1fd90 to 0xffffffc0f2f1fec0)
[ 3602.008580] fd80: 0000000000000000 0000000000000003
[ 3602.009284] fda0: 00000040ee976000 0000000000000020 00ffffffffffffff 0000001425865f85
[ 3602.009988] fdc0: 000000000004b382 0000000000000003 00000032b5593519 ffffff8008081800
[ 3602.010691] fde0: 0000000000001000 0000000000000000 0000000034d5d91d 00000040ee976000
[ 3602.011394] fe00: ffffffc0f2f0a880 0000000000000000 0000000000000000 0000000000000000
[ 3602.012098] fe20: 0000000030d00800 00000346a09ab464 0000000000000002 ffffffc00a4bac00
[ 3602.012801] fe40: ffffff80097ace18 0000000000000001 0000034697fd8ddd ffffff800974e488
[ 3602.013504] fe60: 0000000001ab4000 00000000010f8244 0000000000000000 ffffffc0f2f1fec0
[ 3602.014209] fe80: ffffff8008b9eb10 ffffffc0f2f1fec0 ffffff8008b9eb14 0000000060000145
[ 3602.014912] fea0: ffffffc0f2f1fec0 ffffff8008b9eb10 ffffffffffffffff 0000000000000002
[ 3602.015615] [] el1_irq+0xb4/0x140
[ 3602.016070] [] cpuidle_enter_state+0x1cc/0x25c
[ 3602.016624] [] cpuidle_enter+0x34/0x44
[ 3602.017112] [] call_cpuidle+0x6c/0x74
[ 3602.017598] [] cpu_startup_entry+0x2ac/0x2bc
[ 3602.018128] [] secondary_start_kernel+0x190/0x1bc
[ 3602.018700] [<00000000010f8188>] 0x10f8188
觸發(fā) Crashdump
root@firefly:~# echo c > /proc/sysrq-trigger
[ 3635.905960] sysrq: SysRq : Trigger a crash
[ 3635.906476] Unable to handle kernel NULL pointer dereference at virtual address 00000000
[ 3635.907201] pgd = ffffffc0d4a3e000
[ 3635.907640] [00000000] *pgd=0000000000000000, *pud=0000000000000000
[ 3635.908305] Internal error: Oops: 96000045 [#1] SMP
..............................................................
[ 3648.331820] Exception stack(0xffffffc0cd8cbb30 to 0xffffffc0cd8cbc60)
[ 3648.332405] bb20: ffffff800967ff68 0000008000000000
[ 3648.333108] bb40: ffffffc0cd8cbd00 ffffff80085bb538 000000000000000f 0000000000000000
[ 3648.333811] bb60: ffffff80097f72f8 0000000000000002 ffffffc0cd8cbb90 000000030003c018
[ 3648.334514] bb80: 00000000000000c3 0000000100000000 ffffffc0cd8cbc30 ffffff800810d674
[ 3648.335218] bba0: ffffffc0cd8cbc90 ffffff80092b55d1 ffffff80095fa000 0000000000000006
[ 3648.335921] bbc0: 0000000000000063 0000000000000000 0000000000000001 0000000000000000
[ 3648.336623] bbe0: ffffffc0f7ee8320 0000000000000000 0000000000000000 0000000000000000
[ 3648.337327] bc00: 0000000000000010 ffffff800965ead8 ffffff8008478398 7f7f7f7f7f7f7f7f
[ 3648.338029] bc20: 71277660716d73ff 7f7f7f7f7f7f7f7f 0101010101010101 0000000000000000
[ 3648.338733] bc40: 0ffffffffffffffe 0000000000000000 ffffff80081e5cb8 0000007f8147b8c8
[ 3648.339434] [ ] el1_da+0x24/0x8c
[ 3648.339873] [ ] __handle_sysrq+0xa0/0x14c
[ 3648.340379] [ ] write_sysrq_trigger+0x5c/0x74
[ 3648.340911] [ ] proc_reg_write+0xa8/0xcc
[ 3648.341406] [ ] __vfs_write+0x48/0xe8
[ 3648.341877] [ ] vfs_write+0xa8/0x15c
[ 3648.342338] [ ] SyS_write+0x5c/0xb0
[ 3648.342798] [ ] el0_svc_naked+0x24/0x28
總結
root@firefly:~# echo l > /proc/sysrq-trigger
[ 3602.003162] sysrq: SysRq : Show backtrace of all active CPUs
[ 3602.003919] Call trace:
[ 3602.004175] [] dump_backtrace+0x0/0x220
root@firefly:~# [ 3602.004675] [] show_stack+0x24/0x30
[ 3602.005233] [] showacpu+0x68/0x84
[ 3602.005688] [] flush_smp_call_function_queue+0x84/0x134
[ 3602.006303] [] generic_smp_call_function_single_interrupt+0x18/0x20
[ 3602.007007] [] handle_IPI+0x19c/0x260
[ 3602.007490] [] gic_handle_irq+0x12c/0x184
[ 3602.007995] Exception stack(0xffffffc0f2f1fd90 to 0xffffffc0f2f1fec0)
[ 3602.008580] fd80: 0000000000000000 0000000000000003
[ 3602.009284] fda0: 00000040ee976000 0000000000000020 00ffffffffffffff 0000001425865f85
[ 3602.009988] fdc0: 000000000004b382 0000000000000003 00000032b5593519 ffffff8008081800
[ 3602.010691] fde0: 0000000000001000 0000000000000000 0000000034d5d91d 00000040ee976000
[ 3602.011394] fe00: ffffffc0f2f0a880 0000000000000000 0000000000000000 0000000000000000
[ 3602.012098] fe20: 0000000030d00800 00000346a09ab464 0000000000000002 ffffffc00a4bac00
[ 3602.012801] fe40: ffffff80097ace18 0000000000000001 0000034697fd8ddd ffffff800974e488
[ 3602.013504] fe60: 0000000001ab4000 00000000010f8244 0000000000000000 ffffffc0f2f1fec0
[ 3602.014209] fe80: ffffff8008b9eb10 ffffffc0f2f1fec0 ffffff8008b9eb14 0000000060000145
[ 3602.014912] fea0: ffffffc0f2f1fec0 ffffff8008b9eb10 ffffffffffffffff 0000000000000002
[ 3602.015615] [] el1_irq+0xb4/0x140
[ 3602.016070] [] cpuidle_enter_state+0x1cc/0x25c
[ 3602.016624] [] cpuidle_enter+0x34/0x44
[ 3602.017112] [] call_cpuidle+0x6c/0x74
[ 3602.017598] [] cpu_startup_entry+0x2ac/0x2bc
[ 3602.018128] [] secondary_start_kernel+0x190/0x1bc
[ 3602.018700] [<00000000010f8188>] 0x10f8188
root@firefly:~# echo c > /proc/sysrq-trigger
[ 3635.905960] sysrq: SysRq : Trigger a crash
[ 3635.906476] Unable to handle kernel NULL pointer dereference at virtual address 00000000
[ 3635.907201] pgd = ffffffc0d4a3e000
[ 3635.907640] [00000000] *pgd=0000000000000000, *pud=0000000000000000
[ 3635.908305] Internal error: Oops: 96000045 [#1] SMP
..............................................................
[ 3648.331820] Exception stack(0xffffffc0cd8cbb30 to 0xffffffc0cd8cbc60)
[ 3648.332405] bb20: ffffff800967ff68 0000008000000000
[ 3648.333108] bb40: ffffffc0cd8cbd00 ffffff80085bb538 000000000000000f 0000000000000000
[ 3648.333811] bb60: ffffff80097f72f8 0000000000000002 ffffffc0cd8cbb90 000000030003c018
[ 3648.334514] bb80: 00000000000000c3 0000000100000000 ffffffc0cd8cbc30 ffffff800810d674
[ 3648.335218] bba0: ffffffc0cd8cbc90 ffffff80092b55d1 ffffff80095fa000 0000000000000006
[ 3648.335921] bbc0: 0000000000000063 0000000000000000 0000000000000001 0000000000000000
[ 3648.336623] bbe0: ffffffc0f7ee8320 0000000000000000 0000000000000000 0000000000000000
[ 3648.337327] bc00: 0000000000000010 ffffff800965ead8 ffffff8008478398 7f7f7f7f7f7f7f7f
[ 3648.338029] bc20: 71277660716d73ff 7f7f7f7f7f7f7f7f 0101010101010101 0000000000000000
[ 3648.338733] bc40: 0ffffffffffffffe 0000000000000000 ffffff80081e5cb8 0000007f8147b8c8
[ 3648.339434] [ ] el1_da+0x24/0x8c
[ 3648.339873] [ ] __handle_sysrq+0xa0/0x14c
[ 3648.340379] [ ] write_sysrq_trigger+0x5c/0x74
[ 3648.340911] [ ] proc_reg_write+0xa8/0xcc
[ 3648.341406] [ ] __vfs_write+0x48/0xe8
[ 3648.341877] [ ] vfs_write+0xa8/0x15c
[ 3648.342338] [ ] SyS_write+0x5c/0xb0
[ 3648.342798] [ ] el0_svc_naked+0x24/0x28
SysRq 在處理系統(tǒng)掛起時安全重啟方面已經比較完善了。
https://blog.csdn.net/QTM_Gitee/article/details/122929292
https://blog.csdn.net/weixin_42506599/article/details/108411139
https://www.codenong.com/cs106961584/
https://www.kernel.org/doc/html/latest/translations/zh_CN/admin-guide/sysrq.html?highlight=sysrq
https://blog.csdn.net/zhangzhi123456789/article/details/47088253
end
-
cpu
+關注
關注
68文章
10804瀏覽量
210828 -
內存
+關注
關注
8文章
2966瀏覽量
73812 -
系統(tǒng)
+關注
關注
1文章
1006瀏覽量
21292
發(fā)布評論請先 登錄
相關推薦
評論