Книга: Embedded Linux Primer: A Practical, Real-World Approach

14.3.6. printk Debugging

14.3.6. printk Debugging

Debugging kernel and device driver code using printk is a popular technique, mostly because printk has evolved into a very robust method. You can call printk from almost any context, including from interrupt handlers. printk is the kernel's version of the familiar printf() C library function. printk is defined in .../kernel/printk.c.

It is important to understand the limitations of using printk for debugging. First, printk requires a console device. Moreover, although the console device is configured as early as possible during kernel initialization, there are many calls to printk before the console device has been initialized. We present a method to cope with this limitation later, in Section 14.5, "When It Doesn't Boot."

The printk function allows the addition of a string marker that identifies the level of severity of a given message. The header file .../include/linux/kernel.h defines eight levels:

#define     KERN_EMERG    "<0>" /* system is unusable */
#define     KERN_ALERT    "<1>" /* action must be taken immediately */
#define     KERN_CRIT     "<2>" /* critical conditions */
#define     KERN_ERR      "<3>" /* error conditions */
#define     KERN_WARNING  "<4>" /* warning conditions */
#define     KERN_NOTICE   "<5>" /* normal but significant condition */
#define     KERN_INFO     "<6>" /* informational */
#define     KERN_DEBUG    "<7>" /* debug-level messages */

A simple printk message might look like this:

printk("foo() entered w/ %sn", arg);

If the severity string is omitted, the kernel assigns a default severity level, which is defined in printk.c. In recent kernels, this is set at severity level 4, KERN_WARNING. Specifying printk with a severity level might look something like this:

printk(KERN_CRIT "vmalloc failed in foo()n");

By default, all printk messages below a predefined loglevel are displayed on the system console device. The default loglevel is defined in printk.c. In recent Linux kernels, it has the value 7. This means that any printk message that is greater in importance than KERN_DEBUG will be displayed on the console.

You can set the default kernel loglevel in a variety of ways. At boot time, you can specify the default loglevel on your target board by passing the appropriate kernel command line parameters to the kernel at boot time. Three kernel command line options defined in main.c affect the default loglevel:

• debug Sets the console loglevel to 10

• quiet Sets the console loglevel to 4

• loglevel= Sets the console loglevel to your choice of value

Using debug effectively displays every printk message. Using quiet displays all printk messages of severity KERN_ERR or higher.

printk messages can be logged to files on your target or via the network. Use klogd (kernel log daemon) and syslogd (system log daemon) to control the logging behavior of printk messages. These popular utilities are described in man pages and many Linux references, and are not described here.

Оглавление книги

Генерация: 0.914. Запросов К БД/Cache: 3 / 0
Вверх Вниз