Printk
is a printf-like function of the Linux kernel interface for formatting and writing kernel log entries. Since the C standard library is not available in kernel mode, provides for general-purpose output in the kernel. Due to limitations of the kernel design, the function is often used to aid debugging kernel mode software.
printk can be called from anywhere in the kernel except during early stages of the boot process, before the system console is initialized. The alternative function early_printk is implemented on some architectures and is used identically to printk but during the early stages of the boot process.Use
has the same syntax as, but somewhat different semantics. Like, accepts a format c-string argument and a list of value arguments. Both format text based on the input parameters and with significantly similar behavior, but there are also significant differences. The function prototype is:int printk;
The features different from
printf are described below.Log level
printk allows a caller to specify a log level the type and importance of the message being sent. The level is specified by prepending text that identifies a log level. Typically the text is prepended via C's string literal concatenation and via one of the macros designed for this purpose. For example, a message could be logged at the informational level as:printk
The text specifying the log level consists of the ASCII SOH character followed by a digit that identifies the log level or the letter 'c' to indicate the message is a continuation of the previous message. The following table lists each log level with its canonical meaning.
| 0 | KERN_EMERG | An emergency condition; the system is probably dead |
| 1 | KERN_ALERT | A problem that requires immediate attention |
| 2 | KERN_CRIT | A critical condition |
| 3 | KERN_ERR | An error |
| 4 | KERN_WARNING | A warning |
| 5 | KERN_NOTICE | A normal, but perhaps noteworthy, condition |
| 6 | KERN_INFO | An informational message |
| 7 | KERN_DEBUG | A debug message |
When no log level is specified, the entry is logged as the default level which is typically, but can be set, such as via the boot argument.
Log levels are defined in header file. Which log levels are printed is configured using the sysctl file.
Pointer formats
The%p format specifier which is supported by printf, is extended with additional formatting modes. For example, requesting to print a using %pISpc formats an IPv4/v6 address and port in a human-friendly format such as or.No floating point support
Whileprintf supports formatting floating point numbers, printk does not, since the Linux kernel does not allow the use floating-point numbers in kernel code.Implementation
The function tries to lock the semaphore controlling access to the Linux system console. If it succeeds, the output is logged and the console drivers are called. If it is not possible to acquire the semaphore the output is placed into the log buffer, and the current holder of the console semaphore will notice the new output when they release the console semaphore and will send the buffered output to the console before releasing the semaphore.One effect of this deferred printing is that code which calls
printk and then changes the log levels to be printed may break. This is because the log level to be printed is inspected when the actual printing occurs.