How to do it...

Above all, debugging the Linux kernel remains a manual process, and the most important developer tool is the ability to print debug messages.

The kernel uses the printk function, which is very similar syntactically to the printf function call from standard C libraries, with the addition of an optional log level. The allowed formats are documented in the kernel source under Documentation/printk-formats.txt.

The printk functionality needs to be compiled into the kernel with the CONFIG_PRINTK configuration variable. You can also configure the Linux kernel to prepend a precise timestamp to every message with the CONFIG_PRINTK_TIME configuration variable, or even better, with the printk.time kernel command-line argument or through the sysfs under /sys/module/printk/parameters.

The sysfs is a pseudo filesystem where the Linux kernel exposes device information to user space.

Usually, all kernels contain printk support, and the Wandboard kernel does too, although it is commonly removed on production kernels for small embedded systems.

The printk function can be used in any context: interrupt, non-maskable interrupt (NMI), or scheduler. Note that using it inside interrupt context is not recommended.

A useful debug statement to be used during development could be the following:

printk(KERN_INFO "[%s:%d] %pf -> var1: %d var2: %d
", 
__FUNCTION__, __LINE__, __builtin_return_address(0), var1,
var2);

The first thing to note is that there is no comma between the log-level macro and the print format. We then print the function and line where the debug statement is placed and then the parent function. Finally, we print the variables we are actually interested in.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset