Printk
   HOME

TheInfoList



OR:

printk is a C function from the
Linux kernel interface The Linux kernel provides several interfaces to user-space applications that are used for different purposes and that have different properties by design. There are two types of application programming interface (API) in the Linux kernel that ...
that prints messages to the kernel log. It accepts a string parameter called the
format string The printf format string is a control parameter used by a class of functions in the input/output libraries of C and many other programming languages. The string is written in a simple template language: characters are usually copied literal ...
, which specifies a method for rendering an arbitrary number of varied data type parameter(s) into a string. The string is then printed to the kernel log. It provides a printf-like abstraction and its parsing of the format string and arguments behave similarly to printf. It acts as a debugging tool for kernel programmers who need this function for logging messages from the kernel. The printk function prototype is:
int printk(const char *fmt, ...);
C standard library The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard.ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C ยง7'' Starting from the original ANSI C standard, it wa ...
and its printf function is unavailable in kernel mode, hence the need for printk.


Differences from printf

The function printk is based on printf, but cannot always be used in the same way that printf is used.


Log levels

printk allows a caller to specify the type and importance of the message being sent. This specifier is called the log level. The log level specifies the type of message being sent to the kernel message log. The log level is specified by prepending (using C's
string literal concatenation A string literal or anonymous string is a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally " bracketed delimiters", as in x = "foo", where "foo" is a string ...
) a string describing the log level to the start of the message to be produced. For example, a message could be produced at the KERN_INFO using the following: printk(KERN_INFO "Message: %s\n", arg); The string specifying the log level consists of the ASCII start of header character followed by a digit describing the log level or the character 'c' to indicate the message is a continuation of the previous message. The following log levels, along with their interpretations, are given below. When a log level is not specified, the default log level is KERN_WARNING, unless a different default has been set in the kernel itself. Log levels are defined in . Which log levels are printed is configured using the sysctl file /proc/sys/kernel/printk.


Pointer formats

The %p format specifier (used for printing pointers in printf) is extended to add additional formatting modes, for example, requesting to print a struct sockaddr * using %pISpc would print an IPv4/v6 address and port in a human-friendly format (e.g. "1.2.3.4:12345" or " :2:3:4:5:6:7:812345").


No floating point support

While printf support output of floating point numbers, printk does not, since the Linux kernel does not use floating point numbers within the kernel.


Description

The function tries to lock the
semaphore Semaphore (; ) is the use of an apparatus to create a visual signal transmitted over distance. A semaphore can be performed with devices including: fire, lights, flags, sunlight, and moving arms. Semaphores can be used for telegraphy when arra ...
controlling access to the 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. The function printk can be called from anywhere in the kernel except during the very early stages of the kernel boot process, when the system console is not initialised. The alternative function early_printk is implemented on some architectures and is used identically to printk during the early stages of the boot process.


References

{{Reflist


External links


Format Reference
Linux kernel Operating system APIs