Sunday, November 24, 2013

Force a Kernel Panic

Have you ever wanted to generate a kernel panic, for kernel testing purposes?

A few ways to generate a kernel panic, a kernel oops and reboots:
# echo 1 > /proc/sys/kernel/panic
# dd if=/dev/random of=/dev/port
# cat /dev/port
# cat /dev/zero > /dev/mem
Forcing an Alt-SysReq-c command from the console:
# echo c > /proc/sysrq-trigger

To generate a kernel panic from source code, we will need to compile a kernel module from the kernel source: (source)
// source: force_panic.c
#ifdef __KERNEL__

/* Makefile :
obj-m := force_panic.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

 $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

#include <linux/module.h>
#include <linux/kernel.h>

static int __init panic_init(void) {
  return 0;

static void __exit panic_exit(void) {


Generate a kernel oops: (source)
static int crash_module_init(void) {
     printf("crash module starting\n");
     int *p = 0;

     printk("%d\n", *p);

     return 0;

static void crash_module_exit(void) {
    printf("crash module exiting\n");



bloxorz said...

Thank you for giving them this great knowledge, really thank you for sharing, hope you will have such great posts as this more.

Obat hipertensi ampuh said...

may be useful for all, helpful article once and pardon me permission to share also here :

Cara menyembuhkan batuk menahun
Cara menyembuhkan tumor testis
Cara menyembuhkan PPOK

Frederik said...

Thanks for the article.

Sadly, none of the commands actually make the kernel "panic".

# echo c > /proc/sysrq-trigger does freeze and reboot the system but no stack calls appear like in your screenshot.

Any ideas? I linked my stackexchange question.