# Generate kernel panic with crash dump creating



## blitzkrieg (Apr 17, 2012)

I try to get a little more experience in kernel debugging. How can *I* cause *a* kernel crash?
I put 
	
	



```
dumpdev="AUTO"
```
 and 
	
	



```
dumpdir=/var/crash
```
 into my rc.conf and set debug.kdb.panic to 1. There is no kernel dump in /var/crash. Th*e*n *I* try to use the panic() function:

```
#include <sys/types.h>
#include <sys/systm.h>
main() {
panic();
}
```
But it doesn't compile. What can *I* to do to crash kernel?


----------



## fluca1978 (Apr 19, 2012)

It does not compile because panic(9) wants a printf-like argument list.


----------



## trasz@ (Apr 21, 2012)

Just do `# sysctl debug.kdb.panic=1` as root.


----------



## blitzkrieg (Apr 22, 2012)

trasz@ said:
			
		

> Just do `# sysctl debug.kdb.panic=1` as root.


There is no crash dump in this case.


----------



## blitzkrieg (Apr 22, 2012)

fluca1978 said:
			
		

> It does not compile because panic(9) wants a printf-like argument list.



I've tried with 
	
	



```
panic("iam in panic");
```
It doesn't compile either.


----------



## fluca1978 (Apr 26, 2012)

Interesting,
I've tried to compile the same program too and I got the following:


```
~> uname -a
FreeBSD bsdmag 8.2-RELEASE FreeBSD 8.2-RELEASE #0: 
Fri Feb 18 02:24:46 UTC 2011     
root@almeida.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386


~> cat panic.c
#include <sys/types.h>
#include <sys/systm.h>

main() {
  panic("Hello World!");
}


~> gcc panic.c
In file included from panic.c:2:
/usr/include/sys/systm.h:239: error: expected declaration specifiers or '...' before 'uintfptr_t'
/usr/include/sys/systm.h:243: error: expected declaration specifiers or '...' before 'uintfptr_t'
In file included from panic.c:2:
/usr/include/sys/systm.h:300: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splbio'
/usr/include/sys/systm.h:301: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splcam'
/usr/include/sys/systm.h:302: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splclock'
/usr/include/sys/systm.h:303: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splhigh'
/usr/include/sys/systm.h:304: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splimp'
/usr/include/sys/systm.h:305: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splnet'
/usr/include/sys/systm.h:306: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsoftcam'
/usr/include/sys/systm.h:307: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsoftclock'
/usr/include/sys/systm.h:308: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsofttty'
/usr/include/sys/systm.h:309: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsoftvm'
/usr/include/sys/systm.h:310: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsofttq'
/usr/include/sys/systm.h:311: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splstatclock'
/usr/include/sys/systm.h:312: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'spltty'
/usr/include/sys/systm.h:313: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splvm'
/usr/include/sys/systm.h:314: error: expected ')' before 'ipl'
```

It seems the types defined in types.h are not processed right by the pre-processor. Is this the same error you get? Can anyone suggest a solution?


----------



## trasz@ (Apr 26, 2012)

What you're trying to do - calling panic() - won't work, because panic() is a kernel routine, and you're trying to use it in a userland program.  You would have to write a kernel module that calls it.

I have no idea why the panic syscall doesn't create crashdump.


----------



## fluca1978 (Apr 26, 2012)

trasz@ said:
			
		

> What you're trying to do - calling panic() - won't work, because panic() is a kernel routine, and you're trying to use it in a userland program.  You would have to write a kernel module that calls it.



Right, the problem was that, and it was so simple.....sgrunt!

Now, for the sake of experimentation, I have written a very very simple module to test the panic system call: each time the panicBin module is unloaded the system panics.
Please do not run the module on a production system, and at least make your file systems read only!
In my system it generates correctly the crash dump, but please note that if you don't have the _dumpdev_ option in /etc/rc.conf you will need to run savecore(8) to manually extract and store the crash out of the swap.

This is my little module code: load and the unload it to crash your system.


```
// panic.c

#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/systm.h>

static int 
panicEventHandler(   
        struct module *st_module, 
        int event,                      
        void* argv                      
    )
{

        int error = 0;

        switch( event ){
                case MOD_LOAD:  
                  uprintf("\nLoading the panic module: unload to panic the system!\n");
                        break;

                case MOD_UNLOAD:  
                  uprintf("\nPanic-ing the system!!!!\n");
                  panic("Panic Module!");
                  break;

                default:
                        error = EOPNOTSUPP;     
                        break;
        }

        return error;

}



static moduledata_t panic_module = {
        "panicModule",          
        panicEventHandler,      
        NULL                    
};


DECLARE_MODULE( panicModuleBin,         
                panic_module,           
                SI_SUB_DRIVERS,
                SI_ORDER_MIDDLE
);




// Makefile
KMOD= panicBin
SRCS= panic.c

.include <bsd.kmod.mk>
```


----------



## blitzkrieg (Apr 27, 2012)

Thanks for answer. 
I tried to compile your module but it doesn't compile with the following errors.


```
In file included from panic.c:5:
/usr/include/sys/kernel.h:373: error: expected specifier-qualifier-list before 'TAILQ_ENTRY'
In file included from panic.c:7:
/usr/include/sys/systm.h:240: error: expected declaration specifiers or '...' before 'uintfptr_t'
/usr/include/sys/systm.h:246: error: expected declaration specifiers or '...' before 'uintfptr_t'
In file included from panic.c:7:
/usr/include/sys/systm.h:312: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splbio'
/usr/include/sys/systm.h:313: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splcam'
/usr/include/sys/systm.h:314: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splclock'
/usr/include/sys/systm.h:315: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splhigh'
/usr/include/sys/systm.h:316: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splimp'
/usr/include/sys/systm.h:317: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splnet'
/usr/include/sys/systm.h:318: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsoftcam'
/usr/include/sys/systm.h:319: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsoftclock'
/usr/include/sys/systm.h:320: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsofttty'
/usr/include/sys/systm.h:321: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsoftvm'
/usr/include/sys/systm.h:322: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splsofttq'
/usr/include/sys/systm.h:323: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splstatclock'
/usr/include/sys/systm.h:324: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'spltty'
/usr/include/sys/systm.h:325: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'splvm'
/usr/include/sys/systm.h:326: error: expected ')' before 'ipl'
panic.c: In function 'panicEventHandler':
panic.c:30: error: 'EOPNOTSUPP' undeclared (first use in this function)
panic.c:30: error: (Each undeclared identifier is reported only once
panic.c:30: error: for each function it appears in.)
panic.c: At top level:
panic.c:51: warning: data definition has no type or storage class
panic.c:51: warning: parameter names (without types) in function declaration
panic.c:57: warning: data definition has no type or storage class
panic.c:57: error: 'panicBin' undeclared here (not in a function)
panic.c:58: error: expected ',' or ';' before 'SRCS'
```


----------



## fluca1978 (May 3, 2012)

How are you compiling the module? It requires the Makefile I wrote to compile.


----------



## blitzkrieg (May 4, 2012)

Thanks for the answer. I'am newbie in compiling so *I* can't detect that your last post has a Makefile in it. I compiled your module and it works great.


----------

