Linux Device Driver example for dump_stack() to print the stack trace of module loading

One of the useful options in debugging is to print the call trace/stack trace. Linux kernel provides a function to print the stack trace: dump_stack(). The dump_stack function produces a stack trace much like panic and oops, but causes no problems and we return to the normal control flow.

Calling dump_stack() function will print the stack trace at that point.

Code:

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

static int myinit(void)
{
 pr_info("dump_stack myinit\n");
 dump_stack();
 pr_info("dump_stack after\n");
 return 0;
}

static void myexit(void)
{
 pr_info("panic myexit\n");
}

module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

Output:

Related Post