Printk

From Wikipedia, the free encyclopedia

printk is a printf-like function of the Linux kernel interface for formatting and writing kernel log entries.[1] Since the C standard library (which contains the ubiquitous printf-like functions) is not available in kernel mode, printk provides for general-purpose output in the kernel.[2] Due to limitations of the kernel design, the function is often used to aid debugging kernel mode software.[1]

printk can be called from anywhere in the kernel except during early stages of the boot process, before the system console is initialized.[3] 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.[3]

Log level

printk has the same syntax as printf, but somewhat different semantics. Like printf, printk accepts a format C-string argument and a list of value arguments.[1] Both format text based on the input parameters and with significantly similar behavior, but there are also significant differences.[1] The printk function prototype (which matches that of printf) is:

int printk(const char* format, ...);

The features different from printf are described below.

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:[1]

printk(KERN_INFO "Message: %s", arg)

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.[1][4] The following table lists each log level with its canonical meaning.[3]

0KERN_EMERGAn emergency condition; the system is probably dead
1KERN_ALERTA problem that requires immediate attention
2KERN_CRITA critical condition
3KERN_ERRAn error
4KERN_WARNINGA warning
5KERN_NOTICEA normal, but perhaps noteworthy, condition
6KERN_INFOAn informational message
7KERN_DEBUGA debug message

When no log level is specified, the entry is logged as the default level which is typically KERN_WARNING,[1] but can be set, such as via the loglevel= boot argument.[5]

Log levels are defined in header file <linux/kern_levels.h>.[4] Which log levels are printed is configured using the sysctl file /proc/sys/kernel/printk.[1]

Pointer formats

The %p format specifier which is supported by printf, is extended with additional formatting modes. For example, requesting to print a struct sockaddr * using %pISpc formats an IPv4/v6 address and port in a human-friendly format such as 1.2.3.4:12345 or [1:2:3:4:5:6:7:8]:12345.[6]

No floating point support

While printf supports formatting floating point numbers, printk does not,[6] since the Linux kernel does not allow the use floating-point numbers in kernel code. (The use of floating-point numbers requires saving and restoring additional registers and status flags compared to integer-only code.)[7]

Implementation

References

Related Articles

Wikiwand AI