linux 手动触发崩溃
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 前言
- 一、如何手动触发linux崩溃?
- 二、内核相关panic和oops的cmdline(启动参数)
- 总结
前言
提示:这里可以添加本文要记录的大概内容:
在Linux系统中,“crash”通常指的是操作系统遇到严重错误,导致其无法继续正常运行的情况。
有如下表现:
系统无响应:桌面环境或命令行界面停止响应用户输入,鼠标和键盘操作没有任何效果。
内核崩溃(Kernel Panic):这是Linux系统遇到致命错误时的一种保护机制。当内核检测到系统状态无法恢复时,会打印出错误信息并停止所有进程的执行。这些信息通常包括错误类型、发生错误的代码位置等,有助于诊断问题所在。
服务或应用程序异常退出:虽然单个服务或应用程序的崩溃不一定代表整个系统的崩溃,但如果关键服务出现问题,可能会间接影响系统的稳定性。
硬件故障导致的崩溃:例如内存损坏(bad RAM)、硬盘故障等,这些问题可能导致数据读写错误,进而引发系统崩溃。
日志文件中的错误记录:系统崩溃后,相关的错误信息会被记录在系统日志文件中(如/var/log/messages或/var/log/syslog),可以帮助管理员分析崩溃的原因。
提示:以下是本篇文章正文内容,下面案例可供参考
一、如何手动触发linux崩溃?
方法一:
开启sysrq功能
echo 1 > /proc/sys/kernel/sysrq
(默认情况下,大多数发行版已经启用了SysRq功能。)
触发崩溃:
echo c > /proc/sysrq-trigger
这将立即触发一个Kernel Panic,并打印崩溃信息到控制台。
如果你配置了kdump工具,它会捕获崩溃时的内存转储。
方法二:
调用BUG()或panic()函数
在内核代码中,可以调用内核提供的BUG()或panic()函数来触发崩溃
方法三:
配置CONFIG_PANIC_ON_OOPS
如果你希望任何Oops都直接导致Kernel Panic,可以在编译内核时启用以下选项:
CONFIG_PANIC_ON_OOPS=y
或者在运行时临时启用:
echo 1 > /proc/sys/kernel/panic_on_oops
二、内核相关panic和oops的cmdline(启动参数)
oops=panic Always panic on oopses. Default is to just kill theprocess, but there is a small probability ofdeadlocking the machine.This will also cause panics on machine check exceptions.Useful together with panic=30 to trigger a reboot.panic= [KNL] Kernel behaviour on panic: delay <timeout>timeout > 0: seconds before rebootingtimeout = 0: wait forevertimeout < 0: reboot immediatelyFormat: <timeout>panic_print= Bitmask for printing system info when panic happens.User can chose combination of the following bits:bit 0: print all tasks infobit 1: print system memory infobit 2: print timer infobit 3: print locks info if CONFIG_LOCKDEP is onbit 4: print ftrace bufferbit 5: print all printk messages in bufferpanic_on_taint= Bitmask for conditionally calling panic() in add_taint()Format: <hex>[,nousertaint]Hexadecimal bitmask representing the set of TAINT flagsthat will cause the kernel to panic when add_taint() iscalled with any of the flags in this set.The optional switch "nousertaint" can be utilized toprevent userspace forced crashes by writing to sysctl/proc/sys/kernel/tainted any flagset matching with thebitmask set on panic_on_taint.See Documentation/admin-guide/tainted-kernels.rst forextra details on the taint flags that users can pickto compose the bitmask to assign to panic_on_taint.panic_on_warn panic() instead of WARN(). Useful to cause kdumpon a WARN().softlockup_panic=[KNL] Should the soft-lockup detector generate panics.Format: 0 | 1A value of 1 instructs the soft-lockup detectorto panic the machine when a soft-lockup occurs. It isalso controlled by the kernel.softlockup_panic sysctland CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC, which is therespective build-time switch to that functionality.vmpanic= [KNL,S390] Perform z/VM CP command after kernel panic.Format: <command>
所有cmdline参数可以在内核文档
kernel-parameters中找到
总结
介绍了linux下如何触发崩溃,和panic的内核启动参数