Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a kernel oops or panic crash in Linux kernel code?

How can I generate a kernel oops or crash in kernel code? Is there a function for that?

like image 537
user1804788 Avatar asked May 06 '14 00:05

user1804788


People also ask

How do I create a kernel crash?

Crash the Linux kernelecho c > /proc/sysrq-trigger sends a sysrq command to trigger a crash. Because you trigger the kernel panic with echo commands, kdump should send the dump files to the NFS share. Connect back to the NFS server, and you can conduct a postmortem to find out what happened to the client.

Do you know panic and oops errors in kernel crash?

Oops is a way to debug kernel code, and there are utilities for helping with that. A kernel panic means the system cannot recover and must be restarted. However, with an Oops, the system can usually continue. You can configure klogd and syslogd to log oops messages to files, rather than to std out.

What is kernel panic mode in Linux?

A Linux kernel panic is a system boot issue that occurs when the kernel can't load properly, and prevents the system from booting. It usually manifests as a black screen filled with code. During a normal boot process, the kernel (vmlinuz) doesn't load directly. Instead, the initramfs file loads in the RAM.


2 Answers

The usual way to crash the kernel is by using BUG() macro. There's also WARN() macro, which dumps the stack down to console but the kernel keeps running.

http://kernelnewbies.org/FAQ/BUG

What happens after kernels hits a BUG() macro (which eventually results in an internal trap) or some similar error condition (like null pointer dereference) depends on a setting of panic_on_oops global variable. If it's set to 0, the kernel will try to keep running (with whatever awful consequences). If it's set to 1, the kernel will enter the panic state and halt.

If you want to crash the kernel from user space, you've got a handy <SysRq> + <c> key combo (or, alternatively, echo c > /proc/sysrq-trigger). It's worth looking at the handler implementation for this action (http://code.metager.de/source/xref/linux/stable/drivers/tty/sysrq.c#134):

static void sysrq_handle_crash(int key)
{
    char *killer = NULL;

    panic_on_oops = 1;  /* force panic */
    wmb();
    *killer = 1;
}

The handler sets the global flag to make kernel panic on traps, then tries to dereference a random null pointer.

like image 111
oakad Avatar answered Sep 21 '22 12:09

oakad


panic() function

The kernel also has a panic() function if you want to do it from inside a kernel module code:

#include <kernel.h>

panic("my message");

It is defined at kernel/panic.c.

Here is a minimal runnable example.

Related threads:

  • Using assertion in the Linux kernel
  • https://unix.stackexchange.com/questions/66197/how-to-cause-kernel-panic-with-a-single-command