Chapter 6. Common Routines
Prev |  | Next

## Chapter 6. Common Routines

## `printk()` `include/linux/kernel.h`

`printk()` feeds kernel messages to the console, dmesg, and the syslog daemon. It is useful for debugging and reporting errors, and can be used inside interrupt context, but use with caution: a machine which has its console flooded with printk messages is unusable. It uses a format string mostly compatible with ANSI C printf, and C string concatenation to give it a first "priority" argument:

    printk(KERN_INFO "i = %u\n", i);

See `include/linux/kernel.h`; for other KERN_ values; these are interpreted by syslog as the level. Special case: for printing an IP address use

    __u32 ipaddress;
    printk(KERN_INFO "my ip: %d.%d.%d.%d\n", NIPQUAD(ipaddress));

`printk()` internally uses a 1K buffer and does not catch overruns. Make sure that will be enough.

### Note

You will know when you are a real kernel hacker when you start typoing printf as printk in your user programs :)

### Note

Another sidenote: the original Unix Version 6 sources had a comment on top of its printf function: "Printf should not be used for chit-chat". You should follow that advice.

Prev |  | Next
Chapter 5. Recipes for Deadlock | Home | `copy_[to/from]_user()` / `get_user()` / `put_user()` `include/asm/uaccess.h`